From 07932907626e3d035ff7479e434a554893e5c1c3 Mon Sep 17 00:00:00 2001 From: Alexey Lebedev Date: Thu, 21 Oct 2021 12:03:23 +0300 Subject: [PATCH] [IE PYTHON] optimize blob creation with numpy (#7813) * Optimize blob constructor and remove user blobs * restore user_blobs * Rename helper function * add test * Remove empty line * Fix code style * add const * Add test for SCALAR layout --- .../python/src/openvino/inference_engine/ie_api.pyx | 7 +++---- .../src/openvino/inference_engine/ie_api_impl.cpp | 4 ++++ .../src/openvino/inference_engine/ie_api_impl.hpp | 4 ++++ .../openvino/inference_engine/ie_api_impl_defs.pxd | 2 ++ inference-engine/ie_bridges/python/tests/test_Blob.py | 11 ++++++++--- 5 files changed, 21 insertions(+), 7 deletions(-) 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 5f595850a05..e3e9603ba90 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 @@ -206,10 +206,9 @@ cdef class Blob: elif tensor_desc is not None and self._array_data is not None: c_tensor_desc = tensor_desc.impl precision = tensor_desc.precision - size_arr = np.prod(array.shape) - size_td = np.prod(tensor_desc.dims) - if size_arr != size_td: - raise AttributeError(f"Number of elements in provided numpy array {size_arr} and " + size_td = C.product(c_tensor_desc.getDims()) + if array.size != size_td: + raise AttributeError(f"Number of elements in provided numpy array {array.size} and " f"required by TensorDesc {size_td} are not equal") if self._array_data.dtype != format_map[precision]: raise ValueError(f"Data type {self._array_data.dtype} of provided numpy array " diff --git a/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl.cpp b/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl.cpp index ff252d9923b..e1442b139c0 100644 --- a/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl.cpp +++ b/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl.cpp @@ -682,3 +682,7 @@ InferenceEngine::Blob::Ptr InferenceEnginePython::CVariableState::getState() { void InferenceEnginePython::CVariableState::setState(InferenceEngine::Blob::Ptr state) { variableState.SetState(state); } + +const size_t InferenceEnginePython::product(const InferenceEngine::SizeVector& dims) { + return std::accumulate(dims.begin(), dims.end(), 1, std::multiplies{}); +} diff --git a/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl.hpp b/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl.hpp index 6d479784d14..48fc2fd9c9a 100644 --- a/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl.hpp +++ b/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl.hpp @@ -7,11 +7,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -203,4 +205,6 @@ InferenceEnginePython::IENetwork read_network(std::string path_to_xml, std::stri PyObject* getPartialShape_capsule(InferenceEngine::CDataPtr data); +const size_t product(const InferenceEngine::SizeVector& dims); + }; // namespace InferenceEnginePython diff --git a/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl_defs.pxd b/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl_defs.pxd index 9f60f536257..95629f7133b 100644 --- a/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl_defs.pxd +++ b/inference-engine/ie_bridges/python/src/openvino/inference_engine/ie_api_impl_defs.pxd @@ -230,3 +230,5 @@ cdef extern from "ie_api_impl.hpp" namespace "InferenceEnginePython": cdef IENetwork read_network(string path_to_xml, string path_to_bin) cdef object getPartialShape_capsule(DataPtr) + + cdef const size_t product(const SizeVector& dims) diff --git a/inference-engine/ie_bridges/python/tests/test_Blob.py b/inference-engine/ie_bridges/python/tests/test_Blob.py index 5a90d1228a1..75a557fac9a 100644 --- a/inference-engine/ie_bridges/python/tests/test_Blob.py +++ b/inference-engine/ie_bridges/python/tests/test_Blob.py @@ -21,11 +21,16 @@ def test_init_with_tensor_desc(): assert blob.tensor_desc == tensor_desc -def test_init_with_numpy(): - tensor_desc = TensorDesc("FP32", [1, 3, 127, 127], "NCHW") - array = np.ones(shape=(1, 3, 127, 127), dtype=np.float32) +@pytest.mark.parametrize("shape, layout", [ + ([1, 3, 127, 127], "NCHW"), + ([], "SCALAR"), +]) +def test_init_with_numpy(shape, layout): + tensor_desc = TensorDesc("FP32", shape, layout) + array = np.ones(shape=shape, dtype=np.float32) blob = Blob(tensor_desc, array) assert isinstance(blob.buffer, np.ndarray) + assert np.shares_memory(blob.buffer, array) assert blob.tensor_desc == tensor_desc