def test_get_result_index(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu = ops.relu(param, name="relu") function = Function(relu, [param], "TestFunction") assert len(function.outputs) == 1 assert function.get_result_index(function.outputs[0]) == 0
def test_parameter_index_invalid(): shape1 = PartialShape([1]) param1 = ops.parameter(shape1, dtype=np.float32, name="data1") relu = ops.relu(param1, name="relu") function = Function(relu, [param1], "TestFunction") shape2 = PartialShape([2]) param2 = ops.parameter(shape2, dtype=np.float32, name="data2") assert function.get_parameter_index(param2) == -1
def test_compress_model_transformation(): node_constant = ov.opset8.constant(np.array([[0.0, 0.1, -0.1], [-2.5, 2.5, 3.0]], dtype=np.float32)) node_ceil = ov.opset8.ceiling(node_constant) func = Function(node_ceil, [], "TestFunction") assert func.get_ordered_ops()[0].get_element_type().get_type_name() == "f32" compress_model_transformation(func) assert func is not None assert func.get_ordered_ops()[0].get_element_type().get_type_name() == "f16"
def test_function_add_output_incorrect_tensor_name(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu1 = ops.relu(param, name="relu1") relu1.get_output_tensor(0).set_names({"relu_t1"}) relu2 = ops.relu(relu1, name="relu2") function = Function(relu2, [param], "TestFunction") assert len(function.get_results()) == 1 with pytest.raises(RuntimeError) as e: function.add_outputs("relu_t") assert "Tensor name relu_t was not found." in str(e.value)
def test_add_outputs_incorrect_outputs_list(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu1 = ops.relu(param, name="relu1") relu1.get_output_tensor(0).set_names({"relu_t1"}) function = Function(relu1, [param], "TestFunction") assert len(function.get_results()) == 1 with pytest.raises(TypeError) as e: function.add_outputs([0, 0]) assert "Incorrect type of a value to add as output at index 0" in str( e.value)
def test_function_add_output_incorrect_idx(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu1 = ops.relu(param, name="relu1") relu1.get_output_tensor(0).set_names({"relu_t1"}) relu2 = ops.relu(relu1, name="relu2") function = Function(relu2, [param], "TestFunction") assert len(function.get_results()) == 1 with pytest.raises(RuntimeError) as e: function.add_outputs(("relu1", 10)) assert "Cannot add output to port 10 operation relu1 has only 1 outputs." in str( e.value)
def test_get_result_index_invalid(): shape1 = PartialShape([1]) param1 = ops.parameter(shape1, dtype=np.float32, name="data1") relu1 = ops.relu(param1, name="relu1") function = Function(relu1, [param1], "TestFunction") shape2 = PartialShape([2]) param2 = ops.parameter(shape2, dtype=np.float32, name="data2") relu2 = ops.relu(param2, name="relu2") invalid_output = relu2.outputs()[0] assert len(function.outputs) == 1 assert function.get_result_index(invalid_output) == -1
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 = Function(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_function_add_output_port(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu1 = ops.relu(param, name="relu1") relu1.get_output_tensor(0).set_names({"relu_t1"}) relu2 = ops.relu(relu1, name="relu2") function = Function(relu2, [param], "TestFunction") assert len(function.get_results()) == 1 new_outs = function.add_outputs(relu1.output(0)) assert len(function.get_results()) == 2 assert len(new_outs) == 1 assert new_outs[0].get_node() == function.outputs[1].get_node() assert new_outs[0].get_index() == function.outputs[1].get_index()
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 = Function(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_function_add_outputs_tensor_name(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu1 = ops.relu(param, name="relu1") relu1.get_output_tensor(0).set_names({"relu_t1"}) assert "relu_t1" in relu1.get_output_tensor(0).names relu2 = ops.relu(relu1, name="relu2") function = Function(relu2, [param], "TestFunction") assert len(function.get_results()) == 1 new_outs = function.add_outputs("relu_t1") assert len(function.get_results()) == 2 assert isinstance(function.outputs[1].get_tensor(), DescriptorTensor) assert "relu_t1" in function.outputs[1].get_tensor().names assert len(new_outs) == 1 assert new_outs[0].get_node() == function.outputs[1].get_node() assert new_outs[0].get_index() == function.outputs[1].get_index()
def create_function_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") func = Function(results=[res], sinks=[node], parameters=[input_data], name="name") return func
def test_core_class(): input_shape = [1, 3, 4, 4] param = ov.parameter(input_shape, np.float32, name="parameter") relu = ov.relu(param, name="relu") func = Function([relu], [param], "test") func.get_ordered_ops()[2].friendly_name = "friendly" core = Core() model = core.compile_model(func, "CPU", {}) request = model.create_infer_request() input_data = np.random.rand(*input_shape).astype(np.float32) - 0.5 expected_output = np.maximum(0.0, input_data) input_tensor = Tensor(input_data) results = request.infer({"parameter": input_tensor}) assert np.allclose(results, expected_output)
def test_serialize_pass_v2(): core = Core() xml_path = "./serialized_function.xml" bin_path = "./serialized_function.bin" shape = [100, 100, 2] parameter_a = ov.opset8.parameter(shape, dtype=np.float32, name="A") parameter_b = ov.opset8.parameter(shape, dtype=np.float32, name="B") model = ov.opset8.floor(ov.opset8.minimum(ov.opset8.abs(parameter_a), parameter_b)) func = Function(model, [parameter_a, parameter_b], "Function") serialize(func, xml_path, bin_path) assert func is not None res_func = core.read_model(model=xml_path, weights=bin_path) assert func.get_parameters() == res_func.get_parameters() assert func.get_ordered_ops() == res_func.get_ordered_ops() os.remove(xml_path) os.remove(bin_path)
def test_function_add_outputs_op_name(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu1 = ops.relu(param, name="relu1") relu1.get_output_tensor(0).set_names({"relu_t1"}) relu2 = ops.relu(relu1, name="relu2") function = Function(relu2, [param], "TestFunction") assert len(function.get_results()) == 1 function.add_outputs(("relu1", 0)) assert len(function.get_results()) == 2
def test_replace_parameter(): shape1 = PartialShape([1]) param1 = ops.parameter(shape1, dtype=np.float32, name="data") shape2 = PartialShape([2]) param2 = ops.parameter(shape2, dtype=np.float32, name="data") relu = ops.relu(param1, name="relu") function = Function(relu, [param1], "TestFunction") param_index = function.get_parameter_index(param1) function.replace_parameter(param_index, param2) assert function.get_parameter_index(param2) == param_index assert function.get_parameter_index(param1) == -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 = Function(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 get_test_function(): param = ov.opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") relu = ov.opset8.relu(param) res = ov.opset8.result(relu, name="result") return Function([res], [param], "test")
def test_parameter_index(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu = ops.relu(param, name="relu") function = Function(relu, [param], "TestFunction") assert function.get_parameter_index(param) == 0