Skip to content

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

[OV JS] Expose core.query_model to Node.js Api (#25966)

Nashez Zubair60121df96c27
diff --git a/src/bindings/js/node/include/core_wrap.hpp b/src/bindings/js/node/include/core_wrap.hpp
index 29149aaf157..556857ea362 100644
--- a/src/bindings/js/node/include/core_wrap.hpp
+++ b/src/bindings/js/node/include/core_wrap.hpp
@@ -68,6 +68,8 @@ public:
Napi::Value set_property(const Napi::CallbackInfo& info);
Napi::Value get_property(const Napi::CallbackInfo& info);
+ Napi::Value query_model(const Napi::CallbackInfo& info);
+
void add_extension(const Napi::CallbackInfo& info);
protected:
Napi::Value compile_model_sync(const Napi::CallbackInfo& info,
diff --git a/src/bindings/js/node/lib/addon.ts b/src/bindings/js/node/lib/addon.ts
index 9ea694c154a..c2ba1d9c61f 100644
--- a/src/bindings/js/node/lib/addon.ts
+++ b/src/bindings/js/node/lib/addon.ts
@@ -205,6 +205,19 @@ interface Core {
deviceName: string,
properties: { [key: string]: string | number | boolean },
): void;
+ /**
+ * It queries the device if it supports specified model with the specified
+ * properties.
+ * @param model The passed model to query the property.
+ * @param deviceName The name of a device.
+ * @param properties An object with the property name - property value pairs.
+ * An object with the key-value pairs. (property name, property value).
+ */
+ queryModel(
+ model: Model,
+ deviceName: string,
+ properties?: {[key: string]: string | number | boolean},
+ ): {[key: string]: string | number | boolean};
}
interface CoreConstructor {
new(): Core;
diff --git a/src/bindings/js/node/src/core_wrap.cpp b/src/bindings/js/node/src/core_wrap.cpp
index 20422b7d683..12a916b6e77 100644
--- a/src/bindings/js/node/src/core_wrap.cpp
+++ b/src/bindings/js/node/src/core_wrap.cpp
@@ -57,6 +57,7 @@ Napi::Function CoreWrap::get_class(Napi::Env env) {
InstanceMethod("getVersions", &CoreWrap::get_versions),
InstanceMethod("setProperty", &CoreWrap::set_property),
InstanceMethod("getProperty", &CoreWrap::get_property),
+ InstanceMethod("queryModel", &CoreWrap::query_model),
InstanceMethod("addExtension", &CoreWrap::add_extension)});
}
@@ -463,3 +464,29 @@ void CoreWrap::add_extension(const Napi::CallbackInfo& info) {
reportError(info.Env(), err.what());
}
}
+
+Napi::Value CoreWrap::query_model(const Napi::CallbackInfo& info) {
+ std::vector<std::string> allowed_signatures;
+ try {
+ if (ov::js::validate<ModelWrap, Napi::String>(info, allowed_signatures) ||
+ ov::js::validate<ModelWrap, Napi::String, Napi::Object>(info, allowed_signatures)) {
+ ov::AnyMap properties;
+ auto model = Napi::ObjectWrap<ModelWrap>::Unwrap(info[0].ToObject())->get_model();
+ auto device_name = info[1].ToString();
+ if (info.Length() == 3) {
+ properties = to_anyMap(info.Env(), info[2]);
+ }
+ auto query_result = _core.query_model(model, device_name, properties);
+ Napi::Object result = Napi::Object::New(info.Env());
+ for (const auto& elem : query_result) {
+ result.Set(elem.first, elem.second);
+ }
+ return result;
+ } else {
+ OPENVINO_THROW("'queryModel'", ov::js::get_parameters_error_msg(info, allowed_signatures));
+ }
+ } catch (std::exception& err) {
+ reportError(info.Env(), err.what());
+ return info.Env().Undefined();
+ }
+}
diff --git a/src/bindings/js/node/tests/unit/core.test.js b/src/bindings/js/node/tests/unit/core.test.js
index bec05980c18..fac64e48927 100644
--- a/src/bindings/js/node/tests/unit/core.test.js
+++ b/src/bindings/js/node/tests/unit/core.test.js
@@ -4,15 +4,19 @@
const { addon: ov } = require('../..');
const assert = require('assert');
-const { describe, it, beforeEach } = require('node:test');
+const { describe, it, before, beforeEach } = require('node:test');
+const { testModels, isModelAvailable, getModelPath } = require('./utils.js');
describe('ov.Core tests', () => {
-
let core = null;
+ before(async () => {
+ await isModelAvailable(testModels.testModelFP32);
+ });
+
beforeEach(() => {
core = new ov.Core();
});
-
+
it('Core.setProperty()', () => {
const tmpDir = '/tmp';
@@ -72,4 +76,33 @@ describe('ov.Core tests', () => {
/Cannot load library 'not_exists'/
);
});
+
+ it('Core.queryModel() with empty parameters should throw an error', () => {
+ assert.throws(
+ () => core.queryModel().then(),
+ /'queryModel' method called with incorrect parameters./
+ )
+ });
+
+ it('Core.queryModel() with less arguments should throw an error', () => {
+ assert.throws(
+ () => core.queryModel("Unexpected Argument").then(),
+ /'queryModel' method called with incorrect parameters./
+ )
+ });
+
+ it('Core.queryModel() with incorrect arguments should throw an error', () => {
+ const model = core.readModelSync(getModelPath().xml);
+ assert.throws(
+ () => core.queryModel(model, "arg1", "arg2").then(),
+ /'queryModel' method called with incorrect parameters./
+ )
+ });
+
+ it('Core.queryModel() should have device in the result values', () => {
+ const model = core.readModelSync(getModelPath().xml);
+ const device = 'CPU';
+ const query_model = core.queryModel(model, device);
+ assert(Object.values(query_model).includes(device));
+ });
});