-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Added Tensor.get_size() method to Node.js API #23498
Added Tensor.get_size() method to Node.js API #23498
Conversation
hello @vishniakov-nikolai , @almilosz can you please review ? let me know if any changes are required. |
@@ -51,6 +51,9 @@ class TensorWrap : public Napi::ObjectWrap<TensorWrap> { | |||
Napi::Value get_shape(const Napi::CallbackInfo& info); | |||
/** @return Napi::String containing ov::element type. */ | |||
Napi::Value get_element_type(const Napi::CallbackInfo& info); | |||
/**@return Napi::Number containing tensor size*/ |
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.
/**@return Napi::Number containing tensor size*/ | |
/**@return Napi::Number containing tensor size as total number of elements.*/ |
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.
@almilosz Thanks for the feedback , I have made the requested changes , can you take a look ?
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 TensorWrap::get_size()
method has to be hooked up to the accessor when defining a class in the TensorWrap::get_class()
.
@almilosz thanks for the feedback , sorry it was a mistake from my end , actually I complied and tested the changes in a seperate local repo , but while making changes for PR in a newly cloned repo , I missed to add the get_size() in TensorWrap::get_class(). |
src/bindings/js/node/src/tensor.cpp
Outdated
reportError(info.Env(), "getSize() does not accept any arguments."); | ||
return info.Env().Null(); | ||
} | ||
size_t size = _tensor.get_size(); |
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.
size_t size = _tensor.get_size(); | |
const auto size = static_cast<double>(_tensor.get_size()); |
src/bindings/js/node/src/tensor.cpp
Outdated
} | ||
size_t size = _tensor.get_size(); | ||
|
||
double jsSize = static_cast<double>(size); |
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.
Use snake_case in c++ part
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.
@almilosz Thanks , for the feedback I have made the suggested changes.
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.
Looks good! Minor comments
@almilosz Thanks for your feedback , I have made the suggested changes. |
hello @almilosz Code , Style / clang-format test failed , looks like a token issue ? |
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.
You need to run clang_format_fix_all
to fix CI check.
There are some extra empty lines.
Hi @almilosz , Thanks for the feedback , I have removed empty lines can you please re-run the tests again ? I am on ubuntu 22.04 lts , I was not able to install |
Hey @almilosz I set up ubuntu 20.04 env inside a dev container , and was able to successfully ran here is the ouput root ➜ .../ubuntu/openvino-fork/openvino/build (implement_getSize) $ cmake --build . --target clang_format_fix_all |
reportError(info.Env(), "getSize() does not accept any arguments."); | ||
return info.Env().Null(); | ||
} | ||
const auto size = static_cast<double>(_tensor.get_size()); |
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.
Why does it cast to double, not uint?
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 @vishniakov-nikolai thanks for your feedback , my reasoning behind casting into double
Type was because of it's close alignment with Number
Type in JS.
src/bindings/js/node/src/tensor.cpp
Outdated
|
||
Napi::Value TensorWrap::get_size(const Napi::CallbackInfo& info) { | ||
if (info.Length() > 0) { | ||
reportError(info.Env(), "getSize() does not accept any 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.
You can extract info.Env()
into variable if you use it several times.
src/bindings/js/node/src/tensor.cpp
Outdated
Napi::Value TensorWrap::get_size(const Napi::CallbackInfo& info) { | ||
if (info.Length() > 0) { | ||
reportError(info.Env(), "getSize() does not accept any arguments."); | ||
return info.Env().Null(); |
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 propose return info.Env().Undefined()
in case of error. (I know that in the sources you can meet both variants, but it could be aligned later).
it('calculates size correctly for a small 3D tensor [2, 5, 5]', () => { | ||
const shape = [2, 5, 5]; | ||
const expectedSize = 2*5*5; | ||
const tensorData = new Float32Array(expectedSize).fill(0); | ||
const tensor = new ov.Tensor(ov.element.f32, shape, tensorData); | ||
assert.strictEqual(tensor.getSize(), expectedSize); | ||
}); |
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 really like that you've implemented great tests for implemented functionality. Thank you!
But I don't see difference between this test and calculates size correctly for a common image data shape [3, 224, 224]
. I propose to remove current test.
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.
assert.strictEqual(tensor.getSize(), expectedSize); | ||
}); | ||
|
||
it('calculates size correctly for a small square matrix [4, 4]', () => { |
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 is also extra, I propose to remove it.
assert.strictEqual(tensor.getSize(), expectedSize); | ||
}); | ||
|
||
it('calculates size correctly for another small 3D tensor [2, 3, 3]', () => { |
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.
And this one, I propose to remove it.
@vishniakov-nikolai Thanks for your feedback I have made all the proposed changes , except typecasting , into uint , I have stated my reasons for it in the reply to your comment , please let me know if I need to change it. Best regards, |
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.
Looks great! Let's wait for green checks and then merge it.
@vishniakov-nikolai Android ARM64 with vcpkg / Build (pull_request) and Android ARM64 with vcpkg / ci/gha_overall_status_android are failing , was this bound to happen or something went wrong ? how can fix this ? :( |
build_jenkins |
### This Fixes openvinotoolkit#23440 ### Details: Extended Tensor API , with tensor.getSize() method - Implemented tensor.getSize() in the js api - added parameter validation - updated Tensor interface with the getSize() method in addon.ts - added Tests --------- Co-authored-by: Alicja Miloszewska <alicja.miloszewska@intel.com> Co-authored-by: Vishniakov Nikolai <nikolai.vishniakov@intel.com>
### This Fixes openvinotoolkit#23440 ### Details: Extended Tensor API , with tensor.getSize() method - Implemented tensor.getSize() in the js api - added parameter validation - updated Tensor interface with the getSize() method in addon.ts - added Tests --------- Co-authored-by: Alicja Miloszewska <alicja.miloszewska@intel.com> Co-authored-by: Vishniakov Nikolai <nikolai.vishniakov@intel.com>
This Fixes #23440
Details:
Extended Tensor API , with tensor.getSize() method