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] Tensor::data member preserve constness of tensor (#29594)
Pawel Raaszd119656f9f2e
diff --git a/samples/cpp/benchmark_app/inputs_filling.cpp b/samples/cpp/benchmark_app/inputs_filling.cppindex dda40cd9dd0..ce77e43adda 100644--- a/samples/cpp/benchmark_app/inputs_filling.cpp+++ b/samples/cpp/benchmark_app/inputs_filling.cpp@@ -1015,6 +1015,7 @@ void copy_tensor_data(ov::Tensor& dst, const ov::Tensor& src) {throw std::runtime_error("Source and destination tensors shapes and byte sizes are expected to be equal for data copying.");}-- memcpy(dst.data(), src.data(), src.get_byte_size());+ OPENVINO_SUPPRESS_DEPRECATED_START // keep until 2026.0 release+ memcpy(dst.data(), src.data(), src.get_byte_size());+ OPENVINO_SUPPRESS_DEPRECATED_END // keep until 2026.0 release}diff --git a/samples/cpp/common/utils/include/samples/classification_results.h b/samples/cpp/common/utils/include/samples/classification_results.hindex 26119a2c917..9be3bd6a279 100644--- a/samples/cpp/common/utils/include/samples/classification_results.h+++ b/samples/cpp/common/utils/include/samples/classification_results.h@@ -66,7 +66,10 @@ private:for (size_t i = 0; i < batchSize; i++) {const size_t offset = i * (input.get_size() / batchSize);- const T* batchData = input.data<const T>();+ OPENVINO_SUPPRESS_DEPRECATED_START // keep until 2026.0 release+ const T* batchData = input.data<T>();+ OPENVINO_SUPPRESS_DEPRECATED_END+batchData += offset;std::iota(std::begin(indexes), std::end(indexes), 0);diff --git a/samples/cpp/model_creation_sample/main.cpp b/samples/cpp/model_creation_sample/main.cppindex 8d6e1c271d4..523424138fd 100644--- a/samples/cpp/model_creation_sample/main.cpp+++ b/samples/cpp/model_creation_sample/main.cpp@@ -77,7 +77,9 @@ ov::Tensor read_weights(const std::string& filepath) {*/std::shared_ptr<ov::Model> create_model(const std::string& path_to_weights) {const ov::Tensor weights = read_weights(path_to_weights);- const std::uint8_t* data = weights.data<std::uint8_t>();+ OPENVINO_SUPPRESS_DEPRECATED_START // keep until 2026.0 release+ const std::uint8_t* data = weights.data<std::uint8_t>();+ OPENVINO_SUPPRESS_DEPRECATED_END// -------input------std::vector<ptrdiff_t> padBegin{0, 0};diff --git a/src/core/dev_api/openvino/runtime/itensor.hpp b/src/core/dev_api/openvino/runtime/itensor.hppindex 72b5d592df3..49c0b37d2b6 100644--- a/src/core/dev_api/openvino/runtime/itensor.hpp+++ b/src/core/dev_api/openvino/runtime/itensor.hpp@@ -51,24 +51,32 @@ public:virtual const ov::Strides& get_strides() const = 0;/**- * @brief Provides an access to the underlaying host memory+ * @brief Provides an access to the underlying host memory* @param type Optional type parameter.* @note If type parameter is specified, the method throws an exception* if specified type's fundamental type does not match with tensor element type's fundamental type* @return A host pointer to tensor memory+ * @{*/- virtual void* data(const element::Type& type = {}) const = 0;+ virtual void* data(const element::Type& type = {});+ virtual const void* data(const element::Type& type = {}) const = 0;+ /// @}/**- * @brief Provides an access to the underlaying host memory casted to type `T`+ * @brief Provides an access to the underlying host memory casted to type `T`* @return A host pointer to tensor memory casted to specified type `T`.* @note Throws exception if specified type does not match with tensor element type*/template <typename T, typename datatype = typename std::decay<T>::type>- T* data() const {+ T* data() {return static_cast<T*>(data(element::from<datatype>()));}+ template <typename T, typename datatype = typename std::decay<T>::type>+ const T* data() const {+ return static_cast<const T*>(data(element::from<datatype>()));+ }+/*** @brief Reports whether the tensor is continuous or not*diff --git a/src/core/include/openvino/runtime/tensor.hpp b/src/core/include/openvino/runtime/tensor.hppindex 830b1db694d..3521b5784f9 100644--- a/src/core/include/openvino/runtime/tensor.hpp+++ b/src/core/include/openvino/runtime/tensor.hpp@@ -121,6 +121,17 @@ public:*/Tensor(const element::Type& type, const Shape& shape, void* host_ptr, const Strides& strides = {});+ /**+ * @brief Constructs Tensor using element type and shape. Wraps allocated host memory as read only.+ * @note Does not perform memory allocation internally+ * @param type Tensor element type+ * @param shape Tensor shape+ * @param host_ptr Pointer to pre-allocated host memory with initialized objects+ * @param strides Optional strides parameters in bytes. Strides are supposed to be computed automatically based+ * on shape and element size+ */+ Tensor(const element::Type& type, const Shape& shape, const void* host_ptr, const Strides& strides = {});+/*** @brief Constructs Tensor using port from node. Allocate internal host storage using default allocator* @param port port from node@@ -138,6 +149,16 @@ public:*/Tensor(const ov::Output<const ov::Node>& port, void* host_ptr, const Strides& strides = {});+ /**+ * @brief Constructs Tensor using port from node. Wraps allocated host memory as read only.+ * @note Does not perform memory allocation internally+ * @param port port from node+ * @param host_ptr Pointer to pre-allocated host memory with initialized objects+ * @param strides Optional strides parameters in bytes. Strides are supposed to be computed automatically based+ * on shape and element size+ */+ Tensor(const ov::Output<const ov::Node>& port, const void* host_ptr, const Strides& strides = {});+/*** @brief Constructs region of interest (ROI) tensor form another tensor.* @note Does not perform memory allocation internally@@ -197,23 +218,48 @@ public:Strides get_strides() const;/**- * @brief Provides an access to the underlaying host memory+ * @brief Provides an access to the underlying host memory* @param type Optional type parameter.* @note If type parameter is specified, the method throws an exception* if specified type's fundamental type does not match with tensor element type's fundamental type* @return A host pointer to tensor memory+ * @{*/+#ifndef IN_OV_COMPONENT+ OPENVINO_DEPRECATED("This function will return const void* in 2026.0. Check if used correctly")+#endifvoid* data(const element::Type& type = {}) const;+ void* data(const element::Type& type = {});+ /// @}/**- * @brief Provides an access to the underlaying host memory casted to type `T`+ * @brief Provides an access to the underlying host memory casted to type `T`* @return A host pointer to tensor memory casted to specified type `T`.* @note Throws exception if specified type does not match with tensor element type+ * @{*/- template <typename T, typename datatype = typename std::decay<T>::type>+ template <typename T, typename datatype = std::decay_t<T>>+#ifndef IN_OV_COMPONENT+ OPENVINO_DEPRECATED("This function will return const T* in 2026.0. Check if used correctly")+#endifT* data() const {- return static_cast<T*>(data(element::from<datatype>()));+ OPENVINO_SUPPRESS_DEPRECATED_START // keep until 2026.0 release+ return static_cast<T*>(data(element::from<datatype>()));+ OPENVINO_SUPPRESS_DEPRECATED_END // keep until 2026.0 release+ }++ template <typename T, typename datatype = std::decay_t<T>>+ T* data() {+ if constexpr (std::is_const_v<T>) {+ OPENVINO_SUPPRESS_DEPRECATED_START // keep until 2026.0 release+ return std::as_const(*this)+ .data<T>();+ OPENVINO_SUPPRESS_DEPRECATED_END // keep until 2026.0 release+ } else {+ return static_cast<T*>(data(element::from<datatype>()));+ }}+ /// @}/*** @brief Checks if current Tensor object is not initialized@@ -234,7 +280,7 @@ public:* @return true if this object can be dynamically cast to the type const T*. Otherwise, false*/template <typename T>- typename std::enable_if<std::is_base_of<Tensor, T>::value, bool>::type is() const noexcept {+ std::enable_if_t<std::is_base_of_v<Tensor, T>, bool> is() const noexcept {try {T::type_check(*this);} catch (...) {@@ -250,7 +296,7 @@ public:* @return T object*/template <typename T>- const typename std::enable_if<std::is_base_of<Tensor, T>::value, T>::type as() const {+ const std::enable_if_t<std::is_base_of_v<Tensor, T>, T> as() const {T::type_check(*this);return *static_cast<const T*>(this);}diff --git a/src/core/reference/include/openvino/reference/interpolate.hpp b/src/core/reference/include/openvino/reference/interpolate.hppindex dae5fc47975..a6009557924 100644--- a/src/core/reference/include/openvino/reference/interpolate.hpp+++ b/src/core/reference/include/openvino/reference/interpolate.hpp@@ -653,7 +653,7 @@ void interpolate(const T* input_data,}template <typename T>-void interpolate(T* input_data,+void interpolate(const T* input_data,const PartialShape& input_data_shape,T* out,const Shape& out_shape,@@ -667,7 +667,7 @@ void interpolate(T* input_data,size_t bytes_in_padded_input = shape_size(padded_input_shape) * sizeof(T);std::vector<uint8_t> padded_input_data(bytes_in_padded_input, 0);uint8_t* padded_data_ptr = padded_input_data.data();- pad_input_data(reinterpret_cast<uint8_t*>(input_data),+ pad_input_data(reinterpret_cast<const uint8_t*>(input_data),padded_data_ptr,sizeof(T),input_data_shape.to_shape(),diff --git a/src/core/reference/include/openvino/reference/mvn.hpp b/src/core/reference/include/openvino/reference/mvn.hppindex c99c5eb802c..fd539b5428a 100644--- a/src/core/reference/include/openvino/reference/mvn.hpp+++ b/src/core/reference/include/openvino/reference/mvn.hpp@@ -89,7 +89,7 @@ void mvn_6(const T* arg,template <typename T>AxisSet mvn_6_reduction_axes(const ov::Tensor& axes_input, size_t rank) {- T* a = axes_input.data<T>();+ const T* a = axes_input.data<T>();auto v = std::vector<T>(a, a + axes_input.get_shape()[0]);std::vector<size_t> axes(v.size(), 0);for (size_t i = 0; i < v.size(); i++) {diff --git a/src/core/reference/src/op/loop.cpp b/src/core/reference/src/op/loop.cppindex 217c2e5658f..718e392ad9f 100644--- a/src/core/reference/src/op/loop.cpp+++ b/src/core/reference/src/op/loop.cpp@@ -113,7 +113,7 @@ void loop(const std::shared_ptr<Model>& func,pointers_to_data[slice_desc->m_stride > 0 ? j : (pointers_to_data.size() - j - 1)] =static_cast<char*>(sliced_values[slice_in_idx][j].data());}- reference::split(static_cast<char*>(args[slice_desc->m_input_index].data()),+ reference::split(static_cast<const char*>(args[slice_desc->m_input_index].data()),args[slice_desc->m_input_index].get_shape(),el_size,slice_desc->m_axis,@@ -211,7 +211,7 @@ void loop(const std::shared_ptr<Model>& func,std::vector<const char*> pointers_on_values;pointers_on_values.reserve(values_to_concat[i].size());for (const auto& vec : values_to_concat[i]) {- pointers_on_values.push_back(static_cast<char*>(vec.data()));+ pointers_on_values.push_back(static_cast<const char*>(vec.data()));}reference::concat(pointers_on_values,static_cast<char*>(out[concat_desc->m_output_index].data()),diff --git a/src/core/reference/src/op/tensor_iterator.cpp b/src/core/reference/src/op/tensor_iterator.cppindex 53640226ef0..b90f6ca985e 100644--- a/src/core/reference/src/op/tensor_iterator.cpp+++ b/src/core/reference/src/op/tensor_iterator.cpp@@ -64,7 +64,7 @@ void tensor_iterator(uint64_t num_iterations,pointers_to_data[slice_desc->m_stride > 0 ? j : (pointers_to_data.size() - j - 1)] =static_cast<char*>(sliced_values[slice_in_idx][j].data());}- reference::split(static_cast<char*>(args[slice_desc->m_input_index].data()),+ reference::split(static_cast<const char*>(args[slice_desc->m_input_index].data()),args[slice_desc->m_input_index].get_shape(),el_size,slice_desc->m_axis,diff --git a/src/core/src/bound_evaluate.cpp b/src/core/src/bound_evaluate.cppindex af2945c9f12..fddc596287e 100644--- a/src/core/src/bound_evaluate.cpp+++ b/src/core/src/bound_evaluate.cpp@@ -453,7 +453,7 @@ bool ov::interval_bound_evaluator(const Node* node,node->evaluate(lower_output_values, *input_variants.begin());auto zero = op::v0::Constant::create(element::i64, {1}, {0});- const auto zero_t = ov::Tensor(element::i64, Shape{});+ auto zero_t = ov::Tensor(element::i64, Shape{});*zero_t.data<int64_t>() = 0;std::vector<TensorVector> unsqueezed_output_variants;@@ -529,8 +529,8 @@ bool ov::interval_bound_evaluator(const Node* node,fully_defined = false;} else {// Can not set to make_tensor_of_min_value(lower_output_values[i]->get_element_type()) yet- const auto then = Tensor{lower_out[0].get_element_type(), Shape{}};- const auto then_data = static_cast<char*>(then.data());+ auto then = Tensor{lower_out[0].get_element_type(), Shape{}};+ auto then_data = static_cast<char*>(then.data());std::memset(then_data, 0, then.get_byte_size());op::v1::Select().evaluate(lower_out, {final_input_dyn_mask, then, lower_out[0]});node->get_output_tensor(i).set_lower_value(lower_out[0]);diff --git a/src/core/src/op/constant.cpp b/src/core/src/op/constant.cppindex ec6aef50215..5577481a8a7 100644--- a/src/core/src/op/constant.cpp+++ b/src/core/src/op/constant.cpp@@ -207,8 +207,10 @@ Constant::Constant(const Tensor& tensor): m_element_type{tensor.get_element_type()},m_shape{tensor.get_shape()},m_byte_strides{m_element_type.bitwidth() >= 8 ? tensor.get_strides() : Strides{}},- m_data{- std::make_shared<SharedBuffer<Tensor>>(static_cast<char*>(tensor.data()), tensor.get_byte_size(), tensor)} {+ // cast is for internal use only to store tensor data in shared buffer (not for modification)+ m_data{std::make_shared<SharedBuffer<Tensor>>(const_cast<char*>(static_cast<const char*>(tensor.data())),+ tensor.get_byte_size(),+ tensor)} {constructor_validate_and_infer_types();}diff --git a/src/core/src/op/convert.cpp b/src/core/src/op/convert.cppindex c3acf6caeaf..c10e3438f1a 100644--- a/src/core/src/op/convert.cpp+++ b/src/core/src/op/convert.cpp@@ -57,7 +57,7 @@ struct Evaluate : public element::NoAction<bool> {CONVERT_ET_LIST,EvalByOutputType,out.get_element_type(),- iterator<ET_IN>(reinterpret_cast<const TI*>(arg.data())),+ iterator<ET_IN>(arg.data()),out,count);}diff --git a/src/core/src/op/depth_to_space.cpp b/src/core/src/op/depth_to_space.cppindex d6f0316dbd7..a7e4d0da2b8 100644--- a/src/core/src/op/depth_to_space.cpp+++ b/src/core/src/op/depth_to_space.cpp@@ -51,7 +51,7 @@ bool DepthToSpace::evaluate(TensorVector& outputs, const TensorVector& inputs) cOPENVINO_ASSERT(outputs.size() == 1);const auto& in = inputs[0];- const auto& out = outputs[0];+ auto& out = outputs[0];reference::depth_to_space(static_cast<const char*>(in.data()),in.get_shape(),static_cast<char*>(out.data()),diff --git a/src/core/src/op/divide.cpp b/src/core/src/op/divide.cppindex bfa35150421..1cc9a66c771 100644--- a/src/core/src/op/divide.cpp+++ b/src/core/src/op/divide.cpp@@ -94,7 +94,7 @@ bool evaluate_bound(const Node* node, TensorVector& output_values, bool is_upperreturn false;const auto zeros_const = Constant::create(input2.get_element_type(), {}, {0});- const auto zero_t = Tensor(input2.get_element_type(), Shape{});+ auto zero_t = Tensor(input2.get_element_type(), Shape{});memcpy(zero_t.data(), zeros_const->get_data_ptr(), zero_t.get_byte_size());const auto max_value = ov::util::make_tensor_of_max_value(input2.get_element_type());@@ -172,7 +172,7 @@ bool evaluate_bound(const Node* node, TensorVector& output_values, bool is_upper// replace zeros by 1 values to get result of divide for other values of argumentsconst auto ones = Constant::create(input2.get_element_type(), input2.get_shape(), {1});- const auto ones_t = Tensor(ones->get_element_type(), ones->get_shape());+ auto ones_t = Tensor(ones->get_element_type(), ones->get_shape());memcpy(ones_t.data(), ones->get_data_ptr(), ones_t.get_byte_size());status = Select().evaluate(value2_outs, {input2_zeros_mask, ones_t, value2});diff --git a/src/core/src/op/roi_align.cpp b/src/core/src/op/roi_align.cppindex c62f08478c2..28a0cfb3b15 100644--- a/src/core/src/op/roi_align.cpp+++ b/src/core/src/op/roi_align.cpp@@ -162,7 +162,7 @@ template <element::Type_t ET>bool evaluate(const Tensor& feature_maps,const Tensor& rois,const std::vector<int64_t>& batch_indices_vec_scaled_up,- const Tensor& out,+ Tensor& out,const int pooled_height,const int pooled_width,const int sampling_ratio,@@ -189,7 +189,7 @@ bool evaluate(const Tensor& feature_maps,}bool evaluate(const TensorVector& args,- const Tensor& out,+ Tensor& out,const int pooled_height,const int pooled_width,const int sampling_ratio,diff --git a/src/core/src/op/space_to_batch.cpp b/src/core/src/op/space_to_batch.cppindex 17d20d38f40..842a9a84de3 100644--- a/src/core/src/op/space_to_batch.cpp+++ b/src/core/src/op/space_to_batch.cpp@@ -74,7 +74,7 @@ namespace space_to_batch {namespace {bool evaluate(TensorVector& outputs, const TensorVector& inputs) {const auto& data = inputs[0];- const auto& out = outputs[0];+ auto& out = outputs[0];const auto elem_size = data.get_element_type().size();auto data_shape = data.get_shape();diff --git a/src/core/src/op/space_to_depth.cpp b/src/core/src/op/space_to_depth.cppindex 33ff1063918..39d192eb7f9 100644--- a/src/core/src/op/space_to_depth.cpp+++ b/src/core/src/op/space_to_depth.cpp@@ -55,7 +55,7 @@ bool SpaceToDepth::evaluate(TensorVector& outputs, const TensorVector& inputs) cOPENVINO_ASSERT(outputs.size() == 1);const auto& in = inputs[0];- const auto& out = outputs[0];+ auto& out = outputs[0];reference::space_to_depth(static_cast<const char*>(in.data()),in.get_shape(),static_cast<char*>(out.data()),diff --git a/src/core/src/op/split.cpp b/src/core/src/op/split.cppindex ca54db7ee7c..f4495646753 100644--- a/src/core/src/op/split.cpp+++ b/src/core/src/op/split.cpp@@ -91,7 +91,7 @@ bool Split::evaluate(TensorVector& outputs, const TensorVector& inputs) const {auto axis = get_tensor_data_as<int64_t>(axis_tensor).front();axis = ov::util::normalize(axis, data_tensor.get_shape().size());- ov::reference::split(static_cast<char*>(data_tensor.data()),+ ov::reference::split(static_cast<const char*>(data_tensor.data()),data_tensor.get_shape(),data_tensor.get_element_type().size(),axis,diff --git a/src/core/src/op/transpose.cpp b/src/core/src/op/transpose.cppindex 0de48f87442..78f9a10e749 100644--- a/src/core/src/op/transpose.cpp+++ b/src/core/src/op/transpose.cpp@@ -96,7 +96,8 @@ bool Transpose::evaluate(TensorVector& outputs, const TensorVector& inputs) cons};auto out_ptr = int4_iterator(static_cast<uint8_t*>(out.data()));- auto in_ptr = int4_iterator(static_cast<uint8_t*>(arg.data()));+ // The int4_iterator not supports const pointer but these data are not modified+ auto in_ptr = int4_iterator(static_cast<uint8_t*>(const_cast<void*>(arg.data())));if ((arg_type == ov::element::i4 || arg_type == ov::element::u4) && arg.get_shape().size() == 2) {for (size_t i = 0; i < out_shape[0]; i++) {size_t off = i;diff --git a/src/core/src/op/util/pad_base.cpp b/src/core/src/op/util/pad_base.cppindex 814385c0466..f6c630dcd5f 100644--- a/src/core/src/op/util/pad_base.cpp+++ b/src/core/src/op/util/pad_base.cpp@@ -106,7 +106,7 @@ bool op::util::PadBase::evaluate_pad(TensorVector& outputs, const TensorVector&const char* pad_value = nullptr;const std::vector<char> pad_zero_value(elem_size, 0);if (get_input_size() == 4) {- pad_value = static_cast<char*>(inputs[3].data());+ pad_value = static_cast<const char*>(inputs[3].data());} else {pad_value = pad_zero_value.data();}@@ -127,7 +127,7 @@ bool op::util::PadBase::evaluate_pad(TensorVector& outputs, const TensorVector&}outputs[0].set_shape(padded_shape);- ov::reference::pad(static_cast<char*>(inputs[0].data()),+ ov::reference::pad(static_cast<const char*>(inputs[0].data()),pad_value,static_cast<char*>(outputs[0].data()),elem_size,diff --git a/src/core/src/runtime/itensor.cpp b/src/core/src/runtime/itensor.cppindex 67dde4e38aa..a2049faf470 100644--- a/src/core/src/runtime/itensor.cpp+++ b/src/core/src/runtime/itensor.cpp@@ -189,4 +189,8 @@ void ITensor::copy_to(const std::shared_ptr<ov::ITensor>& dst) const {}}+void* ITensor::data(const element::Type& type) {+ return const_cast<void*>(static_cast<const ITensor*>(this)->data(type));+}+} // namespace ovdiff --git a/src/core/src/runtime/tensor.cpp b/src/core/src/runtime/tensor.cppindex 5aad89d4ffb..d826c690846 100644--- a/src/core/src/runtime/tensor.cpp+++ b/src/core/src/runtime/tensor.cpp@@ -53,6 +53,9 @@ Tensor::Tensor(const element::Type& element_type, const Shape& shape, const AlloTensor::Tensor(const element::Type& element_type, const Shape& shape, void* host_ptr, const Strides& byte_strides): _impl{make_tensor(element_type, shape, host_ptr, byte_strides)} {}+Tensor::Tensor(const element::Type& element_type, const Shape& shape, const void* host_ptr, const Strides& byte_strides)+ : _impl{make_tensor(element_type, shape, host_ptr, byte_strides)} {}+Tensor::Tensor(const Tensor& owner, const Coordinate& begin, const Coordinate& end): _impl{make_tensor(owner._impl, begin, end)},_so{owner._so} {}@@ -68,6 +71,12 @@ Tensor::Tensor(const ov::Output<const ov::Node>& port, void* host_ptr, const Strhost_ptr,byte_strides) {}+Tensor::Tensor(const ov::Output<const ov::Node>& port, const void* host_ptr, const Strides& byte_strides)+ : Tensor(port.get_element_type(),+ port.get_partial_shape().is_dynamic() ? ov::Shape{0} : port.get_shape(),+ host_ptr,+ byte_strides) {}+const element::Type& Tensor::get_element_type() const {OV_TENSOR_STATEMENT(return _impl->get_element_type());}@@ -96,10 +105,14 @@ size_t Tensor::get_byte_size() const {OV_TENSOR_STATEMENT(return _impl->get_byte_size(););}-void* Tensor::data(const element::Type& element_type) const {+void* Tensor::data(const element::Type& element_type) {OV_TENSOR_STATEMENT(return _impl->data(element_type));}+void* Tensor::data(const element::Type& element_type) const {+ OV_TENSOR_STATEMENT(return const_cast<void*>(std::as_const(*_impl).data(element_type)););+}+bool Tensor::operator!() const noexcept {return !_impl;}diff --git a/src/core/tests/ov_tensor_test.cpp b/src/core/tests/ov_tensor_test.cppindex df20e559045..2a5c747a983 100644--- a/src/core/tests/ov_tensor_test.cpp+++ b/src/core/tests/ov_tensor_test.cpp@@ -8,22 +8,24 @@#include <gtest/gtest.h>#include <cstdint>-#include <openvino/core/shape.hpp>-#include <openvino/core/strides.hpp>-#include <openvino/core/type/element_type.hpp>#include "common_test_utils/test_assertions.hpp"#include "openvino/core/except.hpp"#include "openvino/core/partial_shape.hpp"+#include "openvino/core/shape.hpp"+#include "openvino/core/strides.hpp"+#include "openvino/core/type/element_type.hpp"#include "openvino/core/type/element_type_traits.hpp"+#include "openvino/op/constant.hpp"#include "openvino/op/parameter.hpp"#include "openvino/reference/utils/coordinate_transform.hpp"#include "openvino/runtime/allocator.hpp"#include "openvino/runtime/remote_tensor.hpp"#include "openvino/runtime/tensor.hpp"+namespace ov::test {using OVTensorTest = ::testing::Test;-using testing::_;+using testing::_, testing::HasSubstr;const size_t string_size = ov::element::string.size();@@ -60,7 +62,10 @@ TEST_F(OVTensorTest, canCreateStringTensor) {ASSERT_NE(shape, t.get_strides());ASSERT_EQ(byteStrides(ov::Strides({6, 2, 1}), t.get_element_type()), t.get_strides());ASSERT_EQ(string_size * totalSize, t.get_byte_size());- ASSERT_THROW(t.data(ov::element::i64), ov::Exception);+ using namespace ov::element;+ for (auto&& type : {boolean, i8, u8, i16, u16, i32, u32, i64, u64, f16, bf16, f32, f64}) {+ ASSERT_THROW(t.data(type), ov::Exception);+ }ASSERT_THROW(t.data<std::int32_t>(), ov::Exception);}@@ -84,6 +89,26 @@ TEST_F(OVTensorTest, createTensorFromPort) {EXPECT_EQ(t4.get_element_type(), parameter3->get_element_type());}+TEST_F(OVTensorTest, createTensorFromConstantPort) {+ auto constant1 = std::make_shared<ov::op::v0::Constant>(ov::element::f64, ov::Shape{1, 3, 2, 2}, 0);+ auto constant2 = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1, 3}, 1.0f);+ ov::Tensor t1{constant1->output(0)};+ ov::Tensor t2{constant2->output(0), constant1->get_data_ptr()};+ const auto& t2c = t2;+ const ov::Tensor t3{constant2->output(0), constant2->get_data_ptr()};++ EXPECT_EQ(t1.get_shape(), constant1->get_shape());+ EXPECT_EQ(t1.get_element_type(), constant1->get_element_type());+ EXPECT_EQ(t2.get_shape(), constant2->get_shape());+ EXPECT_EQ(t2.get_element_type(), constant2->get_element_type());+ OV_EXPECT_THROW(t2.data(), ov::Exception, _);+ OV_ASSERT_NO_THROW(t2c.data());++ EXPECT_EQ(t3.get_shape(), constant2->get_shape());+ EXPECT_EQ(t3.get_element_type(), constant2->get_element_type());+ OV_ASSERT_NO_THROW(t3.data());+}+TEST_F(OVTensorTest, createStringTensorFromPort) {auto parameter1 = std::make_shared<ov::op::v0::Parameter>(ov::element::string, ov::Shape{1, 3, 2, 2});auto parameter2 = std::make_shared<ov::op::v0::Parameter>(ov::element::string, ov::Shape{1, 3});@@ -290,12 +315,12 @@ TEST_F(OVTensorTest, canAccessExternalDataWithStridesStringTensor) {TEST_F(OVTensorTest, cannotCreateTensorWithExternalNullptr) {ov::Shape shape = {2, 3};- ASSERT_THROW(ov::Tensor(ov::element::f32, shape, nullptr), ov::Exception);+ ASSERT_THROW(ov::Tensor(ov::element::f32, shape, static_cast<void*>(nullptr)), ov::Exception);}TEST_F(OVTensorTest, cannotCreateStringTensorWithExternalNullptr) {ov::Shape shape = {2, 3};- ASSERT_THROW(ov::Tensor(ov::element::string, shape, nullptr), ov::Exception);+ ASSERT_THROW(ov::Tensor(ov::element::string, shape, static_cast<void*>(nullptr)), ov::Exception);}TEST_F(OVTensorTest, cannotCreateTensorWithWrongStrides) {@@ -555,6 +580,15 @@ TEST_F(OVTensorTest, canChangeShapeOnStridedTensorStringTensor) {OV_ASSERT_NO_THROW(t.set_shape(correct_shape));}+TEST_F(OVTensorTest, createReadOnlyStridedView) {+ const std::vector<std::string> data(64 * 4);+ ov::Tensor strided_view{element::string, {4, 2, 2}, data.data(), {8 * string_size, 3 * string_size, string_size}};++ OV_ASSERT_NO_THROW(static_cast<const Tensor&>(strided_view).data(element::string));+ OV_ASSERT_NO_THROW(strided_view.data<const std::string>());+ OV_EXPECT_THROW(strided_view.data<std::string>(), ov::Exception, HasSubstr("Can not access non-const pointer"));+}+TEST_F(OVTensorTest, makeRangeRoiTensor) {ov::Tensor t{ov::element::i32, {1, 3, 6, 5}}; // RGBp picture of size (WxH) = 5x6ov::Tensor roi_tensor{t, {0, 0, 1, 2}, {1, 3, 5, 4}};@@ -831,7 +865,7 @@ TEST_F(OVTensorTest, checkIsContinuousTensor3Dimensions) {}TEST_F(OVTensorTest, checkIsContinuousTensor4Dimensions) {- ov::Tensor tensor(ov::element::f32, ov::Shape{3, 5, 32, 128});+ const ov::Tensor tensor(ov::element::f32, ov::Shape{3, 5, 32, 128});auto data = tensor.data();auto strides = tensor.get_strides();@@ -865,6 +899,19 @@ TEST_F(OVTensorTest, checkIsContinuousTensor4Dimensions) {EXPECT_EQ(view_tensor.is_continuous(), true);}+TEST_F(OVTensorTest, createReadOnlyView) {+ const std::vector<int32_t> data(10, 1);+ ov::Tensor data_view(ov::element::i32, ov::Shape{10}, data.data());++ OV_ASSERT_NO_THROW(static_cast<const Tensor&>(data_view).data(element::i32));+ OV_ASSERT_NO_THROW(data_view.data<const int32_t>());+ OV_EXPECT_THROW(data_view.data<int64_t>(), ov::Exception, HasSubstr("Can not access non-const pointer"));+}++TEST_F(OVTensorTest, createReadOnlyViewFromNullptr) {+ OV_EXPECT_THROW(Tensor(ov::element::i32, ov::Shape{10}, static_cast<const void*>(nullptr)), ov::Exception, _);+}+struct TestParams {ov::Shape src_shape;ov::Strides src_strides;@@ -903,8 +950,8 @@ void compare_data(const ov::Tensor& src, const ov::Tensor& dst) {template <ov::element::Type_t ET,typename T = typename ov::element_type_traits<ET>::value_type,typename std::enable_if<ET != ov::element::Type_t::string, bool>::type = true>-void init_tensor(const ov::Tensor& tensor, bool input) {- const auto origPtr = tensor.data<T>();+void init_tensor(ov::Tensor& tensor, bool input) {+ auto origPtr = tensor.data<T>();ASSERT_NE(nullptr, origPtr);for (size_t i = 0; i < tensor.get_size(); ++i) {origPtr[i] = static_cast<T>(input ? i : -1);@@ -914,7 +961,7 @@ void init_tensor(const ov::Tensor& tensor, bool input) {template <ov::element::Type_t ET,typename T = typename ov::element_type_traits<ET>::value_type,typename std::enable_if<ET == ov::element::Type_t::string, bool>::type = true>-void init_tensor(const ov::Tensor& tensor, bool input) {+void init_tensor(ov::Tensor& tensor, bool input) {const auto origPtr = tensor.data<T>();ASSERT_NE(nullptr, origPtr);for (size_t i = 0; i < tensor.get_size(); ++i) {@@ -922,7 +969,7 @@ void init_tensor(const ov::Tensor& tensor, bool input) {}}-void init_tensor(const ov::Tensor& tensor, bool input) {+void init_tensor(ov::Tensor& tensor, bool input) {switch (tensor.get_element_type()) {case ov::element::bf16:init_tensor<ov::element::bf16>(tensor, input);@@ -1141,3 +1188,4 @@ INSTANTIATE_TEST_SUITE_P(copy_tests_strings,})));// clang-format on+} // namespace ov::testdiff --git a/src/core/tests/utils/eval_utils.hpp b/src/core/tests/utils/eval_utils.hppindex abeb5fc751c..4ceee915c0f 100644--- a/src/core/tests/utils/eval_utils.hpp+++ b/src/core/tests/utils/eval_utils.hpp@@ -11,7 +11,7 @@namespace {template <typename T>-void copy_data(const ov::Tensor& tv, const std::vector<T>& data) {+void copy_data(ov::Tensor& tv, const std::vector<T>& data) {size_t data_size = data.size() * sizeof(T);if (data_size > 0) {OPENVINO_ASSERT(tv.get_byte_size() >= data_size);@@ -21,13 +21,13 @@ void copy_data(const ov::Tensor& tv, const std::vector<T>& data) {}template <>-inline void copy_data<bool>(const ov::Tensor& tv, const std::vector<bool>& data) {+inline void copy_data<bool>(ov::Tensor& tv, const std::vector<bool>& data) {std::vector<char> data_char(data.begin(), data.end());copy_data(tv, data_char);}template <typename T>-void init_int_tv(const ov::Tensor& tv, std::default_random_engine& engine, T min, T max) {+void init_int_tv(ov::Tensor& tv, std::default_random_engine& engine, T min, T max) {size_t size = tv.get_size();std::uniform_int_distribution<T> dist(min, max);std::vector<T> vec(size);@@ -40,7 +40,7 @@ void init_int_tv(const ov::Tensor& tv, std::default_random_engine& engine, T min}template <>-inline void init_int_tv<char>(const ov::Tensor& tv, std::default_random_engine& engine, char min, char max) {+inline void init_int_tv<char>(ov::Tensor& tv, std::default_random_engine& engine, char min, char max) {size_t size = tv.get_size();std::uniform_int_distribution<int16_t> dist(static_cast<short>(min), static_cast<short>(max));std::vector<char> vec(size);@@ -53,7 +53,7 @@ inline void init_int_tv<char>(const ov::Tensor& tv, std::default_random_engine&}template <>-inline void init_int_tv<int8_t>(const ov::Tensor& tv, std::default_random_engine& engine, int8_t min, int8_t max) {+inline void init_int_tv<int8_t>(ov::Tensor& tv, std::default_random_engine& engine, int8_t min, int8_t max) {size_t size = tv.get_size();std::uniform_int_distribution<int16_t> dist(static_cast<short>(min), static_cast<short>(max));std::vector<int8_t> vec(size);@@ -66,7 +66,7 @@ inline void init_int_tv<int8_t>(const ov::Tensor& tv, std::default_random_engine}template <>-inline void init_int_tv<uint8_t>(const ov::Tensor& tv, std::default_random_engine& engine, uint8_t min, uint8_t max) {+inline void init_int_tv<uint8_t>(ov::Tensor& tv, std::default_random_engine& engine, uint8_t min, uint8_t max) {size_t size = tv.get_size();std::uniform_int_distribution<int16_t> dist(static_cast<short>(min), static_cast<short>(max));std::vector<uint8_t> vec(size);@@ -79,7 +79,7 @@ inline void init_int_tv<uint8_t>(const ov::Tensor& tv, std::default_random_engin}template <typename T>-void init_real_tv(const ov::Tensor& tv, std::default_random_engine& engine, T min, T max) {+void init_real_tv(ov::Tensor& tv, std::default_random_engine& engine, T min, T max) {size_t size = tv.get_size();std::uniform_real_distribution<T> dist(min, max);std::vector<T> vec(size);@@ -91,7 +91,7 @@ void init_real_tv(const ov::Tensor& tv, std::default_random_engine& engine, T mimemcpy(tv.data(), vec.data(), data_size);}-inline void random_init(const ov::Tensor& tv, std::default_random_engine& engine) {+inline void random_init(ov::Tensor& tv, std::default_random_engine& engine) {ov::element::Type et = tv.get_element_type();if (et == ov::element::boolean) {init_int_tv<char>(tv, engine, 0, 1);diff --git a/src/inference/dev_api/openvino/runtime/iremote_tensor.hpp b/src/inference/dev_api/openvino/runtime/iremote_tensor.hppindex 82be5a7496d..f1c8112bd07 100644--- a/src/inference/dev_api/openvino/runtime/iremote_tensor.hpp+++ b/src/inference/dev_api/openvino/runtime/iremote_tensor.hpp@@ -17,7 +17,7 @@ namespace ov {class OPENVINO_RUNTIME_API IRemoteTensor : public ITensor {public:- void* data(const element::Type& type = {}) const override final {+ const void* data(const element::Type& type = {}) const override final {OPENVINO_NOT_IMPLEMENTED;}diff --git a/src/inference/dev_api/openvino/runtime/make_tensor.hpp b/src/inference/dev_api/openvino/runtime/make_tensor.hppindex 6cfe74ebaad..10d5de93f43 100644--- a/src/inference/dev_api/openvino/runtime/make_tensor.hpp+++ b/src/inference/dev_api/openvino/runtime/make_tensor.hpp@@ -39,6 +39,20 @@ OPENVINO_RUNTIME_API std::shared_ptr<ITensor> make_tensor(const element::Type tyvoid* host_ptr,const Strides& strides = {});+/**+ * @brief Constructs Tensor using element type and shape. Wraps allocated host memory as read-only.+ * @note Does not perform memory allocation internally+ * @param type Tensor element type+ * @param shape Tensor shape+ * @param host_ptr Pointer to pre-allocated host memory+ * @param strides Optional strides parameters in bytes. Strides are supposed to be computed automatically based+ * on shape and element size+ */+OPENVINO_RUNTIME_API std::shared_ptr<ITensor> make_tensor(const element::Type type,+ const Shape& shape,+ const void* host_ptr,+ const Strides& strides = {});+/*** @brief Constructs region of interest (ROI) tensor form another tensor.* @note Does not perform memory allocation internallydiff --git a/src/inference/src/dev/compilation_context.cpp b/src/inference/src/dev/compilation_context.cppindex bf1a7197826..49e8ae2b1eb 100644--- a/src/inference/src/dev/compilation_context.cpp+++ b/src/inference/src/dev/compilation_context.cpp@@ -109,7 +109,7 @@ std::string ModelCache::compute_hash(const std::string& modelStr,if (tensor) {seed = hash_combine(seed, tensor.get_size());- auto ptr = static_cast<size_t*>(tensor.data());+ auto ptr = static_cast<const size_t*>(tensor.data());size_t size = tensor.get_size() / sizeof(size_t);// 10MB block size in size_t@@ -141,7 +141,7 @@ std::string ModelCache::compute_hash(const std::string& modelStr,}auto size_done = size * sizeof(size_t);- auto ptr_left = static_cast<uint8_t*>(tensor.data()) + size_done;+ auto ptr_left = static_cast<const uint8_t*>(tensor.data()) + size_done;size_t size_left = tensor.get_size() - size_done;for (size_t i = 0; i < size_left; i++)seed = hash_combine(seed, ptr_left[i]);diff --git a/src/inference/src/dev/make_tensor.cpp b/src/inference/src/dev/make_tensor.cppindex 88f0989007c..4110f3c1510 100644--- a/src/inference/src/dev/make_tensor.cpp+++ b/src/inference/src/dev/make_tensor.cpp@@ -57,11 +57,8 @@ public:OPENVINO_ASSERT(m_element_type.is_static());}- void* data(const element::Type& element_type) const override {- if (element_type.is_static() && (element_type.bitwidth() != get_element_type().bitwidth() ||- element_type.is_real() != get_element_type().is_real() ||- (element_type == element::string && get_element_type() != element::string) ||- (element_type != element::string && get_element_type() == element::string))) {+ const void* data(const element::Type& element_type) const override {+ if (!is_pointer_representable(element_type)) {OPENVINO_THROW("Tensor data with element type ",get_element_type(),", is not representable as pointer to ",@@ -94,6 +91,14 @@ public:}protected:+ bool is_pointer_representable(const element::Type& element_type) const {+ return element_type.is_dynamic() ||+ ((get_element_type() != element::string && element_type != element::string &&+ element_type.bitwidth() == get_element_type().bitwidth() &&+ element_type.is_real() == get_element_type().is_real()) ||+ (element_type == element::string && element::string == get_element_type()));+ }+void update_strides() const {if (m_element_type.bitwidth() < 8)return;@@ -118,6 +123,22 @@ protected:void* m_ptr;};+/**+ * @brief Read-only view tensor to external memory+ * The tensor doesn't own the external memory+ */+class ReadOnlyViewTensor : public ViewTensor {+public:+ ReadOnlyViewTensor(const element::Type element_type, const Shape& shape, const void* ptr)+ : ViewTensor{element_type, shape, const_cast<void*>(ptr)} {}++ using ViewTensor::data;++ [[noreturn]] void* data(const element::Type& element_type) override {+ OPENVINO_THROW("Can not access non-const pointer use e.g. 'static_cast<const ov::Tensor&>.data()'");+ }+};+/*** @brief View tensor on external memory with strides*/@@ -173,6 +194,21 @@ public:}};+class ReadOnlyStridedViewTensor : public StridedViewTensor {+public:+ ReadOnlyStridedViewTensor(const element::Type element_type,+ const Shape& shape,+ const void* ptr,+ const Strides& strides)+ : StridedViewTensor{element_type, shape, const_cast<void*>(ptr), strides} {}++ using StridedViewTensor::data;++ [[noreturn]] void* data(const element::Type& element_type) override {+ OPENVINO_THROW("Can not access non-const pointer use e.g. 'static_cast<const ov::Tensor&>.data()'");+ }+};+/*** @brief Creates view tensor on external memory*@@ -191,6 +227,27 @@ std::shared_ptr<ITensor> make_tensor(const element::Type element_type,: std::make_shared<StridedViewTensor>(element_type, shape, ptr, byte_strides);}+/**+ * @brief Creates read-only view tensor on external memory+ *+ * @param element_type Tensor element type+ * @param shape Tensor shape+ * @param ptr pointer to external memory+ * @param byte_strides Tensor strides+ *+ * @return Shared pointer to tensor interface+ */+std::shared_ptr<ITensor> make_tensor(const element::Type element_type,+ const Shape& shape,+ const void* ptr,+ const Strides& byte_strides) {+ if (byte_strides.empty()) {+ return std::make_shared<ReadOnlyViewTensor>(element_type, shape, ptr);+ } else {+ return std::make_shared<ReadOnlyStridedViewTensor>(element_type, shape, ptr, byte_strides);+ }+}+/*** @brief Tensor with allocated memory* Tensor owns the memory@@ -348,7 +405,7 @@ public:BaseRoiTensor::set_shape(new_shape);}- void* data(const element::Type& element_type) const override {+ const void* data(const element::Type& element_type) const override {auto owner_data = m_owner->data(element_type);return static_cast<uint8_t*>(owner_data) + m_offset;}diff --git a/src/inference/src/model_reader.cpp b/src/inference/src/model_reader.cppindex cc544e9e2f0..de8e865d23d 100644--- a/src/inference/src/model_reader.cpp+++ b/src/inference/src/model_reader.cpp@@ -175,7 +175,7 @@ std::shared_ptr<ov::Model> read_model(const std::string& model,ov::AnyVector params{&modelStream};if (weights) {std::shared_ptr<ov::AlignedBuffer> weights_buffer =- std::make_shared<ov::SharedBuffer<ov::Tensor>>(reinterpret_cast<char*>(weights.data()),+ std::make_shared<ov::SharedBuffer<ov::Tensor>>(reinterpret_cast<char*>(const_cast<void*>(weights.data())),weights.get_byte_size(),weights);params.emplace_back(weights_buffer);diff --git a/src/plugins/intel_cpu/src/cpu_tensor.cpp b/src/plugins/intel_cpu/src/cpu_tensor.cppindex 055acd43f6d..724fa9c7bed 100644--- a/src/plugins/intel_cpu/src/cpu_tensor.cpp+++ b/src/plugins/intel_cpu/src/cpu_tensor.cpp@@ -83,7 +83,7 @@ void Tensor::update_strides() const {});}-void* Tensor::data(const element::Type& element_type) const {+const void* Tensor::data(const element::Type& element_type) const {if (element_type.is_static()) {OPENVINO_ASSERT(element_type == get_element_type(),"Tensor data with element type ",diff --git a/src/plugins/intel_cpu/src/cpu_tensor.h b/src/plugins/intel_cpu/src/cpu_tensor.hindex 77506389a1f..fcb1ffdb723 100644--- a/src/plugins/intel_cpu/src/cpu_tensor.h+++ b/src/plugins/intel_cpu/src/cpu_tensor.h@@ -27,7 +27,7 @@ public:const ov::Strides& get_strides() const override;- void* data(const element::Type& type) const override;+ const void* data(const element::Type& type) const override;MemoryPtr get_memory() {return m_memptr;diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/matmul_weights_decompression.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/matmul_weights_decompression.cppindex ab1a20e063c..176ac41ab1e 100644--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/matmul_weights_decompression.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/matmul_weights_decompression.cpp@@ -342,7 +342,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_MatMulCompressedWeights_non_multiples_groups,::testing::Combine(::testing::ValuesIn(input_shapes_non_multiples_groups),::testing::Values(ov::element::u8),::testing::ValuesIn(decompression_precisions),- ::testing::Values(ov::element::undefined),+ ::testing::Values(ov::element::dynamic),::testing::ValuesIn(transpose_weights),::testing::Values(DecompressionType::full),::testing::Values(DecompressionType::full),diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hppindex f17c20cd992..136f450d433 100644--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp@@ -142,7 +142,7 @@ void convert_and_copy(cldnn::memory::ptr dst,cldnn::stream& stream,const cldnn::layout& src_layout = cldnn::layout({}, ov::element::dynamic, cldnn::format::bfyx, cldnn::padding()));-void convert_and_copy(const cldnn::memory::ptr src, ov::ITensor const* dst, const cldnn::stream& stream);+void convert_and_copy(const cldnn::memory::ptr src, ov::ITensor* dst, const cldnn::stream& stream);void convert_and_copy(const ov::ITensor* src, ov::ITensor* dst, const cldnn::stream& stream);void convert_and_copy(const cldnn::memory::ptr src, cldnn::memory::ptr dst, cldnn::stream& stream);diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/sync_infer_request.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/sync_infer_request.hppindex 73a92d98e6a..42168bc663c 100644--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/sync_infer_request.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/sync_infer_request.hpp@@ -113,7 +113,7 @@ private:void allocate_states();void allocate_input(const ov::Output<const ov::Node>& port, size_t input_idx);void allocate_output(const ov::Output<const ov::Node>& port, size_t output_idx);- cldnn::event::ptr copy_output_data(cldnn::memory::ptr src, const ov::ITensor& dst) const;+ cldnn::event::ptr copy_output_data(cldnn::memory::ptr src, ov::ITensor& dst) const;void init_mappings();bool is_batched_input(const ov::Output<const ov::Node>& port) const;diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/usm_host_tensor.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/usm_host_tensor.hppindex c19340af329..caaa029bd15 100644--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/usm_host_tensor.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/usm_host_tensor.hpp@@ -20,7 +20,7 @@ public:~USMHostTensor() override = default;- void* data(const element::Type& element_type) const override;+ const void* data(const element::Type& element_type) const override;const element::Type& get_element_type() const override;const Shape& get_shape() const override;diff --git a/src/plugins/intel_gpu/src/plugin/common_utils.cpp b/src/plugins/intel_gpu/src/plugin/common_utils.cppindex 229d1b8eaa3..ac1ccfd1455 100644--- a/src/plugins/intel_gpu/src/plugin/common_utils.cpp+++ b/src/plugins/intel_gpu/src/plugin/common_utils.cpp@@ -164,7 +164,7 @@ void convert_and_copy(const ov::ITensor* src, cldnn::memory::ptr dst, cldnn::strdst->copy_from(stream, tmp_tensor.data(), blocking);}-void convert_and_copy(const cldnn::memory::ptr src, ov::ITensor const* dst, const cldnn::stream& stream) {+void convert_and_copy(const cldnn::memory::ptr src, ov::ITensor* dst, const cldnn::stream& stream) {auto src_et = src->get_layout().data_type;auto dst_et = dst->get_element_type();diff --git a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp b/src/plugins/intel_gpu/src/plugin/remote_tensor.cppindex 97ca22abdc2..8e6a7eea823 100644--- a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp+++ b/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp@@ -243,7 +243,8 @@ void RemoteTensorImpl::copy_from(const std::shared_ptr<const ov::ITensor>& src,OPENVINO_ASSERT(!std::dynamic_pointer_cast<const ov::IRemoteTensor>(src), "[GPU] Unsupported Remote Tensor type");- auto src_mem = MemWrapper(stream, nullptr, src->data());+ // MemWrapper use tensor pointer as read-only, so const_cast is safe here+ auto src_mem = MemWrapper(stream, nullptr, const_cast<void*>(src->data()));auto dst_mem = MemWrapper(stream, get_memory(), nullptr);copy_roi(src_mem, dst_mem, src_offset, dst_offset, src->get_strides(), get_strides(), roi_strides, src->get_shape(), get_shape(), shape);diff --git a/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp b/src/plugins/intel_gpu/src/plugin/sync_infer_request.cppindex 8c712afbd10..5247206dc5b 100644--- a/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp+++ b/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp@@ -563,7 +563,7 @@ TensorWrapper SyncInferRequest::create_or_share_device_tensor(const TensorWrappereturn { create_device_tensor(actual_memory_shape, element_type, need_lockable_mem), TensorOwner::PLUGIN };}-cldnn::event::ptr SyncInferRequest::copy_output_data(cldnn::memory::ptr src, const ov::ITensor& dst) const {+cldnn::event::ptr SyncInferRequest::copy_output_data(cldnn::memory::ptr src, ov::ITensor& dst) const {OV_ITT_SCOPED_TASK(itt::domains::intel_gpu_plugin, "SyncInferRequest::copy_output_data");OPENVINO_ASSERT(src->count() <= dst.get_size(),"[GPU] Unexpected elements count of dst tensor: ",diff --git a/src/plugins/intel_gpu/src/plugin/usm_host_tensor.cpp b/src/plugins/intel_gpu/src/plugin/usm_host_tensor.cppindex 50580537a8d..e3edfb818c1 100644--- a/src/plugins/intel_gpu/src/plugin/usm_host_tensor.cpp+++ b/src/plugins/intel_gpu/src/plugin/usm_host_tensor.cpp@@ -15,7 +15,7 @@ USMHostTensor::USMHostTensor(std::shared_ptr<RemoteContextImpl> context, const eUSMHostTensor::USMHostTensor(std::shared_ptr<RemoteTensorImpl> tensor): m_impl(tensor) {}-void* USMHostTensor::data(const element::Type& element_type) const {+const void* USMHostTensor::data(const element::Type& element_type) const {return m_impl->get_original_memory()->buffer_ptr();}diff --git a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/gpu_remote_tensor_tests.cpp b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/gpu_remote_tensor_tests.cppindex 90f2a310eac..7d3036130c4 100644--- a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/gpu_remote_tensor_tests.cpp+++ b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/gpu_remote_tensor_tests.cpp@@ -2564,9 +2564,8 @@ void compare_data(const ov::Tensor& src, const ov::Tensor& dst) {}}-template <ov::element::Type_t ET,- typename T = typename ov::element_type_traits<ET>::value_type>-void init_tensor(const ov::Tensor& tensor) {+template <ov::element::Type_t ET, typename T = typename ov::element_type_traits<ET>::value_type>+void init_tensor(ov::Tensor& tensor) {const auto origPtr = tensor.data<T>();ASSERT_NE(nullptr, origPtr);for (size_t i = 0; i < tensor.get_size(); ++i) {@@ -2574,7 +2573,7 @@ void init_tensor(const ov::Tensor& tensor) {}}-void init_tensor(const ov::Tensor& tensor) {+void init_tensor(ov::Tensor& tensor) {switch (tensor.get_element_type()) {case ov::element::f32:init_tensor<ov::element::f32>(tensor);diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/matrix_nms.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/matrix_nms.cppindex 646aa9e8d37..ac62b9fa8a2 100644--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/matrix_nms.cpp+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/matrix_nms.cpp@@ -117,8 +117,8 @@ void MatrixNmsLayerTestGPU::compare(const std::vector<ov::Tensor> &expectedOutpufor (int outputIndex = static_cast<int>(expectedOutputs.size()) - 1; outputIndex >= 0 ; outputIndex--) {const auto& expected = expectedOutputs[outputIndex];const auto& actual = actualOutputs[outputIndex];- const auto actualBuffer = static_cast<uint8_t*>(actual.data());- const auto expectedBuffer = static_cast<uint8_t*>(expected.data());+ const auto actualBuffer = static_cast<const uint8_t*>(actual.data());+ const auto expectedBuffer = static_cast<const uint8_t*>(expected.data());//Compare Selected Outputs & Selected Indicesif (outputIndex != batchIndex) {@@ -147,7 +147,7 @@ void MatrixNmsLayerTestGPU::compare(const std::vector<ov::Tensor> &expectedOutpudefault:break;}- const auto fBuffer = static_cast<float*>(actual.data());+ const auto fBuffer = static_cast<const float*>(actual.data());for (size_t tailing = validNums * 6; tailing < maxOutputBoxesPerBatch * 6; tailing++) {ASSERT_TRUE(std::abs(fBuffer[(actual_offset * 6 + tailing)] + 1.f) < 1e-5)<< "Invalid default value: " << fBuffer[i] << " at index: " << i;diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/multiclass_nms.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/multiclass_nms.cppindex 79c7b60cc6f..d7023580663 100644--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/multiclass_nms.cpp+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/single_layer_tests/multiclass_nms.cpp@@ -237,8 +237,8 @@ void MulticlassNmsLayerTestGPU::compare(const std::vector<ov::Tensor> &expectedOfor (int outputIndex = static_cast<int>(expectedOutputs.size()) - 1; outputIndex >= 0; outputIndex--) {const auto& expected = expectedOutputs[outputIndex];const auto& actual = actualOutputs[outputIndex];- const auto actualBuffer = static_cast<uint8_t*>(actual.data());- const auto expectedBuffer = static_cast<uint8_t*>(expected.data());+ const auto actualBuffer = static_cast<const uint8_t*>(actual.data());+ const auto expectedBuffer = static_cast<const uint8_t*>(expected.data());const auto expected_shape = expected.get_shape();const auto actual_shape = actual.get_shape();@@ -270,7 +270,7 @@ void MulticlassNmsLayerTestGPU::compare(const std::vector<ov::Tensor> &expectedObreak;}- const auto fBuffer = static_cast<float*>(actual.data());+ const auto fBuffer = static_cast<const float*>(actual.data());for (size_t tailing = validNums * 6; tailing < maxOutputBoxesPerBatch * 6; tailing++) {ASSERT_TRUE(std::abs(fBuffer[(actual_offset * 6 + tailing)] - -1.f) < 1e-5)<< "Invalid default value: " << fBuffer[i] << " at index: " << i;diff --git a/src/plugins/intel_npu/src/backend/include/zero_host_tensor.hpp b/src/plugins/intel_npu/src/backend/include/zero_host_tensor.hppindex 55ba4e476ec..2e92c0ca251 100644--- a/src/plugins/intel_npu/src/backend/include/zero_host_tensor.hpp+++ b/src/plugins/intel_npu/src/backend/include/zero_host_tensor.hpp@@ -23,7 +23,7 @@ public:~ZeroHostTensor() override = default;- void* data(const ov::element::Type& element_type) const override;+ const void* data(const ov::element::Type& element_type) const override;const ov::element::Type& get_element_type() const override;const ov::Shape& get_shape() const override;diff --git a/src/plugins/intel_npu/src/backend/include/zero_tensor.hpp b/src/plugins/intel_npu/src/backend/include/zero_tensor.hppindex 4a42f003ea4..332aea6fdda 100644--- a/src/plugins/intel_npu/src/backend/include/zero_tensor.hpp+++ b/src/plugins/intel_npu/src/backend/include/zero_tensor.hpp@@ -29,7 +29,7 @@ public:const ov::Shape& shape,const ov::Allocator& allocator);- void* data(const ov::element::Type& type = {}) const override;+ const void* data(const ov::element::Type& type = {}) const override;const ov::element::Type& get_element_type() const override;diff --git a/src/plugins/intel_npu/src/backend/src/zero_host_tensor.cpp b/src/plugins/intel_npu/src/backend/src/zero_host_tensor.cppindex 7f55cfb3e9c..985d8f5fd6a 100644--- a/src/plugins/intel_npu/src/backend/src/zero_host_tensor.cpp+++ b/src/plugins/intel_npu/src/backend/src/zero_host_tensor.cpp@@ -24,7 +24,7 @@ ZeroHostTensor::ZeroHostTensor(const std::shared_ptr<ov::IRemoteContext>& contextensor_type,ov::intel_npu::MemType::L0_INTERNAL_BUF)) {}-void* ZeroHostTensor::data(const ov::element::Type&) const {+const void* ZeroHostTensor::data(const ov::element::Type&) const {return _impl->get_original_memory();}diff --git a/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp b/src/plugins/intel_npu/src/backend/src/zero_tensor.cppindex 4697b2521d0..163e4082cb6 100644--- a/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp+++ b/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp@@ -33,7 +33,7 @@ ZeroTensor::ZeroTensor(const std::shared_ptr<ZeroInitStructsHolder>& init_struct_ptr = data;}-void* ZeroTensor::data(const ov::element::Type& element_type) const {+const void* ZeroTensor::data(const ov::element::Type& element_type) const {if (element_type != ov::element::dynamic &&(element_type.bitwidth() != get_element_type().bitwidth() ||element_type.is_real() != get_element_type().is_real() ||diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.cpp b/src/plugins/intel_npu/src/plugin/npuw/util.cppindex b278a09f5e0..2016f46bf4a 100644--- a/src/plugins/intel_npu/src/plugin/npuw/util.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/util.cpp@@ -438,7 +438,7 @@ ov::Tensor ov::npuw::util::to_f16(const ov::Tensor& t) {}inline uint8_t tread_4b(const ov::Tensor& t, std::size_t r, std::size_t c, std::size_t COLS) {- const uint8_t* tdata = static_cast<uint8_t*>(t.data());+ const uint8_t* tdata = static_cast<const uint8_t*>(t.data());const uint8_t* trow = tdata + r * COLS / 2;const uint8_t* telem = trow + c / 2;if (c % 2 == 0) {@@ -449,7 +449,7 @@ inline uint8_t tread_4b(const ov::Tensor& t, std::size_t r, std::size_t c, std::template <typename T>inline T tread(const ov::Tensor& t, std::size_t r, std::size_t c, std::size_t COLS) {- const T* tdata = static_cast<T*>(t.data());+ const T* tdata = static_cast<const T*>(t.data());const T* trow = tdata + r * COLS;const T* telem = trow + c;return *telem;@@ -507,7 +507,7 @@ void permute120(const ov::Tensor& src, ov::Tensor& dst) {const ov::Shape dst_shape = dst.get_shape();NPUW_ASSERT(src_shape.size() == 3); // Yes, so far only transpose 3D tensors- const T* pSrc = static_cast<T*>(src.data());+ const T* pSrc = static_cast<const T*>(src.data());T* pDst = static_cast<T*>(dst.data());// DSTs [b,r,c] map to SRC's [r,c,b]@@ -637,7 +637,7 @@ ov::Tensor ov::npuw::util::concat(const std::vector<ov::Tensor>& tt, std::size_tconst bool is_4bit = (type == ov::element::i4 || type == ov::element::u4);for (std::size_t t_idx = 0; t_idx < tt.size(); t_idx++) {- const uint8_t* pSrc = static_cast<uint8_t*>(tt[t_idx].data());+ const uint8_t* pSrc = static_cast<const uint8_t*>(tt[t_idx].data());const auto copy_size = lens[t_idx] * shape[1] * shape[2];const auto copy_len = is_4bit ? copy_size / 2 : copy_size * type.size();@@ -661,7 +661,7 @@ ov::Tensor ov::npuw::util::concat(const std::vector<ov::Tensor>& tt, std::size_tuint8_t* pDstRow = pDst + r_offset + c_offset;const auto r_offset_src = is_4bit ? lens[t_idx] * r / 2 : lens[t_idx] * r * type.size();- const uint8_t* pSrc = static_cast<uint8_t*>(t_src.data());+ const uint8_t* pSrc = static_cast<const uint8_t*>(t_src.data());const uint8_t* pSrcRow = pSrc + r_offset_src;std::copy_n(pSrcRow, copy_len, pDstRow);diff --git a/src/plugins/intel_npu/tools/common/include/tensor_utils.hpp b/src/plugins/intel_npu/tools/common/include/tensor_utils.hppindex 18d431c157d..e4adeeaa194 100644--- a/src/plugins/intel_npu/tools/common/include/tensor_utils.hpp+++ b/src/plugins/intel_npu/tools/common/include/tensor_utils.hpp@@ -19,7 +19,7 @@ namespace utils {* @param in The source tensor* @param out The destination tensor*/-void copyTensor(const ov::Tensor& in, const ov::Tensor& out);+void copyTensor(const ov::Tensor& in, ov::Tensor& out);/*** @brief Copies the contents of one tensor into another one which bears the same shape. Precision conversions from@@ -28,7 +28,7 @@ void copyTensor(const ov::Tensor& in, const ov::Tensor& out);* @param in The source tensor* @param out The destination tensor*/-void convertTensorPrecision(const ov::Tensor& in, const ov::Tensor& out);+void convertTensorPrecision(const ov::Tensor& in, ov::Tensor& out);/*** @brief Constructs a tensor with the same content as the source but with the precision converted to the specifieddiff --git a/src/plugins/intel_npu/tools/common/src/tensor_utils.cpp b/src/plugins/intel_npu/tools/common/src/tensor_utils.cppindex c2b49024977..1532891aada 100644--- a/src/plugins/intel_npu/tools/common/src/tensor_utils.cpp+++ b/src/plugins/intel_npu/tools/common/src/tensor_utils.cpp@@ -17,7 +17,7 @@namespace {template <class InT, class OutT>-void convertTensorPrecisionImpl(const ov::Tensor& in, const ov::Tensor& out) {+void convertTensorPrecisionImpl(const ov::Tensor& in, ov::Tensor& out) {const auto inputBuffer = in.data<const InT>();OPENVINO_ASSERT(inputBuffer != nullptr, "Tensor was not allocated");@@ -34,7 +34,7 @@ void convertTensorPrecisionImpl(const ov::Tensor& in, const ov::Tensor& out) {namespace npu {namespace utils {-void copyTensor(const ov::Tensor& in, const ov::Tensor& out) {+void copyTensor(const ov::Tensor& in, ov::Tensor& out) {OPENVINO_ASSERT(in.get_element_type() == out.get_element_type(), "Precision mismatch");OPENVINO_ASSERT(in.get_shape() == out.get_shape(), "Shape mismatch");@@ -47,7 +47,7 @@ void copyTensor(const ov::Tensor& in, const ov::Tensor& out) {std::copy_n(inputBuffer, in.get_byte_size(), outputBuffer);}-void convertTensorPrecision(const ov::Tensor& in, const ov::Tensor& out) {+void convertTensorPrecision(const ov::Tensor& in, ov::Tensor& out) {OPENVINO_ASSERT(in.get_shape() == out.get_shape(), "Mismatch in Dims");const ov::element::Type& inPrecision = in.get_element_type();diff --git a/src/plugins/intel_npu/tools/single-image-test/main.cpp b/src/plugins/intel_npu/tools/single-image-test/main.cppindex 1ced0ef6daa..30458358bb8 100644--- a/src/plugins/intel_npu/tools/single-image-test/main.cpp+++ b/src/plugins/intel_npu/tools/single-image-test/main.cpp@@ -322,8 +322,11 @@ std::vector<size_t> getStrides(const ov::Shape& shape) {return strides;}-std::vector<cv::Mat> ovToCV(const ov::Tensor& tensor, const ov::Shape& shape, const ov::Layout& layout,- size_t batchInd = 0, size_t depthInd = 0) {+std::vector<cv::Mat> ovToCV(ov::Tensor& tensor,+ const ov::Shape& shape,+ const ov::Layout& layout,+ size_t batchInd = 0,+ size_t depthInd = 0) {const ov::element::Type& precision = tensor.get_element_type();OPENVINO_ASSERT(layout == ov::Layout("NCHW") || layout == ov::Layout("NCDHW"),@@ -433,7 +436,11 @@ struct BatchIndexer {return sstream.str();}};-void cvToOV(const cv::Mat& cvImg, const BatchIndexer &cvImgInBatch, const ov::Tensor& tensor, const ov::Shape& shape, const ov::Layout& layout,+void cvToOV(const cv::Mat& cvImg,+ const BatchIndexer& cvImgInBatch,+ ov::Tensor& tensor,+ const ov::Shape& shape,+ const ov::Layout& layout,const std::string& colorFormat) {const ov::element::Type& precision = tensor.get_element_type();@@ -535,13 +542,15 @@ void cvToOV(const cv::Mat& cvImg, const BatchIndexer &cvImgInBatch, const ov::Te<< n << " up to " << N << " with image data from the array: "<< cvImgInBatch.to_string() << std::endl;}- cv::Mat batch(static_cast<int>(H), static_cast<int>(W), cvType,+ cv::Mat batch(static_cast<const int>(H),+ static_cast<int>(W),+ cvType,dataBuffer + n * (out.size().area() * out.elemSize()));out.copyTo(batch);}} else if (layout == ov::Layout("NCHW")) {ov::Tensor auxTensor(precision, shape);- const ov::Tensor &outTensor = (cvImgInBatch.index == 0 ? tensor : auxTensor);+ ov::Tensor& outTensor = (cvImgInBatch.index == 0 ? tensor : auxTensor);// only a first image from an input image array fills an original input tensor up.// Subsequent images (if exist) will fill batch slices of the input tensor// by its number in the input array respectively@@ -746,7 +755,7 @@ std::string cleanName(std::string&& name) {ov::Tensor loadImages(const ov::element::Type& precision, const ov::Shape& shape, const ov::Layout& layout,const std::vector<std::string>& filePaths, const std::string& colorFormat) {- const ov::Tensor tensor(precision, shape);+ ov::Tensor tensor(precision, shape);for (size_t fileIndex = 0; fileIndex != filePaths.size(); fileIndex++) {const auto &filePath = filePaths[fileIndex];const auto frame = cv::imread(filePath, cv::IMREAD_COLOR);@@ -771,7 +780,7 @@ void loadBinary(const std::string& filePath, const BatchIndexer &fileSourceInBatif (dataPrecision != modelPrecision && dataPrecision != ov::element::Type_t::dynamic) {std::cout << "Converting " << filePath << " input from " << dataPrecision << " to " << modelPrecision<< std::endl;- const ov::Tensor inputTensor(dataPrecision, shape);+ ov::Tensor inputTensor(dataPrecision, shape);if (fileBytes == inputTensor.get_byte_size()) {binaryFile.read(reinterpret_cast<char*>(inputTensor.data()), static_cast<std::streamsize>(fileBytes));npu::utils::convertTensorPrecision(inputTensor, requestedTensor);@@ -788,10 +797,10 @@ void loadBinary(const std::string& filePath, const BatchIndexer &fileSourceInBat" expected while converting precision from ", dataPrecision, " to ", modelPrecision);ov::Shape debatchedInputTensorShape(shape);debatchedInputTensorShape[ov::layout::batch_idx(layout)] = 1;- const ov::Tensor inputDebatchedTensor(dataPrecision, debatchedInputTensorShape);+ ov::Tensor inputDebatchedTensor(dataPrecision, debatchedInputTensorShape);binaryFile.read(reinterpret_cast<char*>(inputDebatchedTensor.data()),static_cast<std::streamsize>(fileBytes));- const ov::Tensor convertedPrecisionTensor(modelPrecision, debatchedInputTensorShape);+ ov::Tensor convertedPrecisionTensor(modelPrecision, debatchedInputTensorShape);npu::utils::convertTensorPrecision(inputDebatchedTensor, convertedPrecisionTensor);std::list<ov::Tensor> tensorsToJoin;std::list<ov::Tensor> tensorsFromSplit = npu::utils::splitBatchedTensor(requestedTensor, layout, N);@@ -882,7 +891,7 @@ ov::Tensor loadInput(const ov::element::Type& modelPrecision,}ov::Tensor loadTensor(const ov::element::Type& precision, const ov::Shape& shape, const std::string& filePath) {- const ov::Tensor tensor(precision, shape);+ ov::Tensor tensor(precision, shape);std::ifstream file(filePath, std::ios_base::in | std::ios_base::binary);OPENVINO_ASSERT(file.is_open(), "Can't open file ", filePath, " for read");@@ -897,7 +906,7 @@ void dumpTensor(const ov::Tensor& tensor, const std::string& filePath) {std::ofstream file(filePath, std::ios_base::out | std::ios_base::binary);OPENVINO_ASSERT(file.is_open(), "Can't open file ", filePath, " for write");- const auto dataBuffer = reinterpret_cast<char*>(tensor.data());+ const auto dataBuffer = reinterpret_cast<const char*>(tensor.data());file.write(dataBuffer, static_cast<std::streamsize>(tensor.get_byte_size()));}@@ -1438,7 +1447,7 @@ std::vector<float> softmax(std::vector<float>& tensor) {return results;}-bool testNRMSE(const TensorMap& outputs, const TensorMap& references, size_t batch_size = 1) {+bool testNRMSE(TensorMap& outputs, const TensorMap& references, size_t batch_size = 1) {if (batch_size != 1) {throw std::runtime_error("The testcase 'nrmse' doesn't support any `override_model_batch_size` values besides 1 yet");@@ -1452,7 +1461,7 @@ bool testNRMSE(const TensorMap& outputs, const TensorMap& references, size_t batstd::vector<std::string> skipped_layers;skipped_layers = splitStringList(FLAGS_skip_output_layers, ';');- for (const auto& [tensorName, output] : outputs) {+ for (auto& [tensorName, output] : outputs) {if (std::find(skipped_layers.begin(), skipped_layers.end(), tensorName) != skipped_layers.end()) {std::cout << "Skip NRMSE test for layers: " << tensorName << std::endl;continue;@@ -1474,7 +1483,10 @@ bool testNRMSE(const TensorMap& outputs, const TensorMap& references, size_t batauto refSoftMax = softmax(refOutput);std::copy_n(actSoftMax.begin(), output.get_size(), output.data<float>());- std::copy_n(refSoftMax.begin(), referencesIterator->second.get_size(), referencesIterator->second.data<float>());+ // Why reference data is not updated?+ std::copy_n(refSoftMax.begin(),+ referencesIterator->second.get_size(),+ const_cast<float*>(referencesIterator->second.data<float>()));}std::cout << "Compare " << tensorName << " with reference" << std::endl;@@ -2277,10 +2289,10 @@ static int runSingleImageTest() {std::cout << "Run inference on " << FLAGS_device << std::endl;const auto startTime = Time::now();- const auto outInference = runInfer(inferRequest, compiledModel, inTensors, dumpedInputsPaths);+ auto outInference = runInfer(inferRequest, compiledModel, inTensors, dumpedInputsPaths);const auto endTime = Time::now();- const TensorMap& outputTensors = outInference.first;+ TensorMap& outputTensors = outInference.first;printPerformanceCountsAndLatency(numberOfTestCase, outInference.second, endTime - startTime);diff --git a/src/plugins/intel_npu/tools/single-image-test/semantic_segmentation_helpers.hpp b/src/plugins/intel_npu/tools/single-image-test/semantic_segmentation_helpers.hppindex abb86996c3c..0b2aae46d10 100644--- a/src/plugins/intel_npu/tools/single-image-test/semantic_segmentation_helpers.hpp+++ b/src/plugins/intel_npu/tools/single-image-test/semantic_segmentation_helpers.hpp@@ -22,9 +22,7 @@ std::vector<std::pair<bool, float>> mean_IoU(std::vector<T> actOutput, std::vectuint32_t ignoreLabel) {std::vector<uint32_t> output;for (size_t i = 0; i < refOutput.size(); i++) {- auto mask = (refOutput[i] < classes) & (actOutput[i] < classes);-- if (mask == 1) {+ if ((refOutput[i] < classes) && (actOutput[i] < classes)) {output.push_back(classes * refOutput[i] + actOutput[i]);}}diff --git a/src/plugins/template/backend/evaluates_map.cpp b/src/plugins/template/backend/evaluates_map.cppindex 26d95ccdeb3..06d5eee729b 100644--- a/src/plugins/template/backend/evaluates_map.cpp+++ b/src/plugins/template/backend/evaluates_map.cpp@@ -12,19 +12,19 @@ std::vector<float> get_floats(const ov::Tensor& input, const ov::Shape& shape) {switch (input.get_element_type()) {case ov::element::bf16: {- ov::bfloat16* p = input.data<ov::bfloat16>();+ auto p = input.data<ov::bfloat16>();for (size_t i = 0; i < input_size; ++i) {result[i] = float(p[i]);}} break;case ov::element::f16: {- ov::float16* p = input.data<ov::float16>();+ auto p = input.data<ov::float16>();for (size_t i = 0; i < input_size; ++i) {result[i] = float(p[i]);}} break;case ov::element::f32: {- float* p = input.data<float>();+ auto p = input.data<float>();memcpy(result.data(), p, input_size * sizeof(float));} break;default:diff --git a/src/plugins/template/backend/ops/binary_convolution.cpp b/src/plugins/template/backend/ops/binary_convolution.cppindex ff24f022a3f..3ac2a36044d 100644--- a/src/plugins/template/backend/ops/binary_convolution.cpp+++ b/src/plugins/template/backend/ops/binary_convolution.cpp@@ -14,8 +14,8 @@ inline void evaluate(const std::shared_ptr<ov::op::v1::BinaryConvolution>& op,using T_IN = typename ov::element_type_traits<t_in>::value_type;using T_F = typename ov::element_type_traits<t_f>::value_type;- const auto in_data_ptr = static_cast<T_IN*>(inputs[0].data());- const auto filter_data_ptr = static_cast<T_F*>(inputs[1].data());+ const auto in_data_ptr = static_cast<const T_IN*>(inputs[0].data());+ const auto filter_data_ptr = static_cast<const T_F*>(inputs[1].data());auto out_data_ptr = static_cast<T_IN*>(outputs[0].data());const auto in_shape = inputs[0].get_shape();const auto filter_shape = inputs[1].get_shape();diff --git a/src/plugins/template/backend/ops/ctc_loss.cpp b/src/plugins/template/backend/ops/ctc_loss.cppindex e63c1bf5297..ad7253eab11 100644--- a/src/plugins/template/backend/ops/ctc_loss.cpp+++ b/src/plugins/template/backend/ops/ctc_loss.cpp@@ -32,12 +32,12 @@ inline void evaluate(const std::shared_ptr<ov::op::v4::CTCLoss>& op,const ov::TensorVector& inputs) {using T1 = typename ov::element_type_traits<ET1>::value_type;using T2 = typename ov::element_type_traits<ET2>::value_type;- ov::reference::CTCLoss<T1, T2>(static_cast<T1*>(inputs[0].data()),+ ov::reference::CTCLoss<T1, T2>(static_cast<const T1*>(inputs[0].data()),inputs[0].get_shape(),- static_cast<T2*>(inputs[1].data()),- static_cast<T2*>(inputs[2].data()),- static_cast<T2*>(inputs[3].data()),- static_cast<T2*>(inputs[4].data()),+ static_cast<const T2*>(inputs[1].data()),+ static_cast<const T2*>(inputs[2].data()),+ static_cast<const T2*>(inputs[3].data()),+ static_cast<const T2*>(inputs[4].data()),op->get_preprocess_collapse_repeated(),op->get_ctc_merge_repeated(),op->get_unique(),diff --git a/src/plugins/template/backend/ops/interpolate.cpp b/src/plugins/template/backend/ops/interpolate.cppindex a6a5cd70c2b..e804b35fd7e 100644--- a/src/plugins/template/backend/ops/interpolate.cpp+++ b/src/plugins/template/backend/ops/interpolate.cpp@@ -14,11 +14,11 @@ bool evaluate(const std::shared_ptr<ov::op::v0::Interpolate>& op,ov::element::Type input_et = op->get_input_element_type(0);switch (input_et) {case ov::element::f64:- ov::reference::interpolate<double>(inputs[0].data<double>(),- op->get_input_partial_shape(0),- outputs[0].data<double>(),- op->get_output_shape(0),- op->get_attrs());+ ov::reference::interpolate(inputs[0].data<double>(),+ op->get_input_partial_shape(0),+ outputs[0].data<double>(),+ op->get_output_shape(0),+ op->get_attrs());break;case ov::element::f32:ov::reference::interpolate<float>(inputs[0].data<float>(),@@ -157,7 +157,7 @@ std::vector<float> get_scales_vector(const ov::TensorVector& args,std::vector<float> scales;size_t num_of_axes = axes.size();if (attrs.shape_calculation_mode == ov::op::util::InterpolateBase::ShapeCalcMode::SCALES) {- float* scales_ptr = args[scales_port].data<float>();+ auto scales_ptr = args[scales_port].data<float>();scales.insert(scales.end(), scales_ptr, scales_ptr + num_of_axes);} else {auto target_shape = get_target_shape_vector(args, num_of_axes);@@ -209,7 +209,7 @@ bool evaluate_interpolate(const std::shared_ptr<ov::op::v11::Interpolate>& op,size_t bytes_in_padded_input = shape_size(padded_input_shape) * type_size;std::vector<uint8_t> padded_input_data(bytes_in_padded_input, 0);- const uint8_t* data_ptr = static_cast<uint8_t*>(inputs[0].data());+ auto data_ptr = static_cast<const uint8_t*>(inputs[0].data());uint8_t* padded_data_ptr = padded_input_data.data();reference::pad_input_data(data_ptr,diff --git a/src/plugins/template/backend/ops/mvn.cpp b/src/plugins/template/backend/ops/mvn.cppindex 17c913787f0..aa566ee2b86 100644--- a/src/plugins/template/backend/ops/mvn.cpp+++ b/src/plugins/template/backend/ops/mvn.cpp@@ -21,7 +21,7 @@ bool evaluate(const std::shared_ptr<ov::op::v0::MVN>& op, ov::TensorVector& outpnamespace mvn_6_axes {template <typename T>ov::AxisSet mvn_6_reduction_axes(const ov::Tensor& axes_input, size_t rank) {- T* a = axes_input.data<T>();+ const T* a = axes_input.data<T>();auto v = std::vector<T>(a, a + axes_input.get_shape()[0]);std::vector<size_t> axes(v.size(), 0);for (size_t i = 0; i < v.size(); i++) {diff --git a/src/plugins/template/backend/ops/reorg_yolo.cpp b/src/plugins/template/backend/ops/reorg_yolo.cppindex 8e92e76ddf7..b41587cacfb 100644--- a/src/plugins/template/backend/ops/reorg_yolo.cpp+++ b/src/plugins/template/backend/ops/reorg_yolo.cpp@@ -9,7 +9,7 @@bool evaluate(const std::shared_ptr<ov::op::v0::ReorgYolo>& op,ov::TensorVector& outputs,const ov::TensorVector& inputs) {- ov::reference::reorg_yolo(static_cast<char*>(inputs[0].data()),+ ov::reference::reorg_yolo(static_cast<const char*>(inputs[0].data()),static_cast<char*>(outputs[0].data()),inputs[0].get_shape(),op->get_strides().at(0),diff --git a/src/plugins/template/backend/ops/sequences.cpp b/src/plugins/template/backend/ops/sequences.cppindex 8704acff310..44f2ddd5046 100644--- a/src/plugins/template/backend/ops/sequences.cpp+++ b/src/plugins/template/backend/ops/sequences.cpp@@ -15,17 +15,17 @@ inline void evaluate(const std::shared_ptr<ov::op::v5::RNNSequence>& op,const ov::TensorVector& inputs) {using T1 = typename ov::element_type_traits<t1>::value_type;using T2 = typename ov::element_type_traits<t2>::value_type;- ov::reference::rnn_sequence<T1, T2>(static_cast<char*>(inputs[0].data()),+ ov::reference::rnn_sequence<T1, T2>(static_cast<const char*>(inputs[0].data()),inputs[0].get_shape(),- static_cast<char*>(inputs[1].data()),+ static_cast<const char*>(inputs[1].data()),inputs[1].get_shape(),- static_cast<char*>(inputs[2].data()),+ static_cast<const char*>(inputs[2].data()),inputs[2].get_shape(),- static_cast<char*>(inputs[3].data()),+ static_cast<const char*>(inputs[3].data()),inputs[3].get_shape(),- static_cast<char*>(inputs[4].data()),+ static_cast<const char*>(inputs[4].data()),inputs[4].get_shape(),- static_cast<char*>(inputs[5].data()),+ static_cast<const char*>(inputs[5].data()),inputs[5].get_shape(),static_cast<char*>(outputs[0].data()),static_cast<char*>(outputs[1].data()),@@ -61,19 +61,19 @@ inline void evaluate(const std::shared_ptr<ov::op::v5::LSTMSequence>& op,const ov::TensorVector& inputs) {using T1 = typename ov::element_type_traits<t1>::value_type;using T2 = typename ov::element_type_traits<t2>::value_type;- ov::reference::lstm_sequence<T1, T2>(static_cast<char*>(inputs[0].data()),+ ov::reference::lstm_sequence<T1, T2>(static_cast<const char*>(inputs[0].data()),inputs[0].get_shape(),- static_cast<char*>(inputs[1].data()),+ static_cast<const char*>(inputs[1].data()),inputs[1].get_shape(),- static_cast<char*>(inputs[2].data()),+ static_cast<const char*>(inputs[2].data()),inputs[2].get_shape(),- static_cast<char*>(inputs[3].data()),+ static_cast<const char*>(inputs[3].data()),inputs[3].get_shape(),- static_cast<char*>(inputs[4].data()),+ static_cast<const char*>(inputs[4].data()),inputs[4].get_shape(),- static_cast<char*>(inputs[5].data()),+ static_cast<const char*>(inputs[5].data()),inputs[5].get_shape(),- static_cast<char*>(inputs[6].data()),+ static_cast<const char*>(inputs[6].data()),inputs[6].get_shape(),static_cast<char*>(outputs[0].data()),static_cast<char*>(outputs[1].data()),@@ -112,17 +112,17 @@ inline void evaluate(const std::shared_ptr<ov::op::v5::GRUSequence>& op,const ov::TensorVector& inputs) {using T1 = typename ov::element_type_traits<t1>::value_type;using T2 = typename ov::element_type_traits<t2>::value_type;- ov::reference::gru_sequence<T1, T2>(static_cast<char*>(inputs[0].data()),+ ov::reference::gru_sequence<T1, T2>(static_cast<const char*>(inputs[0].data()),inputs[0].get_shape(),- static_cast<char*>(inputs[1].data()),+ static_cast<const char*>(inputs[1].data()),inputs[1].get_shape(),- static_cast<char*>(inputs[2].data()),+ static_cast<const char*>(inputs[2].data()),inputs[2].get_shape(),- static_cast<char*>(inputs[3].data()),+ static_cast<const char*>(inputs[3].data()),inputs[3].get_shape(),- static_cast<char*>(inputs[4].data()),+ static_cast<const char*>(inputs[4].data()),inputs[4].get_shape(),- static_cast<char*>(inputs[5].data()),+ static_cast<const char*>(inputs[5].data()),inputs[5].get_shape(),static_cast<char*>(outputs[0].data()),static_cast<char*>(outputs[1].data()),@@ -160,17 +160,17 @@ inline void evaluate(const std::shared_ptr<ov::op::internal::AUGRUSequence>& op,const ov::TensorVector& inputs) {using T1 = typename ov::element_type_traits<t1>::value_type;using T2 = typename ov::element_type_traits<t2>::value_type;- ov::reference::gru_sequence<T1, T2>(static_cast<char*>(inputs[0].data()),+ ov::reference::gru_sequence<T1, T2>(static_cast<const char*>(inputs[0].data()),inputs[0].get_shape(),- static_cast<char*>(inputs[1].data()),+ static_cast<const char*>(inputs[1].data()),inputs[1].get_shape(),- static_cast<char*>(inputs[2].data()),+ static_cast<const char*>(inputs[2].data()),inputs[2].get_shape(),- static_cast<char*>(inputs[3].data()),+ static_cast<const char*>(inputs[3].data()),inputs[3].get_shape(),- static_cast<char*>(inputs[4].data()),+ static_cast<const char*>(inputs[4].data()),inputs[4].get_shape(),- static_cast<char*>(inputs[5].data()),+ static_cast<const char*>(inputs[5].data()),inputs[5].get_shape(),static_cast<char*>(outputs[0].data()),static_cast<char*>(outputs[1].data()),@@ -179,7 +179,7 @@ inline void evaluate(const std::shared_ptr<ov::op::internal::AUGRUSequence>& op,op->get_clip(),op->get_direction(),op->get_linear_before_reset(),- static_cast<char*>(inputs[6].data()));+ static_cast<const char*>(inputs[6].data()));}} // namespace augru_seqdiff --git a/src/plugins/template/src/plugin.cpp b/src/plugins/template/src/plugin.cppindex d46339ef94c..cbd1f07f74e 100644--- a/src/plugins/template/src/plugin.cpp+++ b/src/plugins/template/src/plugin.cpp@@ -72,7 +72,7 @@ std::shared_ptr<ov::Model> get_ov_model_from_blob(const ov::template_plugin::Plusize_t offset,const ov::AnyMap& properties) {if (auto blob_it = properties.find(ov::hint::compiled_blob.name()); blob_it != properties.end()) {- if (auto&& blob = blob_it->second.as<ov::Tensor>(); blob) {+ if (auto blob = blob_it->second.as<ov::Tensor>(); blob) {ov::SharedStreamBuffer shared_buffer(reinterpret_cast<char*>(blob.data()), blob.get_byte_size());std::istream blob_stream(&shared_buffer);blob_stream.seekg(offset, std::ios::beg);diff --git a/src/plugins/template/tests/functional/CMakeLists.txt b/src/plugins/template/tests/functional/CMakeLists.txtindex 8c778ab2101..7b3fffe3b01 100644--- a/src/plugins/template/tests/functional/CMakeLists.txt+++ b/src/plugins/template/tests/functional/CMakeLists.txt@@ -20,6 +20,8 @@ ov_add_test_target(INCLUDES"${OpenVINOTemplatePlugin_SOURCE_DIR}/include""${CMAKE_CURRENT_SOURCE_DIR}/op_reference"+ DEFINES+ IN_OV_COMPONENTADD_CLANG_FORMATLABELSOV UNIT TEMPLATEdiff --git a/src/plugins/template/tests/functional/op_reference/constant.cpp b/src/plugins/template/tests/functional/op_reference/constant.cppindex 520c993578e..8d86ef5ed85 100644--- a/src/plugins/template/tests/functional/op_reference/constant.cpp+++ b/src/plugins/template/tests/functional/op_reference/constant.cpp@@ -113,7 +113,7 @@ private:const auto A = std::make_shared<op::v0::Constant>(params.inType,params.inputShape,- std::vector<std::string>{std::to_string(*reinterpret_cast<int*>(params.inputData.data()))});+ std::vector<std::string>{std::to_string(*reinterpret_cast<const int*>(params.inputData.data()))});return std::make_shared<Model>(A, ParameterVector{});}};diff --git a/src/tests/functional/plugin/shared/include/behavior/compiled_model/compiled_model_base.hpp b/src/tests/functional/plugin/shared/include/behavior/compiled_model/compiled_model_base.hppindex ee443d5603f..832d0e71115 100644--- a/src/tests/functional/plugin/shared/include/behavior/compiled_model/compiled_model_base.hpp+++ b/src/tests/functional/plugin/shared/include/behavior/compiled_model/compiled_model_base.hpp@@ -107,8 +107,8 @@ public:}bool compareTensors(const ov::Tensor& t1, const ov::Tensor& t2) {- void* data1;- void* data2;+ const void* data1;+ const void* data2;try {data1 = t1.data();} catch (const ov::Exception&) {diff --git a/src/tests/functional/plugin/shared/src/behavior/ov_infer_request/io_tensor.cpp b/src/tests/functional/plugin/shared/src/behavior/ov_infer_request/io_tensor.cppindex 92c409627a9..3ecaf49fd1b 100644--- a/src/tests/functional/plugin/shared/src/behavior/ov_infer_request/io_tensor.cpp+++ b/src/tests/functional/plugin/shared/src/behavior/ov_infer_request/io_tensor.cpp@@ -366,8 +366,8 @@ void OVInferRequestCheckTensorPrecision::SetUp() {}bool OVInferRequestCheckTensorPrecision::compareTensors(const ov::Tensor& t1, const ov::Tensor& t2) {- void* data1;- void* data2;+ const void* data1;+ const void* data2;try {data1 = t1.data();} catch (const ov::Exception&) {diff --git a/src/tests/functional/shared_test_classes/src/base/utils/compare_results.cpp b/src/tests/functional/shared_test_classes/src/base/utils/compare_results.cppindex 47728a8fcfe..e137ba3052c 100644--- a/src/tests/functional/shared_test_classes/src/base/utils/compare_results.cpp+++ b/src/tests/functional/shared_test_classes/src/base/utils/compare_results.cpp@@ -82,8 +82,8 @@ inline void validate_colors(const ov::Tensor& expected, const ov::Tensor& actual#define CASE(X) \case X: \validate_colors( \- static_cast<ov::fundamental_type_for<X>*>(expected.data()), \- static_cast<ov::fundamental_type_for<X>*>(actual.data()), \+ static_cast<const ov::fundamental_type_for<X>*>(expected.data()), \+ static_cast<const ov::fundamental_type_for<X>*>(actual.data()), \expected.get_size(), \dev_threshold, \abs_threshold); \diff --git a/src/tests/functional/shared_test_classes/src/single_op/generate_proposals.cpp b/src/tests/functional/shared_test_classes/src/single_op/generate_proposals.cppindex 3232979cb3b..ccaf0d60c05 100644--- a/src/tests/functional/shared_test_classes/src/single_op/generate_proposals.cpp+++ b/src/tests/functional/shared_test_classes/src/single_op/generate_proposals.cpp@@ -112,8 +112,8 @@ void GenerateProposalsLayerTest::compare(const std::vector<ov::Tensor>& expectedconst auto actualNumRois = actual[i].get_shape()[0];ASSERT_LE(expectedNumRois, actualNumRois);- const auto actualBuffer = static_cast<uint8_t*>(actual[i].data());- const auto expectedBuffer = static_cast<uint8_t*>(expected[i].data());+ const auto actualBuffer = static_cast<const uint8_t*>(actual[i].data());+ const auto expectedBuffer = static_cast<const uint8_t*>(expected[i].data());const auto outputSize = i == 0 ? 4 : 1;rel_threshold = ov::test::utils::tensor_comparation::calculate_default_rel_threshold(diff --git a/src/tests/test_utils/common_test_utils/src/test_case.cpp b/src/tests/test_utils/common_test_utils/src/test_case.cppindex 6c002e32e65..2d8bbb2578b 100644--- a/src/tests/test_utils/common_test_utils/src/test_case.cpp+++ b/src/tests/test_utils/common_test_utils/src/test_case.cpp@@ -153,8 +153,8 @@ std::pair<testing::AssertionResult, size_t> TestCase::compare_results(size_t tolbreak;case element::Type_t::string: {res = ::testing::AssertionSuccess();- std::string* exp_strings = exp_result.data<std::string>();- std::string* res_strings = result_tensor.data<std::string>();+ const std::string* exp_strings = exp_result.data<std::string>();+ const std::string* res_strings = result_tensor.data<std::string>();for (size_t i = 0; i < exp_result.get_size(); ++i) {if (exp_strings[i] != res_strings[i]) {res = ::testing::AssertionFailure() << "Wrong string value at index " << i << ", expected \""