From 196d01b952b92a4828c58e976f9687d52d223d06 Mon Sep 17 00:00:00 2001 From: Anastasia Kuporosova Date: Fri, 13 Jan 2023 22:39:17 +0100 Subject: [PATCH] Port changes in python tests to 2022.3 (#15101) * [PyOV] Rewriting properties tests as hardware agnostic (#14684) * [PyOV][Tests] Fix some tests for M1 (#14555) * [PyOV] Make graph tests hardware agnostic - part 1 (#14500) * Halfway done * Prepare part 1 * Minor changes * Minor changes * [PyOV] Make graph tests hardware agnostic - part 3 (#14639) * [PyOV] Make graph tests hardware agnostic - part 2 (#14519) * [PyOV] Make graph tests hardware agnostic - part 4 (#14705) * [PyOV] Make graph tests hardware agnostic - part 5 (#14743) * merge conflict resolve Co-authored-by: Jan Iwaszkiewicz Co-authored-by: Przemyslaw Wysocki --- .../src/compatibility/ngraph/opset4/ops.py | 6 +- .../python/src/openvino/runtime/opset4/ops.py | 6 +- src/bindings/python/tests/__init__.py | 2 - src/bindings/python/tests/conftest.py | 8 - src/bindings/python/tests/runtime.py | 2 +- .../tests/test_graph/test_adaptive_pool.py | 69 -- .../python/tests/test_graph/test_any.py | 94 +-- .../python/tests/test_graph/test_basic.py | 210 ++--- .../tests/test_graph/test_convolution.py | 212 +---- .../python/tests/test_graph/test_create_op.py | 147 ++-- .../tests/test_graph/test_data_movement.py | 182 +---- .../python/tests/test_graph/test_dft.py | 121 +-- .../python/tests/test_graph/test_einsum.py | 38 +- .../python/tests/test_graph/test_eye.py | 15 - .../python/tests/test_graph/test_gather.py | 95 +-- .../python/tests/test_graph/test_idft.py | 69 +- .../python/tests/test_graph/test_if.py | 25 +- .../tests/test_graph/test_log_softmax.py | 3 +- .../python/tests/test_graph/test_manager.py | 3 +- .../tests/test_graph/test_normalization.py | 133 +--- .../python/tests/test_graph/test_ops.py | 741 ++++-------------- .../tests/test_graph/test_ops_binary.py | 203 +++-- .../python/tests/test_graph/test_ops_fused.py | 592 ++------------ .../tests/test_graph/test_ops_matmul.py | 30 +- .../tests/test_graph/test_ops_multioutput.py | 23 +- .../tests/test_graph/test_ops_reshape.py | 102 +-- .../python/tests/test_graph/test_ops_unary.py | 284 +++---- .../python/tests/test_graph/test_pooling.py | 292 ++----- .../tests/test_graph/test_preprocess.py | 319 +++++--- .../tests/test_graph/test_random_uniform.py | 34 +- .../python/tests/test_graph/test_rdft.py | 197 ++--- .../python/tests/test_graph/test_reduction.py | 128 +-- .../python/tests/test_graph/test_roll.py | 10 +- .../test_graph/test_sequence_processing.py | 39 +- .../python/tests/test_graph/test_swish.py | 23 +- .../python/tests/test_graph/test_utils.py | 1 - src/bindings/python/tests/test_graph/util.py | 69 -- .../tests/test_runtime/test_compiled_model.py | 250 ++---- .../python/tests/test_runtime/test_core.py | 86 +- .../tests/test_runtime/test_infer_request.py | 60 +- .../tests/test_runtime/test_properties.py | 369 +++++++-- .../test_graph_rewrite.py | 6 +- .../test_transformations/test_matcher_pass.py | 8 +- .../test_transformations/test_model_pass.py | 4 +- .../test_transformations/test_offline_api.py | 18 +- .../test_public_transformations.py | 4 +- .../test_replacement_api.py | 2 +- .../tests/test_transformations/utils/utils.py | 2 +- .../python/tests/test_utils/test_utils.py | 80 +- .../python/tests/test_utils/utils/plugins.xml | 11 - .../tests/test_utils/utils/plugins_apple.xml | 11 - .../tests/test_utils/utils/plugins_win.xml | 11 - .../python/tests_compatibility/__init__.py | 2 - .../test_ExecutableNetwork.py | 61 +- .../test_InferRequest.py | 95 ++- .../test_ngraph/test_adaptive_pool.py | 66 +- .../test_ngraph/test_basic.py | 174 ++-- .../test_ngraph/test_convolution.py | 197 +---- .../test_ngraph/test_create_op.py | 28 +- .../test_ngraph/test_data_movement.py | 161 +--- .../test_ngraph/test_dft.py | 81 +- .../test_ngraph/test_einsum.py | 15 +- .../test_ngraph/test_eye.py | 14 - .../test_ngraph/test_gather.py | 84 +- .../test_ngraph/test_idft.py | 50 +- .../test_ngraph/test_if.py | 29 +- .../test_ngraph/test_manager.py | 4 +- .../test_ngraph/test_normalization.py | 133 +--- .../test_ngraph/test_ops.py | 349 ++++----- .../test_ngraph/test_ops_binary.py | 210 +++-- .../test_ngraph/test_ops_fused.py | 592 +++----------- .../test_ngraph/test_ops_matmul.py | 35 +- .../test_ngraph/test_ops_multioutput.py | 29 +- .../test_ngraph/test_ops_reshape.py | 103 +-- .../test_ngraph/test_ops_unary.py | 268 +++---- .../test_ngraph/test_pooling.py | 266 ++----- .../test_ngraph/test_random_uniform.py | 24 +- .../test_ngraph/test_reduction.py | 135 ++-- .../test_ngraph/test_roll.py | 13 +- .../test_ngraph/test_sequence_processing.py | 14 +- .../test_ngraph/test_utils.py | 4 +- .../tests_compatibility/test_ngraph/util.py | 69 -- .../test_utils/test_utils.py | 12 +- 83 files changed, 2710 insertions(+), 6056 deletions(-) delete mode 100644 src/bindings/python/tests/test_graph/test_adaptive_pool.py delete mode 100644 src/bindings/python/tests/test_utils/utils/plugins.xml delete mode 100644 src/bindings/python/tests/test_utils/utils/plugins_apple.xml delete mode 100644 src/bindings/python/tests/test_utils/utils/plugins_win.xml diff --git a/src/bindings/python/src/compatibility/ngraph/opset4/ops.py b/src/bindings/python/src/compatibility/ngraph/opset4/ops.py index cb0a7f04daa..ef8bd34e11c 100644 --- a/src/bindings/python/src/compatibility/ngraph/opset4/ops.py +++ b/src/bindings/python/src/compatibility/ngraph/opset4/ops.py @@ -186,7 +186,7 @@ def acosh(node: NodeInput, name: Optional[str] = None) -> Node: :param name: Optional new name for output node. :return: New node with arccosh operation applied on it. """ - return _get_node_factory_opset4().create("Acosh", [node]) + return _get_node_factory_opset4().create("Acosh", as_nodes(node)) @nameable_op @@ -197,7 +197,7 @@ def asinh(node: NodeInput, name: Optional[str] = None) -> Node: :param name: Optional new name for output node. :return: New node with arcsinh operation applied on it. """ - return _get_node_factory_opset4().create("Asinh", [node]) + return _get_node_factory_opset4().create("Asinh", as_nodes(node)) @nameable_op @@ -208,7 +208,7 @@ def atanh(node: NodeInput, name: Optional[str] = None) -> Node: :param name: Optional new name for output node. :return: New node with arctanh operation applied on it. """ - return _get_node_factory_opset4().create("Atanh", [node]) + return _get_node_factory_opset4().create("Atanh", as_nodes(node)) @nameable_op diff --git a/src/bindings/python/src/openvino/runtime/opset4/ops.py b/src/bindings/python/src/openvino/runtime/opset4/ops.py index 1b6ae833079..0ca2a2e27f1 100644 --- a/src/bindings/python/src/openvino/runtime/opset4/ops.py +++ b/src/bindings/python/src/openvino/runtime/opset4/ops.py @@ -173,7 +173,7 @@ def acosh(node: NodeInput, name: Optional[str] = None) -> Node: :param name: Optional new name for output node. :return: New node with arccosh operation applied on it. """ - return _get_node_factory_opset4().create("Acosh", [node]) + return _get_node_factory_opset4().create("Acosh", as_nodes(node)) @nameable_op @@ -184,7 +184,7 @@ def asinh(node: NodeInput, name: Optional[str] = None) -> Node: :param name: Optional new name for output node. :return: New node with arcsinh operation applied on it. """ - return _get_node_factory_opset4().create("Asinh", [node]) + return _get_node_factory_opset4().create("Asinh", as_nodes(node)) @nameable_op @@ -195,7 +195,7 @@ def atanh(node: NodeInput, name: Optional[str] = None) -> Node: :param name: Optional new name for output node. :return: New node with arctanh operation applied on it. """ - return _get_node_factory_opset4().create("Atanh", [node]) + return _get_node_factory_opset4().create("Atanh", as_nodes(node)) @nameable_op diff --git a/src/bindings/python/tests/__init__.py b/src/bindings/python/tests/__init__.py index 95bafaaa833..72d75ee7bd9 100644 --- a/src/bindings/python/tests/__init__.py +++ b/src/bindings/python/tests/__init__.py @@ -50,8 +50,6 @@ xfail_issue_90649 = xfail_test(reason="RuntimeError: OV does not support the fol "MelWeightMatrix, SequenceMap, STFT") xfail_issue_35923 = xfail_test(reason="RuntimeError: PReLU without weights is not supported") xfail_issue_35927 = xfail_test(reason="RuntimeError: B has zero dimension that is not allowable") -xfail_issue_36486 = xfail_test(reason="RuntimeError: HardSigmoid operation should be converted " - "to HardSigmoid_IE") xfail_issue_38091 = xfail_test(reason="AssertionError: Mismatched elements") xfail_issue_38699 = xfail_test(reason="RuntimeError: OV does not support the following ONNX operations: " "ai.onnx.preview.training.Gradient") diff --git a/src/bindings/python/tests/conftest.py b/src/bindings/python/tests/conftest.py index 184c59c8bf6..27995f732f1 100644 --- a/src/bindings/python/tests/conftest.py +++ b/src/bindings/python/tests/conftest.py @@ -85,14 +85,6 @@ def model_onnx_path(): return test_onnx -def plugins_path(): - base_path = os.path.dirname(__file__) - plugins_xml = os.path.join(base_path, "test_utils", "utils", "plugins.xml") - plugins_win_xml = os.path.join(base_path, "test_utils", "utils", "plugins_win.xml") - plugins_osx_xml = os.path.join(base_path, "test_utils", "utils", "plugins_apple.xml") - return (plugins_xml, plugins_win_xml, plugins_osx_xml) - - def _get_default_model_zoo_dir(): return Path(os.getenv("ONNX_HOME", Path.home() / ".onnx/model_zoo")) diff --git a/src/bindings/python/tests/runtime.py b/src/bindings/python/tests/runtime.py index 9177ed38972..3ca435a5394 100644 --- a/src/bindings/python/tests/runtime.py +++ b/src/bindings/python/tests/runtime.py @@ -12,7 +12,7 @@ import numpy as np from openvino.runtime import Core from openvino.runtime.exceptions import UserInputError -from openvino.runtime import Model, Node, PartialShape, Tensor, Type +from openvino.runtime import Model, Node, Tensor, Type from openvino.runtime.utils.types import NumericData, get_shape, get_dtype import tests diff --git a/src/bindings/python/tests/test_graph/test_adaptive_pool.py b/src/bindings/python/tests/test_graph/test_adaptive_pool.py deleted file mode 100644 index 9ac902a020b..00000000000 --- a/src/bindings/python/tests/test_graph/test_adaptive_pool.py +++ /dev/null @@ -1,69 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright (C) 2018-2022 Intel Corporation -# SPDX-License-Identifier: Apache-2.0 - -import openvino.runtime.opset8 as ov -import numpy as np -from tests.runtime import get_runtime - - -def test_adaptive_avg_pool(): - runtime = get_runtime() - input_vals = np.reshape([ - 0.0, 4, 1, 3, -2, -5, -2, - -2, 1, -3, 1, -3, -4, 0, - -2, 1, -1, -2, 3, -1, -3, - -1, -2, 3, 4, -3, -4, 1, - 2, 0, -4, -5, -2, -2, -3, - 2, 3, 1, -5, 2, -4, -2], - (2, 3, 7)) - input_tensor = ov.constant(input_vals) - output_shape = ov.constant(np.array([3], dtype=np.int32)) - - adaptive_pool_node = ov.adaptive_avg_pool(input_tensor, output_shape) - computation = runtime.computation(adaptive_pool_node) - adaptive_pool_results = computation() - expected_results = np.reshape([1.66666663, 0.66666669, -3., - -1.33333337, -1.66666663, -2.33333325, - -0.66666669, 0., -0.33333334, - - 0., 1.33333337, -2., - -0.66666669, -3.66666675, -2.33333325, - 2., -0.66666669, -1.33333337], (2, 3, 3)) - - assert np.allclose(adaptive_pool_results, expected_results) - - -def test_adaptive_max_pool(): - runtime = get_runtime() - input_vals = np.reshape([ - 0, 4, 1, 3, -2, -5, -2, - -2, 1, -3, 1, -3, -4, 0, - -2, 1, -1, -2, 3, -1, -3, - -1, -2, 3, 4, -3, -4, 1, - 2, 0, -4, -5, -2, -2, -3, - 2, 3, 1, -5, 2, -4, -2], - (2, 3, 7)) - input_tensor = ov.constant(input_vals) - output_shape = ov.constant(np.array([3], dtype=np.int32)) - - adaptive_pool_node = ov.adaptive_max_pool(input_tensor, output_shape) - computation = runtime.computation(adaptive_pool_node) - adaptive_pool_results = computation() - expected_results = np.reshape([4, 3, -2, - 1, 1, 0, - 1, 3, 3, - - 3, 4, 1, - 2, -2, -2, - 3, 2, 2], (2, 3, 3)) - - expected_indices = np.reshape([1, 3, 4, - 1, 3, 6, - 1, 4, 4, - - 2, 3, 6, - 0, 4, 4, - 1, 4, 4], (2, 3, 3)) - - assert np.allclose(adaptive_pool_results, [expected_results, expected_indices]) diff --git a/src/bindings/python/tests/test_graph/test_any.py b/src/bindings/python/tests/test_graph/test_any.py index 2c1a402828e..c5e9a736405 100644 --- a/src/bindings/python/tests/test_graph/test_any.py +++ b/src/bindings/python/tests/test_graph/test_any.py @@ -3,68 +3,50 @@ # SPDX-License-Identifier: Apache-2.0 from openvino.runtime import OVAny +import pytest -def test_any_str(): - string = OVAny("test_string") - assert isinstance(string.value, str) - assert string == "test_string" +@pytest.mark.parametrize(("value", "data_type"), [ + ("test_string", str), + (2137, int), + (21.37, float), + (False, bool), +]) +def test_any(value, data_type): + ovany = OVAny(value) + assert isinstance(ovany.value, data_type) + assert ovany == value + assert ovany.get() == value -def test_any_int(): - value = OVAny(2137) - assert isinstance(value.value, int) - assert value == 2137 +@pytest.mark.parametrize(("values", "data_type"), [ + (["test", "string"], str), + ([21, 37], int), + ([21.0, 37.0], float), +]) +def test_any_list(values, data_type): + ovany = OVAny(values) + assert isinstance(ovany.value, list) + assert isinstance(ovany[0], data_type) + assert isinstance(ovany[1], data_type) + assert len(values) == 2 + assert ovany.get() == values -def test_any_float(): - value = OVAny(21.37) - assert isinstance(value.value, float) - - -def test_any_string_list(): - str_list = OVAny(["test", "string"]) - assert isinstance(str_list.value, list) - assert isinstance(str_list[0], str) - assert str_list[0] == "test" - - -def test_any_int_list(): - value = OVAny([21, 37]) - assert isinstance(value.value, list) - assert len(value) == 2 - assert isinstance(value[0], int) - - -def test_any_float_list(): - value = OVAny([21.0, 37.0]) - assert isinstance(value.value, list) - assert len(value) == 2 - assert isinstance(value[0], float) - - -def test_any_bool(): - value = OVAny(False) - assert isinstance(value.value, bool) - assert value is not True - - -def test_any_dict_str(): - value = OVAny({"key": "value"}) - assert isinstance(value.value, dict) - assert value["key"] == "value" - - -def test_any_dict_str_int(): - value = OVAny({"key": 2}) - assert isinstance(value.value, dict) - assert value["key"] == 2 - - -def test_any_int_dict(): - value = OVAny({1: 2}) - assert isinstance(value.value, dict) - assert value[1] == 2 +@pytest.mark.parametrize(("value_dict", "data_type"), [ + ({"key": "value"}, str), + ({21: 37}, int), + ({21.0: 37.0}, float), +]) +def test_any_dict(value_dict, data_type): + ovany = OVAny(value_dict) + key = list(value_dict.keys())[0] + assert isinstance(ovany.value, dict) + assert ovany[key] == list(value_dict.values())[0] + assert len(ovany.value) == 1 + assert type(ovany.value[key]) == data_type + assert type(list(value_dict.values())[0]) == data_type + assert ovany.get() == value_dict def test_any_set_new_value(): diff --git a/src/bindings/python/tests/test_graph/test_basic.py b/src/bindings/python/tests/test_graph/test_basic.py index 20469c8a5ee..3a11c55ac72 100644 --- a/src/bindings/python/tests/test_graph/test_basic.py +++ b/src/bindings/python/tests/test_graph/test_basic.py @@ -2,7 +2,6 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 -import json import numpy as np import pytest @@ -10,15 +9,13 @@ import pytest import openvino.runtime.opset8 as ops import openvino.runtime as ov -from openvino.runtime.exceptions import UserInputError from openvino.runtime import Model, PartialShape, Shape, Type, layout_helpers from openvino.runtime import Strides, AxisVector, Coordinate, CoordinateDiff from openvino.runtime import Tensor, OVAny from openvino._pyopenvino import DescriptorTensor from openvino.runtime.op import Parameter -from tests.runtime import get_runtime -from openvino.runtime.utils.types import get_dtype -from tests.test_graph.util import run_op_node + +from openvino.runtime.utils.types import get_element_type def test_graph_function_api(): @@ -88,76 +85,34 @@ def test_graph_function_api(): Type.u64, ], ) -def test_simple_computation_on_ndarrays(dtype): - runtime = get_runtime() - +def test_simple_model_on_parameters(dtype): shape = [2, 2] parameter_a = ops.parameter(shape, dtype=dtype, name="A") parameter_b = ops.parameter(shape, dtype=dtype, name="B") parameter_c = ops.parameter(shape, dtype=dtype, name="C") model = (parameter_a + parameter_b) * parameter_c - computation = runtime.computation(model, parameter_a, parameter_b, parameter_c) - - np_dtype = get_dtype(dtype) if isinstance(dtype, Type) else dtype - - value_a = np.array([[1, 2], [3, 4]], dtype=np_dtype) - value_b = np.array([[5, 6], [7, 8]], dtype=np_dtype) - value_c = np.array([[2, 3], [4, 5]], dtype=np_dtype) - result = computation(value_a, value_b, value_c) - assert np.allclose(result, np.array([[12, 24], [40, 60]], dtype=np_dtype)) - - value_a = np.array([[9, 10], [11, 12]], dtype=np_dtype) - value_b = np.array([[13, 14], [15, 16]], dtype=np_dtype) - value_c = np.array([[5, 4], [3, 2]], dtype=np_dtype) - result = computation(value_a, value_b, value_c) - assert np.allclose(result, np.array([[110, 96], [78, 56]], dtype=np_dtype)) + expected_type = dtype if isinstance(dtype, Type) else get_element_type(dtype) + assert model.get_type_name() == "Multiply" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == expected_type + assert list(model.get_output_shape(0)) == [2, 2] -def test_serialization(): - dtype = np.float32 - shape = [2, 2] - parameter_a = ops.parameter(shape, dtype=dtype, name="A") - parameter_b = ops.parameter(shape, dtype=dtype, name="B") - parameter_c = ops.parameter(shape, dtype=dtype, name="C") - model = (parameter_a + parameter_b) * parameter_c - - runtime = get_runtime() - computation = runtime.computation(model, parameter_a, parameter_b, parameter_c) - try: - serialized = computation.serialize(2) - serial_json = json.loads(serialized) - - assert serial_json[0]["name"] != "" - assert 10 == len(serial_json[0]["ops"]) - except Exception: - pass - - -def test_broadcast_1(): - input_data = np.array([1, 2, 3], dtype=np.int32) - - new_shape = [3, 3] - expected = [[1, 2, 3], [1, 2, 3], [1, 2, 3]] - result = run_op_node([input_data], ops.broadcast, new_shape) - assert np.allclose(result, expected) - - -def test_broadcast_2(): - input_data = np.arange(4, dtype=np.int32) - new_shape = [3, 4, 2, 4] - expected = np.broadcast_to(input_data, new_shape) - result = run_op_node([input_data], ops.broadcast, new_shape) - assert np.allclose(result, expected) - - -def test_broadcast_3(): - input_data = np.array([1, 2, 3], dtype=np.int32) - new_shape = [3, 3] - axis_mapping = [0] - expected = [[1, 1, 1], [2, 2, 2], [3, 3, 3]] - - result = run_op_node([input_data], ops.broadcast, new_shape, axis_mapping, "EXPLICIT") - assert np.allclose(result, expected) +@pytest.mark.parametrize( + ("input_shape", "dtype", "new_shape", "axis_mapping", "mode"), + [ + ((3,), np.int32, [3, 3], [], []), + ((4,), np.float32, [3, 4, 2, 4], [], []), + ((3,), np.int8, [3, 3], [[0]], ["EXPLICIT"]), + ], +) +def test_broadcast(input_shape, dtype, new_shape, axis_mapping, mode): + input_data = ops.parameter(input_shape, name="input_data", dtype=dtype) + node = ops.broadcast(input_data, new_shape, *axis_mapping, *mode) + assert node.get_type_name() == "Broadcast" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(dtype) + assert list(node.get_output_shape(0)) == new_shape @pytest.mark.parametrize( @@ -165,10 +120,11 @@ def test_broadcast_3(): [(bool, np.zeros((2, 2), dtype=np.int32)), ("boolean", np.zeros((2, 2), dtype=np.int32))], ) def test_convert_to_bool(destination_type, input_data): - expected = np.array(input_data, dtype=bool) - result = run_op_node([input_data], ops.convert, destination_type) - assert np.allclose(result, expected) - assert np.array(result).dtype == bool + node = ops.convert(input_data, destination_type) + assert node.get_type_name() == "Convert" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.boolean + assert list(node.get_output_shape(0)) == [2, 2] @pytest.mark.parametrize( @@ -183,10 +139,11 @@ def test_convert_to_bool(destination_type, input_data): def test_convert_to_float(destination_type, rand_range, in_dtype, expected_type): np.random.seed(133391) input_data = np.random.randint(*rand_range, size=(2, 2), dtype=in_dtype) - expected = np.array(input_data, dtype=expected_type) - result = run_op_node([input_data], ops.convert, destination_type) - assert np.allclose(result, expected) - assert np.array(result).dtype == expected_type + node = ops.convert(input_data, destination_type) + assert node.get_type_name() == "Convert" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(expected_type) + assert list(node.get_output_shape(0)) == [2, 2] @pytest.mark.parametrize( @@ -206,10 +163,11 @@ def test_convert_to_int(destination_type, expected_type): np.random.seed(133391) random_data = np.random.rand(2, 3, 4) * 16 input_data = (np.ceil(-8 + random_data)).astype(expected_type) - expected = np.array(input_data, dtype=expected_type) - result = run_op_node([input_data], ops.convert, destination_type) - assert np.allclose(result, expected) - assert np.array(result).dtype == expected_type + node = ops.convert(input_data, destination_type) + assert node.get_type_name() == "Convert" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(expected_type) + assert list(node.get_output_shape(0)) == [2, 3, 4] @pytest.mark.parametrize( @@ -228,23 +186,11 @@ def test_convert_to_int(destination_type, expected_type): def test_convert_to_uint(destination_type, expected_type): np.random.seed(133391) input_data = np.ceil(np.random.rand(2, 3, 4) * 16).astype(expected_type) - expected = np.array(input_data, dtype=expected_type) - result = run_op_node([input_data], ops.convert, destination_type) - assert np.allclose(result, expected) - assert np.array(result).dtype == expected_type - - -def test_bad_data_shape(): - param_a = ops.parameter(shape=[2, 2], name="A", dtype=np.float32) - param_b = ops.parameter(shape=[2, 2], name="B") - model = param_a + param_b - runtime = get_runtime() - computation = runtime.computation(model, param_a, param_b) - - value_a = np.array([[1, 2]], dtype=np.float32) - value_b = np.array([[5, 6], [7, 8]], dtype=np.float32) - with pytest.raises(RuntimeError): - computation(value_a, value_b) + node = ops.convert(input_data, destination_type) + assert node.get_type_name() == "Convert" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(expected_type) + assert list(node.get_output_shape(0)) == [2, 3, 4] def test_constant_get_data_bool(): @@ -290,41 +236,41 @@ def test_constant_get_data_unsigned_integer(data_type): def test_set_argument(): - runtime = get_runtime() - data1 = np.array([1, 2, 3]) data2 = np.array([4, 5, 6]) data3 = np.array([7, 8, 9]) node1 = ops.constant(data1, dtype=np.float32) node2 = ops.constant(data2, dtype=np.float32) - node3 = ops.constant(data3, dtype=np.float32) + node3 = ops.constant(data3, dtype=np.float64) + node4 = ops.constant(data3, dtype=np.float64) node_add = ops.add(node1, node2) # Original arguments - computation = runtime.computation(node_add) - output = computation() - assert np.allclose(data1 + data2, output) - - # Arguments changed by set_argument - node_add.set_argument(1, node3.output(0)) - output = computation() - assert np.allclose(data1 + data3, output) + node_inputs = node_add.inputs() + assert node_inputs[0].get_element_type() == Type.f32 + assert node_inputs[1].get_element_type() == Type.f32 + assert len(node_inputs) == 2 # Arguments changed by set_argument node_add.set_argument(0, node3.output(0)) - output = computation() - assert np.allclose(data3 + data3, output) + node_add.set_argument(1, node4.output(0)) + node_inputs = node_add.inputs() + assert node_inputs[0].get_element_type() == Type.f64 + assert node_inputs[1].get_element_type() == Type.f64 + assert len(node_inputs) == 2 # Arguments changed by set_argument(OutputVector) - node_add.set_arguments([node2.output(0), node3.output(0)]) - output = computation() - assert np.allclose(data2 + data3, output) + node_add.set_arguments([node1.output(0), node2.output(0)]) + assert node_inputs[0].get_element_type() == Type.f32 + assert node_inputs[1].get_element_type() == Type.f32 + assert len(node_inputs) == 2 # Arguments changed by set_arguments(NodeVector) - node_add.set_arguments([node1, node2]) - output = computation() - assert np.allclose(data1 + data2, output) + node_add.set_arguments([node3, node4]) + assert node_inputs[0].get_element_type() == Type.f64 + assert node_inputs[1].get_element_type() == Type.f64 + assert len(node_inputs) == 2 def test_clone_model(): @@ -352,9 +298,12 @@ def test_clone_model(): def test_result(): - node = np.array([[11, 10], [1, 8], [3, 4]], dtype=np.float32) - result = run_op_node([node], ops.result) - assert np.allclose(result, node) + input_data = np.array([[11, 10], [1, 8], [3, 4]], dtype=np.float32) + node = ops.result(input_data) + assert node.get_type_name() == "Result" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.f32 + assert list(node.get_output_shape(0)) == [3, 2] def test_node_friendly_name(): @@ -529,20 +478,6 @@ def test_node_target_inputs_soruce_output(): assert np.equal([in_model1.get_shape()], [model.get_output_shape(0)]).all() -def test_any(): - any_int = OVAny(32) - any_str = OVAny("test_text") - - assert any_int.get() == 32 - assert any_str.get() == "test_text" - - any_int.set(777) - any_str.set("another_text") - - assert any_int.get() == 777 - assert any_str.get() == "another_text" - - def test_runtime_info(): test_shape = PartialShape([1, 1, 1, 1]) test_type = Type.f32 @@ -567,11 +502,10 @@ def test_multiple_outputs(): split_first_output = split.output(0) relu = ops.relu(split_first_output) - runtime = get_runtime() - computation = runtime.computation(relu, test_param) - output = computation(input_data) - - assert np.equal(output, expected_output).all() + assert relu.get_type_name() == "Relu" + assert relu.get_output_size() == 1 + assert relu.get_output_element_type(0) == Type.f32 + assert list(relu.get_output_shape(0)) == [4, 2] def test_sink_function_ctor(): diff --git a/src/bindings/python/tests/test_graph/test_convolution.py b/src/bindings/python/tests/test_graph/test_convolution.py index aaee513b1f1..5e09c1ea578 100644 --- a/src/bindings/python/tests/test_graph/test_convolution.py +++ b/src/bindings/python/tests/test_graph/test_convolution.py @@ -3,145 +3,34 @@ # SPDX-License-Identifier: Apache-2.0 import numpy as np +import pytest +from openvino.runtime import Type import openvino.runtime.opset8 as ov -from tests.runtime import get_runtime -from tests.test_graph.test_ops import convolution2d -from tests.test_graph.util import run_op_node -def test_convolution_2d(): +@pytest.mark.parametrize(("strides", "pads_begin", "pads_end", "dilations", "expected_shape"), [ + (np.array([1, 1]), np.array([1, 1]), np.array([1, 1]), np.array([1, 1]), [1, 1, 9, 9]), + (np.array([1, 1]), np.array([0, 0]), np.array([0, 0]), np.array([1, 1]), [1, 1, 7, 7]), + (np.array([2, 2]), np.array([0, 0]), np.array([0, 0]), np.array([1, 1]), [1, 1, 4, 4]), + (np.array([1, 1]), np.array([0, 0]), np.array([0, 0]), np.array([2, 2]), [1, 1, 5, 5]), +]) +def test_convolution_2d(strides, pads_begin, pads_end, dilations, expected_shape): # input_x should have shape N(batch) x C x H x W - input_x = np.array( - [ - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - ], - dtype=np.float32, - ).reshape(1, 1, 9, 9) + input_x = ov.parameter((1, 1, 9, 9), name="input_x", dtype=np.float32) # filter weights should have shape M x C x kH x kW - input_filter = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]], dtype=np.float32).reshape( - 1, 1, 3, 3, - ) + input_filter = ov.parameter((1, 1, 3, 3), name="input_filter", dtype=np.float32) - strides = np.array([1, 1]) - pads_begin = np.array([1, 1]) - pads_end = np.array([1, 1]) - dilations = np.array([1, 1]) - - # convolution with padding=1 should produce 9 x 9 output: - result = run_op_node([input_x, input_filter], ov.convolution, strides, pads_begin, pads_end, dilations) - - assert np.allclose( - result, - np.array( - [ - [ - [ - [0.0, -15.0, -15.0, 15.0, 15.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -15.0, -15.0, 15.0, 15.0, 0.0, 0.0, 0.0, 0.0], - ], - ], - ], - dtype=np.float32, - ), - ) - - # convolution with padding=0 should produce 7 x 7 output: - strides = np.array([1, 1]) - pads_begin = np.array([0, 0]) - pads_end = np.array([0, 0]) - dilations = np.array([1, 1]) - result = run_op_node([input_x, input_filter], ov.convolution, strides, pads_begin, pads_end, dilations) - assert np.allclose( - result, - np.array( - [ - [ - [ - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - ], - ], - ], - dtype=np.float32, - ), - ) - - strides = np.array([2, 2]) - pads_begin = np.array([0, 0]) - pads_end = np.array([0, 0]) - dilations = np.array([1, 1]) - - # convolution with strides=2 should produce 4 x 4 output: - result = run_op_node([input_x, input_filter], ov.convolution, strides, pads_begin, pads_end, dilations) - - assert np.allclose( - result, - np.array( - [ - [ - [ - [-20.0, 20.0, 0.0, 0.0], - [-20.0, 20.0, 0.0, 0.0], - [-20.0, 20.0, 0.0, 0.0], - [-20.0, 20.0, 0.0, 0.0], - ], - ], - ], - dtype=np.float32, - ), - ) - - strides = np.array([1, 1]) - pads_begin = np.array([0, 0]) - pads_end = np.array([0, 0]) - dilations = np.array([2, 2]) - - # convolution with dilation=2 should produce 5 x 5 output: - result = run_op_node([input_x, input_filter], ov.convolution, strides, pads_begin, pads_end, dilations) - assert np.allclose( - result, - np.array( - [ - [ - [ - [0, 0, 20, 20, 0], - [0, 0, 20, 20, 0], - [0, 0, 20, 20, 0], - [0, 0, 20, 20, 0], - [0, 0, 20, 20, 0], - ], - ], - ], - dtype=np.float32, - ), - ) + node = ov.convolution(input_x, input_filter, strides, pads_begin, pads_end, dilations) + assert node.get_type_name() == "Convolution" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 def test_convolution_backprop_data(): - runtime = get_runtime() output_spatial_shape = [9, 9] filter_shape = [1, 1, 3, 3] @@ -153,68 +42,7 @@ def test_convolution_backprop_data(): output_shape_node = ov.constant(np.array(output_spatial_shape, dtype=np.int64)) deconvolution = ov.convolution_backprop_data(data_node, filter_node, strides, output_shape_node) - - input_data = np.array( - [ - [ - [ - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - ], - ], - ], - dtype=np.float32, - ) - - filter_data = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]], dtype=np.float32).reshape( - 1, 1, 3, 3, - ) - - model = runtime.computation(deconvolution, data_node, filter_node) - result = model(input_data, filter_data) - assert np.allclose( - result, - np.array( - [ - [ - [ - [-20.0, -20.0, 40.0, 40.0, -20.0, -20.0, 0.0, 0.0, 0.0], - [-60.0, -60.0, 120.0, 120.0, -60.0, -60.0, 0.0, 0.0, 0.0], - [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], - [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], - [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], - [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], - [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], - [-60.0, -60.0, 120.0, 120.0, -60.0, -60.0, 0.0, 0.0, 0.0], - [-20.0, -20.0, 40.0, 40.0, -20.0, -20.0, 0.0, 0.0, 0.0], - ], - ], - ], - dtype=np.float32, - ), - ) - - -def test_convolution_v1(): - input_tensor = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16) - filters = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) - filters[0, 0, 0, 0] = -1 - filters[0, 0, 1, 1] = -1 - filters[0, 0, 2, 2] = -1 - filters[0, 0, 0, 2] = -1 - filters[0, 0, 2, 0] = -1 - strides = np.array([1, 1]) - pads_begin = np.array([0, 0]) - pads_end = np.array([0, 0]) - dilations = np.array([1, 1]) - - result = run_op_node([input_tensor, filters], ov.convolution, strides, pads_begin, pads_end, dilations) - - expected = convolution2d(input_tensor[0, 0], filters[0, 0]).reshape(1, 1, 14, 14) - - assert np.allclose(result, expected) + assert deconvolution.get_type_name() == "ConvolutionBackpropData" + assert deconvolution.get_output_size() == 1 + assert list(deconvolution.get_output_shape(0)) == [1, 1, 9, 9] + assert deconvolution.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests/test_graph/test_create_op.py b/src/bindings/python/tests/test_graph/test_create_op.py index f25c08ea59a..30af12b1666 100644 --- a/src/bindings/python/tests/test_graph/test_create_op.py +++ b/src/bindings/python/tests/test_graph/test_create_op.py @@ -64,12 +64,8 @@ def test_binary_convolution(dtype): mode = "xnor-popcount" pad_value = 0.0 - input0_shape = [1, 1, 9, 9] - input1_shape = [1, 1, 3, 3] - expected_shape = [1, 1, 7, 7] - - parameter_input0 = ov.parameter(input0_shape, name="Input0", dtype=dtype) - parameter_input1 = ov.parameter(input1_shape, name="Input1", dtype=dtype) + parameter_input0 = ov.parameter([1, 1, 9, 9], name="Input0", dtype=dtype) + parameter_input1 = ov.parameter([1, 1, 3, 3], name="Input1", dtype=dtype) node = ov.binary_convolution( parameter_input0, parameter_input1, strides, pads_begin, pads_end, dilations, mode, pad_value, @@ -77,14 +73,13 @@ def test_binary_convolution(dtype): assert node.get_type_name() == "BinaryConvolution" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [1, 1, 7, 7] @pytest.mark.parametrize("dtype", np_types) def test_ctc_greedy_decoder(dtype): input0_shape = [20, 8, 128] input1_shape = [20, 8] - expected_shape = [8, 20, 1, 1] parameter_input0 = ov.parameter(input0_shape, name="Input0", dtype=dtype) parameter_input1 = ov.parameter(input1_shape, name="Input1", dtype=dtype) @@ -93,7 +88,7 @@ def test_ctc_greedy_decoder(dtype): assert node.get_type_name() == "CTCGreedyDecoder" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [8, 20, 1, 1] @pytest.mark.parametrize( @@ -118,16 +113,12 @@ def test_ctc_greedy_decoder(dtype): ], ) def test_ctc_greedy_decoder_seq_len(fp_dtype, int_dtype, int_ci, int_sl, merge_repeated, blank_index): - input0_shape = [8, 20, 128] - input1_shape = [8] - input2_shape = [1] - expected_shape = [8, 20] - parameter_input0 = ov.parameter(input0_shape, name="Input0", dtype=fp_dtype) - parameter_input1 = ov.parameter(input1_shape, name="Input1", dtype=int_dtype) + parameter_input0 = ov.parameter([8, 20, 128], name="Input0", dtype=fp_dtype) + parameter_input1 = ov.parameter([8], name="Input1", dtype=int_dtype) parameter_input2 = None if blank_index: - parameter_input2 = ov.parameter(input2_shape, name="Input2", dtype=int_dtype) + parameter_input2 = ov.parameter([1], name="Input2", dtype=int_dtype) node = ov.ctc_greedy_decoder_seq_len( parameter_input0, parameter_input1, parameter_input2, merge_repeated, int_ci, int_sl, @@ -135,7 +126,7 @@ def test_ctc_greedy_decoder_seq_len(fp_dtype, int_dtype, int_ci, int_sl, merge_r assert node.get_type_name() == "CTCGreedyDecoderSeqLen" assert node.get_output_size() == 2 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [8, 20] @pytest.mark.parametrize("dtype", np_types) @@ -145,14 +136,9 @@ def test_deformable_convolution_opset1(dtype): pads_end = np.array([0, 0]) dilations = np.array([1, 1]) - input0_shape = [1, 1, 9, 9] - input1_shape = [1, 18, 7, 7] - input2_shape = [1, 1, 3, 3] - expected_shape = [1, 1, 7, 7] - - parameter_input0 = ov.parameter(input0_shape, name="Input0", dtype=dtype) - parameter_input1 = ov.parameter(input1_shape, name="Input1", dtype=dtype) - parameter_input2 = ov.parameter(input2_shape, name="Input2", dtype=dtype) + parameter_input0 = ov.parameter([1, 1, 9, 9], name="Input0", dtype=dtype) + parameter_input1 = ov.parameter([1, 18, 7, 7], name="Input1", dtype=dtype) + parameter_input2 = ov.parameter([1, 1, 3, 3], name="Input2", dtype=dtype) node = ov_opset1.deformable_convolution( parameter_input0, parameter_input1, parameter_input2, strides, pads_begin, pads_end, dilations, @@ -160,7 +146,7 @@ def test_deformable_convolution_opset1(dtype): assert node.get_type_name() == "DeformableConvolution" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [1, 1, 7, 7] @pytest.mark.parametrize("dtype", np_types) @@ -170,14 +156,9 @@ def test_deformable_convolution(dtype): pads_end = np.array([0, 0]) dilations = np.array([1, 1]) - input0_shape = [1, 1, 9, 9] - input1_shape = [1, 18, 7, 7] - input2_shape = [1, 1, 3, 3] - expected_shape = [1, 1, 7, 7] - - parameter_input0 = ov.parameter(input0_shape, name="Input0", dtype=dtype) - parameter_input1 = ov.parameter(input1_shape, name="Input1", dtype=dtype) - parameter_input2 = ov.parameter(input2_shape, name="Input2", dtype=dtype) + parameter_input0 = ov.parameter([1, 1, 9, 9], name="Input0", dtype=dtype) + parameter_input1 = ov.parameter([1, 18, 7, 7], name="Input1", dtype=dtype) + parameter_input2 = ov.parameter([1, 1, 3, 3], name="Input2", dtype=dtype) node = ov.deformable_convolution( parameter_input0, parameter_input1, parameter_input2, strides, pads_begin, pads_end, dilations, @@ -185,7 +166,7 @@ def test_deformable_convolution(dtype): assert node.get_type_name() == "DeformableConvolution" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [1, 1, 7, 7] @pytest.mark.parametrize("dtype", np_types) @@ -195,16 +176,10 @@ def test_deformable_convolution_mask(dtype): pads_end = np.array([0, 0]) dilations = np.array([1, 1]) - input0_shape = [1, 1, 9, 9] - input1_shape = [1, 18, 7, 7] - input2_shape = [1, 1, 3, 3] - input3_shape = [1, 9, 7, 7] - expected_shape = [1, 1, 7, 7] - - parameter_input0 = ov.parameter(input0_shape, name="Input0", dtype=dtype) - parameter_input1 = ov.parameter(input1_shape, name="Input1", dtype=dtype) - parameter_input2 = ov.parameter(input2_shape, name="Input2", dtype=dtype) - parameter_input3 = ov.parameter(input3_shape, name="Input3", dtype=dtype) + parameter_input0 = ov.parameter([1, 1, 9, 9], name="Input0", dtype=dtype) + parameter_input1 = ov.parameter([1, 18, 7, 7], name="Input1", dtype=dtype) + parameter_input2 = ov.parameter([1, 1, 3, 3], name="Input2", dtype=dtype) + parameter_input3 = ov.parameter([1, 9, 7, 7], name="Input3", dtype=dtype) node = ov.deformable_convolution( parameter_input0, parameter_input1, parameter_input2, strides, @@ -213,7 +188,7 @@ def test_deformable_convolution_mask(dtype): assert node.get_type_name() == "DeformableConvolution" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [1, 1, 7, 7] @pytest.mark.parametrize("dtype", np_types) @@ -227,14 +202,9 @@ def test_deformable_psroi_pooling(dtype): trans_std = 0.1 part_size = 7 - input0_shape = [1, 392, 38, 63] - input1_shape = [300, 5] - input2_shape = [300, 2, 7, 7] - expected_shape = [300, 8, 7, 7] - - parameter_input0 = ov.parameter(input0_shape, name="Input0", dtype=dtype) - parameter_input1 = ov.parameter(input1_shape, name="Input1", dtype=dtype) - parameter_input2 = ov.parameter(input2_shape, name="Input2", dtype=dtype) + parameter_input0 = ov.parameter([1, 392, 38, 63], name="Input0", dtype=dtype) + parameter_input1 = ov.parameter([300, 5], name="Input1", dtype=dtype) + parameter_input2 = ov.parameter([300, 2, 7, 7], name="Input2", dtype=dtype) node = ov.deformable_psroi_pooling( parameter_input0, @@ -252,43 +222,33 @@ def test_deformable_psroi_pooling(dtype): assert node.get_type_name() == "DeformablePSROIPooling" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [300, 8, 7, 7] @pytest.mark.parametrize("dtype", np_types) def test_floor_mod(dtype): - input0_shape = [8, 1, 6, 1] - input1_shape = [7, 1, 5] - expected_shape = [8, 7, 6, 5] - - parameter_input0 = ov.parameter(input0_shape, name="Input0", dtype=dtype) - parameter_input1 = ov.parameter(input1_shape, name="Input1", dtype=dtype) + parameter_input0 = ov.parameter([8, 1, 6, 1], name="Input0", dtype=dtype) + parameter_input1 = ov.parameter([7, 1, 5], name="Input1", dtype=dtype) node = ov.floor_mod(parameter_input0, parameter_input1) assert node.get_type_name() == "FloorMod" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [8, 7, 6, 5] @pytest.mark.parametrize("dtype", np_types) def test_gather_tree(dtype): - input0_shape = [100, 1, 10] - input1_shape = [100, 1, 10] - input2_shape = [1] - input3_shape = [] - expected_shape = [100, 1, 10] - - parameter_input0 = ov.parameter(input0_shape, name="Input0", dtype=dtype) - parameter_input1 = ov.parameter(input1_shape, name="Input1", dtype=dtype) - parameter_input2 = ov.parameter(input2_shape, name="Input2", dtype=dtype) - parameter_input3 = ov.parameter(input3_shape, name="Input3", dtype=dtype) + parameter_input0 = ov.parameter([100, 1, 10], name="Input0", dtype=dtype) + parameter_input1 = ov.parameter([100, 1, 10], name="Input1", dtype=dtype) + parameter_input2 = ov.parameter([1], name="Input2", dtype=dtype) + parameter_input3 = ov.parameter([], name="Input3", dtype=dtype) node = ov.gather_tree(parameter_input0, parameter_input1, parameter_input2, parameter_input3) assert node.get_type_name() == "GatherTree" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [100, 1, 10] @pytest.mark.parametrize("dtype", [np.float32, np.float64]) @@ -311,16 +271,14 @@ def test_lstm_cell_operator(dtype): parameter_r = ov.parameter(r_shape, name="R", dtype=dtype) parameter_b = ov.parameter(b_shape, name="B", dtype=dtype) - expected_shape = [1, 128] - node_default = ov.lstm_cell( parameter_x, parameter_h_t, parameter_c_t, parameter_w, parameter_r, parameter_b, hidden_size, ) assert node_default.get_type_name() == "LSTMCell" assert node_default.get_output_size() == 2 - assert list(node_default.get_output_shape(0)) == expected_shape - assert list(node_default.get_output_shape(1)) == expected_shape + assert list(node_default.get_output_shape(0)) == [1, 128] + assert list(node_default.get_output_shape(1)) == [1, 128] activations = ["tanh", "Sigmoid", "RELU"] activation_alpha = [1.0, 2.0, 3.0] @@ -343,8 +301,8 @@ def test_lstm_cell_operator(dtype): assert node_param.get_type_name() == "LSTMCell" assert node_param.get_output_size() == 2 - assert list(node_param.get_output_shape(0)) == expected_shape - assert list(node_param.get_output_shape(1)) == expected_shape + assert list(node_param.get_output_shape(0)) == [1, 128] + assert list(node_param.get_output_shape(1)) == [1, 128] @pytest.mark.parametrize("dtype", [np.float32, np.float64]) @@ -367,16 +325,14 @@ def test_lstm_cell_operator_opset1(dtype): parameter_r = ov.parameter(r_shape, name="R", dtype=dtype) parameter_b = ov.parameter(b_shape, name="B", dtype=dtype) - expected_shape = [1, 128] - node_default = ov_opset1.lstm_cell( parameter_x, parameter_h_t, parameter_c_t, parameter_w, parameter_r, parameter_b, hidden_size, ) assert node_default.get_type_name() == "LSTMCell" assert node_default.get_output_size() == 2 - assert list(node_default.get_output_shape(0)) == expected_shape - assert list(node_default.get_output_shape(1)) == expected_shape + assert list(node_default.get_output_shape(0)) == [1, 128] + assert list(node_default.get_output_shape(1)) == [1, 128] activations = ["tanh", "Sigmoid", "RELU"] activation_alpha = [1.0, 2.0, 3.0] @@ -399,8 +355,8 @@ def test_lstm_cell_operator_opset1(dtype): assert node_param.get_type_name() == "LSTMCell" assert node_param.get_output_size() == 2 - assert list(node_param.get_output_shape(0)) == expected_shape - assert list(node_param.get_output_shape(1)) == expected_shape + assert list(node_param.get_output_shape(0)) == [1, 128] + assert list(node_param.get_output_shape(1)) == [1, 128] @pytest.mark.parametrize("dtype", [np.float32, np.float64]) @@ -617,13 +573,11 @@ def test_gru_cell_operator(): parameter_r = ov.parameter(r_shape, name="R", dtype=np.float32) parameter_b = ov.parameter(b_shape, name="B", dtype=np.float32) - expected_shape = [1, 128] - node_default = ov.gru_cell(parameter_x, parameter_h_t, parameter_w, parameter_r, parameter_b, hidden_size) assert node_default.get_type_name() == "GRUCell" assert node_default.get_output_size() == 1 - assert list(node_default.get_output_shape(0)) == expected_shape + assert list(node_default.get_output_shape(0)) == [1, 128] activations = ["tanh", "relu"] activations_alpha = [1.0, 2.0] @@ -651,7 +605,7 @@ def test_gru_cell_operator(): assert node_param.get_type_name() == "GRUCell" assert node_param.get_output_size() == 1 - assert list(node_param.get_output_shape(0)) == expected_shape + assert list(node_param.get_output_shape(0)) == [1, 128] def test_gru_sequence(): @@ -1027,11 +981,10 @@ def test_interpolate_opset1(dtype): image_node = ov.parameter(image_shape, dtype, name="Image") node = ov_opset1.interpolate(image_node, output_shape, attributes) - expected_shape = [1, 3, 64, 64] assert node.get_type_name() == "Interpolate" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [1, 3, 64, 64] @pytest.mark.parametrize( @@ -2203,7 +2156,7 @@ def test_interpolate_opset10(dtype, expected_shape, shape_calculation_mode): def test_is_finite_opset10(): input_shape = [1, 2, 3, 4] - input_node = ov.parameter(input_shape, np.float, name="InputData") + input_node = ov.parameter(input_shape, np.float32, name="InputData") node = ov_opset10.is_finite(input_node) assert node.get_type_name() == "IsFinite" @@ -2214,7 +2167,7 @@ def test_is_finite_opset10(): def test_is_inf_opset10_default(): input_shape = [2, 2, 2, 2] - input_node = ov.parameter(input_shape, dtype=np.float, name="InputData") + input_node = ov.parameter(input_shape, dtype=np.float32, name="InputData") node = ov_opset10.is_inf(input_node) assert node.get_type_name() == "IsInf" @@ -2228,7 +2181,7 @@ def test_is_inf_opset10_default(): def test_is_inf_opset10_custom_attribute(): input_shape = [2, 2, 2] - input_node = ov.parameter(input_shape, dtype=np.float, name="InputData") + input_node = ov.parameter(input_shape, dtype=np.float32, name="InputData") attributes = { "detect_positive": False, } @@ -2245,7 +2198,7 @@ def test_is_inf_opset10_custom_attribute(): def test_is_inf_opset10_custom_all_attributes(): input_shape = [2, 2, 2] - input_node = ov.parameter(input_shape, dtype=np.float, name="InputData") + input_node = ov.parameter(input_shape, dtype=np.float32, name="InputData") attributes = { "detect_negative": False, "detect_positive": True, @@ -2263,7 +2216,7 @@ def test_is_inf_opset10_custom_all_attributes(): def test_is_nan_opset10(): input_shape = [1, 2, 3, 4] - input_node = ov.parameter(input_shape, np.float, name="InputData") + input_node = ov.parameter(input_shape, np.float32, name="InputData") node = ov_opset10.is_nan(input_node) assert node.get_type_name() == "IsNaN" @@ -2274,7 +2227,7 @@ def test_is_nan_opset10(): def test_unique_opset10(): input_shape = [1, 2, 3, 4] - input_node = ov.parameter(input_shape, np.float, name="input_data") + input_node = ov.parameter(input_shape, np.float32, name="input_data") axis = ov.constant([1], np.int32, [1]) node = ov_opset10.unique(input_node, axis, False, "i32") diff --git a/src/bindings/python/tests/test_graph/test_data_movement.py b/src/bindings/python/tests/test_graph/test_data_movement.py index 883112db88a..0e20d265bd2 100644 --- a/src/bindings/python/tests/test_graph/test_data_movement.py +++ b/src/bindings/python/tests/test_graph/test_data_movement.py @@ -6,64 +6,10 @@ import numpy as np import openvino.runtime.opset8 as ov from openvino.runtime import Type, Shape -from tests.runtime import get_runtime -from tests.test_graph.util import run_op_node def test_reverse_sequence(): - input_data = np.array( - [ - 0, - 0, - 3, - 0, - 6, - 0, - 9, - 0, - 1, - 0, - 4, - 0, - 7, - 0, - 10, - 0, - 2, - 0, - 5, - 0, - 8, - 0, - 11, - 0, - 12, - 0, - 15, - 0, - 18, - 0, - 21, - 0, - 13, - 0, - 16, - 0, - 19, - 0, - 22, - 0, - 14, - 0, - 17, - 0, - 20, - 0, - 23, - 0, - ], - dtype=np.int32, - ).reshape([2, 3, 4, 2]) + input_data = ov.parameter((2, 3, 4, 2), name="input_data", dtype=np.int32) seq_lengths = np.array([1, 2, 1, 2], dtype=np.int32) batch_axis = 2 sequence_axis = 1 @@ -72,135 +18,46 @@ def test_reverse_sequence(): seq_lengths_param = ov.parameter(seq_lengths.shape, name="sequence lengths", dtype=np.int32) model = ov.reverse_sequence(input_param, seq_lengths_param, batch_axis, sequence_axis) - runtime = get_runtime() - computation = runtime.computation(model, input_param, seq_lengths_param) - result = computation(input_data, seq_lengths) - - expected = np.array( - [ - 0, - 0, - 4, - 0, - 6, - 0, - 10, - 0, - 1, - 0, - 3, - 0, - 7, - 0, - 9, - 0, - 2, - 0, - 5, - 0, - 8, - 0, - 11, - 0, - 12, - 0, - 16, - 0, - 18, - 0, - 22, - 0, - 13, - 0, - 15, - 0, - 19, - 0, - 21, - 0, - 14, - 0, - 17, - 0, - 20, - 0, - 23, - 0, - ], - ).reshape([1, 2, 3, 4, 2]) - assert np.allclose(result, expected) + assert model.get_type_name() == "ReverseSequence" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 3, 4, 2] + assert model.get_output_element_type(0) == Type.i32 def test_pad_edge(): - input_data = np.arange(1, 13).reshape([3, 4]).astype(np.int32) pads_begin = np.array([0, 1], dtype=np.int32) pads_end = np.array([2, 3], dtype=np.int32) - input_param = ov.parameter(input_data.shape, name="input", dtype=np.int32) + input_param = ov.parameter((3, 4), name="input", dtype=np.int32) model = ov.pad(input_param, pads_begin, pads_end, "edge") - runtime = get_runtime() - computation = runtime.computation(model, input_param) - result = computation(input_data) - - expected = np.array( - [ - [1, 1, 2, 3, 4, 4, 4, 4], - [5, 5, 6, 7, 8, 8, 8, 8], - [9, 9, 10, 11, 12, 12, 12, 12], - [9, 9, 10, 11, 12, 12, 12, 12], - [9, 9, 10, 11, 12, 12, 12, 12], - ], - ) - assert np.allclose(result, expected) - - -def test_pad_constant(): - input_data = np.arange(1, 13).reshape([3, 4]).astype(np.int32) - pads_begin = np.array([0, 1], dtype=np.int32) - pads_end = np.array([2, 3], dtype=np.int32) - - input_param = ov.parameter(input_data.shape, name="input", dtype=np.int32) - model = ov.pad(input_param, pads_begin, pads_end, "constant", arg_pad_value=np.array(100, dtype=np.int32)) - - runtime = get_runtime() - computation = runtime.computation(model, input_param) - result = computation(input_data) - - expected = np.array( - [ - [100, 1, 2, 3, 4, 100, 100, 100], - [100, 5, 6, 7, 8, 100, 100, 100], - [100, 9, 10, 11, 12, 100, 100, 100], - [100, 100, 100, 100, 100, 100, 100, 100], - [100, 100, 100, 100, 100, 100, 100, 100], - ], - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "Pad" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [5, 8] + assert model.get_output_element_type(0) == Type.i32 def test_select(): cond = np.array([[False, False], [True, False], [True, True]]) then_node = np.array([[-1, 0], [1, 2], [3, 4]], dtype=np.int32) else_node = np.array([[11, 10], [9, 8], [7, 6]], dtype=np.int32) - excepted = np.array([[11, 10], [1, 8], [3, 4]], dtype=np.int32) - result = run_op_node([cond, then_node, else_node], ov.select) - assert np.allclose(result, excepted) + node = ov.select(cond, then_node, else_node) + assert node.get_type_name() == "Select" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 2] + assert node.get_output_element_type(0) == Type.i32 def test_gather_v8_nd(): - indices_type = np.int32 - data_dtype = np.float32 - data = ov.parameter([2, 10, 80, 30, 50], dtype=data_dtype, name="data") - indices = ov.parameter([2, 10, 30, 40, 2], dtype=indices_type, name="indices") + data = ov.parameter([2, 10, 80, 30, 50], dtype=np.float32, name="data") + indices = ov.parameter([2, 10, 30, 40, 2], dtype=np.int32, name="indices") batch_dims = 2 - expected_shape = [2, 10, 30, 40, 50] node = ov.gather_nd(data, indices, batch_dims) assert node.get_type_name() == "GatherND" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [2, 10, 30, 40, 50] assert node.get_output_element_type(0) == Type.f32 @@ -210,10 +67,9 @@ def test_gather_elements(): data = ov.parameter(Shape([2, 5]), dtype=data_dtype, name="data") indices = ov.parameter(Shape([2, 100]), dtype=indices_type, name="indices") axis = 1 - expected_shape = [2, 100] node = ov.gather_elements(data, indices, axis) assert node.get_type_name() == "GatherElements" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [2, 100] assert node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests/test_graph/test_dft.py b/src/bindings/python/tests/test_graph/test_dft.py index 3176fd1f99f..0cb0a70bda6 100644 --- a/src/bindings/python/tests/test_graph/test_dft.py +++ b/src/bindings/python/tests/test_graph/test_dft.py @@ -2,10 +2,10 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 +from openvino.runtime import Type import openvino.runtime.opset9 as ov -from openvino.runtime import Shape import numpy as np -from tests.runtime import get_runtime +import pytest def build_fft_input_data(): @@ -13,110 +13,33 @@ def build_fft_input_data(): return np.random.uniform(0, 1, (2, 10, 10, 2)).astype(np.float32) -def test_dft_1d(): - runtime = get_runtime() +@pytest.mark.parametrize("dims", [[2], [1, 2], [0, 1, 2]]) +def test_dft_dims(dims): input_data = build_fft_input_data() input_tensor = ov.constant(input_data) - input_axes = ov.constant(np.array([2], dtype=np.int64)) + input_axes = ov.constant(np.array(dims, dtype=np.int64)) dft_node = ov.dft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fft(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), - axis=2).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.00001) + assert dft_node.get_type_name() == "DFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == [2, 10, 10, 2] + assert dft_node.get_output_element_type(0) == Type.f32 -def test_dft_2d(): - runtime = get_runtime() +@pytest.mark.parametrize(("dims", "signal_size", "expected_shape"), [ + ([-2], [20], [2, 20, 10, 2]), + ([0, 2], [4, 5], [4, 10, 5, 2]), + ([1, 2], [4, 5], [2, 4, 5, 2]), + ([0, 1, 2], [4, 5, 16], [4, 5, 16, 2]), +]) +def test_dft_signal_size(dims, signal_size, expected_shape): input_data = build_fft_input_data() input_tensor = ov.constant(input_data) - input_axes = ov.constant(np.array([1, 2], dtype=np.int64)) - - dft_node = ov.dft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), - axes=[1, 2]).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000062) - - -def test_dft_3d(): - runtime = get_runtime() - input_data = build_fft_input_data() - input_tensor = ov.constant(input_data) - input_axes = ov.constant(np.array([0, 1, 2], dtype=np.int64)) - - dft_node = ov.dft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fftn(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), - axes=[0, 1, 2]).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.0002) - - -def test_dft_1d_signal_size(): - runtime = get_runtime() - input_data = build_fft_input_data() - input_tensor = ov.constant(input_data) - input_axes = ov.constant(np.array([-2], dtype=np.int64)) - input_signal_size = ov.constant(np.array([20], dtype=np.int64)) + input_axes = ov.constant(np.array(dims, dtype=np.int64)) + input_signal_size = ov.constant(np.array(signal_size, dtype=np.int64)) dft_node = ov.dft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fft(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), n=20, - axis=-2).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.00001) - - -def test_dft_2d_signal_size_1(): - runtime = get_runtime() - input_data = build_fft_input_data() - input_tensor = ov.constant(input_data) - input_axes = ov.constant(np.array([0, 2], dtype=np.int64)) - input_signal_size = ov.constant(np.array([4, 5], dtype=np.int64)) - - dft_node = ov.dft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5], - axes=[0, 2]).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000062) - - -def test_dft_2d_signal_size_2(): - runtime = get_runtime() - input_data = build_fft_input_data() - input_tensor = ov.constant(input_data) - input_axes = ov.constant(np.array([1, 2], dtype=np.int64)) - input_signal_size = ov.constant(np.array([4, 5], dtype=np.int64)) - - dft_node = ov.dft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5], - axes=[1, 2]).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000062) - - -def test_dft_3d_signal_size(): - runtime = get_runtime() - input_data = build_fft_input_data() - input_tensor = ov.constant(input_data) - input_axes = ov.constant(np.array([0, 1, 2], dtype=np.int64)) - input_signal_size = ov.constant(np.array([4, 5, 16], dtype=np.int64)) - - dft_node = ov.dft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fftn(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), - s=[4, 5, 16], axes=[0, 1, 2]).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.0002) + assert dft_node.get_type_name() == "DFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == expected_shape + assert dft_node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests/test_graph/test_einsum.py b/src/bindings/python/tests/test_graph/test_einsum.py index 832524bfbeb..07936005967 100644 --- a/src/bindings/python/tests/test_graph/test_einsum.py +++ b/src/bindings/python/tests/test_graph/test_einsum.py @@ -7,12 +7,10 @@ import numpy as np import pytest from openvino.runtime.utils.types import get_element_type -from tests import xfail_issue_58033 -from tests.runtime import get_runtime -def einsum_op_exec(input_shapes: list, equation: str, data_type: np.dtype, - with_value=False, seed=202104): +def einsum_op_check(input_shapes: list, equation: str, data_type: np.dtype, + seed=202104): """Test Einsum operation for given input shapes, equation, and data type. It generates input data of given shapes and type, receives reference results using numpy, @@ -20,17 +18,11 @@ def einsum_op_exec(input_shapes: list, equation: str, data_type: np.dtype, :param input_shapes: a list of tuples with shapes :param equation: Einsum equation :param data_type: a type of input data - :param with_value: if True - tests output data shape and type along with its value, - otherwise, tests only the output shape and type :param seed: a seed for random generation of input data """ np.random.seed(seed) num_inputs = len(input_shapes) - runtime = get_runtime() - - # set absolute tolerance based on the data type - atol = 0.0 if np.issubdtype(data_type, np.integer) else 1e-04 # generate input tensors graph_inputs = [] @@ -49,55 +41,47 @@ def einsum_op_exec(input_shapes: list, equation: str, data_type: np.dtype, assert list(einsum_model.get_output_shape(0)) == list(expected_result.shape) assert einsum_model.get_output_element_type(0) == get_element_type(data_type) - # check inference result - if with_value: - computation = runtime.computation(einsum_model, *graph_inputs) - actual_result = computation(*np_inputs) - np.allclose(actual_result, expected_result, atol=atol) - @pytest.mark.parametrize("data_type", [np.float32, np.int32]) def test_dot_product(data_type): - einsum_op_exec([5, 5], "i,i->", data_type) + einsum_op_check([5, 5], "i,i->", data_type) @pytest.mark.parametrize("data_type", [np.float32, np.int32]) def test_matrix_multiplication(data_type): - einsum_op_exec([(2, 3), (3, 4)], "ab,bc->ac", data_type) + einsum_op_check([(2, 3), (3, 4)], "ab,bc->ac", data_type) @pytest.mark.parametrize("data_type", [np.float32, np.int32]) def test_batch_trace(data_type): - einsum_op_exec([(2, 3, 3)], "kii->k", data_type) + einsum_op_check([(2, 3, 3)], "kii->k", data_type) @pytest.mark.parametrize("data_type", [np.float32, np.int32]) def test_diagonal_extraction(data_type): - einsum_op_exec([(6, 5, 5)], "kii->ki", data_type) + einsum_op_check([(6, 5, 5)], "kii->ki", data_type) @pytest.mark.parametrize("data_type", [np.float32, np.int32]) def test_transpose(data_type): - einsum_op_exec([(1, 2, 3)], "ijk->kij", data_type) + einsum_op_check([(1, 2, 3)], "ijk->kij", data_type) @pytest.mark.parametrize("data_type", [np.float32, np.int32]) def test_multiple_multiplication(data_type): - einsum_op_exec([(2, 5), (5, 3, 6), (5, 3)], "ab,bcd,bc->ca", data_type) + einsum_op_check([(2, 5), (5, 3, 6), (5, 3)], "ab,bcd,bc->ca", data_type) @pytest.mark.parametrize("data_type", [np.float32, np.int32]) def test_simple_ellipsis(data_type): - einsum_op_exec([(5, 3, 4)], "a...->...", data_type) + einsum_op_check([(5, 3, 4)], "a...->...", data_type) -@xfail_issue_58033 @pytest.mark.parametrize("data_type", [np.float32, np.int32]) def test_multiple_ellipsis(data_type): - einsum_op_exec([(3, 5), 1], "a...,...->a...", data_type, with_value=True) + einsum_op_check([(3, 5), 1], "a...,...->a...", data_type) -@xfail_issue_58033 @pytest.mark.parametrize("data_type", [np.float32, np.int32]) def test_broadcasting_ellipsis(data_type): - einsum_op_exec([(9, 1, 4, 3), (3, 11, 7, 1)], "a...b,b...->a...", data_type, with_value=True) + einsum_op_check([(9, 1, 4, 3), (3, 11, 7, 1)], "a...b,b...->a...", data_type) diff --git a/src/bindings/python/tests/test_graph/test_eye.py b/src/bindings/python/tests/test_graph/test_eye.py index d8397156127..599ccf3e8f0 100644 --- a/src/bindings/python/tests/test_graph/test_eye.py +++ b/src/bindings/python/tests/test_graph/test_eye.py @@ -6,7 +6,6 @@ import openvino.runtime.opset9 as ov import numpy as np import pytest -from tests.runtime import get_runtime from openvino.runtime.utils.types import get_element_type_str from openvino.runtime.utils.types import get_element_type @@ -47,13 +46,6 @@ def test_eye_rectangle(num_rows, num_columns, diagonal_index, out_type): assert eye_node.get_output_element_type(0) == get_element_type(out_type) assert tuple(eye_node.get_output_shape(0)) == expected_results.shape - # TODO: Enable with Eye reference implementation - """runtime = get_runtime() - computation = runtime.computation(eye_node) - eye_results = computation() - assert np.allclose(eye_results, expected_results) - """ - @pytest.mark.parametrize( ("num_rows", "num_columns", "diagonal_index", "batch_shape", "out_type"), @@ -96,10 +88,3 @@ def test_eye_batch_shape(num_rows, num_columns, diagonal_index, batch_shape, out assert eye_node.get_output_size() == 1 assert eye_node.get_output_element_type(0) == get_element_type(out_type) assert tuple(eye_node.get_output_shape(0)) == expected_results.shape - - # TODO: Enable with Eye reference implementation - """runtime = get_runtime() - computation = runtime.computation(eye_node) - eye_results = computation() - assert np.allclose(eye_results, expected_results) - """ diff --git a/src/bindings/python/tests/test_graph/test_gather.py b/src/bindings/python/tests/test_graph/test_gather.py index 3650c4acac3..6375a8cf7e8 100644 --- a/src/bindings/python/tests/test_graph/test_gather.py +++ b/src/bindings/python/tests/test_graph/test_gather.py @@ -2,86 +2,25 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 +from openvino.runtime import Type import openvino.runtime.opset8 as ov import numpy as np - -from tests.test_graph.util import run_op_node +import pytest -def test_gather(): - input_data = np.array( - [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2], np.float32, - ).reshape((3, 3)) - input_indices = np.array([0, 2], np.int32).reshape(1, 2) - input_axis = np.array([1], np.int32) +@pytest.mark.parametrize(("input_shape", "indices", "axis", "expected_shape", "batch_dims"), [ + ((3, 3), (1, 2), [1], [3, 1, 2], []), + ((3, 3), (1, 2), 1, [3, 1, 2], []), + ((2, 5), (2, 3), [1], [2, 3], [1]), + ((2, 5), (2, 3), [1], [2, 2, 3], []), +]) +def test_gather(input_shape, indices, axis, expected_shape, batch_dims): + input_data = ov.parameter(input_shape, name="input_data", dtype=np.float32) + input_indices = ov.parameter(indices, name="input_indices", dtype=np.int32) + input_axis = np.array(axis, np.int32) - expected = np.array([1.0, 1.2, 2.0, 2.2, 3.0, 3.2], dtype=np.float32).reshape( - (3, 1, 2), - ) - - result = run_op_node([input_data], ov.gather, input_indices, input_axis) - assert np.allclose(result, expected) - - -def test_gather_with_scalar_axis(): - input_data = np.array( - [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2], np.float32, - ).reshape((3, 3)) - input_indices = np.array([0, 2], np.int32).reshape(1, 2) - input_axis = np.array(1, np.int32) - - expected = np.array([1.0, 1.2, 2.0, 2.2, 3.0, 3.2], dtype=np.float32).reshape( - (3, 1, 2), - ) - - result = run_op_node([input_data], ov.gather, input_indices, input_axis) - assert np.allclose(result, expected) - - -def test_gather_batch_dims_1(): - - input_data = np.array([[1, 2, 3, 4, 5], - [6, 7, 8, 9, 10]], np.float32) - - input_indices = np.array([[0, 0, 4], - [4, 0, 0]], np.int32) - input_axis = np.array([1], np.int32) - batch_dims = 1 - - expected = np.array([[1, 1, 5], - [10, 6, 6]], np.float32) - - result = run_op_node([input_data], ov.gather, input_indices, input_axis, batch_dims) - assert np.allclose(result, expected) - - -def test_gather_negative_indices(): - input_data = np.array( - [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2], np.float32, - ).reshape((3, 3)) - input_indices = np.array([0, -1], np.int32).reshape(1, 2) - input_axis = np.array([1], np.int32) - - expected = np.array([1.0, 1.2, 2.0, 2.2, 3.0, 3.2], dtype=np.float32).reshape( - (3, 1, 2), - ) - - result = run_op_node([input_data], ov.gather, input_indices, input_axis) - assert np.allclose(result, expected) - - -def test_gather_batch_dims_1_negative_indices(): - - input_data = np.array([[1, 2, 3, 4, 5], - [6, 7, 8, 9, 10]], np.float32) - - input_indices = np.array([[0, 1, -2], - [-2, 0, 0]], np.int32) - input_axis = np.array([1], np.int32) - batch_dims = 1 - - expected = np.array([[1, 2, 4], - [9, 6, 6]], np.float32) - - result = run_op_node([input_data], ov.gather, input_indices, input_axis, batch_dims) - assert np.allclose(result, expected) + node = ov.gather(input_data, input_indices, input_axis, *batch_dims) + assert node.get_type_name() == "Gather" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests/test_graph/test_idft.py b/src/bindings/python/tests/test_graph/test_idft.py index 48e5fae8fee..7f76359798d 100644 --- a/src/bindings/python/tests/test_graph/test_idft.py +++ b/src/bindings/python/tests/test_graph/test_idft.py @@ -2,9 +2,10 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 +from openvino.runtime import Type import openvino.runtime.opset8 as ov import numpy as np -from tests.runtime import get_runtime +import pytest def get_data(): @@ -13,7 +14,6 @@ def get_data(): def test_idft_1d(): - runtime = get_runtime() expected_results = get_data() complex_input_data = np.fft.fft(np.squeeze(expected_results.view(dtype=np.complex64), axis=-1), axis=2).astype(np.complex64) @@ -22,28 +22,32 @@ def test_idft_1d(): input_axes = ov.constant(np.array([2], dtype=np.int64)) dft_node = ov.idft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) + assert dft_node.get_output_element_type(0) == Type.f32 -def test_idft_2d(): - runtime = get_runtime() +@pytest.mark.parametrize(("axes"), [ + ([1, 2]), + ([0, 1, 2]), +]) +def test_idft_2d_3d(axes): expected_results = get_data() complex_input_data = np.fft.fft2(np.squeeze(expected_results.view(dtype=np.complex64), axis=-1), - axes=[1, 2]).astype(np.complex64) + axes=axes).astype(np.complex64) input_data = np.stack((complex_input_data.real, complex_input_data.imag), axis=-1) input_tensor = ov.constant(input_data) - input_axes = ov.constant(np.array([1, 2], dtype=np.int64)) + input_axes = ov.constant(np.array(axes, dtype=np.int64)) dft_node = ov.idft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) + assert dft_node.get_output_element_type(0) == Type.f32 def test_idft_3d(): - runtime = get_runtime() expected_results = get_data() complex_input_data = np.fft.fft2(np.squeeze(expected_results.view(dtype=np.complex64), axis=-1), axes=[0, 1, 2]).astype(np.complex64) @@ -52,70 +56,71 @@ def test_idft_3d(): input_axes = ov.constant(np.array([0, 1, 2], dtype=np.int64)) dft_node = ov.idft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - assert np.allclose(dft_results, expected_results, atol=0.000003) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) + assert dft_node.get_output_element_type(0) == Type.f32 def test_idft_1d_signal_size(): - runtime = get_runtime() input_data = get_data() input_tensor = ov.constant(input_data) input_axes = ov.constant(np.array([-2], dtype=np.int64)) input_signal_size = ov.constant(np.array([20], dtype=np.int64)) dft_node = ov.idft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() np_results = np.fft.ifft(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), n=20, axis=-2).astype(np.complex64) expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) + assert dft_node.get_output_element_type(0) == Type.f32 def test_idft_2d_signal_size_1(): - runtime = get_runtime() input_data = get_data() input_tensor = ov.constant(input_data) input_axes = ov.constant(np.array([0, 2], dtype=np.int64)) input_signal_size = ov.constant(np.array([4, 5], dtype=np.int64)) dft_node = ov.idft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() np_results = np.fft.ifft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5], axes=[0, 2]).astype(np.complex64) expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) + assert dft_node.get_output_element_type(0) == Type.f32 def test_idft_2d_signal_size_2(): - runtime = get_runtime() input_data = get_data() input_tensor = ov.constant(input_data) input_axes = ov.constant(np.array([1, 2], dtype=np.int64)) input_signal_size = ov.constant(np.array([4, 5], dtype=np.int64)) dft_node = ov.idft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() np_results = np.fft.ifft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5], axes=[1, 2]).astype(np.complex64) expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) + assert dft_node.get_output_element_type(0) == Type.f32 def test_idft_3d_signal_size(): - runtime = get_runtime() input_data = get_data() input_tensor = ov.constant(input_data) input_axes = ov.constant(np.array([0, 1, 2], dtype=np.int64)) input_signal_size = ov.constant(np.array([4, 5, 16], dtype=np.int64)) dft_node = ov.idft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() np_results = np.fft.ifftn(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5, 16], axes=[0, 1, 2]).astype(np.complex64) expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) + assert dft_node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests/test_graph/test_if.py b/src/bindings/python/tests/test_graph/test_if.py index 2db365c15c9..def8739a2d7 100644 --- a/src/bindings/python/tests/test_graph/test_if.py +++ b/src/bindings/python/tests/test_graph/test_if.py @@ -6,8 +6,6 @@ import numpy as np import openvino.runtime.opset8 as ov from openvino.runtime import Model -from tests.runtime import get_runtime - from openvino.runtime.op.util import InvariantInputDescription, BodyOutputDescription @@ -149,34 +147,33 @@ def check_results(results, expected_results): def check_if(if_model, cond_val, exp_results): last_node = if_model(cond_val) - runtime = get_runtime() - computation = runtime.computation(last_node) - results = computation() - check_results(results, exp_results) + assert last_node.get_type_name() == exp_results[0] + assert last_node.get_output_size() == exp_results[1] + assert list(last_node.get_output_shape(0)) == exp_results[2] def test_if_with_two_outputs(): check_if(create_simple_if_with_two_outputs, True, - [np.array([10], dtype=np.float32), np.array([-20], dtype=np.float32)]) + ["If", 2, []]) check_if(create_simple_if_with_two_outputs, False, - [np.array([17], dtype=np.float32), np.array([16], dtype=np.float32)]) + ["If", 2, []]) def test_diff_if_with_two_outputs(): check_if(create_diff_if_with_two_outputs, True, - [np.array([10], dtype=np.float32), np.array([6, 4], dtype=np.float32)]) + ["If", 2, []]) check_if(create_diff_if_with_two_outputs, False, - [np.array([4], dtype=np.float32), np.array([12, 16], dtype=np.float32)]) + ["If", 2, []]) def test_simple_if(): - check_if(simple_if, True, [np.array([6, 4], dtype=np.float32)]) - check_if(simple_if, False, [np.array([5, 5], dtype=np.float32)]) + check_if(simple_if, True, ["Relu", 1, [2]]) + check_if(simple_if, False, ["Relu", 1, [2]]) def test_simple_if_without_body_parameters(): - check_if(simple_if_without_parameters, True, [np.array([0.7], dtype=np.float32)]) - check_if(simple_if_without_parameters, False, [np.array([9.0], dtype=np.float32)]) + check_if(simple_if_without_parameters, True, ["Relu", 1, []]) + check_if(simple_if_without_parameters, False, ["Relu", 1, []]) def test_simple_if_basic(): diff --git a/src/bindings/python/tests/test_graph/test_log_softmax.py b/src/bindings/python/tests/test_graph/test_log_softmax.py index 134f41e578a..94d4aa5c0f8 100644 --- a/src/bindings/python/tests/test_graph/test_log_softmax.py +++ b/src/bindings/python/tests/test_graph/test_log_softmax.py @@ -8,8 +8,7 @@ from openvino.runtime import Shape, Type def test_log_softmax(): - float_dtype = np.float32 - data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data") + data = ov.parameter(Shape([3, 10]), dtype=np.float32, name="data") node = ov.log_softmax(data, 1) assert node.get_type_name() == "LogSoftmax" diff --git a/src/bindings/python/tests/test_graph/test_manager.py b/src/bindings/python/tests/test_graph/test_manager.py index a86b94e3e17..eee82c7b6f9 100644 --- a/src/bindings/python/tests/test_graph/test_manager.py +++ b/src/bindings/python/tests/test_graph/test_manager.py @@ -3,14 +3,13 @@ # flake8: noqa -import json import os import numpy as np import pytest import openvino.runtime.opset8 as ov -from openvino.runtime import Model, PartialShape, Shape +from openvino.runtime import Model from openvino.runtime.passes import Manager from tests.test_graph.util import count_ops_of_type from openvino.runtime import Core diff --git a/src/bindings/python/tests/test_graph/test_normalization.py b/src/bindings/python/tests/test_graph/test_normalization.py index f8a12828869..ae765071e3b 100644 --- a/src/bindings/python/tests/test_graph/test_normalization.py +++ b/src/bindings/python/tests/test_graph/test_normalization.py @@ -4,44 +4,26 @@ import numpy as np +from openvino.runtime import Type import openvino.runtime.opset8 as ov -from tests.runtime import get_runtime -from tests.test_graph.util import run_op_node def test_lrn(): input_image_shape = (2, 3, 2, 1) input_image = np.arange(int(np.prod(input_image_shape))).reshape(input_image_shape).astype("f") axes = np.array([1], dtype=np.int64) - runtime = get_runtime() model = ov.lrn(ov.constant(input_image), ov.constant(axes), alpha=1.0, beta=2.0, bias=1.0, size=3) - computation = runtime.computation(model) - result = computation() - assert np.allclose( - result, - np.array( - [ - [[[0.0], [0.05325444]], [[0.03402646], [0.01869806]], [[0.06805293], [0.03287071]]], - [[[0.00509002], [0.00356153]], [[0.00174719], [0.0012555]], [[0.00322708], [0.00235574]]], - ], - dtype=np.float32, - ), - ) + assert model.get_type_name() == "LRN" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 3, 2, 1] + assert model.get_output_element_type(0) == Type.f32 # Test LRN default parameter values model = ov.lrn(ov.constant(input_image), ov.constant(axes)) - computation = runtime.computation(model) - result = computation() - assert np.allclose( - result, - np.array( - [ - [[[0.0], [0.35355338]], [[0.8944272], [1.0606602]], [[1.7888544], [1.767767]]], - [[[0.93704253], [0.97827977]], [[1.2493901], [1.2577883]], [[1.5617375], [1.5372968]]], - ], - dtype=np.float32, - ), - ) + assert model.get_type_name() == "LRN" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 3, 2, 1] + assert model.get_output_element_type(0) == Type.f32 def test_lrn_factory(): @@ -50,94 +32,55 @@ def test_lrn_factory(): bias = 2.0 nsize = 3 axis = np.array([1], dtype=np.int32) - inputs = np.array( - [ - [ - [ - [0.31403765, -0.16793324, 1.388258, -0.6902954], - [-0.3994045, -0.7833511, -0.30992958, 0.3557573], - [-0.4682631, 1.1741459, -2.414789, -0.42783254], - ], - [ - [-0.82199496, -0.03900861, -0.43670088, -0.53810567], - [-0.10769883, 0.75242394, -0.2507971, 1.0447186], - [-1.4777364, 0.19993274, 0.925649, -2.282516], - ], - ], - ], - dtype=np.float32, - ) - excepted = np.array( - [ - [ - [ - [0.22205527, -0.11874668, 0.98161197, -0.4881063], - [-0.2824208, -0.553902, -0.21915273, 0.2515533], - [-0.33109877, 0.8302269, -1.7073234, -0.3024961], - ], - [ - [-0.5812307, -0.02758324, -0.30878326, -0.38049328], - [-0.07615435, 0.53203356, -0.17733987, 0.7387126], - [-1.0448756, 0.14137045, 0.6544598, -1.6138376], - ], - ], - ], - dtype=np.float32, - ) - result = run_op_node([inputs], ov.lrn, axis, alpha, beta, bias, nsize) + inputs = ov.parameter((1, 2, 3, 4), name="inputs", dtype=np.float32) - assert np.allclose(result, excepted) + node = ov.lrn(inputs, axis, alpha, beta, bias, nsize) + assert node.get_type_name() == "LRN" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [1, 2, 3, 4] + assert node.get_output_element_type(0) == Type.f32 -def test_batch_norm_inference(): - data = np.array([[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]], dtype=np.float32) - gamma = np.array([2.0, 3.0, 4.0], dtype=np.float32) - beta = np.array([0.0, 0.0, 0.0], dtype=np.float32) - mean = np.array([0.0, 0.0, 0.0], dtype=np.float32) - variance = np.array([1.0, 1.0, 1.0], dtype=np.float32) +def test_batch_norm(): + data = ov.parameter((2, 3), name="data", dtype=np.float32) + gamma = ov.parameter((3,), name="gamma", dtype=np.float32) + beta = ov.parameter((3,), name="beta", dtype=np.float32) + mean = ov.parameter((3,), name="mean", dtype=np.float32) + variance = ov.parameter((3,), name="variance", dtype=np.float32) epsilon = 9.99e-06 - excepted = np.array([[2.0, 6.0, 12.0], [-2.0, -6.0, -12.0]], dtype=np.float32) - result = run_op_node([data, gamma, beta, mean, variance], ov.batch_norm_inference, epsilon) - - assert np.allclose(result, excepted) + node = ov.batch_norm_inference(data, gamma, beta, mean, variance, epsilon) + assert node.get_type_name() == "BatchNormInference" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [2, 3] + assert node.get_output_element_type(0) == Type.f32 def test_mvn_no_variance(): - data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, - 1, 2, 3, 4, 5, 6, 7, 8, 9, - 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.float32).reshape([1, 3, 3, 3]) + data = ov.parameter((1, 3, 3, 3), name="data", dtype=np.float32) axes = np.array([2, 3], dtype=np.int64) epsilon = 1e-9 normalize_variance = False eps_mode = "outside_sqrt" - excepted = np.array([-4, -3, -2, -1, 0, 1, 2, 3, 4, - -4, -3, -2, -1, 0, 1, 2, 3, 4, - -4, -3, -2, -1, 0, 1, 2, 3, 4], dtype=np.float32).reshape([1, 3, 3, 3]) - result = run_op_node([data], ov.mvn, axes, normalize_variance, epsilon, eps_mode) + node = ov.mvn(data, axes, normalize_variance, epsilon, eps_mode) - assert np.allclose(result, excepted) + assert node.get_type_name() == "MVN" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [1, 3, 3, 3] + assert node.get_output_element_type(0) == Type.f32 def test_mvn(): - data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, - 1, 2, 3, 4, 5, 6, 7, 8, 9, - 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.float32).reshape([1, 3, 3, 3]) + data = ov.parameter((1, 3, 3, 3), name="data", dtype=np.float32) axes = np.array([2, 3], dtype=np.int64) epsilon = 1e-9 normalize_variance = True eps_mode = "outside_sqrt" - excepted = np.array([-1.5491934, -1.161895, -0.7745967, - -0.38729835, 0., 0.38729835, - 0.7745967, 1.161895, 1.5491934, - -1.5491934, -1.161895, -0.7745967, - -0.38729835, 0., 0.38729835, - 0.7745967, 1.161895, 1.5491934, - -1.5491934, -1.161895, -0.7745967, - -0.38729835, 0., 0.38729835, - 0.7745967, 1.161895, 1.5491934], dtype=np.float32).reshape([1, 3, 3, 3]) - result = run_op_node([data], ov.mvn, axes, normalize_variance, epsilon, eps_mode) + node = ov.mvn(data, axes, normalize_variance, epsilon, eps_mode) - assert np.allclose(result, excepted) + assert node.get_type_name() == "MVN" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [1, 3, 3, 3] + assert node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests/test_graph/test_ops.py b/src/bindings/python/tests/test_graph/test_ops.py index be0fa20e142..f53d8fe4ae4 100644 --- a/src/bindings/python/tests/test_graph/test_ops.py +++ b/src/bindings/python/tests/test_graph/test_ops.py @@ -4,184 +4,42 @@ # flake8: noqa import numpy as np +import pytest import openvino.runtime.opset8 as ov -from openvino.runtime import AxisSet, Model, Shape, Type +from openvino.runtime import AxisSet, Shape, Type from openvino.runtime.op import Constant, Parameter -from tests.runtime import get_runtime - - -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 binary_op_ref(op_str, a, b): - - if op_str == "+" or op_str == "Add": - return a + b - elif op_str == "-" or op_str == "Sub": - return a - b - elif op_str == "*" or op_str == "Mul": - return a * b - elif op_str == "/" or op_str == "Div": - return a / b - elif op_str == "Dot": - return np.dot(a, b) - elif op_str == "Equal": - return np.equal(a, b) - elif op_str == "Greater": - return np.greater(a, b) - elif op_str == "GreaterEq": - return np.greater_equal(a, b) - elif op_str == "Less": - return np.less(a, b) - elif op_str == "LessEq": - return np.less_equal(a, b) - elif op_str == "Maximum": - return np.maximum(a, b) - elif op_str == "Minimum": - return np.minimum(a, b) - elif op_str == "NotEqual": - return np.not_equal(a, b) - elif op_str == "Power": - return np.power(a, b) - - -def binary_op_exec(op_str): +@pytest.mark.parametrize(("ov_op", "expected_ov_str", "expected_type"), [ + (lambda a, b: a + b, "Add", Type.f32), + (ov.add, "Add", Type.f32), + (lambda a, b: a - b, "Subtract", Type.f32), + (ov.subtract, "Subtract", Type.f32), + (lambda a, b: a * b, "Multiply", Type.f32), + (ov.multiply, "Multiply", Type.f32), + (lambda a, b: a / b, "Divide", Type.f32), + (ov.divide, "Divide", Type.f32), + (ov.maximum, "Maximum", Type.f32), + (ov.minimum, "Minimum", Type.f32), + (ov.power, "Power", Type.f32), + (ov.equal, "Equal", Type.boolean), + (ov.greater, "Greater", Type.boolean), + (ov.greater_equal, "GreaterEqual", Type.boolean), + (ov.less, "Less", Type.boolean), + (ov.less_equal, "LessEqual", Type.boolean), + (ov.not_equal, "NotEqual", Type.boolean), +]) +def test_binary_op(ov_op, expected_ov_str, expected_type): element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) - parameter_list = [A, B] - function = Model([binary_op(op_str, A, B)], parameter_list, "test") + node = ov_op(A, B) - a_arr = np.array([[1, 6], [7, 4]], dtype=np.float32) - b_arr = np.array([[5, 2], [3, 8]], dtype=np.float32) - - runtime = get_runtime() - computation = runtime.computation(function, A, B) - result = computation(a_arr, b_arr)[0] - - expected = binary_op_ref(op_str, a_arr, b_arr) - assert np.allclose(result, expected) - - -def binary_op_comparison(op_str): - - element_type = Type.f32 - shape = Shape([2, 2]) - A = Parameter(element_type, shape) - B = Parameter(element_type, shape) - parameter_list = [A, B] - function = Model([binary_op(op_str, A, B)], parameter_list, "test") - a_arr = np.array([[1, 5], [3, 2]], dtype=np.float32) - b_arr = np.array([[2, 4], [3, 1]], dtype=np.float32) - - runtime = get_runtime() - computation = runtime.computation(function, A, B) - result = computation(a_arr, b_arr)[0] - - expected = binary_op_ref(op_str, a_arr, b_arr) - assert np.allclose(result, expected) - - -def test_add(): - binary_op_exec("+") - - -def test_add_op(): - binary_op_exec("Add") - - -def test_sub(): - binary_op_exec("-") - - -def test_sub_op(): - binary_op_exec("Sub") - - -def test_mul(): - binary_op_exec("*") - - -def test_mul_op(): - binary_op_exec("Mul") - - -def test_div(): - binary_op_exec("/") - - -def test_div_op(): - binary_op_exec("Div") - - -def test_maximum(): - binary_op_exec("Maximum") - - -def test_minimum(): - binary_op_exec("Minimum") - - -def test_power(): - binary_op_exec("Power") - - -def test_greater(): - binary_op_comparison("Greater") - - -def test_greater_eq(): - binary_op_comparison("GreaterEq") - - -def test_less(): - binary_op_comparison("Less") - - -def test_less_eq(): - binary_op_comparison("LessEq") - - -def test_not_equal(): - binary_op_comparison("NotEqual") + assert node.get_type_name() == expected_ov_str + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [2, 2] + assert node.get_output_element_type(0) == expected_type def test_add_with_mul(): @@ -191,341 +49,93 @@ def test_add_with_mul(): 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") + node = ov.multiply(ov.add(A, B), C) - 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) + assert node.get_type_name() == "Multiply" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [4] + assert node.get_output_element_type(0) == Type.f32 -def unary_op(op_str, a): - if op_str == "Abs": - return ov.abs(a) - elif op_str == "Acos": - return ov.acos(a) - elif op_str == "Acosh": - return ov.acosh(a) - elif op_str == "Asin": - return ov.asin(a) - elif op_str == "Asinh": - return ov.asinh(a) - elif op_str == "Atan": - return ov.atan(a) - elif op_str == "Atanh": - return ov.atanh(a) - elif op_str == "Ceiling": - return ov.ceiling(a) - elif op_str == "Cos": - return ov.cos(a) - elif op_str == "Cosh": - return ov.cosh(a) - elif op_str == "Floor": - return ov.floor(a) - elif op_str == "log": - return ov.log(a) - elif op_str == "exp": - return ov.exp(a) - elif op_str == "negative": - return ov.negative(a) - elif op_str == "Sign": - return ov.sign(a) - elif op_str == "Sin": - return ov.sin(a) - elif op_str == "Sinh": - return ov.sinh(a) - elif op_str == "Sqrt": - return ov.sqrt(a) - elif op_str == "Tan": - return ov.tan(a) - elif op_str == "Tanh": - return ov.tanh(a) +@pytest.mark.parametrize(("ov_op", "expected_ov_str"), [ + (ov.abs, "Abs"), + (ov.acos, "Acos"), + (ov.acosh, "Acosh"), + (ov.asin, "Asin"), + (ov.asinh, "Asinh"), + (ov.atan, "Atan"), + (ov.atanh, "Atanh"), + (ov.ceiling, "Ceiling"), + (ov.cos, "Cos"), + (ov.cosh, "Cosh"), + (ov.floor, "Floor"), + (ov.log, "Log"), + (ov.exp, "Exp"), + (ov.negative, "Negative"), + (ov.sign, "Sign"), + (ov.sin, "Sin"), + (ov.sinh, "Sinh"), + (ov.sqrt, "Sqrt"), + (ov.tan, "Tan"), + (ov.tanh, "Tanh"), +]) +def test_unary_op(ov_op, expected_ov_str): - -def unary_op_ref(op_str, a): - if op_str == "Abs": - return np.abs(a) - elif op_str == "Acos": - return np.arccos(a) - elif op_str == "Acosh": - return np.arccosh(a) - elif op_str == "Asin": - return np.arcsin(a) - elif op_str == "Asinh": - return np.arcsinh(a) - elif op_str == "Atan": - return np.arctan(a) - elif op_str == "Atanh": - return np.arctanh(a) - elif op_str == "Ceiling": - return np.ceil(a) - elif op_str == "Cos": - return np.cos(a) - elif op_str == "Cosh": - return np.cosh(a) - elif op_str == "Floor": - return np.floor(a) - elif op_str == "log": - return np.log(a) - elif op_str == "exp": - return np.exp(a) - elif op_str == "negative": - return np.negative(a) - elif op_str == "Reverse": - return np.fliplr(a) - elif op_str == "Sign": - return np.sign(a) - elif op_str == "Sin": - return np.sin(a) - elif op_str == "Sinh": - return np.sinh(a) - elif op_str == "Sqrt": - return np.sqrt(a) - elif op_str == "Tan": - return np.tan(a) - elif op_str == "Tanh": - return np.tanh(a) - - -def unary_op_exec(op_str, input_list): - """ - input_list needs to have deep length of 4 - """ element_type = Type.f32 - shape = Shape(np.array(input_list).shape) + shape = Shape([4]) A = Parameter(element_type, shape) - parameter_list = [A] - function = Model([unary_op(op_str, A)], parameter_list, "test") + node = ov_op(A) - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(np.array(input_list, dtype=np.float32))[0] - - expected = unary_op_ref(op_str, np.array(input_list, dtype=np.float32)) - assert np.allclose(result, expected) - - -def test_abs(): - input_list = [-1, 0, 1, 2] - op_str = "Abs" - unary_op_exec(op_str, input_list) - - -def test_acos(): - input_list = [-1, 0, 0.5, 1] - op_str = "Acos" - unary_op_exec(op_str, input_list) - - -def test_acosh(): - input_list = [2., 3., 1.5, 1.0] - op_str = "Acosh" - unary_op_exec(op_str, input_list) - - -def test_asin(): - input_list = [-1, 0, 0.5, 1] - op_str = "Asin" - unary_op_exec(op_str, input_list) - - -def test_asinh(): - input_list = [-1, 0, 0.5, 1] - op_str = "Asinh" - unary_op_exec(op_str, input_list) - - -def test_atan(): - input_list = [-1, 0, 0.5, 1] - op_str = "Atan" - unary_op_exec(op_str, input_list) - - -def test_atanh(): - input_list = [-1, 0, 0.5, 1] - op_str = "Atanh" - unary_op_exec(op_str, input_list) - - -def test_ceiling(): - input_list = [0.5, 0, 0.4, 0.5] - op_str = "Ceiling" - unary_op_exec(op_str, input_list) - - -def test_cos(): - input_list = [0, 0.7, 1.7, 3.4] - op_str = "Cos" - unary_op_exec(op_str, input_list) - - -def test_cosh(): - input_list = [-1, 0.0, 0.5, 1] - op_str = "Cosh" - unary_op_exec(op_str, input_list) - - -def test_floor(): - input_list = [-0.5, 0, 0.4, 0.5] - op_str = "Floor" - unary_op_exec(op_str, input_list) - - -def test_log(): - input_list = [1, 2, 3, 4] - op_str = "log" - unary_op_exec(op_str, input_list) - - -def test_exp(): - input_list = [-1, 0, 1, 2] - op_str = "exp" - unary_op_exec(op_str, input_list) - - -def test_negative(): - input_list = [-1, 0, 1, 2] - op_str = "negative" - unary_op_exec(op_str, input_list) - - -def test_sign(): - input_list = [-1, 0, 0.5, 1] - op_str = "Sign" - unary_op_exec(op_str, input_list) - - -def test_sin(): - input_list = [0, 0.7, 1.7, 3.4] - op_str = "Sin" - unary_op_exec(op_str, input_list) - - -def test_sinh(): - input_list = [-1, 0.0, 0.5, 1] - op_str = "Sinh" - unary_op_exec(op_str, input_list) - - -def test_sqrt(): - input_list = [0.0, 0.5, 1, 2] - op_str = "Sqrt" - unary_op_exec(op_str, input_list) - - -def test_tan(): - input_list = [-np.pi / 4, 0, np.pi / 8, np.pi / 8] - op_str = "Tan" - unary_op_exec(op_str, input_list) - - -def test_tanh(): - input_list = [-1, 0, 0.5, 1] - op_str = "Tanh" - unary_op_exec(op_str, input_list) + assert node.get_type_name() == expected_ov_str + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == list(shape) + assert node.get_output_element_type(0) == Type.f32 def test_reshape(): - element_type = Type.f32 shape = Shape([2, 3]) A = Parameter(element_type, shape) - parameter_list = [A] - function = Model([ov.reshape(A, Shape([3, 2]), special_zero=False)], parameter_list, "test") + node = ov.reshape(A, Shape([3, 2]), special_zero=False) - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(np.array(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), dtype=np.float32))[0] - - expected = np.reshape(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), (3, 2)) - assert np.allclose(result, expected) + assert node.get_type_name() == "Reshape" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 2] + assert node.get_output_element_type(0) == element_type def test_broadcast(): - element_type = Type.f32 A = Parameter(element_type, Shape([3])) - parameter_list = [A] - function = Model([ov.broadcast(A, [3, 3])], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(np.array([1, 2, 3], dtype=np.float32))[0] - - a_arr = np.array([[0], [0], [0]], dtype=np.float32) - b_arr = np.array([[1, 2, 3]], dtype=np.float32) - expected = np.add(a_arr, b_arr) - assert np.allclose(result, expected) + node = ov.broadcast(A, [3, 3]) + assert node.get_type_name() == "Broadcast" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 3] + assert node.get_output_element_type(0) == element_type -def test_constant(): - element_type = Type.f32 - parameter_list = [] - function = Model([Constant(element_type, Shape([3, 3]), list(range(9)))], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation()[0] - - expected = np.arange(9).reshape(3, 3) - assert np.allclose(result, expected) - - -def test_constant_opset_ov_type(): - parameter_list = [] - function = Model([ov.constant(np.arange(9).reshape(3, 3), Type.f32)], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation()[0] - - expected = np.arange(9).reshape(3, 3) - assert np.allclose(result, expected) - - -def test_constant_opset_numpy_type(): - parameter_list = [] - function = Model([ov.constant(np.arange(9).reshape(3, 3), np.float32)], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation()[0] - - expected = np.arange(9).reshape(3, 3) - assert np.allclose(result, expected) +@pytest.mark.parametrize("node", [ + Constant(Type.f32, Shape([3, 3]), list(range(9))), + ov.constant(np.arange(9).reshape(3, 3), Type.f32), + ov.constant(np.arange(9).reshape(3, 3), np.float32) +]) +def test_constant(node): + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 3] + assert node.get_output_element_type(0) == Type.f32 def test_concat(): - element_type = Type.f32 A = Parameter(element_type, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) - parameter_list = [A, B, C] - axis = 0 - function = Model([ov.concat([A, B, C], axis)], parameter_list, "test") - - a_arr = np.array([[1, 2]], dtype=np.float32) - b_arr = np.array([[5, 6]], dtype=np.float32) - c_arr = np.array([[7, 8]], dtype=np.float32) - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(a_arr, b_arr, c_arr)[0] - - expected = np.concatenate((a_arr, b_arr, c_arr), axis) - assert np.allclose(result, expected) + node = ov.concat([A, B, C], axis=0) + assert node.get_type_name() == "Concat" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 2] + assert node.get_output_element_type(0) == element_type def test_axisset(): @@ -549,29 +159,17 @@ def test_select(): A = Parameter(Type.boolean, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) - parameter_list = [A, B, C] + node = ov.select(A, B, C) + assert node.get_type_name() == "Select" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [1, 2] + assert node.get_output_element_type(0) == element_type - function = Model([ov.select(A, B, C)], parameter_list, "test") - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation( - np.array([[True, False]], dtype=bool), - np.array([[5, 6]], dtype=np.float32), - np.array([[7, 8]], dtype=np.float32), - )[0] - - expected = np.array([[5, 8]]) - assert np.allclose(result, expected) - -def test_max_pool(): - # test 1d +def test_max_pool_1d(): element_type = Type.f32 shape = Shape([1, 1, 10]) A = Parameter(element_type, shape) - parameter_list = [A] - - input_arr = np.arange(10, dtype=np.float32).reshape([1, 1, 10]) window_shape = [3] strides = [1] * len(window_shape) @@ -593,19 +191,25 @@ def test_max_pool(): auto_pad, idx_elem_type, ) - function = Model([model], parameter_list, "test") + assert model.get_type_name() == "MaxPool" + assert model.get_output_size() == 2 + assert list(model.get_output_shape(0)) == [1, 1, 8] + assert list(model.get_output_shape(1)) == [1, 1, 8] + assert model.get_output_element_type(0) == element_type + assert model.get_output_element_type(1) == Type.i32 - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(input_arr)[0] - - expected = (np.arange(8) + 2).reshape(1, 1, 8) - assert np.allclose(result, expected) - - # test 1d with strides +def test_max_pool_1d_with_strides(): + element_type = Type.f32 + shape = Shape([1, 1, 10]) + A = Parameter(element_type, shape) + window_shape = [3] strides = [2] pads_begin = [0] * len(window_shape) + dilations = [1] * len(window_shape) pads_end = [0] * len(window_shape) + rounding_type = "floor" + auto_pad = "explicit" + idx_elem_type = "i32" model = ov.max_pool( A, @@ -618,23 +222,22 @@ def test_max_pool(): auto_pad, idx_elem_type, ) - function = Model([model], parameter_list, "test") - size = 4 - computation = runtime.computation(function, *parameter_list) - result = computation(input_arr)[0] + assert model.get_type_name() == "MaxPool" + assert model.get_output_size() == 2 + assert list(model.get_output_shape(0)) == [1, 1, 4] + assert list(model.get_output_shape(1)) == [1, 1, 4] + assert model.get_output_element_type(0) == element_type + assert model.get_output_element_type(1) == Type.i32 - expected = ((np.arange(size) + 1) * 2).reshape(1, 1, size) - assert np.allclose(result, expected) - - # test 2d +def test_max_pool_2d(): element_type = Type.f32 shape = Shape([1, 1, 10, 10]) A = Parameter(element_type, shape) - parameter_list = [A] - - input_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) window_shape = [3, 3] + rounding_type = "floor" + auto_pad = "explicit" + idx_elem_type = "i32" strides = [1, 1] dilations = [1, 1] @@ -652,19 +255,26 @@ def test_max_pool(): auto_pad, idx_elem_type, ) - function = Model([model], parameter_list, "test") + assert model.get_type_name() == "MaxPool" + assert model.get_output_size() == 2 + assert list(model.get_output_shape(0)) == [1, 1, 8, 8] + assert list(model.get_output_shape(1)) == [1, 1, 8, 8] + assert model.get_output_element_type(0) == element_type + assert model.get_output_element_type(1) == Type.i32 - computation = runtime.computation(function, *parameter_list) - result = computation(input_arr)[0] - expected = ((np.arange(100).reshape(10, 10))[2:, 2:]).reshape(1, 1, 8, 8) - assert np.allclose(result, expected) - - # test 2d with strides +def test_max_pool_2d_with_strides(): + element_type = Type.f32 + shape = Shape([1, 1, 10, 10]) + A = Parameter(element_type, shape) strides = [2, 2] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] + window_shape = [3, 3] + rounding_type = "floor" + auto_pad = "explicit" + idx_elem_type = "i32" model = ov.max_pool( A, @@ -677,13 +287,12 @@ def test_max_pool(): auto_pad, idx_elem_type, ) - function = Model([model], parameter_list, "test") - computation = runtime.computation(function, *parameter_list) - result = computation(input_arr)[0] - - size = 4 - expected = ((np.arange(100).reshape(10, 10))[2::2, 2::2]).reshape(1, 1, size, size) - assert np.allclose(result, expected) + assert model.get_type_name() == "MaxPool" + assert model.get_output_size() == 2 + assert list(model.get_output_shape(0)) == [1, 1, 4, 4] + assert list(model.get_output_shape(1)) == [1, 1, 4, 4] + assert model.get_output_element_type(0) == element_type + assert model.get_output_element_type(1) == Type.i32 def convolution2d( @@ -733,15 +342,11 @@ def convolution2d( def test_convolution_simple(): - element_type = Type.f32 image_shape = Shape([1, 1, 16, 16]) filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) - parameter_list = [data, filters] - - image_arr = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][0][0] = -1 filter_arr[0][0][1][1] = -1 @@ -755,14 +360,11 @@ def test_convolution_simple(): dilations = [1, 1] model = ov.convolution(data, filters, strides, pads_begin, pads_end, dilations) - function = Model([model], parameter_list, "test") - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(image_arr, filter_arr)[0] - - expected = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14) - assert np.allclose(result, expected) + assert model.get_type_name() == "Convolution" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 14, 14] + assert model.get_output_element_type(0) == element_type def test_convolution_with_strides(): @@ -772,9 +374,6 @@ def test_convolution_with_strides(): filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) - parameter_list = [data, filters] - - image_arr = np.arange(100, dtype=np.float32).reshape([1, 1, 10, 10]) filter_arr = np.zeros(9, dtype=np.float32).reshape([1, 1, 3, 3]) filter_arr[0][0][1][1] = 1 strides = [2, 2] @@ -783,14 +382,11 @@ def test_convolution_with_strides(): dilations = [1, 1] model = ov.convolution(data, filters, strides, pads_begin, pads_end, dilations) - function = Model([model], parameter_list, "test") - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(image_arr, filter_arr)[0] - - expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides).reshape(1, 1, 4, 4) - assert np.allclose(result, expected) + assert model.get_type_name() == "Convolution" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 4, 4] + assert model.get_output_element_type(0) == element_type def test_convolution_with_filter_dilation(): @@ -800,24 +396,17 @@ def test_convolution_with_filter_dilation(): filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) - parameter_list = [data, filters] - - image_arr = np.arange(100, dtype=np.float32).reshape([1, 1, 10, 10]) - filter_arr = np.ones(9, dtype=np.float32).reshape([1, 1, 3, 3]) strides = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] dilations = [2, 2] model = ov.convolution(data, filters, strides, pads_begin, pads_end, dilations) - function = Model([model], parameter_list, "test") - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(image_arr, filter_arr)[0] - - expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilations).reshape([1, 1, 6, 6]) - assert np.allclose(result, expected) + assert model.get_type_name() == "Convolution" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 6, 6] + assert model.get_output_element_type(0) == element_type def test_convolution_with_padding(): @@ -827,9 +416,6 @@ def test_convolution_with_padding(): filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) - parameter_list = [data, filters] - - image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = np.zeros(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][1][1] = 1 strides = [1, 1] @@ -838,16 +424,11 @@ def test_convolution_with_padding(): pads_end = [0, 0] model = ov.convolution(data, filters, strides, pads_begin, pads_end, dilations) - function = Model([model], parameter_list, "test") - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(image_arr, filter_arr)[0] - - expected = convolution2d( - image_arr[0][0], filter_arr[0][0], strides, dilations, pads_begin, pads_end - ).reshape([1, 1, 6, 6]) - assert np.allclose(result, expected) + assert model.get_type_name() == "Convolution" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 6, 6] + assert model.get_output_element_type(0) == element_type def test_convolution_with_non_zero_padding(): @@ -856,9 +437,6 @@ def test_convolution_with_non_zero_padding(): filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) - parameter_list = [data, filters] - - image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = (np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)) * -1 filter_arr[0][0][1][1] = 1 strides = [1, 1] @@ -867,13 +445,8 @@ def test_convolution_with_non_zero_padding(): pads_end = [1, 2] model = ov.convolution(data, filters, strides, pads_begin, pads_end, dilations) - function = Model([model], parameter_list, "test") - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(image_arr, filter_arr)[0] - - expected = convolution2d( - image_arr[0][0], filter_arr[0][0], strides, dilations, pads_begin, pads_end - ).reshape([1, 1, 9, 9]) - assert np.allclose(result, expected) + assert model.get_type_name() == "Convolution" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 9, 9] + assert model.get_output_element_type(0) == element_type diff --git a/src/bindings/python/tests/test_graph/test_ops_binary.py b/src/bindings/python/tests/test_graph/test_ops_binary.py index 67b66a788c5..32ee009f33d 100644 --- a/src/bindings/python/tests/test_graph/test_ops_binary.py +++ b/src/bindings/python/tests/test_graph/test_ops_binary.py @@ -7,204 +7,179 @@ import operator import numpy as np import pytest +from openvino.runtime import Type import openvino.runtime.opset8 as ov -from tests.runtime import get_runtime -from tests.test_graph.util import run_op_node @pytest.mark.parametrize( - ("graph_api_helper", "numpy_function"), + ("graph_api_helper", "expected_type"), [ - (ov.add, np.add), - (ov.divide, np.divide), - (ov.multiply, np.multiply), - (ov.subtract, np.subtract), - (ov.minimum, np.minimum), - (ov.maximum, np.maximum), - (ov.mod, np.mod), - (ov.equal, np.equal), - (ov.not_equal, np.not_equal), - (ov.greater, np.greater), - (ov.greater_equal, np.greater_equal), - (ov.less, np.less), - (ov.less_equal, np.less_equal), + (ov.add, Type.f32), + (ov.divide, Type.f32), + (ov.multiply, Type.f32), + (ov.subtract, Type.f32), + (ov.minimum, Type.f32), + (ov.maximum, Type.f32), + (ov.mod, Type.f32), + (ov.equal, Type.boolean), + (ov.not_equal, Type.boolean), + (ov.greater, Type.boolean), + (ov.greater_equal, Type.boolean), + (ov.less, Type.boolean), + (ov.less_equal, Type.boolean), ], ) -def test_binary_op(graph_api_helper, numpy_function): - runtime = get_runtime() - +def test_binary_op(graph_api_helper, expected_type): shape = [2, 2] parameter_a = ov.parameter(shape, name="A", dtype=np.float32) parameter_b = ov.parameter(shape, name="B", dtype=np.float32) model = graph_api_helper(parameter_a, parameter_b) - computation = runtime.computation(model, parameter_a, parameter_b) - value_a = np.array([[1, 2], [3, 4]], dtype=np.float32) - value_b = np.array([[5, 6], [7, 8]], dtype=np.float32) - - result = computation(value_a, value_b) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == shape + assert model.get_output_element_type(0) == expected_type @pytest.mark.parametrize( - ("graph_api_helper", "numpy_function"), + ("graph_api_helper", "expected_type"), [ - (ov.add, np.add), - (ov.divide, np.divide), - (ov.multiply, np.multiply), - (ov.subtract, np.subtract), - (ov.minimum, np.minimum), - (ov.maximum, np.maximum), - (ov.mod, np.mod), - (ov.equal, np.equal), - (ov.not_equal, np.not_equal), - (ov.greater, np.greater), - (ov.greater_equal, np.greater_equal), - (ov.less, np.less), - (ov.less_equal, np.less_equal), + (ov.add, Type.f32), + (ov.divide, Type.f32), + (ov.multiply, Type.f32), + (ov.subtract, Type.f32), + (ov.minimum, Type.f32), + (ov.maximum, Type.f32), + (ov.mod, Type.f32), + (ov.equal, Type.boolean), + (ov.not_equal, Type.boolean), + (ov.greater, Type.boolean), + (ov.greater_equal, Type.boolean), + (ov.less, Type.boolean), + (ov.less_equal, Type.boolean), ], ) -def test_binary_op_with_scalar(graph_api_helper, numpy_function): - runtime = get_runtime() - - value_a = np.array([[1, 2], [3, 4]], dtype=np.float32) +def test_binary_op_with_scalar(graph_api_helper, expected_type): value_b = np.array([[5, 6], [7, 8]], dtype=np.float32) shape = [2, 2] parameter_a = ov.parameter(shape, name="A", dtype=np.float32) model = graph_api_helper(parameter_a, value_b) - computation = runtime.computation(model, parameter_a) - result = computation(value_a) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == shape + assert model.get_output_element_type(0) == expected_type @pytest.mark.parametrize( - ("graph_api_helper", "numpy_function"), - [(ov.logical_and, np.logical_and), (ov.logical_or, np.logical_or), (ov.logical_xor, np.logical_xor)], + "graph_api_helper", + [ov.logical_and, ov.logical_or, ov.logical_xor], ) -def test_binary_logical_op(graph_api_helper, numpy_function): - runtime = get_runtime() - +def test_binary_logical_op(graph_api_helper): shape = [2, 2] parameter_a = ov.parameter(shape, name="A", dtype=bool) parameter_b = ov.parameter(shape, name="B", dtype=bool) model = graph_api_helper(parameter_a, parameter_b) - computation = runtime.computation(model, parameter_a, parameter_b) - value_a = np.array([[True, False], [False, True]], dtype=bool) - value_b = np.array([[False, True], [False, True]], dtype=bool) - - result = computation(value_a, value_b) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == shape + assert model.get_output_element_type(0) == Type.boolean @pytest.mark.parametrize( - ("graph_api_helper", "numpy_function"), - [(ov.logical_and, np.logical_and), (ov.logical_or, np.logical_or), (ov.logical_xor, np.logical_xor)], + "graph_api_helper", + [ov.logical_and, ov.logical_or, ov.logical_xor], ) -def test_binary_logical_op_with_scalar(graph_api_helper, numpy_function): - runtime = get_runtime() - - value_a = np.array([[True, False], [False, True]], dtype=bool) +def test_binary_logical_op_with_scalar(graph_api_helper): value_b = np.array([[False, True], [False, True]], dtype=bool) shape = [2, 2] parameter_a = ov.parameter(shape, name="A", dtype=bool) model = graph_api_helper(parameter_a, value_b) - computation = runtime.computation(model, parameter_a) - result = computation(value_a) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == shape + assert model.get_output_element_type(0) == Type.boolean @pytest.mark.parametrize( - ("operator", "numpy_function"), + ("operator", "expected_type"), [ - (operator.add, np.add), - (operator.sub, np.subtract), - (operator.mul, np.multiply), - (operator.truediv, np.divide), - (operator.eq, np.equal), - (operator.ne, np.not_equal), - (operator.gt, np.greater), - (operator.ge, np.greater_equal), - (operator.lt, np.less), - (operator.le, np.less_equal), + (operator.add, Type.f32), + (operator.sub, Type.f32), + (operator.mul, Type.f32), + (operator.truediv, Type.f32), + (operator.eq, Type.boolean), + (operator.ne, Type.boolean), + (operator.gt, Type.boolean), + (operator.ge, Type.boolean), + (operator.lt, Type.boolean), + (operator.le, Type.boolean), ], ) -def test_binary_operators(operator, numpy_function): - runtime = get_runtime() - - value_a = np.array([[1, 2], [3, 4]], dtype=np.float32) +def test_binary_operators(operator, expected_type): value_b = np.array([[4, 5], [1, 7]], dtype=np.float32) shape = [2, 2] parameter_a = ov.parameter(shape, name="A", dtype=np.float32) model = operator(parameter_a, value_b) - computation = runtime.computation(model, parameter_a) - result = computation(value_a) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == shape + assert model.get_output_element_type(0) == expected_type @pytest.mark.parametrize( - ("operator", "numpy_function"), + ("operator", "expected_type"), [ - (operator.add, np.add), - (operator.sub, np.subtract), - (operator.mul, np.multiply), - (operator.truediv, np.divide), - (operator.eq, np.equal), - (operator.ne, np.not_equal), - (operator.gt, np.greater), - (operator.ge, np.greater_equal), - (operator.lt, np.less), - (operator.le, np.less_equal), + (operator.add, Type.f32), + (operator.sub, Type.f32), + (operator.mul, Type.f32), + (operator.truediv, Type.f32), + (operator.eq, Type.boolean), + (operator.ne, Type.boolean), + (operator.gt, Type.boolean), + (operator.ge, Type.boolean), + (operator.lt, Type.boolean), + (operator.le, Type.boolean), ], ) -def test_binary_operators_with_scalar(operator, numpy_function): - runtime = get_runtime() - - value_a = np.array([[1, 2], [3, 4]], dtype=np.float32) +def test_binary_operators_with_scalar(operator, expected_type): value_b = np.array([[5, 6], [7, 8]], dtype=np.float32) shape = [2, 2] parameter_a = ov.parameter(shape, name="A", dtype=np.float32) model = operator(parameter_a, value_b) - computation = runtime.computation(model, parameter_a) - result = computation(value_a) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == shape + assert model.get_output_element_type(0) == expected_type def test_multiply(): param_a = np.arange(48, dtype=np.int32).reshape((8, 1, 6, 1)) param_b = np.arange(35, dtype=np.int32).reshape((7, 1, 5)) - expected = np.multiply(param_a, param_b) - result = run_op_node([param_a, param_b], ov.multiply) + node = ov.multiply(param_a, param_b) - assert np.allclose(result, expected) + assert node.get_type_name() == "Multiply" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [8, 7, 6, 5] + assert node.get_output_element_type(0) == Type.i32 def test_power_v1(): param_a = np.arange(48, dtype=np.float32).reshape((8, 1, 6, 1)) param_b = np.arange(20, dtype=np.float32).reshape((4, 1, 5)) - expected = np.power(param_a, param_b) - result = run_op_node([param_a, param_b], ov.power) + node = ov.power(param_a, param_b) - assert np.allclose(result, expected) + assert node.get_type_name() == "Power" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [8, 4, 6, 5] + assert node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests/test_graph/test_ops_fused.py b/src/bindings/python/tests/test_graph/test_ops_fused.py index a3827fb9f1d..3d6932491ad 100644 --- a/src/bindings/python/tests/test_graph/test_ops_fused.py +++ b/src/bindings/python/tests/test_graph/test_ops_fused.py @@ -5,50 +5,33 @@ import numpy as np import pytest +import openvino.runtime as ov_runtime import openvino.runtime.opset8 as ov -from tests.runtime import get_runtime -from tests import xfail_issue_36486 def test_elu_operator_with_scalar_and_array(): - runtime = get_runtime() - - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) + data_value = ov.parameter((2, 2), name="data_value", dtype=np.float32) alpha_value = np.float32(3) model = ov.elu(data_value, alpha_value) - computation = runtime.computation(model) - result = computation() - expected = np.array([[-2.9797862, 1.0], [-2.5939941, 3.0]], dtype=np.float32) - assert np.allclose(result, expected) + assert model.get_type_name() == "Elu" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] def test_elu_operator_with_scalar(): - runtime = get_runtime() - - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) alpha_value = np.float32(3) - - data_shape = [2, 2] - parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) + parameter_data = ov.parameter([2, 2], name="Data", dtype=np.float32) model = ov.elu(parameter_data, alpha_value) - computation = runtime.computation(model, parameter_data) - result = computation(data_value) - expected = np.array([[-2.9797862, 1.0], [-2.5939941, 3.0]], dtype=np.float32) - assert np.allclose(result, expected) + assert model.get_type_name() == "Elu" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] def test_fake_quantize(): - runtime = get_runtime() - - data_value = np.arange(24.0, dtype=np.float32).reshape(1, 2, 3, 4) - input_low_value = np.float32(0) - input_high_value = np.float32(23) - output_low_value = np.float32(2) - output_high_value = np.float32(16) levels = np.int32(4) data_shape = [1, 2, 3, 4] @@ -67,476 +50,198 @@ def test_fake_quantize(): parameter_output_high, levels, ) - computation = runtime.computation( - model, - parameter_data, - parameter_input_low, - parameter_input_high, - parameter_output_low, - parameter_output_high, - ) - result = computation(data_value, input_low_value, input_high_value, output_low_value, output_high_value) - - expected = np.array( - [ - [ - [ - [ - [2.0, 2.0, 2.0, 2.0], - [6.6666669, 6.6666669, 6.6666669, 6.6666669], - [6.6666669, 6.6666669, 6.6666669, 6.6666669], - ], - [ - [11.33333301, 11.33333301, 11.33333301, 11.33333301], - [11.33333301, 11.33333301, 11.33333301, 11.33333301], - [16.0, 16.0, 16.0, 16.0], - ], - ], - ], - ], - dtype=np.float32, - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "FakeQuantize" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 2, 3, 4] def test_depth_to_space(): - runtime = get_runtime() - - data_value = np.array( - [ - [ - [[0, 1, 2], [3, 4, 5]], - [[6, 7, 8], [9, 10, 11]], - [[12, 13, 14], [15, 16, 17]], - [[18, 19, 20], [21, 22, 23]], - ], - ], - dtype=np.float32, - ) mode = "blocks_first" block_size = np.int32(2) - data_shape = [1, 4, 2, 3] parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) model = ov.depth_to_space(parameter_data, mode, block_size) - computation = runtime.computation(model, parameter_data) - result = computation(data_value) - expected = np.array( - [[[[0, 6, 1, 7, 2, 8], [12, 18, 13, 19, 14, 20], [3, 9, 4, 10, 5, 11], [15, 21, 16, 22, 17, 23]]]], - dtype=np.float32, - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "DepthToSpace" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 4, 6] def test_space_to_batch(): - runtime = get_runtime() - - data_value = np.array([[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]], dtype=np.float32) - data_shape = data_value.shape - + data_shape = [1, 2, 2, 3] block_shape = np.array([1, 2, 3, 2], dtype=np.int64) pads_begin = np.array([0, 0, 1, 0], dtype=np.int64) pads_end = np.array([0, 0, 0, 1], dtype=np.int64) - parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) model = ov.space_to_batch(parameter_data, block_shape, pads_begin, pads_end) - computation = runtime.computation(model, parameter_data) - result = computation(data_value) - expected = np.array( - [ - [[[0, 0]]], - [[[0, 0]]], - [[[0, 2]]], - [[[1, 0]]], - [[[3, 5]]], - [[[4, 0]]], - [[[0, 0]]], - [[[0, 0]]], - [[[6, 8]]], - [[[7, 0]]], - [[[9, 11]]], - [[[10, 0]]], - ], - dtype=np.float32, - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "SpaceToBatch" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [12, 1, 1, 2] def test_batch_to_space(): - runtime = get_runtime() - - data = np.array( - [ - [[[0, 0]]], - [[[0, 0]]], - [[[0, 2]]], - [[[1, 0]]], - [[[3, 5]]], - [[[4, 0]]], - [[[0, 0]]], - [[[0, 0]]], - [[[6, 8]]], - [[[7, 0]]], - [[[9, 11]]], - [[[10, 0]]], - ], - dtype=np.float32, - ) - data_shape = data.shape - + data_shape = [12, 1, 1, 2] block_shape = np.array([1, 2, 3, 2], dtype=np.int64) crops_begin = np.array([0, 0, 1, 0], dtype=np.int64) crops_end = np.array([0, 0, 0, 1], dtype=np.int64) - parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) model = ov.batch_to_space(parameter_data, block_shape, crops_begin, crops_end) - computation = runtime.computation(model, parameter_data) - result = computation(data) - expected = np.array([[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]], dtype=np.float32) - - assert np.allclose(result, expected) + assert model.get_type_name() == "BatchToSpace" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 2, 2, 3] def test_clamp_operator(): - runtime = get_runtime() - data_shape = [2, 2] parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) min_value = np.float32(3) max_value = np.float32(12) model = ov.clamp(parameter_data, min_value, max_value) - computation = runtime.computation(model, parameter_data) - data_value = np.array([[-5, 9], [45, 3]], dtype=np.float32) - - result = computation(data_value) - expected = np.clip(data_value, min_value, max_value) - assert np.allclose(result, expected) - - -def test_clamp_operator_with_array(): - runtime = get_runtime() - - data_value = np.array([[-5, 9], [45, 3]], dtype=np.float32) - min_value = np.float32(3) - max_value = np.float32(12) - - model = ov.clamp(data_value, min_value, max_value) - computation = runtime.computation(model) - - result = computation() - expected = np.clip(data_value, min_value, max_value) - - assert np.allclose(result, expected) + assert model.get_type_name() == "Clamp" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] def test_squeeze_operator(): - runtime = get_runtime() - data_shape = [1, 2, 1, 3, 1, 1] parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) - data_value = np.arange(6.0, dtype=np.float32).reshape([1, 2, 1, 3, 1, 1]) axes = [2, 4] model = ov.squeeze(parameter_data, axes) - computation = runtime.computation(model, parameter_data) - result = computation(data_value) - expected = np.arange(6.0, dtype=np.float32).reshape([1, 2, 3, 1]) - assert np.allclose(result, expected) + assert model.get_type_name() == "Squeeze" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 2, 3, 1] def test_squared_difference_operator(): - runtime = get_runtime() - x1_shape = [1, 2, 3, 4] x2_shape = [2, 3, 4] parameter_x1 = ov.parameter(x1_shape, name="x1", dtype=np.float32) parameter_x2 = ov.parameter(x2_shape, name="x2", dtype=np.float32) - x1_value = np.arange(24.0, dtype=np.float32).reshape(x1_shape) - x2_value = np.arange(start=4.0, stop=28.0, step=1.0, dtype=np.float32).reshape(x2_shape) - model = ov.squared_difference(parameter_x1, parameter_x2) - computation = runtime.computation(model, parameter_x1, parameter_x2) - - result = computation(x1_value, x2_value) - expected = np.square(np.subtract(x1_value, x2_value)) - assert np.allclose(result, expected) + assert model.get_type_name() == "SquaredDifference" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [1, 2, 3, 4] def test_shuffle_channels_operator(): - runtime = get_runtime() - data_shape = [1, 15, 2, 2] axis = 1 groups = 5 - parameter = ov.parameter(data_shape, name="Data", dtype=np.float32) - data_value = np.arange(60.0, dtype=np.float32).reshape(data_shape) - model = ov.shuffle_channels(parameter, axis, groups) - computation = runtime.computation(model, parameter) - - result = computation(data_value) - expected = np.array( - [ - [ - [[0.0, 1.0], [2.0, 3.0]], - [[12.0, 13.0], [14.0, 15.0]], - [[24.0, 25.0], [26.0, 27.0]], - [[36.0, 37.0], [38.0, 39.0]], - [[48.0, 49.0], [50.0, 51.0]], - [[4.0, 5.0], [6.0, 7.0]], - [[16.0, 17.0], [18.0, 19.0]], - [[28.0, 29.0], [30.0, 31.0]], - [[40.0, 41.0], [42.0, 43.0]], - [[52.0, 53.0], [54.0, 55.0]], - [[8.0, 9.0], [10.0, 11.0]], - [[20.0, 21.0], [22.0, 23.0]], - [[32.0, 33.0], [34.0, 35.0]], - [[44.0, 45.0], [46.0, 47.0]], - [[56.0, 57.0], [58.0, 59.0]], - ], - ], - dtype=np.float32, - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "ShuffleChannels" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [1, 15, 2, 2] def test_unsqueeze(): - runtime = get_runtime() - data_shape = [3, 4, 5] parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) - data_value = np.arange(60.0, dtype=np.float32).reshape(3, 4, 5) axes = [0, 4] - model = ov.unsqueeze(parameter_data, axes) - computation = runtime.computation(model, parameter_data) - result = computation(data_value) - expected = np.arange(60.0, dtype=np.float32).reshape([1, 3, 4, 5, 1]) - assert np.allclose(result, expected) + model = ov.unsqueeze(parameter_data, axes) + assert model.get_type_name() == "Unsqueeze" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [1, 3, 4, 5, 1] def test_grn_operator(): - runtime = get_runtime() - - data_value = np.arange(start=1.0, stop=25.0, dtype=np.float32).reshape([1, 2, 3, 4]) bias = np.float32(1e-6) - data_shape = [1, 2, 3, 4] - parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) model = ov.grn(parameter_data, bias) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array( - [ - [ - [ - [0.0766965, 0.14142136, 0.19611613, 0.24253564], - [0.28216633, 0.31622776, 0.34570536, 0.37139067], - [0.39391932, 0.41380295, 0.4314555, 0.4472136], - ], - [ - [0.9970545, 0.98994946, 0.9805807, 0.97014254], - [0.9593655, 0.9486833, 0.9383431, 0.9284767], - [0.91914505, 0.9103665, 0.9021342, 0.8944272], - ], - ], - ], - dtype=np.float32, - ) - - assert np.allclose(result, expected) + assert model.get_type_name() == "GRN" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [1, 2, 3, 4] def test_prelu_operator(): - runtime = get_runtime() - data_shape = [1, 2, 3, 4] slope_shape = [2, 3, 1] - data_value = np.arange(start=1.0, stop=25.0, dtype=np.float32).reshape(data_shape) slope_value = np.arange(start=-10.0, stop=-4.0, dtype=np.float32).reshape(slope_shape) parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) parameter_slope = ov.parameter(slope_shape, name="Slope", dtype=np.float32) model = ov.prelu(parameter_data, parameter_slope) - computation = runtime.computation(model, parameter_data, parameter_slope) - - result = computation(data_value, slope_value) expected = np.clip(data_value, 0, np.inf) + np.clip(data_value, -np.inf, 0) * slope_value - assert np.allclose(result, expected) + assert model.get_type_name() == "PRelu" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == list(expected.shape) def test_selu_operator(): - runtime = get_runtime() - data_shape = [4, 2, 3, 1] - - data = np.arange(start=-1.0, stop=23.0, dtype=np.float32).reshape(data_shape) alpha = np.array(1.6733, dtype=np.float32) lambda_value = np.array(1.0507, dtype=np.float32) parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) model = ov.selu(parameter_data, alpha, lambda_value) - computation = runtime.computation(model, parameter_data) - - result = computation(data) - mask = (data > 0) * data + (data <= 0) * (alpha * np.exp(data) - alpha) - expected = mask * lambda_value - assert np.allclose(result, expected) + assert model.get_type_name() == "Selu" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [4, 2, 3, 1] -@xfail_issue_36486 def test_hard_sigmoid_operator(): - runtime = get_runtime() - data_shape = [3] - alpha_value = np.float32(0.5) - beta_value = np.float32(0.6) - - data_value = np.array([-1, 0, 1], dtype=np.float32) - parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) parameter_alpha = ov.parameter([], name="Alpha", dtype=np.float32) parameter_beta = ov.parameter([], name="Beta", dtype=np.float32) model = ov.hard_sigmoid(parameter_data, parameter_alpha, parameter_beta) - computation = runtime.computation(model, parameter_data, parameter_alpha, parameter_beta) - - result = computation(data_value, alpha_value, beta_value) - expected = [0.1, 0.6, 1.0] - assert np.allclose(result, expected) + assert model.get_type_name() == "HardSigmoid" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [3] def test_mvn_operator(): - runtime = get_runtime() - data_shape = [3, 3, 3, 1] axes = [0, 2, 3] normalize_variance = True eps = np.float32(1e-9) eps_mode = "outside_sqrt" - - data_value = np.array( - [ - [ - [[0.8439683], [0.5665144], [0.05836735]], - [[0.02916367], [0.12964272], [0.5060197]], - [[0.79538304], [0.9411346], [0.9546573]], - ], - [ - [[0.17730942], [0.46192095], [0.26480448]], - [[0.6746842], [0.01665257], [0.62473077]], - [[0.9240844], [0.9722341], [0.11965699]], - ], - [ - [[0.41356155], [0.9129373], [0.59330076]], - [[0.81929934], [0.7862604], [0.11799799]], - [[0.69248444], [0.54119414], [0.07513223]], - ], - ], - dtype=np.float32, - ) - parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) model = ov.mvn(parameter_data, axes, normalize_variance, eps, eps_mode) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - - expected = np.array( - [ - [ - [[1.3546423], [0.33053496], [-1.5450814]], - [[-1.2106764], [-0.8925952], [0.29888135]], - [[0.38083088], [0.81808794], [0.85865635]], - ], - [ - [[-1.1060555], [-0.05552877], [-0.78310335]], - [[0.83281356], [-1.250282], [0.67467856]], - [[0.7669372], [0.9113869], [-1.6463585]], - ], - [ - [[-0.23402764], [1.6092131], [0.42940593]], - [[1.2906139], [1.1860244], [-0.92945826]], - [[0.0721334], [-0.38174], [-1.7799333]], - ], - ], - dtype=np.float32, - ) - - assert np.allclose(result, expected) + assert model.get_type_name() == "MVN" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [3, 3, 3, 1] @pytest.mark.skip(reason="Sporadically failed. Need further investigation. Ticket - 95970") def test_space_to_depth_operator(): - runtime = get_runtime() - data_shape = [1, 2, 4, 4] - data_value = np.arange(start=0, stop=32, step=1.0, dtype=np.float32).reshape(data_shape) mode = "blocks_first" block_size = 2 parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) model = ov.space_to_depth(parameter_data, mode, block_size) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array( - [ - 0, - 2, - 8, - 10, - 16, - 18, - 24, - 26, - 1, - 3, - 9, - 11, - 17, - 19, - 25, - 27, - 4, - 6, - 12, - 14, - 20, - 22, - 28, - 30, - 5, - 7, - 13, - 15, - 21, - 23, - 29, - 31, - ], - dtype=np.float32, - ).reshape(1, 8, 2, 2) - assert np.allclose(result, expected) + assert model.get_type_name() == "SpaceToDepth" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [1, 8, 2, 2] batch_size = 2 input_size = 3 @@ -554,41 +259,6 @@ def test_space_to_depth_operator(): parameter_r = ov.parameter(r_shape, name="R", dtype=np.float32) parameter_b = ov.parameter(b_shape, name="B", dtype=np.float32) - x_value = np.array( - [0.3432185, 0.612268, 0.20272376, 0.9513413, 0.30585995, 0.7265472], dtype=np.float32, - ).reshape(x_shape) - h_t_value = np.array( - [0.12444675, 0.52055854, 0.46489045, 0.4983964, 0.7730452, 0.28439692], dtype=np.float32, - ).reshape(h_t_shape) - w_value = np.array( - [ - 0.41930267, - 0.7872176, - 0.89940447, - 0.23659843, - 0.24676207, - 0.17101714, - 0.3147149, - 0.6555601, - 0.4559603, - ], - dtype=np.float32, - ).reshape(w_shape) - r_value = np.array( - [ - 0.8374871, - 0.86660194, - 0.82114047, - 0.71549815, - 0.18775631, - 0.3182116, - 0.25392973, - 0.38301638, - 0.85531586, - ], - dtype=np.float32, - ).reshape(r_shape) - b_value = np.array([1.0289404, 1.6362579, 0.4370661], dtype=np.float32).reshape(b_shape) activations = ["sigmoid"] activation_alpha = [] activation_beta = [] @@ -606,47 +276,33 @@ def test_space_to_depth_operator(): activation_beta, clip, ) - computation = runtime.computation( - model, parameter_x, parameter_h_t, parameter_w, parameter_r, parameter_b, - ) - - result = computation(x_value, h_t_value, w_value, r_value, b_value) - expected = np.array( - [0.94126844, 0.9036043, 0.841243, 0.9468489, 0.934215, 0.873708], dtype=np.float32, - ).reshape(batch_size, hidden_size) - - assert np.allclose(result, expected) + assert model.get_type_name() == "SpaceToDepth" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [batch_size, hidden_size] def test_group_convolution_operator(): - runtime = get_runtime() - data_shape = [1, 4, 2, 2] filters_shape = [2, 1, 2, 1, 1] parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) parameter_filters = ov.parameter(filters_shape, name="Filters", dtype=np.float32) - data_value = np.arange(start=1.0, stop=17.0, dtype=np.float32).reshape(data_shape) - filters_value = np.arange(start=1.0, stop=5.0, dtype=np.float32).reshape(filters_shape) strides = [1, 1] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] model = ov.group_convolution(parameter_data, parameter_filters, strides, pads_begin, pads_end, dilations) - computation = runtime.computation(model, parameter_data, parameter_filters) - result = computation(data_value, filters_value) - - expected = np.array([11, 14, 17, 20, 79, 86, 93, 100], dtype=np.float32).reshape(1, 2, 2, 2) - - assert np.allclose(result, expected) + assert model.get_type_name() == "GroupConvolution" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [1, 2, 2, 2] @pytest.mark.xfail(reason="Computation mismatch") def test_group_convolution_backprop_data(): - runtime = get_runtime() - data_shape = [1, 1, 3, 3] filters_shape = [1, 1, 1, 3, 3] strides = [2, 2] @@ -660,87 +316,13 @@ def test_group_convolution_backprop_data(): data_node, filters_node, strides, None, pads_begin, pads_end, output_padding=output_padding, ) - data_value = np.array( - [ - 0.16857791, - -0.15161794, - 0.08540368, - 0.1820628, - -0.21746576, - 0.08245695, - 0.1431433, - -0.43156421, - 0.30591947, - ], - dtype=np.float32, - ).reshape(data_shape) - - filters_value = np.array( - [ - -0.06230065, - 0.37932432, - -0.25388849, - 0.33878803, - 0.43709868, - -0.22477469, - 0.04118127, - -0.44696793, - 0.06373066, - ], - dtype=np.float32, - ).reshape(filters_shape) - - computation = runtime.computation(model, data_node, filters_node) - result = computation(data_value, filters_value) - - expected = np.array( - [ - 0.07368518, - -0.08925839, - -0.06627201, - 0.06301362, - 0.03732984, - -0.01919658, - -0.00628807, - -0.02817563, - -0.01472169, - 0.04392925, - -0.00689478, - -0.01549204, - 0.07957941, - -0.11459791, - -0.09505399, - 0.07681622, - 0.03604182, - -0.01853423, - -0.0270785, - -0.00680824, - -0.06650258, - 0.08004665, - 0.07918708, - 0.0724144, - 0.06256775, - -0.17838378, - -0.18863615, - 0.20064656, - 0.133717, - -0.06876295, - -0.06398046, - -0.00864975, - 0.19289537, - -0.01490572, - -0.13673618, - 0.01949645, - ], - dtype=np.float32, - ).reshape(1, 1, 6, 6) - - assert np.allclose(result, expected) + assert model.get_type_name() == "GroupConvolutionBackpropData" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [1, 1, 6, 6] def test_group_convolution_backprop_data_output_shape(): - runtime = get_runtime() - data_shape = [1, 1, 1, 10] filters_shape = [1, 1, 1, 1, 5] strides = [1, 1] @@ -753,17 +335,7 @@ def test_group_convolution_backprop_data_output_shape(): data_node, filters_node, strides, output_shape_node, auto_pad="same_upper", ) - data_value = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=np.float32).reshape( - data_shape, - ) - - filters_value = np.array([1.0, 2.0, 3.0, 2.0, 1.0], dtype=np.float32).reshape(filters_shape) - - computation = runtime.computation(model, data_node, filters_node) - result = computation(data_value, filters_value) - - expected = np.array( - [0.0, 1.0, 4.0, 10.0, 18.0, 27.0, 36.0, 45.0, 54.0, 63.0, 62.0, 50.0, 26.0, 9.0], dtype=np.float32, - ).reshape(1, 1, 1, 14) - - assert np.allclose(result, expected) + assert model.get_type_name() == "GroupConvolutionBackpropData" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [1, 1, 1, 14] diff --git a/src/bindings/python/tests/test_graph/test_ops_matmul.py b/src/bindings/python/tests/test_graph/test_ops_matmul.py index 3d899831331..cb6abbdd773 100644 --- a/src/bindings/python/tests/test_graph/test_ops_matmul.py +++ b/src/bindings/python/tests/test_graph/test_ops_matmul.py @@ -6,37 +6,31 @@ import numpy as np import pytest import openvino.runtime.opset8 as ov -from tests.test_graph.util import run_op_node @pytest.mark.parametrize( - ("shape_a", "shape_b", "transpose_a", "transpose_b"), + ("shape_a", "shape_b", "transpose_a", "transpose_b", "output_shape"), [ # matrix, vector - ([2, 4], [4], False, False), - ([4], [4, 2], False, False), + ([2, 4], [4], False, False, [2]), + ([4], [4, 2], False, False, [2]), # matrix, matrix - ([2, 4], [4, 2], False, False), + ([2, 4], [4, 2], False, False, [2, 2]), # tensor, vector - ([2, 4, 5], [5], False, False), + ([2, 4, 5], [5], False, False, [2, 4]), # # tensor, matrix - ([2, 4, 5], [5, 4], False, False), + ([2, 4, 5], [5, 4], False, False, [2, 4, 4]), # # tensor, tensor - ([2, 2, 4], [2, 4, 2], False, False), + ([2, 2, 4], [2, 4, 2], False, False, [2, 2, 2]), ], ) @pytest.mark.skip(reason="Sporadically failed. Need further investigation. Ticket - 95970") -def test_matmul(shape_a, shape_b, transpose_a, transpose_b): +def test_matmul(shape_a, shape_b, transpose_a, transpose_b, output_shape): np.random.seed(133391) left_input = -100.0 + np.random.rand(*shape_a).astype(np.float32) * 200.0 right_input = -100.0 + np.random.rand(*shape_b).astype(np.float32) * 200.0 - result = run_op_node([left_input, right_input], ov.matmul, transpose_a, transpose_b) - - if transpose_a: - left_input = np.transpose(left_input) - if transpose_b: - right_input = np.transpose(right_input) - - expected = np.matmul(left_input, right_input) - assert np.allclose(result, expected) + node = ov.matmul(left_input, right_input, transpose_a, transpose_b) + assert node.get_type_name() == "MatMul" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == output_shape diff --git a/src/bindings/python/tests/test_graph/test_ops_multioutput.py b/src/bindings/python/tests/test_graph/test_ops_multioutput.py index 1bf4c9ac6be..2650a7b6fa6 100644 --- a/src/bindings/python/tests/test_graph/test_ops_multioutput.py +++ b/src/bindings/python/tests/test_graph/test_ops_multioutput.py @@ -5,33 +5,28 @@ import numpy as np import openvino.runtime.opset8 as ov -from tests.runtime import get_runtime def test_split(): - runtime = get_runtime() input_tensor = ov.constant(np.array([0, 1, 2, 3, 4, 5], dtype=np.int32)) axis = ov.constant(0, dtype=np.int64) splits = 3 split_node = ov.split(input_tensor, axis, splits) - computation = runtime.computation(split_node) - split_results = computation() - expected_results = np.array([[0, 1], [2, 3], [4, 5]], dtype=np.int32) - assert np.allclose(split_results, expected_results) + assert split_node.get_type_name() == "Split" + assert split_node.get_output_size() == 3 + assert list(split_node.get_output_shape(0)) == [2] + assert list(split_node.get_output_shape(1)) == [2] + assert list(split_node.get_output_shape(2)) == [2] def test_variadic_split(): - runtime = get_runtime() input_tensor = ov.constant(np.array([[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]], dtype=np.int32)) axis = ov.constant(1, dtype=np.int64) splits = ov.constant(np.array([2, 4], dtype=np.int64)) v_split_node = ov.variadic_split(input_tensor, axis, splits) - computation = runtime.computation(v_split_node) - results = computation() - split0 = np.array([[0, 1], [6, 7]], dtype=np.int32) - split1 = np.array([[2, 3, 4, 5], [8, 9, 10, 11]], dtype=np.int32) - - assert np.allclose(results[0], split0) - assert np.allclose(results[1], split1) + assert v_split_node.get_type_name() == "VariadicSplit" + assert v_split_node.get_output_size() == 2 + assert list(v_split_node.get_output_shape(0)) == [2, 2] + assert list(v_split_node.get_output_shape(1)) == [2, 4] diff --git a/src/bindings/python/tests/test_graph/test_ops_reshape.py b/src/bindings/python/tests/test_graph/test_ops_reshape.py index f09c6d6ffc4..1f8848583e5 100644 --- a/src/bindings/python/tests/test_graph/test_ops_reshape.py +++ b/src/bindings/python/tests/test_graph/test_ops_reshape.py @@ -2,36 +2,36 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 +import openvino.runtime as ov_runtime import openvino.runtime.opset8 as ov import numpy as np import pytest -from tests.runtime import get_runtime -from tests.test_graph.util import run_op_node, run_op_numeric_data +from openvino.runtime.utils.types import get_element_type def test_concat(): input_a = np.array([[1, 2], [3, 4]]).astype(np.float32) input_b = np.array([[5, 6]]).astype(np.float32) axis = 0 - expected = np.concatenate((input_a, input_b), axis=0) - runtime = get_runtime() parameter_a = ov.parameter(list(input_a.shape), name="A", dtype=np.float32) parameter_b = ov.parameter(list(input_b.shape), name="B", dtype=np.float32) node = ov.concat([parameter_a, parameter_b], axis) - computation = runtime.computation(node, parameter_a, parameter_b) - result = computation(input_a, input_b) - assert np.allclose(result, expected) + assert node.get_type_name() == "Concat" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 2] @pytest.mark.parametrize( - ("val_type", "value"), [(bool, False), (bool, np.empty((2, 2), dtype=bool))], + ("val_type", "value", "output_shape"), [(bool, False, []), (bool, np.empty((2, 2), dtype=bool), [2, 2])], ) -def test_constant_from_bool(val_type, value): - expected = np.array(value, dtype=val_type) - result = run_op_numeric_data(value, ov.constant, val_type) - assert np.allclose(result, expected) +def test_constant_from_bool(val_type, value, output_shape): + node = ov.constant(value, val_type) + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == ov_runtime.Type.boolean + assert list(node.get_output_shape(0)) == output_shape @pytest.mark.parametrize( @@ -50,9 +50,11 @@ def test_constant_from_bool(val_type, value): ], ) def test_constant_from_scalar(val_type, value): - expected = np.array(value, dtype=val_type) - result = run_op_numeric_data(value, ov.constant, val_type) - assert np.allclose(result, expected) + node = ov.constant(value, val_type) + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(val_type) + assert list(node.get_output_shape(0)) == [] @pytest.mark.parametrize( @@ -65,8 +67,11 @@ def test_constant_from_scalar(val_type, value): def test_constant_from_float_array(val_type): np.random.seed(133391) input_data = np.array(-1 + np.random.rand(2, 3, 4) * 2, dtype=val_type) - result = run_op_numeric_data(input_data, ov.constant, val_type) - assert np.allclose(result, input_data) + node = ov.constant(input_data, val_type) + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(val_type) + assert list(node.get_output_shape(0)) == [2, 3, 4] @pytest.mark.parametrize( @@ -87,8 +92,11 @@ def test_constant_from_integer_array(val_type, range_start, range_end): input_data = np.array( np.random.randint(range_start, range_end, size=(2, 2)), dtype=val_type, ) - result = run_op_numeric_data(input_data, ov.constant, val_type) - assert np.allclose(result, input_data) + node = ov.constant(input_data, val_type) + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(val_type) + assert list(node.get_output_shape(0)) == [2, 2] def test_broadcast_numpy(): @@ -127,27 +135,24 @@ def test_transpose(): ) input_order = np.array([0, 2, 3, 1], dtype=np.int32) - result = run_op_node([input_tensor], ov.transpose, input_order) - - expected = np.transpose(input_tensor, input_order) - - assert np.allclose(result, expected) + node = ov.transpose(input_tensor, input_order) + assert node.get_type_name() == "Transpose" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == ov_runtime.Type.i32 + assert list(node.get_output_shape(0)) == [3, 224, 224, 3] def test_tile(): input_tensor = np.arange(6, dtype=np.int32).reshape((2, 1, 3)) repeats = np.array([2, 1], dtype=np.int32) + node = ov.tile(input_tensor, repeats) - result = run_op_node([input_tensor], ov.tile, repeats) - - expected = np.array([0, 1, 2, 0, 1, 2, 3, 4, 5, 3, 4, 5]).reshape((2, 2, 3)) - - assert np.allclose(result, expected) + assert node.get_type_name() == "Tile" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == ov_runtime.Type.i32 + assert list(node.get_output_shape(0)) == [2, 2, 3] -@pytest.mark.xfail( - reason="RuntimeError: Check 'shape_size(get_input_shape(0)) == shape_size(output_shape)'", -) def test_strided_slice(): input_tensor = np.arange(2 * 3 * 4, dtype=np.float32).reshape((2, 3, 4)) begin = np.array([1, 0], dtype=np.int32) @@ -159,9 +164,8 @@ def test_strided_slice(): shrink_axis_mask = np.array([1, 0, 0], dtype=np.int32) ellipsis_mask = np.array([0, 0, 0], dtype=np.int32) - result = run_op_node( - [input_tensor], - ov.strided_slice, + node = ov.strided_slice( + input_tensor, begin, end, strides, @@ -171,12 +175,10 @@ def test_strided_slice(): shrink_axis_mask, ellipsis_mask, ) - - expected = np.array( - [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], dtype=np.float32, - ).reshape((1, 3, 4)) - - assert np.allclose(result, expected) + assert node.get_type_name() == "StridedSlice" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(node.get_output_shape(0)) == [1, 3, 4] def test_reshape_v1(): @@ -184,16 +186,18 @@ def test_reshape_v1(): shape = np.array([0, -1, 4], dtype=np.int32) special_zero = True - expected_shape = np.array([2, 150, 4]) - expected = np.reshape(param_a, expected_shape) - result = run_op_node([param_a], ov.reshape, shape, special_zero) - - assert np.allclose(result, expected) + node = ov.reshape(param_a, shape, special_zero) + assert node.get_type_name() == "Reshape" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(node.get_output_shape(0)) == [2, 150, 4] def test_shape_of(): input_tensor = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32) - result = run_op_node([input_tensor], ov.shape_of) - - assert np.allclose(result, [3, 3]) + node = ov.shape_of(input_tensor) + assert node.get_type_name() == "ShapeOf" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == ov_runtime.Type.i64 + assert list(node.get_output_shape(0)) == [2] diff --git a/src/bindings/python/tests/test_graph/test_ops_unary.py b/src/bindings/python/tests/test_graph/test_ops_unary.py index 8a006d4e83d..0a1a08c1e3b 100644 --- a/src/bindings/python/tests/test_graph/test_ops_unary.py +++ b/src/bindings/python/tests/test_graph/test_ops_unary.py @@ -5,79 +5,78 @@ import numpy as np import pytest +import openvino.runtime as ov_runtime import openvino.runtime.opset9 as ov from openvino.runtime import Shape, Type -from tests.runtime import get_runtime -from tests.test_graph.util import run_op_node R_TOLERANCE = 1e-6 # global relative tolerance @pytest.mark.parametrize( - ("graph_api_fn", "numpy_fn", "range_start", "range_end"), + ("graph_api_fn", "type_name"), [ - (ov.absolute, np.abs, -1, 1), - (ov.abs, np.abs, -1, 1), - (ov.acos, np.arccos, -1, 1), - (ov.acosh, np.arccosh, 1, 2), - (ov.asin, np.arcsin, -1, 1), - (ov.asinh, np.arcsinh, -1, 1), - (ov.atan, np.arctan, -100.0, 100.0), - (ov.atanh, np.arctanh, 0.0, 1.0), - (ov.ceiling, np.ceil, -100.0, 100.0), - (ov.ceil, np.ceil, -100.0, 100.0), - (ov.cos, np.cos, -100.0, 100.0), - (ov.cosh, np.cosh, -100.0, 100.0), - (ov.exp, np.exp, -100.0, 100.0), - (ov.floor, np.floor, -100.0, 100.0), - (ov.log, np.log, 0, 100.0), - (ov.relu, lambda x: np.maximum(0, x), -100.0, 100.0), - (ov.sign, np.sign, -100.0, 100.0), - (ov.sin, np.sin, -100.0, 100.0), - (ov.sinh, np.sinh, -100.0, 100.0), - (ov.sqrt, np.sqrt, 0.0, 100.0), - (ov.tan, np.tan, -1.0, 1.0), - (ov.tanh, np.tanh, -100.0, 100.0), + (ov.absolute, "Abs"), + (ov.abs, "Abs"), + (ov.acos, "Acos"), + (ov.acosh, "Acosh"), + (ov.asin, "Asin"), + (ov.asinh, "Asinh"), + (ov.atan, "Atan"), + (ov.atanh, "Atanh"), + (ov.ceiling, "Ceiling"), + (ov.ceil, "Ceiling"), + (ov.cos, "Cos"), + (ov.cosh, "Cosh"), + (ov.exp, "Exp"), + (ov.floor, "Floor"), + (ov.log, "Log"), + (ov.relu, "Relu"), + (ov.sign, "Sign"), + (ov.sin, "Sin"), + (ov.sinh, "Sinh"), + (ov.sqrt, "Sqrt"), + (ov.tan, "Tan"), + (ov.tanh, "Tanh"), ], ) -def test_unary_op_array(graph_api_fn, numpy_fn, range_start, range_end): +def test_unary_op_array(graph_api_fn, type_name): np.random.seed(133391) - input_data = (range_start + np.random.rand(2, 3, 4) * (range_end - range_start)).astype(np.float32) - expected = numpy_fn(input_data) + input_data = np.random.rand(2, 3, 4).astype(np.float32) - result = run_op_node([input_data], graph_api_fn) - assert np.allclose(result, expected, rtol=0.001) + node = graph_api_fn(input_data) + assert node.get_output_size() == 1 + assert node.get_type_name() == type_name + assert node.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(node.get_output_shape(0)) == [2, 3, 4] -@pytest.mark.parametrize( - ("graph_api_fn", "numpy_fn", "input_data"), - [ - pytest.param(ov.absolute, np.abs, np.float32(-3)), - pytest.param(ov.abs, np.abs, np.float32(-3)), - pytest.param(ov.acos, np.arccos, np.float32(-0.5)), - pytest.param(ov.asin, np.arcsin, np.float32(-0.5)), - pytest.param(ov.atan, np.arctan, np.float32(-0.5)), - pytest.param(ov.ceiling, np.ceil, np.float32(1.5)), - pytest.param(ov.ceil, np.ceil, np.float32(1.5)), - pytest.param(ov.cos, np.cos, np.float32(np.pi / 4.0)), - pytest.param(ov.cosh, np.cosh, np.float32(np.pi / 4.0)), - pytest.param(ov.exp, np.exp, np.float32(1.5)), - pytest.param(ov.floor, np.floor, np.float32(1.5)), - pytest.param(ov.log, np.log, np.float32(1.5)), - pytest.param(ov.relu, lambda x: np.maximum(0, x), np.float32(-0.125)), - pytest.param(ov.sign, np.sign, np.float32(0.0)), - pytest.param(ov.sin, np.sin, np.float32(np.pi / 4.0)), - pytest.param(ov.sinh, np.sinh, np.float32(0.0)), - pytest.param(ov.sqrt, np.sqrt, np.float32(3.5)), - pytest.param(ov.tan, np.tan, np.float32(np.pi / 4.0)), - pytest.param(ov.tanh, np.tanh, np.float32(0.1234)), - ], -) -def test_unary_op_scalar(graph_api_fn, numpy_fn, input_data): - expected = numpy_fn(input_data) +@pytest.mark.parametrize("graph_api_fn", [ + ov.absolute, + ov.abs, + ov.acos, + ov.asin, + ov.atan, + ov.ceiling, + ov.ceil, + ov.cos, + ov.cosh, + ov.exp, + ov.floor, + ov.log, + ov.relu, + ov.sign, + ov.sin, + ov.sinh, + ov.sqrt, + ov.tan, + ov.tanh, +]) +def test_unary_op_scalar(graph_api_fn): + node = graph_api_fn(np.float32(-0.5)) - result = run_op_node([input_data], graph_api_fn) - assert np.allclose(result, expected) + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(node.get_output_shape(0)) == [] @pytest.mark.parametrize( @@ -85,52 +84,42 @@ def test_unary_op_scalar(graph_api_fn, numpy_fn, input_data): [(np.array([True, False, True, False])), (np.array([True])), (np.array([False]))], ) def test_logical_not(input_data): - expected = np.logical_not(input_data) - - result = run_op_node([input_data], ov.logical_not) - assert np.allclose(result, expected) + node = ov.logical_not(input_data) + assert node.get_output_size() == 1 + assert node.get_type_name() == "LogicalNot" + assert node.get_output_element_type(0) == ov_runtime.Type.boolean + assert list(node.get_output_shape(0)) == list(input_data.shape) def test_sigmoid(): input_data = np.array([-3.14, -1.0, 0.0, 2.71001, 1000.0], dtype=np.float32) - result = run_op_node([input_data], ov.sigmoid) + node = ov.sigmoid(input_data) - def sigmoid(value): - return 1.0 / (1.0 + np.exp(-value)) - - expected = np.array(list(map(sigmoid, input_data))) - - assert np.allclose(result, expected) + assert node.get_output_size() == 1 + assert node.get_type_name() == "Sigmoid" + assert node.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(node.get_output_shape(0)) == [5] -def test_softmax_positive_axis(): +def test_softmax(): axis = 1 input_tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) - result = run_op_node([input_tensor], ov.softmax, axis) - - expected = [[0.09003056, 0.24472842, 0.6652409], [0.09003056, 0.24472842, 0.6652409]] - - assert np.allclose(result, expected) - - -def test_softmax_negative_axis(): - axis = -1 - input_tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) - - result = run_op_node([input_tensor], ov.softmax, axis) - - expected = [[0.09003056, 0.24472842, 0.6652409], [0.09003056, 0.24472842, 0.6652409]] - - assert np.allclose(result, expected) + node = ov.softmax(input_tensor, axis) + assert node.get_output_size() == 1 + assert node.get_type_name() == "Softmax" + assert node.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(node.get_output_shape(0)) == [2, 3] def test_erf(): input_tensor = np.array([-1.0, 0.0, 1.0, 2.5, 3.14, 4.0], dtype=np.float32) - expected = [-0.842701, 0.0, 0.842701, 0.999593, 0.999991, 1.0] - result = run_op_node([input_tensor], ov.erf) - assert np.allclose(result, expected) + node = ov.erf(input_tensor) + assert node.get_output_size() == 1 + assert node.get_type_name() == "Erf" + assert node.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(node.get_output_shape(0)) == [6] def test_hswish(): @@ -144,7 +133,7 @@ def test_hswish(): assert node.get_output_element_type(0) == Type.f32 -def test_round_even(): +def test_round(): float_dtype = np.float32 data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data") @@ -155,27 +144,12 @@ def test_round_even(): assert node.get_output_element_type(0) == Type.f32 input_tensor = np.array([-2.5, -1.5, -0.5, 0.5, 0.9, 1.5, 2.3, 2.5, 3.5], dtype=np.float32) - expected = [-2.0, -2.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 4.0] - result = run_op_node([input_tensor], ov.round, "HALF_TO_EVEN") - assert np.allclose(result, expected) - - -def test_round_away(): - float_dtype = np.float32 - data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data") - - node = ov.round(data, "HALF_AWAY_FROM_ZERO") - assert node.get_type_name() == "Round" + node = ov.round(input_tensor, "HALF_TO_EVEN") assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == [3, 10] - assert node.get_output_element_type(0) == Type.f32 - - input_tensor = np.array([-2.5, -1.5, -0.5, 0.5, 0.9, 1.5, 2.3, 2.5, 3.5], dtype=np.float32) - expected = [-3.0, -2.0, -1.0, 1.0, 1.0, 2.0, 2.0, 3.0, 4.0] - - result = run_op_node([input_tensor], ov.round, "HALF_AWAY_FROM_ZERO") - assert np.allclose(result, expected) + assert node.get_type_name() == "Round" + assert node.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(node.get_output_shape(0)) == [9] def test_hsigmoid(): @@ -190,102 +164,42 @@ def test_hsigmoid(): def test_gelu_operator_with_parameters(): - runtime = get_runtime() - - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) - data_shape = [2, 2] parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) model = ov.gelu(parameter_data, "erf") - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array([[-1.6391277e-06, 8.4134471e-01], [-4.5500278e-02, 2.9959502]], dtype=np.float32) - assert np.allclose(result, expected, 1e-6, 1e-6) + assert model.get_output_size() == 1 + assert model.get_type_name() == "Gelu" + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [2, 2] def test_gelu_operator_with_array(): - runtime = get_runtime() - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) model = ov.gelu(data_value, "erf") - computation = runtime.computation(model) - - result = computation() - expected = np.array([[-1.6391277e-06, 8.4134471e-01], [-4.5500278e-02, 2.9959502]], dtype=np.float32) - assert np.allclose(result, expected, 1e-6, 1e-6) + assert model.get_output_size() == 1 + assert model.get_type_name() == "Gelu" + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [2, 2] def test_gelu_tanh_operator_with_parameters(): - runtime = get_runtime() - - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) - data_shape = [2, 2] parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) model = ov.gelu(parameter_data, "tanh") - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array([[0.0, 0.841192], [-0.04540223, 2.9963627]], dtype=np.float32) - assert np.allclose(result, expected, 1e-6, 1e-6) + assert model.get_output_size() == 1 + assert model.get_type_name() == "Gelu" + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [2, 2] def test_gelu_tanh_operator_with_array(): - runtime = get_runtime() - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) model = ov.gelu(data_value, "tanh") - computation = runtime.computation(model) - - result = computation() - expected = np.array([[0.0, 0.841192], [-0.04540223, 2.9963627]], dtype=np.float32) - - assert np.allclose(result, expected, 1e-6, 1e-6) - - -type_tolerance = [ - (np.float64, 1e-6), - (np.float32, 1e-6), - (np.float16, 1e-3), -] - - -@pytest.mark.parametrize("type_tolerance", type_tolerance) -def test_softsign_with_parameters(type_tolerance): - dtype, atol = type_tolerance - data = np.random.uniform(-1.0, 1.0, (32, 5)).astype(dtype) - expected = np.divide(data, np.abs(data) + 1) - - runtime = get_runtime() - param = ov.parameter(data.shape, dtype, name="Data") - result = runtime.computation(ov.softsign(param, "SoftSign"), param)(data) - - assert np.allclose(result, expected, R_TOLERANCE, atol) - - -@pytest.mark.parametrize("type_tolerance", type_tolerance) -def test_softsign_with_array(type_tolerance): - dtype, atol = type_tolerance - data = np.random.uniform(-1.0, 1.0, (32, 5)).astype(dtype) - expected = np.divide(data, np.abs(data) + 1) - - runtime = get_runtime() - result = runtime.computation(ov.softsign(data, "SoftSign"))() - - assert np.allclose(result, expected, R_TOLERANCE, atol) - - -@pytest.mark.parametrize("type_tolerance", type_tolerance) -def test_softsign(type_tolerance): - dtype, atol = type_tolerance - data = np.random.uniform(-1.0, 1.0, (32, 5)).astype(dtype) - expected = np.divide(data, np.abs(data) + 1) - - result = run_op_node([data], ov.softsign) - - assert np.allclose(result, expected, R_TOLERANCE, atol) + assert model.get_output_size() == 1 + assert model.get_type_name() == "Gelu" + assert model.get_output_element_type(0) == ov_runtime.Type.f32 + assert list(model.get_output_shape(0)) == [2, 2] diff --git a/src/bindings/python/tests/test_graph/test_pooling.py b/src/bindings/python/tests/test_graph/test_pooling.py index 6ad24c43fee..91a5c0ea5d7 100644 --- a/src/bindings/python/tests/test_graph/test_pooling.py +++ b/src/bindings/python/tests/test_graph/test_pooling.py @@ -6,7 +6,7 @@ import numpy as np import pytest import openvino.runtime.opset8 as ov -from tests.runtime import get_runtime +from openvino.runtime import Type @pytest.fixture() @@ -15,7 +15,6 @@ def ndarray_1x1x4x4(): def test_avg_pool_2d(ndarray_1x1x4x4): - runtime = get_runtime() input_data = ndarray_1x1x4x4 param = ov.parameter(input_data.shape, name="A", dtype=np.float32) @@ -25,41 +24,15 @@ def test_avg_pool_2d(ndarray_1x1x4x4): pads_end = [0] * spatial_dim_count strides = [2, 2] exclude_pad = True - expected = [[[[13.5, 15.5], [21.5, 23.5]]]] - avg_pool_node = ov.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) - computation = runtime.computation(avg_pool_node, param) - result = computation(input_data) - assert np.allclose(result, expected) - - expected = [[[[13.5, 14.5, 15.5], [17.5, 18.5, 19.5], [21.5, 22.5, 23.5]]]] - strides = [1, 1] - avg_pool_node = ov.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) - computation = runtime.computation(avg_pool_node, param) - result = computation(input_data) - assert np.allclose(result, expected) - - pads_begin = [1, 1] - pads_end = [1, 1] - strides = [2, 2] - exclude_pad = True - - expected = [[[[11.0, 12.5, 14.0], [17.0, 18.5, 20.0], [23.0, 24.5, 26.0]]]] - avg_pool_node = ov.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) - computation = runtime.computation(avg_pool_node, param) - result = computation(input_data) - assert np.allclose(result, expected) - - exclude_pad = False - expected = [[[[2.75, 6.25, 3.5], [8.5, 18.5, 10.0], [5.75, 12.25, 6.5]]]] - avg_pool_node = ov.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) - computation = runtime.computation(avg_pool_node, param) - result = computation(input_data) - assert np.allclose(result, expected) + node = ov.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) + assert node.get_type_name() == "AvgPool" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [1, 1, 2, 2] + assert node.get_output_element_type(0) == Type.f32 def test_avg_pooling_3d(ndarray_1x1x4x4): - rt = get_runtime() data = ndarray_1x1x4x4 data = np.broadcast_to(data, (1, 1, 4, 4, 4)) param = ov.parameter(list(data.shape)) @@ -70,21 +43,14 @@ def test_avg_pooling_3d(ndarray_1x1x4x4): pads_end = [0] * spatial_dim_count exclude_pad = True - avgpool = ov.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) - comp = rt.computation(avgpool, param) - result = comp(data) - result_ref = [[[[[13.5, 15.5], [21.5, 23.5]], [[13.5, 15.5], [21.5, 23.5]]]]] - assert np.allclose(result, result_ref) + node = ov.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) + assert node.get_type_name() == "AvgPool" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [1, 1, 2, 2, 2] + assert node.get_output_element_type(0) == Type.f32 def test_max_pool_basic(): - rt = get_runtime() - - """array([[[[ 0.5, 1.5, 2.5, 3.5], - [ 4.5, 5.5, 6.5, 7.5], - [ 8.5, 9.5, 10.5, 11.5], - [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) - """ data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] @@ -96,7 +62,7 @@ def test_max_pool_basic(): index_et = "i32" data_node = ov.parameter(data.shape, name="A", dtype=np.float32) - maxpool_node = ov.max_pool( + node = ov.max_pool( data_node, strides, dilations, @@ -107,23 +73,15 @@ def test_max_pool_basic(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array([[[[5.5, 6.5, 7.5], [9.5, 10.5, 11.5], [13.5, 14.5, 15.5]]]], dtype=np.float32) - expected_idx = np.array([[[[5, 6, 7], [9, 10, 11], [13, 14, 15]]]], dtype=np.int32) - assert np.allclose(result[0], expected) - assert np.allclose(result[1], expected_idx) + assert node.get_type_name() == "MaxPool" + assert node.get_output_size() == 2 + assert list(node.get_output_shape(0)) == [1, 1, 3, 3] + assert list(node.get_output_shape(1)) == [1, 1, 3, 3] + assert node.get_output_element_type(0) == Type.f32 + assert node.get_output_element_type(1) == Type.i32 def test_max_pool_strides(): - rt = get_runtime() - - """array([[[[ 0.5, 1.5, 2.5, 3.5], - [ 4.5, 5.5, 6.5, 7.5], - [ 8.5, 9.5, 10.5, 11.5], - [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) - """ data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [2, 1] dilations = [1, 1] @@ -135,7 +93,7 @@ def test_max_pool_strides(): index_et = "i32" data_node = ov.parameter(data.shape, name="A", dtype=np.float32) - maxpool_node = ov.max_pool( + node = ov.max_pool( data_node, strides, dilations, @@ -146,23 +104,15 @@ def test_max_pool_strides(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array([[[[5.5, 6.5, 7.5], [13.5, 14.5, 15.5]]]], dtype=np.float32) - expected_idx = np.array([[[[5, 6, 7], [13, 14, 15]]]], dtype=np.int32) - assert np.allclose(result[0], expected) - assert np.allclose(result[1], expected_idx) + assert node.get_type_name() == "MaxPool" + assert node.get_output_size() == 2 + assert list(node.get_output_shape(0)) == [1, 1, 2, 3] + assert list(node.get_output_shape(1)) == [1, 1, 2, 3] + assert node.get_output_element_type(0) == Type.f32 + assert node.get_output_element_type(1) == Type.i32 def test_max_pool_kernel_shape1x1(): - rt = get_runtime() - - """array([[[[ 0.5, 1.5, 2.5, 3.5], - [ 4.5, 5.5, 6.5, 7.5], - [ 8.5, 9.5, 10.5, 11.5], - [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) - """ data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] @@ -174,7 +124,7 @@ def test_max_pool_kernel_shape1x1(): index_et = "i32" data_node = ov.parameter(data.shape, name="A", dtype=np.float32) - maxpool_node = ov.max_pool( + node = ov.max_pool( data_node, strides, dilations, @@ -185,21 +135,15 @@ def test_max_pool_kernel_shape1x1(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - assert np.allclose(result[0], data) - assert np.allclose(result[1], np.arange(0, 16, dtype=np.int32).reshape((1, 1, 4, 4))) + assert node.get_type_name() == "MaxPool" + assert node.get_output_size() == 2 + assert list(node.get_output_shape(0)) == [1, 1, 4, 4] + assert list(node.get_output_shape(1)) == [1, 1, 4, 4] + assert node.get_output_element_type(0) == Type.f32 + assert node.get_output_element_type(1) == Type.i32 def test_max_pool_kernel_shape3x3(): - rt = get_runtime() - - """array([[[[ 0.5, 1.5, 2.5, 3.5], - [ 4.5, 5.5, 6.5, 7.5], - [ 8.5, 9.5, 10.5, 11.5], - [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) - """ data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] @@ -211,7 +155,7 @@ def test_max_pool_kernel_shape3x3(): index_et = "i32" data_node = ov.parameter(data.shape, name="A", dtype=np.float32) - maxpool_node = ov.max_pool( + node = ov.max_pool( data_node, strides, dilations, @@ -222,40 +166,27 @@ def test_max_pool_kernel_shape3x3(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array([[[[10.5, 11.5], [14.5, 15.5]]]], dtype=np.float32) - assert np.allclose(result[0], expected) + assert node.get_type_name() == "MaxPool" + assert node.get_output_size() == 2 + assert list(node.get_output_shape(0)) == [1, 1, 2, 2] + assert list(node.get_output_shape(1)) == [1, 1, 2, 2] + assert node.get_output_element_type(0) == Type.f32 + assert node.get_output_element_type(1) == Type.i32 def test_max_pool_non_zero_pads(): - rt = get_runtime() - - """array([[[[ 0.5, 1.5, 2.5, 3.5], - [ 4.5, 5.5, 6.5, 7.5], - [ 8.5, 9.5, 10.5, 11.5], - [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) - """ data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] pads_begin = [1, 1] pads_end = [1, 1] - """0 0 , 0 , 0 , 0, 0 - 0 [ 0.5, 1.5, 2.5, 3.5], 0, - 0 [ 4.5, 5.5, 6.5, 7.5], 0, - 0 [ 8.5, 9.5, 10.5, 11.5], 0, - 0 [12.5, 13.5, 14.5, 15.5], 0 - 0 0 , 0 , 0 , 0, 0 - """ kernel_shape = [2, 2] rounding_type = "floor" auto_pad = None index_et = "i32" data_node = ov.parameter(data.shape, name="A", dtype=np.float32) - maxpool_node = ov.max_pool( + node = ov.max_pool( data_node, strides, dilations, @@ -266,67 +197,27 @@ def test_max_pool_non_zero_pads(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array( - [ - [ - [ - [0.5, 1.5, 2.5, 3.5, 3.5], - [4.5, 5.5, 6.5, 7.5, 7.5], - [8.5, 9.5, 10.5, 11.5, 11.5], - [12.5, 13.5, 14.5, 15.5, 15.5], - [12.5, 13.5, 14.5, 15.5, 15.5], - ], - ], - ], - dtype=np.float32, - ) - expected_idx = np.array( - [ - [ - [ - [0, 1, 2, 3, 3], - [4, 5, 6, 7, 7], - [8, 9, 10, 11, 11], - [12, 13, 14, 15, 15], - [12, 13, 14, 15, 15], - ], - ], - ], - dtype=np.int32, - ) - assert np.allclose(result[0], expected) - assert np.allclose(result[1], expected_idx) + assert node.get_type_name() == "MaxPool" + assert node.get_output_size() == 2 + assert list(node.get_output_shape(0)) == [1, 1, 5, 5] + assert list(node.get_output_shape(1)) == [1, 1, 5, 5] + assert node.get_output_element_type(0) == Type.f32 + assert node.get_output_element_type(1) == Type.i32 def test_max_pool_same_upper_auto_pads(): - rt = get_runtime() - - """array([[[[ 0.5, 1.5, 2.5, 3.5], - [ 4.5, 5.5, 6.5, 7.5], - [ 8.5, 9.5, 10.5, 11.5], - [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) - """ data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] - """[ 0.5, 1.5, 2.5, 3.5], 0, - [ 4.5, 5.5, 6.5, 7.5], 0, - [ 8.5, 9.5, 10.5, 11.5], 0, - [12.5, 13.5, 14.5, 15.5], 0 - 0 , 0 , 0 , 0, 0 - """ kernel_shape = [2, 2] auto_pad = "same_upper" rounding_type = "floor" index_et = "i32" data_node = ov.parameter(data.shape, name="A", dtype=np.float32) - maxpool_node = ov.max_pool( + node = ov.max_pool( data_node, strides, dilations, @@ -337,65 +228,27 @@ def test_max_pool_same_upper_auto_pads(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array( - [ - [ - [ - [5.5, 6.5, 7.5, 7.5], - [9.5, 10.5, 11.5, 11.5], - [13.5, 14.5, 15.5, 15.5], - [13.5, 14.5, 15.5, 15.5], - ], - ], - ], - dtype=np.float32, - ) - expected_idx = np.array( - [ - [ - [ - [5, 6, 7, 7], - [9, 10, 11, 11], - [13, 14, 15, 15], - [13, 14, 15, 15], - ], - ], - ], - dtype=np.int32, - ) - assert np.allclose(result[0], expected) - assert np.allclose(result[1], expected_idx) + assert node.get_type_name() == "MaxPool" + assert node.get_output_size() == 2 + assert list(node.get_output_shape(0)) == [1, 1, 4, 4] + assert list(node.get_output_shape(1)) == [1, 1, 4, 4] + assert node.get_output_element_type(0) == Type.f32 + assert node.get_output_element_type(1) == Type.i32 def test_max_pool_same_lower_auto_pads(): - rt = get_runtime() - - """array([[[[ 0.5, 1.5, 2.5, 3.5], - [ 4.5, 5.5, 6.5, 7.5], - [ 8.5, 9.5, 10.5, 11.5], - [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) - """ data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] - """0 0 , 0 , 0 , 0, - 0 [ 0.5, 1.5, 2.5, 3.5], - 0 [ 4.5, 5.5, 6.5, 7.5], - 0 [ 8.5, 9.5, 10.5, 11.5], - 0 [12.5, 13.5, 14.5, 15.5], - """ kernel_shape = [2, 2] auto_pad = "same_lower" rounding_type = "floor" index_et = "i32" data_node = ov.parameter(data.shape, name="A", dtype=np.float32) - maxpool_node = ov.max_pool( + node = ov.max_pool( data_node, strides, dilations, @@ -406,34 +259,9 @@ def test_max_pool_same_lower_auto_pads(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array( - [ - [ - [ - [0.5, 1.5, 2.5, 3.5], - [4.5, 5.5, 6.5, 7.5], - [8.5, 9.5, 10.5, 11.5], - [12.5, 13.5, 14.5, 15.5], - ], - ], - ], - dtype=np.float32, - ) - expected_idx = np.array( - [ - [ - [ - [0, 1, 2, 3], - [4, 5, 6, 7], - [8, 9, 10, 11], - [12, 13, 14, 15], - ], - ], - ], - dtype=np.int32, - ) - assert np.allclose(result[0], expected) - assert np.allclose(result[1], expected_idx) + assert node.get_type_name() == "MaxPool" + assert node.get_output_size() == 2 + assert list(node.get_output_shape(0)) == [1, 1, 4, 4] + assert list(node.get_output_shape(1)) == [1, 1, 4, 4] + assert node.get_output_element_type(0) == Type.f32 + assert node.get_output_element_type(1) == Type.i32 diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.py index 411903b821e..247763fd966 100644 --- a/src/bindings/python/tests/test_graph/test_preprocess.py +++ b/src/bindings/python/tests/test_graph/test_preprocess.py @@ -10,7 +10,6 @@ import openvino.runtime.opset8 as ops from openvino.runtime import Model, Output, Type from openvino.runtime.utils.decorators import custom_preprocess_function from openvino.runtime import Core -from tests.runtime import get_runtime from openvino.preprocess import PrePostProcessor, ColorFormat, ResizeAlgorithm @@ -19,20 +18,18 @@ def test_graph_preprocess_mean(): parameter_a = ops.parameter(shape, dtype=np.float32, name="A") model = parameter_a function = Model(model, [parameter_a], "TestFunction") - ppp = PrePostProcessor(function) inp = ppp.input() prep = inp.preprocess() prep.mean(1.0) function = ppp.build() - - input_data = np.array([[1, 2], [3, 4]]).astype(np.float32) - expected_output = np.array([[0, 1], [2, 3]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ordered_ops()] + assert len(model_operators) == 4 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [2, 2] + assert function.get_output_element_type(0) == Type.f32 + assert "Constant" in model_operators + assert "Subtract" in model_operators def test_graph_preprocess_mean_vector(): @@ -47,13 +44,13 @@ def test_graph_preprocess_mean_vector(): ppp.input().preprocess().mean([1., 2.]) function = ppp.build() - input_data = np.array([[1, 2], [3, 4]]).astype(np.float32) - expected_output = np.array([[0, 0], [2, 2]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ordered_ops()] + assert len(model_operators) == 4 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [2, 2] + assert function.get_output_element_type(0) == Type.f32 + assert "Constant" in model_operators + assert "Subtract" in model_operators def test_graph_preprocess_scale_vector(): @@ -69,13 +66,13 @@ def test_graph_preprocess_scale_vector(): inp.preprocess().scale([0.5, 2.0]) function = ppp.build() - input_data = np.array([[1, 2], [3, 4]]).astype(np.float32) - expected_output = np.array([[2, 1], [6, 2]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ordered_ops()] + assert len(model_operators) == 4 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [2, 2] + assert function.get_output_element_type(0) == Type.f32 + assert "Constant" in model_operators + assert "Divide" in model_operators def test_graph_preprocess_mean_scale_convert(): @@ -97,16 +94,24 @@ def test_graph_preprocess_mean_scale_convert(): inp1.preprocess().convert_element_type(Type.f32).mean(1.).custom(custom_preprocess) function = ppp.build() - input_data1 = np.array([[0, 1], [2, -2]]).astype(np.int32) - input_data2 = np.array([[1, 3], [5, 7]]).astype(np.int32) - expected_output1 = np.array([[1, 0], [1, 3]]).astype(np.float32) - expected_output2 = np.array([[0, 1], [2, 3]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - [output1, output2] = computation(input_data1, input_data2) - assert np.equal(output1, expected_output1).all() - assert np.equal(output2, expected_output2).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Convert", + "Constant", + "Subtract", + "Divide", + "Result", + "Abs", + ] + assert len(model_operators) == 15 + assert function.get_output_size() == 2 + assert list(function.get_output_shape(0)) == [2, 2] + assert list(function.get_output_shape(1)) == [2, 2] + assert function.get_output_element_type(0) == Type.i32 + assert function.get_output_element_type(1) == Type.i32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_input_output_by_name(): @@ -131,16 +136,24 @@ def test_graph_preprocess_input_output_by_name(): out2.postprocess().custom(custom_preprocess) function = ppp.build() - input_data1 = np.array([[0, 1], [2, -2]]).astype(np.int32) - input_data2 = np.array([[-1, 3], [5, 7]]).astype(np.int32) - expected_output1 = np.array([[1, 0], [1, 3]]).astype(np.float32) - expected_output2 = np.array([[1, 1], [2, 3]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - [output1, output2] = computation(input_data1, input_data2) - assert np.equal(output1, expected_output1).all() - assert np.equal(output2, expected_output2).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Convert", + "Constant", + "Subtract", + "Divide", + "Result", + "Abs", + ] + assert len(model_operators) == 16 + assert function.get_output_size() == 2 + assert list(function.get_output_shape(0)) == [2, 2] + assert list(function.get_output_shape(1)) == [2, 2] + assert function.get_output_element_type(0) == Type.i32 + assert function.get_output_element_type(1) == Type.i32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_output_postprocess(): @@ -155,7 +168,6 @@ def test_graph_preprocess_output_postprocess(): @custom_preprocess_function def custom_postprocess(output: Output): return ops.abs(output) - ppp = PrePostProcessor(function) inp = ppp.input() inp.tensor().set_layout(layout1) @@ -168,13 +180,22 @@ def test_graph_preprocess_output_postprocess(): out.postprocess().custom(custom_postprocess).convert_element_type(Type.f16).convert_element_type() function = ppp.build() - input_data = np.array([[-1, -2, -3], [-4, -5, -6]]).astype(np.int32) - expected_output = np.array([[2, 4, 6], [5, 7, 9]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Convert", + "Constant", + "Subtract", + "Transpose", + "Result", + "Abs", + ] + assert len(model_operators) == 14 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [2, 3] + assert function.get_output_element_type(0) == Type.f32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_spatial_static_shape(): @@ -196,13 +217,20 @@ def test_graph_preprocess_spatial_static_shape(): out.model().set_layout(layout) function = ppp.build() - input_data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]).astype(np.int32) - expected_output = np.array([[[0, 1], [2, 3]], [[3, 4], [5, 6]]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Convert", + "Constant", + "Subtract", + "Result", + ] + assert len(model_operators) == 7 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [2, 2, 2] + assert function.get_output_element_type(0) == Type.f32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_set_shape(): @@ -225,15 +253,19 @@ def test_graph_preprocess_set_shape(): inp.preprocess().custom(custom_crop) function = ppp.build() - input_data = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], - [[9, 10, 11], [12, 13, 14], [15, 16, 17]], - [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]).astype(np.int32) - expected_output = np.array([[[13]]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Constant", + "Result", + "Slice", + ] + assert len(model_operators) == 7 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [1, 1, 1] + assert function.get_output_element_type(0) == Type.i32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_set_from_tensor(): @@ -282,12 +314,20 @@ def test_graph_preprocess_set_from_np_infer(): assert function.input().shape == ov.Shape([3, 3, 3]) assert function.input().element_type == Type.i32 - expected_output = np.array([[[13]]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Convert", + "Constant", + "Result", + "Slice", + ] + assert len(model_operators) == 8 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [1, 1, 1] + assert function.get_output_element_type(0) == Type.f32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_set_memory_type(): @@ -344,13 +384,20 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail assert "is not convertible to" in str(e.value) else: function = custom_processor.build() - input_data = np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]]).astype(np.float32) - expected_output = np.array([[[[0, 3, 6], [1, 4, 7], [2, 5, 8]]]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Constant", + "Result", + "Gather", + "Interpolate", + ] + assert len(model_operators) == 16 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [1, 1, 3, 3] + assert function.get_output_element_type(0) == Type.f32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_postprocess_layout(): @@ -369,13 +416,21 @@ def test_graph_preprocess_postprocess_layout(): out.postprocess().convert_layout([0, 1, 2, 3]) function = ppp.build() - input_data = np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]]).astype(np.float32) - expected_output = np.array([[[[0, 3, 6], [1, 4, 7], [2, 5, 8]]]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Constant", + "Result", + "Gather", + "Range", + "Transpose", + ] + assert len(model_operators) == 14 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [1, 1, 3, 3] + assert function.get_output_element_type(0) == Type.f32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_reverse_channels(): @@ -391,13 +446,20 @@ def test_graph_preprocess_reverse_channels(): inp.preprocess().mean(1.).reverse_channels() function = ppp.build() - input_data = np.array([[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]]).astype(np.float32) - expected_output = np.array([[[[4, 5], [6, 7]], [[0, 1], [2, 3]]]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Constant", + "Result", + "Gather", + "Range", + ] + assert len(model_operators) == 10 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [1, 2, 2, 2] + assert function.get_output_element_type(0) == Type.f32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_crop(): @@ -412,13 +474,20 @@ def test_graph_preprocess_crop(): ppp.input().preprocess().crop([0, 0, 1, 1], [1, 2, -1, -1]) function = ppp.build() - input_data = np.arange(18).astype(np.float32).reshape(tensor_shape) - expected_output = np.array([4, 13]).astype(np.float32).reshape(orig_shape) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Constant", + "Result", + "Relu", + "Slice", + ] + assert len(model_operators) == 7 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [1, 2, 1, 1] + assert function.get_output_element_type(0) == Type.f32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_resize_algorithm(): @@ -435,13 +504,20 @@ def test_graph_preprocess_resize_algorithm(): inp.preprocess().mean(1.).resize(resize_alg, 3, 3) function = ppp.build() - input_data = np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]]).astype(np.float32) - expected_output = np.array([[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data) - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Constant", + "Result", + "Subtract", + "Interpolate", + ] + assert len(model_operators) == 8 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [1, 1, 3, 3] + assert function.get_output_element_type(0) == Type.f32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_model(): @@ -517,14 +593,23 @@ def test_graph_preprocess_model(): ppp.output(0).postprocess().custom(custom_preprocess) function = ppp.build() - input_data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]).astype(np.float32) - expected_output = np.array([[[2, 1], [4, 7]], [[10, 13], [16, 19]]]).astype(np.float32) - - runtime = get_runtime() - computation = runtime.computation(function) - output = computation(input_data, input_data) - - assert np.equal(output, expected_output).all() + model_operators = [op.get_name().split("_")[0] for op in function.get_ops()] + expected_ops = [ + "Parameter", + "Constant", + "Result", + "Subtract", + "Convert", + "Abs", + "Add", + "Divide", + ] + assert len(model_operators) == 13 + assert function.get_output_size() == 1 + assert list(function.get_output_shape(0)) == [2, 2, 2] + assert function.get_output_element_type(0) == Type.i32 + for op in expected_ops: + assert op in model_operators def test_graph_preprocess_dump(): diff --git a/src/bindings/python/tests/test_graph/test_random_uniform.py b/src/bindings/python/tests/test_graph/test_random_uniform.py index fac2f66b608..dcc42fb2930 100644 --- a/src/bindings/python/tests/test_graph/test_random_uniform.py +++ b/src/bindings/python/tests/test_graph/test_random_uniform.py @@ -2,30 +2,20 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 -import openvino.runtime.opset8 as ov +import openvino.runtime as ov +import openvino.runtime.opset8 as ops import numpy as np -from tests.runtime import get_runtime def test_random_uniform(): - runtime = get_runtime() - input_tensor = ov.constant(np.array([2, 4, 3], dtype=np.int32)) - min_val = ov.constant(np.array([-2.7], dtype=np.float32)) - max_val = ov.constant(np.array([3.5], dtype=np.float32)) + input_tensor = ops.constant(np.array([2, 4, 3], dtype=np.int32)) + min_val = ops.constant(np.array([-2.7], dtype=np.float32)) + max_val = ops.constant(np.array([3.5], dtype=np.float32)) - random_uniform_node = ov.random_uniform(input_tensor, min_val, max_val, - output_type="f32", global_seed=7461, - op_seed=1546) - computation = runtime.computation(random_uniform_node) - random_uniform_results = computation() - expected_results = np.array([[[2.8450181, -2.3457108, 2.2134445], - [-1.0436587, 0.79548645, 1.3023183], - [0.34447956, -2.0267959, 1.3989122], - [0.9607613, 1.5363653, 3.117298]], - - [[1.570041, 2.2782724, 2.3193843], - [3.3393657, 0.63299894, 0.41231918], - [3.1739233, 0.03919673, -0.2136085], - [-1.4519991, -2.277353, 2.630727]]], dtype=np.float32) - - assert np.allclose(random_uniform_results, expected_results) + random_uniform_node = ops.random_uniform(input_tensor, min_val, max_val, + output_type="f32", global_seed=7461, + op_seed=1546) + assert random_uniform_node.get_output_size() == 1 + assert random_uniform_node.get_type_name() == "RandomUniform" + assert random_uniform_node.get_output_element_type(0) == ov.Type.f32 + assert list(random_uniform_node.get_output_shape(0)) == [2, 4, 3] diff --git a/src/bindings/python/tests/test_graph/test_rdft.py b/src/bindings/python/tests/test_graph/test_rdft.py index 3c3392f72aa..28e3c68ba0d 100644 --- a/src/bindings/python/tests/test_graph/test_rdft.py +++ b/src/bindings/python/tests/test_graph/test_rdft.py @@ -3,163 +3,70 @@ # SPDX-License-Identifier: Apache-2.0 import openvino.runtime.opset9 as ov -from openvino.runtime import Shape +from openvino.runtime import Shape, Type import numpy as np -from tests.runtime import get_runtime +import pytest np.random.seed(0) -def test_rdft_1d(): - runtime = get_runtime() - input_size = 50 - shape = [input_size] - data = np.random.uniform(0, 1, shape).astype(np.float32) +@pytest.mark.parametrize(("shape", "axes", "expected_shape"), [ + ([50], [0], [26, 2]), + ([100, 128], [0, 1], [100, 65, 2]), + ([1, 192, 36, 64], [-2, -1], [1, 192, 36, 33, 2]), +]) +def test_rdft(shape, axes, expected_shape): param = ov.parameter(Shape(shape), name="input", dtype=np.float32) - input_axes = ov.constant(np.array([0], dtype=np.int64)) - - node = ov.rdft(param, input_axes) - computation = runtime.computation(node, param) - actual = computation(data) - np_results = np.fft.rfft(data) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - np.testing.assert_allclose(expected_results, actual[0], atol=0.0001) - - -def test_irdft_1d(): - runtime = get_runtime() - signal_size = 50 - shape = [signal_size // 2 + 1, 2] - data = np.random.uniform(0, 1, shape).astype(np.float32) - param = ov.parameter(Shape(shape), name="input", dtype=np.float32) - input_axes = ov.constant(np.array([0], dtype=np.int64)) - node = ov.irdft(param, input_axes, ov.constant(np.array([signal_size], dtype=np.int64))) - computation = runtime.computation(node, param) - actual = computation(data) - expected_results = np.fft.irfft(data[:, 0] + 1j * data[:, 1], signal_size) - np.testing.assert_allclose(expected_results, actual[0], atol=0.0001) - - -def test_rdft_2d(): - runtime = get_runtime() - shape = [100, 128] - data = np.random.uniform(0, 1, shape).astype(np.float32) - param = ov.parameter(Shape(shape), name="input", dtype=np.float32) - axes = [0, 1] input_axes = ov.constant(np.array(axes, dtype=np.int64)) + node = ov.rdft(param, input_axes) - computation = runtime.computation(node, param) - actual = computation(data) - np_results = np.fft.rfftn(data, axes=axes) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - np.testing.assert_allclose(expected_results, actual[0], atol=0.0007) + assert node.get_type_name() == "RDFT" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 -def test_rdft_2d_signal_size(): - runtime = get_runtime() - shape = [100, 128] - data = np.random.uniform(0, 1, shape).astype(np.float32) +@pytest.mark.parametrize(("shape", "axes", "expected_shape"), [ + ([100, 65, 2], [0, 1], [100, 128]), + ([1, 192, 36, 33, 2], [-2, -1], [1, 192, 36, 64]), +]) +def test_irdft(shape, axes, expected_shape): + param = ov.parameter(Shape(shape), name="input", dtype=np.float32) + input_axes = ov.constant(np.array(axes, dtype=np.int64)) + node = ov.irdft(param, input_axes) + assert node.get_type_name() == "IRDFT" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 + + +@pytest.mark.parametrize(("shape", "axes", "expected_shape", "signal_size"), [ + ([26, 2], [0], [50], [50]), + ([100, 65, 2], [0, 1], [100, 65], [100, 65]), + ([1, 192, 36, 33, 2], [-2, -1], [1, 192, 36, 64], [36, 64]), +]) +def test_irdft_signal_size(shape, axes, expected_shape, signal_size): + param = ov.parameter(Shape(shape), name="input", dtype=np.float32) + input_axes = ov.constant(np.array(axes, dtype=np.int64)) + signal_size_node = ov.constant(np.array(signal_size, dtype=np.int64)) + node = ov.irdft(param, input_axes, signal_size_node) + assert node.get_type_name() == "IRDFT" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 + + +@pytest.mark.parametrize(("shape", "axes", "expected_shape", "signal_size"), [ + ([100, 128], [0, 1], [30, 21, 2], [30, 40]), + ([1, 192, 36, 64], [-2, -1], [1, 192, 36, 33, 2], [36, 64]), +]) +def test_rdft_signal_size(shape, axes, expected_shape, signal_size): param = ov.parameter(Shape(shape), name="input", dtype=np.float32) - axes = [0, 1] - signal_size = [30, 40] axes_node = ov.constant(np.array(axes, dtype=np.int64)) signal_size_node = ov.constant(np.array(signal_size, dtype=np.int64)) node = ov.rdft(param, axes_node, signal_size_node) - computation = runtime.computation(node, param) - actual = computation(data) - np_results = np.fft.rfftn(data, s=signal_size, axes=axes) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - np.testing.assert_allclose(expected_results, actual[0], atol=0.0007) - - -def test_irdft_2d(): - runtime = get_runtime() - axes = [0, 1] - input_shape = [100, 65, 2] - data = np.random.uniform(0, 1, input_shape).astype(np.float32) - param = ov.parameter(Shape(input_shape), name="input", dtype=np.float32) - input_axes = ov.constant(np.array(axes, dtype=np.int64)) - node = ov.irdft(param, input_axes) - computation = runtime.computation(node, param) - actual = computation(data) - expected_results = np.fft.irfftn(data[:, :, 0] + 1j * data[:, :, 1], axes=axes) - np.testing.assert_allclose(expected_results, actual[0], atol=0.0001) - - -def test_irdft_2d_signal_size(): - runtime = get_runtime() - axes = [0, 1] - input_shape = [100, 65, 2] - signal_size = [100, 65] - data = np.random.uniform(0, 1, input_shape).astype(np.float32) - param = ov.parameter(Shape(input_shape), name="input", dtype=np.float32) - input_axes = ov.constant(np.array(axes, dtype=np.int64)) - signal_size_node = ov.constant(np.array(signal_size, dtype=np.int64)) - node = ov.irdft(param, input_axes, signal_size_node) - computation = runtime.computation(node, param) - actual = computation(data) - expected_results = np.fft.irfftn(data[:, :, 0] + 1j * data[:, :, 1], s=signal_size, axes=axes) - np.testing.assert_allclose(expected_results, actual[0], atol=0.0001) - - -def test_rdft_4d(): - runtime = get_runtime() - shape = [1, 192, 36, 64] - data = np.random.uniform(0, 1, shape).astype(np.float32) - param = ov.parameter(Shape(shape), name="input", dtype=np.float32) - axes = [-2, -1] - input_axes = ov.constant(np.array(axes, dtype=np.int64)) - node = ov.rdft(param, input_axes) - computation = runtime.computation(node, param) - actual = computation(data) - np_results = np.fft.rfftn(data, axes=axes) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - np.testing.assert_allclose(expected_results, actual[0], atol=0.0007) - - -def test_rdft_4d_signal_size(): - runtime = get_runtime() - shape = [1, 192, 36, 64] - signal_size = [36, 64] - data = np.random.uniform(0, 1, shape).astype(np.float32) - param = ov.parameter(Shape(shape), name="input", dtype=np.float32) - axes = [-2, -1] - input_axes = ov.constant(np.array(axes, dtype=np.int64)) - signal_size_node = ov.constant(np.array(signal_size, dtype=np.int64)) - node = ov.rdft(param, input_axes, signal_size_node) - computation = runtime.computation(node, param) - actual = computation(data) - np_results = np.fft.rfftn(data, signal_size, axes=axes) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - np.testing.assert_allclose(expected_results, actual[0], atol=0.0007) - - -def test_irdft_4d(): - runtime = get_runtime() - shape = [1, 192, 36, 33, 2] - data = np.random.uniform(0, 1, shape).astype(np.float32) - param = ov.parameter(Shape(shape), name="input", dtype=np.float32) - axes = [-2, -1] - input_axes = ov.constant(np.array(axes, dtype=np.int64)) - node = ov.irdft(param, input_axes) - computation = runtime.computation(node, param) - actual = computation(data) - expected_results = np.fft.irfftn(data[:, :, :, :, 0] + 1j * data[:, :, :, :, 1], axes=axes) - np.testing.assert_allclose(expected_results, actual[0], atol=0.0001) - - -def test_irdft_4d_signal_size(): - runtime = get_runtime() - shape = [1, 192, 36, 33, 2] - signal_size = [36, 64] - data = np.random.uniform(0, 1, shape).astype(np.float32) - param = ov.parameter(Shape(shape), name="input", dtype=np.float32) - axes = [-2, -1] - input_axes = ov.constant(np.array(axes, dtype=np.int64)) - signal_size_node = ov.constant(np.array(signal_size, dtype=np.int64)) - node = ov.irdft(param, input_axes, signal_size_node) - computation = runtime.computation(node, param) - actual = computation(data) - expected_results = np.fft.irfftn(data[:, :, :, :, 0] + 1j * data[:, :, :, :, 1], signal_size, axes=axes) - np.testing.assert_allclose(expected_results, actual[0], atol=0.0001) + assert node.get_type_name() == "RDFT" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests/test_graph/test_reduction.py b/src/bindings/python/tests/test_graph/test_reduction.py index 8adaf02b914..85c1cb54740 100644 --- a/src/bindings/python/tests/test_graph/test_reduction.py +++ b/src/bindings/python/tests/test_graph/test_reduction.py @@ -4,60 +4,56 @@ import numpy as np import pytest -from openvino.runtime import PartialShape, Dimension import openvino.runtime.opset9 as ov -from openvino.runtime.utils.types import make_constant_node -from tests.runtime import get_runtime -from tests.test_graph.util import run_op_node @pytest.mark.parametrize( - ("graph_api_helper", "numpy_function", "reduction_axes"), + ("graph_api_helper", "reduction_axes", "expected_shape"), [ - (ov.reduce_max, np.max, np.array([0, 1, 2, 3])), - (ov.reduce_min, np.min, np.array([0, 1, 2, 3])), - (ov.reduce_sum, np.sum, np.array([0, 1, 2, 3])), - (ov.reduce_prod, np.prod, np.array([0, 1, 2, 3])), - (ov.reduce_max, np.max, np.array([0])), - (ov.reduce_min, np.min, np.array([0])), - (ov.reduce_sum, np.sum, np.array([0])), - (ov.reduce_prod, np.prod, np.array([0])), - (ov.reduce_max, np.max, np.array([0, 2])), - (ov.reduce_min, np.min, np.array([0, 2])), - (ov.reduce_sum, np.sum, np.array([0, 2])), - (ov.reduce_prod, np.prod, np.array([0, 2])), + (ov.reduce_max, np.array([0, 1, 2, 3]), []), + (ov.reduce_min, np.array([0, 1, 2, 3]), []), + (ov.reduce_sum, np.array([0, 1, 2, 3]), []), + (ov.reduce_prod, np.array([0, 1, 2, 3]), []), + (ov.reduce_max, np.array([0]), [4, 3, 2]), + (ov.reduce_min, np.array([0]), [4, 3, 2]), + (ov.reduce_sum, np.array([0]), [4, 3, 2]), + (ov.reduce_prod, np.array([0]), [4, 3, 2]), + (ov.reduce_max, np.array([0, 2]), [4, 2]), + (ov.reduce_min, np.array([0, 2]), [4, 2]), + (ov.reduce_sum, np.array([0, 2]), [4, 2]), + (ov.reduce_prod, np.array([0, 2]), [4, 2]), ], ) -def test_reduction_ops(graph_api_helper, numpy_function, reduction_axes): +def test_reduction_ops(graph_api_helper, reduction_axes, expected_shape): shape = [2, 4, 3, 2] np.random.seed(133391) input_data = np.random.randn(*shape).astype(np.float32) - expected = numpy_function(input_data, axis=tuple(reduction_axes)) - result = run_op_node([input_data], graph_api_helper, reduction_axes) - assert np.allclose(result, expected) + node = graph_api_helper(input_data, reduction_axes) + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape @pytest.mark.parametrize( - ("graph_api_helper", "numpy_function", "reduction_axes"), + ("graph_api_helper", "reduction_axes", "expected_shape"), [ - (ov.reduce_logical_and, np.logical_and.reduce, np.array([0])), - (ov.reduce_logical_or, np.logical_or.reduce, np.array([0])), - (ov.reduce_logical_and, np.logical_and.reduce, np.array([0, 2])), - (ov.reduce_logical_or, np.logical_or.reduce, np.array([0, 2])), - (ov.reduce_logical_and, np.logical_and.reduce, np.array([0, 1, 2, 3])), - (ov.reduce_logical_or, np.logical_or.reduce, np.array([0, 1, 2, 3])), + (ov.reduce_logical_and, np.array([0]), [4, 3, 2]), + (ov.reduce_logical_or, np.array([0]), [4, 3, 2]), + (ov.reduce_logical_and, np.array([0, 2]), [4, 2]), + (ov.reduce_logical_or, np.array([0, 2]), [4, 2]), + (ov.reduce_logical_and, np.array([0, 1, 2, 3]), []), + (ov.reduce_logical_or, np.array([0, 1, 2, 3]), []), ], ) -def test_reduction_logical_ops(graph_api_helper, numpy_function, reduction_axes): +def test_reduction_logical_ops(graph_api_helper, reduction_axes, expected_shape): shape = [2, 4, 3, 2] np.random.seed(133391) input_data = np.random.randn(*shape).astype(bool) - expected = numpy_function(input_data, axis=tuple(reduction_axes)) - result = run_op_node([input_data], graph_api_helper, reduction_axes) - assert np.allclose(result, expected) + node = graph_api_helper(input_data, reduction_axes) + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape def test_topk(): @@ -73,21 +69,21 @@ def test_topk(): @pytest.mark.parametrize( - ("graph_api_helper", "numpy_function", "reduction_axes"), + ("graph_api_helper", "reduction_axes", "expected_shape"), [ - (ov.reduce_mean, np.mean, np.array([0, 1, 2, 3])), - (ov.reduce_mean, np.mean, np.array([0])), - (ov.reduce_mean, np.mean, np.array([0, 2])), + (ov.reduce_mean, np.array([0, 1, 2, 3]), []), + (ov.reduce_mean, np.array([0]), [4, 3, 2]), + (ov.reduce_mean, np.array([0, 2]), [4, 2]), ], ) -def test_reduce_mean_op(graph_api_helper, numpy_function, reduction_axes): +def test_reduce_mean_op(graph_api_helper, reduction_axes, expected_shape): shape = [2, 4, 3, 2] np.random.seed(133391) input_data = np.random.randn(*shape).astype(np.float32) - expected = numpy_function(input_data, axis=tuple(reduction_axes)) - result = run_op_node([input_data], graph_api_helper, reduction_axes) - assert np.allclose(result, expected) + node = graph_api_helper(input_data, reduction_axes) + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape def test_non_zero(): @@ -107,7 +103,6 @@ def test_roi_align(): data_shape = [7, 256, 200, 200] rois = [1000, 4] batch_indices = [1000] - expected_shape = [1000, 256, 6, 6] data_parameter = ov.parameter(data_shape, name="Data", dtype=np.float32) rois_parameter = ov.parameter(rois, name="Rois", dtype=np.float32) @@ -131,7 +126,7 @@ def test_roi_align(): assert node.get_type_name() == "ROIAlign" assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == expected_shape + assert list(node.get_output_shape(0)) == [1000, 256, 6, 6] @pytest.mark.parametrize( @@ -141,16 +136,10 @@ def test_roi_align(): def test_cum_sum(input_shape, cumsum_axis, reverse): input_data = np.arange(np.prod(input_shape)).reshape(input_shape) - if reverse: - expected = np.cumsum(input_data[::-1], axis=cumsum_axis)[::-1] - else: - expected = np.cumsum(input_data, axis=cumsum_axis) - - runtime = get_runtime() node = ov.cum_sum(input_data, cumsum_axis, reverse=reverse) - computation = runtime.computation(node) - result = computation() - assert np.allclose(result, expected) + assert node.get_output_size() == 1 + assert node.get_type_name() == "CumSum" + assert list(node.get_output_shape(0)) == input_shape def test_normalize_l2(): @@ -161,38 +150,7 @@ def test_normalize_l2(): eps = 1e-6 eps_mode = "add" - runtime = get_runtime() node = ov.normalize_l2(input_data, axes, eps, eps_mode) - computation = runtime.computation(node) - result = computation() - - expected = np.array( - [ - 0.01428571, - 0.02857143, - 0.04285714, - 0.05714286, - 0.07142857, - 0.08571429, - 0.1, - 0.11428571, - 0.12857144, - 0.14285715, - 0.15714286, - 0.17142858, - 0.18571429, - 0.2, - 0.21428572, - 0.22857143, - 0.24285714, - 0.25714287, - 0.27142859, - 0.2857143, - 0.30000001, - 0.31428573, - 0.32857144, - 0.34285715, - ], - ).reshape(input_shape) - - assert np.allclose(result, expected) + assert node.get_output_size() == 1 + assert node.get_type_name() == "NormalizeL2" + assert list(node.get_output_shape(0)) == input_shape diff --git a/src/bindings/python/tests/test_graph/test_roll.py b/src/bindings/python/tests/test_graph/test_roll.py index 7496abb7f28..c0e2fd99066 100644 --- a/src/bindings/python/tests/test_graph/test_roll.py +++ b/src/bindings/python/tests/test_graph/test_roll.py @@ -4,19 +4,15 @@ import openvino.runtime.opset8 as ov import numpy as np -from tests.runtime import get_runtime def test_roll(): - runtime = get_runtime() input_vals = np.reshape(np.arange(10), (2, 5)) input_tensor = ov.constant(input_vals) input_shift = ov.constant(np.array([-10, 7], dtype=np.int32)) input_axes = ov.constant(np.array([-1, 0], dtype=np.int32)) roll_node = ov.roll(input_tensor, input_shift, input_axes) - computation = runtime.computation(roll_node) - roll_results = computation() - expected_results = np.roll(input_vals, shift=(-10, 7), axis=(-1, 0)) - - assert np.allclose(roll_results, expected_results) + assert roll_node.get_output_size() == 1 + assert roll_node.get_type_name() == "Roll" + assert list(roll_node.get_output_shape(0)) == [2, 5] diff --git a/src/bindings/python/tests/test_graph/test_sequence_processing.py b/src/bindings/python/tests/test_graph/test_sequence_processing.py index 3894739499b..21e17e4a142 100644 --- a/src/bindings/python/tests/test_graph/test_sequence_processing.py +++ b/src/bindings/python/tests/test_graph/test_sequence_processing.py @@ -3,34 +3,21 @@ # SPDX-License-Identifier: Apache-2.0 import numpy as np +import pytest import openvino.runtime.opset8 as ov -from tests.runtime import get_runtime -from tests.test_graph.util import run_op_node -def test_onehot(): - runtime = get_runtime() +@pytest.mark.parametrize(("depth", "on_value", "off_value", "axis", "expected_shape"), [ + (2, 5, 10, -1, [3, 2]), + (3, 1, 0, 0, [3, 3]), +]) +def test_one_hot(depth, on_value, off_value, axis, expected_shape): param = ov.parameter([3], dtype=np.int32) - model = ov.one_hot(param, 3, 1, 0, 0) - computation = runtime.computation(model, param) - - expected = np.eye(3)[np.array([1, 0, 2])] - input_data = np.array([1, 0, 2], dtype=np.int32) - result = computation(input_data) - assert np.allclose(result, expected) - - -def test_one_hot(): - data = np.array([0, 1, 2], dtype=np.int32) - depth = 2 - on_value = 5 - off_value = 10 - axis = -1 - excepted = [[5, 10], [10, 5], [10, 10]] - - result = run_op_node([data, depth, on_value, off_value], ov.one_hot, axis) - assert np.allclose(result, excepted) + node = ov.one_hot(param, depth, on_value, off_value, axis) + assert node.get_output_size() == 1 + assert node.get_type_name() == "OneHot" + assert list(node.get_output_shape(0)) == expected_shape def test_range(): @@ -38,5 +25,7 @@ def test_range(): stop = 35 step = 5 - result = run_op_node([start, stop, step], ov.range) - assert np.allclose(result, [5, 10, 15, 20, 25, 30]) + node = ov.range(start, stop, step) + assert node.get_output_size() == 1 + assert node.get_type_name() == "Range" + assert list(node.get_output_shape(0)) == [6] diff --git a/src/bindings/python/tests/test_graph/test_swish.py b/src/bindings/python/tests/test_graph/test_swish.py index bb61f4137e9..1bae8934c4d 100644 --- a/src/bindings/python/tests/test_graph/test_swish.py +++ b/src/bindings/python/tests/test_graph/test_swish.py @@ -3,27 +3,18 @@ # SPDX-License-Identifier: Apache-2.0 import numpy as np +import pytest import openvino.runtime.opset8 as ov from openvino.runtime import Shape, Type -def test_swish_props_with_beta(): - float_dtype = np.float32 - data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data") - beta = ov.parameter(Shape([]), dtype=float_dtype, name="beta") +@pytest.mark.parametrize(("beta"), [ + [], + [ov.parameter(Shape([]), dtype=np.float32, name="beta")]]) +def test_swish(beta): + data = ov.parameter(Shape([3, 10]), dtype=np.float32, name="data") - node = ov.swish(data, beta) - assert node.get_type_name() == "Swish" - assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == [3, 10] - assert node.get_output_element_type(0) == Type.f32 - - -def test_swish_props_without_beta(): - float_dtype = np.float32 - data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data") - - node = ov.swish(data) + node = ov.swish(data, *beta) assert node.get_type_name() == "Swish" assert node.get_output_size() == 1 assert list(node.get_output_shape(0)) == [3, 10] diff --git a/src/bindings/python/tests/test_graph/test_utils.py b/src/bindings/python/tests/test_graph/test_utils.py index 4801ea14a52..06f25d89adb 100644 --- a/src/bindings/python/tests/test_graph/test_utils.py +++ b/src/bindings/python/tests/test_graph/test_utils.py @@ -2,7 +2,6 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 -import numpy as np import openvino.runtime as ov import pytest from openvino._pyopenvino.util import deprecation_warning diff --git a/src/bindings/python/tests/test_graph/util.py b/src/bindings/python/tests/test_graph/util.py index a5aed287f5e..1487513ffae 100644 --- a/src/bindings/python/tests/test_graph/util.py +++ b/src/bindings/python/tests/test_graph/util.py @@ -2,75 +2,6 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 -from typing import Any, Callable, List, Union - -import numpy as np - -import openvino.runtime.opset8 as ov -from openvino.runtime.utils.types import NumericData -from tests.runtime import get_runtime -from string import ascii_uppercase - - -def _get_numpy_dtype(scalar): - return np.array([scalar]).dtype - - -def run_op_node(input_data, op_fun, *args): - # type: (Union[NumericData, List[NumericData]], Callable, *Any) -> List[NumericData] - """Run computation on node performing `op_fun`. - - `op_fun` has to accept a node as an argument. - - This function converts passed raw input data to graph Constant Node and that form is passed - to `op_fun`. - - :param input_data: The input data for performed computation. - :param op_fun: The function handler for operation we want to carry out. - :param args: The arguments passed to operation we want to carry out. - :return: The result from computations. - """ - runtime = get_runtime() - comp_args = [] - op_fun_args = [] - comp_inputs = [] - - for idx, data in enumerate(input_data): - node = None - if np.isscalar(data): - node = ov.parameter([], name=ascii_uppercase[idx], dtype=_get_numpy_dtype(data)) - else: - node = ov.parameter(data.shape, name=ascii_uppercase[idx], dtype=data.dtype) - op_fun_args.append(node) - comp_args.append(node) - comp_inputs.append(data) - - op_fun_args.extend(args) - node = op_fun(*op_fun_args) - computation = runtime.computation(node, *comp_args) - return computation(*comp_inputs) - - -def run_op_numeric_data(input_data, op_fun, *args): - # type: (NumericData, Callable, *Any) -> List[NumericData] - """Run computation on node performing `op_fun`. - - `op_fun` has to accept a scalar or an array. - - This function passess input data AS IS. This mean that in case they're a scalar (integral, - or floating point value) or a NumPy's ndarray object they will be automatically converted - to graph's Constant Nodes. - - :param input_data: The input data for performed computation. - :param op_fun: The function handler for operation we want to carry out. - :param args: The arguments passed to operation we want to carry out. - :return: The result from computations. - """ - runtime = get_runtime() - node = op_fun(input_data, *args) - computation = runtime.computation(node) - return computation() - def count_ops_of_type(func, op_type): count = 0 diff --git a/src/bindings/python/tests/test_runtime/test_compiled_model.py b/src/bindings/python/tests/test_runtime/test_compiled_model.py index 7a0737c158a..6aecdd83a6b 100644 --- a/src/bindings/python/tests/test_runtime/test_compiled_model.py +++ b/src/bindings/python/tests/test_runtime/test_compiled_model.py @@ -7,34 +7,21 @@ import pytest import numpy as np from tests.conftest import model_path -from tests.test_utils.test_utils import generate_image -from openvino.runtime import Model, ConstOutput, Shape - -from openvino.runtime import Core, Tensor +from tests.test_utils.test_utils import get_relu_model, generate_image, generate_model_and_image, generate_relu_compiled_model +from openvino.runtime import Model, ConstOutput, Shape, Core, Tensor is_myriad = os.environ.get("TEST_DEVICE") == "MYRIAD" test_net_xml, test_net_bin = model_path(is_myriad) -def test_get_property_model_name(device): +def test_get_property(device): + model = get_relu_model([1, 3, 32, 32]) core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) + compiled_model = core.compile_model(model, device, {}) network_name = compiled_model.get_property("NETWORK_NAME") assert network_name == "test_model" -@pytest.mark.skipif(os.environ.get("TEST_DEVICE", "CPU") != "CPU", reason="Device dependent test") -def test_get_property(device): - core = Core() - if core.get_property(device, "FULL_DEVICE_NAME") == "arm_compute::NEON": - pytest.skip("Can't run on ARM plugin due-to CPU dependent test") - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - profiling_enabled = compiled_model.get_property("PERF_COUNT") - assert not profiling_enabled - - def test_get_runtime_model(device): core = Core() model = core.read_model(model=test_net_xml, weights=test_net_bin) @@ -43,14 +30,18 @@ def test_get_runtime_model(device): assert isinstance(runtime_model, Model) -def test_export_import(): +def test_export_import(device): core = Core() + + if "EXPORT_IMPORT" not in core.get_property(device, "OPTIMIZATION_CAPABILITIES"): + pytest.skip(f"{core.get_property(device, 'FULL_DEVICE_NAME')} plugin due-to export, import model API isn't implemented.") + model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, "CPU") + compiled_model = core.compile_model(model, device) user_stream = compiled_model.export_model() - new_compiled = core.import_model(user_stream, "CPU") + new_compiled = core.import_model(user_stream, device) img = generate_image() res = new_compiled.infer_new_request({"data": img}) @@ -58,18 +49,22 @@ def test_export_import(): assert np.argmax(res[new_compiled.outputs[0]]) == 9 -def test_export_import_advanced(): +def test_export_import_advanced(device): import io core = Core() + + if "EXPORT_IMPORT" not in core.get_property(device, "OPTIMIZATION_CAPABILITIES"): + pytest.skip(f"{core.get_property(device, 'FULL_DEVICE_NAME')} plugin due-to export, import model API isn't implemented.") + model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, "CPU") + compiled_model = core.compile_model(model, device) user_stream = io.BytesIO() compiled_model.export_model(user_stream) - new_compiled = core.import_model(user_stream, "CPU") + new_compiled = core.import_model(user_stream, device) img = generate_image() res = new_compiled.infer_new_request({"data": img}) @@ -77,59 +72,23 @@ def test_export_import_advanced(): assert np.argmax(res[new_compiled.outputs[0]]) == 9 -def test_get_input_i(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - net_input = compiled_model.input(0) - input_node = net_input.get_node() - name = input_node.friendly_name +@pytest.mark.parametrize("input_arguments", [[0], ["data"], []]) +def test_get_input(device, input_arguments): + compiled_model = generate_relu_compiled_model(device) + net_input = compiled_model.input(*input_arguments) assert isinstance(net_input, ConstOutput) - assert name == "data" + assert net_input.get_node().friendly_name == "data" -def test_get_input_tensor_name(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - net_input = compiled_model.input("data") - input_node = net_input.get_node() - name = input_node.friendly_name - assert isinstance(net_input, ConstOutput) - assert name == "data" - - -def test_get_input(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - net_input = compiled_model.input() - input_node = net_input.get_node() - name = input_node.friendly_name - assert isinstance(net_input, ConstOutput) - assert name == "data" - - -def test_get_output_i(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - output = compiled_model.output(0) - assert isinstance(output, ConstOutput) - - -def test_get_output(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - output = compiled_model.output() +@pytest.mark.parametrize("output_arguments", [[0], []]) +def test_get_output(device, output_arguments): + compiled_model = generate_relu_compiled_model(device) + output = compiled_model.output(*output_arguments) assert isinstance(output, ConstOutput) def test_input_set_friendly_name(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) + compiled_model = generate_relu_compiled_model(device) net_input = compiled_model.input("data") input_node = net_input.get_node() input_node.set_friendly_name("input_1") @@ -139,9 +98,7 @@ def test_input_set_friendly_name(device): def test_output_set_friendly_name(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) + compiled_model = generate_relu_compiled_model(device) output = compiled_model.output(0) output_node = output.get_node() output_node.set_friendly_name("output_1") @@ -151,200 +108,119 @@ def test_output_set_friendly_name(device): def test_outputs(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) + compiled_model = generate_relu_compiled_model(device) outputs = compiled_model.outputs assert isinstance(outputs, list) assert len(outputs) == 1 - - -def test_outputs_items(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - outputs = compiled_model.outputs assert isinstance(outputs[0], ConstOutput) def test_output_type(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) + compiled_model = generate_relu_compiled_model(device) output = compiled_model.output(0) output_type = output.get_element_type().get_type_name() assert output_type == "f32" def test_output_shape(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) + compiled_model = generate_relu_compiled_model(device) output = compiled_model.output(0) - expected_shape = Shape([1, 10]) + expected_shape = Shape([1, 3, 32, 32]) assert str(output.get_shape()) == str(expected_shape) def test_input_get_index(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) + compiled_model = generate_relu_compiled_model(device) net_input = compiled_model.input(0) - expected_idx = 0 - assert net_input.get_index() == expected_idx + assert net_input.get_index() == 0 def test_inputs(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) + compiled_model = generate_relu_compiled_model(device) inputs = compiled_model.inputs assert isinstance(inputs, list) assert len(inputs) == 1 - - -def test_inputs_items(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - inputs = compiled_model.inputs assert isinstance(inputs[0], ConstOutput) def test_inputs_get_friendly_name(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - inputs = compiled_model.inputs - input_0 = inputs[0] - node = input_0.get_node() + compiled_model = generate_relu_compiled_model(device) + node = compiled_model.inputs[0].get_node() name = node.friendly_name assert name == "data" def test_inputs_set_friendly_name(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - inputs = compiled_model.inputs - input_0 = inputs[0] - node = input_0.get_node() + compiled_model = generate_relu_compiled_model(device) + node = compiled_model.inputs[0].get_node() node.set_friendly_name("input_0") name = node.friendly_name assert name == "input_0" def test_inputs_docs(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - compiled_model = core.compile_model(model, device) - inputs = compiled_model.inputs - input_0 = inputs[0] - expected_string = "openvino.runtime.ConstOutput represents port/node output." - assert input_0.__doc__ == expected_string + compiled_model = generate_relu_compiled_model(device) + + input_0 = compiled_model.inputs[0] + assert input_0.__doc__ == "openvino.runtime.ConstOutput represents port/node output." def test_infer_new_request_numpy(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - img = generate_image() - compiled_model = core.compile_model(model, device) + compiled_model, img = generate_model_and_image(device) res = compiled_model.infer_new_request({"data": img}) - assert np.argmax(res[list(res)[0]]) == 9 + assert np.argmax(res[list(res)[0]]) == 531 def test_infer_new_request_tensor_numpy_copy(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - img = generate_image() + compiled_model, img = generate_model_and_image(device) + tensor = Tensor(img) - compiled_model = core.compile_model(model, device) res_tensor = compiled_model.infer_new_request({"data": tensor}) res_img = compiled_model.infer_new_request({"data": img}) - assert np.argmax(res_tensor[list(res_tensor)[0]]) == 9 + assert np.argmax(res_tensor[list(res_tensor)[0]]) == 531 assert np.argmax(res_tensor[list(res_tensor)[0]]) == np.argmax(res_img[list(res_img)[0]]) def test_infer_tensor_numpy_shared_memory(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - img = generate_image() + compiled_model, img = generate_model_and_image(device) + img = np.ascontiguousarray(img) tensor = Tensor(img, shared_memory=True) - compiled_model = core.compile_model(model, device) res_tensor = compiled_model.infer_new_request({"data": tensor}) res_img = compiled_model.infer_new_request({"data": img}) - assert np.argmax(res_tensor[list(res_tensor)[0]]) == 9 + assert np.argmax(res_tensor[list(res_tensor)[0]]) == 531 assert np.argmax(res_tensor[list(res_tensor)[0]]) == np.argmax(res_img[list(res_img)[0]]) def test_infer_new_request_wrong_port_name(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - img = generate_image() + compiled_model, img = generate_model_and_image(device) + tensor = Tensor(img) - compiled_model = core.compile_model(model, device) with pytest.raises(RuntimeError) as e: compiled_model.infer_new_request({"_data_": tensor}) assert "Check" in str(e.value) def test_infer_tensor_wrong_input_data(device): - core = Core() - model = core.read_model(model=test_net_xml, weights=test_net_bin) - img = generate_image() + compiled_model, img = generate_model_and_image(device) + img = np.ascontiguousarray(img) tensor = Tensor(img, shared_memory=True) - compiled_model = core.compile_model(model, device) with pytest.raises(TypeError) as e: compiled_model.infer_new_request({0.: tensor}) assert "Incompatible key type for input: 0.0" in str(e.value) -def test_infer_numpy_model_from_buffer(device): - core = Core() - with open(test_net_bin, "rb") as f: - weights = f.read() - with open(test_net_xml, "rb") as f: - xml = f.read() - model = core.read_model(model=xml, weights=weights) - img = generate_image() - compiled_model = core.compile_model(model, device) - res = compiled_model.infer_new_request({"data": img}) - assert np.argmax(res[list(res)[0]]) == 9 - - -def test_infer_tensor_model_from_buffer(device): - core = Core() - with open(test_net_bin, "rb") as f: - weights = f.read() - with open(test_net_xml, "rb") as f: - xml = f.read() - model = core.read_model(model=xml, weights=weights) - img = generate_image() - tensor = Tensor(img) - compiled_model = core.compile_model(model, device) - res = compiled_model.infer_new_request({"data": tensor}) - assert np.argmax(res[list(res)[0]]) == 9 - - def test_direct_infer(device): - core = Core() - with open(test_net_bin, "rb") as f: - weights = f.read() - with open(test_net_xml, "rb") as f: - xml = f.read() - model = core.read_model(model=xml, weights=weights) - img = generate_image() + compiled_model, img = generate_model_and_image(device) + tensor = Tensor(img) - comp_model = core.compile_model(model, device) - res = comp_model({"data": tensor}) - assert np.argmax(res[comp_model.outputs[0]]) == 9 - ref = comp_model.infer_new_request({"data": tensor}) - assert np.array_equal(ref[comp_model.outputs[0]], res[comp_model.outputs[0]]) + res = compiled_model({"data": tensor}) + assert np.argmax(res[compiled_model.outputs[0]]) == 531 + ref = compiled_model.infer_new_request({"data": tensor}) + assert np.array_equal(ref[compiled_model.outputs[0]], res[compiled_model.outputs[0]]) -@pytest.mark.template_plugin() def test_compiled_model_after_core_destroyed(device): core = Core() with open(test_net_bin, "rb") as f: diff --git a/src/bindings/python/tests/test_runtime/test_core.py b/src/bindings/python/tests/test_runtime/test_core.py index 55e0b9b1e77..0d0e0fadc65 100644 --- a/src/bindings/python/tests/test_runtime/test_core.py +++ b/src/bindings/python/tests/test_runtime/test_core.py @@ -5,10 +5,8 @@ import pytest import numpy as np import os -from sys import platform from pathlib import Path -import openvino.runtime.opset8 as ov from openvino.runtime import ( Model, Core, @@ -23,17 +21,18 @@ from openvino.runtime import ( from tests.conftest import ( model_path, model_onnx_path, - plugins_path, get_model_with_template_extension, ) from tests.test_utils.test_utils import ( generate_image, - generate_relu_model, + generate_relu_compiled_model, + get_relu_model, + generate_lib_name, + plugins_path, ) -plugins_xml, plugins_win_xml, plugins_osx_xml = plugins_path() test_net_xml, test_net_bin = model_path() test_net_onnx = model_onnx_path() @@ -41,19 +40,15 @@ test_net_onnx = model_onnx_path() def test_compact_api_xml(): img = generate_image() - model = compile_model(test_net_xml) - assert isinstance(model, CompiledModel) - results = model.infer_new_request({"data": img}) - assert np.argmax(results[list(results)[0]]) == 9 + compiled_model = compile_model(get_relu_model()) + assert isinstance(compiled_model, CompiledModel) + results = compiled_model.infer_new_request({"data": img}) + assert np.argmax(results[list(results)[0]]) == 531 def test_compact_api_xml_posix_path(): - img = generate_image() - - model = compile_model(Path(test_net_xml)) - assert isinstance(model, CompiledModel) - results = model.infer_new_request({"data": img}) - assert np.argmax(results[list(results)[0]]) == 9 + compiled_model = compile_model(Path(test_net_xml)) + assert isinstance(compiled_model, CompiledModel) def test_compact_api_wrong_path(): @@ -69,35 +64,17 @@ def test_compact_api_wrong_path(): assert "Path: 'test class' does not exist. Please provide valid model's path either as a string or pathlib.Path" in str(e.value) -def test_compact_api_onnx(): - img = generate_image() - - model = compile_model(test_net_onnx) - assert isinstance(model, CompiledModel) - results = model.infer_new_request({"data": img}) - assert np.argmax(results[list(results)[0]]) == 9 - - -def test_compact_api_onnx_posix_path(): - img = generate_image() - - model = compile_model(Path(test_net_onnx)) - assert isinstance(model, CompiledModel) - results = model.infer_new_request({"data": img}) - assert np.argmax(results[list(results)[0]]) == 9 - - -def test_core_class(): +def test_core_class(device): input_shape = [1, 3, 4, 4] - model = generate_relu_model(input_shape) + compiled_model = generate_relu_compiled_model(device, input_shape=input_shape) - request = model.create_infer_request() + request = compiled_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}) + results = request.infer({"data": input_tensor}) assert np.allclose(results[list(results)[0]], expected_output) @@ -276,33 +253,30 @@ def test_query_model(device): assert [ key for key in query_model.keys() if key not in ops_func_names ] == [], "Not all network layers present in query_model results" - assert next(iter(set(query_model.values()))) == device, "Wrong device for some layers" + assert device in next(iter(set(query_model.values()))), "Wrong device for some layers" @pytest.mark.dynamic_library() -@pytest.mark.skipif(os.environ.get("TEST_DEVICE", "CPU") != "CPU", reason="Device independent test") -def test_register_plugin(): +def test_register_plugin(device): core = Core() - core.register_plugin("openvino_intel_cpu_plugin", "BLA") + full_device_name = core.get_property(device, "FULL_DEVICE_NAME") + lib_name = generate_lib_name(device, full_device_name) + core.register_plugin(lib_name, "BLA") model = core.read_model(model=test_net_xml, weights=test_net_bin) - exec_net = core.compile_model(model, "BLA") - assert isinstance(exec_net, CompiledModel), "Cannot load the network to the registered plugin with name 'BLA'" + compiled_model = core.compile_model(model, "BLA") + assert isinstance(compiled_model, CompiledModel), "Cannot load the network to the registered plugin with name 'BLA'" @pytest.mark.dynamic_library() -@pytest.mark.skipif(os.environ.get("TEST_DEVICE", "CPU") != "CPU", reason="Device independent test") -def test_register_plugins(): +def test_register_plugins(device): core = Core() - if platform == "linux" or platform == "linux2": - core.register_plugins(plugins_xml) - elif platform == "darwin": - core.register_plugins(plugins_osx_xml) - elif platform == "win32": - core.register_plugins(plugins_win_xml) - + full_device_name = core.get_property(device, "FULL_DEVICE_NAME") + plugins_xml = plugins_path(device, full_device_name) + core.register_plugins(plugins_xml) model = core.read_model(model=test_net_xml, weights=test_net_bin) - exec_net = core.compile_model(model, "CUSTOM") - assert isinstance(exec_net, CompiledModel), ( + compiled_model = core.compile_model(model, "CUSTOM") + os.remove(plugins_xml) + assert isinstance(compiled_model, CompiledModel), ( "Cannot load the network to " "the registered plugin with name 'CUSTOM' " "registered in the XML file" @@ -336,8 +310,8 @@ def test_add_extension_template_extension(device): model.reshape(new_shapes) # compile to check objects can be destroyed # in order core -> model -> compiled - compiled = core.compile_model(model, device) - assert compiled.input().partial_shape == after_reshape + compiled_model = core.compile_model(model, device) + assert compiled_model.input().partial_shape == after_reshape def test_add_extension(): diff --git a/src/bindings/python/tests/test_runtime/test_infer_request.py b/src/bindings/python/tests/test_runtime/test_infer_request.py index 2cc0b81a88c..606fbc17895 100644 --- a/src/bindings/python/tests/test_runtime/test_infer_request.py +++ b/src/bindings/python/tests/test_runtime/test_infer_request.py @@ -16,7 +16,7 @@ from openvino.runtime import Type, PartialShape, Shape, Layout from openvino.preprocess import PrePostProcessor from tests.conftest import model_path -from tests.test_utils.test_utils import generate_image +from tests.test_utils.test_utils import generate_image, get_relu_model is_myriad = os.environ.get("TEST_DEVICE") == "MYRIAD" test_net_xml, test_net_bin = model_path(is_myriad) @@ -98,7 +98,7 @@ def test_get_profiling_info(device): assert request.latency > 0 prof_info = request.get_profiling_info() soft_max_node = next(node for node in prof_info if node.node_name == "fc_out") - assert soft_max_node.node_type == "Softmax" + assert "Softmax" in soft_max_node.node_type assert soft_max_node.status == ProfilingInfo.Status.EXECUTED assert isinstance(soft_max_node.real_time, datetime.timedelta) assert isinstance(soft_max_node.cpu_time, datetime.timedelta) @@ -107,7 +107,8 @@ def test_get_profiling_info(device): def test_tensor_setter(device): core = Core() - model = core.read_model(test_net_xml, test_net_bin) + model = get_relu_model() + compiled_1 = core.compile_model(model=model, device_name=device) compiled_2 = core.compile_model(model=model, device_name=device) compiled_3 = core.compile_model(model=model, device_name=device) @@ -124,12 +125,12 @@ def test_tensor_setter(device): res = request1.infer({0: tensor}) key = list(res)[0] res_1 = np.sort(res[key]) - t2 = request1.get_tensor("fc_out") + t2 = request1.get_output_tensor() assert np.allclose(t2.data, res[key].data, atol=1e-2, rtol=1e-2) request = compiled_2.create_infer_request() res = request.infer({"data": tensor}) - res_2 = np.sort(request.get_tensor("fc_out").data) + res_2 = np.sort(request.get_output_tensor().data) assert np.allclose(res_1, res_2, atol=1e-2, rtol=1e-2) request.set_tensor("data", tensor) @@ -204,6 +205,9 @@ def test_set_tensors(device): def test_batched_tensors(device): core = Core() + if device == "CPU": + if "Intel" not in core.get_property(device, "FULL_DEVICE_NAME"): + pytest.skip("Can't run on ARM plugin") batch = 4 one_shape = [1, 2, 2, 2] @@ -349,9 +353,8 @@ def test_infer_list_as_inputs(device): def test_infer_mixed_keys(device): core = Core() - model = core.read_model(test_net_xml, test_net_bin) - core.set_property(device, {"PERF_COUNT": "YES"}) - model = core.compile_model(model, device) + model = get_relu_model() + compiled_model = core.compile_model(model, device) img = generate_image() tensor = Tensor(img) @@ -359,9 +362,9 @@ def test_infer_mixed_keys(device): data2 = np.ones(shape=img.shape, dtype=np.float32) tensor2 = Tensor(data2) - request = model.create_infer_request() + request = compiled_model.create_infer_request() res = request.infer({0: tensor2, "data": tensor}) - assert np.argmax(res[model.output()]) == 9 + assert np.argmax(res[compiled_model.output()]) == 531 @pytest.mark.parametrize(("ov_type", "numpy_dtype"), [ @@ -470,6 +473,8 @@ def test_infer_queue(device): img = generate_image() infer_queue.set_callback(callback) + assert infer_queue.is_ready() + for i in range(jobs): infer_queue.start_async({"data": img}, i) infer_queue.wait_all() @@ -477,23 +482,6 @@ def test_infer_queue(device): assert all(job["latency"] > 0 for job in jobs_done) -def test_infer_queue_is_ready(device): - core = Core() - param = ops.parameter([10]) - model = Model(ops.relu(param), [param]) - compiled_model = core.compile_model(model, device) - infer_queue = AsyncInferQueue(compiled_model, 1) - - def callback(request, _): - time.sleep(0.001) - - infer_queue.set_callback(callback) - assert infer_queue.is_ready() - infer_queue.start_async() - assert not infer_queue.is_ready() - infer_queue.wait_all() - - def test_infer_queue_iteration(device): core = Core() param = ops.parameter([10]) @@ -578,10 +566,10 @@ def test_infer_queue_fail_in_inference(device, with_callback): jobs = 6 num_request = 4 core = Core() - data = ops.parameter([5, 2], dtype=np.float32, name="data") - indexes = ops.parameter(Shape([3, 2]), dtype=np.int32, name="indexes") - emb = ops.embedding_bag_packed_sum(data, indexes) - model = Model(emb, [data, indexes]) + data = ops.parameter([10], dtype=np.float32, name="data") + k_op = ops.parameter(Shape([]), dtype=np.int32, name="k") + emb = ops.topk(data, k_op, axis=0, mode="max", sort="value") + model = Model(emb, [data, k_op]) compiled_model = core.compile_model(model, device) infer_queue = AsyncInferQueue(compiled_model, num_request) @@ -591,15 +579,15 @@ def test_infer_queue_fail_in_inference(device, with_callback): if with_callback: infer_queue.set_callback(callback) - data_tensor = Tensor(np.arange(10).reshape((5, 2)).astype(np.float32)) - indexes_tensor = Tensor(np.array([[100, 101], [102, 103], [104, 105]], dtype=np.int32)) + data_tensor = Tensor(np.arange(10).astype(np.float32)) + k_tensor = Tensor(np.array(11, dtype=np.int32)) with pytest.raises(RuntimeError) as e: for _ in range(jobs): - infer_queue.start_async({"data": data_tensor, "indexes": indexes_tensor}) + infer_queue.start_async({"data": data_tensor, "k": k_tensor}) infer_queue.wait_all() - assert "has invalid embedding bag index:" in str(e.value) + assert "Can not clone with new dims" in str(e.value) def test_infer_queue_get_idle_handle(device): @@ -693,7 +681,7 @@ def test_results_async_infer(device): jobs = 8 num_request = 4 core = Core() - model = core.read_model(test_net_xml, test_net_bin) + model = get_relu_model() compiled_model = core.compile_model(model, device) infer_queue = AsyncInferQueue(compiled_model, num_request) jobs_done = [{"finished": False, "latency": 0} for _ in range(jobs)] diff --git a/src/bindings/python/tests/test_runtime/test_properties.py b/src/bindings/python/tests/test_runtime/test_properties.py index 0200fab4078..2a80472fa5c 100644 --- a/src/bindings/python/tests/test_runtime/test_properties.py +++ b/src/bindings/python/tests/test_runtime/test_properties.py @@ -3,13 +3,227 @@ # SPDX-License-Identifier: Apache-2.0 import pytest +import numpy as np import os -from openvino.runtime import Core, Type, OVAny -from openvino.runtime import properties +from openvino.runtime import Core, Type, OVAny, properties -def test_property_rw(): +### +# Base properties API +### +def test_properties_ro_base(): + with pytest.raises(TypeError) as e: + properties.supported_properties("something") + assert "incompatible function arguments" in str(e.value) + + +def test_properties_rw_base(): + assert properties.cache_dir() == "CACHE_DIR" + assert properties.cache_dir("./test_dir") == ("CACHE_DIR", OVAny("./test_dir")) + + with pytest.raises(TypeError) as e: + properties.cache_dir(6) + assert "incompatible function arguments" in str(e.value) + + +### +# Enum-like values +### +@pytest.mark.parametrize( + ("ov_enum", "expected_values"), + [ + ( + properties.Affinity, + ( + (properties.Affinity.NONE, "Affinity.NONE", -1), + (properties.Affinity.CORE, "Affinity.CORE", 0), + (properties.Affinity.NUMA, "Affinity.NUMA", 1), + (properties.Affinity.HYBRID_AWARE, "Affinity.HYBRID_AWARE", 2), + ), + ), + ( + properties.hint.Priority, + ( + (properties.hint.Priority.LOW, "Priority.LOW", 0), + (properties.hint.Priority.MEDIUM, "Priority.MEDIUM", 1), + (properties.hint.Priority.HIGH, "Priority.HIGH", 2), + (properties.hint.Priority.DEFAULT, "Priority.MEDIUM", 1), + ), + ), + ( + properties.hint.PerformanceMode, + ( + (properties.hint.PerformanceMode.UNDEFINED, "PerformanceMode.UNDEFINED", -1), + (properties.hint.PerformanceMode.LATENCY, "PerformanceMode.LATENCY", 1), + (properties.hint.PerformanceMode.THROUGHPUT, "PerformanceMode.THROUGHPUT", 2), + (properties.hint.PerformanceMode.CUMULATIVE_THROUGHPUT, "PerformanceMode.CUMULATIVE_THROUGHPUT", 3), + ), + ), + ( + properties.device.Type, + ( + (properties.device.Type.INTEGRATED, "Type.INTEGRATED", 0), + (properties.device.Type.DISCRETE, "Type.DISCRETE", 1), + ), + ), + ( + properties.log.Level, + ( + (properties.log.Level.NO, "Level.NO", -1), + (properties.log.Level.ERR, "Level.ERR", 0), + (properties.log.Level.WARNING, "Level.WARNING", 1), + (properties.log.Level.INFO, "Level.INFO", 2), + (properties.log.Level.DEBUG, "Level.DEBUG", 3), + (properties.log.Level.TRACE, "Level.TRACE", 4), + ), + ), + ], +) +def test_properties_enums(ov_enum, expected_values): + assert ov_enum is not None + enum_entries = iter(ov_enum.__entries.values()) + + for property_obj, property_str, property_int in expected_values: + assert property_obj == next(enum_entries)[0] + assert str(property_obj) == property_str + assert int(property_obj) == property_int + + +### +# Read-Only properties +### +@pytest.mark.parametrize( + ("ov_property_ro", "expected_value"), + [ + (properties.supported_properties, "SUPPORTED_PROPERTIES"), + (properties.available_devices, "AVAILABLE_DEVICES"), + (properties.model_name, "NETWORK_NAME"), + (properties.optimal_number_of_infer_requests, "OPTIMAL_NUMBER_OF_INFER_REQUESTS"), + (properties.range_for_streams, "RANGE_FOR_STREAMS"), + (properties.optimal_batch_size, "OPTIMAL_BATCH_SIZE"), + (properties.max_batch_size, "MAX_BATCH_SIZE"), + (properties.range_for_async_infer_requests, "RANGE_FOR_ASYNC_INFER_REQUESTS"), + (properties.device.full_name, "FULL_DEVICE_NAME"), + (properties.device.architecture, "DEVICE_ARCHITECTURE"), + (properties.device.type, "DEVICE_TYPE"), + (properties.device.gops, "DEVICE_GOPS"), + (properties.device.thermal, "DEVICE_THERMAL"), + (properties.device.capabilities, "OPTIMIZATION_CAPABILITIES"), + ], +) +def test_properties_ro(ov_property_ro, expected_value): + # Test if property is correctly registered + assert ov_property_ro() == expected_value + + +### +# Read-Write properties +### +@pytest.mark.parametrize( + ("ov_property_rw", "expected_value", "test_values"), + [ + ( + properties.enable_profiling, + "PERF_COUNT", + ( + (True, True), + (False, False), + (1, True), + (0, False), + ), + ), + ( + properties.cache_dir, + "CACHE_DIR", + (("./test_cache", "./test_cache"),), + ), + ( + properties.auto_batch_timeout, + "AUTO_BATCH_TIMEOUT", + ( + (21, 21), + (np.uint32(37), 37), + (21, np.uint32(21)), + (np.uint32(37), np.uint32(37)), + ), + ), + ( + properties.inference_num_threads, + "INFERENCE_NUM_THREADS", + ( + (-8, -8), + (8, 8), + ), + ), + ( + properties.compilation_num_threads, + "COMPILATION_NUM_THREADS", + ((44, 44),), + ), + ( + properties.affinity, + "AFFINITY", + ((properties.Affinity.NONE, properties.Affinity.NONE),), + ), + (properties.force_tbb_terminate, "FORCE_TBB_TERMINATE", ((True, True),)), + (properties.hint.inference_precision, "INFERENCE_PRECISION_HINT", ((Type.f32, Type.f32),)), + ( + properties.hint.model_priority, + "MODEL_PRIORITY", + ((properties.hint.Priority.LOW, properties.hint.Priority.LOW),), + ), + ( + properties.hint.performance_mode, + "PERFORMANCE_HINT", + ((properties.hint.PerformanceMode.UNDEFINED, properties.hint.PerformanceMode.UNDEFINED),), + ), + ( + properties.hint.num_requests, + "PERFORMANCE_HINT_NUM_REQUESTS", + ((8, 8),), + ), + ( + properties.hint.allow_auto_batching, + "ALLOW_AUTO_BATCHING", + ((True, True),), + ), + ( + properties.intel_cpu.denormals_optimization, + "CPU_DENORMALS_OPTIMIZATION", + ((True, True),), + ), + ( + properties.intel_cpu.sparse_weights_decompression_rate, + "SPARSE_WEIGHTS_DECOMPRESSION_RATE", + ( + (0.1, np.float32(0.1)), + (2.0, 2.0), + ), + ), + (properties.device.id, "DEVICE_ID", (("0", "0"),)), + ( + properties.log.level, + "LOG_LEVEL", + ((properties.log.Level.NO, properties.log.Level.NO),), + ), + ], +) +def test_properties_rw(ov_property_rw, expected_value, test_values): + # Test if property is correctly registered + assert ov_property_rw() == expected_value + + # Test if property process values correctly + for values in test_values: + property_tuple = ov_property_rw(values[0]) + assert property_tuple[0] == expected_value + assert property_tuple[1].value == values[1] + + +### +# Special cases +### +def test_properties_device_priorities(): assert properties.device.priorities() == "MULTI_DEVICE_PRIORITIES" assert properties.device.priorities("CPU,GPU") == ("MULTI_DEVICE_PRIORITIES", OVAny("CPU,GPU,")) assert properties.device.priorities("CPU", "GPU") == ("MULTI_DEVICE_PRIORITIES", OVAny("CPU,GPU,")) @@ -20,86 +234,111 @@ def test_property_rw(): assert f"Incorrect passed value: {value} , expected string values." in str(e.value) -def test_property_ro(): - assert properties.available_devices() == "AVAILABLE_DEVICES" +def test_properties_streams(): + # Test extra Num class + assert properties.streams.Num().to_integer() == -1 + assert properties.streams.Num(2).to_integer() == 2 + assert properties.streams.Num.AUTO.to_integer() == -1 + assert properties.streams.Num.NUMA.to_integer() == -2 + # Test RW property + property_tuple = properties.streams.num(properties.streams.Num.AUTO) + assert property_tuple[0] == "NUM_STREAMS" + assert property_tuple[1].value == -1 - with pytest.raises(TypeError) as e: - properties.available_devices("something") - assert "available_devices(): incompatible function arguments." in str(e.value) + property_tuple = properties.streams.num(42) + assert property_tuple[0] == "NUM_STREAMS" + assert property_tuple[1].value == 42 -def test_allow_auto_batching_property(): +def test_properties_capability(): + assert properties.device.Capability.FP32 == "FP32" + assert properties.device.Capability.BF16 == "BF16" + assert properties.device.Capability.FP16 == "FP16" + assert properties.device.Capability.INT8 == "INT8" + assert properties.device.Capability.INT16 == "INT16" + assert properties.device.Capability.BIN == "BIN" + assert properties.device.Capability.WINOGRAD == "WINOGRAD" + assert properties.device.Capability.EXPORT_IMPORT == "EXPORT_IMPORT" + + +def test_properties_hint_model(): + # Temporary imports + from tests.test_utils.test_utils import generate_add_model + + model = generate_add_model() + + assert properties.hint.model() == "MODEL_PTR" + + property_tuple = properties.hint.model(model) + assert property_tuple[0] == "MODEL_PTR" + + +def test_single_property_setting(device): core = Core() - core.set_property({"ALLOW_AUTO_BATCHING": False}) - assert core.get_property(properties.hint.allow_auto_batching()) is False - core.set_property({"ALLOW_AUTO_BATCHING": True}) - assert core.get_property(properties.hint.allow_auto_batching()) is True + if device == "CPU" and "Intel" not in core.get_property(device, "FULL_DEVICE_NAME"): + pytest.skip("This test runs only on openvino intel cpu plugin") - -@pytest.mark.skipif(os.environ.get("TEST_DEVICE", "CPU") != "CPU", - reason=f"Cannot run test on device {os.environ.get('TEST_DEVICE')}, Plugin specific test") -def test_single_property_setting(): - core = Core() - core.set_property("CPU", properties.streams.num(properties.streams.Num.AUTO)) + core.set_property(device, properties.streams.num(properties.streams.Num.AUTO)) assert properties.streams.Num.AUTO.to_integer() == -1 - assert type(core.get_property("CPU", properties.streams.num())) == int + assert type(core.get_property(device, properties.streams.num())) == int -@pytest.mark.skipif(os.environ.get("TEST_DEVICE", "CPU") != "CPU", - reason=f"Cannot run test on device {os.environ.get('TEST_DEVICE')}, Plugin specific test") -@pytest.mark.parametrize("properties_to_set", [ - # Dict from list of tuples - dict([ # noqa: C406 - properties.enable_profiling(True), - properties.cache_dir("./"), - properties.inference_num_threads(9), - properties.affinity(properties.Affinity.NONE), - properties.hint.inference_precision(Type.f32), - properties.hint.performance_mode(properties.hint.PerformanceMode.LATENCY), - properties.hint.num_requests(12), - properties.streams.num(5), - ]), - # Pure dict - { - properties.enable_profiling(): True, - properties.cache_dir(): "./", - properties.inference_num_threads(): 9, - properties.affinity(): properties.Affinity.NONE, - properties.hint.inference_precision(): Type.f32, - properties.hint.performance_mode(): properties.hint.PerformanceMode.LATENCY, - properties.hint.num_requests(): 12, - properties.streams.num(): 5, - }, - # Mixed dict - { - properties.enable_profiling(): True, - "CACHE_DIR": "./", - properties.inference_num_threads(): 9, - properties.affinity(): "NONE", - "INFERENCE_PRECISION_HINT": Type.f32, - properties.hint.performance_mode(): properties.hint.PerformanceMode.LATENCY, - properties.hint.num_requests(): 12, - "NUM_STREAMS": properties.streams.Num(5), - }, -]) -def test_properties_core(properties_to_set): +@pytest.mark.skipif(os.environ.get("TEST_DEVICE", "CPU") != "CPU", reason=f"Cannot run test on device {os.environ.get('TEST_DEVICE')}, Plugin specific test") +@pytest.mark.parametrize( + "properties_to_set", + [ + # Dict from list of tuples + dict( # noqa: C406 + [ # noqa: C406 + properties.enable_profiling(True), + properties.cache_dir("./"), + properties.inference_num_threads(9), + properties.affinity(properties.Affinity.NONE), + properties.hint.inference_precision(Type.f32), + properties.hint.performance_mode(properties.hint.PerformanceMode.LATENCY), + properties.hint.num_requests(12), + properties.streams.num(5), + ], + ), + # Pure dict + { + properties.enable_profiling(): True, + properties.cache_dir(): "./", + properties.inference_num_threads(): 9, + properties.affinity(): properties.Affinity.NONE, + properties.hint.inference_precision(): Type.f32, + properties.hint.performance_mode(): properties.hint.PerformanceMode.LATENCY, + properties.hint.num_requests(): 12, + properties.streams.num(): 5, + }, + # Mixed dict + { + properties.enable_profiling(): True, + "CACHE_DIR": "./", + properties.inference_num_threads(): 9, + properties.affinity(): "NONE", + "INFERENCE_PRECISION_HINT": Type.f32, + properties.hint.performance_mode(): properties.hint.PerformanceMode.LATENCY, + properties.hint.num_requests(): 12, + "NUM_STREAMS": properties.streams.Num(5), + }, + ], +) +def test_core_cpu_properties(properties_to_set): core = Core() - core.set_property(properties_to_set) - # RW properties without device name - assert core.get_property(properties.cache_dir()) == "./" - assert core.get_property(properties.force_tbb_terminate()) is False + if "Intel" not in core.get_property("CPU", "FULL_DEVICE_NAME"): + pytest.skip("This test runs only on openvino intel cpu plugin") + + core.set_property(properties_to_set) # RW properties assert core.get_property("CPU", properties.enable_profiling()) is True assert core.get_property("CPU", properties.cache_dir()) == "./" assert core.get_property("CPU", properties.inference_num_threads()) == 9 assert core.get_property("CPU", properties.affinity()) == properties.Affinity.NONE - assert core.get_property("CPU", properties.hint.inference_precision()) == Type.f32 - assert core.get_property("CPU", properties.hint.performance_mode()) == properties.hint.PerformanceMode.LATENCY - assert core.get_property("CPU", properties.hint.num_requests()) == 12 assert core.get_property("CPU", properties.streams.num()) == 5 # RO properties diff --git a/src/bindings/python/tests/test_transformations/test_graph_rewrite.py b/src/bindings/python/tests/test_transformations/test_graph_rewrite.py index 4603415a86e..a006823b961 100644 --- a/src/bindings/python/tests/test_transformations/test_graph_rewrite.py +++ b/src/bindings/python/tests/test_transformations/test_graph_rewrite.py @@ -4,11 +4,11 @@ from openvino.runtime import opset8 from openvino.runtime.passes import Manager, GraphRewrite, MatcherPass, WrapType, Matcher -from tests.test_transformations.utils.utils import count_ops, get_test_model, PatternReplacement +from tests.test_transformations.utils.utils import count_ops, get_relu_model, PatternReplacement def test_graph_rewrite(): - model = get_test_model() + model = get_relu_model() manager = Manager() # check that register pass returns pass instance @@ -68,7 +68,7 @@ def test_register_new_node(): manager = Manager() ins = manager.register_pass(InsertExp()) rem = manager.register_pass(RemoveExp()) - manager.run_passes(get_test_model()) + manager.run_passes(get_relu_model()) assert ins.model_changed assert rem.model_changed diff --git a/src/bindings/python/tests/test_transformations/test_matcher_pass.py b/src/bindings/python/tests/test_transformations/test_matcher_pass.py index a63e54391a5..c414e81167c 100644 --- a/src/bindings/python/tests/test_transformations/test_matcher_pass.py +++ b/src/bindings/python/tests/test_transformations/test_matcher_pass.py @@ -5,7 +5,7 @@ from openvino.runtime import opset8 from openvino.runtime.passes import Manager, Matcher, MatcherPass, WrapType from openvino.runtime.utils import replace_node -from tests.test_transformations.utils.utils import count_ops, get_test_model, PatternReplacement +from tests.test_transformations.utils.utils import count_ops, get_relu_model, PatternReplacement def test_simple_pattern_replacement(): @@ -27,7 +27,7 @@ def test_simple_pattern_replacement(): return Matcher(relu, "SimpleReplacement"), callback - model = get_test_model() + model = get_relu_model() manager = Manager() manager.register_pass(MatcherPass(*pattern_replacement())) @@ -37,7 +37,7 @@ def test_simple_pattern_replacement(): def test_matcher_pass(): - model = get_test_model() + model = get_relu_model() manager = Manager() # check that register pass returns pass instance @@ -49,7 +49,7 @@ def test_matcher_pass(): def test_matcher_pass_apply(): - model = get_test_model() + model = get_relu_model() pattern_replacement = PatternReplacement() pattern_replacement.apply(model.get_result().input_value(0).get_node()) diff --git a/src/bindings/python/tests/test_transformations/test_model_pass.py b/src/bindings/python/tests/test_transformations/test_model_pass.py index 1f25ec5d58b..144456e767f 100644 --- a/src/bindings/python/tests/test_transformations/test_model_pass.py +++ b/src/bindings/python/tests/test_transformations/test_model_pass.py @@ -3,12 +3,12 @@ # SPDX-License-Identifier: Apache-2.0 from openvino.runtime.passes import Manager -from tests.test_transformations.utils.utils import get_test_model, MyModelPass +from tests.test_transformations.utils.utils import get_relu_model, MyModelPass def test_model_pass(): manager = Manager() model_pass = manager.register_pass(MyModelPass()) - manager.run_passes(get_test_model()) + manager.run_passes(get_relu_model()) assert model_pass.model_changed diff --git a/src/bindings/python/tests/test_transformations/test_offline_api.py b/src/bindings/python/tests/test_transformations/test_offline_api.py index 462849f7820..c28fbbb92a6 100644 --- a/src/bindings/python/tests/test_transformations/test_offline_api.py +++ b/src/bindings/python/tests/test_transformations/test_offline_api.py @@ -22,7 +22,7 @@ import openvino.runtime as ov from tests.test_utils.test_utils import create_filename_for_test -def get_test_model(): +def get_relu_model(): param = ov.opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") param.get_output_tensor(0).set_names({"parameter"}) relu = ov.opset8.relu(param) @@ -96,7 +96,7 @@ def get_gru_sequence_model(): def test_moc_transformations(): - model = get_test_model() + model = get_relu_model() apply_moc_transformations(model, False) @@ -105,7 +105,7 @@ def test_moc_transformations(): def test_moc_with_smart_reshape(): - model = get_test_model() + model = get_relu_model() apply_moc_transformations(model, cf=False, smart_reshape=True) @@ -114,7 +114,7 @@ def test_moc_with_smart_reshape(): def test_pot_transformations(): - model = get_test_model() + model = get_relu_model() apply_pot_transformations(model, "GNA") @@ -123,7 +123,7 @@ def test_pot_transformations(): def test_low_latency_transformation(): - model = get_test_model() + model = get_relu_model() apply_low_latency_transformation(model, True) @@ -132,7 +132,7 @@ def test_low_latency_transformation(): def test_pruning_transformation(): - model = get_test_model() + model = get_relu_model() apply_pruning_transformation(model) @@ -141,7 +141,7 @@ def test_pruning_transformation(): def test_make_stateful_transformations(): - model = get_test_model() + model = get_relu_model() apply_make_stateful_transformation(model, {"parameter": "result"}) @@ -151,7 +151,7 @@ def test_make_stateful_transformations(): def test_fused_names_cleanup(): - model = get_test_model() + model = get_relu_model() for node in model.get_ops(): node.get_rt_info()["fused_names_0"] = "test_op_name" @@ -224,7 +224,7 @@ def test_version_default(request): # request - https://docs.pytest.org/en/7.1.x/reference/reference.html#request def test_serialize_default_bin(request): xml_path, bin_path = create_filename_for_test(request.node.name) - model = get_test_model() + model = get_relu_model() serialize(model, xml_path) assert os.path.exists(bin_path) os.remove(xml_path) diff --git a/src/bindings/python/tests/test_transformations/test_public_transformations.py b/src/bindings/python/tests/test_transformations/test_public_transformations.py index 7238845fb9a..d45faf9a041 100644 --- a/src/bindings/python/tests/test_transformations/test_public_transformations.py +++ b/src/bindings/python/tests/test_transformations/test_public_transformations.py @@ -15,7 +15,7 @@ from openvino.runtime.passes import ( LowLatency2, Serialize, ) -from tests.test_transformations.utils.utils import count_ops, get_test_model +from tests.test_transformations.utils.utils import count_ops, get_relu_model from tests.test_utils.test_utils import create_filename_for_test @@ -108,7 +108,7 @@ def test_serialize_pass(request): core = Core() xml_path, bin_path = create_filename_for_test(request.node.name) - func = get_test_model() + func = get_relu_model() manager = Manager() manager.register_pass(Serialize(xml_path, bin_path)) diff --git a/src/bindings/python/tests/test_transformations/test_replacement_api.py b/src/bindings/python/tests/test_transformations/test_replacement_api.py index b57c1a0afa6..0755c3bc770 100644 --- a/src/bindings/python/tests/test_transformations/test_replacement_api.py +++ b/src/bindings/python/tests/test_transformations/test_replacement_api.py @@ -6,7 +6,7 @@ from openvino.runtime import Model, PartialShape, opset8 from openvino.runtime.utils import replace_node, replace_output_update_name -def get_test_model(): +def get_relu_model(): # Parameter->Relu->Result param = opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") relu = opset8.relu(param.output(0)) diff --git a/src/bindings/python/tests/test_transformations/utils/utils.py b/src/bindings/python/tests/test_transformations/utils/utils.py index b07e3d64f31..46e8a7e2531 100644 --- a/src/bindings/python/tests/test_transformations/utils/utils.py +++ b/src/bindings/python/tests/test_transformations/utils/utils.py @@ -6,7 +6,7 @@ from openvino.runtime import Model, PartialShape, opset8 from openvino.runtime.passes import ModelPass, Matcher, MatcherPass, WrapType -def get_test_model(): +def get_relu_model(): # Parameter->Relu->Result param = opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") relu = opset8.relu(param.output(0)) diff --git a/src/bindings/python/tests/test_utils/test_utils.py b/src/bindings/python/tests/test_utils/test_utils.py index 7b7ecaa6c32..bc24de41b12 100644 --- a/src/bindings/python/tests/test_utils/test_utils.py +++ b/src/bindings/python/tests/test_utils/test_utils.py @@ -4,48 +4,94 @@ from typing import Tuple, Union, List +import os import sys import numpy as np +import pytest + +from pathlib import Path +from platform import processor + import openvino import openvino.runtime.opset8 as ops -import pytest -from openvino.runtime import Model, Core, Shape, Type -from openvino.runtime.op import Parameter +from openvino.runtime import Model, Core, Shape from openvino.utils import deprecated -def get_test_model(): - element_type = Type.f32 - param = Parameter(element_type, Shape([1, 3, 22, 22])) - relu = ops.relu(param) - model = Model([relu], [param], "test") - assert model is not None - return model - - def test_compare_models(): try: from openvino.test_utils import compare_models - model = get_test_model() + model = get_relu_model() status, _ = compare_models(model, model) assert status except RuntimeError: print("openvino.test_utils.compare_models is not available") # noqa: T201 +def generate_lib_name(device, full_device_name): + lib_name = "" + arch = processor() + if arch == "x86_64" or "Intel" in full_device_name or device in ["GNA", "HDDL", "MYRIAD", "VPUX"]: + lib_name = "openvino_intel_" + device.lower() + "_plugin" + elif arch != "x86_64" and device == "CPU": + lib_name = "openvino_arm_cpu_plugin" + elif device in ["HETERO", "MULTI", "AUTO"]: + lib_name = "openvino_" + device.lower() + "_plugin" + return lib_name + + +def plugins_path(device, full_device_name): + lib_name = generate_lib_name(device, full_device_name) + full_lib_name = "" + + if sys.platform == "win32": + full_lib_name = lib_name + ".dll" + else: + full_lib_name = "lib" + lib_name + ".so" + + plugin_xml = f""" + + + + + """ + + with open("plugin_path.xml", "w") as f: + f.write(plugin_xml) + + plugins_paths = os.path.join(os.getcwd(), "plugin_path.xml") + return plugins_paths + + def generate_image(shape: Tuple = (1, 3, 32, 32), dtype: Union[str, np.dtype] = "float32") -> np.array: np.random.seed(42) return np.random.rand(*shape).astype(dtype) -def generate_relu_model(input_shape: List[int]) -> openvino.runtime.ie_api.CompiledModel: - param = ops.parameter(input_shape, np.float32, name="parameter") +def get_relu_model(input_shape: List[int] = None) -> openvino.runtime.Model: + if input_shape is None: + input_shape = [1, 3, 32, 32] + param = ops.parameter(input_shape, np.float32, name="data") relu = ops.relu(param, name="relu") - model = Model([relu], [param], "test") + model = Model([relu], [param], "test_model") model.get_ordered_ops()[2].friendly_name = "friendly" + assert model is not None + return model + + +def generate_relu_compiled_model(device, input_shape: List[int] = None) -> openvino.runtime.CompiledModel: + if input_shape is None: + input_shape = [1, 3, 32, 32] + model = get_relu_model(input_shape) core = Core() - return core.compile_model(model, "CPU", {}) + return core.compile_model(model, device, {}) + + +def generate_model_and_image(device, input_shape: List[int] = None): + if input_shape is None: + input_shape = [1, 3, 32, 32] + return (generate_relu_compiled_model(device, input_shape), generate_image(input_shape)) def generate_add_model() -> openvino._pyopenvino.Model: diff --git a/src/bindings/python/tests/test_utils/utils/plugins.xml b/src/bindings/python/tests/test_utils/utils/plugins.xml deleted file mode 100644 index 1b0c122f05b..00000000000 --- a/src/bindings/python/tests/test_utils/utils/plugins.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - diff --git a/src/bindings/python/tests/test_utils/utils/plugins_apple.xml b/src/bindings/python/tests/test_utils/utils/plugins_apple.xml deleted file mode 100644 index 1b0c122f05b..00000000000 --- a/src/bindings/python/tests/test_utils/utils/plugins_apple.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - diff --git a/src/bindings/python/tests/test_utils/utils/plugins_win.xml b/src/bindings/python/tests/test_utils/utils/plugins_win.xml deleted file mode 100644 index 51f871a3a8c..00000000000 --- a/src/bindings/python/tests/test_utils/utils/plugins_win.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - diff --git a/src/bindings/python/tests_compatibility/__init__.py b/src/bindings/python/tests_compatibility/__init__.py index 3bd5872fef2..c5878b69360 100644 --- a/src/bindings/python/tests_compatibility/__init__.py +++ b/src/bindings/python/tests_compatibility/__init__.py @@ -44,8 +44,6 @@ xfail_issue_33581 = xfail_test(reason="RuntimeError: nGraph does not support the "GatherElements") xfail_issue_35923 = xfail_test(reason="RuntimeError: PReLU without weights is not supported") xfail_issue_35927 = xfail_test(reason="RuntimeError: B has zero dimension that is not allowable") -xfail_issue_36486 = xfail_test(reason="RuntimeError: HardSigmoid operation should be converted " - "to HardSigmoid_IE") xfail_issue_38084 = xfail_test(reason="RuntimeError: AssertionFailed: layer->get_output_partial_shape(i)." "is_static() nGraph operation with name: cannot be " "converted to layer with name: because output " diff --git a/src/bindings/python/tests_compatibility/test_inference_engine/test_ExecutableNetwork.py b/src/bindings/python/tests_compatibility/test_inference_engine/test_ExecutableNetwork.py index ab61b8b9155..1ae91e485f1 100644 --- a/src/bindings/python/tests_compatibility/test_inference_engine/test_ExecutableNetwork.py +++ b/src/bindings/python/tests_compatibility/test_inference_engine/test_ExecutableNetwork.py @@ -8,7 +8,7 @@ import time from openvino.inference_engine import ie_api as ie from tests_compatibility.conftest import model_path -from tests_compatibility.test_utils.test_utils import generate_image +from tests_compatibility.test_utils.test_utils import generate_image, generate_relu_model is_myriad = os.environ.get("TEST_DEVICE") == "MYRIAD" @@ -17,34 +17,15 @@ test_net_xml, test_net_bin = model_path(is_myriad) def test_infer(device): ie_core = ie.IECore() - net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device) img = generate_image() - res = exec_net.infer({'data': img}) - assert np.argmax(res['fc_out'][0]) == 9 + res = exec_net.infer({'parameter': img}) + assert np.argmax(res['relu'][0]) == 531 del exec_net del ie_core -def test_infer_net_from_buffer(device): - ie_core = ie.IECore() - with open(test_net_bin, 'rb') as f: - bin = f.read() - with open(test_net_xml, 'rb') as f: - xml = f.read() - net = ie_core.read_network(model=xml, weights=bin, init_from_buffer=True) - net2 = ie_core.read_network(model=test_net_xml, weights=test_net_bin) - exec_net = ie_core.load_network(net, device) - exec_net2 = ie_core.load_network(net2, device) - img = generate_image() - res = exec_net.infer({'data': img}) - res2 = exec_net2.infer({'data': img}) - del ie_core - del exec_net - del exec_net2 - assert np.allclose(res['fc_out'], res2['fc_out'], atol=1E-4, rtol=1E-4) - - def test_infer_wrong_input_name(device): ie_core = ie.IECore() net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) @@ -92,34 +73,34 @@ def test_access_requests(device): def test_async_infer_one_req(device): ie_core = ie.IECore() - net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=1) img = generate_image() - request_handler = exec_net.start_async(request_id=0, inputs={'data': img}) + request_handler = exec_net.start_async(request_id=0, inputs={'parameter': img}) request_handler.wait() - res = request_handler.output_blobs['fc_out'].buffer - assert np.argmax(res) == 9 + res = request_handler.output_blobs['relu'].buffer + assert np.argmax(res) == 531 del exec_net del ie_core def test_async_infer_many_req(device): ie_core = ie.IECore() - net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=5) img = generate_image() for id in range(5): - request_handler = exec_net.start_async(request_id=id, inputs={'data': img}) + request_handler = exec_net.start_async(request_id=id, inputs={'parameter': img}) request_handler.wait() - res = request_handler.output_blobs['fc_out'].buffer - assert np.argmax(res) == 9 + res = request_handler.output_blobs['relu'].buffer + assert np.argmax(res) == 531 del exec_net del ie_core def test_async_infer_many_req_get_idle(device): ie_core = ie.IECore() - net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) num_requests = 5 exec_net = ie_core.load_network(net, device, num_requests=num_requests) img = generate_image() @@ -131,20 +112,20 @@ def test_async_infer_many_req_get_idle(device): assert(status == ie.StatusCode.OK) request_id = exec_net.get_idle_request_id() assert(request_id >= 0) - request_handler = exec_net.start_async(request_id=request_id, inputs={'data': img}) + request_handler = exec_net.start_async(request_id=request_id, inputs={'parameter': img}) check_id.add(request_id) status = exec_net.wait(timeout=ie.WaitMode.RESULT_READY) assert status == ie.StatusCode.OK for id in range(num_requests): if id in check_id: - assert np.argmax(exec_net.requests[id].output_blobs['fc_out'].buffer) == 9 + assert np.argmax(exec_net.requests[id].output_blobs['relu'].buffer) == 531 del exec_net del ie_core def test_wait_before_start(device): ie_core = ie.IECore() - net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) num_requests = 5 exec_net = ie_core.load_network(net, device, num_requests=num_requests) img = generate_image() @@ -152,10 +133,10 @@ def test_wait_before_start(device): for id in range(num_requests): status = requests[id].wait() assert status == ie.StatusCode.INFER_NOT_STARTED - request_handler = exec_net.start_async(request_id=id, inputs={'data': img}) + request_handler = exec_net.start_async(request_id=id, inputs={'parameter': img}) status = requests[id].wait() assert status == ie.StatusCode.OK - assert np.argmax(request_handler.output_blobs['fc_out'].buffer) == 9 + assert np.argmax(request_handler.output_blobs['relu'].buffer) == 531 del exec_net del ie_core @@ -214,11 +195,11 @@ def test_wrong_num_requests_core(device): def test_plugin_accessible_after_deletion(device): ie_core = ie.IECore() - net = ie_core.read_network(model=test_net_xml, weights=test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device) img = generate_image() - res = exec_net.infer({'data': img}) - assert np.argmax(res['fc_out'][0]) == 9 + res = exec_net.infer({'parameter': img}) + assert np.argmax(res['relu'][0]) == 531 del exec_net del ie_core diff --git a/src/bindings/python/tests_compatibility/test_inference_engine/test_InferRequest.py b/src/bindings/python/tests_compatibility/test_inference_engine/test_InferRequest.py index 28333e40552..d466a5d9d24 100644 --- a/src/bindings/python/tests_compatibility/test_inference_engine/test_InferRequest.py +++ b/src/bindings/python/tests_compatibility/test_inference_engine/test_InferRequest.py @@ -10,7 +10,7 @@ import time from openvino.inference_engine import ie_api as ie from tests_compatibility.conftest import model_path, create_encoder -from tests_compatibility.test_utils.test_utils import generate_image +from tests_compatibility.test_utils.test_utils import generate_image, generate_relu_model import ngraph as ng from ngraph.impl import Function, Type @@ -129,13 +129,13 @@ def test_write_to_input_blobs_copy(device): def test_infer(device): ie_core = ie.IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=1) img = generate_image() request = exec_net.requests[0] - request.infer({'data': img}) - res = request.output_blobs['fc_out'].buffer - assert np.argmax(res) == 9 + request.infer({'parameter': img}) + res = request.output_blobs['relu'].buffer + assert np.argmax(res) == 531 del exec_net del ie_core del net @@ -143,14 +143,14 @@ def test_infer(device): def test_async_infer_default_timeout(device): ie_core = ie.IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=1) img = generate_image() request = exec_net.requests[0] - request.async_infer({'data': img}) + request.async_infer({'parameter': img}) request.wait() - res = request.output_blobs['fc_out'].buffer - assert np.argmax(res) == 9 + res = request.output_blobs['relu'].buffer + assert np.argmax(res) == 531 del exec_net del ie_core del net @@ -158,14 +158,14 @@ def test_async_infer_default_timeout(device): def test_async_infer_wait_finish(device): ie_core = ie.IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=1) img = generate_image() request = exec_net.requests[0] - request.async_infer({'data': img}) + request.async_infer({'parameter': img}) request.wait(ie.WaitMode.RESULT_READY) - res = request.output_blobs['fc_out'].buffer - assert np.argmax(res) == 9 + res = request.output_blobs['relu'].buffer + assert np.argmax(res) == 531 del exec_net del ie_core del net @@ -173,11 +173,11 @@ def test_async_infer_wait_finish(device): def test_async_infer_wait_time(device): ie_core = ie.IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=2) img = generate_image() request = exec_net.requests[0] - request.async_infer({'data': img}) + request.async_infer({'parameter': img}) start_time = datetime.utcnow() status = request.wait(ie.WaitMode.RESULT_READY) assert status == ie.StatusCode.OK @@ -185,7 +185,7 @@ def test_async_infer_wait_time(device): latency_ms = (time_delta.microseconds / 1000) + (time_delta.seconds * 1000) timeout = max(100, latency_ms) request = exec_net.requests[1] - request.async_infer({'data': img}) + request.async_infer({'parameter': img}) max_repeat = 10 status = ie.StatusCode.REQUEST_BUSY i = 0 @@ -193,8 +193,8 @@ def test_async_infer_wait_time(device): status = request.wait(timeout) i += 1 assert status == ie.StatusCode.OK - res = request.output_blobs['fc_out'].buffer - assert np.argmax(res) == 9 + res = request.output_blobs['relu'].buffer + assert np.argmax(res) == 531 del exec_net del ie_core del net @@ -202,14 +202,14 @@ def test_async_infer_wait_time(device): def test_async_infer_wait_status(device): ie_core = ie.IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=1) img = generate_image() request = exec_net.requests[0] - request.async_infer({'data': img}) + request.async_infer({'parameter': img}) request.wait(ie.WaitMode.RESULT_READY) - res = request.output_blobs['fc_out'].buffer - assert np.argmax(res) == 9 + res = request.output_blobs['relu'].buffer + assert np.argmax(res) == 531 status = request.wait(ie.WaitMode.STATUS_ONLY) assert status == ie.StatusCode.OK del exec_net @@ -219,16 +219,16 @@ def test_async_infer_wait_status(device): def test_async_infer_fill_inputs(device): ie_core = ie.IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=1) img = generate_image() request = exec_net.requests[0] - request.input_blobs['data'].buffer[:] = img + request.input_blobs['parameter'].buffer[:] = img request.async_infer() status_end = request.wait() assert status_end == ie.StatusCode.OK - res = request.output_blobs['fc_out'].buffer - assert np.argmax(res[0]) == 9 + res = request.output_blobs['relu'].buffer + assert np.argmax(res[0]) == 531 del exec_net del ie_core del net @@ -236,20 +236,20 @@ def test_async_infer_fill_inputs(device): def test_infer_modify_outputs(device): ie_core = ie.IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=1) img = generate_image() request = exec_net.requests[0] - outputs0 = exec_net.infer({'data': img}) + outputs0 = exec_net.infer({'parameter': img}) status_end = request.wait() assert status_end == ie.StatusCode.OK - assert np.argmax(outputs0['fc_out']) == 9 - outputs0['fc_out'][:] = np.zeros(shape=(1, 10), dtype=np.float32) + assert np.argmax(outputs0['relu']) == 531 + outputs0['relu'][:] = np.zeros(shape=(1, 3, 32, 32), dtype=np.float32) outputs1 = request.output_blobs - assert np.argmax(outputs1['fc_out'].buffer) == 9 - outputs1['fc_out'].buffer[:] = np.ones(shape=(1, 10), dtype=np.float32) + assert np.argmax(outputs1['relu'].buffer) == 531 + outputs1['relu'].buffer[:] = np.ones(shape=(1, 3, 32, 32), dtype=np.float32) outputs2 = request.output_blobs - assert np.argmax(outputs2['fc_out'].buffer) == 9 + assert np.argmax(outputs2['relu'].buffer) == 531 del exec_net del ie_core del net @@ -269,16 +269,16 @@ def test_async_infer_callback(device): callback.callback_called = 1 ie_core = ie.IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=1) img = generate_image() request = exec_net.requests[0] request.set_completion_callback(callback) - request.async_infer({'data': img}) + request.async_infer({'parameter': img}) status = request.wait() assert status == ie.StatusCode.OK - res = request.output_blobs['fc_out'].buffer - assert np.argmax(res) == 9 + res = request.output_blobs['relu'].buffer + assert np.argmax(res) == 531 assert callback.callback_called == 1 del exec_net del ie_core @@ -297,18 +297,18 @@ def test_async_infer_callback_wait_before_start(device): callback.callback_called = 1 ie_core = ie.IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net = ie_core.load_network(net, device, num_requests=1) img = generate_image() request = exec_net.requests[0] request.set_completion_callback(callback) status = request.wait() assert status == ie.StatusCode.INFER_NOT_STARTED - request.async_infer({'data': img}) + request.async_infer({'parameter': img}) status = request.wait() assert status == ie.StatusCode.OK - res = request.output_blobs['fc_out'].buffer - assert np.argmax(res) == 9 + res = request.output_blobs['relu'].buffer + assert np.argmax(res) == 531 assert callback.callback_called == 1 del exec_net del ie_core @@ -482,23 +482,23 @@ def test_getting_preprocess(device): def test_resize_algorithm_work(device): ie_core = ie.IECore() - net = ie_core.read_network(test_net_xml, test_net_bin) + net = generate_relu_model([1, 3, 32, 32]) exec_net_1 = ie_core.load_network(network=net, device_name=device, num_requests=1) img = generate_image() - res_1 = np.sort(exec_net_1.infer({"data": img})['fc_out']) + res_1 = np.sort(exec_net_1.infer({"parameter": img})['relu']) - net.input_info['data'].preprocess_info.resize_algorithm = ie.ResizeAlgorithm.RESIZE_BILINEAR + net.input_info['parameter'].preprocess_info.resize_algorithm = ie.ResizeAlgorithm.RESIZE_BILINEAR exec_net_2 = ie_core.load_network(net, device) tensor_desc = ie.TensorDesc("FP32", [1, 3, img.shape[2], img.shape[3]], "NCHW") img_blob = ie.Blob(tensor_desc, img) request = exec_net_2.requests[0] - assert request.preprocess_info["data"].resize_algorithm == ie.ResizeAlgorithm.RESIZE_BILINEAR - request.set_blob('data', img_blob) + assert request.preprocess_info["parameter"].resize_algorithm == ie.ResizeAlgorithm.RESIZE_BILINEAR + request.set_blob('parameter', img_blob) request.infer() - res_2 = np.sort(request.output_blobs['fc_out'].buffer) + res_2 = np.sort(request.output_blobs['relu'].buffer) assert np.allclose(res_1, res_2, atol=1e-2, rtol=1e-2) @@ -578,7 +578,6 @@ def test_set_blob_with_incorrect_size(device): tensor_desc = exec_net.requests[0].input_blobs["data"].tensor_desc tensor_desc.dims = [tensor_desc.dims[0]*2, 4, 20, 20] blob = ie.Blob(tensor_desc) - print(exec_net.requests[0].output_blobs) with pytest.raises(RuntimeError) as e: exec_net.requests[0].set_blob("data", blob) assert f"Input blob size is not equal network input size" in str(e.value) diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_adaptive_pool.py b/src/bindings/python/tests_compatibility/test_ngraph/test_adaptive_pool.py index 7c100ece796..895b64d0ca9 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_adaptive_pool.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_adaptive_pool.py @@ -3,64 +3,28 @@ import ngraph as ng import numpy as np -from tests_compatibility.runtime import get_runtime +from ngraph.impl import Type def test_adaptive_avg_pool(): - runtime = get_runtime() - input = np.reshape([0.0, 4, 1, 3, -2, -5, -2, - -2, 1, -3, 1, -3, -4, 0, - -2, 1, -1, -2, 3, -1, -3, - - -1, -2, 3, 4, -3, -4, 1, - 2, 0, -4, -5, -2, -2, -3, - 2, 3, 1, -5, 2, -4, -2], (2, 3, 7)) - input_tensor = ng.constant(input) + input_parameter = ng.parameter((2, 3, 7), name="input_data", dtype=np.float32) output_shape = ng.constant(np.array([3], dtype=np.int32)) - adaptive_pool_node = ng.adaptive_avg_pool(input_tensor, output_shape) - computation = runtime.computation(adaptive_pool_node) - adaptive_pool_results = computation() - expected_results = np.reshape([1.66666663, 0.66666669, -3., - -1.33333337, -1.66666663, -2.33333325, - -0.66666669, 0., -0.33333334, - - 0., 1.33333337, -2., - -0.66666669, -3.66666675, -2.33333325, - 2., -0.66666669, -1.33333337], (2, 3, 3)) - - assert np.allclose(adaptive_pool_results, expected_results) + adaptive_pool_node = ng.adaptive_avg_pool(input_parameter, output_shape) + assert adaptive_pool_node.get_type_name() == "AdaptiveAvgPool" + assert adaptive_pool_node.get_output_size() == 1 + assert adaptive_pool_node.get_output_element_type(0) == Type.f32 + assert list(adaptive_pool_node.get_output_shape(0)) == [2, 3, 3] def test_adaptive_max_pool(): - runtime = get_runtime() - input = np.reshape([0, 4, 1, 3, -2, -5, -2, - -2, 1, -3, 1, -3, -4, 0, - -2, 1, -1, -2, 3, -1, -3, - - -1, -2, 3, 4, -3, -4, 1, - 2, 0, -4, -5, -2, -2, -3, - 2, 3, 1, -5, 2, -4, -2], (2, 3, 7)) - input_tensor = ng.constant(input) + input_parameter = ng.parameter((2, 3, 7), name="input_data", dtype=np.float32) output_shape = ng.constant(np.array([3], dtype=np.int32)) - adaptive_pool_node = ng.adaptive_max_pool(input_tensor, output_shape) - computation = runtime.computation(adaptive_pool_node) - adaptive_pool_results = computation() - expected_results = np.reshape([4, 3, -2, - 1, 1, 0, - 1, 3, 3, - - 3, 4, 1, - 2, -2, -2, - 3, 2, 2], (2, 3, 3)) - - expected_indices = np.reshape([1, 3, 4, - 1, 3, 6, - 1, 4, 4, - - 2, 3, 6, - 0, 4, 4, - 1, 4, 4], (2, 3, 3)) - - assert np.allclose(adaptive_pool_results, [expected_results, expected_indices]) + adaptive_pool_node = ng.adaptive_max_pool(input_parameter, output_shape) + assert adaptive_pool_node.get_type_name() == "AdaptiveMaxPool" + assert adaptive_pool_node.get_output_size() == 2 + assert adaptive_pool_node.get_output_element_type(0) == Type.f32 + assert adaptive_pool_node.get_output_element_type(1) == Type.i64 + assert list(adaptive_pool_node.get_output_shape(0)) == [2, 3, 3] + assert list(adaptive_pool_node.get_output_shape(1)) == [2, 3, 3] diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_basic.py b/src/bindings/python/tests_compatibility/test_ngraph/test_basic.py index 6d269c6753c..8841e2a066f 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_basic.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_basic.py @@ -1,19 +1,15 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 -import json - import numpy as np import pytest from _pyngraph import VariantInt, VariantString import ngraph as ng -from ngraph.exceptions import UserInputError from ngraph.impl import Function, PartialShape, Shape, Type from ngraph.impl.op import Parameter -from tests_compatibility.runtime import get_runtime -from tests_compatibility.test_ngraph.util import run_op_node +from ngraph.utils.types import get_element_type def test_ngraph_function_api(): @@ -56,73 +52,48 @@ def test_ngraph_function_api(): ], ) def test_simple_computation_on_ndarrays(dtype): - runtime = get_runtime() - shape = [2, 2] parameter_a = ng.parameter(shape, dtype=dtype, name="A") parameter_b = ng.parameter(shape, dtype=dtype, name="B") parameter_c = ng.parameter(shape, dtype=dtype, name="C") model = (parameter_a + parameter_b) * parameter_c - computation = runtime.computation(model, parameter_a, parameter_b, parameter_c) - - value_a = np.array([[1, 2], [3, 4]], dtype=dtype) - value_b = np.array([[5, 6], [7, 8]], dtype=dtype) - value_c = np.array([[2, 3], [4, 5]], dtype=dtype) - result = computation(value_a, value_b, value_c) - assert np.allclose(result, np.array([[12, 24], [40, 60]], dtype=dtype)) - - value_a = np.array([[9, 10], [11, 12]], dtype=dtype) - value_b = np.array([[13, 14], [15, 16]], dtype=dtype) - value_c = np.array([[5, 4], [3, 2]], dtype=dtype) - result = computation(value_a, value_b, value_c) - assert np.allclose(result, np.array([[110, 96], [78, 56]], dtype=dtype)) - - -def test_serialization(): - dtype = np.float32 - shape = [2, 2] - parameter_a = ng.parameter(shape, dtype=dtype, name="A") - parameter_b = ng.parameter(shape, dtype=dtype, name="B") - parameter_c = ng.parameter(shape, dtype=dtype, name="C") - model = (parameter_a + parameter_b) * parameter_c - - runtime = get_runtime() - computation = runtime.computation(model, parameter_a, parameter_b, parameter_c) - try: - serialized = computation.serialize(2) - serial_json = json.loads(serialized) - - assert serial_json[0]["name"] != "" - assert 10 == len(serial_json[0]["ops"]) - except Exception: - pass + assert model.get_type_name() == "Multiply" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == get_element_type(dtype) + assert list(model.get_output_shape(0)) == [2, 2] def test_broadcast_1(): input_data = np.array([1, 2, 3], dtype=np.int32) new_shape = [3, 3] - expected = [[1, 2, 3], [1, 2, 3], [1, 2, 3]] - result = run_op_node([input_data], ng.broadcast, new_shape) - assert np.allclose(result, expected) + node = ng.broadcast(input_data, new_shape) + assert node.get_type_name() == "Broadcast" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.i32 + assert list(node.get_output_shape(0)) == [3, 3] def test_broadcast_2(): input_data = np.arange(4, dtype=np.int32) new_shape = [3, 4, 2, 4] - expected = np.broadcast_to(input_data, new_shape) - result = run_op_node([input_data], ng.broadcast, new_shape) - assert np.allclose(result, expected) + node = ng.broadcast(input_data, new_shape) + assert node.get_type_name() == "Broadcast" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.i32 + assert list(node.get_output_shape(0)) == [3, 4, 2, 4] def test_broadcast_3(): input_data = np.array([1, 2, 3], dtype=np.int32) new_shape = [3, 3] axis_mapping = [0] - expected = [[1, 1, 1], [2, 2, 2], [3, 3, 3]] - result = run_op_node([input_data], ng.broadcast, new_shape, axis_mapping, "EXPLICIT") - assert np.allclose(result, expected) + node = ng.broadcast(input_data, new_shape, axis_mapping, "EXPLICIT") + assert node.get_type_name() == "Broadcast" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.i32 + assert list(node.get_output_shape(0)) == [3, 3] @pytest.mark.parametrize( @@ -130,10 +101,11 @@ def test_broadcast_3(): [(bool, np.zeros((2, 2), dtype=np.int32)), ("boolean", np.zeros((2, 2), dtype=np.int32))], ) def test_convert_to_bool(destination_type, input_data): - expected = np.array(input_data, dtype=bool) - result = run_op_node([input_data], ng.convert, destination_type) - assert np.allclose(result, expected) - assert np.array(result).dtype == bool + node = ng.convert(input_data, destination_type) + assert node.get_type_name() == "Convert" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.boolean + assert list(node.get_output_shape(0)) == [2, 2] @pytest.mark.parametrize( @@ -148,10 +120,11 @@ def test_convert_to_bool(destination_type, input_data): def test_convert_to_float(destination_type, rand_range, in_dtype, expected_type): np.random.seed(133391) input_data = np.random.randint(*rand_range, size=(2, 2), dtype=in_dtype) - expected = np.array(input_data, dtype=expected_type) - result = run_op_node([input_data], ng.convert, destination_type) - assert np.allclose(result, expected) - assert np.array(result).dtype == expected_type + node = ng.convert(input_data, destination_type) + assert node.get_type_name() == "Convert" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(expected_type) + assert list(node.get_output_shape(0)) == [2, 2] @pytest.mark.parametrize( @@ -170,10 +143,11 @@ def test_convert_to_float(destination_type, rand_range, in_dtype, expected_type) def test_convert_to_int(destination_type, expected_type): np.random.seed(133391) input_data = (np.ceil(-8 + np.random.rand(2, 3, 4) * 16)).astype(np.float32) - expected = np.array(input_data, dtype=expected_type) - result = run_op_node([input_data], ng.convert, destination_type) - assert np.allclose(result, expected) - assert np.array(result).dtype == expected_type + node = ng.convert(input_data, destination_type) + assert node.get_type_name() == "Convert" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(expected_type) + assert list(node.get_output_shape(0)) == [2, 3, 4] @pytest.mark.parametrize( @@ -192,23 +166,11 @@ def test_convert_to_int(destination_type, expected_type): def test_convert_to_uint(destination_type, expected_type): np.random.seed(133391) input_data = np.ceil(np.random.rand(2, 3, 4) * 16).astype(np.float32) - expected = np.array(input_data, dtype=expected_type) - result = run_op_node([input_data], ng.convert, destination_type) - assert np.allclose(result, expected) - assert np.array(result).dtype == expected_type - - -def test_bad_data_shape(): - A = ng.parameter(shape=[2, 2], name="A", dtype=np.float32) - B = ng.parameter(shape=[2, 2], name="B") - model = A + B - runtime = get_runtime() - computation = runtime.computation(model, A, B) - - value_a = np.array([[1, 2]], dtype=np.float32) - value_b = np.array([[5, 6], [7, 8]], dtype=np.float32) - with pytest.raises(UserInputError): - computation(value_a, value_b) + node = ng.convert(input_data, destination_type) + assert node.get_type_name() == "Convert" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(expected_type) + assert list(node.get_output_shape(0)) == [2, 3, 4] def test_constant_get_data_bool(): @@ -254,47 +216,52 @@ def test_constant_get_data_unsigned_integer(data_type): def test_set_argument(): - runtime = get_runtime() - data1 = np.array([1, 2, 3]) data2 = np.array([4, 5, 6]) data3 = np.array([7, 8, 9]) node1 = ng.constant(data1, dtype=np.float32) node2 = ng.constant(data2, dtype=np.float32) - node3 = ng.constant(data3, dtype=np.float32) + node3 = ng.constant(data3, dtype=np.float64) + node4 = ng.constant(data3, dtype=np.float64) node_add = ng.add(node1, node2) # Original arguments - computation = runtime.computation(node_add) - output = computation() - assert np.allclose(data1 + data2, output) - - # Arguments changed by set_argument - node_add.set_argument(1, node3.output(0)) - output = computation() - assert np.allclose(data1 + data3, output) + node_inputs = node_add.inputs() + assert node_inputs[0].get_element_type() == Type.f32 + assert node_inputs[1].get_element_type() == Type.f32 # Arguments changed by set_argument node_add.set_argument(0, node3.output(0)) - output = computation() - assert np.allclose(data3 + data3, output) + node_add.set_argument(1, node4.output(0)) + node_inputs = node_add.inputs() + assert node_inputs[0].get_element_type() == Type.f64 + assert node_inputs[1].get_element_type() == Type.f64 + + # Arguments changed by set_argument + node_add.set_argument(0, node1.output(0)) + node_add.set_argument(1, node2.output(0)) + assert node_inputs[0].get_element_type() == Type.f32 + assert node_inputs[1].get_element_type() == Type.f32 # Arguments changed by set_argument(OutputVector) - node_add.set_arguments([node2.output(0), node3.output(0)]) - output = computation() - assert np.allclose(data2 + data3, output) + node_add.set_arguments([node3.output(0), node4.output(0)]) + assert node_inputs[0].get_element_type() == Type.f64 + assert node_inputs[1].get_element_type() == Type.f64 # Arguments changed by set_arguments(NodeVector) node_add.set_arguments([node1, node2]) - output = computation() - assert np.allclose(data1 + data2, output) + assert node_inputs[0].get_element_type() == Type.f32 + assert node_inputs[1].get_element_type() == Type.f32 def test_result(): - node = np.array([[11, 10], [1, 8], [3, 4]]) - result = run_op_node([node], ng.result) - assert np.allclose(result, node) + input_data = np.array([[11, 10], [1, 8], [3, 4]], dtype=np.float32) + node = ng.result(input_data) + assert node.get_type_name() == "Result" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.f32 + assert list(node.get_output_shape(0)) == [3, 2] def test_node_friendly_name(): @@ -436,11 +403,10 @@ def test_mutiple_outputs(): split_first_output = split.output(0) relu = ng.relu(split_first_output) - runtime = get_runtime() - computation = runtime.computation(relu, test_param) - output = computation(input_data) - - assert np.equal(output, expected_output).all() + assert relu.get_type_name() == "Relu" + assert relu.get_output_size() == 1 + assert relu.get_output_element_type(0) == Type.f32 + assert list(relu.get_output_shape(0)) == [4, 2] def test_sink_function_ctor(): diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_convolution.py b/src/bindings/python/tests_compatibility/test_ngraph/test_convolution.py index 4ae37af2842..4caecddd200 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_convolution.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_convolution.py @@ -2,146 +2,37 @@ # SPDX-License-Identifier: Apache-2.0 import numpy as np +import pytest import ngraph as ng -from tests_compatibility.runtime import get_runtime -from tests_compatibility.test_ngraph.test_ops import convolution2d -from tests_compatibility.test_ngraph.util import run_op_node +from ngraph.impl import Type -def test_convolution_2d(): +@pytest.mark.parametrize(("strides", "pads_begin", "pads_end", "dilations", "expected_shape"), [ + (np.array([1, 1]), np.array([1, 1]), np.array([1, 1]), np.array([1, 1]), [1, 1, 9, 9]), + (np.array([1, 1]), np.array([0, 0]), np.array([0, 0]), np.array([1, 1]), [1, 1, 7, 7]), + (np.array([2, 2]), np.array([0, 0]), np.array([0, 0]), np.array([1, 1]), [1, 1, 4, 4]), + (np.array([1, 1]), np.array([0, 0]), np.array([0, 0]), np.array([2, 2]), [1, 1, 5, 5]), +]) +def test_convolution_2d(strides, pads_begin, pads_end, dilations, expected_shape): # input_x should have shape N(batch) x C x H x W - input_x = np.array( - [ - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0], - ], - dtype=np.float32, - ).reshape(1, 1, 9, 9) + input_x = ng.parameter((1, 1, 9, 9), name="input_data", dtype=np.float32) # filter weights should have shape M x C x kH x kW input_filter = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]], dtype=np.float32).reshape( 1, 1, 3, 3 ) - strides = np.array([1, 1]) - pads_begin = np.array([1, 1]) - pads_end = np.array([1, 1]) - dilations = np.array([1, 1]) + node = ng.convolution(input_x, input_filter, strides, pads_begin, pads_end, dilations) - # convolution with padding=1 should produce 9 x 9 output: - result = run_op_node([input_x, input_filter], ng.convolution, strides, pads_begin, pads_end, dilations) - - assert np.allclose( - result, - np.array( - [ - [ - [ - [0.0, -15.0, -15.0, 15.0, 15.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -20.0, -20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0], - [0.0, -15.0, -15.0, 15.0, 15.0, 0.0, 0.0, 0.0, 0.0], - ] - ] - ], - dtype=np.float32, - ), - ) - - # convolution with padding=0 should produce 7 x 7 output: - strides = np.array([1, 1]) - pads_begin = np.array([0, 0]) - pads_end = np.array([0, 0]) - dilations = np.array([1, 1]) - result = run_op_node([input_x, input_filter], ng.convolution, strides, pads_begin, pads_end, dilations) - assert np.allclose( - result, - np.array( - [ - [ - [ - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - ] - ] - ], - dtype=np.float32, - ), - ) - - strides = np.array([2, 2]) - pads_begin = np.array([0, 0]) - pads_end = np.array([0, 0]) - dilations = np.array([1, 1]) - - # convolution with strides=2 should produce 4 x 4 output: - result = run_op_node([input_x, input_filter], ng.convolution, strides, pads_begin, pads_end, dilations) - - assert np.allclose( - result, - np.array( - [ - [ - [ - [-20.0, 20.0, 0.0, 0.0], - [-20.0, 20.0, 0.0, 0.0], - [-20.0, 20.0, 0.0, 0.0], - [-20.0, 20.0, 0.0, 0.0], - ] - ] - ], - dtype=np.float32, - ), - ) - - strides = np.array([1, 1]) - pads_begin = np.array([0, 0]) - pads_end = np.array([0, 0]) - dilations = np.array([2, 2]) - - # convolution with dilation=2 should produce 5 x 5 output: - result = run_op_node([input_x, input_filter], ng.convolution, strides, pads_begin, pads_end, dilations) - assert np.allclose( - result, - np.array( - [ - [ - [ - [0, 0, 20, 20, 0], - [0, 0, 20, 20, 0], - [0, 0, 20, 20, 0], - [0, 0, 20, 20, 0], - [0, 0, 20, 20, 0], - ] - ] - ], - dtype=np.float32, - ), - ) + assert node.get_type_name() == "Convolution" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 def test_convolution_backprop_data(): - runtime = get_runtime() - output_spatial_shape = [9, 9] filter_shape = [1, 1, 3, 3] data_shape = [1, 1, 7, 7] @@ -152,51 +43,10 @@ def test_convolution_backprop_data(): output_shape_node = ng.constant(np.array(output_spatial_shape, dtype=np.int64)) deconvolution = ng.convolution_backprop_data(data_node, filter_node, strides, output_shape_node) - - input_data = np.array( - [ - [ - [ - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - [-20, -20, 20, 20, 0, 0, 0], - ] - ] - ], - dtype=np.float32, - ) - - filter_data = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]], dtype=np.float32).reshape( - 1, 1, 3, 3 - ) - - model = runtime.computation(deconvolution, data_node, filter_node) - result = model(input_data, filter_data) - assert np.allclose( - result, - np.array( - [ - [ - [ - [-20.0, -20.0, 40.0, 40.0, -20.0, -20.0, 0.0, 0.0, 0.0], - [-60.0, -60.0, 120.0, 120.0, -60.0, -60.0, 0.0, 0.0, 0.0], - [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], - [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], - [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], - [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], - [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], - [-60.0, -60.0, 120.0, 120.0, -60.0, -60.0, 0.0, 0.0, 0.0], - [-20.0, -20.0, 40.0, 40.0, -20.0, -20.0, 0.0, 0.0, 0.0], - ] - ] - ], - dtype=np.float32, - ), - ) + assert deconvolution.get_type_name() == "ConvolutionBackpropData" + assert deconvolution.get_output_size() == 1 + assert list(deconvolution.get_output_shape(0)) == [1, 1, 9, 9] + assert deconvolution.get_output_element_type(0) == Type.f32 def test_convolution_v1(): @@ -212,8 +62,9 @@ def test_convolution_v1(): pads_end = np.array([0, 0]) dilations = np.array([1, 1]) - result = run_op_node([input_tensor, filters], ng.convolution, strides, pads_begin, pads_end, dilations) + node = ng.convolution(input_tensor, filters, strides, pads_begin, pads_end, dilations) - expected = convolution2d(input_tensor[0, 0], filters[0, 0]).reshape(1, 1, 14, 14) - - assert np.allclose(result, expected) + assert node.get_type_name() == "Convolution" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [1, 1, 14, 14] + assert node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_create_op.py b/src/bindings/python/tests_compatibility/test_ngraph/test_create_op.py index a24b91de812..31c5037c36e 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_create_op.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_create_op.py @@ -259,7 +259,7 @@ def test_deformable_psroi_pooling(dtype): ([2, 3, 5, 6], [7, 4], [7], 2, 2, 1, 1.0, "avg", "asymmetric", [7, 3, 2, 2]), ([10, 3, 5, 5], [7, 4], [7], 3, 4, 1, 1.0, "avg", "half_pixel_for_nn", [7, 3, 3, 4]), ([10, 3, 5, 5], [3, 4], [3], 3, 4, 1, 1.0, "avg", "half_pixel", [3, 3, 3, 4]), - ([10, 3, 5, 5], [3, 4], [3], 3, 4, 1, np.float(1), "avg", "half_pixel", [3, 3, 3, 4]), + ([10, 3, 5, 5], [3, 4], [3], 3, 4, 1, np.float32(1), "avg", "half_pixel", [3, 3, 3, 4]), ], ) def test_roi_align(data_shape, rois, batch_indices, pooled_h, pooled_w, sampling_ratio, spatial_scale, mode, aligned_mode, expected_shape): @@ -1882,11 +1882,11 @@ def test_multiclass_nms(): 0.0, -0.1, 1.0, 0.9, 0.0, 10.0, 1.0, 11.0, 0.0, 10.1, 1.0, 11.1, 0.0, 100.0, 1.0, 101.0], dtype="float32") boxes_data = boxes_data.reshape([1, 6, 4]) - box = ng.constant(boxes_data, dtype=np.float) + box = ng.constant(boxes_data, dtype=np.float32) scores_data = np.array([0.9, 0.75, 0.6, 0.95, 0.5, 0.3, 0.95, 0.75, 0.6, 0.80, 0.5, 0.3], dtype="float32") scores_data = scores_data.reshape([1, 2, 6]) - score = ng.constant(scores_data, dtype=np.float) + score = ng.constant(scores_data, dtype=np.float32) nms_node = ng.multiclass_nms(box, score, None, output_type="i32", nms_top_k=3, iou_threshold=0.5, score_threshold=0.0, sort_result_type="classid", @@ -1907,13 +1907,13 @@ def test_multiclass_nms(): [9.66, 3.36, 18.57, 13.26]], [[6.50, 7.00, 13.33, 17.63], [0.73, 5.34, 19.97, 19.97]]]).astype("float32") - box = ng.constant(boxes_data, dtype=np.float) + box = ng.constant(boxes_data, dtype=np.float32) scores_data = np.array([[0.34, 0.66], [0.45, 0.61], [0.39, 0.59]]).astype("float32") - score = ng.constant(scores_data, dtype=np.float) + score = ng.constant(scores_data, dtype=np.float32) rois_num_data = np.array([3]).astype("int32") - roisnum = ng.constant(rois_num_data, dtype=np.int) + roisnum = ng.constant(rois_num_data, dtype=np.int32) nms_node = ng.multiclass_nms(box, score, roisnum, output_type="i32", nms_top_k=3, iou_threshold=0.5, score_threshold=0.0, sort_result_type="classid", nms_eta=1.0) @@ -1933,11 +1933,11 @@ def test_matrix_nms(): 0.0, -0.1, 1.0, 0.9, 0.0, 10.0, 1.0, 11.0, 0.0, 10.1, 1.0, 11.1, 0.0, 100.0, 1.0, 101.0], dtype="float32") boxes_data = boxes_data.reshape([1, 6, 4]) - box = ng.constant(boxes_data, dtype=np.float) + box = ng.constant(boxes_data, dtype=np.float32) scores_data = np.array([0.9, 0.75, 0.6, 0.95, 0.5, 0.3, 0.95, 0.75, 0.6, 0.80, 0.5, 0.3], dtype="float32") scores_data = scores_data.reshape([1, 2, 6]) - score = ng.constant(scores_data, dtype=np.float) + score = ng.constant(scores_data, dtype=np.float32) nms_node = ng.matrix_nms(box, score, output_type="i32", nms_top_k=3, score_threshold=0.0, sort_result_type="score", background_class=0, @@ -2268,7 +2268,7 @@ def test_interpolate_opset10(dtype, expected_shape, shape_calculation_mode): def test_is_finite_opset10(): input_shape = [1, 2, 3, 4] - input_node = ng.parameter(input_shape, np.float, name="InputData") + input_node = ng.parameter(input_shape, np.float32, name="InputData") node = ng_opset10.is_finite(input_node) assert node.get_type_name() == "IsFinite" @@ -2278,7 +2278,7 @@ def test_is_finite_opset10(): def test_is_inf_opset10_default(): input_shape = [2, 2, 2, 2] - input_node = ng.parameter(input_shape, dtype=np.float, name="InputData") + input_node = ng.parameter(input_shape, dtype=np.float32, name="InputData") node = ng_opset10.is_inf(input_node) assert node.get_type_name() == "IsInf" @@ -2292,7 +2292,7 @@ def test_is_inf_opset10_default(): def test_is_inf_opset10_custom_attribute(): input_shape = [2, 2, 2] - input_node = ng.parameter(input_shape, dtype=np.float, name="InputData") + input_node = ng.parameter(input_shape, dtype=np.float32, name="InputData") attributes = { "detect_positive": False, } @@ -2309,7 +2309,7 @@ def test_is_inf_opset10_custom_attribute(): def test_is_inf_opset10_custom_all_attributes(): input_shape = [2, 2, 2] - input_node = ng.parameter(input_shape, dtype=np.float, name="InputData") + input_node = ng.parameter(input_shape, dtype=np.float32, name="InputData") attributes = { "detect_negative": False, "detect_positive": True, @@ -2327,7 +2327,7 @@ def test_is_inf_opset10_custom_all_attributes(): def test_is_nan_opset10(): input_shape = [1, 2, 3, 4] - input_node = ng.parameter(input_shape, np.float, name="InputData") + input_node = ng.parameter(input_shape, np.float32, name="InputData") node = ng_opset10.is_nan(input_node) assert node.get_type_name() == "IsNaN" @@ -2338,7 +2338,7 @@ def test_is_nan_opset10(): def test_unique_opset10(): input_shape = [1, 2, 3, 4] - input_node = ng.parameter(input_shape, np.float, name="input_data") + input_node = ng.parameter(input_shape, np.float32, name="input_data") axis = ng.constant([1], np.int32, [1]) node = ng_opset10.unique(input_node, axis, False, "i32") diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_data_movement.py b/src/bindings/python/tests_compatibility/test_ngraph/test_data_movement.py index a938cf8710a..d8892e7cfc2 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_data_movement.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_data_movement.py @@ -5,64 +5,10 @@ import numpy as np import ngraph as ng from ngraph.impl import Type, Shape -from tests_compatibility.runtime import get_runtime -from tests_compatibility.test_ngraph.util import run_op_node def test_reverse_sequence(): - input_data = np.array( - [ - 0, - 0, - 3, - 0, - 6, - 0, - 9, - 0, - 1, - 0, - 4, - 0, - 7, - 0, - 10, - 0, - 2, - 0, - 5, - 0, - 8, - 0, - 11, - 0, - 12, - 0, - 15, - 0, - 18, - 0, - 21, - 0, - 13, - 0, - 16, - 0, - 19, - 0, - 22, - 0, - 14, - 0, - 17, - 0, - 20, - 0, - 23, - 0, - ], - dtype=np.int32, - ).reshape([2, 3, 4, 2]) + input_data = ng.parameter((2, 3, 4, 2), name="input_data", dtype=np.int32) seq_lengths = np.array([1, 2, 1, 2], dtype=np.int32) batch_axis = 2 sequence_axis = 1 @@ -71,63 +17,10 @@ def test_reverse_sequence(): seq_lengths_param = ng.parameter(seq_lengths.shape, name="sequence lengths", dtype=np.int32) model = ng.reverse_sequence(input_param, seq_lengths_param, batch_axis, sequence_axis) - runtime = get_runtime() - computation = runtime.computation(model, input_param, seq_lengths_param) - result = computation(input_data, seq_lengths) - - expected = np.array( - [ - 0, - 0, - 4, - 0, - 6, - 0, - 10, - 0, - 1, - 0, - 3, - 0, - 7, - 0, - 9, - 0, - 2, - 0, - 5, - 0, - 8, - 0, - 11, - 0, - 12, - 0, - 16, - 0, - 18, - 0, - 22, - 0, - 13, - 0, - 15, - 0, - 19, - 0, - 21, - 0, - 14, - 0, - 17, - 0, - 20, - 0, - 23, - 0, - ], - ).reshape([1, 2, 3, 4, 2]) - assert np.allclose(result, expected) + assert model.get_type_name() == "ReverseSequence" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 3, 4, 2] + assert model.get_output_element_type(0) == Type.i32 def test_pad_edge(): @@ -138,20 +31,10 @@ def test_pad_edge(): input_param = ng.parameter(input_data.shape, name="input", dtype=np.int32) model = ng.pad(input_param, pads_begin, pads_end, "edge") - runtime = get_runtime() - computation = runtime.computation(model, input_param) - result = computation(input_data) - - expected = np.array( - [ - [1, 1, 2, 3, 4, 4, 4, 4], - [5, 5, 6, 7, 8, 8, 8, 8], - [9, 9, 10, 11, 12, 12, 12, 12], - [9, 9, 10, 11, 12, 12, 12, 12], - [9, 9, 10, 11, 12, 12, 12, 12], - ] - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "Pad" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [5, 8] + assert model.get_output_element_type(0) == Type.i32 def test_pad_constant(): @@ -162,30 +45,22 @@ def test_pad_constant(): input_param = ng.parameter(input_data.shape, name="input", dtype=np.int32) model = ng.pad(input_param, pads_begin, pads_end, "constant", arg_pad_value=np.array(100, dtype=np.int32)) - runtime = get_runtime() - computation = runtime.computation(model, input_param) - result = computation(input_data) - - expected = np.array( - [ - [100, 1, 2, 3, 4, 100, 100, 100], - [100, 5, 6, 7, 8, 100, 100, 100], - [100, 9, 10, 11, 12, 100, 100, 100], - [100, 100, 100, 100, 100, 100, 100, 100], - [100, 100, 100, 100, 100, 100, 100, 100], - ] - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "Pad" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [5, 8] + assert model.get_output_element_type(0) == Type.i32 def test_select(): cond = np.array([[False, False], [True, False], [True, True]]) then_node = np.array([[-1, 0], [1, 2], [3, 4]], dtype=np.int32) else_node = np.array([[11, 10], [9, 8], [7, 6]], dtype=np.int32) - excepted = np.array([[11, 10], [1, 8], [3, 4]], dtype=np.int32) - result = run_op_node([cond, then_node, else_node], ng.select) - assert np.allclose(result, excepted) + node = ng.select(cond, then_node, else_node) + assert node.get_type_name() == "Select" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 2] + assert node.get_output_element_type(0) == Type.i32 def test_gather_nd(): diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_dft.py b/src/bindings/python/tests_compatibility/test_ngraph/test_dft.py index 103aaf33bd9..492adf0884e 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_dft.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_dft.py @@ -1,9 +1,9 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 +from ngraph.impl import Type import ngraph as ng import numpy as np -from tests_compatibility.runtime import get_runtime def build_fft_input_data(): @@ -12,109 +12,90 @@ def build_fft_input_data(): def test_dft_1d(): - runtime = get_runtime() input_data = build_fft_input_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([2], dtype=np.int64)) - - dft_node = ng.dft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() np_results = np.fft.fft(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), axis=2).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.00001) + + dft_node = ng.dft(input_tensor, input_axes) + assert dft_node.get_type_name() == "DFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(np.stack((np_results.real, np_results.imag), axis=-1).shape) + assert dft_node.get_output_element_type(0) == Type.f32 def test_dft_2d(): - runtime = get_runtime() input_data = build_fft_input_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([1, 2], dtype=np.int64)) dft_node = ng.dft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), - axes=[1, 2]).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000062) + assert dft_node.get_type_name() == "DFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == [2, 10, 10, 2] + assert dft_node.get_output_element_type(0) == Type.f32 def test_dft_3d(): - runtime = get_runtime() input_data = build_fft_input_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([0, 1, 2], dtype=np.int64)) dft_node = ng.dft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fftn(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), - axes=[0, 1, 2]).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.0002) + assert dft_node.get_type_name() == "DFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == [2, 10, 10, 2] + assert dft_node.get_output_element_type(0) == Type.f32 def test_dft_1d_signal_size(): - runtime = get_runtime() input_data = build_fft_input_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([-2], dtype=np.int64)) input_signal_size = ng.constant(np.array([20], dtype=np.int64)) dft_node = ng.dft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fft(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), n=20, - axis=-2).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.00001) + assert dft_node.get_type_name() == "DFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == [2, 20, 10, 2] + assert dft_node.get_output_element_type(0) == Type.f32 def test_dft_2d_signal_size_1(): - runtime = get_runtime() input_data = build_fft_input_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([0, 2], dtype=np.int64)) input_signal_size = ng.constant(np.array([4, 5], dtype=np.int64)) dft_node = ng.dft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5], - axes=[0, 2]).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000062) + assert dft_node.get_type_name() == "DFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == [4, 10, 5, 2] + assert dft_node.get_output_element_type(0) == Type.f32 def test_dft_2d_signal_size_2(): - runtime = get_runtime() input_data = build_fft_input_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([1, 2], dtype=np.int64)) input_signal_size = ng.constant(np.array([4, 5], dtype=np.int64)) dft_node = ng.dft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5], - axes=[1, 2]).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000062) + assert dft_node.get_type_name() == "DFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == [2, 4, 5, 2] + assert dft_node.get_output_element_type(0) == Type.f32 def test_dft_3d_signal_size(): - runtime = get_runtime() input_data = build_fft_input_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([0, 1, 2], dtype=np.int64)) input_signal_size = ng.constant(np.array([4, 5, 16], dtype=np.int64)) dft_node = ng.dft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() - np_results = np.fft.fftn(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), - s=[4, 5, 16], axes=[0, 1, 2]).astype(np.complex64) - expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.0002) + assert dft_node.get_type_name() == "DFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == [4, 5, 16, 2] + assert dft_node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_einsum.py b/src/bindings/python/tests_compatibility/test_ngraph/test_einsum.py index ec3407432cd..9601d1897d2 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_einsum.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_einsum.py @@ -7,11 +7,10 @@ import pytest from ngraph.utils.types import get_element_type from tests_compatibility import xfail_issue_58033 -from tests_compatibility.runtime import get_runtime def einsum_op_exec(input_shapes: list, equation: str, data_type: np.dtype, - with_value=False, seed=202104): + seed=202104): """Test Einsum operation for given input shapes, equation, and data type. It generates input data of given shapes and type, receives reference results using numpy, @@ -19,16 +18,10 @@ def einsum_op_exec(input_shapes: list, equation: str, data_type: np.dtype, :param input_shapes: a list of tuples with shapes :param equation: Einsum equation :param data_type: a type of input data - :param with_value: if True - tests output data shape and type along with its value, - otherwise, tests only the output shape and type :param seed: a seed for random generation of input data """ np.random.seed(seed) num_inputs = len(input_shapes) - runtime = get_runtime() - - # set absolute tolerance based on the data type - atol = 0.0 if np.issubdtype(data_type, np.integer) else 1e-04 # generate input tensors ng_inputs = [] @@ -47,12 +40,6 @@ def einsum_op_exec(input_shapes: list, equation: str, data_type: np.dtype, assert list(einsum_model.get_output_shape(0)) == list(expected_result.shape) assert einsum_model.get_output_element_type(0) == get_element_type(data_type) - # check inference result - if with_value: - computation = runtime.computation(einsum_model, *ng_inputs) - actual_result = computation(*np_inputs) - np.allclose(actual_result, expected_result, atol=atol) - @pytest.mark.parametrize("data_type", [np.float32, np.int32]) def test_dot_product(data_type): diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_eye.py b/src/bindings/python/tests_compatibility/test_ngraph/test_eye.py index 069a7d80f0b..cf61b80376c 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_eye.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_eye.py @@ -7,8 +7,6 @@ import pytest from ngraph.utils.types import get_element_type from ngraph.utils.types import get_element_type_str -from tests_compatibility.runtime import get_runtime -from tests_compatibility.test_ngraph.util import run_op_node @pytest.mark.parametrize( @@ -47,12 +45,6 @@ def test_eye_rectangle(num_rows, num_columns, diagonal_index, out_type): assert eye_node.get_output_element_type(0) == get_element_type(out_type) assert tuple(eye_node.get_output_shape(0)) == expected_results.shape - # TODO: Enable with Eye reference implementation - # runtime = get_runtime() - # computation = runtime.computation(eye_node) - # eye_results = computation() - # assert np.allclose(eye_results, expected_results) - @pytest.mark.parametrize( "num_rows, num_columns, diagonal_index, batch_shape, out_type", @@ -95,9 +87,3 @@ def test_eye_batch_shape(num_rows, num_columns, diagonal_index, batch_shape, out assert eye_node.get_output_size() == 1 assert eye_node.get_output_element_type(0) == get_element_type(out_type) assert tuple(eye_node.get_output_shape(0)) == expected_results.shape - - # TODO: Enable with Eye reference implementation - # runtime = get_runtime() - # computation = runtime.computation(eye_node) - # eye_results = computation() - # assert np.allclose(eye_results, expected_results) diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_gather.py b/src/bindings/python/tests_compatibility/test_ngraph/test_gather.py index 4cfbacd0d28..2329116604e 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_gather.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_gather.py @@ -4,83 +4,59 @@ import ngraph as ng import numpy as np -from tests_compatibility.test_ngraph.util import run_op_node - def test_gather(): - input_data = np.array( - [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2], np.float32 - ).reshape((3, 3)) - input_indices = np.array([0, 2], np.int32).reshape(1, 2) + input_data = ng.parameter((3, 3), name="input_data", dtype=np.float32) + input_indices = ng.parameter((1, 2), name="input_indices", dtype=np.int32) input_axis = np.array([1], np.int32) - expected = np.array([1.0, 1.2, 2.0, 2.2, 3.0, 3.2], dtype=np.float32).reshape( - (3, 1, 2) - ) - - result = run_op_node([input_data], ng.gather, input_indices, input_axis) - assert np.allclose(result, expected) + node = ng.gather(input_data, input_indices, input_axis) + assert node.get_type_name() == "Gather" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 1, 2] def test_gather_with_scalar_axis(): - input_data = np.array( - [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2], np.float32 - ).reshape((3, 3)) - input_indices = np.array([0, 2], np.int32).reshape(1, 2) + input_data = ng.parameter((3, 3), name="input_data", dtype=np.float32) + input_indices = ng.parameter((1, 2), name="input_indices", dtype=np.int32) input_axis = np.array(1, np.int32) - expected = np.array([1.0, 1.2, 2.0, 2.2, 3.0, 3.2], dtype=np.float32).reshape( - (3, 1, 2) - ) - - result = run_op_node([input_data], ng.gather, input_indices, input_axis) - assert np.allclose(result, expected) + node = ng.gather(input_data, input_indices, input_axis) + assert node.get_type_name() == "Gather" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 1, 2] def test_gather_batch_dims_1(): - - input_data = np.array([[1, 2, 3, 4, 5], - [6, 7, 8, 9, 10]], np.float32) - - input_indices = np.array([[0, 0, 4], - [4, 0, 0]], np.int32) + input_data = ng.parameter((2, 5), name="input_data", dtype=np.float32) + input_indices = ng.parameter((2, 3), name="input_indices", dtype=np.int32) input_axis = np.array([1], np.int32) batch_dims = 1 - expected = np.array([[1, 1, 5], - [10, 6, 6]], np.float32) - - result = run_op_node([input_data], ng.gather, input_indices, input_axis, batch_dims) - assert np.allclose(result, expected) + node = ng.gather(input_data, input_indices, input_axis, batch_dims) + assert node.get_type_name() == "Gather" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [2, 3] def test_gather_negative_indices(): - input_data = np.array( - [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2], np.float32 - ).reshape((3, 3)) - input_indices = np.array([0, -1], np.int32).reshape(1, 2) + input_data = ng.parameter((3, 3), name="input_data", dtype=np.float32) + input_indices = ng.parameter((1, 2), name="input_indices", dtype=np.int32) input_axis = np.array([1], np.int32) - expected = np.array([1.0, 1.2, 2.0, 2.2, 3.0, 3.2], dtype=np.float32).reshape( - (3, 1, 2) - ) - - result = run_op_node([input_data], ng.gather, input_indices, input_axis) - assert np.allclose(result, expected) + node = ng.gather(input_data, input_indices, input_axis) + assert node.get_type_name() == "Gather" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 1, 2] def test_gather_batch_dims_1_negative_indices(): - - input_data = np.array([[1, 2, 3, 4, 5], - [6, 7, 8, 9, 10]], np.float32) - - input_indices = np.array([[0, 1, -2], - [-2, 0, 0]], np.int32) + input_data = ng.parameter((2, 5), name="input_data", dtype=np.float32) + input_indices = ng.parameter((2, 3), name="input_indices", dtype=np.int32) input_axis = np.array([1], np.int32) batch_dims = 1 - expected = np.array([[1, 2, 4], - [9, 6, 6]], np.float32) - - result = run_op_node([input_data], ng.gather, input_indices, input_axis, batch_dims) - assert np.allclose(result, expected) + node = ng.gather(input_data, input_indices, input_axis, batch_dims) + assert node.get_type_name() == "Gather" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [2, 3] diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_idft.py b/src/bindings/python/tests_compatibility/test_ngraph/test_idft.py index 18447ffa04e..b818cbd397d 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_idft.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_idft.py @@ -3,7 +3,6 @@ import ngraph as ng import numpy as np -from tests_compatibility.runtime import get_runtime def get_data(): @@ -12,7 +11,6 @@ def get_data(): def test_idft_1d(): - runtime = get_runtime() expected_results = get_data() complex_input_data = np.fft.fft(np.squeeze(expected_results.view(dtype=np.complex64), axis=-1), axis=2).astype(np.complex64) @@ -21,13 +19,12 @@ def test_idft_1d(): input_axes = ng.constant(np.array([2], dtype=np.int64)) dft_node = ng.idft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) def test_idft_2d(): - runtime = get_runtime() expected_results = get_data() complex_input_data = np.fft.fft2(np.squeeze(expected_results.view(dtype=np.complex64), axis=-1), axes=[1, 2]).astype(np.complex64) @@ -36,13 +33,12 @@ def test_idft_2d(): input_axes = ng.constant(np.array([1, 2], dtype=np.int64)) dft_node = ng.idft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) def test_idft_3d(): - runtime = get_runtime() expected_results = get_data() complex_input_data = np.fft.fft2(np.squeeze(expected_results.view(dtype=np.complex64), axis=-1), axes=[0, 1, 2]).astype(np.complex64) @@ -51,70 +47,66 @@ def test_idft_3d(): input_axes = ng.constant(np.array([0, 1, 2], dtype=np.int64)) dft_node = ng.idft(input_tensor, input_axes) - computation = runtime.computation(dft_node) - dft_results = computation() - assert np.allclose(dft_results, expected_results, atol=0.000003) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) def test_idft_1d_signal_size(): - runtime = get_runtime() input_data = get_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([-2], dtype=np.int64)) input_signal_size = ng.constant(np.array([20], dtype=np.int64)) dft_node = ng.idft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() np_results = np.fft.ifft(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), n=20, axis=-2).astype(np.complex64) expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) def test_idft_2d_signal_size_1(): - runtime = get_runtime() input_data = get_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([0, 2], dtype=np.int64)) input_signal_size = ng.constant(np.array([4, 5], dtype=np.int64)) dft_node = ng.idft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() np_results = np.fft.ifft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5], axes=[0, 2]).astype(np.complex64) expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) def test_idft_2d_signal_size_2(): - runtime = get_runtime() input_data = get_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([1, 2], dtype=np.int64)) input_signal_size = ng.constant(np.array([4, 5], dtype=np.int64)) dft_node = ng.idft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() np_results = np.fft.ifft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5], axes=[1, 2]).astype(np.complex64) expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) def test_idft_3d_signal_size(): - runtime = get_runtime() input_data = get_data() input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([0, 1, 2], dtype=np.int64)) input_signal_size = ng.constant(np.array([4, 5, 16], dtype=np.int64)) dft_node = ng.idft(input_tensor, input_axes, input_signal_size) - computation = runtime.computation(dft_node) - dft_results = computation() np_results = np.fft.ifftn(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5, 16], axes=[0, 1, 2]).astype(np.complex64) expected_results = np.stack((np_results.real, np_results.imag), axis=-1) - assert np.allclose(dft_results, expected_results, atol=0.000002) + assert dft_node.get_type_name() == "IDFT" + assert dft_node.get_output_size() == 1 + assert list(dft_node.get_output_shape(0)) == list(expected_results.shape) diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_if.py b/src/bindings/python/tests_compatibility/test_ngraph/test_if.py index daab62b855d..14f2de3e0d2 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_if.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_if.py @@ -3,13 +3,11 @@ import ngraph as ng import numpy as np -import pytest from ngraph.utils.tensor_iterator_types import ( GraphBody, TensorIteratorInvariantInputDesc, TensorIteratorBodyOutputDesc, ) -from tests_compatibility.runtime import get_runtime def create_simple_if_with_two_outputs(condition_val): @@ -117,14 +115,14 @@ def simple_if_without_parameters(condition_val): condition = ng.constant(condition_val, dtype=np.bool) # then_body - then_constant = ng.constant(0.7, dtype=np.float) + then_constant = ng.constant(0.7, dtype=np.float32) then_body_res_1 = ng.result(then_constant) then_body = GraphBody([], [then_body_res_1]) then_body_inputs = [] then_body_outputs = [TensorIteratorBodyOutputDesc(0, 0)] # else_body - else_const = ng.constant(9.0, dtype=np.float) + else_const = ng.constant(9.0, dtype=np.float32) else_body_res_1 = ng.result(else_const) else_body = GraphBody([], [else_body_res_1]) else_body_inputs = [] @@ -144,31 +142,30 @@ def check_results(results, expected_results): def check_if(if_model, cond_val, exp_results): last_node = if_model(cond_val) - runtime = get_runtime() - computation = runtime.computation(last_node) - results = computation() - check_results(results, exp_results) + assert last_node.get_type_name() == exp_results[0] + assert last_node.get_output_size() == exp_results[1] + assert list(last_node.get_output_shape(0)) == exp_results[2] def test_if_with_two_outputs(): check_if(create_simple_if_with_two_outputs, True, - [np.array([10], dtype=np.float32), np.array([-20], dtype=np.float32)]) + ["If", 2, []]) check_if(create_simple_if_with_two_outputs, False, - [np.array([17], dtype=np.float32), np.array([16], dtype=np.float32)]) + ["If", 2, []]) def test_diff_if_with_two_outputs(): check_if(create_diff_if_with_two_outputs, True, - [np.array([10], dtype=np.float32), np.array([6, 4], dtype=np.float32)]) + ["If", 2, []]) check_if(create_diff_if_with_two_outputs, False, - [np.array([4], dtype=np.float32), np.array([12, 16], dtype=np.float32)]) + ["If", 2, []]) def test_simple_if(): - check_if(simple_if, True, [np.array([6, 4], dtype=np.float32)]) - check_if(simple_if, False, [np.array([5, 5], dtype=np.float32)]) + check_if(simple_if, True, ["Relu", 1, [2]]) + check_if(simple_if, False, ["Relu", 1, [2]]) def test_simple_if_without_body_parameters(): - check_if(simple_if_without_parameters, True, [np.array([0.7], dtype=np.float32)]) - check_if(simple_if_without_parameters, False, [np.array([9.0], dtype=np.float32)]) + check_if(simple_if_without_parameters, True, ["Relu", 1, []]) + check_if(simple_if_without_parameters, False, ["Relu", 1, []]) diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_manager.py b/src/bindings/python/tests_compatibility/test_ngraph/test_manager.py index 5757ce346ca..d5b35d97564 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_manager.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_manager.py @@ -3,13 +3,11 @@ # flake8: noqa -import json import numpy as np -import pytest import ngraph as ng -from ngraph.impl import Function, PartialShape, Shape +from ngraph.impl import Function from ngraph.impl.passes import Manager from tests_compatibility.test_ngraph.util import count_ops_of_type diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_normalization.py b/src/bindings/python/tests_compatibility/test_ngraph/test_normalization.py index 1bb139cd07d..980a2f22813 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_normalization.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_normalization.py @@ -4,43 +4,25 @@ import numpy as np import ngraph as ng -from tests_compatibility.runtime import get_runtime -from tests_compatibility.test_ngraph.util import run_op_node +from ngraph.impl import Type def test_lrn(): input_image_shape = (2, 3, 2, 1) input_image = np.arange(int(np.prod(input_image_shape))).reshape(input_image_shape).astype("f") axes = np.array([1], dtype=np.int64) - runtime = get_runtime() model = ng.lrn(ng.constant(input_image), ng.constant(axes), alpha=1.0, beta=2.0, bias=1.0, size=3) - computation = runtime.computation(model) - result = computation() - assert np.allclose( - result, - np.array( - [ - [[[0.0], [0.05325444]], [[0.03402646], [0.01869806]], [[0.06805293], [0.03287071]]], - [[[0.00509002], [0.00356153]], [[0.00174719], [0.0012555]], [[0.00322708], [0.00235574]]], - ], - dtype=np.float32, - ), - ) + assert model.get_type_name() == "LRN" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 3, 2, 1] + assert model.get_output_element_type(0) == Type.f32 # Test LRN default parameter values model = ng.lrn(ng.constant(input_image), ng.constant(axes)) - computation = runtime.computation(model) - result = computation() - assert np.allclose( - result, - np.array( - [ - [[[0.0], [0.35355338]], [[0.8944272], [1.0606602]], [[1.7888544], [1.767767]]], - [[[0.93704253], [0.97827977]], [[1.2493901], [1.2577883]], [[1.5617375], [1.5372968]]], - ], - dtype=np.float32, - ), - ) + assert model.get_type_name() == "LRN" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 3, 2, 1] + assert model.get_output_element_type(0) == Type.f32 def test_lrn_factory(): @@ -49,94 +31,41 @@ def test_lrn_factory(): bias = 2.0 nsize = 3 axis = np.array([1], dtype=np.int32) - x = np.array( - [ - [ - [ - [0.31403765, -0.16793324, 1.388258, -0.6902954], - [-0.3994045, -0.7833511, -0.30992958, 0.3557573], - [-0.4682631, 1.1741459, -2.414789, -0.42783254], - ], - [ - [-0.82199496, -0.03900861, -0.43670088, -0.53810567], - [-0.10769883, 0.75242394, -0.2507971, 1.0447186], - [-1.4777364, 0.19993274, 0.925649, -2.282516], - ], - ] - ], - dtype=np.float32, - ) - excepted = np.array( - [ - [ - [ - [0.22205527, -0.11874668, 0.98161197, -0.4881063], - [-0.2824208, -0.553902, -0.21915273, 0.2515533], - [-0.33109877, 0.8302269, -1.7073234, -0.3024961], - ], - [ - [-0.5812307, -0.02758324, -0.30878326, -0.38049328], - [-0.07615435, 0.53203356, -0.17733987, 0.7387126], - [-1.0448756, 0.14137045, 0.6544598, -1.6138376], - ], - ] - ], - dtype=np.float32, - ) - result = run_op_node([x], ng.lrn, axis, alpha, beta, bias, nsize) + inputs = ng.parameter((1, 2, 3, 4), name="inputs", dtype=np.float32) + node = ng.lrn(inputs, axis, alpha, beta, bias, nsize) - assert np.allclose(result, excepted) + assert node.get_type_name() == "LRN" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [1, 2, 3, 4] + assert node.get_output_element_type(0) == Type.f32 def test_batch_norm_inference(): - data = np.array([[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]], dtype=np.float32) - gamma = np.array([2.0, 3.0, 4.0], dtype=np.float32) - beta = np.array([0.0, 0.0, 0.0], dtype=np.float32) - mean = np.array([0.0, 0.0, 0.0], dtype=np.float32) - variance = np.array([1.0, 1.0, 1.0], dtype=np.float32) + data = ng.parameter((2, 3), name="data", dtype=np.float32) + gamma = ng.parameter((3,), name="gamma", dtype=np.float32) + beta = ng.parameter((3,), name="beta", dtype=np.float32) + mean = ng.parameter((3,), name="mean", dtype=np.float32) + variance = ng.parameter((3,), name="variance", dtype=np.float32) epsilon = 9.99e-06 - excepted = np.array([[2.0, 6.0, 12.0], [-2.0, -6.0, -12.0]], dtype=np.float32) - result = run_op_node([data, gamma, beta, mean, variance], ng.batch_norm_inference, epsilon) + node = ng.batch_norm_inference(data, gamma, beta, mean, variance, epsilon) - assert np.allclose(result, excepted) - - -def test_mvn_no_variance(): - data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, - 1, 2, 3, 4, 5, 6, 7, 8, 9, - 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.float32).reshape([1, 3, 3, 3]) - axes = np.array([2, 3], dtype=np.int64) - epsilon = 1e-9 - normalize_variance = False - eps_mode = "outside_sqrt" - excepted = np.array([-4, -3, -2, -1, 0, 1, 2, 3, 4, - -4, -3, -2, -1, 0, 1, 2, 3, 4, - -4, -3, -2, -1, 0, 1, 2, 3, 4], dtype=np.float32).reshape([1, 3, 3, 3]) - - result = run_op_node([data], ng.mvn, axes, normalize_variance, epsilon, eps_mode) - - assert np.allclose(result, excepted) + assert node.get_type_name() == "BatchNormInference" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [2, 3] + assert node.get_output_element_type(0) == Type.f32 def test_mvn(): - data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, - 1, 2, 3, 4, 5, 6, 7, 8, 9, - 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.float32).reshape([1, 3, 3, 3]) + data = ng.parameter((1, 3, 3, 3), name="data", dtype=np.float32) axes = np.array([2, 3], dtype=np.int64) epsilon = 1e-9 normalize_variance = True eps_mode = "outside_sqrt" - excepted = np.array([-1.5491934, -1.161895, -0.7745967, - -0.38729835, 0., 0.38729835, - 0.7745967, 1.161895, 1.5491934, - -1.5491934, -1.161895, -0.7745967, - -0.38729835, 0., 0.38729835, - 0.7745967, 1.161895, 1.5491934, - -1.5491934, -1.161895, -0.7745967, - -0.38729835, 0., 0.38729835, - 0.7745967, 1.161895, 1.5491934], dtype=np.float32).reshape([1, 3, 3, 3]) - result = run_op_node([data], ng.mvn, axes, normalize_variance, epsilon, eps_mode) + node = ng.mvn(data, axes, normalize_variance, epsilon, eps_mode) - assert np.allclose(result, excepted) + assert node.get_type_name() == "MVN" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [1, 3, 3, 3] + assert node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_ops.py b/src/bindings/python/tests_compatibility/test_ngraph/test_ops.py index 95dd6e5e237..995281ebc86 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_ops.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_ops.py @@ -6,9 +6,8 @@ import numpy as np import ngraph as ng -from ngraph.impl import AxisSet, Function, Shape, Type +from ngraph.impl import AxisSet, Shape, Type from ngraph.impl.op import Constant, Parameter -from tests_compatibility.runtime import get_runtime def binary_op(op_str, a, b): @@ -81,47 +80,42 @@ def binary_op_ref(op_str, a, b): return np.power(a, b) -def binary_op_exec(op_str): +def binary_op_exec(op_str, expected_ov_str=None): + if not expected_ov_str: + expected_ov_str = op_str element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) - parameter_list = [A, B] - function = Function([binary_op(op_str, A, B)], parameter_list, "test") - a_arr = np.array([[1, 6], [7, 4]], dtype=np.float32) - b_arr = np.array([[5, 2], [3, 8]], dtype=np.float32) + node = binary_op(op_str, A, B) - runtime = get_runtime() - computation = runtime.computation(function, A, B) - result = computation(a_arr, b_arr)[0] - - expected = binary_op_ref(op_str, a_arr, b_arr) - assert np.allclose(result, expected) + assert node.get_type_name() == expected_ov_str + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [2, 2] + assert node.get_output_element_type(0) == Type.f32 -def binary_op_comparison(op_str): +def binary_op_comparison(op_str, expected_ov_str=None): + if not expected_ov_str: + expected_ov_str = op_str element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) - parameter_list = [A, B] - function = Function([binary_op(op_str, A, B)], parameter_list, "test") - a_arr = np.array([[1, 5], [3, 2]], dtype=np.float32) - b_arr = np.array([[2, 4], [3, 1]], dtype=np.float32) - runtime = get_runtime() - computation = runtime.computation(function, A, B) - result = computation(a_arr, b_arr)[0] + node = binary_op(op_str, A, B) - expected = binary_op_ref(op_str, a_arr, b_arr) - assert np.allclose(result, expected) + assert node.get_type_name() == expected_ov_str + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [2, 2] + assert node.get_output_element_type(0) == Type.boolean def test_add(): - binary_op_exec("+") + binary_op_exec("+", "Add") def test_add_op(): @@ -129,27 +123,27 @@ def test_add_op(): def test_sub(): - binary_op_exec("-") + binary_op_exec("-", "Subtract") def test_sub_op(): - binary_op_exec("Sub") + binary_op_exec("Sub", "Subtract") def test_mul(): - binary_op_exec("*") + binary_op_exec("*", "Multiply") def test_mul_op(): - binary_op_exec("Mul") + binary_op_exec("Mul", "Multiply") def test_div(): - binary_op_exec("/") + binary_op_exec("/", "Divide") def test_div_op(): - binary_op_exec("Div") + binary_op_exec("Div", "Divide") def test_maximum(): @@ -169,7 +163,7 @@ def test_greater(): def test_greater_eq(): - binary_op_comparison("GreaterEq") + binary_op_comparison("GreaterEq", "GreaterEqual") def test_less(): @@ -177,7 +171,7 @@ def test_less(): def test_less_eq(): - binary_op_comparison("LessEq") + binary_op_comparison("LessEq", "LessEqual") def test_not_equal(): @@ -191,23 +185,12 @@ def test_add_with_mul(): A = Parameter(element_type, shape) B = Parameter(element_type, shape) C = Parameter(element_type, shape) - parameter_list = [A, B, C] - function = Function([ng.multiply(ng.add(A, B), C)], parameter_list, "test") + node = ng.multiply(ng.add(A, B), C) - 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) + assert node.get_type_name() == "Multiply" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [4] + assert node.get_output_element_type(0) == Type.f32 def unary_op(op_str, a): @@ -298,22 +281,21 @@ def unary_op_ref(op_str, a): return np.tanh(a) -def unary_op_exec(op_str, input_list): +def unary_op_exec(op_str, input_list, expected_ov_str=None): """ input_list needs to have deep length of 4 """ + if not expected_ov_str: + expected_ov_str = op_str element_type = Type.f32 shape = Shape(np.array(input_list).shape) A = Parameter(element_type, shape) - parameter_list = [A] - function = Function([unary_op(op_str, A)], parameter_list, "test") + node = unary_op(op_str, A) - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(np.array(input_list, dtype=np.float32))[0] - - expected = unary_op_ref(op_str, np.array(input_list, dtype=np.float32)) - assert np.allclose(result, expected) + assert node.get_type_name() == expected_ov_str + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == list(shape) + assert node.get_output_element_type(0) == Type.f32 def test_abs(): @@ -385,19 +367,19 @@ def test_floor(): def test_log(): input_list = [1, 2, 3, 4] op_str = "log" - unary_op_exec(op_str, input_list) + unary_op_exec(op_str, input_list, "Log") def test_exp(): input_list = [-1, 0, 1, 2] op_str = "exp" - unary_op_exec(op_str, input_list) + unary_op_exec(op_str, input_list, "Exp") def test_negative(): input_list = [-1, 0, 1, 2] op_str = "negative" - unary_op_exec(op_str, input_list) + unary_op_exec(op_str, input_list, "Negative") def test_sign(): @@ -441,67 +423,42 @@ def test_reshape(): element_type = Type.f32 shape = Shape([2, 3]) A = Parameter(element_type, shape) - parameter_list = [A] - function = Function([ng.reshape(A, Shape([3, 2]), special_zero=False)], parameter_list, "test") + node = ng.reshape(A, Shape([3, 2]), special_zero=False) - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(np.array(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), dtype=np.float32))[0] - - expected = np.reshape(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), (3, 2)) - assert np.allclose(result, expected) + assert node.get_type_name() == "Reshape" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 2] + assert node.get_output_element_type(0) == element_type def test_broadcast(): element_type = Type.f32 A = Parameter(element_type, Shape([3])) - parameter_list = [A] - function = Function([ng.broadcast(A, [3, 3])], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(np.array([1, 2, 3], dtype=np.float32))[0] - - a_arr = np.array([[0], [0], [0]], dtype=np.float32) - b_arr = np.array([[1, 2, 3]], dtype=np.float32) - expected = np.add(a_arr, b_arr) - assert np.allclose(result, expected) + node = ng.broadcast(A, [3, 3]) + assert node.get_type_name() == "Broadcast" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 3] + assert node.get_output_element_type(0) == element_type def test_constant(): element_type = Type.f32 - parameter_list = [] - function = Function([Constant(element_type, Shape([3, 3]), list(range(9)))], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation()[0] - - expected = np.arange(9).reshape(3, 3) - assert np.allclose(result, expected) + node = Constant(element_type, Shape([3, 3]), list(range(9))) + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 3] + assert node.get_output_element_type(0) == element_type def test_concat(): element_type = Type.f32 - A = Parameter(element_type, Shape([1, 2])) - B = Parameter(element_type, Shape([1, 2])) - C = Parameter(element_type, Shape([1, 2])) - parameter_list = [A, B, C] - axis = 0 - function = Function([ng.concat([A, B, C], axis)], parameter_list, "test") - - a_arr = np.array([[1, 2]], dtype=np.float32) - b_arr = np.array([[5, 6]], dtype=np.float32) - c_arr = np.array([[7, 8]], dtype=np.float32) - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(a_arr, b_arr, c_arr)[0] - - expected = np.concatenate((a_arr, b_arr, c_arr), axis) - assert np.allclose(result, expected) + node = Constant(element_type, Shape([3, 3]), list(range(9))) + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 3] + assert node.get_output_element_type(0) == element_type def test_axisset(): @@ -525,31 +482,18 @@ def test_select(): A = Parameter(Type.boolean, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) - parameter_list = [A, B, C] - - function = Function([ng.select(A, B, C)], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation( - np.array([[True, False]], dtype=np.bool), - np.array([[5, 6]], dtype=np.float32), - np.array([[7, 8]], dtype=np.float32), - )[0] - - expected = np.array([[5, 8]]) - assert np.allclose(result, expected) + node = ng.select(A, B, C) + assert node.get_type_name() == "Select" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [1, 2] + assert node.get_output_element_type(0) == element_type -def test_max_pool(): - # test 1d +def test_max_pool_1d(): element_type = Type.f32 shape = Shape([1, 1, 10]) - A = Parameter(element_type, shape) - parameter_list = [A] - - input_arr = np.arange(10, dtype=np.float32).reshape([1, 1, 10]) window_shape = [3] + A = Parameter(element_type, shape) strides = [1] * len(window_shape) dilations = [1] * len(window_shape) @@ -570,19 +514,26 @@ def test_max_pool(): auto_pad, idx_elem_type, ) - function = Function([model], parameter_list, "test") + assert model.get_type_name() == "MaxPool" + assert model.get_output_size() == 2 + assert list(model.get_output_shape(0)) == [1, 1, 8] + assert list(model.get_output_shape(1)) == [1, 1, 8] + assert model.get_output_element_type(0) == element_type + assert model.get_output_element_type(1) == Type.i32 - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(input_arr)[0] - expected = (np.arange(8) + 2).reshape(1, 1, 8) - assert np.allclose(result, expected) - - # test 1d with strides +def test_max_pool_1d_with_strides(): + element_type = Type.f32 + shape = Shape([1, 1, 10]) + A = Parameter(element_type, shape) + window_shape = [3] strides = [2] pads_begin = [0] * len(window_shape) + dilations = [1] * len(window_shape) pads_end = [0] * len(window_shape) + rounding_type = "floor" + auto_pad = "explicit" + idx_elem_type = "i32" model = ng.max_pool( A, @@ -595,16 +546,15 @@ def test_max_pool(): auto_pad, idx_elem_type, ) - function = Function([model], parameter_list, "test") + assert model.get_type_name() == "MaxPool" + assert model.get_output_size() == 2 + assert list(model.get_output_shape(0)) == [1, 1, 4] + assert list(model.get_output_shape(1)) == [1, 1, 4] + assert model.get_output_element_type(0) == element_type + assert model.get_output_element_type(1) == Type.i32 - size = 4 - computation = runtime.computation(function, *parameter_list) - result = computation(input_arr)[0] - expected = ((np.arange(size) + 1) * 2).reshape(1, 1, size) - assert np.allclose(result, expected) - - # test 2d +def test_max_pool_2d(): element_type = Type.f32 shape = Shape([1, 1, 10, 10]) A = Parameter(element_type, shape) @@ -612,6 +562,9 @@ def test_max_pool(): input_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) window_shape = [3, 3] + rounding_type = "floor" + auto_pad = "explicit" + idx_elem_type = "i32" strides = [1, 1] dilations = [1, 1] @@ -629,19 +582,26 @@ def test_max_pool(): auto_pad, idx_elem_type, ) - function = Function([model], parameter_list, "test") + assert model.get_type_name() == "MaxPool" + assert model.get_output_size() == 2 + assert list(model.get_output_shape(0)) == [1, 1, 8, 8] + assert list(model.get_output_shape(1)) == [1, 1, 8, 8] + assert model.get_output_element_type(0) == element_type + assert model.get_output_element_type(1) == Type.i32 - computation = runtime.computation(function, *parameter_list) - result = computation(input_arr)[0] - expected = ((np.arange(100).reshape(10, 10))[2:, 2:]).reshape(1, 1, 8, 8) - assert np.allclose(result, expected) - - # test 2d with strides +def test_max_pool_2d_with_strides(): + element_type = Type.f32 + shape = Shape([1, 1, 10, 10]) + A = Parameter(element_type, shape) strides = [2, 2] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] + window_shape = [3, 3] + rounding_type = "floor" + auto_pad = "explicit" + idx_elem_type = "i32" model = ng.max_pool( A, @@ -654,13 +614,12 @@ def test_max_pool(): auto_pad, idx_elem_type, ) - function = Function([model], parameter_list, "test") - computation = runtime.computation(function, *parameter_list) - result = computation(input_arr)[0] - - size = 4 - expected = ((np.arange(100).reshape(10, 10))[2::2, 2::2]).reshape(1, 1, size, size) - assert np.allclose(result, expected) + assert model.get_type_name() == "MaxPool" + assert model.get_output_size() == 2 + assert list(model.get_output_shape(0)) == [1, 1, 4, 4] + assert list(model.get_output_shape(1)) == [1, 1, 4, 4] + assert model.get_output_element_type(0) == element_type + assert model.get_output_element_type(1) == Type.i32 def convolution2d( @@ -716,9 +675,6 @@ def test_convolution_simple(): filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) - parameter_list = [data, filters] - - image_arr = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][0][0] = -1 filter_arr[0][0][1][1] = -1 @@ -732,14 +688,10 @@ def test_convolution_simple(): dilations = [1, 1] model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) - function = Function([model], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(image_arr, filter_arr)[0] - - expected = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14) - assert np.allclose(result, expected) + assert model.get_type_name() == "Convolution" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 14, 14] + assert model.get_output_element_type(0) == element_type def test_convolution_with_strides(): @@ -749,9 +701,6 @@ def test_convolution_with_strides(): filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) - parameter_list = [data, filters] - - image_arr = np.arange(100, dtype=np.float32).reshape([1, 1, 10, 10]) filter_arr = np.zeros(9, dtype=np.float32).reshape([1, 1, 3, 3]) filter_arr[0][0][1][1] = 1 strides = [2, 2] @@ -760,14 +709,10 @@ def test_convolution_with_strides(): dilations = [1, 1] model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) - function = Function([model], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(image_arr, filter_arr)[0] - - expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides).reshape(1, 1, 4, 4) - assert np.allclose(result, expected) + assert model.get_type_name() == "Convolution" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 4, 4] + assert model.get_output_element_type(0) == element_type def test_convolution_with_filter_dilation(): @@ -777,24 +722,16 @@ def test_convolution_with_filter_dilation(): filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) - parameter_list = [data, filters] - - image_arr = np.arange(100, dtype=np.float32).reshape([1, 1, 10, 10]) - filter_arr = np.ones(9, dtype=np.float32).reshape([1, 1, 3, 3]) strides = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] dilations = [2, 2] model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) - function = Function([model], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(image_arr, filter_arr)[0] - - expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilations).reshape([1, 1, 6, 6]) - assert np.allclose(result, expected) + assert model.get_type_name() == "Convolution" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 6, 6] + assert model.get_output_element_type(0) == element_type def test_convolution_with_padding(): @@ -804,9 +741,6 @@ def test_convolution_with_padding(): filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) - parameter_list = [data, filters] - - image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = np.zeros(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][1][1] = 1 strides = [1, 1] @@ -815,16 +749,10 @@ def test_convolution_with_padding(): pads_end = [0, 0] model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) - function = Function([model], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(image_arr, filter_arr)[0] - - expected = convolution2d( - image_arr[0][0], filter_arr[0][0], strides, dilations, pads_begin, pads_end - ).reshape([1, 1, 6, 6]) - assert np.allclose(result, expected) + assert model.get_type_name() == "Convolution" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 6, 6] + assert model.get_output_element_type(0) == element_type def test_convolution_with_non_zero_padding(): @@ -833,9 +761,6 @@ def test_convolution_with_non_zero_padding(): filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) - parameter_list = [data, filters] - - image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = (np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)) * -1 filter_arr[0][0][1][1] = 1 strides = [1, 1] @@ -844,13 +769,7 @@ def test_convolution_with_non_zero_padding(): pads_end = [1, 2] model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) - function = Function([model], parameter_list, "test") - - runtime = get_runtime() - computation = runtime.computation(function, *parameter_list) - result = computation(image_arr, filter_arr)[0] - - expected = convolution2d( - image_arr[0][0], filter_arr[0][0], strides, dilations, pads_begin, pads_end - ).reshape([1, 1, 9, 9]) - assert np.allclose(result, expected) + assert model.get_type_name() == "Convolution" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 9, 9] + assert model.get_output_element_type(0) == element_type diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_binary.py b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_binary.py index 92b3cdb826a..44f5237d45b 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_binary.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_binary.py @@ -7,203 +7,173 @@ import numpy as np import pytest import ngraph as ng -from tests_compatibility.runtime import get_runtime -from tests_compatibility.test_ngraph.util import run_op_node +from ngraph.impl import Type @pytest.mark.parametrize( - "ng_api_helper,numpy_function", + ("ng_api_helper", "expected_type"), [ - (ng.add, np.add), - (ng.divide, np.divide), - (ng.multiply, np.multiply), - (ng.subtract, np.subtract), - (ng.minimum, np.minimum), - (ng.maximum, np.maximum), - (ng.mod, np.mod), - (ng.equal, np.equal), - (ng.not_equal, np.not_equal), - (ng.greater, np.greater), - (ng.greater_equal, np.greater_equal), - (ng.less, np.less), - (ng.less_equal, np.less_equal), + (ng.add, Type.f32), + (ng.divide, Type.f32), + (ng.multiply, Type.f32), + (ng.subtract, Type.f32), + (ng.minimum, Type.f32), + (ng.maximum, Type.f32), + (ng.mod, Type.f32), + (ng.equal, Type.boolean), + (ng.not_equal, Type.boolean), + (ng.greater, Type.boolean), + (ng.greater_equal, Type.boolean), + (ng.less, Type.boolean), + (ng.less_equal, Type.boolean), ], ) -def test_binary_op(ng_api_helper, numpy_function): - runtime = get_runtime() - +def test_binary_op(ng_api_helper, expected_type): shape = [2, 2] parameter_a = ng.parameter(shape, name="A", dtype=np.float32) parameter_b = ng.parameter(shape, name="B", dtype=np.float32) model = ng_api_helper(parameter_a, parameter_b) - computation = runtime.computation(model, parameter_a, parameter_b) - value_a = np.array([[1, 2], [3, 4]], dtype=np.float32) - value_b = np.array([[5, 6], [7, 8]], dtype=np.float32) - - result = computation(value_a, value_b) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] + assert model.get_output_element_type(0) == expected_type @pytest.mark.parametrize( - "ng_api_helper,numpy_function", + ("ng_api_helper", "expected_type"), [ - (ng.add, np.add), - (ng.divide, np.divide), - (ng.multiply, np.multiply), - (ng.subtract, np.subtract), - (ng.minimum, np.minimum), - (ng.maximum, np.maximum), - (ng.mod, np.mod), - (ng.equal, np.equal), - (ng.not_equal, np.not_equal), - (ng.greater, np.greater), - (ng.greater_equal, np.greater_equal), - (ng.less, np.less), - (ng.less_equal, np.less_equal), + (ng.add, Type.f32), + (ng.divide, Type.f32), + (ng.multiply, Type.f32), + (ng.subtract, Type.f32), + (ng.minimum, Type.f32), + (ng.maximum, Type.f32), + (ng.mod, Type.f32), + (ng.equal, Type.boolean), + (ng.not_equal, Type.boolean), + (ng.greater, Type.boolean), + (ng.greater_equal, Type.boolean), + (ng.less, Type.boolean), + (ng.less_equal, Type.boolean), ], ) -def test_binary_op_with_scalar(ng_api_helper, numpy_function): - runtime = get_runtime() - - value_a = np.array([[1, 2], [3, 4]], dtype=np.float32) +def test_binary_op(ng_api_helper, expected_type): value_b = np.array([[5, 6], [7, 8]], dtype=np.float32) shape = [2, 2] parameter_a = ng.parameter(shape, name="A", dtype=np.float32) model = ng_api_helper(parameter_a, value_b) - computation = runtime.computation(model, parameter_a) - - result = computation(value_a) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] + assert model.get_output_element_type(0) == expected_type @pytest.mark.parametrize( - "ng_api_helper,numpy_function", - [(ng.logical_and, np.logical_and), (ng.logical_or, np.logical_or), (ng.logical_xor, np.logical_xor)], + "ng_api_helper", + [ng.logical_and, ng.logical_or, ng.logical_xor], ) -def test_binary_logical_op(ng_api_helper, numpy_function): - runtime = get_runtime() - +def test_binary_logical_op_parameter_inputs(ng_api_helper): shape = [2, 2] parameter_a = ng.parameter(shape, name="A", dtype=np.bool) parameter_b = ng.parameter(shape, name="B", dtype=np.bool) model = ng_api_helper(parameter_a, parameter_b) - computation = runtime.computation(model, parameter_a, parameter_b) - - value_a = np.array([[True, False], [False, True]], dtype=np.bool) - value_b = np.array([[False, True], [False, True]], dtype=np.bool) - - result = computation(value_a, value_b) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] + assert model.get_output_element_type(0) == Type.boolean @pytest.mark.parametrize( - "ng_api_helper,numpy_function", - [(ng.logical_and, np.logical_and), (ng.logical_or, np.logical_or), (ng.logical_xor, np.logical_xor)], + "ng_api_helper", + [ng.logical_and, ng.logical_or, ng.logical_xor], ) -def test_binary_logical_op_with_scalar(ng_api_helper, numpy_function): - runtime = get_runtime() - - value_a = np.array([[True, False], [False, True]], dtype=np.bool) +def test_binary_logical_numpy_input(ng_api_helper): value_b = np.array([[False, True], [False, True]], dtype=np.bool) shape = [2, 2] parameter_a = ng.parameter(shape, name="A", dtype=np.bool) model = ng_api_helper(parameter_a, value_b) - computation = runtime.computation(model, parameter_a) - - result = computation(value_a) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] + assert model.get_output_element_type(0) == Type.boolean @pytest.mark.parametrize( - "operator,numpy_function", + ("operator", "expected_type"), [ - (operator.add, np.add), - (operator.sub, np.subtract), - (operator.mul, np.multiply), - (operator.truediv, np.divide), - (operator.eq, np.equal), - (operator.ne, np.not_equal), - (operator.gt, np.greater), - (operator.ge, np.greater_equal), - (operator.lt, np.less), - (operator.le, np.less_equal), + (operator.add, Type.f32), + (operator.sub, Type.f32), + (operator.mul, Type.f32), + (operator.truediv, Type.f32), + (operator.eq, Type.boolean), + (operator.ne, Type.boolean), + (operator.gt, Type.boolean), + (operator.ge, Type.boolean), + (operator.lt, Type.boolean), + (operator.le, Type.boolean), ], ) -def test_binary_operators(operator, numpy_function): - runtime = get_runtime() - - value_a = np.array([[1, 2], [3, 4]], dtype=np.float32) +def test_binary_operators(operator, expected_type): value_b = np.array([[4, 5], [1, 7]], dtype=np.float32) shape = [2, 2] parameter_a = ng.parameter(shape, name="A", dtype=np.float32) model = operator(parameter_a, value_b) - computation = runtime.computation(model, parameter_a) - - result = computation(value_a) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] + assert model.get_output_element_type(0) == expected_type @pytest.mark.parametrize( - "operator,numpy_function", + ("operator", "expected_type"), [ - (operator.add, np.add), - (operator.sub, np.subtract), - (operator.mul, np.multiply), - (operator.truediv, np.divide), - (operator.eq, np.equal), - (operator.ne, np.not_equal), - (operator.gt, np.greater), - (operator.ge, np.greater_equal), - (operator.lt, np.less), - (operator.le, np.less_equal), + (operator.add, Type.f32), + (operator.sub, Type.f32), + (operator.mul, Type.f32), + (operator.truediv, Type.f32), + (operator.eq, Type.boolean), + (operator.ne, Type.boolean), + (operator.gt, Type.boolean), + (operator.ge, Type.boolean), + (operator.lt, Type.boolean), + (operator.le, Type.boolean), ], ) -def test_binary_operators_with_scalar(operator, numpy_function): - runtime = get_runtime() - - value_a = np.array([[1, 2], [3, 4]], dtype=np.float32) - value_b = np.array([[5, 6], [7, 8]], dtype=np.float32) +def test_binary_operators_with_scalar(operator, expected_type): + value_b = np.array(3, dtype=np.float32) shape = [2, 2] parameter_a = ng.parameter(shape, name="A", dtype=np.float32) model = operator(parameter_a, value_b) - computation = runtime.computation(model, parameter_a) - - result = computation(value_a) - expected = numpy_function(value_a, value_b) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] + assert model.get_output_element_type(0) == expected_type def test_multiply(): A = np.arange(48, dtype=np.int32).reshape((8, 1, 6, 1)) B = np.arange(35, dtype=np.int32).reshape((7, 1, 5)) - expected = np.multiply(A, B) - result = run_op_node([A, B], ng.multiply) + node = ng.multiply(A, B) - assert np.allclose(result, expected) + assert node.get_type_name() == "Multiply" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [8, 7, 6, 5] + assert node.get_output_element_type(0) == Type.i32 def test_power_v1(): A = np.arange(48, dtype=np.float32).reshape((8, 1, 6, 1)) B = np.arange(20, dtype=np.float32).reshape((4, 1, 5)) - expected = np.power(A, B) - result = run_op_node([A, B], ng.power) + node = ng.power(A, B) - assert np.allclose(result, expected) + assert node.get_type_name() == "Power" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [8, 4, 6, 5] + assert node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_fused.py b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_fused.py index ddaa6f183de..7ac6c5f4b74 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_fused.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_fused.py @@ -2,52 +2,37 @@ # SPDX-License-Identifier: Apache-2.0 import numpy as np -import pytest import ngraph as ng -from tests_compatibility.runtime import get_runtime -from tests_compatibility import xfail_issue_36486 +from ngraph.impl import Type def test_elu_operator_with_scalar_and_array(): - runtime = get_runtime() - - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) + data_value = ng.parameter((2, 2), name="data_value", dtype=np.float32) alpha_value = np.float32(3) model = ng.elu(data_value, alpha_value) - computation = runtime.computation(model) - - result = computation() - expected = np.array([[-2.9797862, 1.0], [-2.5939941, 3.0]], dtype=np.float32) - assert np.allclose(result, expected) + assert model.get_type_name() == "Elu" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] + assert model.get_output_element_type(0) == Type.f32 def test_elu_operator_with_scalar(): - runtime = get_runtime() - - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) + parameter_data = ng.parameter([2, 2], name="Data", dtype=np.float32) alpha_value = np.float32(3) data_shape = [2, 2] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.elu(parameter_data, alpha_value) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array([[-2.9797862, 1.0], [-2.5939941, 3.0]], dtype=np.float32) - assert np.allclose(result, expected) + assert model.get_type_name() == "Elu" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] + assert model.get_output_element_type(0) == Type.f32 def test_fake_quantize(): - runtime = get_runtime() - - data_value = np.arange(24.0, dtype=np.float32).reshape(1, 2, 3, 4) - input_low_value = np.float32(0) - input_high_value = np.float32(23) - output_low_value = np.float32(2) - output_high_value = np.float32(16) levels = np.float32(4) data_shape = [1, 2, 3, 4] @@ -66,74 +51,29 @@ def test_fake_quantize(): parameter_output_high, levels, ) - computation = runtime.computation( - model, - parameter_data, - parameter_input_low, - parameter_input_high, - parameter_output_low, - parameter_output_high, - ) - - result = computation(data_value, input_low_value, input_high_value, output_low_value, output_high_value) - - expected = np.array( - [ - [ - [ - [ - [2.0, 2.0, 2.0, 2.0], - [6.6666669, 6.6666669, 6.6666669, 6.6666669], - [6.6666669, 6.6666669, 6.6666669, 6.6666669], - ], - [ - [11.33333301, 11.33333301, 11.33333301, 11.33333301], - [11.33333301, 11.33333301, 11.33333301, 11.33333301], - [16.0, 16.0, 16.0, 16.0], - ], - ] - ] - ], - dtype=np.float32, - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "FakeQuantize" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 2, 3, 4] + assert model.get_output_element_type(0) == Type.f32 def test_depth_to_space(): - runtime = get_runtime() - - data_value = np.array( - [ - [ - [[0, 1, 2], [3, 4, 5]], - [[6, 7, 8], [9, 10, 11]], - [[12, 13, 14], [15, 16, 17]], - [[18, 19, 20], [21, 22, 23]], - ] - ], - dtype=np.float32, - ) + data_shape = [1, 4, 2, 3] mode = "blocks_first" block_size = np.float32(2) - data_shape = [1, 4, 2, 3] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.depth_to_space(parameter_data, mode, block_size) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array( - [[[[0, 6, 1, 7, 2, 8], [12, 18, 13, 19, 14, 20], [3, 9, 4, 10, 5, 11], [15, 21, 16, 22, 17, 23]]]], - dtype=np.float32, - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "DepthToSpace" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 1, 4, 6] + assert model.get_output_element_type(0) == Type.f32 def test_space_to_batch(): - runtime = get_runtime() - data_value = np.array([[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]], dtype=np.float32) + data_shape = [1, 2, 2, 3] data_shape = data_value.shape block_shape = np.array([1, 2, 3, 2], dtype=np.int64) @@ -143,50 +83,14 @@ def test_space_to_batch(): parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.space_to_batch(parameter_data, block_shape, pads_begin, pads_end) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array( - [ - [[[0, 0]]], - [[[0, 0]]], - [[[0, 2]]], - [[[1, 0]]], - [[[3, 5]]], - [[[4, 0]]], - [[[0, 0]]], - [[[0, 0]]], - [[[6, 8]]], - [[[7, 0]]], - [[[9, 11]]], - [[[10, 0]]], - ], - dtype=np.float32, - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "SpaceToBatch" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [12, 1, 1, 2] + assert model.get_output_element_type(0) == Type.f32 def test_batch_to_space(): - runtime = get_runtime() - - data = np.array( - [ - [[[0, 0]]], - [[[0, 0]]], - [[[0, 2]]], - [[[1, 0]]], - [[[3, 5]]], - [[[4, 0]]], - [[[0, 0]]], - [[[0, 0]]], - [[[6, 8]]], - [[[7, 0]]], - [[[9, 11]]], - [[[10, 0]]], - ], - dtype=np.float32, - ) - data_shape = data.shape + data_shape = [12, 1, 1, 2] block_shape = np.array([1, 2, 3, 2], dtype=np.int64) crops_begin = np.array([0, 0, 1, 0], dtype=np.int64) @@ -195,142 +99,89 @@ def test_batch_to_space(): parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.batch_to_space(parameter_data, block_shape, crops_begin, crops_end) - computation = runtime.computation(model, parameter_data) - - result = computation(data) - expected = np.array([[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]], dtype=np.float32) - - assert np.allclose(result, expected) + assert model.get_type_name() == "BatchToSpace" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 2, 2, 3] + assert model.get_output_element_type(0) == Type.f32 def test_clamp_operator(): - runtime = get_runtime() - data_shape = [2, 2] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) min_value = np.float32(3) max_value = np.float32(12) model = ng.clamp(parameter_data, min_value, max_value) - computation = runtime.computation(model, parameter_data) - - data_value = np.array([[-5, 9], [45, 3]], dtype=np.float32) - - result = computation(data_value) - expected = np.clip(data_value, min_value, max_value) - assert np.allclose(result, expected) + assert model.get_type_name() == "Clamp" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] + assert model.get_output_element_type(0) == Type.f32 def test_clamp_operator_with_array(): - runtime = get_runtime() - data_value = np.array([[-5, 9], [45, 3]], dtype=np.float32) min_value = np.float32(3) max_value = np.float32(12) model = ng.clamp(data_value, min_value, max_value) - computation = runtime.computation(model) - - result = computation() - expected = np.clip(data_value, min_value, max_value) - - assert np.allclose(result, expected) + assert model.get_type_name() == "Clamp" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [2, 2] + assert model.get_output_element_type(0) == Type.f32 def test_squeeze_operator(): - runtime = get_runtime() - data_shape = [1, 2, 1, 3, 1, 1] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) - data_value = np.arange(6.0, dtype=np.float32).reshape([1, 2, 1, 3, 1, 1]) axes = [2, 4] model = ng.squeeze(parameter_data, axes) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.arange(6.0, dtype=np.float32).reshape([1, 2, 3, 1]) - assert np.allclose(result, expected) + assert model.get_type_name() == "Squeeze" + assert model.get_output_size() == 1 + assert list(model.get_output_shape(0)) == [1, 2, 3, 1] + assert model.get_output_element_type(0) == Type.f32 def test_squared_difference_operator(): - runtime = get_runtime() - x1_shape = [1, 2, 3, 4] x2_shape = [2, 3, 4] parameter_x1 = ng.parameter(x1_shape, name="x1", dtype=np.float32) parameter_x2 = ng.parameter(x2_shape, name="x2", dtype=np.float32) - x1_value = np.arange(24.0, dtype=np.float32).reshape(x1_shape) - x2_value = np.arange(start=4.0, stop=28.0, step=1.0, dtype=np.float32).reshape(x2_shape) - model = ng.squared_difference(parameter_x1, parameter_x2) - computation = runtime.computation(model, parameter_x1, parameter_x2) - - result = computation(x1_value, x2_value) - expected = np.square(np.subtract(x1_value, x2_value)) - assert np.allclose(result, expected) + assert model.get_type_name() == "SquaredDifference" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [1, 2, 3, 4] def test_shuffle_channels_operator(): - runtime = get_runtime() - data_shape = [1, 15, 2, 2] axis = 1 groups = 5 parameter = ng.parameter(data_shape, name="Data", dtype=np.float32) - data_value = np.arange(60.0, dtype=np.float32).reshape(data_shape) - model = ng.shuffle_channels(parameter, axis, groups) - computation = runtime.computation(model, parameter) - - result = computation(data_value) - expected = np.array( - [ - [ - [[0.0, 1.0], [2.0, 3.0]], - [[12.0, 13.0], [14.0, 15.0]], - [[24.0, 25.0], [26.0, 27.0]], - [[36.0, 37.0], [38.0, 39.0]], - [[48.0, 49.0], [50.0, 51.0]], - [[4.0, 5.0], [6.0, 7.0]], - [[16.0, 17.0], [18.0, 19.0]], - [[28.0, 29.0], [30.0, 31.0]], - [[40.0, 41.0], [42.0, 43.0]], - [[52.0, 53.0], [54.0, 55.0]], - [[8.0, 9.0], [10.0, 11.0]], - [[20.0, 21.0], [22.0, 23.0]], - [[32.0, 33.0], [34.0, 35.0]], - [[44.0, 45.0], [46.0, 47.0]], - [[56.0, 57.0], [58.0, 59.0]], - ] - ], - dtype=np.float32, - ) - assert np.allclose(result, expected) + assert model.get_type_name() == "ShuffleChannels" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [1, 15, 2, 2] def test_unsqueeze(): - runtime = get_runtime() data_shape = [3, 4, 5] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) - data_value = np.arange(60.0, dtype=np.float32).reshape(3, 4, 5) axes = [0, 4] model = ng.unsqueeze(parameter_data, axes) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.arange(60.0, dtype=np.float32).reshape([1, 3, 4, 5, 1]) - assert np.allclose(result, expected) + assert model.get_type_name() == "Unsqueeze" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [1, 3, 4, 5, 1] def test_grn_operator(): - runtime = get_runtime() - - data_value = np.arange(start=1.0, stop=25.0, dtype=np.float32).reshape([1, 2, 3, 4]) bias = np.float32(1e-6) data_shape = [1, 2, 3, 4] @@ -338,202 +189,82 @@ def test_grn_operator(): parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.grn(parameter_data, bias) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array( - [ - [ - [ - [0.0766965, 0.14142136, 0.19611613, 0.24253564], - [0.28216633, 0.31622776, 0.34570536, 0.37139067], - [0.39391932, 0.41380295, 0.4314555, 0.4472136], - ], - [ - [0.9970545, 0.98994946, 0.9805807, 0.97014254], - [0.9593655, 0.9486833, 0.9383431, 0.9284767], - [0.91914505, 0.9103665, 0.9021342, 0.8944272], - ], - ] - ], - dtype=np.float32, - ) - - assert np.allclose(result, expected) + assert model.get_type_name() == "GRN" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == data_shape def test_prelu_operator(): - runtime = get_runtime() - data_shape = [1, 2, 3, 4] slope_shape = [2, 3, 1] - data_value = np.arange(start=1.0, stop=25.0, dtype=np.float32).reshape(data_shape) - slope_value = np.arange(start=-10.0, stop=-4.0, dtype=np.float32).reshape(slope_shape) parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) parameter_slope = ng.parameter(slope_shape, name="Slope", dtype=np.float32) model = ng.prelu(parameter_data, parameter_slope) - computation = runtime.computation(model, parameter_data, parameter_slope) - - result = computation(data_value, slope_value) - expected = np.clip(data_value, 0, np.inf) + np.clip(data_value, -np.inf, 0) * slope_value - assert np.allclose(result, expected) + assert model.get_type_name() == "PRelu" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [1, 2, 3, 4] def test_selu_operator(): - runtime = get_runtime() - data_shape = [4, 2, 3, 1] - data = np.arange(start=1.0, stop=25.0, dtype=np.float32).reshape(data_shape) alpha = np.array(1.6733, dtype=np.float32) lambda_value = np.array(1.0507, dtype=np.float32) parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.selu(parameter_data, alpha, lambda_value) - computation = runtime.computation(model, parameter_data) - - result = computation(data) - expected = lambda_value * ((data > 0) * data + (data <= 0) * (alpha * np.exp(data) - alpha)) - assert np.allclose(result, expected) + assert model.get_type_name() == "Selu" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [4, 2, 3, 1] -@xfail_issue_36486 def test_hard_sigmoid_operator(): - runtime = get_runtime() - data_shape = [3] - alpha_value = np.float32(0.5) - beta_value = np.float32(0.6) - - data_value = np.array([-1, 0, 1], dtype=np.float32) parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) parameter_alpha = ng.parameter([], name="Alpha", dtype=np.float32) parameter_beta = ng.parameter([], name="Beta", dtype=np.float32) model = ng.hard_sigmoid(parameter_data, parameter_alpha, parameter_beta) - computation = runtime.computation(model, parameter_data, parameter_alpha, parameter_beta) - - result = computation(data_value, alpha_value, beta_value) - expected = [0.1, 0.6, 1.0] - assert np.allclose(result, expected) + assert model.get_type_name() == "HardSigmoid" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [3] def test_mvn_operator(): - runtime = get_runtime() - data_shape = [3, 3, 3, 1] axes = [0, 2, 3] normalize_variance = True eps = np.float32(1e-9) eps_mode = "outside_sqrt" - data_value = np.array( - [ - [ - [[0.8439683], [0.5665144], [0.05836735]], - [[0.02916367], [0.12964272], [0.5060197]], - [[0.79538304], [0.9411346], [0.9546573]], - ], - [ - [[0.17730942], [0.46192095], [0.26480448]], - [[0.6746842], [0.01665257], [0.62473077]], - [[0.9240844], [0.9722341], [0.11965699]], - ], - [ - [[0.41356155], [0.9129373], [0.59330076]], - [[0.81929934], [0.7862604], [0.11799799]], - [[0.69248444], [0.54119414], [0.07513223]], - ], - ], - dtype=np.float32, - ) - parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.mvn(parameter_data, axes, normalize_variance, eps, eps_mode) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - - expected = np.array( - [ - [ - [[1.3546423], [0.33053496], [-1.5450814]], - [[-1.2106764], [-0.8925952], [0.29888135]], - [[0.38083088], [0.81808794], [0.85865635]], - ], - [ - [[-1.1060555], [-0.05552877], [-0.78310335]], - [[0.83281356], [-1.250282], [0.67467856]], - [[0.7669372], [0.9113869], [-1.6463585]], - ], - [ - [[-0.23402764], [1.6092131], [0.42940593]], - [[1.2906139], [1.1860244], [-0.92945826]], - [[0.0721334], [-0.38174], [-1.7799333]], - ], - ], - dtype=np.float32, - ) - - assert np.allclose(result, expected) + assert model.get_type_name() == "MVN" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == data_shape def test_space_to_depth_operator(): - runtime = get_runtime() - data_shape = [1, 2, 4, 4] - data_value = np.arange(start=0, stop=32, step=1.0, dtype=np.float32).reshape(data_shape) mode = "blocks_first" block_size = 2 parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.space_to_depth(parameter_data, mode, block_size) - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array( - [ - 0, - 2, - 8, - 10, - 16, - 18, - 24, - 26, - 1, - 3, - 9, - 11, - 17, - 19, - 25, - 27, - 4, - 6, - 12, - 14, - 20, - 22, - 28, - 30, - 5, - 7, - 13, - 15, - 21, - 23, - 29, - 31, - ], - dtype=np.float32, - ).reshape(1, 8, 2, 2) - assert np.allclose(result, expected) + assert model.get_type_name() == "SpaceToDepth" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [1, 8, 2, 2] batch_size = 2 input_size = 3 @@ -551,41 +282,6 @@ def test_space_to_depth_operator(): parameter_R = ng.parameter(R_shape, name="R", dtype=np.float32) parameter_B = ng.parameter(B_shape, name="B", dtype=np.float32) - X_value = np.array( - [0.3432185, 0.612268, 0.20272376, 0.9513413, 0.30585995, 0.7265472], dtype=np.float32 - ).reshape(X_shape) - H_t_value = np.array( - [0.12444675, 0.52055854, 0.46489045, 0.4983964, 0.7730452, 0.28439692], dtype=np.float32 - ).reshape(H_t_shape) - W_value = np.array( - [ - 0.41930267, - 0.7872176, - 0.89940447, - 0.23659843, - 0.24676207, - 0.17101714, - 0.3147149, - 0.6555601, - 0.4559603, - ], - dtype=np.float32, - ).reshape(W_shape) - R_value = np.array( - [ - 0.8374871, - 0.86660194, - 0.82114047, - 0.71549815, - 0.18775631, - 0.3182116, - 0.25392973, - 0.38301638, - 0.85531586, - ], - dtype=np.float32, - ).reshape(R_shape) - B_value = np.array([1.0289404, 1.6362579, 0.4370661], dtype=np.float32).reshape(B_shape) activations = ["sigmoid"] activation_alpha = [] activation_beta = [] @@ -603,47 +299,32 @@ def test_space_to_depth_operator(): activation_beta, clip, ) - computation = runtime.computation( - model, parameter_X, parameter_H_t, parameter_W, parameter_R, parameter_B - ) - - result = computation(X_value, H_t_value, W_value, R_value, B_value) - expected = np.array( - [0.94126844, 0.9036043, 0.841243, 0.9468489, 0.934215, 0.873708], dtype=np.float32 - ).reshape(batch_size, hidden_size) - - assert np.allclose(result, expected) + assert model.get_type_name() == "RNNCell" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [batch_size, hidden_size] def test_group_convolution_operator(): - runtime = get_runtime() - data_shape = [1, 4, 2, 2] filters_shape = [2, 1, 2, 1, 1] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) parameter_filters = ng.parameter(filters_shape, name="Filters", dtype=np.float32) - data_value = np.arange(start=1.0, stop=17.0, dtype=np.float32).reshape(data_shape) - filters_value = np.arange(start=1.0, stop=5.0, dtype=np.float32).reshape(filters_shape) strides = [1, 1] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] model = ng.group_convolution(parameter_data, parameter_filters, strides, pads_begin, pads_end, dilations) - computation = runtime.computation(model, parameter_data, parameter_filters) - result = computation(data_value, filters_value) - - expected = np.array([11, 14, 17, 20, 79, 86, 93, 100], dtype=np.float32).reshape(1, 2, 2, 2) - - assert np.allclose(result, expected) + assert model.get_type_name() == "GroupConvolution" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [1, 2, 2, 2] -@pytest.mark.xfail(reason="Computation mismatch") def test_group_convolution_backprop_data(): - runtime = get_runtime() - data_shape = [1, 1, 3, 3] filters_shape = [1, 1, 1, 3, 3] strides = [2, 2] @@ -657,87 +338,13 @@ def test_group_convolution_backprop_data(): data_node, filters_node, strides, None, pads_begin, pads_end, output_padding=output_padding ) - data_value = np.array( - [ - 0.16857791, - -0.15161794, - 0.08540368, - 0.1820628, - -0.21746576, - 0.08245695, - 0.1431433, - -0.43156421, - 0.30591947, - ], - dtype=np.float32, - ).reshape(data_shape) - - filters_value = np.array( - [ - -0.06230065, - 0.37932432, - -0.25388849, - 0.33878803, - 0.43709868, - -0.22477469, - 0.04118127, - -0.44696793, - 0.06373066, - ], - dtype=np.float32, - ).reshape(filters_shape) - - computation = runtime.computation(model, data_node, filters_node) - result = computation(data_value, filters_value) - - expected = np.array( - [ - 0.07368518, - -0.08925839, - -0.06627201, - 0.06301362, - 0.03732984, - -0.01919658, - -0.00628807, - -0.02817563, - -0.01472169, - 0.04392925, - -0.00689478, - -0.01549204, - 0.07957941, - -0.11459791, - -0.09505399, - 0.07681622, - 0.03604182, - -0.01853423, - -0.0270785, - -0.00680824, - -0.06650258, - 0.08004665, - 0.07918708, - 0.0724144, - 0.06256775, - -0.17838378, - -0.18863615, - 0.20064656, - 0.133717, - -0.06876295, - -0.06398046, - -0.00864975, - 0.19289537, - -0.01490572, - -0.13673618, - 0.01949645, - ], - dtype=np.float32, - ).reshape(1, 1, 6, 6) - - assert np.allclose(result, expected) + assert model.get_type_name() == "GroupConvolutionBackpropData" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [1, 1, 6, 6] def test_group_convolution_backprop_data_output_shape(): - runtime = get_runtime() - data_shape = [1, 1, 1, 10] filters_shape = [1, 1, 1, 1, 5] strides = [1, 1] @@ -749,18 +356,7 @@ def test_group_convolution_backprop_data_output_shape(): model = ng.group_convolution_backprop_data( data_node, filters_node, strides, output_shape_node, auto_pad="same_upper" ) - - data_value = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=np.float32).reshape( - data_shape - ) - - filters_value = np.array([1.0, 2.0, 3.0, 2.0, 1.0], dtype=np.float32).reshape(filters_shape) - - computation = runtime.computation(model, data_node, filters_node) - result = computation(data_value, filters_value) - - expected = np.array( - [0.0, 1.0, 4.0, 10.0, 18.0, 27.0, 36.0, 45.0, 54.0, 63.0, 62.0, 50.0, 26.0, 9.0], dtype=np.float32, - ).reshape(1, 1, 1, 14) - - assert np.allclose(result, expected) + assert model.get_type_name() == "GroupConvolutionBackpropData" + assert model.get_output_size() == 1 + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [1, 1, 1, 14] diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_matmul.py b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_matmul.py index 4a8a9ebb31d..8c02d59ab9f 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_matmul.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_matmul.py @@ -5,36 +5,33 @@ import numpy as np import pytest import ngraph as ng -from tests_compatibility.test_ngraph.util import run_op_node +from ngraph.impl import Type @pytest.mark.parametrize( - "shape_a, shape_b, transpose_a, transpose_b", + ("shape_a", "shape_b", "transpose_a", "transpose_b", "expected_shape"), [ # matrix, vector - ([2, 4], [4], False, False), - ([4], [4, 2], False, False), + ([2, 4], [4], False, False, [2]), + ([4], [4, 2], False, False, [2]), # matrix, matrix - ([2, 4], [4, 2], False, False), + ([2, 4], [4, 2], False, False, [2, 2]), # tensor, vector - ([2, 4, 5], [5], False, False), + ([2, 4, 5], [5], False, False, [2, 4]), # # tensor, matrix - ([2, 4, 5], [5, 4], False, False), + ([2, 4, 5], [5, 4], False, False, [2, 4, 4]), # # tensor, tensor - ([2, 2, 4], [2, 4, 2], False, False), + ([2, 2, 4], [2, 4, 2], False, False, [2, 2, 2]), ], ) -def test_matmul(shape_a, shape_b, transpose_a, transpose_b): +def test_matmul(shape_a, shape_b, transpose_a, transpose_b, expected_shape): np.random.seed(133391) - left_input = -100.0 + np.random.rand(*shape_a).astype(np.float32) * 200.0 - right_input = -100.0 + np.random.rand(*shape_b).astype(np.float32) * 200.0 + left_input = np.random.rand(*shape_a).astype(np.float32) + right_input = np.random.rand(*shape_b).astype(np.float32) - result = run_op_node([left_input, right_input], ng.matmul, transpose_a, transpose_b) + node = ng.matmul(left_input, right_input, transpose_a, transpose_b) - if transpose_a: - left_input = np.transpose(left_input) - if transpose_b: - right_input = np.transpose(right_input) - - expected = np.matmul(left_input, right_input) - assert np.allclose(result, expected) + assert node.get_output_size() == 1 + assert node.get_type_name() == "MatMul" + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_multioutput.py b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_multioutput.py index 1129c9709da..5648f95f686 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_multioutput.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_multioutput.py @@ -4,33 +4,34 @@ import numpy as np import ngraph as ng -from tests_compatibility.runtime import get_runtime +from ngraph.impl import Type def test_split(): - runtime = get_runtime() input_tensor = ng.constant(np.array([0, 1, 2, 3, 4, 5], dtype=np.int32)) axis = ng.constant(0, dtype=np.int64) splits = 3 split_node = ng.split(input_tensor, axis, splits) - computation = runtime.computation(split_node) - split_results = computation() - expected_results = np.array([[0, 1], [2, 3], [4, 5]], dtype=np.int32) - assert np.allclose(split_results, expected_results) + assert split_node.get_type_name() == "Split" + assert split_node.get_output_size() == 3 + assert list(split_node.get_output_shape(0)) == [2] + assert list(split_node.get_output_shape(1)) == [2] + assert list(split_node.get_output_shape(2)) == [2] + assert split_node.get_output_element_type(0) == Type.i32 + assert split_node.get_output_element_type(1) == Type.i32 + assert split_node.get_output_element_type(2) == Type.i32 def test_variadic_split(): - runtime = get_runtime() input_tensor = ng.constant(np.array([[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]], dtype=np.int32)) axis = ng.constant(1, dtype=np.int64) splits = ng.constant(np.array([2, 4], dtype=np.int64)) v_split_node = ng.variadic_split(input_tensor, axis, splits) - computation = runtime.computation(v_split_node) - results = computation() - split0 = np.array([[0, 1], [6, 7]], dtype=np.int32) - split1 = np.array([[2, 3, 4, 5], [8, 9, 10, 11]], dtype=np.int32) - - assert np.allclose(results[0], split0) - assert np.allclose(results[1], split1) + assert v_split_node.get_type_name() == "VariadicSplit" + assert v_split_node.get_output_size() == 2 + assert list(v_split_node.get_output_shape(0)) == [2, 2] + assert list(v_split_node.get_output_shape(1)) == [2, 4] + assert v_split_node.get_output_element_type(0) == Type.i32 + assert v_split_node.get_output_element_type(1) == Type.i32 diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_reshape.py b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_reshape.py index 9a714a4efed..fbbbc42ac68 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_reshape.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_reshape.py @@ -1,36 +1,37 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 -import ngraph as ng import numpy as np import pytest -from tests_compatibility.runtime import get_runtime -from tests_compatibility.test_ngraph.util import run_op_node, run_op_numeric_data +import ngraph as ng +from ngraph.impl import Type +from ngraph.utils.types import get_element_type def test_concat(): a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6]]) axis = 0 - expected = np.concatenate((a, b), axis=0) - runtime = get_runtime() parameter_a = ng.parameter(list(a.shape), name="A", dtype=np.float32) parameter_b = ng.parameter(list(b.shape), name="B", dtype=np.float32) node = ng.concat([parameter_a, parameter_b], axis) - computation = runtime.computation(node, parameter_a, parameter_b) - result = computation(a, b) - assert np.allclose(result, expected) + assert node.get_type_name() == "Concat" + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == [3, 2] + assert node.get_output_element_type(0) == Type.f32 @pytest.mark.parametrize( - "val_type, value", [(bool, False), (bool, np.empty((2, 2), dtype=bool))] + ("val_type", "value", "output_shape"), [(bool, False, []), (bool, np.empty((2, 2), dtype=bool), [2, 2])] ) -def test_constant_from_bool(val_type, value): - expected = np.array(value, dtype=val_type) - result = run_op_numeric_data(value, ng.constant, val_type) - assert np.allclose(result, expected) +def test_constant_from_bool(val_type, value, output_shape): + node = ng.constant(value, val_type) + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.boolean + assert list(node.get_output_shape(0)) == output_shape @pytest.mark.parametrize( @@ -49,9 +50,11 @@ def test_constant_from_bool(val_type, value): ], ) def test_constant_from_scalar(val_type, value): - expected = np.array(value, dtype=val_type) - result = run_op_numeric_data(value, ng.constant, val_type) - assert np.allclose(result, expected) + node = ng.constant(value, val_type) + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(val_type) + assert list(node.get_output_shape(0)) == [] @pytest.mark.parametrize( @@ -64,8 +67,11 @@ def test_constant_from_scalar(val_type, value): def test_constant_from_float_array(val_type): np.random.seed(133391) input_data = np.array(-1 + np.random.rand(2, 3, 4) * 2, dtype=val_type) - result = run_op_numeric_data(input_data, ng.constant, val_type) - assert np.allclose(result, input_data) + node = ng.constant(input_data, val_type) + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(val_type) + assert list(node.get_output_shape(0)) == [2, 3, 4] @pytest.mark.parametrize( @@ -86,8 +92,11 @@ def test_constant_from_integer_array(val_type, range_start, range_end): input_data = np.array( np.random.randint(range_start, range_end, size=(2, 2)), dtype=val_type ) - result = run_op_numeric_data(input_data, ng.constant, val_type) - assert np.allclose(result, input_data) + node = ng.constant(input_data, val_type) + assert node.get_type_name() == "Constant" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == get_element_type(val_type) + assert list(node.get_output_shape(0)) == [2, 2] def test_broadcast_numpy(): @@ -126,27 +135,25 @@ def test_transpose(): ) input_order = np.array([0, 2, 3, 1], dtype=np.int32) - result = run_op_node([input_tensor], ng.transpose, input_order) - - expected = np.transpose(input_tensor, input_order) - - assert np.allclose(result, expected) + node = ng.transpose(input_tensor, input_order) + assert node.get_type_name() == "Transpose" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.i32 + assert list(node.get_output_shape(0)) == [3, 224, 224, 3] def test_tile(): input_tensor = np.arange(6, dtype=np.int32).reshape((2, 1, 3)) repeats = np.array([2, 1], dtype=np.int32) - result = run_op_node([input_tensor], ng.tile, repeats) + node = ng.tile(input_tensor, repeats) - expected = np.array([0, 1, 2, 0, 1, 2, 3, 4, 5, 3, 4, 5]).reshape((2, 2, 3)) - - assert np.allclose(result, expected) + assert node.get_type_name() == "Tile" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.i32 + assert list(node.get_output_shape(0)) == [2, 2, 3] -@pytest.mark.xfail( - reason="RuntimeError: Check 'shape_size(get_input_shape(0)) == shape_size(output_shape)'" -) def test_strided_slice(): input_tensor = np.arange(2 * 3 * 4, dtype=np.float32).reshape((2, 3, 4)) begin = np.array([1, 0], dtype=np.int32) @@ -158,9 +165,8 @@ def test_strided_slice(): shrink_axis_mask = np.array([1, 0, 0], dtype=np.int32) ellipsis_mask = np.array([0, 0, 0], dtype=np.int32) - result = run_op_node( - [input_tensor], - ng.strided_slice, + node = ng.strided_slice( + input_tensor, begin, end, strides, @@ -171,11 +177,10 @@ def test_strided_slice(): ellipsis_mask, ) - expected = np.array( - [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], dtype=np.float32 - ).reshape((1, 3, 4)) - - assert np.allclose(result, expected) + assert node.get_type_name() == "StridedSlice" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.f32 + assert list(node.get_output_shape(0)) == [1, 3, 4] def test_reshape_v1(): @@ -183,16 +188,18 @@ def test_reshape_v1(): shape = np.array([0, -1, 4], dtype=np.int32) special_zero = True - expected_shape = np.array([2, 150, 4]) - expected = np.reshape(A, expected_shape) - result = run_op_node([A], ng.reshape, shape, special_zero) - - assert np.allclose(result, expected) + node = ng.reshape(A, shape, special_zero) + assert node.get_type_name() == "Reshape" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.f32 + assert list(node.get_output_shape(0)) == [2, 150, 4] def test_shape_of(): input_tensor = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32) - result = run_op_node([input_tensor], ng.shape_of) - - assert np.allclose(result, [3, 3]) + node = ng.shape_of(input_tensor) + assert node.get_type_name() == "ShapeOf" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.i64 + assert list(node.get_output_shape(0)) == [2] diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_unary.py b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_unary.py index c7b2431ed63..c44cdc520b6 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_ops_unary.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_ops_unary.py @@ -6,129 +6,118 @@ import pytest import ngraph as ng from ngraph.impl import Shape, Type -from tests_compatibility.runtime import get_runtime -from tests_compatibility.test_ngraph.util import run_op_node R_TOLERANCE = 1e-6 # global relative tolerance @pytest.mark.parametrize( - "graph_api_fn, numpy_fn, range_start, range_end", + ("graph_api_fn", "type_name"), [ - (ng.absolute, np.abs, -1, 1), - (ng.abs, np.abs, -1, 1), - (ng.acos, np.arccos, -1, 1), - (ng.acosh, np.arccosh, 1, 2), - (ng.asin, np.arcsin, -1, 1), - (ng.asinh, np.arcsinh, -1, 1), - (ng.atan, np.arctan, -100.0, 100.0), - (ng.atanh, np.arctanh, 0.0, 1.0), - (ng.ceiling, np.ceil, -100.0, 100.0), - (ng.ceil, np.ceil, -100.0, 100.0), - (ng.cos, np.cos, -100.0, 100.0), - (ng.cosh, np.cosh, -100.0, 100.0), - (ng.exp, np.exp, -100.0, 100.0), - (ng.floor, np.floor, -100.0, 100.0), - (ng.log, np.log, 0, 100.0), - (ng.relu, lambda x: np.maximum(0, x), -100.0, 100.0), - (ng.sign, np.sign, -100.0, 100.0), - (ng.sin, np.sin, -100.0, 100.0), - (ng.sinh, np.sinh, -100.0, 100.0), - (ng.sqrt, np.sqrt, 0.0, 100.0), - (ng.tan, np.tan, -1.0, 1.0), - (ng.tanh, np.tanh, -100.0, 100.0), + (ng.absolute, "Abs"), + (ng.abs, "Abs"), + (ng.acos, "Acos"), + (ng.acosh, "Acosh"), + (ng.asin, "Asin"), + (ng.asinh, "Asinh"), + (ng.atan, "Atan"), + (ng.atanh, "Atanh"), + (ng.ceiling, "Ceiling"), + (ng.ceil, "Ceiling"), + (ng.cos, "Cos"), + (ng.cosh, "Cosh"), + (ng.exp, "Exp"), + (ng.floor, "Floor"), + (ng.log, "Log"), + (ng.relu, "Relu"), + (ng.sign, "Sign"), + (ng.sin, "Sin"), + (ng.sinh, "Sinh"), + (ng.sqrt, "Sqrt"), + (ng.tan, "Tan"), + (ng.tanh, "Tanh"), ], ) -def test_unary_op_array(graph_api_fn, numpy_fn, range_start, range_end): +def test_unary_op_array(graph_api_fn, type_name): np.random.seed(133391) - input_data = (range_start + np.random.rand(2, 3, 4) * (range_end - range_start)).astype(np.float32) - expected = numpy_fn(input_data) - - result = run_op_node([input_data], graph_api_fn) - assert np.allclose(result, expected, rtol=0.001) + input_data = np.random.rand(2, 3, 4).astype(np.float32) + node = graph_api_fn(input_data) + assert node.get_output_size() == 1 + assert node.get_type_name() == type_name + assert node.get_output_element_type(0) == Type.f32 + assert list(node.get_output_shape(0)) == [2, 3, 4] @pytest.mark.parametrize( - "graph_api_fn, numpy_fn, input_data", + ("graph_api_fn", "input_data"), [ - pytest.param(ng.absolute, np.abs, np.float32(-3)), - pytest.param(ng.abs, np.abs, np.float32(-3)), - pytest.param(ng.acos, np.arccos, np.float32(-0.5)), - pytest.param(ng.asin, np.arcsin, np.float32(-0.5)), - pytest.param(ng.atan, np.arctan, np.float32(-0.5)), - pytest.param(ng.ceiling, np.ceil, np.float32(1.5)), - pytest.param(ng.ceil, np.ceil, np.float32(1.5)), - pytest.param(ng.cos, np.cos, np.float32(np.pi / 4.0)), - pytest.param(ng.cosh, np.cosh, np.float32(np.pi / 4.0)), - pytest.param(ng.exp, np.exp, np.float32(1.5)), - pytest.param(ng.floor, np.floor, np.float32(1.5)), - pytest.param(ng.log, np.log, np.float32(1.5)), - pytest.param(ng.relu, lambda x: np.maximum(0, x), np.float32(-0.125)), - pytest.param(ng.sign, np.sign, np.float32(0.0)), - pytest.param(ng.sin, np.sin, np.float32(np.pi / 4.0)), - pytest.param(ng.sinh, np.sinh, np.float32(0.0)), - pytest.param(ng.sqrt, np.sqrt, np.float32(3.5)), - pytest.param(ng.tan, np.tan, np.float32(np.pi / 4.0)), - pytest.param(ng.tanh, np.tanh, np.float32(0.1234)), + pytest.param(ng.absolute, np.float32(-3)), + pytest.param(ng.abs, np.float32(-3)), + pytest.param(ng.acos, np.float32(-0.5)), + pytest.param(ng.asin, np.float32(-0.5)), + pytest.param(ng.atan, np.float32(-0.5)), + pytest.param(ng.ceiling, np.float32(1.5)), + pytest.param(ng.ceil, np.float32(1.5)), + pytest.param(ng.cos, np.float32(np.pi / 4.0)), + pytest.param(ng.cosh, np.float32(np.pi / 4.0)), + pytest.param(ng.exp, np.float32(1.5)), + pytest.param(ng.floor, np.float32(1.5)), + pytest.param(ng.log, np.float32(1.5)), + pytest.param(ng.relu, np.float32(-0.125)), + pytest.param(ng.sign, np.float32(0.0)), + pytest.param(ng.sin, np.float32(np.pi / 4.0)), + pytest.param(ng.sinh, np.float32(0.0)), + pytest.param(ng.sqrt, np.float32(3.5)), + pytest.param(ng.tan, np.float32(np.pi / 4.0)), + pytest.param(ng.tanh, np.float32(0.1234)), ], ) -def test_unary_op_scalar(graph_api_fn, numpy_fn, input_data): - expected = numpy_fn(input_data) +def test_unary_op_scalar(graph_api_fn, input_data): + node = graph_api_fn(input_data) - result = run_op_node([input_data], graph_api_fn) - assert np.allclose(result, expected) + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.f32 + assert list(node.get_output_shape(0)) == [] @pytest.mark.parametrize( "input_data", [(np.array([True, False, True, False])), (np.array([True])), (np.array([False]))] ) def test_logical_not(input_data): - expected = np.logical_not(input_data) - - result = run_op_node([input_data], ng.logical_not) - assert np.allclose(result, expected) + node = ng.logical_not(input_data) + assert node.get_output_size() == 1 + assert node.get_type_name() == "LogicalNot" + assert node.get_output_element_type(0) == Type.boolean + assert list(node.get_output_shape(0)) == list(input_data.shape) def test_sigmoid(): input_data = np.array([-3.14, -1.0, 0.0, 2.71001, 1000.0], dtype=np.float32) - result = run_op_node([input_data], ng.sigmoid) + node = ng.sigmoid(input_data) - def sigmoid(x): - return 1.0 / (1.0 + np.exp(-x)) - - expected = np.array(list(map(sigmoid, input_data))) - - assert np.allclose(result, expected) + assert node.get_output_size() == 1 + assert node.get_type_name() == "Sigmoid" + assert node.get_output_element_type(0) == Type.f32 + assert list(node.get_output_shape(0)) == [5] -def test_softmax_positive_axis(): +def test_softmax(): axis = 1 input_tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) - result = run_op_node([input_tensor], ng.softmax, axis) - - expected = [[0.09003056, 0.24472842, 0.6652409], [0.09003056, 0.24472842, 0.6652409]] - - assert np.allclose(result, expected) - - -def test_softmax_negative_axis(): - axis = -1 - input_tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) - - result = run_op_node([input_tensor], ng.softmax, axis) - - expected = [[0.09003056, 0.24472842, 0.6652409], [0.09003056, 0.24472842, 0.6652409]] - - assert np.allclose(result, expected) + node = ng.softmax(input_tensor, axis) + assert node.get_output_size() == 1 + assert node.get_type_name() == "Softmax" + assert node.get_output_element_type(0) == Type.f32 + assert list(node.get_output_shape(0)) == [2, 3] def test_erf(): input_tensor = np.array([-1.0, 0.0, 1.0, 2.5, 3.14, 4.0], dtype=np.float32) - expected = [-0.842701, 0.0, 0.842701, 0.999593, 0.999991, 1.0] - - result = run_op_node([input_tensor], ng.erf) - assert np.allclose(result, expected) + node = ng.erf(input_tensor) + assert node.get_output_size() == 1 + assert node.get_type_name() == "Erf" + assert node.get_output_element_type(0) == Type.f32 + assert list(node.get_output_shape(0)) == [6] def test_hswish(): @@ -152,29 +141,6 @@ def test_round_even(): assert list(node.get_output_shape(0)) == [3, 10] assert node.get_output_element_type(0) == Type.f32 - input_tensor = np.array([-2.5, -1.5, -0.5, 0.5, 0.9, 1.5, 2.3, 2.5, 3.5], dtype=np.float32) - expected = [-2.0, -2.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 4.0] - - result = run_op_node([input_tensor], ng.round, "HALF_TO_EVEN") - assert np.allclose(result, expected) - - -def test_round_away(): - float_dtype = np.float32 - data = ng.parameter(Shape([3, 10]), dtype=float_dtype, name="data") - - node = ng.round(data, "HALF_AWAY_FROM_ZERO") - assert node.get_type_name() == "Round" - assert node.get_output_size() == 1 - assert list(node.get_output_shape(0)) == [3, 10] - assert node.get_output_element_type(0) == Type.f32 - - input_tensor = np.array([-2.5, -1.5, -0.5, 0.5, 0.9, 1.5, 2.3, 2.5, 3.5], dtype=np.float32) - expected = [-3.0, -2.0, -1.0, 1.0, 1.0, 2.0, 2.0, 3.0, 4.0] - - result = run_op_node([input_tensor], ng.round, "HALF_AWAY_FROM_ZERO") - assert np.allclose(result, expected) - def test_hsigmoid(): float_dtype = np.float32 @@ -188,92 +154,42 @@ def test_hsigmoid(): def test_gelu_operator_with_parameters(): - runtime = get_runtime() - - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) - data_shape = [2, 2] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.gelu(parameter_data, "erf") - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array([[-1.6391277e-06, 8.4134471e-01], [-4.5500278e-02, 2.9959502]], dtype=np.float32) - assert np.allclose(result, expected, 1e-6, 1e-6) + assert model.get_output_size() == 1 + assert model.get_type_name() == "Gelu" + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [2, 2] def test_gelu_operator_with_array(): - runtime = get_runtime() - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) model = ng.gelu(data_value, "erf") - computation = runtime.computation(model) - - result = computation() - expected = np.array([[-1.6391277e-06, 8.4134471e-01], [-4.5500278e-02, 2.9959502]], dtype=np.float32) - assert np.allclose(result, expected, 1e-6, 1e-6) + assert model.get_output_size() == 1 + assert model.get_type_name() == "Gelu" + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [2, 2] def test_gelu_tanh_operator_with_parameters(): - runtime = get_runtime() - - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) - data_shape = [2, 2] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.gelu(parameter_data, "tanh") - computation = runtime.computation(model, parameter_data) - - result = computation(data_value) - expected = np.array([[0.0, 0.841192], [-0.04540223, 2.9963627]], dtype=np.float32) - assert np.allclose(result, expected, 1e-6, 1e-6) + assert model.get_output_size() == 1 + assert model.get_type_name() == "Gelu" + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [2, 2] def test_gelu_tanh_operator_with_array(): - runtime = get_runtime() - data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) model = ng.gelu(data_value, "tanh") - computation = runtime.computation(model) - - result = computation() - expected = np.array([[0.0, 0.841192], [-0.04540223, 2.9963627]], dtype=np.float32) - - assert np.allclose(result, expected, 1e-6, 1e-6) - - -type_tolerance = [ - (np.float64, 1e-6), - (np.float32, 1e-6), - (np.float16, 1e-3), -] - - -@pytest.mark.parametrize("type_tolerance", type_tolerance) -def test_softsign_with_parameters(type_tolerance): - dtype, atol = type_tolerance - data = np.random.uniform(-1.0, 1.0, (32, 5)).astype(dtype) - - expected = np.divide(data, np.abs(data) + 1) - - runtime = get_runtime() - param = ng.parameter(data.shape, dtype, name="Data") - result = runtime.computation(ng.softsign(param, "SoftSign"), param)(data) - - assert np.allclose(result, expected, R_TOLERANCE, atol) - - -@pytest.mark.parametrize("type_tolerance", type_tolerance) -def test_softsign_with_array(type_tolerance): - dtype, atol = type_tolerance - data = np.random.uniform(-1.0, 1.0, (32, 5)).astype(dtype) - expected = np.divide(data, np.abs(data) + 1) - - runtime = get_runtime() - result = runtime.computation(ng.softsign(data, "SoftSign"))() - - assert np.allclose(result, expected, R_TOLERANCE, atol) + assert model.get_output_size() == 1 + assert model.get_type_name() == "Gelu" + assert model.get_output_element_type(0) == Type.f32 + assert list(model.get_output_shape(0)) == [2, 2] diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_pooling.py b/src/bindings/python/tests_compatibility/test_ngraph/test_pooling.py index b7071b839bc..a729e90d272 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_pooling.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_pooling.py @@ -5,7 +5,7 @@ import numpy as np import pytest import ngraph as ng -from tests_compatibility.runtime import get_runtime +from ngraph.impl import Type @pytest.fixture @@ -14,7 +14,6 @@ def _ndarray_1x1x4x4(): def test_avg_pool_2d(_ndarray_1x1x4x4): - runtime = get_runtime() input_data = _ndarray_1x1x4x4 param = ng.parameter(input_data.shape, name="A", dtype=np.float32) @@ -24,41 +23,15 @@ def test_avg_pool_2d(_ndarray_1x1x4x4): pads_end = [0] * spatial_dim_count strides = [2, 2] exclude_pad = True - expected = [[[[13.5, 15.5], [21.5, 23.5]]]] avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) - computation = runtime.computation(avg_pool_node, param) - result = computation(input_data) - assert np.allclose(result, expected) - - expected = [[[[13.5, 14.5, 15.5], [17.5, 18.5, 19.5], [21.5, 22.5, 23.5]]]] - strides = [1, 1] - avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) - computation = runtime.computation(avg_pool_node, param) - result = computation(input_data) - assert np.allclose(result, expected) - - pads_begin = [1, 1] - pads_end = [1, 1] - strides = [2, 2] - exclude_pad = True - - expected = [[[[11.0, 12.5, 14.0], [17.0, 18.5, 20.0], [23.0, 24.5, 26.0]]]] - avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) - computation = runtime.computation(avg_pool_node, param) - result = computation(input_data) - assert np.allclose(result, expected) - - exclude_pad = False - expected = [[[[2.75, 6.25, 3.5], [8.5, 18.5, 10.0], [5.75, 12.25, 6.5]]]] - avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) - computation = runtime.computation(avg_pool_node, param) - result = computation(input_data) - assert np.allclose(result, expected) + assert avg_pool_node.get_type_name() == "AvgPool" + assert avg_pool_node.get_output_size() == 1 + assert list(avg_pool_node.get_output_shape(0)) == [1, 1, 2, 2] + assert avg_pool_node.get_output_element_type(0) == Type.f32 def test_avg_pooling_3d(_ndarray_1x1x4x4): - rt = get_runtime() data = _ndarray_1x1x4x4 data = np.broadcast_to(data, (1, 1, 4, 4, 4)) param = ng.parameter(list(data.shape)) @@ -70,19 +43,13 @@ def test_avg_pooling_3d(_ndarray_1x1x4x4): exclude_pad = True avgpool = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad) - comp = rt.computation(avgpool, param) - result = comp(data) - result_ref = [[[[[13.5, 15.5], [21.5, 23.5]], [[13.5, 15.5], [21.5, 23.5]]]]] - assert np.allclose(result, result_ref) + assert avgpool.get_type_name() == "AvgPool" + assert avgpool.get_output_size() == 1 + assert list(avgpool.get_output_shape(0)) == [1, 1, 2, 2, 2] + assert avgpool.get_output_element_type(0) == Type.f32 def test_max_pool_basic(): - rt = get_runtime() - - # array([[[[ 0.5, 1.5, 2.5, 3.5], - # [ 4.5, 5.5, 6.5, 7.5], - # [ 8.5, 9.5, 10.5, 11.5], - # [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] @@ -105,24 +72,15 @@ def test_max_pool_basic(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array( - [[[[5.5, 6.5, 7.5], [9.5, 10.5, 11.5], [13.5, 14.5, 15.5]]]], dtype=np.float32 - ) - expected_idx = np.array([[[[5, 6, 7], [9, 10, 11], [13, 14, 15]]]], dtype=np.int32) - assert np.allclose(result[0], expected) - assert np.allclose(result[1], expected_idx) + assert maxpool_node.get_type_name() == "MaxPool" + assert maxpool_node.get_output_size() == 2 + assert list(maxpool_node.get_output_shape(0)) == [1, 1, 3, 3] + assert list(maxpool_node.get_output_shape(1)) == [1, 1, 3, 3] + assert maxpool_node.get_output_element_type(0) == Type.f32 + assert maxpool_node.get_output_element_type(1) == Type.i32 def test_max_pool_strides(): - rt = get_runtime() - - # array([[[[ 0.5, 1.5, 2.5, 3.5], - # [ 4.5, 5.5, 6.5, 7.5], - # [ 8.5, 9.5, 10.5, 11.5], - # [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [2, 1] dilations = [1, 1] @@ -145,22 +103,15 @@ def test_max_pool_strides(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array([[[[5.5, 6.5, 7.5], [13.5, 14.5, 15.5]]]], dtype=np.float32) - expected_idx = np.array([[[[5, 6, 7], [13, 14, 15]]]], dtype=np.int32) - assert np.allclose(result[0], expected) - assert np.allclose(result[1], expected_idx) + assert maxpool_node.get_type_name() == "MaxPool" + assert maxpool_node.get_output_size() == 2 + assert list(maxpool_node.get_output_shape(0)) == [1, 1, 2, 3] + assert list(maxpool_node.get_output_shape(1)) == [1, 1, 2, 3] + assert maxpool_node.get_output_element_type(0) == Type.f32 + assert maxpool_node.get_output_element_type(1) == Type.i32 def test_max_pool_kernel_shape1x1(): - rt = get_runtime() - - # array([[[[ 0.5, 1.5, 2.5, 3.5], - # [ 4.5, 5.5, 6.5, 7.5], - # [ 8.5, 9.5, 10.5, 11.5], - # [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] @@ -183,20 +134,15 @@ def test_max_pool_kernel_shape1x1(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - assert np.allclose(result[0], data) - assert np.allclose(result[1], np.arange(0, 16, dtype=np.int32).reshape((1, 1, 4, 4))) + assert maxpool_node.get_type_name() == "MaxPool" + assert maxpool_node.get_output_size() == 2 + assert list(maxpool_node.get_output_shape(0)) == [1, 1, 4, 4] + assert list(maxpool_node.get_output_shape(1)) == [1, 1, 4, 4] + assert maxpool_node.get_output_element_type(0) == Type.f32 + assert maxpool_node.get_output_element_type(1) == Type.i32 def test_max_pool_kernel_shape3x3(): - rt = get_runtime() - - # array([[[[ 0.5, 1.5, 2.5, 3.5], - # [ 4.5, 5.5, 6.5, 7.5], - # [ 8.5, 9.5, 10.5, 11.5], - # [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] @@ -219,31 +165,20 @@ def test_max_pool_kernel_shape3x3(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array([[[[10.5, 11.5], [14.5, 15.5]]]], dtype=np.float32) - assert np.allclose(result[0], expected) + assert maxpool_node.get_type_name() == "MaxPool" + assert maxpool_node.get_output_size() == 2 + assert list(maxpool_node.get_output_shape(0)) == [1, 1, 2, 2] + assert list(maxpool_node.get_output_shape(1)) == [1, 1, 2, 2] + assert maxpool_node.get_output_element_type(0) == Type.f32 + assert maxpool_node.get_output_element_type(1) == Type.i32 def test_max_pool_non_zero_pads(): - rt = get_runtime() - - # array([[[[ 0.5, 1.5, 2.5, 3.5], - # [ 4.5, 5.5, 6.5, 7.5], - # [ 8.5, 9.5, 10.5, 11.5], - # [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] pads_begin = [1, 1] pads_end = [1, 1] - # 0 0 , 0 , 0 , 0, 0 - # 0 [ 0.5, 1.5, 2.5, 3.5], 0, - # 0 [ 4.5, 5.5, 6.5, 7.5], 0, - # 0 [ 8.5, 9.5, 10.5, 11.5], 0, - # 0 [12.5, 13.5, 14.5, 15.5], 0 - # 0 0 , 0 , 0 , 0, 0 kernel_shape = [2, 2] rounding_type = "floor" auto_pad = None @@ -261,58 +196,20 @@ def test_max_pool_non_zero_pads(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array( - [ - [ - [ - [0.5, 1.5, 2.5, 3.5, 3.5], - [4.5, 5.5, 6.5, 7.5, 7.5], - [8.5, 9.5, 10.5, 11.5, 11.5], - [12.5, 13.5, 14.5, 15.5, 15.5], - [12.5, 13.5, 14.5, 15.5, 15.5], - ] - ] - ], - dtype=np.float32, - ) - expected_idx = np.array( - [ - [ - [ - [0, 1, 2, 3, 3], - [4, 5, 6, 7, 7], - [8, 9, 10, 11, 11], - [12, 13, 14, 15, 15], - [12, 13, 14, 15, 15], - ] - ] - ], - dtype=np.int32, - ) - assert np.allclose(result[0], expected) - assert np.allclose(result[1], expected_idx) + assert maxpool_node.get_type_name() == "MaxPool" + assert maxpool_node.get_output_size() == 2 + assert list(maxpool_node.get_output_shape(0)) == [1, 1, 5, 5] + assert list(maxpool_node.get_output_shape(1)) == [1, 1, 5, 5] + assert maxpool_node.get_output_element_type(0) == Type.f32 + assert maxpool_node.get_output_element_type(1) == Type.i32 def test_max_pool_same_upper_auto_pads(): - rt = get_runtime() - - # array([[[[ 0.5, 1.5, 2.5, 3.5], - # [ 4.5, 5.5, 6.5, 7.5], - # [ 8.5, 9.5, 10.5, 11.5], - # [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] - # [ 0.5, 1.5, 2.5, 3.5], 0, - # [ 4.5, 5.5, 6.5, 7.5], 0, - # [ 8.5, 9.5, 10.5, 11.5], 0, - # [12.5, 13.5, 14.5, 15.5], 0 - # 0 , 0 , 0 , 0, 0 kernel_shape = [2, 2] auto_pad = "same_upper" rounding_type = "floor" @@ -330,56 +227,20 @@ def test_max_pool_same_upper_auto_pads(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array( - [ - [ - [ - [5.5, 6.5, 7.5, 7.5], - [9.5, 10.5, 11.5, 11.5], - [13.5, 14.5, 15.5, 15.5], - [13.5, 14.5, 15.5, 15.5], - ] - ] - ], - dtype=np.float32, - ) - expected_idx = np.array( - [ - [ - [ - [5, 6, 7, 7], - [9, 10, 11, 11], - [13, 14, 15, 15], - [13, 14, 15, 15], - ] - ] - ], - dtype=np.int32, - ) - assert np.allclose(result[0], expected) - assert np.allclose(result[1], expected_idx) + assert maxpool_node.get_type_name() == "MaxPool" + assert maxpool_node.get_output_size() == 2 + assert list(maxpool_node.get_output_shape(0)) == [1, 1, 4, 4] + assert list(maxpool_node.get_output_shape(1)) == [1, 1, 4, 4] + assert maxpool_node.get_output_element_type(0) == Type.f32 + assert maxpool_node.get_output_element_type(1) == Type.i32 def test_max_pool_same_lower_auto_pads(): - rt = get_runtime() - - # array([[[[ 0.5, 1.5, 2.5, 3.5], - # [ 4.5, 5.5, 6.5, 7.5], - # [ 8.5, 9.5, 10.5, 11.5], - # [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] - # 0 0 , 0 , 0 , 0, - # 0 [ 0.5, 1.5, 2.5, 3.5], - # 0 [ 4.5, 5.5, 6.5, 7.5], - # 0 [ 8.5, 9.5, 10.5, 11.5], - # 0 [12.5, 13.5, 14.5, 15.5], kernel_shape = [2, 2] auto_pad = "same_lower" rounding_type = "floor" @@ -397,34 +258,9 @@ def test_max_pool_same_lower_auto_pads(): auto_pad, index_et, ) - comp = rt.computation(maxpool_node, data_node) - result = comp(data) - - expected = np.array( - [ - [ - [ - [0.5, 1.5, 2.5, 3.5], - [4.5, 5.5, 6.5, 7.5], - [8.5, 9.5, 10.5, 11.5], - [12.5, 13.5, 14.5, 15.5], - ] - ] - ], - dtype=np.float32, - ) - expected_idx = np.array( - [ - [ - [ - [0, 1, 2, 3], - [4, 5, 6, 7], - [8, 9, 10, 11], - [12, 13, 14, 15], - ] - ] - ], - dtype=np.int32, - ) - assert np.allclose(result[0], expected) - assert np.allclose(result[1], expected_idx) + assert maxpool_node.get_type_name() == "MaxPool" + assert maxpool_node.get_output_size() == 2 + assert list(maxpool_node.get_output_shape(0)) == [1, 1, 4, 4] + assert list(maxpool_node.get_output_shape(1)) == [1, 1, 4, 4] + assert maxpool_node.get_output_element_type(0) == Type.f32 + assert maxpool_node.get_output_element_type(1) == Type.i32 diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_random_uniform.py b/src/bindings/python/tests_compatibility/test_ngraph/test_random_uniform.py index e05d67adaec..4ceb23e00cd 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_random_uniform.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_random_uniform.py @@ -2,12 +2,12 @@ # SPDX-License-Identifier: Apache-2.0 import ngraph as ng +from ngraph.impl import Type + import numpy as np -from tests_compatibility.runtime import get_runtime def test_random_uniform(): - runtime = get_runtime() input_tensor = ng.constant(np.array([2, 4, 3], dtype=np.int32)) min_val = ng.constant(np.array([-2.7], dtype=np.float32)) max_val = ng.constant(np.array([3.5], dtype=np.float32)) @@ -15,16 +15,10 @@ def test_random_uniform(): random_uniform_node = ng.random_uniform(input_tensor, min_val, max_val, output_type="f32", global_seed=7461, op_seed=1546) - computation = runtime.computation(random_uniform_node) - random_uniform_results = computation() - expected_results = np.array([[[2.8450181, -2.3457108, 2.2134445], - [-1.0436587, 0.79548645, 1.3023183], - [0.34447956, -2.0267959, 1.3989122], - [0.9607613, 1.5363653, 3.117298]], - - [[1.570041, 2.2782724, 2.3193843], - [3.3393657, 0.63299894, 0.41231918], - [3.1739233, 0.03919673, -0.2136085], - [-1.4519991, -2.277353, 2.630727]]], dtype=np.float32) - - assert np.allclose(random_uniform_results, expected_results) + random_uniform_node = ng.random_uniform(input_tensor, min_val, max_val, + output_type="f32", global_seed=7461, + op_seed=1546) + assert random_uniform_node.get_output_size() == 1 + assert random_uniform_node.get_type_name() == "RandomUniform" + assert random_uniform_node.get_output_element_type(0) == Type.f32 + assert list(random_uniform_node.get_output_shape(0)) == [2, 4, 3] diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_reduction.py b/src/bindings/python/tests_compatibility/test_ngraph/test_reduction.py index 2b266c72bae..f102e07b23b 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_reduction.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_reduction.py @@ -3,60 +3,59 @@ import numpy as np import pytest -from _pyngraph import PartialShape, Dimension import ngraph as ng -from ngraph.utils.types import make_constant_node -from tests_compatibility.runtime import get_runtime -from tests_compatibility.test_ngraph.util import run_op_node +from ngraph.impl import Type @pytest.mark.parametrize( - "ng_api_helper, numpy_function, reduction_axes", + ("ng_api_helper", "reduction_axes", "expected_shape"), [ - (ng.reduce_max, np.max, np.array([0, 1, 2, 3])), - (ng.reduce_min, np.min, np.array([0, 1, 2, 3])), - (ng.reduce_sum, np.sum, np.array([0, 1, 2, 3])), - (ng.reduce_prod, np.prod, np.array([0, 1, 2, 3])), - (ng.reduce_max, np.max, np.array([0])), - (ng.reduce_min, np.min, np.array([0])), - (ng.reduce_sum, np.sum, np.array([0])), - (ng.reduce_prod, np.prod, np.array([0])), - (ng.reduce_max, np.max, np.array([0, 2])), - (ng.reduce_min, np.min, np.array([0, 2])), - (ng.reduce_sum, np.sum, np.array([0, 2])), - (ng.reduce_prod, np.prod, np.array([0, 2])), + (ng.reduce_max, np.array([0, 1, 2, 3]), []), + (ng.reduce_min, np.array([0, 1, 2, 3]), []), + (ng.reduce_sum, np.array([0, 1, 2, 3]), []), + (ng.reduce_prod, np.array([0, 1, 2, 3]), []), + (ng.reduce_max, np.array([0]), [4, 3, 2]), + (ng.reduce_min, np.array([0]), [4, 3, 2]), + (ng.reduce_sum, np.array([0]), [4, 3, 2]), + (ng.reduce_prod, np.array([0]), [4, 3, 2]), + (ng.reduce_max, np.array([0, 2]), [4, 2]), + (ng.reduce_min, np.array([0, 2]), [4, 2]), + (ng.reduce_sum, np.array([0, 2]), [4, 2]), + (ng.reduce_prod, np.array([0, 2]), [4, 2]), ], ) -def test_reduction_ops(ng_api_helper, numpy_function, reduction_axes): +def test_reduction_ops(ng_api_helper, reduction_axes, expected_shape): shape = [2, 4, 3, 2] np.random.seed(133391) input_data = np.random.randn(*shape).astype(np.float32) - expected = numpy_function(input_data, axis=tuple(reduction_axes)) - result = run_op_node([input_data], ng_api_helper, reduction_axes) - assert np.allclose(result, expected) + node = ng_api_helper(input_data, reduction_axes) + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 @pytest.mark.parametrize( - "ng_api_helper, numpy_function, reduction_axes", + ("ng_api_helper", "reduction_axes", "expected_shape"), [ - (ng.reduce_logical_and, np.logical_and.reduce, np.array([0])), - (ng.reduce_logical_or, np.logical_or.reduce, np.array([0])), - (ng.reduce_logical_and, np.logical_and.reduce, np.array([0, 2])), - (ng.reduce_logical_or, np.logical_or.reduce, np.array([0, 2])), - (ng.reduce_logical_and, np.logical_and.reduce, np.array([0, 1, 2, 3])), - (ng.reduce_logical_or, np.logical_or.reduce, np.array([0, 1, 2, 3])), + (ng.reduce_logical_and, np.array([0]), [4, 3, 2]), + (ng.reduce_logical_or, np.array([0]), [4, 3, 2]), + (ng.reduce_logical_and, np.array([0, 2]), [4, 2]), + (ng.reduce_logical_or, np.array([0, 2]), [4, 2]), + (ng.reduce_logical_and, np.array([0, 1, 2, 3]), []), + (ng.reduce_logical_or, np.array([0, 1, 2, 3]), []), ], ) -def test_reduction_logical_ops(ng_api_helper, numpy_function, reduction_axes): +def test_reduction_logical_ops(ng_api_helper, reduction_axes, expected_shape): shape = [2, 4, 3, 2] np.random.seed(133391) input_data = np.random.randn(*shape).astype(np.bool) - expected = numpy_function(input_data, axis=tuple(reduction_axes)) - result = run_op_node([input_data], ng_api_helper, reduction_axes) - assert np.allclose(result, expected) + node = ng_api_helper(input_data, reduction_axes) + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.boolean def test_topk(): @@ -69,24 +68,27 @@ def test_topk(): assert node.get_output_size() == 2 assert list(node.get_output_shape(0)) == [6, 3, 10, 24] assert list(node.get_output_shape(1)) == [6, 3, 10, 24] + assert node.get_output_element_type(0) == Type.f32 + assert node.get_output_element_type(1) == Type.i32 @pytest.mark.parametrize( - "ng_api_helper, numpy_function, reduction_axes", + ("ng_api_helper", "reduction_axes", "expected_shape"), [ - (ng.reduce_mean, np.mean, np.array([0, 1, 2, 3])), - (ng.reduce_mean, np.mean, np.array([0])), - (ng.reduce_mean, np.mean, np.array([0, 2])), + (ng.reduce_mean, np.array([0, 1, 2, 3]), []), + (ng.reduce_mean, np.array([0]), [4, 3, 2]), + (ng.reduce_mean, np.array([0, 2]), [4, 2]), ], ) -def test_reduce_mean_op(ng_api_helper, numpy_function, reduction_axes): +def test_reduce_mean_op(ng_api_helper, reduction_axes, expected_shape): shape = [2, 4, 3, 2] np.random.seed(133391) input_data = np.random.randn(*shape).astype(np.float32) - expected = numpy_function(input_data, axis=tuple(reduction_axes)) - result = run_op_node([input_data], ng_api_helper, reduction_axes) - assert np.allclose(result, expected) + node = ng_api_helper(input_data, reduction_axes) + assert node.get_output_size() == 1 + assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 def test_non_zero(): @@ -99,6 +101,7 @@ def test_non_zero(): assert node.get_type_name() == "NonZero" assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.i64 def test_roi_align(): @@ -131,6 +134,7 @@ def test_roi_align(): assert node.get_type_name() == "ROIAlign" assert node.get_output_size() == 1 assert list(node.get_output_shape(0)) == expected_shape + assert node.get_output_element_type(0) == Type.f32 @pytest.mark.parametrize( @@ -140,16 +144,11 @@ def test_roi_align(): def test_cum_sum(input_shape, cumsum_axis, reverse): input_data = np.arange(np.prod(input_shape)).reshape(input_shape) - if reverse: - expected = np.cumsum(input_data[::-1], axis=cumsum_axis)[::-1] - else: - expected = np.cumsum(input_data, axis=cumsum_axis) - - runtime = get_runtime() node = ng.cum_sum(input_data, cumsum_axis, reverse=reverse) - computation = runtime.computation(node) - result = computation() - assert np.allclose(result, expected) + assert node.get_output_size() == 1 + assert node.get_type_name() == "CumSum" + assert list(node.get_output_shape(0)) == input_shape + assert node.get_output_element_type(0) == Type.i64 def test_normalize_l2(): @@ -160,38 +159,8 @@ def test_normalize_l2(): eps = 1e-6 eps_mode = "add" - runtime = get_runtime() node = ng.normalize_l2(input_data, axes, eps, eps_mode) - computation = runtime.computation(node) - result = computation() - - expected = np.array( - [ - 0.01428571, - 0.02857143, - 0.04285714, - 0.05714286, - 0.07142857, - 0.08571429, - 0.1, - 0.11428571, - 0.12857144, - 0.14285715, - 0.15714286, - 0.17142858, - 0.18571429, - 0.2, - 0.21428572, - 0.22857143, - 0.24285714, - 0.25714287, - 0.27142859, - 0.2857143, - 0.30000001, - 0.31428573, - 0.32857144, - 0.34285715, - ] - ).reshape(input_shape) - - assert np.allclose(result, expected) + assert node.get_output_size() == 1 + assert node.get_type_name() == "NormalizeL2" + assert list(node.get_output_shape(0)) == input_shape + assert node.get_output_element_type(0) == Type.f32 diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_roll.py b/src/bindings/python/tests_compatibility/test_ngraph/test_roll.py index 7b97201145a..10ad9382c21 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_roll.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_roll.py @@ -2,20 +2,19 @@ # SPDX-License-Identifier: Apache-2.0 import ngraph as ng +from ngraph.impl import Type + import numpy as np -from tests_compatibility.runtime import get_runtime def test_roll(): - runtime = get_runtime() input = np.reshape(np.arange(10), (2, 5)) input_tensor = ng.constant(input) input_shift = ng.constant(np.array([-10, 7], dtype=np.int32)) input_axes = ng.constant(np.array([-1, 0], dtype=np.int32)) roll_node = ng.roll(input_tensor, input_shift, input_axes) - computation = runtime.computation(roll_node) - roll_results = computation() - expected_results = np.roll(input, shift=(-10, 7), axis=(-1, 0)) - - assert np.allclose(roll_results, expected_results) + assert roll_node.get_output_size() == 1 + assert roll_node.get_type_name() == "Roll" + assert list(roll_node.get_output_shape(0)) == [2, 5] + assert roll_node.get_output_element_type(0) == Type.i64 diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_sequence_processing.py b/src/bindings/python/tests_compatibility/test_ngraph/test_sequence_processing.py index 028bde81531..0503df99f05 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_sequence_processing.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_sequence_processing.py @@ -4,17 +4,13 @@ import numpy as np import ngraph as ng -from tests_compatibility.runtime import get_runtime -from tests_compatibility.test_ngraph.util import run_op_node +from ngraph.impl import Type def test_onehot(): - runtime = get_runtime() param = ng.parameter([3], dtype=np.int32) model = ng.one_hot(param, 3, 1, 0, 0) - computation = runtime.computation(model, param) - - expected = np.eye(3)[np.array([1, 0, 2])] - input_data = np.array([1, 0, 2], dtype=np.int32) - result = computation(input_data) - assert np.allclose(result, expected) + assert model.get_output_size() == 1 + assert model.get_type_name() == "OneHot" + assert list(model.get_output_shape(0)) == [3, 3] + assert model.get_output_element_type(0) == Type.i64 diff --git a/src/bindings/python/tests_compatibility/test_ngraph/test_utils.py b/src/bindings/python/tests_compatibility/test_ngraph/test_utils.py index 0178296e938..d8e3ded2d7f 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/test_utils.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/test_utils.py @@ -7,7 +7,7 @@ from ngraph.impl import Shape def test_get_constant_from_source_success(): - dtype = np.int + dtype = np.int32 input1 = ng.parameter(Shape([5, 5]), dtype=dtype, name="input_1") input2 = ng.parameter(Shape([25]), dtype=dtype, name="input_2") shape_of = ng.shape_of(input2, name="shape_of") @@ -19,7 +19,7 @@ def test_get_constant_from_source_success(): def test_get_constant_from_source_failed(): - dtype = np.int + dtype = np.int32 input1 = ng.parameter(Shape([5, 5]), dtype=dtype, name="input_1") input2 = ng.parameter(Shape([1]), dtype=dtype, name="input_2") reshape = ng.reshape(input1, input2, special_zero=True) diff --git a/src/bindings/python/tests_compatibility/test_ngraph/util.py b/src/bindings/python/tests_compatibility/test_ngraph/util.py index 7c6c2388f71..763d4d3cd9d 100644 --- a/src/bindings/python/tests_compatibility/test_ngraph/util.py +++ b/src/bindings/python/tests_compatibility/test_ngraph/util.py @@ -1,75 +1,6 @@ # Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 -from typing import Any, Callable, List, Union - -import numpy as np - -import ngraph as ng -from ngraph.utils.types import NumericData -from tests_compatibility.runtime import get_runtime -from string import ascii_uppercase - - -def _get_numpy_dtype(scalar): - return np.array([scalar]).dtype - - -def run_op_node(input_data, op_fun, *args): - # type: (Union[NumericData, List[NumericData]], Callable, *Any) -> List[NumericData] - """Run computation on node performing `op_fun`. - - `op_fun` has to accept a node as an argument. - - This function converts passed raw input data to nGraph Constant Node and that form is passed - to `op_fun`. - - :param input_data: The input data for performed computation. - :param op_fun: The function handler for operation we want to carry out. - :param args: The arguments passed to operation we want to carry out. - :return: The result from computations. - """ - runtime = get_runtime() - comp_args = [] - op_fun_args = [] - comp_inputs = [] - - for idx, data in enumerate(input_data): - node = None - if np.isscalar(data): - node = ng.parameter([], name=ascii_uppercase[idx], dtype=_get_numpy_dtype(data)) - else: - node = ng.parameter(data.shape, name=ascii_uppercase[idx], dtype=data.dtype) - op_fun_args.append(node) - comp_args.append(node) - comp_inputs.append(data) - - op_fun_args.extend(args) - node = op_fun(*op_fun_args) - computation = runtime.computation(node, *comp_args) - return computation(*comp_inputs) - - -def run_op_numeric_data(input_data, op_fun, *args): - # type: (NumericData, Callable, *Any) -> List[NumericData] - """Run computation on node performing `op_fun`. - - `op_fun` has to accept a scalar or an array. - - This function passess input data AS IS. This mean that in case they're a scalar (integral, - or floating point value) or a NumPy's ndarray object they will be automatically converted - to nGraph's Constant Nodes. - - :param input_data: The input data for performed computation. - :param op_fun: The function handler for operation we want to carry out. - :param args: The arguments passed to operation we want to carry out. - :return: The result from computations. - """ - runtime = get_runtime() - node = op_fun(input_data, *args) - computation = runtime.computation(node) - return computation() - def count_ops_of_type(func, op_type): count = 0 diff --git a/src/bindings/python/tests_compatibility/test_utils/test_utils.py b/src/bindings/python/tests_compatibility/test_utils/test_utils.py index f1789b53f61..908251c4d8f 100644 --- a/src/bindings/python/tests_compatibility/test_utils/test_utils.py +++ b/src/bindings/python/tests_compatibility/test_utils/test_utils.py @@ -25,13 +25,17 @@ def generate_image(shape: Tuple = (1, 3, 32, 32), dtype: Union[str, np.dtype] = return np.random.rand(*shape).astype(dtype) -def generate_model(input_shape: List[int]) -> openvino.inference_engine.ExecutableNetwork: +def generate_relu_model(input_shape: List[int]) -> openvino.inference_engine.IENetwork: param = ng.parameter(input_shape, np.float32, name="parameter") relu = ng.relu(param, name="relu") func = Function([relu], [param], "test") func.get_ordered_ops()[2].friendly_name = "friendly" - - core = IECore() caps = Function.to_capsule(func) cnnNetwork = IENetwork(caps) - return core.load_network(cnnNetwork, "CPU", {}) + return cnnNetwork + + +def generate_relu_compiled_model(input_shape: List[int], device = "CPU") -> openvino.inference_engine.ExecutableNetwork: + core = IECore() + cnnNetwork = generate_relu_model(input_shape) + return core.load_network(cnnNetwork, device, {})