Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[OV JS] Fix named inputs in inferAsync (#37740)

Ali Ihsan Nergiz994e080adb8a
diff --git a/src/bindings/js/node/include/helper.hpp b/src/bindings/js/node/include/helper.hpp
index af53c70f5c6..ed1744f3dc2 100644
--- a/src/bindings/js/node/include/helper.hpp
+++ b/src/bindings/js/node/include/helper.hpp
@@ -7,6 +7,7 @@
#include <filesystem>
#include <unordered_set>
+#include <utility>
#include <variant>
#include "openvino/core/type/element_type.hpp"
@@ -142,8 +143,11 @@ Napi::Object cpp_to_js(const Napi::Env& env, const ov::CompiledModel& compiled_m
*/
Napi::Object cpp_to_js(const Napi::Env& env, const ov::Version& version);
-/** @brief Takes Napi::Value and parse Napi::Array or Napi::Object to ov::TensorVector. */
-ov::TensorVector parse_input_data(const Napi::Value& input);
+using NamedInputData = std::vector<std::pair<std::string, ov::Tensor>>;
+using ParsedInputData = std::variant<ov::TensorVector, NamedInputData>;
+
+/** @brief Parses positional or named input tensors without losing object keys. */
+ParsedInputData parse_input_data(const Napi::Value& input);
/** @brief Gets an input/output tensor from InferRequest by key. */
ov::Tensor get_request_tensor(ov::InferRequest& infer_request, const std::string key);
diff --git a/src/bindings/js/node/include/infer_request.hpp b/src/bindings/js/node/include/infer_request.hpp
index 39ad59e0eb1..341afd90c03 100644
--- a/src/bindings/js/node/include/infer_request.hpp
+++ b/src/bindings/js/node/include/infer_request.hpp
@@ -7,6 +7,7 @@
#include <thread>
+#include "node/include/helper.hpp"
#include "openvino/runtime/infer_request.hpp"
struct TsfnContext {
@@ -18,7 +19,7 @@ struct TsfnContext {
Napi::ThreadSafeFunction tsfn;
ov::InferRequest* _ir;
- std::vector<ov::Tensor> _inputs;
+ ParsedInputData _inputs;
std::map<std::string, ov::Tensor> result;
};
diff --git a/src/bindings/js/node/lib/addon.ts b/src/bindings/js/node/lib/addon.ts
index 525ed1602f7..c930ddcf964 100644
--- a/src/bindings/js/node/lib/addon.ts
+++ b/src/bindings/js/node/lib/addon.ts
@@ -574,9 +574,8 @@ export interface InferRequest {
};
/**
* It infers specified input(s) in the asynchronous mode.
- * @param inputData An object with the key-value pairs where the key is the
- * input name and value is a tensor or an array with tensors. If the model has
- * multiple inputs, the Tensors must be passed in the correct order.
+ * @param inputData Either an object mapping input names to tensors, or an array
+ * of tensors in model input order.
*/
inferAsync(
inputData: { [inputName: string]: Tensor } | Tensor[],
diff --git a/src/bindings/js/node/src/helper.cpp b/src/bindings/js/node/src/helper.cpp
index e930d56402e..bbf427486e6 100644
--- a/src/bindings/js/node/src/helper.cpp
+++ b/src/bindings/js/node/src/helper.cpp
@@ -349,24 +349,26 @@ Napi::Object cpp_to_js(const Napi::Env& env, const ov::Version& version) {
return version_obj;
}
-ov::TensorVector parse_input_data(const Napi::Value& input) {
- ov::TensorVector parsed_input;
+ParsedInputData parse_input_data(const Napi::Value& input) {
if (input.IsArray()) {
+ ov::TensorVector parsed_input;
auto inputs = input.As<Napi::Array>();
for (uint32_t i = 0; i < inputs.Length(); ++i) {
parsed_input.emplace_back(cast_to_tensor(static_cast<Napi::Value>(inputs[i])));
}
+ return parsed_input;
} else if (input.IsObject()) {
+ NamedInputData parsed_input;
auto inputs = input.ToObject();
const auto& keys = inputs.GetPropertyNames();
for (uint32_t i = 0; i < keys.Length(); ++i) {
- auto value = inputs.Get(static_cast<Napi::Value>(keys[i]).ToString().Utf8Value());
- parsed_input.emplace_back(cast_to_tensor(static_cast<Napi::Value>(value)));
+ auto name = static_cast<Napi::Value>(keys[i]).ToString().Utf8Value();
+ parsed_input.emplace_back(name, cast_to_tensor(inputs.Get(name)));
}
+ return parsed_input;
} else {
OPENVINO_THROW("parse_input_data(): wrong arg");
}
- return parsed_input;
}
ov::Tensor get_request_tensor(ov::InferRequest& infer_request, const std::string key) {
diff --git a/src/bindings/js/node/src/infer_request.cpp b/src/bindings/js/node/src/infer_request.cpp
index 6ecfe193966..368548857ad 100644
--- a/src/bindings/js/node/src/infer_request.cpp
+++ b/src/bindings/js/node/src/infer_request.cpp
@@ -225,8 +225,14 @@ void perform_inference_thread(TsfnContext* context) {
std::exception_ptr stored_exception;
try {
const std::lock_guard<std::mutex> lock(infer_mutex);
- for (size_t i = 0; i < context->_inputs.size(); ++i) {
- context->_ir->set_input_tensor(i, context->_inputs[i]);
+ if (const auto* positional_inputs = std::get_if<ov::TensorVector>(&context->_inputs)) {
+ for (size_t i = 0; i < positional_inputs->size(); ++i) {
+ context->_ir->set_input_tensor(i, positional_inputs->at(i));
+ }
+ } else {
+ for (const auto& [name, tensor] : std::get<NamedInputData>(context->_inputs)) {
+ context->_ir->set_tensor(name, tensor);
+ }
}
context->_ir->infer();
diff --git a/src/bindings/js/node/tests/unit/infer_request.test.js b/src/bindings/js/node/tests/unit/infer_request.test.js
index b3239957b1c..4a34d90749b 100644
--- a/src/bindings/js/node/tests/unit/infer_request.test.js
+++ b/src/bindings/js/node/tests/unit/infer_request.test.js
@@ -141,6 +141,36 @@ describe("ov.InferRequest tests", () => {
});
});
+ it("inferAsync() assigns object inputs by name", async () => {
+ const { addModel } = testModels;
+ const core = new ov.Core();
+ const modelXml = (await fs.readFile(addModel.xml, "utf8")).replace(
+ 'type="Add"',
+ 'type="Subtract"',
+ );
+ const model = core.readModelSync(Buffer.from(modelXml));
+ const compiled = core.compileModelSync(model, "CPU");
+ const inputNames = compiled.inputs.map((input) => input.anyName);
+ const tensors = new Map(
+ inputNames.map((name, index) => [
+ name,
+ new ov.Tensor(ov.element.f32, [2, 1], new Float32Array(2).fill(index === 0 ? 11 : 3)),
+ ]),
+ );
+ const infer = async (inputs) => {
+ const result = await compiled.createInferRequest().inferAsync(inputs);
+ return Object.values(result)[0].data[0];
+ };
+ const namedInputs = (names) =>
+ Object.fromEntries(names.map((name) => [name, tensors.get(name)]));
+ const positionalInputs = (names) => names.map((name) => tensors.get(name));
+
+ assert.strictEqual(await infer(namedInputs(inputNames)), 8);
+ assert.strictEqual(await infer(namedInputs(inputNames.toReversed())), 8);
+ assert.strictEqual(await infer(positionalInputs(inputNames)), 8);
+ assert.strictEqual(await infer(positionalInputs(inputNames.toReversed())), -8);
+ });
+
describe("BigInt InferRequest support", () => {
let core, originalModel, compiledModel, inferRequest;