def test_get_batch_CHWN(): param1 = ops.parameter(Shape([3, 1, 3, 4]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([3, 1, 3, 4]), dtype=np.float32, name="data2") param3 = ops.parameter(Shape([3, 1, 3, 4]), dtype=np.float32, name="data3") add = ops.add(param1, param2) add2 = ops.add(add, param3) func = Model(add2, [param1, param2, param3], "TestFunction") param = func.get_parameters()[0] param.set_layout(Layout("CHWN")) assert get_batch(func) == 4
def create_simple_if_with_two_outputs(condition_val): condition = ov.constant(condition_val, dtype=np.bool) # then_body X_t = ov.parameter([], np.float32, "X") Y_t = ov.parameter([], np.float32, "Y") Z_t = ov.parameter([], np.float32, "Z") add_t = ov.add(X_t, Y_t) mul_t = ov.multiply(Y_t, Z_t) then_body_res_1 = ov.result(add_t) then_body_res_2 = ov.result(mul_t) then_body = GraphBody([X_t, Y_t, Z_t], [then_body_res_1, then_body_res_2]) then_body_inputs = [ TensorIteratorInvariantInputDesc(1, 0), TensorIteratorInvariantInputDesc(2, 1), TensorIteratorInvariantInputDesc(3, 2) ] then_body_outputs = [ TensorIteratorBodyOutputDesc(0, 0), TensorIteratorBodyOutputDesc(1, 1) ] # else_body X_e = ov.parameter([], np.float32, "X") Z_e = ov.parameter([], np.float32, "Z") W_e = ov.parameter([], np.float32, "W") add_e = ov.add(X_e, W_e) pow_e = ov.power(W_e, Z_e) else_body_res_1 = ov.result(add_e) else_body_res_2 = ov.result(pow_e) else_body = GraphBody([X_e, Z_e, W_e], [else_body_res_1, else_body_res_2]) else_body_inputs = [ TensorIteratorInvariantInputDesc(1, 0), TensorIteratorInvariantInputDesc(3, 1), TensorIteratorInvariantInputDesc(4, 2) ] else_body_outputs = [ TensorIteratorBodyOutputDesc(0, 0), TensorIteratorBodyOutputDesc(1, 1) ] X = ov.constant(15.0, dtype=np.float32) Y = ov.constant(-5.0, dtype=np.float32) Z = ov.constant(4.0, dtype=np.float32) W = ov.constant(2.0, dtype=np.float32) if_node = ov.if_op(condition, [X, Y, Z, W], (then_body, else_body), (then_body_inputs, else_body_inputs), (then_body_outputs, else_body_outputs)) return if_node
def custom_converter(node: NodeContext): nonlocal invoked invoked = True def check_attribute(context, name, expected_type, expected_value): assert context.has_attribute(name) attribute = context.get_attribute(name) assert type(attribute) == expected_type assert attribute == expected_value check_attribute(node, "attribute_i32", int, 10) check_attribute(node, "attribute_i64", int, 10) check_attribute(node, "attribute_str", str, "string") check_attribute(node, "attribute_f32", float, 10.) check_attribute(node, "attribute_f64", float, 10.) check_attribute(node, "attribute_bool", int, 1) check_attribute(node, "attribute_type", int, 6) check_attribute(node, "attribute_list_i32", list, [1, 2, 3]) check_attribute(node, "attribute_list_i64", list, [1, 2, 3]) check_attribute(node, "attribute_list_str", list, ["a", "b", "c"]) check_attribute(node, "attribute_list_f32", list, [1., 2., 3.]) check_attribute(node, "attribute_list_f64", list, [1., 2., 3.]) check_attribute(node, "attribute_list_bool", list, [1, 0, 1]) check_attribute(node, "attribute_list_type", list, [6, 1]) a = node.get_input(0) b = node.get_input(1) add = ops.add(a, b) return [add.output(0)]
def test_low_latency2(): X = opset8.parameter(Shape([32, 40, 10]), np.float32, "X") Y = opset8.parameter(Shape([32, 40, 10]), np.float32, "Y") M = opset8.parameter(Shape([32, 2, 10]), np.float32, "M") X_i = opset8.parameter(Shape([32, 2, 10]), np.float32, "X_i") Y_i = opset8.parameter(Shape([32, 2, 10]), np.float32, "Y_i") M_body = opset8.parameter(Shape([32, 2, 10]), np.float32, "M_body") sum = opset8.add(X_i, Y_i) Zo = opset8.multiply(sum, M_body) body = Model([Zo], [X_i, Y_i, M_body], "body_function") ti = opset8.tensor_iterator() ti.set_body(body) ti.set_sliced_input(X_i, X.output(0), 0, 2, 2, 39, 1) ti.set_sliced_input(Y_i, Y.output(0), 0, 2, 2, -1, 1) ti.set_invariant_input(M_body, M.output(0)) out0 = ti.get_iter_value(Zo.output(0), -1) out1 = ti.get_concatenated_slices(Zo.output(0), 0, 2, 2, 39, 1) result0 = opset8.result(out0) result1 = opset8.result(out1) model = Model([result0, result1], [X, Y, M]) m = Manager() m.register_pass(LowLatency2()) m.run_passes(model) # TODO: create TI which will be transformed by LowLatency2 assert count_ops(model, "TensorIterator") == [1]
def test_batched_tensors(device): core = Core() # TODO: remove when plugins will support set_input_tensors core.register_plugin("openvino_template_plugin", "TEMPLATE") batch = 4 one_shape = [1, 2, 2, 2] one_shape_size = np.prod(one_shape) batch_shape = [batch, 2, 2, 2] data1 = ops.parameter(batch_shape, np.float32) data1.set_friendly_name("input0") data1.get_output_tensor(0).set_names({"tensor_input0"}) data1.set_layout(Layout("N...")) constant = ops.constant([1], np.float32) op1 = ops.add(data1, constant) op1.set_friendly_name("Add0") res1 = ops.result(op1) res1.set_friendly_name("Result0") res1.get_output_tensor(0).set_names({"tensor_output0"}) model = Model([res1], [data1]) compiled = core.compile_model(model, "TEMPLATE") req = compiled.create_infer_request() # Allocate 8 chunks, set 'user tensors' to 0, 2, 4, 6 chunks buffer = np.zeros([batch * 2, *batch_shape[1:]], dtype=np.float32) tensors = [] for i in range(batch): # non contiguous memory (i*2) tensors.append( Tensor(np.expand_dims(buffer[i * 2], 0), shared_memory=True)) req.set_input_tensors(tensors) with pytest.raises(RuntimeError) as e: req.get_tensor("tensor_input0") assert "get_tensor shall not be used together with batched set_tensors/set_input_tensors" in str( e.value) actual_tensor = req.get_tensor("tensor_output0") actual = actual_tensor.data for test_num in range(0, 5): for i in range(0, batch): tensors[i].data[:] = test_num + 10 req.infer() # Adds '1' to each element # Reference values for each batch: _tmp = np.array([test_num + 11] * one_shape_size, dtype=np.float32).reshape([2, 2, 2]) for j in range(0, batch): assert np.array_equal(actual[j], _tmp)
def custom_converter(node: NodeContext): nonlocal invoked invoked = True a = node.get_input(0) b = node.get_input(1) add = ops.add(a, b) return [add.output(0)]
def simple_if(condition_val): condition = ov.constant(condition_val, dtype=np.bool) # then_body X_t = ov.parameter([2], np.float32, "X") Y_t = ov.parameter([2], np.float32, "Y") then_mul = ov.multiply(X_t, Y_t) then_body_res_1 = ov.result(then_mul) then_body = GraphBody([X_t, Y_t], [then_body_res_1]) then_body_inputs = [ TensorIteratorInvariantInputDesc(1, 0), TensorIteratorInvariantInputDesc(2, 1) ] then_body_outputs = [TensorIteratorBodyOutputDesc(0, 0)] # else_body X_e = ov.parameter([2], np.float32, "X") Y_e = ov.parameter([2], np.float32, "Y") add_e = ov.add(X_e, Y_e) else_body_res_1 = ov.result(add_e) else_body = GraphBody([X_e, Y_e], [else_body_res_1]) else_body_inputs = [ TensorIteratorInvariantInputDesc(1, 0), TensorIteratorInvariantInputDesc(2, 1) ] else_body_outputs = [TensorIteratorBodyOutputDesc(0, 0)] X = ov.constant([3, 4], dtype=np.float32) Y = ov.constant([2, 1], dtype=np.float32) if_node = ov.if_op(condition, [X, Y], (then_body, else_body), (then_body_inputs, else_body_inputs), (then_body_outputs, else_body_outputs)) relu = ov.relu(if_node) return relu
def test_node_input_values(): shapes = [Shape([3]), Shape([3])] data1 = np.array([1, 2, 3]) data2 = np.array([3, 2, 1]) node = ops.add(data1, data2) assert node.get_input_size() == 2 assert np.equal( [input_node.get_shape() for input_node in node.input_values()], shapes).all() assert np.equal([ node.input_value(i).get_shape() for i in range(node.get_input_size()) ], shapes).all() assert np.allclose([ input_node.get_node().get_vector() for input_node in node.input_values() ], [data1, data2]) assert np.allclose([ node.input_value(i).get_node().get_vector() for i in range(node.get_input_size()) ], [data1, data2])
def custom_converter(node: NodeContext): nonlocal invoked invoked = True def check_attribute(context, name, default_value): assert not context.has_attribute(name) attribute = context.get_attribute(name, default_value) assert type(attribute) == type(default_value) if isinstance(attribute, np.ndarray): assert np.all(attribute == default_value) else: assert attribute == default_value check_attribute(node, "attribute_i32", np.int32(5)) check_attribute(node, "attribute_i64", np.int64(5)) check_attribute(node, "attribute_str", "abc") check_attribute(node, "attribute_f32", np.float32(5)) check_attribute(node, "attribute_f64", np.float64(5)) check_attribute(node, "attribute_bool", np.bool(False)) check_attribute(node, "attribute_type", onnx.TensorProto.FLOAT) check_attribute(node, "attribute_list_i32", np.array([4, 5, 6], dtype=np.int32)) check_attribute(node, "attribute_list_i64", np.array([4, 5, 6], dtype=np.int64)) check_attribute(node, "attribute_list_str", np.array(["d", "e", "f"], dtype=np.str)) check_attribute(node, "attribute_list_f32", np.array([4, 5, 6], dtype=np.float)) check_attribute(node, "attribute_list_f64", np.array([4, 5, 6], dtype=np.float64)) check_attribute(node, "attribute_list_bool", np.array([True, False, True], dtype=np.bool)) check_attribute(node, "attribute_list_type", np.array([onnx.TensorProto.INT32, onnx.TensorProto.FLOAT])) a = node.get_input(0) b = node.get_input(1) add = ops.add(a, b) return [add.output(0)]
def test_ports_as_inputs(device): input_shape = [2, 2] param_a = ops.parameter(input_shape, np.float32) param_b = ops.parameter(input_shape, np.float32) model = Model(ops.add(param_a, param_b), [param_a, param_b]) core = Core() compiled = core.compile_model(model, device) request = compiled.create_infer_request() arr_1 = np.array([[1, 2], [3, 4]], dtype=np.float32) arr_2 = np.array([[3, 4], [1, 2]], dtype=np.float32) tensor1 = Tensor(arr_1) tensor2 = Tensor(arr_2) res = request.infer({ compiled.inputs[0]: tensor1, compiled.inputs[1]: tensor2 }) assert np.array_equal(res[compiled.outputs[0]], tensor1.data + tensor2.data) res = request.infer({ request.model_inputs[0]: tensor1, request.model_inputs[1]: tensor2 }) assert np.array_equal(res[request.model_outputs[0]], tensor1.data + tensor2.data)
def simple_if(condition_val): condition = ov.constant(condition_val, dtype=np.bool) # then_body X_t = ov.parameter([2], np.float32, "X") Y_t = ov.parameter([2], np.float32, "Y") then_mul = ov.multiply(X_t, Y_t) then_body_res_1 = ov.result(then_mul) then_body = Model([then_body_res_1], [X_t, Y_t], "then_body_function") # else_body X_e = ov.parameter([2], np.float32, "X") Y_e = ov.parameter([2], np.float32, "Y") add_e = ov.add(X_e, Y_e) else_body_res_1 = ov.result(add_e) else_body = Model([else_body_res_1], [X_e, Y_e], "else_body_function") X = ov.constant([3, 4], dtype=np.float32) Y = ov.constant([2, 1], dtype=np.float32) if_node = ov.if_op(condition) if_node.set_then_body(then_body) if_node.set_else_body(else_body) if_node.set_input(X.output(0), X_t, X_e) if_node.set_input(Y.output(0), Y_t, Y_e) if_res = if_node.set_output(then_body_res_1, else_body_res_1) relu = ov.relu(if_res) return relu
def test_simple_tensor_iterator(): X = ov.parameter(Shape([32, 40, 10]), np.float32, "X") Y = ov.parameter(Shape([32, 40, 10]), np.float32, "Y") M = ov.parameter(Shape([32, 2, 10]), np.float32, "M") X_i = ov.parameter(Shape([32, 2, 10]), np.float32, "X_i") Y_i = ov.parameter(Shape([32, 2, 10]), np.float32, "Y_i") M_body = ov.parameter(Shape([32, 2, 10]), np.float32, "M_body") sum = ov.add(X_i, Y_i) Zo = ov.multiply(sum, M_body) body = Model([Zo], [X_i, Y_i, M_body], "body_function") ti = ov.tensor_iterator() ti.set_body(body) ti.set_sliced_input(X_i, X.output(0), 0, 2, 2, 39, 1) ti.set_sliced_input(Y_i, Y.output(0), 0, 2, 2, -1, 1) ti.set_invariant_input(M_body, M.output(0)) out0 = ti.get_iter_value(Zo.output(0), -1) out1 = ti.get_concatenated_slices(Zo.output(0), 0, 2, 2, 39, 1) result0 = ov.result(out0) result1 = ov.result(out1) out0_shape = [32, 2, 10] out1_shape = [32, 40, 10] assert list(result0.get_output_shape(0)) == out0_shape assert list(result1.get_output_shape(0)) == out1_shape assert list(ti.get_output_shape(0)) == out0_shape assert list(ti.get_output_shape(1)) == out1_shape
def custom_converter(node: NodeContext): nonlocal invoked invoked = True def check_attribute(context, name, expected_value, dtype): attribute = context.get_attribute(name, dtype=dtype) if isinstance(attribute, list): assert type(attribute[0]) == dtype else: assert type(attribute) == dtype assert attribute == expected_value check_attribute(node, "attribute_i32", 10, float) check_attribute(node, "attribute_i64", 10, float) check_attribute(node, "attribute_str", "string", np.str) check_attribute(node, "attribute_f32", 10, int) check_attribute(node, "attribute_f64", 10, int) check_attribute(node, "attribute_bool", True, bool) check_attribute(node, "attribute_type", Type.i32, Type) check_attribute(node, "attribute_list_i32", [1., 2., 3.], float) check_attribute(node, "attribute_list_i64", [1., 2., 3.], float) check_attribute(node, "attribute_list_str", ["a", "b", "c"], np.str) check_attribute(node, "attribute_list_f32", [1, 2, 3], int) check_attribute(node, "attribute_list_f64", [1, 2, 3], int) check_attribute(node, "attribute_list_bool", [True, False, True], bool) check_attribute(node, "attribute_list_type", [Type.i32, Type.f32], Type) a = node.get_input(0) b = node.get_input(1) add = ops.add(a, b) return [add.output(0)]
def create_model_with_memory(input_shape, data_type): input_data = ops.parameter(input_shape, name="input_data", dtype=data_type) rv = ops.read_value(input_data, "var_id_667") add = ops.add(rv, input_data, name="MemoryAdd") node = ops.assign(add, "var_id_667") res = ops.result(add, "res") model = Model(results=[res], sinks=[node], parameters=[input_data], name="name") return model
def test_get_batch(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Model(add, [param1, param2], "TestFunction") param = func.get_parameters()[0] param.set_layout(Layout("NC")) assert get_batch(func) == 2
def test_batched_tensors(device): batch = 4 one_shape = Shape([1, 2, 2, 2]) batch_shape = Shape([batch, 2, 2, 2]) one_shape_size = np.prod(one_shape) core = Core() core.register_plugin("openvino_template_plugin", "TEMPLATE") data1 = ops.parameter(batch_shape, np.float32) data1.set_friendly_name("input0") data1.get_output_tensor(0).set_names({"tensor_input0"}) data1.set_layout(Layout("N...")) constant = ops.constant([1], np.float32) op1 = ops.add(data1, constant) op1.set_friendly_name("Add0") res1 = ops.result(op1) res1.set_friendly_name("Result0") res1.get_output_tensor(0).set_names({"tensor_output0"}) model = Model([res1], [data1]) compiled = core.compile_model(model, "TEMPLATE") buffer = np.zeros([one_shape_size * batch * 2], dtype=np.float32) req = compiled.create_infer_request() tensors = [] for i in range(0, batch): _start = i * one_shape_size * 2 # Use of special constructor for Tensor. # It creates a Tensor from pointer, thus it requires only # one element from original buffer, and shape to "crop". tensor = Tensor(buffer[_start:(_start + 1)], one_shape) tensors.append(tensor) req.set_input_tensors(tensors) # using list overload! actual_tensor = req.get_tensor("tensor_output0") actual = actual_tensor.data for test_num in range(0, 5): for i in range(0, batch): tensors[i].data[:] = test_num + 10 req.infer() # Adds '1' to each element # Reference values for each batch: _tmp = np.array([test_num + 11] * one_shape_size, dtype=np.float32).reshape([2, 2, 2]) for j in range(0, batch): assert np.array_equal(actual[j], _tmp)
def test_set_batch_default_batch_size(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Model(add, [param1, param2], "TestFunction") func_param1 = func.get_parameters()[0] func_param1.set_layout(Layout("NC")) set_batch(func) assert func.is_dynamic()
def create_simple_if_with_two_outputs(condition_val): condition = ov.constant(condition_val, dtype=np.bool) # then_body X_t = ov.parameter([], np.float32, "X") Y_t = ov.parameter([], np.float32, "Y") Z_t = ov.parameter([], np.float32, "Z") add_t = ov.add(X_t, Y_t) mul_t = ov.multiply(Y_t, Z_t) then_body_res_1 = ov.result(add_t) then_body_res_2 = ov.result(mul_t) then_body = Model([then_body_res_1, then_body_res_2], [X_t, Y_t, Z_t], "then_body_function") # else_body X_e = ov.parameter([], np.float32, "X") Z_e = ov.parameter([], np.float32, "Z") W_e = ov.parameter([], np.float32, "W") add_e = ov.add(X_e, W_e) pow_e = ov.power(W_e, Z_e) else_body_res_1 = ov.result(add_e) else_body_res_2 = ov.result(pow_e) else_body = Model([else_body_res_1, else_body_res_2], [X_e, Z_e, W_e], "else_body_function") X = ov.constant(15.0, dtype=np.float32) Y = ov.constant(-5.0, dtype=np.float32) Z = ov.constant(4.0, dtype=np.float32) W = ov.constant(2.0, dtype=np.float32) if_node = ov.if_op(condition) if_node.set_then_body(then_body) if_node.set_else_body(else_body) if_node.set_input(X.output(0), X_t, X_e) if_node.set_input(Y.output(0), Y_t, None) if_node.set_input(Z.output(0), Z_t, Z_e) if_node.set_input(W.output(0), None, W_e) if_node.set_output(then_body_res_1, else_body_res_1) if_node.set_output(then_body_res_2, else_body_res_2) return if_node
def test_node_factory_wrapper_add(): shape = [2, 2] dtype = np.int8 parameter_a = ov.parameter(shape, dtype=dtype, name="A") parameter_b = ov.parameter(shape, dtype=dtype, name="B") node = ov.add(parameter_a, parameter_b, name="TestNode") assert node.get_type_name() == "Add" assert node.get_output_size() == 1 assert list(node.get_output_shape(0)) == [2, 2] assert node.friendly_name == "TestNode"
def test_evaluate(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Model(add, [param1, param2], "TestFunction") input1 = np.array([2, 1], dtype=np.float32).reshape(2, 1) input2 = np.array([3, 7], dtype=np.float32).reshape(2, 1) out_tensor = Tensor("float32", Shape([2, 1])) assert func.evaluate([out_tensor], [Tensor(input1), Tensor(input2)]) assert np.allclose(out_tensor.data, np.array([5, 8]).reshape(2, 1))
def test_validate_nodes_and_infer_types(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Model(add, [param1, param2], "TestFunction") invalid_shape = Shape([3, 7]) param3 = ops.parameter(invalid_shape, dtype=np.float32, name="data3") func.replace_parameter(0, param3) with pytest.raises(RuntimeError) as e: func.validate_nodes_and_infer_types() assert "Argument shapes are inconsistent" in str(e.value)
def test_node_input_tensor(): data1 = np.array([[1, 2, 3], [1, 2, 3]]) data2 = np.array([3, 2, 1]) node = ops.add(data1, data2) inputTensor1 = node.get_input_tensor(0) inputTensor2 = node.get_input_tensor(1) assert (isinstance(inputTensor1, DescriptorTensor)) assert (isinstance(inputTensor2, DescriptorTensor)) assert np.equal(inputTensor1.get_shape(), data1.shape).all() assert np.equal(inputTensor2.get_shape(), data2.shape).all()
def create_simple_request_and_inputs(device): input_shape = [2, 2] param_a = ops.parameter(input_shape, np.float32) param_b = ops.parameter(input_shape, np.float32) model = Model(ops.add(param_a, param_b), [param_a, param_b]) core = Core() compiled = core.compile_model(model, device) request = compiled.create_infer_request() arr_1 = np.array([[1, 2], [3, 4]], dtype=np.float32) arr_2 = np.array([[3, 4], [1, 2]], dtype=np.float32) return request, arr_1, arr_2
def test_simple_loop(): X = ov.parameter(Shape([32, 1, 10]), np.float32, "X") Y = ov.parameter(Shape([32, 1, 10]), np.float32, "Y") M = ov.parameter(Shape([32, 1, 10]), np.float32, "M") input_shape = Shape([]) current_iteration = ov.parameter(Shape([1]), np.int64) X_i = ov.parameter(input_shape, np.float32) Y_i = ov.parameter(input_shape, np.float32) M_body = ov.parameter(input_shape, np.float32) bool_val = np.array([1], dtype=np.bool) bool_val[0] = True body_condition = ov.constant(bool_val) trip_count = ov.constant(10, dtype=np.int64) exec_condition = ov.constant(True, dtype=np.bool) sum = ov.add(X_i, Y_i) Zo = ov.multiply(sum, M_body) body = Model([body_condition, Zo], [current_iteration, X_i, Y_i, M_body], "body_function") loop = ov.loop(trip_count, exec_condition) loop.set_function(body) loop.set_invariant_input(X_i, X.output(0)) loop.set_invariant_input(Y_i, Y.output(0)) loop.set_merged_input(M_body, M.output(0), Zo.output(0)) loop.set_special_body_ports([-1, 0]) out0 = loop.get_iter_value(body_condition.output(0), -1) out1 = loop.get_iter_value(Zo.output(0), -1) out2 = loop.get_concatenated_slices(Zo.output(0), 0, 1, 1, -1, 1) result0 = ov.result(out0) result1 = ov.result(out1) result2 = ov.result(out2) out0_shape = [1] out1_shape = [32, 1, 10] out2_shape = [32, 10, 10] assert list(result0.get_output_shape(0)) == out0_shape assert list(result1.get_output_shape(0)) == out1_shape assert list(result2.get_output_shape(0)) == out2_shape assert list(loop.get_output_shape(0)) == out0_shape assert list(loop.get_output_shape(1)) == out1_shape assert list(loop.get_output_shape(2)) == out2_shape
def test_evaluate_invalid_input_shape(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Model(add, [param1, param2], "TestFunction") with pytest.raises(RuntimeError) as e: assert func.evaluate( [Tensor("float32", Shape([2, 1]))], [ Tensor("float32", Shape([3, 1])), Tensor("float32", Shape([3, 1])) ], ) assert "must be compatible with the partial shape: {2,1}" in str(e.value)
def test_set_batch_int(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Model(add, [param1, param2], "TestFunction") func_param1 = func.get_parameters()[0] func_param2 = func.get_parameters()[1] # batch == 2 func_param1.set_layout(Layout("NC")) assert get_batch(func) == 2 # set batch to 1 set_batch(func, 1) assert get_batch(func) == 1 # check if shape of param 1 has changed assert str(func_param1.get_output_shape(0) == {1, 1}) # check if shape of param 2 has not changed assert str(func_param2.get_output_shape(0) == {2, 1})
def test_node_evaluate(): data1 = np.array([3, 2, 3]) data2 = np.array([4, 2, 3]) expected_result = data1 + data2 data1 = np.ascontiguousarray(data1) data2 = np.ascontiguousarray(data2) output = np.array([0, 0, 0]) output = np.ascontiguousarray(output) node = ops.add(data1, data2) inputTensor1 = Tensor(array=data1, shared_memory=True) inputTensor2 = Tensor(array=data2, shared_memory=True) inputsTensorVector = [inputTensor1, inputTensor2] outputTensorVector = [Tensor(array=output, shared_memory=True)] assert node.evaluate(outputTensorVector, inputsTensorVector) is True assert np.equal(outputTensorVector[0].data, expected_result).all()
def test_sink_function_ctor(): input_data = ops.parameter([2, 2], name="input_data", dtype=np.float32) rv = ops.read_value(input_data, "var_id_667") add = ops.add(rv, input_data, name="MemoryAdd") node = ops.assign(add, "var_id_667") res = ops.result(add, "res") function = Model(results=[res], sinks=[node], parameters=[input_data], name="TestFunction") ordered_ops = function.get_ordered_ops() op_types = [op.get_type_name() for op in ordered_ops] assert op_types == ["Parameter", "ReadValue", "Add", "Assign", "Result"] assert len(function.get_ops()) == 5 assert function.get_output_size() == 1 assert function.get_output_op(0).get_type_name() == "Result" assert function.get_output_element_type(0) == input_data.get_element_type() assert list(function.get_output_shape(0)) == [2, 2] assert (function.get_parameters()[0].get_partial_shape()) == PartialShape( [2, 2]) assert len(function.get_parameters()) == 1 assert len(function.get_results()) == 1 assert function.get_friendly_name() == "TestFunction"
def binary_op(op_str, a, b): if op_str == "+": return a + b elif op_str == "Add": return ov.add(a, b) elif op_str == "-": return a - b elif op_str == "Sub": return ov.subtract(a, b) elif op_str == "*": return a * b elif op_str == "Mul": return ov.multiply(a, b) elif op_str == "/": return a / b elif op_str == "Div": return ov.divide(a, b) elif op_str == "Equal": return ov.equal(a, b) elif op_str == "Greater": return ov.greater(a, b) elif op_str == "GreaterEq": return ov.greater_equal(a, b) elif op_str == "Less": return ov.less(a, b) elif op_str == "LessEq": return ov.less_equal(a, b) elif op_str == "Maximum": return ov.maximum(a, b) elif op_str == "Minimum": return ov.minimum(a, b) elif op_str == "NotEqual": return ov.not_equal(a, b) elif op_str == "Power": return ov.power(a, b)
def test_add_with_mul(): element_type = Type.f32 shape = Shape([4]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) C = Parameter(element_type, shape) parameter_list = [A, B, C] function = Model([ov.multiply(ov.add(A, B), C)], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, A, B, C) result = computation( np.array([1, 2, 3, 4], dtype=np.float32), np.array([5, 6, 7, 8], dtype=np.float32), np.array([9, 10, 11, 12], dtype=np.float32), )[0] a_arr = np.array([1, 2, 3, 4], dtype=np.float32) b_arr = np.array([5, 6, 7, 8], dtype=np.float32) c_arr = np.array([9, 10, 11, 12], dtype=np.float32) result_arr_ref = (a_arr + b_arr) * c_arr assert np.allclose(result, result_arr_ref)