OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
feat: [OV JS] Expose Tensor.setShape method to Node.js api (#32061)
piyush kumara146a8626261
diff --git a/src/bindings/js/node/include/tensor.hpp b/src/bindings/js/node/include/tensor.hppindex caed3a0d32b..14d3ca0b8d8 100644--- a/src/bindings/js/node/include/tensor.hpp+++ b/src/bindings/js/node/include/tensor.hpp@@ -62,8 +62,15 @@ public:/*** @brief Getter to check if tensor is continuous* @return Napi::Boolean- */+ */Napi::Value is_continuous(const Napi::CallbackInfo& info);+ /**+ * @brief Binds the C++ ov::Tensor::set_shape() method to JavaScript as Tensor.setShape().+ * @param info Callback info containing a single argument: an array of numbers+ * representing the new tensor dimensions.+ * @return JavaScript undefined on success, or throws a JS exception on error.+ */+ Napi::Value set_shape(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.tsindex 761c6718dc0..c251ddeb968 100644--- a/src/bindings/js/node/lib/addon.ts+++ b/src/bindings/js/node/lib/addon.ts@@ -469,6 +469,11 @@ interface Tensor {* Reports whether the tensor is continuous or not.*/isContinuous(): boolean;+ /**+ * Sets the shape of the tensor.+ * @param shape - Array of dimensions for the new shape+ */+ setShape(shape: number[]): void;}/**diff --git a/src/bindings/js/node/src/tensor.cpp b/src/bindings/js/node/src/tensor.cppindex fecd88d1f77..80b739d97c5 100644--- a/src/bindings/js/node/src/tensor.cpp+++ b/src/bindings/js/node/src/tensor.cpp@@ -44,7 +44,8 @@ Napi::Function TensorWrap::get_class(Napi::Env env) {InstanceMethod("getShape", &TensorWrap::get_shape),InstanceMethod("getElementType", &TensorWrap::get_element_type),InstanceMethod("getSize", &TensorWrap::get_size),- InstanceMethod("isContinuous", &TensorWrap::is_continuous)});+ InstanceMethod("isContinuous", &TensorWrap::is_continuous),+ InstanceMethod("setShape", &TensorWrap::set_shape)});}ov::Tensor TensorWrap::get_tensor() const {@@ -168,6 +169,20 @@ Napi::Value TensorWrap::get_shape(const Napi::CallbackInfo& info) {}return cpp_to_js<ov::Shape, Napi::Array>(info, _tensor.get_shape());}+Napi::Value TensorWrap::set_shape(const Napi::CallbackInfo& info) {+ if (info.Length() == 1) {+ try {+ const auto& shape = js_to_cpp<ov::Shape>(info, 0);+ _tensor.set_shape(shape);+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ }+ } else {+ reportError(info.Env(), "Error in setShape(). Wrong number of parameters.");+ }++ return info.Env().Undefined();+}Napi::Value TensorWrap::get_element_type(const Napi::CallbackInfo& info) {return cpp_to_js<ov::element::Type_t, Napi::String>(info, _tensor.get_element_type());diff --git a/src/bindings/js/node/tests/unit/tensor.test.js b/src/bindings/js/node/tests/unit/tensor.test.jsindex 92bcd796348..ead65fc1984 100644--- a/src/bindings/js/node/tests/unit/tensor.test.js+++ b/src/bindings/js/node/tests/unit/tensor.test.js@@ -197,6 +197,60 @@ describe("ov.Tensor tests", () => {});});+ describe("Tensor setShape", () => {+ it("reshapes tensor when element count stays the same for all types", () => {+ params.forEach(([type, , data]) => {+ if (type === ov.element.string) return;++ const initialShape = [2, 4];+ const totalElements = 8;+ const slicedData = data.slice(0, totalElements);+ const tensor = new ov.Tensor(type, initialShape, slicedData);++ tensor.setShape([totalElements]);+ assert.deepStrictEqual(tensor.getShape(), [totalElements]);++ tensor.setShape([4, 2]);+ assert.deepStrictEqual(tensor.getShape(), [4, 2]);+ });+ });++ it("throws if the new shape changes the element count for all types", () => {+ params.forEach(([type, , data]) => {+ if (type === ov.element.string) return;++ const initialShape = [2, 4];+ const totalElements = 8;+ const slicedData = data.slice(0, totalElements);+ const tensor = new ov.Tensor(type, initialShape, slicedData);++ assert.throws(() => tensor.setShape([totalElements + 1]), {+ name: "Error",+ message: /Could set new shape/i,+ });+ });+ });++ it("throws if no arguments are provided", () => {+ const tensor = new ov.Tensor(ov.element.f32, [1]);+ assert.throws(() => tensor.setShape(), {+ message: /Wrong number of parameters/i,+ });+ });++ it("throws for invalid shape contents", () => {+ const tensor = new ov.Tensor(ov.element.f32, [1, 6]);++ assert.throws(() => tensor.setShape("not an array"), {+ message: /must be of type Array or TypedArray/i,+ });++ assert.throws(() => tensor.setShape([2, -3]), {+ message: /Cannot allocate memory/i,+ });+ });+ });+describe("Tensor element type", () => {it("comparisons of ov.element to string", () => {params.forEach(([elemType, val]) => {