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

[OV JS] Enable CompiledModel set/get property() #25808

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 16 additions & 0 deletions src/bindings/js/node/include/compiled_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ class CompiledModelWrap : public Napi::ObjectWrap<CompiledModelWrap> {
/** @brief Exports the compiled model to bytes/output stream. */
Napi::Value export_model(const Napi::CallbackInfo& info);

/**
* @brief Sets properties for current compiled model.
* @param info Contains information about the environment and passed arguments,
* this method accepts only one argument of type object.
* @return Napi::Undefined
*/
Napi::Value set_property(const Napi::CallbackInfo& info);

/**
* @brief Gets property for current compiled model.
* @param info Contains information about the environment and passed arguments,
* this method accepts only one argument of type string.
* @return A Napi::Value
*/
Napi::Value get_property(const Napi::CallbackInfo& info);

private:
/** @brief Gets node of a compiled model specified in CallbackInfo. */
Napi::Value get_node(const Napi::CallbackInfo& info,
Expand Down
15 changes: 14 additions & 1 deletion src/bindings/js/node/lib/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ interface CompiledModel {
inputs: Output[];
/** It gets all outputs of a compiled model. */
outputs: Output[];
/**
* It gets property for current compiled model.
hub-bla marked this conversation as resolved.
Show resolved Hide resolved
* @param propertyName A string to get property value.
* @returns property value.
hub-bla marked this conversation as resolved.
Show resolved Hide resolved
*/
getProperty(propertyName: string): string | number | boolean;
/**
* It creates an inference request object used to infer the compiled model.
* @return {InferRequest}
Expand Down Expand Up @@ -349,7 +355,14 @@ interface CompiledModel {
* @returns {Output} A compiled model input.
*/
input(name: string): Output;

/**
* It sets properties for current compiled model. Properties
hub-bla marked this conversation as resolved.
Show resolved Hide resolved
* can be retrieved via {@link CompiledModel.getProperty}.
* @param property The object to set properties for current compiled model.
hub-bla marked this conversation as resolved.
Show resolved Hide resolved
*/
setProperty(property: {
[propertyName: string]: string | number | boolean
hub-bla marked this conversation as resolved.
Show resolved Hide resolved
}): void;
}

/**
Expand Down
35 changes: 34 additions & 1 deletion src/bindings/js/node/src/compiled_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "node/include/addon.hpp"
#include "node/include/errors.hpp"
#include "node/include/helper.hpp"
#include "node/include/infer_request.hpp"
#include "node/include/node_output.hpp"

Expand All @@ -20,7 +21,9 @@ Napi::Function CompiledModelWrap::get_class(Napi::Env env) {
InstanceAccessor<&CompiledModelWrap::get_inputs>("inputs"),
InstanceMethod("output", &CompiledModelWrap::get_output),
InstanceAccessor<&CompiledModelWrap::get_outputs>("outputs"),
InstanceMethod("exportModelSync", &CompiledModelWrap::export_model)});
InstanceMethod("exportModelSync", &CompiledModelWrap::export_model),
InstanceMethod("setProperty", &CompiledModelWrap::set_property),
InstanceMethod("getProperty", &CompiledModelWrap::get_property)});
}

Napi::Object CompiledModelWrap::wrap(Napi::Env env, ov::CompiledModel compiled_model) {
Expand Down Expand Up @@ -122,3 +125,33 @@ Napi::Value CompiledModelWrap::export_model(const Napi::CallbackInfo& info) {
const auto& exported = _stream.str();
return Napi::Buffer<const char>::Copy(info.Env(), exported.c_str(), exported.size());
}

Napi::Value CompiledModelWrap::set_property(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
try{
if (info.Length() != 1 || !info[0].IsObject()) {
OPENVINO_THROW("Expected a single object argument for setting properties");
hub-bla marked this conversation as resolved.
Show resolved Hide resolved
}
const auto properties = to_anyMap(env, info[0]);
_compiled_model.set_property(properties);
} catch (const std::exception& e) {
reportError(env, e.what());
hub-bla marked this conversation as resolved.
Show resolved Hide resolved
}
return env.Undefined();
}

Napi::Value CompiledModelWrap::get_property(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
try {
if (info.Length() != 1 || !info[0].IsString()) {
hub-bla marked this conversation as resolved.
Show resolved Hide resolved
reportError(env, "Expected a single string argument to retrieve property value");
return env.Undefined();
}
const auto property_name = info[0].As<Napi::String>().Utf8Value();
const auto property = _compiled_model.get_property(property_name);
return any_to_js(info, property);
} catch (const std::exception& e) {
reportError(env, e.what());
}
return env.Undefined();
}
78 changes: 78 additions & 0 deletions src/bindings/js/node/tests/unit/compiled_model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// -*- coding: utf-8 -*-
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

const { addon: ov } = require('../..');
const assert = require('assert');
const { describe, it } = require('node:test');
const { getModelPath } = require('./utils.js');

const testXml = getModelPath().xml;
const core = new ov.Core();
const properties = {
"AUTO_BATCH_TIMEOUT": '1'
};
const compiledModel = core.compileModelSync(testXml, 'BATCH:CPU', properties);

describe('setProperty() / getProperty()', () => {

describe('getProperty()', () => {
it('returns the value of property from compiled model', () => {
assert.strictEqual(compiledModel.getProperty('AUTO_BATCH_TIMEOUT'), '1');
});
it('throws an error when called without arguments', () => {
assert.throws(
() => compiledModel.getProperty(),
/Expected a single string argument to retrieve property value/
hub-bla marked this conversation as resolved.
Show resolved Hide resolved
);
});
it('throws an error when called with property name that does not exists', ()=>{
assert.throws(
() => compiledModel.getProperty('PROPERTY_THAT_DOES_NOT_EXIST')
);
});
});

describe('setProperty()', () => {
it('sets a properties for compiled model', () => {
properties["AUTO_BATCH_TIMEOUT"] = '1000';
assert.doesNotThrow(() => compiledModel.setProperty(properties));
});

it('throws an error when called without an object argument', () => {
assert.throws(
() => compiledModel.setProperty(),
/Expected a single object argument for setting properties/
);
});
it('throws an error when called with wrong argument', () => {
assert.throws(
() => compiledModel.setProperty(123),
/Expected a single object argument for setting properties/
);
});

it('throws an error when called with multiple arguments', () => {
assert.throws(
() => compiledModel.setProperty({"PERFORMANCE_HINT": "THROUGHPUT"}, {"NUM_STREAMS": "AUTO"}),
/Expected a single object argument for setting properties/
);
});

it('returns the set property of the compiled model', () => {
properties["AUTO_BATCH_TIMEOUT"] = '123';
compiledModel.setProperty(properties);
assert.strictEqual(compiledModel.getProperty('AUTO_BATCH_TIMEOUT'), 123);
});

it('retains the last set property when set multiple times', () => {
compiledModel.setProperty({"AUTO_BATCH_TIMEOUT": '321'});
compiledModel.setProperty({'AUTO_BATCH_TIMEOUT': '132'});
assert.strictEqual(compiledModel.getProperty('AUTO_BATCH_TIMEOUT'), 132);
});

it('allows to pass empty object', () => {
assert.doesNotThrow(() => compiledModel.setProperty({}));
});
});
});
Loading