-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
/** | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 commentThe 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("copyTo", &TensorWrap::copy_to)}); | ||||||
} | ||||||
|
||||||
ov::Tensor TensorWrap::get_tensor() const { | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. The declaration of this method must be defined in the header file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should return void instead of Boolean.
Suggested change
|
||||||
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(); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"scripts": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's not necessary. Please remove it |
||
"test": "node --test" | ||
} | ||
} |
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', () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add this test to |
||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
const tensor2 = new Tensor('float32', [2, 2]); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
tensor1.data = new Float32Array([1, 2, 3, 4]); | ||||||
|
||||||
tensor1.copyTo(tensor2); | ||||||
|
||||||
assert.deepEqual(tensor1.getData(), tensor2.getData(), 'Data was not copied correctly.'); | ||||||
}); | ||||||
}); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add tests with incorrect arguments:
|
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.