Skip to content

Conversation

Sujanian1304
Copy link
Contributor

Details:

Implements ov::Tensor::copy_to() method in JavaScript bindings.

  • Added copyTo() method in C++ (tensor.cpp)
  • Added method declaration in header (tensor.hpp)
  • Updated TypeScript definitions (addon.ts)
  • Added comprehensive unit tests

@Sujanian1304 Sujanian1304 requested a review from a team as a code owner October 9, 2025 02:31
@github-actions github-actions bot added the category: JS API OpenVino JS API Bindings label Oct 9, 2025
@sys-openvino-ci sys-openvino-ci added the ExternalPR External contributor label Oct 9, 2025
@Sujanian1304
Copy link
Contributor Author

hi @almilosz @Retribution98 @mlukasze ,I have submitted this pull request to expose the Tensor.copyTo() method.please let me know your feedback on my PR , Thanks.

Best Regards
Piyush

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.

Hello, I left some comments

Comment on lines +191 to +199
if (info.Length() != 1) {
reportError(env, "copyTo() must receive one argument, which is the destination Tensor.");
return env.Undefined();
}

if (!info[0].IsObject()) {
reportError(env, "Invalid argument");
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.

Please use OPENVINO_ASSERT. It's used in "newer" parts of js api

}

try {
auto dst_tensor_wrap = Napi::ObjectWrap<TensorWrap>::Unwrap(info[0].As<Napi::Object>());
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use

ov::Tensor cast_to_tensor(const Napi::CallbackInfo& info, int index) {


} catch (const std::exception& e) {
reportError(env, e.what());
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.

Suggested change
return env.Undefined();

I think this one is not needed

});

describe("Tensor.copyTo()", () => {
test("should copy data from source tensor to destination tensor with f32 type", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please parametrize tests for different data types

Comment on lines +362 to +383
test("should throw error for incompatible element types", () => {
const sourceData = new Float32Array([1.0, 2.0, 3.0, 4.0]);
const sourceTensor = new ov.Tensor(ov.element.f32, [2, 2], sourceData);
const destTensor = new ov.Tensor(ov.element.i32, [2, 2]);

assert.throws(() => {
sourceTensor.copyTo(destTensor);
});
});

test("should copy when shapes differ but element count is same", () => {
const sourceData = new Float32Array([1.0, 2.0, 3.0, 4.0]);
const sourceTensor = new ov.Tensor(ov.element.f32, [2, 2], sourceData);
const destTensor = new ov.Tensor(ov.element.f32, [4, 1]);

assert.doesNotThrow(() => {
sourceTensor.copyTo(destTensor);
});

const destData = destTensor.getData();
assert.deepStrictEqual(Array.from(destData), Array.from(sourceData));
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's skip those, they are not directly related to binding.

Comment on lines +399 to +406
test("should handle empty tensors", () => {
const sourceTensor = new ov.Tensor(ov.element.f32, [0]);
const destTensor = new ov.Tensor(ov.element.f32, [0]);

assert.doesNotThrow(() => {
sourceTensor.copyTo(destTensor);
});
});
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
test("should handle empty tensors", () => {
const sourceTensor = new ov.Tensor(ov.element.f32, [0]);
const destTensor = new ov.Tensor(ov.element.f32, [0]);
assert.doesNotThrow(() => {
sourceTensor.copyTo(destTensor);
});
});

sourceTensor.data = modifiedSourceData;

const destData = destTensor.getData();
assert.deepStrictEqual(Array.from(destData), [1.0, 2.0, 3.0, 4.0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!
Please also add assert that sourceTensor contain [10.0, 20.0, 30.0, 40.0]

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants