-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
@@ -423,6 +423,8 @@ interface Tensor { | |||
* Reports whether the tensor is continuous or not. | |||
*/ | |||
isContinuous(): boolean; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** | |
* Copy tensor, destination tensor should have the same element type and shape. | |
*/ |
There was a problem hiding this 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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copyTo(): boolean; | |
copyTo(tensor: Tensor): undefined; |
@@ -0,0 +1,5 @@ | |||
{ | |||
"scripts": { |
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
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]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.'); | ||
}); | ||
}); |
There was a problem hiding this comment.
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(); | ||
} |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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), |
There was a problem hiding this comment.
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.
InstanceMethod("isContinuous", &TensorWrap::is_continuous), | |
InstanceMethod("isContinuous", &TensorWrap::is_continuous), |
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: