Skip to content

Add python bindings to read_tensor_data#32984

Open
olpipi wants to merge 4 commits intoopenvinotoolkit:masterfrom
olpipi:read_tensor_data_py
Open

Add python bindings to read_tensor_data#32984
olpipi wants to merge 4 commits intoopenvinotoolkit:masterfrom
olpipi:read_tensor_data_py

Conversation

@olpipi
Copy link
Contributor

@olpipi olpipi commented Nov 21, 2025

Details:

  • Add python bindings to read_tensor_data
  • ...

Tickets:

  • ticket-id

@github-actions github-actions bot added category: Python API OpenVINO Python bindings category: tools OpenVINO C++ / Python tools category: OVC OVC tool labels Nov 21, 2025
@olpipi olpipi changed the title Add bindings to read_tensor_data [TEST PR] Add bindings to read_tensor_data Nov 21, 2025
@olpipi olpipi force-pushed the read_tensor_data_py branch from 8e56854 to 56a5139 Compare January 7, 2026 15:34
@olpipi olpipi changed the title [TEST PR] Add bindings to read_tensor_data Add python bindings to read_tensor_data Jan 7, 2026
@olpipi olpipi marked this pull request as ready for review January 7, 2026 16:18
@olpipi olpipi requested review from a team as code owners January 7, 2026 16:18
@olpipi olpipi requested a review from praasz January 7, 2026 16:18
@olpipi olpipi force-pushed the read_tensor_data_py branch from 835410a to f5c5a3b Compare January 7, 2026 16:22
@olpipi olpipi requested a review from a team as a code owner January 8, 2026 15:53
@github-actions github-actions bot added category: inference OpenVINO Runtime library - Inference category: build OpenVINO cmake script / infra labels Jan 8, 2026
@praasz praasz added this to the 2026.0 milestone Jan 9, 2026
regclass_Tensor(m);

