Skip to content
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

[JS API] Fix inference for outputs with missing names #28665

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/bindings/js/node/include/infer_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,6 @@ class InferRequestWrap : public Napi::ObjectWrap<InferRequestWrap> {

void FinalizerCallback(Napi::Env env, void* finalizeData, TsfnContext* context);

std::map<std::string, ov::Tensor> get_js_infer_result(ov::InferRequest* infer_request);

void performInferenceThread(TsfnContext* context);
43 changes: 26 additions & 17 deletions src/bindings/js/node/src/infer_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,33 @@ Napi::Value InferRequestWrap::get_output_tensor(const Napi::CallbackInfo& info)
return TensorWrap::wrap(info.Env(), tensor);
}

std::map<std::string, ov::Tensor> get_js_infer_result(ov::InferRequest* infer_request) {
auto model_outputs = infer_request->get_compiled_model().outputs();
std::map<std::string, ov::Tensor> outputs;
Copy link
Contributor

Choose a reason for hiding this comment

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

Here is better to use model output ID (number) to show all outputs.

In case model has 3 outputs but output 2,3 have same tensor (same names) then this map shows only two outputs but model has got three.

for (auto& output : model_outputs) {
const auto& tensor = infer_request->get_tensor(output);
auto new_tensor = ov::Tensor(tensor.get_element_type(), tensor.get_shape());
tensor.copy_to(new_tensor);
const auto name = output.get_names().empty() ? output.get_node()->get_name() : output.get_any_name();

auto key = name;
int counter = 1;
while (outputs.find(key) != outputs.end()) {
key = name + std::to_string(counter);
++counter;
}

outputs.insert({key, new_tensor});
}
return outputs;
}

Napi::Value InferRequestWrap::get_output_tensors(const Napi::CallbackInfo& info) {
auto compiled_model = _infer_request.get_compiled_model().outputs();
auto output_map = get_js_infer_result(&_infer_request);
auto outputs_obj = Napi::Object::New(info.Env());

for (auto& node : compiled_model) {
auto tensor = _infer_request.get_tensor(node);
auto new_tensor = ov::Tensor(tensor.get_element_type(), tensor.get_shape());
tensor.copy_to(new_tensor);
outputs_obj.Set(node.get_any_name(), TensorWrap::wrap(info.Env(), new_tensor));
for (const auto& [key, tensor] : output_map) {
outputs_obj.Set(key, TensorWrap::wrap(info.Env(), tensor));
}
return outputs_obj;
}
Expand Down Expand Up @@ -208,17 +226,8 @@ void performInferenceThread(TsfnContext* context) {
}
context->_ir->infer();

auto compiled_model = context->_ir->get_compiled_model().outputs();
std::map<std::string, ov::Tensor> outputs;

for (auto& node : compiled_model) {
const auto& tensor = context->_ir->get_tensor(node);
auto new_tensor = ov::Tensor(tensor.get_element_type(), tensor.get_shape());
tensor.copy_to(new_tensor);
outputs.insert({node.get_any_name(), new_tensor});
}

context->result = outputs;
auto model_outputs = context->_ir->get_compiled_model().outputs();
context->result = get_js_infer_result(context->_ir);
}

auto callback = [](Napi::Env env, Napi::Function, TsfnContext* context) {
Expand Down
1 change: 1 addition & 0 deletions src/bindings/js/node/tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ if (require.main === module) {

async function main() {
await downloadTestModel(testModels.testModelFP32);
await downloadTestModel(testModels.modelV3Small);
}
43 changes: 43 additions & 0 deletions src/bindings/js/node/tests/unit/infer_request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (C) 2018-2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

const fs = require('node:fs/promises');
const { addon: ov } = require('../..');
const assert = require('assert');
const { describe, it, before, beforeEach } = require('node:test');
Expand Down Expand Up @@ -324,3 +325,45 @@ describe('ov.InferRequest tests', () => {
});
});
});

describe('ov.InferRequest tests with missing outputs names', () => {
const { modelV3Small } = testModels;
let compiledModel = null;
let tensorData = null;
let tensor = null;
let inferRequest = null;

before(async () => {
await isModelAvailable(modelV3Small);

const core = new ov.Core();

let modelData = await fs.readFile(modelV3Small.xml, 'utf8');
const weights = await fs.readFile(modelV3Small.bin);
modelData = modelData.replace(
'names="MobilenetV3/Predictions/Softmax:0"',
''
);
const model = await core.readModel(Buffer.from(modelData, 'utf8'), weights);

compiledModel = await core.compileModel(model, 'CPU');
inferRequest = compiledModel.createInferRequest();

tensorData = Float32Array.from(
{ length: lengthFromShape(modelV3Small.inputShape) },
() => Math.random() + epsilon,
);
tensor = new ov.Tensor(ov.element.f32, modelV3Small.inputShape, tensorData);
});

it('Test infer(inputData: Tensor[])', () => {
const result = inferRequest.infer([tensor]);
assert.deepStrictEqual(Object.keys(result).length, 1);
});

it('Test inferAsync(inputData: Tensor[])', () => {
inferRequest.inferAsync([tensor]).then((result) => {
assert.deepStrictEqual(Object.keys(result).length, 1);
});
});
});
10 changes: 10 additions & 0 deletions src/bindings/js/node/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ const testModels = {
binURL:
'https://media.githubusercontent.com/media/openvinotoolkit/testdata/master/models/test_model/test_model_fp32.bin',
},
modelV3Small: {
xml: getModelPath('v3-small_224_1.0_float.xml'),
bin: getModelPath('v3-small_224_1.0_float.bin'),
inputShape: [1, 224, 224, 3],
outputShape: [1, 1001],
xmlURL:
'https://storage.openvinotoolkit.org/repositories/openvino_notebooks/models/mobelinet-v3-tf/FP32/v3-small_224_1.0_float.xml',
binURL:
'https://storage.openvinotoolkit.org/repositories/openvino_notebooks/models/mobelinet-v3-tf/FP32/v3-small_224_1.0_float.bin',
},
};

module.exports = {
Expand Down
Loading