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

Conversation

sanleo-wq
Copy link

@sanleo-wq sanleo-wq commented Mar 26, 2025

Details:

Added the copyTo method to the Tensor class to copy data from one tensor to another.

Added tests to verify that the copyTo method works correctly.

Fixed [any bug or error].

Tickets:

@sanleo-wq sanleo-wq requested review from a team as code owners March 26, 2025 16:15
@github-actions github-actions bot added category: JS API OpenVino JS API Bindings no-match-files labels Mar 26, 2025
@sys-openvino-ci sys-openvino-ci added the ExternalPR External contributor label Mar 26, 2025
@mlukasze mlukasze requested review from Retribution98 and almilosz and removed request for a team March 27, 2025 05:50
@mlukasze mlukasze linked an issue Mar 27, 2025 that may be closed by this pull request
@@ -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.
*/

Copy link
Contributor

@almilosz almilosz left a comment

Choose a reason for hiding this comment

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

Hi, thanks for creating this Pull Request!
Please take a look at my comments

@@ -423,6 +423,8 @@ interface Tensor {
* Reports whether the tensor is continuous or not.
*/
isContinuous(): boolean;

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;

@@ -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

@@ -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

Copy link
Contributor

@Retribution98 Retribution98 left a comment

Choose a reason for hiding this comment

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

Hello @sanleo-wq ,
Thank you for your contribution!
Please fix these comments.

@@ -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.

@@ -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.

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) {


describe('Tensor class', () => {
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]);

it('should correctly copy data to another tensor', () => {
const tensor1 = new Tensor('float32', [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]);


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

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).

@@ -423,6 +423,8 @@ interface Tensor {
* Reports whether the tensor is continuous or not.
*/
isContinuous(): boolean;

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.

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

@@ -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),

@sanleo-wq sanleo-wq closed this by deleting the head repository Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: JS API OpenVino JS API Bindings ExternalPR External contributor no-match-files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Good First Issue]: Enable Tensor.copyTo() method
4 participants