m.def(
"read_tensor_data",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++ API read_tensor_data is located in src/core/include/openvino/runtime/tensor.hpp
To not bloat pyopenvino.cpp, maybe it's better to move it into regclass Tensor?

void regclass_Tensor(py::module m) {

@p-wysocki

@olpipi olpipi force-pushed the read_tensor_data_py branch from e0ff46e to dce9734 Compare January 19, 2026 16:33
@praasz praasz modified the milestones: 2026.0, 2026.1 Jan 20, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds Python bindings for the read_tensor_data C++ function, enabling Python users to load tensor data directly from binary files with memory-mapping support.

Changes:

  • Added is_tensor_read_only() helper function in C++ to detect read-only tensors
  • Exposed read_tensor_data function through Python bindings with proper parameter defaults
  • Modified array_from_tensor to mark numpy arrays as read-only when the underlying tensor is read-only
  • Added comprehensive test coverage for the new Python API

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/inference/src/dev/make_tensor.cpp Implements is_tensor_read_only() to identify read-only tensor views
src/inference/dev_api/openvino/runtime/make_tensor.hpp Declares is_tensor_read_only() in the dev API with documentation
src/bindings/python/src/pyopenvino/pyopenvino.cpp Adds Python binding for read_tensor_data with proper docstring
src/bindings/python/src/pyopenvino/core/common.cpp Updates array_from_tensor to set numpy array writeable flag based on tensor read-only status
src/bindings/python/src/pyopenvino/CMakeLists.txt Adds openvino::runtime::dev dependency to access dev API functions
src/bindings/python/src/openvino/init.py Exports read_tensor_data in the main Python package
tools/ovc/openvino/init.py Exports read_tensor_data in the OVC tool package
tools/benchmark_tool/openvino/init.py Exports read_tensor_data in the benchmark tool package
src/bindings/python/tests/test_runtime/test_read_tensor_data.py Comprehensive test suite covering various use cases and edge cases

Comment on lines +626 to +632
bool is_tensor_read_only(const ov::Tensor& tensor) {
auto impl = get_tensor_impl(tensor);
if (std::dynamic_pointer_cast<ViewTensor>(impl._ptr)) {
return true;
}
return false;
}
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BLOCKER] The is_tensor_read_only function incorrectly identifies all ViewTensor subclasses as read-only, including writable ones. The current implementation checks for ViewTensor base class, but AllocatedTensor (which owns and manages writable memory) and StridedViewTensor (writable view with strides) also inherit from ViewTensor. This will cause normal allocated tensors and writable view tensors to be incorrectly marked as read-only in Python.

The function should specifically check for ReadOnlyViewTensor and ReadOnlyStridedViewTensor, not the base ViewTensor class. The correct implementation should be:

bool is_tensor_read_only(const ov::Tensor& tensor) {
    auto impl = get_tensor_impl(tensor);
    if (std::dynamic_pointer_cast<ReadOnlyViewTensor>(impl._ptr) ||
        std::dynamic_pointer_cast<ReadOnlyStridedViewTensor>(impl._ptr)) {
        return true;
    }
    return false;
}

This is critical because it affects the Python bindings' behavior - regular allocated tensors would become read-only incorrectly, breaking existing functionality.

Copilot uses AI. Check for mistakes.
@praasz praasz requested a review from Copilot February 25, 2026 08:19
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

src/bindings/python/tests/test_runtime/test_read_tensor_data.py:104

  • This call formatting is inconsistent with the rest of the file (closing parenthesis on the same line as the last argument). Consider formatting it like the other multi-line calls (or running the project formatter) to keep the test file style consistent.
        ov.read_tensor_data(
            path,
            element_type=ov.Type.f32,
            shape=ov.PartialShape(list(shape)),
            offset_in_bytes=1,
            mmap=mmap)

Comment on lines +307 to 323
auto data_ptr = std::as_const(t).data();
auto is_read_only = ov::is_tensor_read_only(t);

// Return the array as a view:
if (is_shared) {
py::array result;
if (ov_type.bitwidth() < Common::values::min_bitwidth) {
return py::array(dtype, t.get_byte_size(), t.data(), py::cast(t));
result = py::array(dtype, t.get_byte_size(), data_ptr, py::cast(t));
} else {
result = py::array(dtype, t.get_shape(), t.get_strides(), data_ptr, py::cast(t));
}
if (is_read_only) {
// Mark array as read-only
result.attr("flags").attr("writeable") = false;
}
return py::array(dtype, t.get_shape(), t.get_strides(), t.data(), py::cast(t));
return result;
}
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the Python-visible mutability semantics for any tensor detected as ViewTensor by is_tensor_read_only(). If writable view tensors exist, they will now surface as non-writeable NumPy arrays, which is a breaking behavior change for Python users. Once is_tensor_read_only() is corrected to match the intended 'const-host view' definition, this risk should be mitigated; alternatively, scope the read-only marking to tensors created by read_tensor_data (if there is a reliable way to tag/detect that provenance).

Copilot uses AI. Check for mistakes.
Comment on lines +103 to +108
* @details A tensor is considered read-only if it was created as a view tensor from a const pointer
* using the make_tensor() function that accepts const void* host_ptr.
*
* @param tensor OpenVINO Tensor to check
*
* @return true if the tensor is read-only, false otherwise
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documented definition ('view tensor from a const pointer') doesn’t match the current implementation (which checks only whether the impl is ViewTensor). Either tighten the implementation to reflect this definition, or update the documentation to describe the actual condition being tested so callers don’t rely on incorrect semantics.

Suggested change
* @details A tensor is considered read-only if it was created as a view tensor from a const pointer
* using the make_tensor() function that accepts const void* host_ptr.
*
* @param tensor OpenVINO Tensor to check
*
* @return true if the tensor is read-only, false otherwise
* @details A tensor is considered read-only when its underlying implementation is a view
* tensor that represents a non-mutable view on existing memory. This typically includes
* tensors created by the make_tensor() overload that accepts const void* host_ptr, as
* well as other APIs that may return ViewTensor-based read-only views.
*
* @note The exact condition checked by this function is whether the internal ITensor
* implementation is of type ViewTensor; callers must not rely on more specific semantics
* such as how the tensor view was originally constructed.
*
* @param tensor OpenVINO Tensor to check
*
* @return true if the tensor is read-only according to the above condition, false otherwise

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: build OpenVINO cmake script / infra category: inference OpenVINO Runtime library - Inference category: OVC OVC tool category: Python API OpenVINO Python bindings category: tools OpenVINO C++ / Python tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants