Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OV JS] Add method copyTo in tensor file #29760

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bindings/js/node/lib/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ interface Tensor {
* Reports whether the tensor is continuous or not.
*/
isContinuous(): boolean;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* Copy tensor, destination tensor should have the same element type and shape.
*/

copyTo(): boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copyTo should take one argument. Please update this definition

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
copyTo(): boolean;
copyTo(tensor: Tensor): undefined;

}

/**
Expand Down
25 changes: 24 additions & 1 deletion src/bindings/js/node/src/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see in the CI, this space is unnecessary. Please fix other problems on your own.

Suggested change
InstanceMethod("isContinuous", &TensorWrap::is_continuous),
InstanceMethod("isContinuous", &TensorWrap::is_continuous),

InstanceMethod("copyTo", &TensorWrap::copy_to)});
}

ov::Tensor TensorWrap::get_tensor() const {
Expand Down Expand Up @@ -191,3 +192,25 @@ Napi::Value TensorWrap::is_continuous(const Napi::CallbackInfo& info) {
}
return Napi::Boolean::New(env, _tensor.is_continuous());
}

Napi::Value TensorWrap::copy_to(const Napi::CallbackInfo& info) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The declaration of this method must be defined in the header file src/bindings/js/node/include/tensor.hpp. Do not forget add a doc string there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should return void instead of Boolean.

Suggested change
Napi::Value TensorWrap::copy_to(const Napi::CallbackInfo& info) {
void TensorWrap::copy_to(const Napi::CallbackInfo& info) {

Napi::Env env = info.Env();

if (info.Length() != 1 || !info[0].IsObject()) {
reportError(env, "The copyTo method requires one argument of type Tensor.");
return env.Undefined();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required to check if the tensor from the argument is an acceptable tensor (same element type and shape).


TensorWrap* target_tensor_wrap = Napi::ObjectWrap<TensorWrap>::Unwrap(info[0].As<Napi::Object>());
ov::Tensor target_tensor = target_tensor_wrap->get_tensor();

try {
_tensor.copy_to(target_tensor);
} catch (const std::exception& e) {
reportError(env, std::string("Failed to copy tensor: ") + e.what());
return env.Undefined();
}

return Napi::Boolean::New(env, true); // Successo
}

5 changes: 5 additions & 0 deletions tests/test_tensor_copyTo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not necessary. Please remove it

"test": "node --test"
}
}
15 changes: 15 additions & 0 deletions tests/test_tensor_copyTo/tensor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { Tensor } = require('../../src/bindings/js/node/lib/addon.ts');

describe('Tensor class', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this test to src/bindings/js/node/tests/unit/tensor.test.js

it('should correctly copy data to another tensor', () => {
const tensor1 = new Tensor('float32', [2, 2]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const tensor1 = new Tensor('float32', [2, 2]);
const tensor1 = new Tensor('ov.element.f32', [2, 2]);


const tensor2 = new Tensor('float32', [2, 2]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const tensor2 = new Tensor('float32', [2, 2]);
const tensor2 = new Tensor('ov.element.f32', [2, 2]);


tensor1.data = new Float32Array([1, 2, 3, 4]);

tensor1.copyTo(tensor2);

assert.deepEqual(tensor1.getData(), tensor2.getData(), 'Data was not copied correctly.');
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests with incorrect arguments:

  • incorrect count of args
  • different types
  • different shapes

Loading