OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[core] Move get_memory_size to memory util (#31772)
Pawel Raasz43b3c4a0ebb6
diff --git a/src/core/dev_api/openvino/core/memory_util.hpp b/src/core/dev_api/openvino/core/memory_util.hppindex 5ece113307d..cdf6a98bb45 100644--- a/src/core/dev_api/openvino/core/memory_util.hpp+++ b/src/core/dev_api/openvino/core/memory_util.hpp@@ -11,6 +11,16 @@namespace ov::util {+/**+ * @brief Gets size of memory in bytes for N elements of given precision.+ *+ * @param type Element precision.+ * @param n Number of elements.+ *+ * @return Elements size in bytes.+ */+OPENVINO_API size_t get_memory_size(const element::Type& type, const size_t n);+/*** @brief Gets size of memory in bytes for N elements of given precision if there is no overflow.*diff --git a/src/core/dev_api/openvino/core/type/element_iterator.hpp b/src/core/dev_api/openvino/core/type/element_iterator.hppindex aed11325447..493548337cb 100644--- a/src/core/dev_api/openvino/core/type/element_iterator.hpp+++ b/src/core/dev_api/openvino/core/type/element_iterator.hpp@@ -634,15 +634,5 @@ template <Type_t ET, class T = typename std::add_const<ov::fundamental_type_for<constexpr auto iterator(const void* ptr) -> decltype(iterator<ET, T>(reinterpret_cast<T*>(ptr))) {return iterator<ET, T>(reinterpret_cast<T*>(ptr));}--/**- * @brief Gets size of memory in bytes for N elements of given precision.- *- * @param type Element precision.- * @param n Number of elements.- *- * @return Elements size in bytes.- */-OPENVINO_API size_t get_memory_size(const element::Type& type, const size_t n);} // namespace element} // namespace ovdiff --git a/src/core/src/descriptor/tensor.cpp b/src/core/src/descriptor/tensor.cppindex 816652dc7d3..0fa9b4f2857 100644--- a/src/core/src/descriptor/tensor.cpp+++ b/src/core/src/descriptor/tensor.cpp@@ -7,8 +7,8 @@#include "atomic_guard.hpp"#include "openvino/core/descriptor_tensor.hpp"#include "openvino/core/except.hpp"+#include "openvino/core/memory_util.hpp"#include "openvino/core/node.hpp"-#include "openvino/core/type/element_iterator.hpp"#include "openvino/op/util/symbolic_info.hpp"#include "openvino/util/common_util.hpp"@@ -213,7 +213,7 @@ const Shape& Tensor::get_shape() const {}size_t Tensor::size() const {- return element::get_memory_size(get_element_type(), shape_size(get_shape()));+ return ov::util::get_memory_size(get_element_type(), shape_size(get_shape()));}const std::unordered_set<std::string>& Tensor::get_names() const {diff --git a/src/core/src/memory_util.cpp b/src/core/src/memory_util.cppindex cccee0023b8..bbb65c7a9ae 100644--- a/src/core/src/memory_util.cpp+++ b/src/core/src/memory_util.cpp@@ -9,27 +9,42 @@#include "openvino/util/common_util.hpp"namespace ov::util {+namespace {+size_t get_split_bit_memory_size(const element::Type& type, const size_t shape_size) {+ constexpr size_t storage_unit_size = 24;+ const size_t elements_per_storage_unit = storage_unit_size / type.bitwidth();+ auto byte_size = shape_size / elements_per_storage_unit;+ byte_size += static_cast<size_t>(byte_size * elements_per_storage_unit != shape_size);+ return byte_size * 3;+}-std::optional<size_t> get_memory_size_safe(const element::Type& type, const size_t n) {- auto byte_size = std::make_optional(n);+size_t get_bit_memory_size(const element::Type& type, const size_t shape_size) {+ constexpr size_t storage_unit_size = 8;+ const auto elements_per_storage_unit = storage_unit_size / type.bitwidth();+ auto byte_size = shape_size / elements_per_storage_unit;+ byte_size += static_cast<size_t>((byte_size * elements_per_storage_unit) != shape_size);+ return byte_size;+}+} // namespace- if (n != 0) {- if (element::is_split_bit_type(type)) {- constexpr size_t storage_unit_size = 24;- const size_t elements_per_storage_unit = storage_unit_size / type.bitwidth();- *byte_size /= elements_per_storage_unit;- *byte_size += static_cast<size_t>(*byte_size * elements_per_storage_unit != n);- *byte_size *= 3;- } else if (element::is_bit_type(type) || element::is_nibble_type(type)) {- constexpr size_t storage_unit_size = 8;- const auto elements_per_storage_unit = storage_unit_size / type.bitwidth();- *byte_size /= elements_per_storage_unit;- *byte_size += static_cast<size_t>((*byte_size * elements_per_storage_unit) != n);- } else if (mul_overflow<size_t>(type.bitwidth() / 8, *byte_size, *byte_size)) {- byte_size.reset();- }+size_t get_memory_size(const element::Type& type, const size_t n) {+ if (n == 0) {+ return n;+ } else if (element::is_split_bit_type(type)) {+ return get_split_bit_memory_size(type, n);+ } else if (element::is_bit_type(type) || element::is_nibble_type(type)) {+ return get_bit_memory_size(type, n);+ } else {+ return (type.bitwidth() / 8) * n;+ }+}++std::optional<size_t> get_memory_size_safe(const element::Type& type, const size_t n) {+ if (auto s = type.bitwidth(); s >= 8) {+ return mul_overflow<size_t>(s / 8, n, s) ? std::nullopt : std::make_optional(s);+ } else {+ return std::make_optional(ov::util::get_memory_size(type, n));}- return byte_size;}std::optional<size_t> get_memory_size_safe(const element::Type& type, const ov::Shape& shape) {diff --git a/src/core/src/op/constant.cpp b/src/core/src/op/constant.cppindex 48656a82465..4515a53e985 100644--- a/src/core/src/op/constant.cpp+++ b/src/core/src/op/constant.cpp@@ -312,7 +312,7 @@ Constant::Constant(const element::Type& type, const Shape& shape, const void* daconst auto dst_strings = static_cast<std::string*>(get_data_ptr_nc());std::uninitialized_copy_n(src_strings, num_elements, dst_strings);} else {- std::memcpy(get_data_ptr_nc(), data, element::get_memory_size(m_element_type, num_elements));+ std::memcpy(get_data_ptr_nc(), data, ov::util::get_memory_size(m_element_type, num_elements));}}@@ -354,7 +354,7 @@ Constant::Constant(const element::Type& type, const Shape& shape, const void* dashape,// Note: const_cast used to store pointer onlystd::make_shared<ov::SharedBuffer<std::shared_ptr<void>>>(reinterpret_cast<char*>(const_cast<void*>(data)),- element::get_memory_size(type, shape_size(shape)),+ ov::util::get_memory_size(type, shape_size(shape)),so)) {}Constant::~Constant() = default;diff --git a/src/core/src/runtime/itensor.cpp b/src/core/src/runtime/itensor.cppindex 5019e0d8451..66e9a9c7ce5 100644--- a/src/core/src/runtime/itensor.cpp+++ b/src/core/src/runtime/itensor.cpp@@ -8,8 +8,8 @@#include "compare.hpp"#include "openvino/core/except.hpp"+#include "openvino/core/memory_util.hpp"#include "openvino/core/shape_util.hpp"-#include "openvino/core/type/element_iterator.hpp"#include "openvino/runtime/allocator.hpp"#include "openvino/runtime/iremote_tensor.hpp"#include "openvino/runtime/make_tensor.hpp"@@ -39,7 +39,7 @@ size_t ITensor::get_size() const {}size_t ITensor::get_byte_size() const {- return element::get_memory_size(get_element_type(), get_size());+ return util::get_memory_size(get_element_type(), get_size());}bool ITensor::is_continuous() const {diff --git a/src/core/src/type/element_iterator.cpp b/src/core/src/type/element_iterator.cppdeleted file mode 100644index fbae0fdbd8a..00000000000--- a/src/core/src/type/element_iterator.cpp+++ /dev/null@@ -1,26 +0,0 @@-// Copyright (C) 2018-2025 Intel Corporation-// SPDX-License-Identifier: Apache-2.0-//--#include "openvino/core/type/element_iterator.hpp"--namespace ov {--namespace element {--size_t get_memory_size(const element::Type& type, const size_t n) {- auto byte_size = n * type.bitwidth();- if (element::is_split_bit_type(type)) {- constexpr size_t storage_unit_size = 24;- byte_size += storage_unit_size - 1;- byte_size /= storage_unit_size;- byte_size *= 3;- } else {- constexpr size_t storage_unit_size = 8;- byte_size += storage_unit_size - 1;- byte_size /= storage_unit_size;- }- return byte_size;-}-} // namespace element-} // namespace ovdiff --git a/src/core/tests/ov_tensor_test.cpp b/src/core/tests/ov_tensor_test.cppindex 2adce29761b..189b5449ea3 100644--- a/src/core/tests/ov_tensor_test.cpp+++ b/src/core/tests/ov_tensor_test.cpp@@ -477,7 +477,6 @@ TEST_F(OVTensorTest, canSetShapeStringTensor) {{constexpr auto max_dim = std::numeric_limits<size_t>::max();OV_EXPECT_THROW(t.set_shape(Shape({3, max_dim / 80})), ov::Exception, _);- OV_EXPECT_THROW(t.set_shape(Shape({1, max_dim / 80})), ov::Exception, _);}}diff --git a/src/inference/src/dev/make_tensor.cpp b/src/inference/src/dev/make_tensor.cppindex 20432273c72..834aac9ca08 100644--- a/src/inference/src/dev/make_tensor.cpp+++ b/src/inference/src/dev/make_tensor.cpp@@ -8,7 +8,6 @@#include <mutex>#include "openvino/core/memory_util.hpp"-#include "openvino/core/type/element_iterator.hpp"#include "openvino/core/type/element_type_info.hpp"#include "openvino/runtime/iremote_tensor.hpp"#include "openvino/runtime/properties.hpp"@@ -351,7 +350,7 @@ private:}size_t get_bytes_capacity() const {- return element::get_memory_size(get_element_type(), get_capacity());+ return util::get_memory_size(get_element_type(), get_capacity());}static std::string bad_alloc_error_msg(const element::Type& element_type, const Shape& shape) {diff --git a/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp b/src/plugins/intel_npu/src/backend/src/zero_tensor.cppindex 1db3d0e4256..de6453055db 100644--- a/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp+++ b/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp@@ -6,7 +6,6 @@#include "intel_npu/config/options.hpp"#include "openvino/core/memory_util.hpp"-#include "openvino/core/type/element_iterator.hpp"#include "openvino/runtime/properties.hpp"#include "openvino/runtime/tensor.hpp"@@ -120,7 +119,7 @@ size_t ZeroTensor::get_capacity() const {}size_t ZeroTensor::get_bytes_capacity() const {- return ov::element::get_memory_size(get_element_type(), get_capacity());+ return ov::util::get_memory_size(get_element_type(), get_capacity());}void ZeroTensor::destroy_elements(size_t begin_ind, size_t end_ind) {diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cppindex 99f6f7e8af5..f6008434834 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp@@ -12,9 +12,9 @@#include "intel_npu/config/options.hpp"#include "intel_npu/prefix.hpp"#include "intel_npu/utils/utils.hpp"+#include "openvino/core/memory_util.hpp"#include "openvino/core/model.hpp"#include "openvino/core/rt_info/weightless_caching_attributes.hpp"-#include "openvino/core/type/element_iterator.hpp"#include "openvino/runtime/make_tensor.hpp"#define USE_SINGLE_THREADED_RUN_INIT 0@@ -393,7 +393,7 @@ WeightlessGraph::InputData WeightlessGraph::allocate_inputs(for (const IODescriptor& descriptor : _initsMetadata.at(initIndex).inputs) {initInputsByteSize +=- ov::element::get_memory_size(descriptor.precision, shape_size(descriptor.shapeFromCompiler.to_shape()));+ ov::util::get_memory_size(descriptor.precision, shape_size(descriptor.shapeFromCompiler.to_shape()));}// Due to the large number of init inputs, allocating a single buffer for all of them is more efficient. "View@@ -411,7 +411,7 @@ WeightlessGraph::InputData WeightlessGraph::allocate_inputs(static_cast<unsigned char*>(const_cast<void*>(initInputsAllocatedTensor->data(ov::element::Type_t::u8))) +offset;const size_t currentInputSize =- ov::element::get_memory_size(descriptor.precision, shape_size(descriptor.shapeFromCompiler.to_shape()));+ ov::util::get_memory_size(descriptor.precision, shape_size(descriptor.shapeFromCompiler.to_shape()));std::shared_ptr<ov::op::v0::Constant> constant;const size_t id = std::stoi(descriptor.nameFromCompiler);@@ -440,7 +440,7 @@ WeightlessGraph::OutputData WeightlessGraph::allocate_outputs(const size_t initIfor (const IODescriptor& descriptor : _initsMetadata.at(initIndex).outputs) {initOutputsByteSize +=- ov::element::get_memory_size(descriptor.precision, shape_size(descriptor.shapeFromCompiler.to_shape()));+ ov::util::get_memory_size(descriptor.precision, shape_size(descriptor.shapeFromCompiler.to_shape()));}const ov::SoPtr<ZeroHostTensor> initOutputsAllocatedTensor = {@@ -461,8 +461,7 @@ WeightlessGraph::OutputData WeightlessGraph::allocate_outputs(const size_t initIinitOutputsViewTensorsVector.push_back(hostTensor._ptr);initOutputsViewTensorsMap.emplace(descriptor.nameFromCompiler, hostTensor._ptr);- offset +=- ov::element::get_memory_size(descriptor.precision, shape_size(descriptor.shapeFromCompiler.to_shape()));+ offset += ov::util::get_memory_size(descriptor.precision, shape_size(descriptor.shapeFromCompiler.to_shape()));}return {initOutputsViewTensorsVector, initOutputsAllocatedTensor, initOutputsViewTensorsMap};diff --git a/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/dma_buf_remote_run.hpp b/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/dma_buf_remote_run.hppindex 5cb9b2d2f31..ac1aaa67de3 100644--- a/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/dma_buf_remote_run.hpp+++ b/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/dma_buf_remote_run.hpp@@ -12,7 +12,7 @@#include "common/npu_test_env_cfg.hpp"#include "common/utils.hpp"#include "openvino/core/any.hpp"-#include "openvino/core/type/element_iterator.hpp"+#include "openvino/core/memory_util.hpp"#include "openvino/runtime/compiled_model.hpp"#include "openvino/runtime/core.hpp"#include "openvino/runtime/intel_npu/level_zero/level_zero.hpp"@@ -122,7 +122,7 @@ TEST_P(DmaBufRemoteRunTests, CheckRemoteTensorSharedBuf) {OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());auto tensor = inference_request.get_input_tensor();- const auto byte_size = ov::element::get_memory_size(ov::element::f32, shape_size(tensor.get_shape()));+ const auto byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(tensor.get_shape()));auto context = core->get_default_context(target_device).as<ov::intel_npu::level_zero::ZeroContext>();@@ -154,7 +154,7 @@ TEST_P(DmaBufRemoteRunTests, CheckRemoteTensorSharedBuChangingTensors) {OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());auto tensor = inference_request.get_input_tensor();- const auto byte_size = ov::element::get_memory_size(ov::element::f32, shape_size(tensor.get_shape()));+ const auto byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(tensor.get_shape()));auto context = core->get_default_context(target_device).as<ov::intel_npu::level_zero::ZeroContext>();@@ -184,7 +184,7 @@ TEST_P(DmaBufRemoteRunTests, CheckRemoteTensorSharedBuChangingTensors) {// set random output tensorauto output_tensor = inference_request.get_output_tensor();- const auto output_byte_size = ov::element::get_memory_size(ov::element::f32, shape_size(output_tensor.get_shape()));+ const auto output_byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(output_tensor.get_shape()));float* output_random_buffer_tensor = new float[output_byte_size / sizeof(float)];memset(output_random_buffer_tensor, 1, output_byte_size);@@ -210,7 +210,7 @@ TEST_P(DmaBufRemoteRunTests, CheckOutputDataFromMultipleRuns) {auto tensor = inference_request.get_input_tensor();auto shape = tensor.get_shape();- const auto byte_size = ov::element::get_memory_size(ov::element::f32, shape_size(shape));+ const auto byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(shape));tensor = {};auto fd_heap = getFdDmaHeap(byte_size);diff --git a/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/dx12_remote_run.hpp b/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/dx12_remote_run.hppindex 18587d21e34..eb79198b54e 100644--- a/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/dx12_remote_run.hpp+++ b/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/dx12_remote_run.hpp@@ -12,7 +12,7 @@#include "common/npu_test_env_cfg.hpp"#include "common/utils.hpp"#include "openvino/core/any.hpp"-#include "openvino/core/type/element_iterator.hpp"+#include "openvino/core/memory_util.hpp"#include "openvino/runtime/compiled_model.hpp"#include "openvino/runtime/core.hpp"#include "openvino/runtime/intel_npu/level_zero/level_zero.hpp"@@ -267,7 +267,7 @@ TEST_P(DX12RemoteRunTests, CheckRemoteTensorSharedBuf) {OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());auto tensor = inference_request.get_input_tensor();- const auto byte_size = ov::element::get_memory_size(ov::element::f32, shape_size(tensor.get_shape()));+ const auto byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(tensor.get_shape()));auto context = core->get_default_context(target_device).as<ov::intel_npu::level_zero::ZeroContext>();@@ -292,7 +292,7 @@ TEST_P(DX12RemoteRunTests, CheckRemoteTensorSharedBuChangingTensors) {OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());auto tensor = inference_request.get_input_tensor();- const auto byte_size = ov::element::get_memory_size(ov::element::f32, shape_size(tensor.get_shape()));+ const auto byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(tensor.get_shape()));auto context = core->get_default_context(target_device).as<ov::intel_npu::level_zero::ZeroContext>();@@ -317,7 +317,7 @@ TEST_P(DX12RemoteRunTests, CheckRemoteTensorSharedBuChangingTensors) {// set random output tensorauto output_tensor = inference_request.get_output_tensor();- const auto output_byte_size = ov::element::get_memory_size(ov::element::f32, shape_size(output_tensor.get_shape()));+ const auto output_byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(output_tensor.get_shape()));float* output_random_buffer_tensor = new float[output_byte_size / sizeof(float)];memset(output_random_buffer_tensor, 1, output_byte_size);@@ -341,7 +341,7 @@ TEST_P(DX12RemoteRunTests, CheckOutputDataFromMultipleRuns) {auto tensor = inference_request.get_input_tensor();auto shape = tensor.get_shape();- const auto byte_size = ov::element::get_memory_size(ov::element::f32, shape_size(shape));+ const auto byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(shape));tensor = {};createResources(byte_size);