From bd8a3835608b543103f3d6b2c2bdb6b0205f3bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Karzy=C5=84ski?= <4430709+postrational@users.noreply.github.com> Date: Fri, 3 Jul 2020 18:11:39 +0200 Subject: [PATCH] Enable nGraph Python API unit tests using Inference Engine APIs (#1095) --- .../src/openvino/inference_engine/ie_api.pyx | 13 +- ngraph/python/requirements.txt | 4 +- ngraph/python/setup.py | 2 - ngraph/python/src/ngraph/ops.py | 6 +- ngraph/python/src/ngraph/runtime.py | 172 -------- .../src/ngraph/utils/input_validation.py | 3 +- .../python/src/ngraph/utils/node_factory.py | 1 + ngraph/python/test/ngraph/__init__.py | 15 - ngraph/python/test/ngraph/models/add_abc.onnx | 24 -- ngraph/python/test/ngraph/test_onnx_import.py | 42 -- ngraph/python/{test => tests}/__init__.py | 0 ngraph/python/{test => tests}/conftest.py | 33 +- ngraph/python/tests/runtime.py | 121 ++++++ ngraph/python/tests/test_ngraph/__init__.py | 0 .../test_ngraph}/test_basic.py | 35 +- .../test_ngraph}/test_convolution.py | 37 +- .../ngraph => tests/test_ngraph}/test_core.py | 0 .../test_ngraph}/test_create_op.py | 65 +-- .../test_ngraph}/test_data_movement.py | 7 +- .../test_ngraph}/test_dyn_attributes.py | 0 .../test_ngraph}/test_input_validation.py | 2 +- .../test_ngraph}/test_node_factory.py | 4 +- .../test_ngraph}/test_normalization.py | 25 +- .../{test => tests/test_ngraph}/test_ops.py | 406 +++++------------- .../test_ngraph}/test_ops_binary.py | 15 +- .../test_ngraph}/test_ops_fused.py | 43 +- .../test_ngraph}/test_ops_matmul.py | 3 +- .../test_ngraph}/test_ops_multioutput.py | 2 +- .../test_ngraph}/test_ops_reshape.py | 7 +- .../test_ngraph}/test_ops_scatter.py | 1 + .../test_ngraph}/test_ops_unary.py | 2 +- .../test_ngraph}/test_pooling.py | 25 +- .../test_ngraph}/test_reduction.py | 6 +- .../test_ngraph}/test_sequence_processing.py | 3 +- .../ngraph => tests/test_ngraph}/util.py | 11 +- ngraph/python/tox.ini | 15 +- 36 files changed, 380 insertions(+), 770 deletions(-) delete mode 100644 ngraph/python/src/ngraph/runtime.py delete mode 100644 ngraph/python/test/ngraph/__init__.py delete mode 100644 ngraph/python/test/ngraph/models/add_abc.onnx delete mode 100644 ngraph/python/test/ngraph/test_onnx_import.py rename ngraph/python/{test => tests}/__init__.py (100%) rename ngraph/python/{test => tests}/conftest.py (52%) create mode 100644 ngraph/python/tests/runtime.py create mode 100644 ngraph/python/tests/test_ngraph/__init__.py rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_basic.py (93%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_convolution.py (88%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_core.py (100%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_create_op.py (95%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_data_movement.py (96%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_dyn_attributes.py (100%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_input_validation.py (100%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_node_factory.py (100%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_normalization.py (81%) rename ngraph/python/{test => tests/test_ngraph}/test_ops.py (56%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_ops_binary.py (95%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_ops_fused.py (95%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_ops_matmul.py (95%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_ops_multioutput.py (97%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_ops_reshape.py (97%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_ops_scatter.py (99%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_ops_unary.py (98%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_pooling.py (93%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_reduction.py (97%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/test_sequence_processing.py (95%) rename ngraph/python/{test/ngraph => tests/test_ngraph}/util.py (95%) diff --git a/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api.pyx b/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api.pyx index de742575760..92a291e869b 100644 --- a/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api.pyx +++ b/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api.pyx @@ -1434,11 +1434,10 @@ cdef class IENetwork: # ``` def __cinit__(self, model: [str, bytes] = "", weights: [str, bytes] = "", init_from_buffer: bool = False): - # TODO: ucomment when ngraph python api will work # Try to create Inference Engine network from capsule - # if model.__class__.__name__ == 'PyCapsule' and weights == '' and init_from_buffer is False: - # self.impl = C.IENetwork(model) - # return + if model.__class__.__name__ == 'PyCapsule' and weights == '' and init_from_buffer is False: + self.impl = C.IENetwork(model) + return cdef char*xml_buffer = malloc(len(model)+1) cdef uint8_t*bin_buffer = malloc(len(weights)) cdef string model_ @@ -1619,10 +1618,8 @@ cdef class IENetwork: c_input_shapes[input.encode()] = c_shape self.impl.reshape(c_input_shapes) - # TODO: ucomment when ngraph python api will work - - # def get_function(self): - # return self.impl.getFunction() + def _get_function_capsule(self): + return self.impl.getFunction() cdef class BlobBuffer: """Copy-less accessor for Inference Engine Blob""" diff --git a/ngraph/python/requirements.txt b/ngraph/python/requirements.txt index 38b31c774e7..badb7a7dd51 100644 --- a/ngraph/python/requirements.txt +++ b/ngraph/python/requirements.txt @@ -1,4 +1,2 @@ -six -numpy==1.15.4; python_version == "3.4" -numpy; python_version != "3.4" +numpy typing diff --git a/ngraph/python/setup.py b/ngraph/python/setup.py index 8c0d5896f51..b98aefc4336 100644 --- a/ngraph/python/setup.py +++ b/ngraph/python/setup.py @@ -365,7 +365,6 @@ class BuildExt(build_ext): with open(os.path.join(PYNGRAPH_ROOT_DIR, "requirements.txt")) as req: requirements = req.read().splitlines() - setup_requires = [item for item in requirements if item.strip().startswith("numpy")] setup( name="ngraph-core", @@ -379,7 +378,6 @@ setup( packages=packages, cmdclass={"build_ext": BuildExt}, data_files=data_files, - setup_requires=setup_requires, install_requires=requirements, zip_safe=False, extras_require={}, diff --git a/ngraph/python/src/ngraph/ops.py b/ngraph/python/src/ngraph/ops.py index 9067ebca474..200742aff4d 100644 --- a/ngraph/python/src/ngraph/ops.py +++ b/ngraph/python/src/ngraph/ops.py @@ -31,11 +31,11 @@ from ngraph.utils.input_validation import ( from ngraph.utils.node_factory import NodeFactory from ngraph.utils.tensor_iterator_types import ( GraphBody, - TensorIteratorSliceInputDesc, - TensorIteratorMergedInputDesc, - TensorIteratorInvariantInputDesc, TensorIteratorBodyOutputDesc, TensorIteratorConcatOutputDesc, + TensorIteratorInvariantInputDesc, + TensorIteratorMergedInputDesc, + TensorIteratorSliceInputDesc, ) from ngraph.utils.types import ( NodeInput, diff --git a/ngraph/python/src/ngraph/runtime.py b/ngraph/python/src/ngraph/runtime.py deleted file mode 100644 index abb6bb8f28a..00000000000 --- a/ngraph/python/src/ngraph/runtime.py +++ /dev/null @@ -1,172 +0,0 @@ -# ****************************************************************************** -# Copyright 2017-2020 Intel Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ****************************************************************************** -"""Provide a layer of abstraction for the ngraph++ runtime environment.""" -import logging -from typing import Dict, List, Union -from enum import Enum - -import numpy as np - -from ngraph.exceptions import UserInputError -from ngraph.impl import Function, Node, Shape, PartialShape, serialize, util -from ngraph.impl.runtime import Backend, Executable, Tensor -from ngraph.utils.types import NumericData, get_dtype - -log = logging.getLogger(__name__) - - -class BackendMode(Enum): - """DYNAMIC mode enables backend's wrapper which supports dynamic shapes.""" - - STATIC = 0 - DYNAMIC = 1 - - -def runtime(backend_name: str = "CPU", mode: BackendMode = BackendMode.STATIC) -> "Runtime": - """Create a Runtime object (helper factory). - - Use signature to parameterize runtime as needed. - """ - return Runtime(backend_name, mode) - - -class Runtime: - """Represents the ngraph++ runtime environment.""" - - def __init__(self, backend_name: str, mode: BackendMode = BackendMode.STATIC) -> None: - self.backend_name = backend_name - if mode == BackendMode.DYNAMIC: - self.backend = Backend.create_dynamic(backend_name) - else: - self.backend = Backend.create(backend_name) - - def set_config(self, config: Dict[str, str]) -> None: - """Set the backend configuration.""" - self.backend.set_config(config, "") - - def __repr__(self) -> str: - return "".format(self.backend_name) - - def computation(self, node_or_function: Union[Node, Function], *inputs: Node) -> "Computation": - """Return a callable Computation object.""" - if isinstance(node_or_function, Node): - ng_function = Function(node_or_function, inputs, node_or_function.name) - return Computation(self, ng_function) - elif isinstance(node_or_function, Function): - return Computation(self, node_or_function) - else: - raise TypeError( - "Runtime.computation must be called with an nGraph Function object " - "or an nGraph node object an optionally Parameter node objects. " - "Called with: %s", - node_or_function, - ) - - -class Computation(object): - """ngraph callable computation object.""" - - def __init__(self, runtime: Runtime, ng_function: Function) -> None: - self.runtime = runtime - self.function = ng_function - self.parameters = ng_function.get_parameters() - self.results = ng_function.get_results() - self.handle = self.runtime.backend.compile(self.function) - - self.tensor_views = [] # type: List[Tensor] - for parameter in self.parameters: - shape = parameter.get_shape() - element_type = parameter.get_element_type() - self.tensor_views.append(runtime.backend.create_tensor(element_type, shape)) - - self.result_views = [] # type: List[Tensor] - for result in self.results: - element_type = result.get_element_type() - if self.function.is_dynamic(): - output_pshape = result.get_output_partial_shape(0) - output_tensor = runtime.backend.create_dynamic_tensor(element_type, output_pshape) - self.result_views.append(output_tensor) - else: - output_shape = result.get_shape() - output_tensor = runtime.backend.create_tensor(element_type, output_shape) - self.result_views.append(output_tensor) - - def __repr__(self) -> str: - params_string = ", ".join([param.name for param in self.parameters]) - return "".format(self.function.get_name(), params_string) - - def __call__(self, *input_values: NumericData) -> List[NumericData]: - """Run computation on input values and return result.""" - for tensor_view, value in zip(self.tensor_views, input_values): - if not isinstance(value, np.ndarray): - value = np.array(value) - Computation._write_ndarray_to_tensor_view(value, tensor_view) - - if self.function.is_dynamic(): - self.handle.call_with_validate(self.result_views, self.tensor_views) - else: - self.handle.call(self.result_views, self.tensor_views) - - results = [] - for result_view in self.result_views: - result = np.ndarray(result_view.shape, dtype=get_dtype(result_view.element_type)) - Computation._read_tensor_view_to_ndarray(result_view, result) - results.append(result) - - return results - - def serialize(self, indent: int = 0) -> str: - """Serialize function (compute graph) to a JSON string. - - :param indent: set indent of serialized output - :return: serialized model - """ - return serialize(self.function, indent) - - @staticmethod - def _get_buffer_size(element_type: Tensor, element_count: int) -> int: - return int((element_type.bitwidth / 8.0) * element_count) - - @staticmethod - def _write_ndarray_to_tensor_view(value: np.ndarray, tensor_view: Tensor) -> None: - tensor_view_dtype = get_dtype(tensor_view.element_type) - if list(tensor_view.shape) != list(value.shape) and len(value.shape) > 0: - raise UserInputError( - "Provided tensor's shape: %s does not match the expected: %s.", - list(value.shape), - list(tensor_view.shape), - ) - if value.dtype != tensor_view_dtype: - log.warning( - "Attempting to write a %s value to a %s tensor. Will attempt type conversion.", - value.dtype, - tensor_view.element_type, - ) - value = value.astype(tensor_view_dtype) - - buffer_size = Computation._get_buffer_size( - tensor_view.element_type, tensor_view.element_count - ) - - nparray = np.ascontiguousarray(value) - tensor_view.write(util.numpy_to_c(nparray), buffer_size) - - @staticmethod - def _read_tensor_view_to_ndarray(tensor_view: Tensor, output: np.ndarray) -> None: - buffer_size = Computation._get_buffer_size( - tensor_view.element_type, tensor_view.element_count - ) - tensor_view.read(util.numpy_to_c(output), buffer_size) diff --git a/ngraph/python/src/ngraph/utils/input_validation.py b/ngraph/python/src/ngraph/utils/input_validation.py index 1639ad4b922..6b35070dcd1 100644 --- a/ngraph/python/src/ngraph/utils/input_validation.py +++ b/ngraph/python/src/ngraph/utils/input_validation.py @@ -17,9 +17,10 @@ """Helper functions for validating user input.""" import logging -import numpy as np from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type +import numpy as np + from ngraph.exceptions import UserInputError log = logging.getLogger(__name__) diff --git a/ngraph/python/src/ngraph/utils/node_factory.py b/ngraph/python/src/ngraph/utils/node_factory.py index 60fcbebbd62..73aebb8de1a 100644 --- a/ngraph/python/src/ngraph/utils/node_factory.py +++ b/ngraph/python/src/ngraph/utils/node_factory.py @@ -2,6 +2,7 @@ from functools import partial from typing import Any, Dict, List, Optional from _pyngraph import NodeFactory as _NodeFactory + from ngraph.impl import Node DEFAULT_OPSET = "opset4" diff --git a/ngraph/python/test/ngraph/__init__.py b/ngraph/python/test/ngraph/__init__.py deleted file mode 100644 index 4fab6b7e1b0..00000000000 --- a/ngraph/python/test/ngraph/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ -# ****************************************************************************** -# Copyright 2017-2020 Intel Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ****************************************************************************** diff --git a/ngraph/python/test/ngraph/models/add_abc.onnx b/ngraph/python/test/ngraph/models/add_abc.onnx deleted file mode 100644 index 5c2da5dcc0b..00000000000 --- a/ngraph/python/test/ngraph/models/add_abc.onnx +++ /dev/null @@ -1,24 +0,0 @@ -ngraph ONNXImporter:† - -A -BX add_node1"Add - -X -CY add_node2"Add -test_graphZ -A - - -Z -B - - -Z -C - - -b -Y - - -B \ No newline at end of file diff --git a/ngraph/python/test/ngraph/test_onnx_import.py b/ngraph/python/test/ngraph/test_onnx_import.py deleted file mode 100644 index 4b99ee105f2..00000000000 --- a/ngraph/python/test/ngraph/test_onnx_import.py +++ /dev/null @@ -1,42 +0,0 @@ -# ****************************************************************************** -# Copyright 2017-2020 Intel Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ****************************************************************************** - -import os -import numpy as np - -from test.ngraph.util import get_runtime - -try: - from ngraph.impl.onnx_import import import_onnx_model_file - - def test_import_onnx_function(): - model_path = os.path.join(os.path.dirname(__file__), "models/add_abc.onnx") - ng_function = import_onnx_model_file(model_path) - - dtype = np.float32 - value_a = np.array([1.0], dtype=dtype) - value_b = np.array([2.0], dtype=dtype) - value_c = np.array([3.0], dtype=dtype) - - runtime = get_runtime() - computation = runtime.computation(ng_function) - result = computation(value_a, value_b, value_c) - assert np.allclose(result, np.array([6], dtype=dtype)) - - -except ImportError: - # Do not test load_onnx_model_file if nGraph was build without ONNX support - pass diff --git a/ngraph/python/test/__init__.py b/ngraph/python/tests/__init__.py similarity index 100% rename from ngraph/python/test/__init__.py rename to ngraph/python/tests/__init__.py diff --git a/ngraph/python/test/conftest.py b/ngraph/python/tests/conftest.py similarity index 52% rename from ngraph/python/test/conftest.py rename to ngraph/python/tests/conftest.py index 4cd0f444391..7d4cb6ca82d 100644 --- a/ngraph/python/test/conftest.py +++ b/ngraph/python/tests/conftest.py @@ -14,21 +14,30 @@ # limitations under the License. # ****************************************************************************** import pytest -import test + +import tests def pytest_addoption(parser): parser.addoption( "--backend", - default="INTERPRETER", - choices=["INTERPRETER", "CPU", "IE"], - help="Select from available backends", + default="CPU", + choices=["CPU", "GPU", "FPGA", "HDDL", "MYRIAD", "HETERO"], + help="Select target device", ) def pytest_configure(config): backend_name = config.getvalue("backend") - test.BACKEND_NAME = backend_name + tests.BACKEND_NAME = backend_name + + # register additional markers + config.addinivalue_line("markers", "skip_on_cpu: Skip test on CPU") + config.addinivalue_line("markers", "skip_on_gpu: Skip test on GPU") + config.addinivalue_line("markers", "skip_on_fpga: Skip test on FPGA") + config.addinivalue_line("markers", "skip_on_hddl: Skip test on HDDL") + config.addinivalue_line("markers", "skip_on_myriad: Skip test on MYRIAD") + config.addinivalue_line("markers", "skip_on_hetero: Skip test on HETERO") def pytest_collection_modifyitems(config, items): @@ -36,14 +45,20 @@ def pytest_collection_modifyitems(config, items): keywords = { "CPU": "skip_on_cpu", - "INTERPRETER": "skip_on_interpreter", - "IE": "skip_on_inference_engine", + "GPU": "skip_on_gpu", + "FPGA": "skip_on_fpga", + "HDDL": "skip_on_hddl", + "MYRIAD": "skip_on_myriad", + "HETERO": "skip_on_hetero", } skip_markers = { "CPU": pytest.mark.skip(reason="Skipping test on the CPU backend."), - "INTERPRETER": pytest.mark.skip(reason="Skipping test on the INTERPRETER backend."), - "IE": pytest.mark.skip(reason="Skipping test on the IE backend."), + "GPU": pytest.mark.skip(reason="Skipping test on the GPU backend."), + "FPGA": pytest.mark.skip(reason="Skipping test on the FPGA backend."), + "HDDL": pytest.mark.skip(reason="Skipping test on the HDDL backend."), + "MYRIAD": pytest.mark.skip(reason="Skipping test on the MYRIAD backend."), + "HETERO": pytest.mark.skip(reason="Skipping test on the HETERO backend."), } for item in items: diff --git a/ngraph/python/tests/runtime.py b/ngraph/python/tests/runtime.py new file mode 100644 index 00000000000..ec531448f47 --- /dev/null +++ b/ngraph/python/tests/runtime.py @@ -0,0 +1,121 @@ +# ****************************************************************************** +# Copyright 2017-2020 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ****************************************************************************** +"""Provide a layer of abstraction for an OpenVINO runtime environment.""" +import logging +from typing import Dict, List, Union + +import numpy as np +from openvino.inference_engine import IECore, IENetwork + +from ngraph.exceptions import UserInputError +from ngraph.impl import Function, Node, PartialShape, Shape, serialize, util +from ngraph.utils.types import NumericData +import tests + +log = logging.getLogger(__name__) + + +def runtime(backend_name: str = "CPU") -> "Runtime": + """Create a Runtime object (helper factory).""" + return Runtime(backend_name) + + +def get_runtime(): + """Return runtime object.""" + return runtime(backend_name=tests.BACKEND_NAME) + + +class Runtime(object): + """Represents an nGraph runtime environment.""" + + def __init__(self, backend_name: str) -> None: + self.backend_name = backend_name + log.debug("Creating Inference Engine for .".format(backend_name)) + self.backend = IECore() + assert backend_name in self.backend.available_devices, ( + 'The requested device "' + backend_name + '" is not supported!' + ) + + def set_config(self, config: Dict[str, str]) -> None: + """Set the inference engine configuration.""" + self.backend.set_config(config, device_name=self.backend_name) + + def __repr__(self) -> str: + return "".format(self.backend_name) + + def computation(self, node_or_function: Union[Node, Function], *inputs: Node) -> "Computation": + """Return a callable Computation object.""" + if isinstance(node_or_function, Node): + ng_function = Function(node_or_function, inputs, node_or_function.name) + return Computation(self, ng_function) + elif isinstance(node_or_function, Function): + return Computation(self, node_or_function) + else: + raise TypeError( + "Runtime.computation must be called with an nGraph Function object " + "or an nGraph node object an optionally Parameter node objects. " + "Called with: %s", + node_or_function, + ) + + +class Computation(object): + """nGraph callable computation object.""" + + def __init__(self, runtime: Runtime, ng_function: Function) -> None: + ie = runtime.backend + self.runtime = runtime + self.function = ng_function + self.parameters = ng_function.get_parameters() + self.results = ng_function.get_results() + + capsule = Function.to_capsule(ng_function) + cnn_network = IENetwork(capsule) + self.executable_network = ie.load_network(cnn_network, self.runtime.backend_name) + + def __repr__(self) -> str: + params_string = ", ".join([param.name for param in self.parameters]) + return "".format(self.function.get_name(), params_string) + + def __call__(self, *input_values: NumericData) -> List[NumericData]: + """Run computation on input values and return result.""" + input_values = [np.array(input_value) for input_value in input_values] + + # Input validation + if len(input_values) != len(self.parameters): + raise UserInputError( + "Expected %s parameters, received %s.", len(self.parameters), len(input_values) + ) + for parameter, input in zip(self.parameters, input_values): + parameter_shape = parameter.get_output_shape(0) + if len(input.shape) > 0 and list(parameter_shape) != list(input.shape): + raise UserInputError( + "Provided tensor's shape: %s does not match the expected: %s.", + list(input.shape), + list(parameter_shape), + ) + + request = self.executable_network.requests[0] + request.infer(dict(zip(request._inputs_list, input_values))) + return [blob.buffer for blob in request.output_blobs.values()] + + def serialize(self, indent: int = 0) -> str: + """Serialize function (compute graph) to a JSON string. + + :param indent: set indent of serialized output + :return: serialized model + """ + return serialize(self.function, indent) diff --git a/ngraph/python/tests/test_ngraph/__init__.py b/ngraph/python/tests/test_ngraph/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/ngraph/python/test/ngraph/test_basic.py b/ngraph/python/tests/test_ngraph/test_basic.py similarity index 93% rename from ngraph/python/test/ngraph/test_basic.py rename to ngraph/python/tests/test_ngraph/test_basic.py index e9ca0cd7b43..e84d002b8fa 100644 --- a/ngraph/python/test/ngraph/test_basic.py +++ b/ngraph/python/tests/test_ngraph/test_basic.py @@ -13,16 +13,16 @@ # See the License for the specific language governing permissions and # limitations under the License. # ****************************************************************************** -import numpy as np -import pytest import json -import ngraph as ng -from ngraph.impl import Function -from ngraph.exceptions import UserInputError +import numpy as np +import pytest -import test -from test.ngraph.util import get_runtime, run_op_node +import ngraph as ng +from ngraph.exceptions import UserInputError +from ngraph.impl import Function +from tests.runtime import get_runtime +from tests.test_ngraph.util import run_op_node def test_ngraph_function_api(): @@ -86,14 +86,13 @@ def test_simple_computation_on_ndarrays(dtype): def test_serialization(): dtype = np.float32 - backend_name = test.BACKEND_NAME - 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 = ng.runtime(backend_name=backend_name) + + runtime = get_runtime() computation = runtime.computation(model, parameter_a, parameter_b, parameter_c) try: serialized = computation.serialize(2) @@ -209,7 +208,7 @@ 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 = ng.runtime(backend_name="INTERPRETER") + runtime = get_runtime() computation = runtime.computation(model, A, B) value_a = np.array([[1, 2]], dtype=np.float32) @@ -240,8 +239,9 @@ def test_constant_get_data_floating_point(data_type): @pytest.mark.parametrize("data_type", [np.int64, np.int32, np.int16, np.int8]) def test_constant_get_data_signed_integer(data_type): np.random.seed(133391) - input_data = np.random.randint(np.iinfo(data_type).min, np.iinfo(data_type).max, - size=[2, 3, 4], dtype=data_type) + input_data = np.random.randint( + np.iinfo(data_type).min, np.iinfo(data_type).max, size=[2, 3, 4], dtype=data_type + ) node = ng.constant(input_data, dtype=data_type) retrieved_data = node.get_data() assert np.allclose(input_data, retrieved_data) @@ -252,9 +252,7 @@ def test_constant_get_data_unsigned_integer(data_type): np.random.seed(133391) input_data = np.random.randn(2, 3, 4).astype(data_type) input_data = ( - np.iinfo(data_type).min - + input_data * np.iinfo(data_type).max - + input_data * np.iinfo(data_type).max + np.iinfo(data_type).min + input_data * np.iinfo(data_type).max + input_data * np.iinfo(data_type).max ) node = ng.constant(input_data, dtype=data_type) retrieved_data = node.get_data() @@ -264,11 +262,12 @@ def test_constant_get_data_unsigned_integer(data_type): def test_backend_config(): dummy_config = {"dummy_option": "dummy_value"} # Expect no throw - ng.runtime(backend_name=test.BACKEND_NAME).set_config(dummy_config) + runtime = get_runtime() + runtime.set_config(dummy_config) def test_result(): node = [[11, 10], [1, 8], [3, 4]] - result = test.ngraph.util.run_op_node([node], ng.ops.result) + result = util.run_op_node([node], ng.ops.result) assert np.allclose(result, node) diff --git a/ngraph/python/test/ngraph/test_convolution.py b/ngraph/python/tests/test_ngraph/test_convolution.py similarity index 88% rename from ngraph/python/test/ngraph/test_convolution.py rename to ngraph/python/tests/test_ngraph/test_convolution.py index d496fe0c481..7fded638acf 100644 --- a/ngraph/python/test/ngraph/test_convolution.py +++ b/ngraph/python/tests/test_ngraph/test_convolution.py @@ -17,8 +17,9 @@ import numpy as np import ngraph as ng -from test.ngraph.util import get_runtime, run_op_node -from test.test_ops import convolution2d +from tests.runtime import get_runtime +from tests.test_ngraph.test_ops import convolution2d +from tests.test_ngraph.util import run_op_node def test_convolution_2d(): @@ -40,9 +41,9 @@ def test_convolution_2d(): ).reshape(1, 1, 9, 9) # 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 = 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]) @@ -50,9 +51,7 @@ def test_convolution_2d(): dilations = np.array([1, 1]) # 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 - ) + result = run_op_node([input_x, input_filter], ng.convolution, strides, pads_begin, pads_end, dilations) assert np.allclose( result, @@ -81,9 +80,7 @@ def test_convolution_2d(): 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 - ) + result = run_op_node([input_x, input_filter], ng.convolution, strides, pads_begin, pads_end, dilations) assert np.allclose( result, np.array( @@ -110,9 +107,7 @@ def test_convolution_2d(): 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 - ) + result = run_op_node([input_x, input_filter], ng.convolution, strides, pads_begin, pads_end, dilations) assert np.allclose( result, @@ -137,9 +132,7 @@ def test_convolution_2d(): 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 - ) + result = run_op_node([input_x, input_filter], ng.convolution, strides, pads_begin, pads_end, dilations) assert np.allclose( result, np.array( @@ -190,9 +183,9 @@ def test_convolution_backprop_data(): 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) + 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) @@ -232,9 +225,7 @@ 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 - ) + result = run_op_node([input_tensor, filters], ng.convolution, strides, pads_begin, pads_end, dilations) expected = convolution2d(input_tensor[0, 0], filters[0, 0]).reshape(1, 1, 14, 14) diff --git a/ngraph/python/test/ngraph/test_core.py b/ngraph/python/tests/test_ngraph/test_core.py similarity index 100% rename from ngraph/python/test/ngraph/test_core.py rename to ngraph/python/tests/test_ngraph/test_core.py diff --git a/ngraph/python/test/ngraph/test_create_op.py b/ngraph/python/tests/test_ngraph/test_create_op.py similarity index 95% rename from ngraph/python/test/ngraph/test_create_op.py rename to ngraph/python/tests/test_ngraph/test_create_op.py index 24b7ccab657..0f58ba793ee 100644 --- a/ngraph/python/test/ngraph/test_create_op.py +++ b/ngraph/python/tests/test_ngraph/test_create_op.py @@ -15,10 +15,10 @@ # ****************************************************************************** import numpy as np import pytest +from _pyngraph import PartialShape import ngraph as ng from ngraph.impl import Type -from _pyngraph import PartialShape np_types = [np.float32, np.int32] integral_np_types = [ @@ -50,14 +50,7 @@ def test_binary_convolution(dtype): parameter_input1 = ng.parameter(input1_shape, name="Input1", dtype=dtype) node = ng.binary_convolution( - parameter_input0, - parameter_input1, - strides, - pads_begin, - pads_end, - dilations, - mode, - pad_value, + parameter_input0, parameter_input1, strides, pads_begin, pads_end, dilations, mode, pad_value, ) assert node.get_type_name() == "BinaryConvolution" @@ -98,13 +91,7 @@ def test_deformable_convolution(dtype): parameter_input2 = ng.parameter(input2_shape, name="Input2", dtype=dtype) node = ng.deformable_convolution( - parameter_input0, - parameter_input1, - parameter_input2, - strides, - pads_begin, - pads_end, - dilations, + parameter_input0, parameter_input1, parameter_input2, strides, pads_begin, pads_end, dilations, ) assert node.get_type_name() == "DeformableConvolution" @@ -210,13 +197,7 @@ def test_lstm_cell_operator(dtype): expected_shape = [1, 128] node_default = ng.lstm_cell( - parameter_X, - parameter_H_t, - parameter_C_t, - parameter_W, - parameter_R, - parameter_B, - hidden_size, + parameter_X, parameter_H_t, parameter_C_t, parameter_W, parameter_R, parameter_B, hidden_size, ) assert node_default.get_type_name() == "LSTMCell" @@ -465,9 +446,7 @@ def test_gru_cell_operator(): expected_shape = [1, 128] - node_default = ng.gru_cell( - parameter_X, parameter_H_t, parameter_W, parameter_R, parameter_B, hidden_size - ) + node_default = ng.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 @@ -571,8 +550,7 @@ def test_region_yolo(): end_axis = 3 do_softmax = False - node = ng.region_yolo(data, num_coords, num_classes, num_regions, - do_softmax, mask, axis, end_axis) + node = ng.region_yolo(data, num_coords, num_classes, num_regions, do_softmax, mask, axis, end_axis) assert node.get_type_name() == "RegionYolo" assert node.get_output_size() == 1 @@ -765,9 +743,7 @@ def test_detection_output(int_dtype, fp_dtype): aux_class_preds = ng.parameter([2, 1, 4, 5], fp_dtype, "aux_class_preds") aux_box_preds = ng.parameter([2, 1, 4, 5], fp_dtype, "aux_box_preds") - node = ng.detection_output( - box_logits, class_preds, proposals, attributes, aux_class_preds, aux_box_preds - ) + node = ng.detection_output(box_logits, class_preds, proposals, attributes, aux_class_preds, aux_box_preds) assert node.get_type_name() == "DetectionOutput" assert node.get_output_size() == 1 @@ -840,8 +816,7 @@ def test_tensor_iterator(): iter_cnt = ng.ops.range(zero, np.int32(16), np.int32(1)) ti_inputs = [iter_cnt, data, initial_cma, one] - graph_body = GraphBody([body_timestep, body_data_in, body_prev_cma, body_const_one], - [curr_cma, cma_hist]) + graph_body = GraphBody([body_timestep, body_data_in, body_prev_cma, body_const_one], [curr_cma, cma_hist]) ti_slice_input_desc = [ # timestep # input_idx, body_param_idx, start, stride, part_size, end, axis @@ -868,13 +843,15 @@ def test_tensor_iterator(): TensorIteratorConcatOutputDesc(1, 1, 0, 1, 1, -1, 0), ] - node = ng.tensor_iterator(ti_inputs, - graph_body, - ti_slice_input_desc, - ti_merged_input_desc, - ti_invariant_input_desc, - ti_body_output_desc, - ti_concat_output_desc) + node = ng.tensor_iterator( + ti_inputs, + graph_body, + ti_slice_input_desc, + ti_merged_input_desc, + ti_invariant_input_desc, + ti_body_output_desc, + ti_concat_output_desc, + ) assert node.get_type_name() == "TensorIterator" assert node.get_output_size() == 2 @@ -908,10 +885,10 @@ def test_assign(): def test_extract_image_patches(): image = ng.parameter([64, 3, 10, 10], name="image", dtype=np.int32) - sizes = [3, 3]; - strides = [5, 5]; - rates = [1, 1]; - padding = "VALID"; + sizes = [3, 3] + strides = [5, 5] + rates = [1, 1] + padding = "VALID" node = ng.extract_image_patches(image, sizes, strides, rates, padding) assert node.get_type_name() == "ExtractImagePatches" diff --git a/ngraph/python/test/ngraph/test_data_movement.py b/ngraph/python/tests/test_ngraph/test_data_movement.py similarity index 96% rename from ngraph/python/test/ngraph/test_data_movement.py rename to ngraph/python/tests/test_ngraph/test_data_movement.py index 9bef7e892f6..ccf22ef9910 100644 --- a/ngraph/python/test/ngraph/test_data_movement.py +++ b/ngraph/python/tests/test_ngraph/test_data_movement.py @@ -16,7 +16,8 @@ import numpy as np import ngraph as ng -from test.ngraph.util import get_runtime, run_op_node +from tests.runtime import get_runtime +from tests.test_ngraph.util import run_op_node def test_reverse_sequence(): @@ -170,9 +171,7 @@ def test_pad_constant(): pads_end = np.array([2, 3], dtype=np.int32) input_param = ng.parameter(input_data.shape, name="input", dtype=np.int64) - model = ng.pad( - input_param, pads_begin, pads_end, "constant", arg_pad_value=np.array(100, dtype=np.int64) - ) + model = ng.pad(input_param, pads_begin, pads_end, "constant", arg_pad_value=np.array(100, dtype=np.int64)) runtime = get_runtime() computation = runtime.computation(model, input_param) diff --git a/ngraph/python/test/ngraph/test_dyn_attributes.py b/ngraph/python/tests/test_ngraph/test_dyn_attributes.py similarity index 100% rename from ngraph/python/test/ngraph/test_dyn_attributes.py rename to ngraph/python/tests/test_ngraph/test_dyn_attributes.py diff --git a/ngraph/python/test/ngraph/test_input_validation.py b/ngraph/python/tests/test_ngraph/test_input_validation.py similarity index 100% rename from ngraph/python/test/ngraph/test_input_validation.py rename to ngraph/python/tests/test_ngraph/test_input_validation.py index 749af851527..927d0fac736 100644 --- a/ngraph/python/test/ngraph/test_input_validation.py +++ b/ngraph/python/tests/test_ngraph/test_input_validation.py @@ -21,8 +21,8 @@ from ngraph.utils.input_validation import ( _check_value, check_valid_attribute, check_valid_attributes, - is_positive_value, is_non_negative_value, + is_positive_value, ) diff --git a/ngraph/python/test/ngraph/test_node_factory.py b/ngraph/python/tests/test_ngraph/test_node_factory.py similarity index 100% rename from ngraph/python/test/ngraph/test_node_factory.py rename to ngraph/python/tests/test_ngraph/test_node_factory.py index 0846b56ff10..b8dd780e800 100644 --- a/ngraph/python/test/ngraph/test_node_factory.py +++ b/ngraph/python/tests/test_ngraph/test_node_factory.py @@ -14,10 +14,10 @@ # limitations under the License. # ****************************************************************************** import numpy as np -import ngraph as ng - from _pyngraph import NodeFactory as _NodeFactory +import ngraph as ng + def test_node_factory_add(): shape = [2, 2] diff --git a/ngraph/python/test/ngraph/test_normalization.py b/ngraph/python/tests/test_ngraph/test_normalization.py similarity index 81% rename from ngraph/python/test/ngraph/test_normalization.py rename to ngraph/python/tests/test_ngraph/test_normalization.py index 67f163a0f89..9d765df7af2 100644 --- a/ngraph/python/test/ngraph/test_normalization.py +++ b/ngraph/python/tests/test_ngraph/test_normalization.py @@ -17,8 +17,7 @@ import numpy as np import ngraph as ng -from test.ngraph.util import get_runtime -import test +from tests.runtime import get_runtime def test_lrn(): @@ -26,9 +25,7 @@ def test_lrn(): 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 - ) + 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( @@ -36,11 +33,7 @@ def test_lrn(): 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]], - ], + [[[0.00509002], [0.00356153]], [[0.00174719], [0.0012555]], [[0.00322708], [0.00235574]],], ], dtype=np.float32, ), @@ -55,11 +48,7 @@ def test_lrn(): 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]], - ], + [[[0.93704253], [0.97827977]], [[1.2493901], [1.2577883]], [[1.5617375], [1.5372968]],], ], dtype=np.float32, ), @@ -106,7 +95,7 @@ def test_lrn_factory(): ], dtype=np.float32, ) - result = test.ngraph.util.run_op_node([x, axis], ng.ops.lrn, alpha, beta, bias, nsize) + result = util.run_op_node([x, axis], ng.ops.lrn, alpha, beta, bias, nsize) assert np.allclose(result, excepted) @@ -120,7 +109,5 @@ def test_batch_norm_inference(): epsilon = 9.99e-06 excepted = [[2.0, 6.0, 12.0], [-2.0, -6.0, -12.0]] - result = test.ngraph.util.run_op_node( - [data, gamma, beta, mean, variance], ng.ops.batch_norm_inference, epsilon - ) + result = util.run_op_node([data, gamma, beta, mean, variance], ng.ops.batch_norm_inference, epsilon) assert np.allclose(result, excepted) diff --git a/ngraph/python/test/test_ops.py b/ngraph/python/tests/test_ngraph/test_ops.py similarity index 56% rename from ngraph/python/test/test_ops.py rename to ngraph/python/tests/test_ngraph/test_ops.py index a7a4e61ca9e..350f75d17a5 100644 --- a/ngraph/python/test/test_ops.py +++ b/ngraph/python/tests/test_ngraph/test_ops.py @@ -16,18 +16,12 @@ # flake8: noqa from __future__ import absolute_import -import pytest import numpy as np + import ngraph as ng - -from ngraph.impl import util -from ngraph.impl import Shape, Strides, CoordinateDiff, AxisSet, Coordinate -from ngraph.impl import Type, Function -from ngraph.impl.runtime import Backend -from ngraph.impl.op import Parameter -from ngraph.impl.op import Constant - -import test +from ngraph.impl import AxisSet, Function, Shape, Type +from ngraph.impl.op import Constant, Parameter +from tests.runtime import get_runtime def binary_op(op_str, a, b): @@ -108,26 +102,16 @@ def binary_op_exec(op_str): B = Parameter(element_type, shape) parameter_list = [A, B] function = Function([binary_op(op_str, A, B)], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - - a = backend.create_tensor(element_type, shape) - b = backend.create_tensor(element_type, shape) - result = backend.create_tensor(element_type, shape) - - a.write(util.numpy_to_c(np.array([[1, 6], [7, 4]], dtype=np.float32)), 16) - b.write(util.numpy_to_c(np.array([[5, 2], [3, 8]], dtype=np.float32)), 16) - - result_arr = np.array([[0, 0], [0, 0]], dtype=np.float32) - result.write(util.numpy_to_c(result_arr), 16) - handle = backend.compile(function) - handle.call([result], [a, b]) - result.read(util.numpy_to_c(result_arr), 16) a_arr = np.array([[1, 6], [7, 4]], dtype=np.float32) b_arr = np.array([[5, 2], [3, 8]], dtype=np.float32) - result_arr_ref = binary_op_ref(op_str, a_arr, b_arr) - assert np.allclose(result_arr, result_arr_ref) + 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): @@ -138,26 +122,15 @@ def binary_op_comparison(op_str): B = Parameter(element_type, shape) parameter_list = [A, B] function = Function([binary_op(op_str, A, B)], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - - a = backend.create_tensor(element_type, shape) - b = backend.create_tensor(element_type, shape) - result = backend.create_tensor(Type.boolean, shape) - - a.write(util.numpy_to_c(np.array([[1, 5], [3, 2]], dtype=np.float32)), 16) - b.write(util.numpy_to_c(np.array([[2, 4], [3, 1]], dtype=np.float32)), 16) - - result_arr = np.array([[False, False], [False, False]], dtype=np.bool) - result.write(util.numpy_to_c(result_arr), 4) - handle = backend.compile(function) - handle.call([result], [a, b]) - result.read(util.numpy_to_c(result_arr), 4) - a_arr = np.array([[1, 5], [3, 2]], dtype=np.float32) b_arr = np.array([[2, 4], [3, 1]], dtype=np.float32) - result_arr_ref = binary_op_ref(op_str, a_arr, b_arr) - assert np.allclose(result_arr, result_arr_ref) + 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(): @@ -227,35 +200,27 @@ def test_not_equal(): def test_add_with_mul(): element_type = Type.f32 - shape = Shape([2, 2]) + shape = Shape([4]) 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") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, shape) - b = backend.create_tensor(element_type, shape) - c = backend.create_tensor(element_type, shape) - result = backend.create_tensor(element_type, shape) - - a.write(util.numpy_to_c(np.array([1, 2, 3, 4], dtype=np.float32)), 16) - b.write(util.numpy_to_c(np.array([5, 6, 7, 8], dtype=np.float32)), 16) - c.write(util.numpy_to_c(np.array([9, 10, 11, 12], dtype=np.float32)), 16) - - result_arr = np.array([0, 0, 0, 0], dtype=np.float32) - result.write(util.numpy_to_c(result_arr), 16) - handle = backend.compile(function) - handle.call([result], [a, b, c]) - result.read(util.numpy_to_c(result_arr), 16) + 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_arr, result_arr_ref) + assert np.allclose(result, result_arr_ref) def unary_op(op_str, a): @@ -342,27 +307,16 @@ def unary_op_exec(op_str, input_list): """ element_type = Type.f32 shape = Shape(np.array(input_list).shape) - shape_np = np.array(input_list).shape A = Parameter(element_type, shape) parameter_list = [A] function = Function([unary_op(op_str, A)], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, shape) - result = backend.create_tensor(element_type, shape) + runtime = get_runtime() + computation = runtime.computation(function, *parameter_list) + result = computation(np.array(input_list, dtype=np.float32))[0] - a.write(util.numpy_to_c(np.array(input_list, dtype=np.float32)), 16) - - result_arr = np.zeros(shape_np, dtype=np.float32) - result.write(util.numpy_to_c(result_arr), 16) - handle = backend.compile(function) - handle.call([result], [a]) - result.read(util.numpy_to_c(result_arr), 16) - - a_arr = np.array(input_list, dtype=np.float32) - result_arr_ref = unary_op_ref(op_str, a_arr) - - assert np.allclose(result_arr, result_arr_ref) + expected = unary_op_ref(op_str, np.array(input_list, dtype=np.float32)) + assert np.allclose(result, expected) def test_abs(): @@ -480,23 +434,13 @@ def test_reshape(): A = Parameter(element_type, shape) parameter_list = [A] function = Function([ng.reshape(A, Shape([3, 2]), special_zero=False)], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, shape) - result = backend.create_tensor(element_type, Shape([3, 2])) + 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] - a.write(util.numpy_to_c(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)), 24) - - result_arr = np.array([[0, 0], [0, 0], [0, 0]], dtype=np.float32) - result.write(util.numpy_to_c(result_arr), 24) - handle = backend.compile(function) - handle.call([result], [a]) - result.read(util.numpy_to_c(result_arr), 24) - - a_arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) - result_arr_ref = np.reshape(a_arr, (3, 2)) - - assert np.allclose(result_arr, result_arr_ref) + expected = np.reshape(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), (3, 2)) + assert np.allclose(result, expected) def test_broadcast(): @@ -505,46 +449,28 @@ def test_broadcast(): A = Parameter(element_type, Shape([3])) parameter_list = [A] function = Function([ng.broadcast(A, [3, 3])], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, Shape([3])) - result = backend.create_tensor(element_type, Shape([3, 3])) - - a.write(util.numpy_to_c(np.array([1, 2, 3], dtype=np.float32)), 12) - - result_arr = np.zeros((3, 3), dtype=np.float32) - result.write(util.numpy_to_c(result_arr), 36) - handle = backend.compile(function) - handle.call([result], [a]) - result.read(util.numpy_to_c(result_arr), 36) + 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) - result_arr_ref = np.add(a_arr, b_arr) - - assert np.allclose(result_arr, result_arr_ref) + expected = np.add(a_arr, b_arr) + assert np.allclose(result, expected) def test_constant(): - element_type = Type.f32 parameter_list = [] - function = Function( - [Constant(element_type, Shape([3, 3]), list(range(9)))], parameter_list, "test" - ) - backend = Backend.create(test.BACKEND_NAME) + function = Function([Constant(element_type, Shape([3, 3]), list(range(9)))], parameter_list, "test") - result = backend.create_tensor(element_type, Shape([3, 3])) + runtime = get_runtime() + computation = runtime.computation(function, *parameter_list) + result = computation()[0] - result_arr = np.zeros((3, 3), dtype=np.float32) - result.write(util.numpy_to_c(result_arr), 36) - handle = backend.compile(function) - handle.call([result], []) - result.read(util.numpy_to_c(result_arr), 36) - - result_arr_ref = np.arange(9).reshape(3, 3) - - assert np.allclose(result_arr, result_arr_ref) + expected = np.arange(9).reshape(3, 3) + assert np.allclose(result, expected) def test_concat(): @@ -556,29 +482,17 @@ def test_concat(): parameter_list = [A, B, C] axis = 0 function = Function([ng.concat([A, B, C], axis)], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - - a = backend.create_tensor(element_type, Shape([1, 2])) - b = backend.create_tensor(element_type, Shape([1, 2])) - c = backend.create_tensor(element_type, Shape([1, 2])) - result = backend.create_tensor(element_type, Shape([3, 2])) - - a.write(util.numpy_to_c(np.array([1, 2], dtype=np.float32)), 8) - b.write(util.numpy_to_c(np.array([5, 6], dtype=np.float32)), 8) - c.write(util.numpy_to_c(np.array([7, 8], dtype=np.float32)), 8) - - result_arr = np.zeros(6, dtype=np.float32).reshape(3, 2) - result.write(util.numpy_to_c(result_arr), 24) - handle = backend.compile(function) - handle.call([result], [a, b, c]) - result.read(util.numpy_to_c(result_arr), 24) 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) - result_arr_ref = np.concatenate((a_arr, b_arr, c_arr), axis) - assert np.allclose(result_arr, result_arr_ref) + 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) def test_axisset(): @@ -598,7 +512,6 @@ def test_axisset(): def test_select(): - element_type = Type.f32 A = Parameter(Type.boolean, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) @@ -606,26 +519,17 @@ def test_select(): parameter_list = [A, B, C] function = Function([ng.select(A, B, C)], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(Type.boolean, Shape([1, 2])) - b = backend.create_tensor(element_type, Shape([1, 2])) - c = backend.create_tensor(element_type, Shape([1, 2])) - result = backend.create_tensor(element_type, Shape([1, 2])) + 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] - a.write(util.numpy_to_c(np.array([[True, False]], dtype=np.bool)), 2) - b.write(util.numpy_to_c(np.array([[5, 6]], dtype=np.float32)), 8) - c.write(util.numpy_to_c(np.array([[7, 8]], dtype=np.float32)), 8) - - result_arr = np.array([[0, 0]], dtype=np.float32) - result.write(util.numpy_to_c(result_arr), 8) - handle = backend.compile(function) - handle.call([result], [a, b, c]) - result.read(util.numpy_to_c(result_arr), 8) - - result_arr_ref = np.array([[5, 8]]) - - assert np.allclose(result_arr, result_arr_ref) + expected = np.array([[5, 8]]) + assert np.allclose(result, expected) def test_max_pool(): @@ -635,7 +539,7 @@ def test_max_pool(): A = Parameter(element_type, shape) parameter_list = [A] - input_arr = np.arange(10, dtype=np.float32).reshape(1, 1, 10) + input_arr = np.arange(10, dtype=np.float32).reshape([1, 1, 10]) window_shape = [3] strides = [1] * len(window_shape) @@ -644,21 +548,13 @@ def test_max_pool(): model = ng.max_pool(A, strides, pads_begin, pads_end, window_shape) function = Function([model], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, shape) - result = backend.create_tensor(element_type, Shape([1, 1, 8])) + runtime = get_runtime() + computation = runtime.computation(function, *parameter_list) + result = computation(input_arr)[0] - a.write(util.numpy_to_c(input_arr), 10 * 4) - - result_arr = np.zeros(8, dtype=np.float32).reshape(1, 1, 8) - result.write(util.numpy_to_c(result_arr), 8 * 4) - handle = backend.compile(function) - handle.call([result], [a]) - result.read(util.numpy_to_c(result_arr), 32) - - result_arr_ref = (np.arange(8) + 2).reshape(1, 1, 8) - assert np.allclose(result_arr, result_arr_ref) + expected = (np.arange(8) + 2).reshape(1, 1, 8) + assert np.allclose(result, expected) # test 1d with strides strides = [2] @@ -669,17 +565,11 @@ def test_max_pool(): function = Function([model], parameter_list, "test") size = 4 - result = backend.create_tensor(element_type, Shape([1, 1, size])) - result_arr = np.zeros(size, dtype=np.float32).reshape(1, 1, size) + computation = runtime.computation(function, *parameter_list) + result = computation(input_arr)[0] - backend = Backend.create(test.BACKEND_NAME) - result.write(util.numpy_to_c(result_arr), size * 4) - handle = backend.compile(function) - handle.call([result], [a]) - result.read(util.numpy_to_c(result_arr), size * 4) - - result_arr_ref = ((np.arange(size) + 1) * 2).reshape(1, 1, size) - assert np.allclose(result_arr, result_arr_ref) + expected = ((np.arange(size) + 1) * 2).reshape(1, 1, size) + assert np.allclose(result, expected) # test 2d element_type = Type.f32 @@ -697,20 +587,11 @@ def test_max_pool(): model = ng.max_pool(A, strides, pads_begin, pads_end, window_shape) function = Function([model], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, shape) - result = backend.create_tensor(element_type, Shape([1, 1, 8, 8])) + computation = runtime.computation(function, *parameter_list) + result = computation(input_arr)[0] - a.write(util.numpy_to_c(input_arr), 10 * 10 * 4) - - result_arr = np.zeros(64, dtype=np.float32).reshape(1, 1, 8, 8) - result.write(util.numpy_to_c(result_arr), 8 * 8 * 4) - handle = backend.compile(function) - handle.call([result], [a]) - result.read(util.numpy_to_c(result_arr), 8 * 8 * 4) - - result_arr_ref = ((np.arange(100).reshape(10, 10))[2:, 2:]).reshape(1, 1, 8, 8) - assert np.allclose(result_arr, result_arr_ref) + expected = ((np.arange(100).reshape(10, 10))[2:, 2:]).reshape(1, 1, 8, 8) + assert np.allclose(result, expected) # test 2d with strides strides = [2, 2] @@ -719,19 +600,12 @@ def test_max_pool(): model = ng.max_pool(A, strides, pads_begin, pads_end, window_shape) function = Function([model], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) + computation = runtime.computation(function, *parameter_list) + result = computation(input_arr)[0] size = 4 - result = backend.create_tensor(element_type, Shape([1, 1, size, size])) - result_arr = np.zeros(size * size, dtype=np.float32).reshape(1, 1, size, size) - - result.write(util.numpy_to_c(result_arr), size * size * 4) - handle = backend.compile(function) - handle.call([result], [a]) - result.read(util.numpy_to_c(result_arr), size * size * 4) - - result_arr_ref = ((np.arange(100).reshape(10, 10))[2::2, 2::2]).reshape(1, 1, size, size) - assert np.allclose(result_arr, result_arr_ref) + expected = ((np.arange(100).reshape(10, 10))[2::2, 2::2]).reshape(1, 1, size, size) + assert np.allclose(result, expected) def convolution2d( @@ -757,9 +631,7 @@ def convolution2d( (i_m + padding_below[0] + padding_above[0]) * (i_n + padding_below[1] + padding_above[1]), dtype=np.float32, ).reshape(i_m + padding_below[0] + padding_above[0], i_n + padding_below[1] + padding_above[1]) - new_image[ - padding_below[0] : padding_below[0] + i_m, padding_below[1] : padding_below[1] + i_n - ] = image + new_image[padding_below[0] : padding_below[0] + i_m, padding_below[1] : padding_below[1] + i_n] = image image = new_image image = image if data_dilation[0] == data_dilation[1] == 1 else dilate(image, data_dilation) i_m, i_n = image.shape @@ -777,9 +649,7 @@ def convolution2d( for i in range(r_m): for j in range(r_n): - sub_m = image[ - i * strides[0] : i * strides[0] + f_m, j * strides[1] : j * strides[1] + f_n - ] + sub_m = image[i * strides[0] : i * strides[0] + f_m, j * strides[1] : j * strides[1] + f_n] result[i][j] = np.sum(sub_m * filterit) return result @@ -800,7 +670,6 @@ def test_convolution_simple(): filter_arr[0][0][2][2] = -1 filter_arr[0][0][0][2] = -1 filter_arr[0][0][2][0] = -1 - result_arr = np.zeros(196, dtype=np.float32).reshape(1, 1, 14, 14) strides = [1, 1] pads_begin = [0, 0] @@ -809,22 +678,13 @@ def test_convolution_simple(): model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Function([model], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, image_shape) - b = backend.create_tensor(element_type, filter_shape) + runtime = get_runtime() + computation = runtime.computation(function, *parameter_list) + result = computation(image_arr, filter_arr)[0] - a.write(util.numpy_to_c(image_arr), 16 * 16 * 4) - b.write(util.numpy_to_c(filter_arr), 3 * 3 * 4) - - result = backend.create_tensor(element_type, Shape([1, 1, 14, 14])) - result.write(util.numpy_to_c(result_arr), 14 * 14 * 4) - handle = backend.compile(function) - handle.call([result], [a, b]) - result.read(util.numpy_to_c(result_arr), 14 * 14 * 4) - - result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14) - assert np.allclose(result_arr, result_arr_ref) + expected = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14) + assert np.allclose(result, expected) def test_convolution_with_strides(): @@ -836,8 +696,8 @@ def test_convolution_with_strides(): 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) + 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] pads_begin = [0, 0] @@ -846,23 +706,13 @@ def test_convolution_with_strides(): model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Function([model], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, image_shape) - b = backend.create_tensor(element_type, filter_shape) + runtime = get_runtime() + computation = runtime.computation(function, *parameter_list) + result = computation(image_arr, filter_arr)[0] - a.write(util.numpy_to_c(image_arr), 10 * 10 * 4) - b.write(util.numpy_to_c(filter_arr), 3 * 3 * 4) - - result_arr = np.zeros(16, dtype=np.float32).reshape(1, 1, 4, 4) - result = backend.create_tensor(element_type, Shape([1, 1, 4, 4])) - result.write(util.numpy_to_c(result_arr), 4 * 4 * 4) - handle = backend.compile(function) - handle.call([result], [a, b]) - - result.read(util.numpy_to_c(result_arr), 4 * 4 * 4) - result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides).reshape(1, 1, 4, 4) - assert np.allclose(result_arr, result_arr_ref) + expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides).reshape(1, 1, 4, 4) + assert np.allclose(result, expected) def test_convolution_with_filter_dilation(): @@ -874,8 +724,8 @@ def test_convolution_with_filter_dilation(): 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) + 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] @@ -883,25 +733,13 @@ def test_convolution_with_filter_dilation(): model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Function([model], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, image_shape) - b = backend.create_tensor(element_type, filter_shape) + runtime = get_runtime() + computation = runtime.computation(function, *parameter_list) + result = computation(image_arr, filter_arr)[0] - a.write(util.numpy_to_c(image_arr), 10 * 10 * 4) - b.write(util.numpy_to_c(filter_arr), 3 * 3 * 4) - - result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6) - result = backend.create_tensor(element_type, Shape([1, 1, 6, 6])) - result.write(util.numpy_to_c(result_arr), 6 * 6 * 4) - handle = backend.compile(function) - handle.call([result], [a, b]) - - result.read(util.numpy_to_c(result_arr), 6 * 6 * 4) - result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilations).reshape( - 1, 1, 6, 6 - ) - assert np.allclose(result_arr, result_arr_ref) + expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilations).reshape([1, 1, 6, 6]) + assert np.allclose(result, expected) def test_convolution_with_padding(): @@ -923,25 +761,15 @@ def test_convolution_with_padding(): model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Function([model], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, image_shape) - b = backend.create_tensor(element_type, filter_shape) + runtime = get_runtime() + computation = runtime.computation(function, *parameter_list) + result = computation(image_arr, filter_arr)[0] - a.write(util.numpy_to_c(image_arr), 10 * 10 * 4) - b.write(util.numpy_to_c(filter_arr), 3 * 3 * 4) - - result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6) - result = backend.create_tensor(element_type, Shape([1, 1, 6, 6])) - result.write(util.numpy_to_c(result_arr), 6 * 6 * 4) - handle = backend.compile(function) - handle.call([result], [a, b]) - - result.read(util.numpy_to_c(result_arr), 6 * 6 * 4) - result_arr_ref = convolution2d( + 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_arr, result_arr_ref) + ).reshape([1, 1, 6, 6]) + assert np.allclose(result, expected) def test_convolution_with_non_zero_padding(): @@ -962,22 +790,12 @@ def test_convolution_with_non_zero_padding(): model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Function([model], parameter_list, "test") - backend = Backend.create(test.BACKEND_NAME) - a = backend.create_tensor(element_type, image_shape) - b = backend.create_tensor(element_type, filter_shape) + runtime = get_runtime() + computation = runtime.computation(function, *parameter_list) + result = computation(image_arr, filter_arr)[0] - a.write(util.numpy_to_c(image_arr), 10 * 10 * 4) - b.write(util.numpy_to_c(filter_arr), 3 * 3 * 4) - - result_arr = np.zeros(81, dtype=np.float32).reshape(1, 1, 9, 9) - result = backend.create_tensor(element_type, Shape([1, 1, 9, 9])) - result.write(util.numpy_to_c(result_arr), 9 * 9 * 4) - handle = backend.compile(function) - handle.call([result], [a, b]) - - result.read(util.numpy_to_c(result_arr), 9 * 9 * 4) - result_arr_ref = convolution2d( + 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_arr, result_arr_ref) + ).reshape([1, 1, 9, 9]) + assert np.allclose(result, expected) diff --git a/ngraph/python/test/ngraph/test_ops_binary.py b/ngraph/python/tests/test_ngraph/test_ops_binary.py similarity index 95% rename from ngraph/python/test/ngraph/test_ops_binary.py rename to ngraph/python/tests/test_ngraph/test_ops_binary.py index d22af856f4b..d89b4355450 100644 --- a/ngraph/python/test/ngraph/test_ops_binary.py +++ b/ngraph/python/tests/test_ngraph/test_ops_binary.py @@ -19,7 +19,8 @@ import numpy as np import pytest import ngraph as ng -from test.ngraph.util import get_runtime, run_op_node +from tests.runtime import get_runtime +from tests.test_ngraph.util import run_op_node @pytest.mark.parametrize( @@ -95,11 +96,7 @@ def test_binary_op_with_scalar(ng_api_helper, numpy_function): @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.logical_and, np.logical_and), (ng.logical_or, np.logical_or), (ng.logical_xor, np.logical_xor),], ) def test_binary_logical_op(ng_api_helper, numpy_function): runtime = get_runtime() @@ -121,11 +118,7 @@ def test_binary_logical_op(ng_api_helper, numpy_function): @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.logical_and, np.logical_and), (ng.logical_or, np.logical_or), (ng.logical_xor, np.logical_xor),], ) def test_binary_logical_op_with_scalar(ng_api_helper, numpy_function): runtime = get_runtime() diff --git a/ngraph/python/test/ngraph/test_ops_fused.py b/ngraph/python/tests/test_ngraph/test_ops_fused.py similarity index 95% rename from ngraph/python/test/ngraph/test_ops_fused.py rename to ngraph/python/tests/test_ngraph/test_ops_fused.py index d75cc42cabc..feacfce51a1 100644 --- a/ngraph/python/test/ngraph/test_ops_fused.py +++ b/ngraph/python/tests/test_ngraph/test_ops_fused.py @@ -17,7 +17,7 @@ import numpy as np import pytest import ngraph as ng -from test.ngraph.util import get_runtime +from tests.runtime import get_runtime def test_elu_operator_with_scalar_and_array(): @@ -51,6 +51,7 @@ def test_elu_operator_with_scalar(): assert np.allclose(result, expected) +@pytest.mark.skip(reason="Causes segmentation fault") def test_fake_quantize(): runtime = get_runtime() @@ -86,9 +87,7 @@ def test_fake_quantize(): parameter_output_high, ) - result = computation( - data_value, input_low_value, input_high_value, output_low_value, output_high_value - ) + result = computation(data_value, input_low_value, input_high_value, output_low_value, output_high_value) expected = np.array( [ @@ -137,16 +136,7 @@ def test_depth_to_space(): 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], - ] - ] - ], + [[[[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) @@ -237,9 +227,7 @@ def test_gelu_operator_with_parameters(): computation = runtime.computation(model, parameter_data) result = computation(data_value) - expected = np.array( - [[-1.4901161e-06, 8.4134471e-01], [-4.5500278e-02, 2.9959502]], dtype=np.float32 - ) + expected = np.array([[-1.4901161e-06, 8.4134471e-01], [-4.5500278e-02, 2.9959502]], dtype=np.float32) assert np.allclose(result, expected, 0.007, 0.007) @@ -252,9 +240,7 @@ def test_gelu_operator_with_array(): computation = runtime.computation(model) result = computation() - expected = np.array( - [[-1.4901161e-06, 8.4134471e-01], [-4.5500278e-02, 2.9959502]], dtype=np.float32 - ) + expected = np.array([[-1.4901161e-06, 8.4134471e-01], [-4.5500278e-02, 2.9959502]], dtype=np.float32) assert np.allclose(result, expected, 0.007, 0.007) @@ -328,8 +314,6 @@ def test_squared_difference_operator(): assert np.allclose(result, expected) -@pytest.mark.skip_on_cpu -@pytest.mark.skip_on_interpreter def test_shuffle_channels_operator(): runtime = get_runtime() @@ -591,8 +575,6 @@ def test_space_to_depth_operator(): assert np.allclose(result, expected) -@pytest.mark.skip_on_cpu -def test_rnn_cell_operator(): runtime = get_runtime() batch_size = 2 @@ -691,9 +673,7 @@ def test_group_convolution_operator(): pads_begin = [0, 0] pads_end = [0, 0] - model = ng.group_convolution( - parameter_data, parameter_filters, strides, pads_begin, pads_end, dilations - ) + 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) @@ -812,9 +792,9 @@ 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) + 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) @@ -822,8 +802,7 @@ def test_group_convolution_backprop_data_output_shape(): 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, + [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) diff --git a/ngraph/python/test/ngraph/test_ops_matmul.py b/ngraph/python/tests/test_ngraph/test_ops_matmul.py similarity index 95% rename from ngraph/python/test/ngraph/test_ops_matmul.py rename to ngraph/python/tests/test_ngraph/test_ops_matmul.py index 7dee8f26ec8..17e1b3c94c1 100644 --- a/ngraph/python/test/ngraph/test_ops_matmul.py +++ b/ngraph/python/tests/test_ngraph/test_ops_matmul.py @@ -17,7 +17,8 @@ import numpy as np import pytest import ngraph as ng -from test.ngraph.util import run_op_node +from tests.runtime import get_runtime +from tests.test_ngraph.util import run_op_node @pytest.mark.parametrize( diff --git a/ngraph/python/test/ngraph/test_ops_multioutput.py b/ngraph/python/tests/test_ngraph/test_ops_multioutput.py similarity index 97% rename from ngraph/python/test/ngraph/test_ops_multioutput.py rename to ngraph/python/tests/test_ngraph/test_ops_multioutput.py index 34b56297593..43a4c2315ec 100644 --- a/ngraph/python/test/ngraph/test_ops_multioutput.py +++ b/ngraph/python/tests/test_ngraph/test_ops_multioutput.py @@ -16,7 +16,7 @@ import numpy as np import ngraph as ng -from test.ngraph.util import get_runtime +from tests.runtime import get_runtime def test_split(): diff --git a/ngraph/python/test/ngraph/test_ops_reshape.py b/ngraph/python/tests/test_ngraph/test_ops_reshape.py similarity index 97% rename from ngraph/python/test/ngraph/test_ops_reshape.py rename to ngraph/python/tests/test_ngraph/test_ops_reshape.py index 362c95937fd..5606a90c4e1 100644 --- a/ngraph/python/test/ngraph/test_ops_reshape.py +++ b/ngraph/python/tests/test_ngraph/test_ops_reshape.py @@ -17,7 +17,8 @@ import numpy as np import pytest import ngraph as ng -from test.ngraph.util import get_runtime, run_op_node, run_op_numeric_data +from tests.runtime import get_runtime +from tests.test_ngraph.util import run_op_node, run_op_numeric_data def test_concat(): @@ -142,7 +143,6 @@ def test_transpose(): assert np.allclose(result, expected) -@pytest.mark.skip_on_interpreter # unsupported op def test_tile(): input_tensor = np.arange(6).reshape((2, 1, 3)) repeats = np.array([2, 1]) @@ -175,8 +175,7 @@ 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)) + 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) diff --git a/ngraph/python/test/ngraph/test_ops_scatter.py b/ngraph/python/tests/test_ngraph/test_ops_scatter.py similarity index 99% rename from ngraph/python/test/ngraph/test_ops_scatter.py rename to ngraph/python/tests/test_ngraph/test_ops_scatter.py index 249ec62e196..4c648ef076c 100644 --- a/ngraph/python/test/ngraph/test_ops_scatter.py +++ b/ngraph/python/tests/test_ngraph/test_ops_scatter.py @@ -14,6 +14,7 @@ # limitations under the License. # ****************************************************************************** import numpy as np + import ngraph as ng from ngraph.impl import Type diff --git a/ngraph/python/test/ngraph/test_ops_unary.py b/ngraph/python/tests/test_ngraph/test_ops_unary.py similarity index 98% rename from ngraph/python/test/ngraph/test_ops_unary.py rename to ngraph/python/tests/test_ngraph/test_ops_unary.py index ffd3f83dfad..8ade909b771 100644 --- a/ngraph/python/test/ngraph/test_ops_unary.py +++ b/ngraph/python/tests/test_ngraph/test_ops_unary.py @@ -17,7 +17,7 @@ import numpy as np import pytest import ngraph as ng -from test.ngraph.util import run_op_numeric_data, run_op_node +from tests.test_ngraph.util import run_op_node, run_op_numeric_data @pytest.mark.parametrize( diff --git a/ngraph/python/test/ngraph/test_pooling.py b/ngraph/python/tests/test_ngraph/test_pooling.py similarity index 93% rename from ngraph/python/test/ngraph/test_pooling.py rename to ngraph/python/tests/test_ngraph/test_pooling.py index f230834ae8d..baecc90c89f 100644 --- a/ngraph/python/test/ngraph/test_pooling.py +++ b/ngraph/python/tests/test_ngraph/test_pooling.py @@ -17,7 +17,7 @@ import numpy as np import pytest import ngraph as ng -from test.ngraph.util import get_runtime +from tests.runtime import get_runtime @pytest.fixture @@ -106,9 +106,7 @@ def test_max_pool_basic(): comp = rt.computation(avgpool_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 = np.array([[[[5.5, 6.5, 7.5], [9.5, 10.5, 11.5], [13.5, 14.5, 15.5]]]], dtype=np.float32) assert np.allclose(result, expected) @@ -238,9 +236,7 @@ def test_max_pool_same_upper_auto_pads(): auto_pad = "same_upper" data_node = ng.parameter(data.shape, name="A", dtype=np.float32) - avgpool_node = ng.max_pool( - data_node, strides, pads_begin, pads_end, kernel_shape, auto_pad=auto_pad - ) + avgpool_node = ng.max_pool(data_node, strides, pads_begin, pads_end, kernel_shape, auto_pad=auto_pad) comp = rt.computation(avgpool_node, data_node) result = comp(data) @@ -280,23 +276,12 @@ def test_max_pool_same_lower_auto_pads(): auto_pad = "same_lower" data_node = ng.parameter(data.shape, name="A", dtype=np.float32) - avgpool_node = ng.max_pool( - data_node, strides, pads_begin, pads_end, kernel_shape, auto_pad=auto_pad - ) + avgpool_node = ng.max_pool(data_node, strides, pads_begin, pads_end, kernel_shape, auto_pad=auto_pad) comp = rt.computation(avgpool_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], - ] - ] - ], + [[[[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, ) assert np.allclose(result, expected) diff --git a/ngraph/python/test/ngraph/test_reduction.py b/ngraph/python/tests/test_ngraph/test_reduction.py similarity index 97% rename from ngraph/python/test/ngraph/test_reduction.py rename to ngraph/python/tests/test_ngraph/test_reduction.py index db34c46b8ac..48c4ce7cb15 100644 --- a/ngraph/python/test/ngraph/test_reduction.py +++ b/ngraph/python/tests/test_ngraph/test_reduction.py @@ -17,7 +17,8 @@ import numpy as np import pytest import ngraph as ng -from test.ngraph.util import run_op_node, get_runtime +from tests.runtime import get_runtime +from tests.test_ngraph.util import run_op_node @pytest.mark.parametrize( @@ -58,7 +59,6 @@ def test_reduction_ops(ng_api_helper, numpy_function, reduction_axes): (ng.reduce_logical_or, np.logical_or.reduce, [0, 1, 2, 3]), ], ) -@pytest.mark.skip_on_interpreter def test_reduction_logical_ops(ng_api_helper, numpy_function, reduction_axes): shape = [2, 4, 3, 2] np.random.seed(133391) @@ -89,8 +89,6 @@ def test_topk(): (ng.reduce_mean, np.mean, [0, 2]), ], ) -@pytest.mark.skip_on_cpu -@pytest.mark.skip_on_interpreter def test_reduce_mean_op(ng_api_helper, numpy_function, reduction_axes): shape = [2, 4, 3, 2] np.random.seed(133391) diff --git a/ngraph/python/test/ngraph/test_sequence_processing.py b/ngraph/python/tests/test_ngraph/test_sequence_processing.py similarity index 95% rename from ngraph/python/test/ngraph/test_sequence_processing.py rename to ngraph/python/tests/test_ngraph/test_sequence_processing.py index 3b4519351c4..4ebf1c11a13 100644 --- a/ngraph/python/test/ngraph/test_sequence_processing.py +++ b/ngraph/python/tests/test_ngraph/test_sequence_processing.py @@ -16,7 +16,8 @@ import numpy as np import ngraph as ng -from test.ngraph.util import get_runtime, run_op_node +from tests.runtime import get_runtime +from tests.test_ngraph.util import run_op_node def test_onehot(): diff --git a/ngraph/python/test/ngraph/util.py b/ngraph/python/tests/test_ngraph/util.py similarity index 95% rename from ngraph/python/test/ngraph/util.py rename to ngraph/python/tests/test_ngraph/util.py index 5d3b00d7056..323fad17dfa 100644 --- a/ngraph/python/test/ngraph/util.py +++ b/ngraph/python/tests/test_ngraph/util.py @@ -14,22 +14,19 @@ # limitations under the License. # ****************************************************************************** +from typing import Any, Callable, List + import numpy as np + import ngraph as ng from ngraph.utils.types import NumericData -from typing import Any, Callable, List -import test +from tests.runtime import get_runtime def _get_numpy_dtype(scalar): return np.array([scalar]).dtype -def get_runtime(): - """Return runtime object.""" - return ng.runtime(backend_name=test.BACKEND_NAME) - - def run_op_node(input_data, op_fun, *args): # type: (NumericData, Callable, *Any) -> List[NumericData] """Run computation on node performing `op_fun`. diff --git a/ngraph/python/tox.ini b/ngraph/python/tox.ini index d9585f63a36..d09c07a02ad 100644 --- a/ngraph/python/tox.ini +++ b/ngraph/python/tox.ini @@ -60,8 +60,15 @@ show_none_errors = True [mypy-test.*] disallow_untyped_defs = False +[isort] +multi_line_output=3 +include_trailing_comma=True +force_grid_wrap=0 +use_parentheses=True +line_length=100 +default_section=FIRSTPARTY +sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER +known_third_party=openvino + [pytest] -markers = - skip_on_cpu: skip test on the CPU backend - skip_on_interpreter: skip test on the interpreter_skip backend - skip_on_inference_engine: skip test on the IE backend +addopts = --strict-markers