Skip to content

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

[OV JS]: Expose ov.saveModel() functionality (#27148)

Hubert Błaszczyk58672c3c8ac7
diff --git a/src/bindings/js/node/include/addon.hpp b/src/bindings/js/node/include/addon.hpp
index ee4864876ea..ca963e971b5 100644
--- a/src/bindings/js/node/include/addon.hpp
+++ b/src/bindings/js/node/include/addon.hpp
@@ -26,4 +26,19 @@ void init_class(Napi::Env env,
Prototype func,
Napi::FunctionReference& reference);
+template <typename Callable>
+void init_function(Napi::Env env,
+ Napi::Object exports,
+ std::string func_name,
+ Callable func) {
+ const auto& napi_func = Napi::Function::New(env, func, func_name);
+
+ exports.Set(func_name, napi_func);
+}
+
Napi::Object init_module(Napi::Env env, Napi::Object exports);
+
+/**
+ * @brief Saves model in a specified path.
+*/
+Napi::Value save_model_sync(const Napi::CallbackInfo& info);
diff --git a/src/bindings/js/node/include/type_validation.hpp b/src/bindings/js/node/include/type_validation.hpp
index 4d5e4d3ba98..3633134c267 100644
--- a/src/bindings/js/node/include/type_validation.hpp
+++ b/src/bindings/js/node/include/type_validation.hpp
@@ -29,6 +29,9 @@ const char* get_attr_type<Napi::String>();
template <>
const char* get_attr_type<Napi::Object>();
+template <>
+const char* get_attr_type<Napi::Boolean>();
+
template <>
const char* get_attr_type<Napi::Buffer<uint8_t>>();
@@ -52,6 +55,9 @@ bool validate_value<Napi::String>(const Napi::Env& env, const Napi::Value& value
template <>
bool validate_value<Napi::Object>(const Napi::Env& env, const Napi::Value& value);
+template <>
+bool validate_value<Napi::Boolean>(const Napi::Env& env, const Napi::Value& value);
+
template <>
bool validate_value<Napi::Buffer<uint8_t>>(const Napi::Env& env, const Napi::Value& value);
diff --git a/src/bindings/js/node/lib/addon.ts b/src/bindings/js/node/lib/addon.ts
index 24c9d780aa9..6e57e10c417 100644
--- a/src/bindings/js/node/lib/addon.ts
+++ b/src/bindings/js/node/lib/addon.ts
@@ -675,6 +675,22 @@ export interface NodeAddon {
resizeAlgorithm: typeof resizeAlgorithm;
PrePostProcessor: PrePostProcessorConstructor;
};
+
+ /**
+ * It saves a model into IR files (xml and bin).
+ * Floating point weights are compressed to FP16 by default.
+ * This method saves a model to IR applying all necessary transformations
+ * that usually applied in model conversion flow provided by mo tool.
+ * Particularly, floating point weights are compressed to FP16,
+ * debug information in model nodes are cleaned up, etc.
+ * @param model The model which will be
+ * converted to IR representation and saved.
+ * @param path The path for saving the model.
+ * @param compressToFp16 Whether to compress
+ * floating point weights to FP16. Default is set to `true`.
+ */
+ saveModelSync(model: Model, path: string, compressToFp16?: boolean): void;
+
element: typeof element;
}
diff --git a/src/bindings/js/node/src/addon.cpp b/src/bindings/js/node/src/addon.cpp
index 4d43c8f27e3..ac3e3170ba6 100644
--- a/src/bindings/js/node/src/addon.cpp
+++ b/src/bindings/js/node/src/addon.cpp
@@ -8,12 +8,15 @@
#include "node/include/compiled_model.hpp"
#include "node/include/core_wrap.hpp"
#include "node/include/element_type.hpp"
+#include "node/include/errors.hpp"
+#include "node/include/helper.hpp"
#include "node/include/infer_request.hpp"
#include "node/include/model_wrap.hpp"
#include "node/include/node_output.hpp"
#include "node/include/partial_shape_wrap.hpp"
#include "node/include/preprocess/preprocess.hpp"
#include "node/include/tensor.hpp"
+#include "node/include/type_validation.hpp"
#include "openvino/openvino.hpp"
void init_class(Napi::Env env,
@@ -27,6 +30,30 @@ void init_class(Napi::Env env,
exports.Set(class_name, prototype);
}
+Napi::Value save_model_sync(const Napi::CallbackInfo& info) {
+ std::vector<std::string> allowed_signatures;
+ try {
+ if (ov::js::validate<ModelWrap, Napi::String>(info, allowed_signatures)) {
+ const auto& model = info[0].ToObject();
+ const auto m = Napi::ObjectWrap<ModelWrap>::Unwrap(model);
+ const auto path = js_to_cpp<std::string>(info, 1);
+ ov::save_model(m->get_model(), path);
+ } else if (ov::js::validate<ModelWrap, Napi::String, Napi::Boolean>(info, allowed_signatures)) {
+ const auto& model = info[0].ToObject();
+ const auto m = Napi::ObjectWrap<ModelWrap>::Unwrap(model);
+ const auto path = js_to_cpp<std::string>(info, 1);
+ const auto compress_to_fp16 = info[2].ToBoolean();
+ ov::save_model(m->get_model(), path, compress_to_fp16);
+ } else {
+ OPENVINO_THROW("'saveModelSync'", ov::js::get_parameters_error_msg(info, allowed_signatures));
+ }
+ } catch (const std::exception& e) {
+ reportError(info.Env(), e.what());
+ }
+
+ return info.Env().Undefined();
+}
+
/** @brief Initialize native add-on */
Napi::Object init_module(Napi::Env env, Napi::Object exports) {
auto addon_data = new AddonData();
@@ -41,6 +68,8 @@ Napi::Object init_module(Napi::Env env, Napi::Object exports) {
init_class(env, exports, "ConstOutput", &Output<const ov::Node>::get_class, addon_data->const_output);
init_class(env, exports, "PartialShape", &PartialShapeWrap::get_class, addon_data->partial_shape);
+ init_function(env, exports, "saveModelSync", save_model_sync);
+
preprocess::init(env, exports);
element::init(env, exports);
diff --git a/src/bindings/js/node/src/type_validation.cpp b/src/bindings/js/node/src/type_validation.cpp
index 4852e0e4051..3d1ba5f7a0f 100644
--- a/src/bindings/js/node/src/type_validation.cpp
+++ b/src/bindings/js/node/src/type_validation.cpp
@@ -85,6 +85,11 @@ const char* get_attr_type<Napi::Object>() {
return NapiArg::get_type_name(napi_object);
}
+template <>
+const char* get_attr_type<Napi::Boolean>() {
+ return NapiArg::get_type_name(napi_boolean);
+}
+
template <>
const char* get_attr_type<Napi::Buffer<uint8_t>>() {
return BindingTypename::BUFFER;
@@ -115,6 +120,11 @@ bool validate_value<Napi::Object>(const Napi::Env& env, const Napi::Value& value
return napi_object == value.Type();
}
+template <>
+bool validate_value<Napi::Boolean>(const Napi::Env& env, const Napi::Value& value) {
+ return napi_boolean == value.Type();
+}
+
template <>
bool validate_value<Napi::Buffer<uint8_t>>(const Napi::Env& env, const Napi::Value& value) {
return value.IsBuffer();
diff --git a/src/bindings/js/node/tests/unit/basic.test.js b/src/bindings/js/node/tests/unit/basic.test.js
index 0717b430ae0..6b27b1fcdbd 100644
--- a/src/bindings/js/node/tests/unit/basic.test.js
+++ b/src/bindings/js/node/tests/unit/basic.test.js
@@ -4,8 +4,17 @@
const { addon: ov } = require('../..');
const assert = require('assert');
-const { describe, it, before, beforeEach } = require('node:test');
-const { testModels, getModelPath, isModelAvailable } = require('./utils.js');
+const fs = require('fs');
+const os = require('os');
+const path = require('path');
+const { after, describe, it, before, beforeEach } = require('node:test');
+const {
+ testModels,
+ compareModels,
+ getModelPath,
+ isModelAvailable,
+ sleep,
+} = require('./utils.js');
const epsilon = 0.5;
describe('ov basic tests.', () => {
@@ -14,8 +23,10 @@ describe('ov basic tests.', () => {
let model = null;
let compiledModel = null;
let modelLike = null;
+ let outDir = null;
before(async () => {
+ outDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'ov_js_out_'));
await isModelAvailable(testModels.testModelFP32);
testXml = getModelPath().xml;
});
@@ -27,12 +38,69 @@ describe('ov basic tests.', () => {
modelLike = [model, compiledModel];
});
+ after(async () => {
+ // Wait to ensure the model file is released
+ await sleep(1);
+ await fs.promises.rm(outDir, { recursive: true });
+ });
+
it('Core.getAvailableDevices()', () => {
const devices = core.getAvailableDevices();
assert.ok(devices.includes('CPU'));
});
+ describe('ov.saveModelSync()', () => {
+ it('saveModelSync(model, path, compressToFp16=true)', () => {
+ const xmlPath = path.join(outDir, `${model.getName()}_fp16.xml`);
+ assert.doesNotThrow(() => ov.saveModelSync(model, xmlPath, true));
+
+ const savedModel = core.readModelSync(xmlPath);
+ assert.doesNotThrow(() => compareModels(model, savedModel));
+ });
+
+ it('saveModelSync(model, path, compressToFp16)', () => {
+ const xmlPath = path.join(outDir, `${model.getName()}_fp32.xml`);
+ assert.doesNotThrow(() => ov.saveModelSync(model, xmlPath));
+
+ const savedModel = core.readModelSync(xmlPath);
+ assert.doesNotThrow(() => compareModels(model, savedModel));
+ });
+ it('saveModelSync(model, path, compressToFp16=false)', () => {
+ const xmlPath = path.join(outDir, `${model.getName()}_fp32.xml`);
+ assert.doesNotThrow(() => ov.saveModelSync(model, xmlPath, false));
+
+ const savedModel = core.readModelSync(xmlPath);
+ assert.doesNotThrow(() => compareModels(model, savedModel));
+ });
+
+ it('saveModelSync(model) throws', () => {
+ const expectedMsg = (
+ '\'saveModelSync\' method called with incorrect parameters.\n' +
+ 'Provided signature: (object) \n' +
+ 'Allowed signatures:\n' +
+ '- (Model, string)\n' +
+ '- (Model, string, boolean)\n'
+ ).replace(/[()]/g, '\\$&');
+
+ assert.throws(
+ () => ov.saveModelSync(model),
+ new RegExp(expectedMsg));
+ });
+
+ it('saveModelSync(model, path) throws with incorrect path', () => {
+ const expectedMsg = (
+ 'Path for xml file doesn\'t ' +
+ 'contains file name with \'xml\' extension'
+ ).replace(/[()]/g, '\\$&');
+
+ const noXmlPath = `${outDir}${model.getName()}_fp32`;
+ assert.throws(
+ () => ov.saveModelSync(model, noXmlPath),
+ new RegExp(expectedMsg));
+ });
+ });
+
describe('Core.getVersions()', () => {
it('getVersions(validDeviceName: string)', () => {
const deviceVersion = core.getVersions('CPU');
diff --git a/src/bindings/js/node/tests/unit/utils.js b/src/bindings/js/node/tests/unit/utils.js
index 456f87983db..bef8ea285c4 100644
--- a/src/bindings/js/node/tests/unit/utils.js
+++ b/src/bindings/js/node/tests/unit/utils.js
@@ -22,12 +22,43 @@ const testModels = {
};
module.exports = {
+ compareModels,
+ sleep,
getModelPath,
downloadTestModel,
isModelAvailable,
testModels,
};
+function compareModels(model1, model2) {
+ const differences = [];
+ if (model1.getFriendlyName() !== model2.getFriendlyName()) {
+ differences.push('Friendly names of models are not equal ' +
+ `model_one: ${model1.getFriendlyName()},` +
+ `model_two: ${model2.getFriendlyName()}`);
+ }
+
+ if (model1.inputs.length !== model2.inputs.length) {
+ differences.push('Number of models\' inputs are not equal ' +
+ `model_one: ${model1.inputs.length}, ` +
+ `model_two: ${model2.inputs.length}`);
+ }
+
+ if (model1.outputs.length !== model2.outputs.length) {
+ differences.push('Number of models\' outputs are not equal ' +
+ `model_one: ${model1.outputs.length}, ` +
+ `model_two: ${model2.outputs.length}`);
+ }
+
+ if (differences.length) {
+ throw new Error(differences.join('\n'));
+ }
+}
+
+function sleep(ms) {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
+
function getModelPath(isFP16 = false) {
const modelName = `test_model_fp${isFP16 ? 16 : 32}`;