From 3e3b04c4f5ad04d72312ababb46b5b28b21edcee Mon Sep 17 00:00:00 2001 From: Alicja Miloszewska Date: Thu, 16 Oct 2025 13:48:53 +0200 Subject: [PATCH 1/3] Fix seekoff() --- .../python/src/pyopenvino/utils/utils.hpp | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/bindings/python/src/pyopenvino/utils/utils.hpp b/src/bindings/python/src/pyopenvino/utils/utils.hpp index f28a4faa8c0c54..f35cda8f529e5c 100644 --- a/src/bindings/python/src/pyopenvino/utils/utils.hpp +++ b/src/bindings/python/src/pyopenvino/utils/utils.hpp @@ -106,26 +106,37 @@ class MemoryBuffer : public std::streambuf { pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which = std::ios_base::in) override { + if (!(which & std::ios_base::in)) { + return pos_type(off_type(-1)); + } + + const auto size = static_cast(egptr() - eback()); + off_type new_pos = 0; + switch (dir) { case std::ios_base::beg: - setg(eback(), eback() + off, egptr()); - break; - case std::ios_base::end: - setg(eback(), egptr() + off, egptr()); + new_pos = off; break; case std::ios_base::cur: - setg(eback(), gptr() + off, egptr()); + new_pos = static_cast(gptr() - eback()) + off; + break; + case std::ios_base::end: + new_pos = size + off; break; default: return pos_type(off_type(-1)); - } - return (gptr() < eback() || gptr() > egptr()) ? pos_type(off_type(-1)) : pos_type(gptr() - eback()); + } + if (new_pos < 0 || new_pos > size) { + return pos_type(off_type(-1)); + } + char* const base = eback(); + setg(base, base + new_pos, base + size); + return pos_type(new_pos); } pos_type seekpos(pos_type pos, std::ios_base::openmode which) override { - return seekoff(pos, std::ios_base::beg, which); + return seekoff(static_cast(pos), std::ios_base::beg, which); } - }; enum class PY_TYPE : int { UNKNOWN = 0, STR, INT, FLOAT, BOOL, PARTIAL_SHAPE, MODEL_DISTRIBUTION_POLICY }; From 7b63f86c073c1329eb11eed05696fe860c1883e1 Mon Sep 17 00:00:00 2001 From: Alicja Miloszewska Date: Wed, 29 Oct 2025 11:54:15 +0100 Subject: [PATCH 2/3] Replace MemoryBuffer with SharedStreamBuffer --- .../python/src/pyopenvino/core/core.cpp | 5 ++- .../src/pyopenvino/frontend/frontend.cpp | 5 ++- .../python/src/pyopenvino/utils/utils.hpp | 43 ------------------- 3 files changed, 6 insertions(+), 47 deletions(-) diff --git a/src/bindings/python/src/pyopenvino/core/core.cpp b/src/bindings/python/src/pyopenvino/core/core.cpp index 4d3e2f3b298abd..3bf497e57fd7c7 100644 --- a/src/bindings/python/src/pyopenvino/core/core.cpp +++ b/src/bindings/python/src/pyopenvino/core/core.cpp @@ -14,6 +14,7 @@ #include #include "common.hpp" +#include "openvino/runtime/shared_buffer.hpp" #include "pyopenvino/core/remote_context.hpp" #include "pyopenvino/graph/op_extension.hpp" #include "pyopenvino/utils/utils.hpp" @@ -554,8 +555,8 @@ void regclass_Core(py::module m) { info = py::buffer(model_stream).request(); } - Common::utils::MemoryBuffer mb(reinterpret_cast(info.ptr), info.size); - std::istream stream(&mb); + ov::SharedStreamBuffer mb{reinterpret_cast(info.ptr), static_cast(info.size)}; + std::istream stream{&mb}; ConditionalGILScopedRelease release; return self.import_model(stream, device_name, _properties); diff --git a/src/bindings/python/src/pyopenvino/frontend/frontend.cpp b/src/bindings/python/src/pyopenvino/frontend/frontend.cpp index 5182b829dd1591..fc8521c4e01d06 100644 --- a/src/bindings/python/src/pyopenvino/frontend/frontend.cpp +++ b/src/bindings/python/src/pyopenvino/frontend/frontend.cpp @@ -12,6 +12,7 @@ #include "openvino/frontend/exception.hpp" #include "openvino/frontend/extension/telemetry.hpp" #include "openvino/frontend/manager.hpp" +#include "openvino/runtime/shared_buffer.hpp" #include "openvino/util/file_util.hpp" #include "pyopenvino/graph/model.hpp" #include "pyopenvino/utils/utils.hpp" @@ -50,8 +51,8 @@ void regclass_frontend_FrontEnd(py::module m) { } else if (py::isinstance(py_obj, pybind11::module::import("io").attr("BytesIO"))) { // support of BytesIO py::buffer_info info = py::buffer(py_obj.attr("getbuffer")()).request(); - Common::utils::MemoryBuffer mb(reinterpret_cast(info.ptr), info.size); - std::istream _istream(&mb); + ov::SharedStreamBuffer mb{reinterpret_cast(info.ptr), static_cast(info.size)}; + std::istream _istream{&mb}; return self.load(&_istream, enable_mmap); } else { // Extended for one argument only for this time diff --git a/src/bindings/python/src/pyopenvino/utils/utils.hpp b/src/bindings/python/src/pyopenvino/utils/utils.hpp index f35cda8f529e5c..4aee27ed27a74a 100644 --- a/src/bindings/python/src/pyopenvino/utils/utils.hpp +++ b/src/bindings/python/src/pyopenvino/utils/utils.hpp @@ -96,49 +96,6 @@ class OutPyBuffer : public std::streambuf { py::object m_py_stream; }; -class MemoryBuffer : public std::streambuf { -public: - MemoryBuffer(char* data, std::size_t size) { - setg(data, data, data + size); - } - -protected: - pos_type seekoff(off_type off, - std::ios_base::seekdir dir, - std::ios_base::openmode which = std::ios_base::in) override { - if (!(which & std::ios_base::in)) { - return pos_type(off_type(-1)); - } - - const auto size = static_cast(egptr() - eback()); - off_type new_pos = 0; - - switch (dir) { - case std::ios_base::beg: - new_pos = off; - break; - case std::ios_base::cur: - new_pos = static_cast(gptr() - eback()) + off; - break; - case std::ios_base::end: - new_pos = size + off; - break; - default: - return pos_type(off_type(-1)); - } - if (new_pos < 0 || new_pos > size) { - return pos_type(off_type(-1)); - } - char* const base = eback(); - setg(base, base + new_pos, base + size); - return pos_type(new_pos); - } - - pos_type seekpos(pos_type pos, std::ios_base::openmode which) override { - return seekoff(static_cast(pos), std::ios_base::beg, which); - } -}; - enum class PY_TYPE : int { UNKNOWN = 0, STR, INT, FLOAT, BOOL, PARTIAL_SHAPE, MODEL_DISTRIBUTION_POLICY }; struct EmptyList {}; From 7c9bfe7523466929df503c2ff916a637eb89a3c5 Mon Sep 17 00:00:00 2001 From: Alicja Miloszewska Date: Thu, 30 Oct 2025 10:37:42 +0100 Subject: [PATCH 3/3] Update after pull/32594 --- src/bindings/python/src/pyopenvino/core/core.cpp | 2 +- src/bindings/python/src/pyopenvino/frontend/frontend.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bindings/python/src/pyopenvino/core/core.cpp b/src/bindings/python/src/pyopenvino/core/core.cpp index 3bf497e57fd7c7..3d0b7c6add2e18 100644 --- a/src/bindings/python/src/pyopenvino/core/core.cpp +++ b/src/bindings/python/src/pyopenvino/core/core.cpp @@ -555,7 +555,7 @@ void regclass_Core(py::module m) { info = py::buffer(model_stream).request(); } - ov::SharedStreamBuffer mb{reinterpret_cast(info.ptr), static_cast(info.size)}; + ov::SharedStreamBuffer mb{info.ptr, static_cast(info.size)}; std::istream stream{&mb}; ConditionalGILScopedRelease release; diff --git a/src/bindings/python/src/pyopenvino/frontend/frontend.cpp b/src/bindings/python/src/pyopenvino/frontend/frontend.cpp index fc8521c4e01d06..cbccc065055c88 100644 --- a/src/bindings/python/src/pyopenvino/frontend/frontend.cpp +++ b/src/bindings/python/src/pyopenvino/frontend/frontend.cpp @@ -51,7 +51,7 @@ void regclass_frontend_FrontEnd(py::module m) { } else if (py::isinstance(py_obj, pybind11::module::import("io").attr("BytesIO"))) { // support of BytesIO py::buffer_info info = py::buffer(py_obj.attr("getbuffer")()).request(); - ov::SharedStreamBuffer mb{reinterpret_cast(info.ptr), static_cast(info.size)}; + ov::SharedStreamBuffer mb{info.ptr, static_cast(info.size)}; std::istream _istream{&mb}; return self.load(&_istream, enable_mmap); } else {