Skip to content

Commit 1f32a07

Browse files
committed
Add method copyTo in tensor file
1 parent e11eb21 commit 1f32a07

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/bindings/js/node/lib/addon.ts

+2
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ interface Tensor {
423423
* Reports whether the tensor is continuous or not.
424424
*/
425425
isContinuous(): boolean;
426+
427+
copyTo(): boolean;
426428
}
427429

428430
/**

src/bindings/js/node/src/tensor.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Napi::Function TensorWrap::get_class(Napi::Env env) {
4444
InstanceMethod("getShape", &TensorWrap::get_shape),
4545
InstanceMethod("getElementType", &TensorWrap::get_element_type),
4646
InstanceMethod("getSize", &TensorWrap::get_size),
47-
InstanceMethod("isContinuous", &TensorWrap::is_continuous)});
47+
InstanceMethod("isContinuous", &TensorWrap::is_continuous),
48+
InstanceMethod("copyTo", &TensorWrap::copy_to)});
4849
}
4950

5051
ov::Tensor TensorWrap::get_tensor() const {
@@ -191,3 +192,25 @@ Napi::Value TensorWrap::is_continuous(const Napi::CallbackInfo& info) {
191192
}
192193
return Napi::Boolean::New(env, _tensor.is_continuous());
193194
}
195+
196+
Napi::Value TensorWrap::copy_to(const Napi::CallbackInfo& info) {
197+
Napi::Env env = info.Env();
198+
199+
if (info.Length() != 1 || !info[0].IsObject()) {
200+
reportError(env, "The copyTo method requires one argument of type Tensor.");
201+
return env.Undefined();
202+
}
203+
204+
TensorWrap* target_tensor_wrap = Napi::ObjectWrap<TensorWrap>::Unwrap(info[0].As<Napi::Object>());
205+
ov::Tensor target_tensor = target_tensor_wrap->get_tensor();
206+
207+
try {
208+
_tensor.copy_to(target_tensor);
209+
} catch (const std::exception& e) {
210+
reportError(env, std::string("Failed to copy tensor: ") + e.what());
211+
return env.Undefined();
212+
}
213+
214+
return Napi::Boolean::New(env, true); // Successo
215+
}
216+

tests/test_tensor_copyTo/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"scripts": {
3+
"test": "node --test"
4+
}
5+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { Tensor } = require('../../src/bindings/js/node/lib/addon.ts');
2+
3+
describe('Tensor class', () => {
4+
it('should correctly copy data to another tensor', () => {
5+
const tensor1 = new Tensor('float32', [2, 2]);
6+
7+
const tensor2 = new Tensor('float32', [2, 2]);
8+
9+
tensor1.data = new Float32Array([1, 2, 3, 4]);
10+
11+
tensor1.copyTo(tensor2);
12+
13+
assert.deepEqual(tensor1.getData(), tensor2.getData(), 'Data was not copied correctly.');
14+
});
15+
});

0 commit comments

Comments
 (0)