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
[OV JS] Add Tensor.copyTo() method to Node.js bindings (#32340)
piyush kumar962ac987aa2a
diff --git a/src/bindings/js/node/include/tensor.hpp b/src/bindings/js/node/include/tensor.hppindex c7ed16cd4b3..a121067fd96 100644--- a/src/bindings/js/node/include/tensor.hpp+++ b/src/bindings/js/node/include/tensor.hpp@@ -75,6 +75,7 @@ public:* @return JavaScript undefined on success, or throws a JS exception on error.*/Napi::Value set_shape(const Napi::CallbackInfo& info);+ void copy_to(const Napi::CallbackInfo& info);/*** @brief Returns a pointer to the underlying ov::Tensor object for cross-addon interoperability.diff --git a/src/bindings/js/node/lib/addon.ts b/src/bindings/js/node/lib/addon.tsindex 2082493b175..e0c9ab7c030 100644--- a/src/bindings/js/node/lib/addon.ts+++ b/src/bindings/js/node/lib/addon.ts@@ -493,6 +493,12 @@ export interface Tensor {* @param shape - Array of dimensions for the new shape*/setShape(shape: number[]): void;+ /**+ * Copies data from this tensor to a destination tensor.+ * The destination tensor must have the same shape and element type.+ * @param tensor The destination tensor to which the data will be copied.+ */+ copyTo(tensor: Tensor): void;}/**diff --git a/src/bindings/js/node/src/tensor.cpp b/src/bindings/js/node/src/tensor.cppindex a5ad45fcb5d..3cb3691093c 100644--- a/src/bindings/js/node/src/tensor.cpp+++ b/src/bindings/js/node/src/tensor.cpp@@ -8,6 +8,7 @@#include "node/include/addon.hpp"#include "node/include/errors.hpp"#include "node/include/helper.hpp"+#include "node/include/type_validation.hpp"#include "openvino/core/shape.hpp"#include "openvino/core/type/element_type.hpp"@@ -58,7 +59,8 @@ Napi::Function TensorWrap::get_class(Napi::Env env) {InstanceMethod("getSize", &TensorWrap::get_size),InstanceMethod("isContinuous", &TensorWrap::is_continuous),InstanceMethod("setShape", &TensorWrap::set_shape),- InstanceMethod("__getExternalTensor", &TensorWrap::get_external_tensor)});+ InstanceMethod("__getExternalTensor", &TensorWrap::get_external_tensor),+ InstanceMethod("copyTo", &TensorWrap::copy_to)});}ov::Tensor TensorWrap::get_tensor() const {@@ -197,6 +199,19 @@ Napi::Value TensorWrap::set_shape(const Napi::CallbackInfo& info) {return info.Env().Undefined();}+void TensorWrap::copy_to(const Napi::CallbackInfo& info) {+ Napi::Env env = info.Env();+ try {+ OPENVINO_ASSERT(info.Length() == 1, "copyTo() must receive one argument, which is the destination Tensor.");+ OPENVINO_ASSERT(!info[0].IsUndefined() && !info[0].IsNull(), "The argument must be a Tensor object.");+ OPENVINO_ASSERT(ov::js::validate_value<TensorWrap>(env, info[0]), "Invalid argument");+ auto dst_tensor_wrap = Napi::ObjectWrap<TensorWrap>::Unwrap(info[0].ToObject());+ _tensor.copy_to(dst_tensor_wrap->_tensor);+ } catch (const std::exception& e) {+ reportError(env, e.what());+ }+}+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 ac889468e34..95f99c0a027 100644--- a/src/bindings/js/node/tests/unit/tensor.test.js+++ b/src/bindings/js/node/tests/unit/tensor.test.js@@ -251,6 +251,125 @@ describe("ov.Tensor tests", () => {});});+ describe("Tensor.copyTo()", () => {+ const elementTypes = [+ { type: ov.element.f32, TypedArray: Float32Array, name: "f32" },+ { type: ov.element.i32, TypedArray: Int32Array, name: "i32" },+ { type: ov.element.u8, TypedArray: Uint8Array, name: "u8" },+ { type: ov.element.i64, TypedArray: BigInt64Array, name: "i64" },+ { type: ov.element.f64, TypedArray: Float64Array, name: "f64" },+ { type: ov.element.u16, TypedArray: Uint16Array, name: "u16" },+ ];++ elementTypes.forEach(({ type, TypedArray, name }) => {+ test(`should copy data with ${name} type`, () => {+ const sourceData = new TypedArray(name === "i64" ? [1n, 2n, 3n, 4n] : [1, 2, 3, 4]);+ const sourceTensor = new ov.Tensor(type, [2, 2], sourceData);+ const destTensor = new ov.Tensor(type, [2, 2]);++ sourceTensor.copyTo(destTensor);++ const destData = destTensor.getData();+ assert.deepStrictEqual(Array.from(destData), Array.from(sourceData));+ });+ });+ const shapes = [+ { shape: [2, 3], size: 6, name: "2D" },+ { shape: [2, 2, 2], size: 8, name: "3D" },+ { shape: [4], size: 4, name: "1D" },+ { shape: [3, 4], size: 12, name: "2D (3x4)" },+ ];++ shapes.forEach(({ shape, size, name }) => {+ test(`should copy data with ${name} tensor shape`, () => {+ const sourceData = new Float32Array(size);+ for (let i = 0; i < size; i++) {+ sourceData[i] = i + 1;+ }++ const sourceTensor = new ov.Tensor(ov.element.f32, shape, sourceData);+ const destTensor = new ov.Tensor(ov.element.f32, shape);++ sourceTensor.copyTo(destTensor);++ const destData = destTensor.getData();+ assert.deepStrictEqual(Array.from(destData), Array.from(sourceData));+ });+ });++ test("should throw error when argument is missing", () => {+ const sourceTensor = new ov.Tensor(ov.element.f32, [2, 2]);++ assert.throws(+ () => {+ sourceTensor.copyTo();+ },+ {+ message: /copyTo\(\) must receive one argument, which is the destination Tensor\./,+ },+ );+ });++ test("should throw error when argument is not a Tensor", () => {+ const sourceTensor = new ov.Tensor(ov.element.f32, [2, 2]);++ assert.throws(+ () => {+ sourceTensor.copyTo({});+ },+ {+ message: /Invalid argument/,+ },+ );+ });++ test("should throw error when destination tensor is null", () => {+ const sourceTensor = new ov.Tensor(ov.element.f32, [2, 2]);++ assert.throws(+ () => {+ sourceTensor.copyTo(null);+ },+ {+ message: /The argument must be a Tensor object\./,+ },+ );+ });++ test("should verify data independence after copy", () => {+ const sourceData = new Float32Array([1.0, 2.0, 3.0, 4.0]);+ const sourceTensor = new ov.Tensor(ov.element.f32, [4], sourceData);+ const destTensor = new ov.Tensor(ov.element.f32, [4]);++ sourceTensor.copyTo(destTensor);++ const modifiedSourceData = new Float32Array([10.0, 20.0, 30.0, 40.0]);+ sourceTensor.data = modifiedSourceData;++ const destData = destTensor.getData();+ assert.deepStrictEqual(Array.from(destData), [1.0, 2.0, 3.0, 4.0]);++ const sourceDataAfterModification = sourceTensor.getData();+ assert.deepStrictEqual(Array.from(sourceDataAfterModification), [10.0, 20.0, 30.0, 40.0]);+ });++ test("should handle large tensors", () => {+ const size = 1000;+ const sourceData = new Float32Array(size);+ for (let i = 0; i < size; i++) {+ sourceData[i] = i * 0.5;+ }++ const sourceTensor = new ov.Tensor(ov.element.f32, [size], sourceData);+ const destTensor = new ov.Tensor(ov.element.f32, [size]);++ sourceTensor.copyTo(destTensor);++ const destData = destTensor.getData();+ assert.deepStrictEqual(Array.from(destData), Array.from(sourceData));+ });+ });+describe("Tensor element type", () => {it("comparisons of ov.element to string", () => {params.forEach(([elemType, val]) => {