-
Notifications
You must be signed in to change notification settings - Fork 3k
[OV JS] Improve Core.importModel implementation: SharedStreamBuffer + variant source #33658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[OV JS] Improve Core.importModel implementation: SharedStreamBuffer + variant source #33658
Conversation
…or sources - Add required includes: <variant>, <istream>, <memory>, shared_buffer.hpp - Replace std::stringstream with std::variant<monostate, BufferSource, TensorSource> - BufferSource: pins JS Buffer with ObjectReference, stores SharedStreamBuffer - TensorSource: pins JS TensorWrap, stores ov::Tensor directly - Add _error_msg field for proper async error propagation - Use ov::AnyMap instead of std::map<std::string, ov::Any> Part of issue openvinotoolkit#33601: Improve implementation of Node.js API Core.import_model()
- Add required includes: <type_traits>, tensor.hpp, shared_buffer.hpp - Replace direct _stream access with std::visit over source variant - BufferSource: create local std::istream over SharedStreamBuffer - TensorSource: call import_model(tensor, ...) directly - Add try/catch with proper Promise rejection on error - Handle both std::exception and unknown exceptions Part of issue openvinotoolkit#33601: Improve implementation of Node.js API Core.import_model()
…nsor support - Replace std::string + std::stringstream with ov::SharedStreamBuffer (zero-copy) - Add Tensor input support using cast_to_tensor and direct tensor overload - Use ov::js::validate pattern for consistent argument validation - Use ov::AnyMap for config consistency - Proper static_cast for Buffer length to size_t Part of issue openvinotoolkit#33601: Improve implementation of Node.js API Core.import_model()
- Use std::make_unique for RAII safety (no leak if setup throws) - Add Tensor input support: pin TensorWrap with ObjectReference - Buffer input: pin JS Buffer, use SharedStreamBuffer (zero-copy) - Store source in variant, worker thread dispatches via std::visit - Consistent ov::AnyMap usage for config Part of issue openvinotoolkit#33601: Improve implementation of Node.js API Core.import_model()
Auto-format core_wrap.hpp and core_wrap.cpp to match project code style.
- importModelSync now uses ov::js::validate() and reports standardized parameter mismatch errors - Update basic.test.js to assert on the new stable error message instead of legacy per-arg type strings Part of issue openvinotoolkit#33601: Improve implementation of Node.js API Core.import_model()
|
build_jenkins |
almilosz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I left a few comments
- Remove explicit casts in SharedStreamBuffer construction - Use overloaded helper for std::visit instead of if constexpr chain - Remove redundant data/size fields from BufferSource struct - Use raw new with try-catch guard instead of make_unique + release - Remove unnecessary regex escape in test assertions - Add test for importModel promise rejection on invalid buffer
|
build_jenkins |
|
Hey, I can see that all my comments are addressed, thank you. Unfortunately, code style check failed :( do you need help with fixing it? |
|
Thanks @almilosz for the careful review and being patient with me — appreciate it! I ran Could you please take another look when you have a moment? If anything is still not aligned with the expected style checks, I’m happy to adjust.Thanks again for offering to help — really appreciate it. |
|
Over 300 files is changed. Only changes to files you modified should stay |
e94aa96 to
47cb1a5
Compare
|
Apologies for the noise on that last push — I ran I've reset that commit and force-pushed a clean one that applies clang-format only to |
|
build_jenkins |
|
build_jenkins |
| }; | ||
|
|
||
| using Source = std::variant<std::monostate, BufferSource, TensorSource>; | ||
| Source source{std::monostate{}}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Source source{std::monostate{}}; | |
| Source source{}; |
It should use first variant as default initializer
|
|
||
| // Helper for std::visit with multiple lambdas | ||
| template <class... Ts> | ||
| struct overloaded : Ts... { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is helper in OV with this implementation variant_visitor.hpp
| context->_compiled_model = | ||
| std::visit(overloaded{ | ||
| [](std::monostate&) -> ov::CompiledModel { | ||
| throw std::runtime_error("ImportModelContext source not initialized"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not OPENVINO_THROW/ASSERT is not used?
Fixes #33601
What / Why
This refactors the Node.js bindings for
Core.importModelSync()andCore.importModel().The async Buffer path was still going through
std::string→std::stringstream, which adds extra copies. Also, the async worker didn’t cleanly separate “import from stream bytes” vs “import from tensor”, so it was easy to end up treating tensor data like a stream. This change switches Buffer imports toov::SharedStreamBufferand makes the async worker explicitly dispatch based on input type.No JS API changes — just internal implementation cleanup.
What changed
ov::SharedStreamBuffer+std::istreaminstead of building astd::stringstream.ImportModelContextnow stores the input asstd::variant<std::monostate, BufferSource, TensorSource>.BufferSourcekeeps a persistent reference to the JS Buffer so it can’t be GC’d while the worker is running, and wraps the underlying bytes withov::SharedStreamBuffer.TensorSourcekeeps a persistent reference to the JS object backingTensorWrap, and storesov::Tensordirectly.importModelThreadusesstd::visitto call the rightov::Core::import_modeloverload (stream vs tensor).Error.Files changed
src/bindings/js/node/include/core_wrap.hppsrc/bindings/js/node/src/core_wrap.cppsrc/bindings/js/node/tests/unit/basic.test.jsChecks
Ran:
npm run lintnpm run formatnpm testCC: @almilosz @Retribution98