Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions samples/cpp/benchmark_app/inputs_filling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ class ClassificationResult {

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);
Expand Down
4 changes: 3 additions & 1 deletion samples/cpp/model_creation_sample/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
16 changes: 12 additions & 4 deletions src/core/dev_api/openvino/runtime/itensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,32 @@ class OPENVINO_API ITensor : public std::enable_shared_from_this<ITensor> {
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
*
Expand Down
58 changes: 52 additions & 6 deletions src/core/include/openvino/runtime/tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ class OPENVINO_API Tensor {
*/
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
Expand All @@ -138,6 +149,16 @@ class OPENVINO_API Tensor {
*/
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
Expand Down Expand Up @@ -197,23 +218,48 @@ class OPENVINO_API Tensor {
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")
#endif
void* 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")
#endif
T* 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
Expand All @@ -234,7 +280,7 @@ class OPENVINO_API Tensor {
* @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 (...) {
Expand All @@ -250,7 +296,7 @@ class OPENVINO_API Tensor {
* @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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/core/reference/include/openvino/reference/mvn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/reference/src/op/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion src/core/reference/src/op/tensor_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/core/src/bound_evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down
6 changes: 4 additions & 2 deletions src/core/src/op/constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/src/op/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/op/depth_to_space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ bool DepthToSpace::evaluate(TensorVector& outputs, const TensorVector& inputs) c
OPENVINO_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()),
Expand Down
4 changes: 2 additions & 2 deletions src/core/src/op/divide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ bool evaluate_bound(const Node* node, TensorVector& output_values, bool is_upper
return 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());
Expand Down Expand Up @@ -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 arguments
const 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});
Expand Down
4 changes: 2 additions & 2 deletions src/core/src/op/roi_align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/op/space_to_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/op/space_to_depth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool SpaceToDepth::evaluate(TensorVector& outputs, const TensorVector& inputs) c
OPENVINO_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()),
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/op/split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/core/src/op/transpose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/core/src/op/util/pad_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/core/src/runtime/itensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ov
Loading
Loading