From bd45732410b75c1f772de308f7e2a4c9e036ac3b Mon Sep 17 00:00:00 2001 From: Nashez Zubair Date: Sat, 23 Nov 2024 15:42:17 +0530 Subject: [PATCH] [OV][JS] Expose the Tensor.isContinuous to Node.js API * Add a TensorWrap::is_continuous function: Calls the underlying Tensor.isContinous function * Update the addon.ts file with the isContinuous method * Add unit tests for the isContinuous Api Closes: #27701 Signed-off-by: Nashez Zubair --- src/bindings/js/node/include/tensor.hpp | 5 +++++ src/bindings/js/node/lib/addon.ts | 4 ++++ src/bindings/js/node/package-lock.json | 6 +++--- src/bindings/js/node/src/tensor.cpp | 12 +++++++++++- src/bindings/js/node/tests/unit/tensor.test.js | 14 ++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/bindings/js/node/include/tensor.hpp b/src/bindings/js/node/include/tensor.hpp index 4b827f82860c4c..b0ca9f5d9ed0bb 100644 --- a/src/bindings/js/node/include/tensor.hpp +++ b/src/bindings/js/node/include/tensor.hpp @@ -59,6 +59,11 @@ class TensorWrap : public Napi::ObjectWrap { Napi::Value get_element_type(const Napi::CallbackInfo& info); /** @return Napi::Number containing tensor size as total number of elements. */ Napi::Value get_size(const Napi::CallbackInfo& info); + /** + * @brief Getter to check if tensor is continuous + * @return Napi::Boolean + */ + Napi::Value is_continuous(const Napi::CallbackInfo& info); private: ov::Tensor _tensor; diff --git a/src/bindings/js/node/lib/addon.ts b/src/bindings/js/node/lib/addon.ts index 6e57e10c4173b6..552fde4168def6 100644 --- a/src/bindings/js/node/lib/addon.ts +++ b/src/bindings/js/node/lib/addon.ts @@ -419,6 +419,10 @@ interface Tensor { * It gets the tensor size as a total number of elements. */ getSize(): number; + /** + * It gets the tensor is continuous or not. + */ + isContinuous(): boolean; } /** diff --git a/src/bindings/js/node/package-lock.json b/src/bindings/js/node/package-lock.json index 41a813579ad83c..b3054f3d87c629 100644 --- a/src/bindings/js/node/package-lock.json +++ b/src/bindings/js/node/package-lock.json @@ -659,9 +659,9 @@ "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" }, "node_modules/cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", "dev": true, "dependencies": { "path-key": "^3.1.0", diff --git a/src/bindings/js/node/src/tensor.cpp b/src/bindings/js/node/src/tensor.cpp index 78785a10b2275b..f648a4ca2df421 100644 --- a/src/bindings/js/node/src/tensor.cpp +++ b/src/bindings/js/node/src/tensor.cpp @@ -43,7 +43,8 @@ Napi::Function TensorWrap::get_class(Napi::Env env) { InstanceMethod("getData", &TensorWrap::get_data), InstanceMethod("getShape", &TensorWrap::get_shape), InstanceMethod("getElementType", &TensorWrap::get_element_type), - InstanceMethod("getSize", &TensorWrap::get_size)}); + InstanceMethod("getSize", &TensorWrap::get_size), + InstanceMethod("isContinuous", &TensorWrap::is_continuous)}); } ov::Tensor TensorWrap::get_tensor() const { @@ -175,3 +176,12 @@ Napi::Value TensorWrap::get_size(const Napi::CallbackInfo& info) { const auto size = static_cast(_tensor.get_size()); return Napi::Number::New(env, size); } + +Napi::Value TensorWrap::is_continuous(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + if (info.Length() > 0) { + reportError(env, "isContinuous() does not accept any arguments."); + return env.Null(); + } + return Napi::Boolean::New(env, _tensor.is_continuous()); +} diff --git a/src/bindings/js/node/tests/unit/tensor.test.js b/src/bindings/js/node/tests/unit/tensor.test.js index 656bdf9b122e58..e42c67b5928aac 100644 --- a/src/bindings/js/node/tests/unit/tensor.test.js +++ b/src/bindings/js/node/tests/unit/tensor.test.js @@ -291,4 +291,18 @@ describe('ov.Tensor tests', () => { assert.strictEqual(tensor.getSize(), expectedSize); }); }); + + describe('Tensor isContinuous', () => { + it('isContinuous returns true if tensor is continuous', () => { + const tensor = new ov.Tensor(ov.element.f32, [3, 2, 2]); + assert.strictEqual(tensor.isContinuous(), true); + }); + + it('isContinuous should throw an error if arguments are provided', () => { + const tensor = new ov.Tensor(ov.element.f32, shape, data); + assert.throws(() => tensor.isContinuous(1), { + message: 'isContinuous() does not accept any arguments.', + }); + }); + }); });