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

Added Tensor.get_size() method to Node.js API #23498

Merged
merged 10 commits into from
Mar 28, 2024

Conversation

qxprakash
Copy link
Contributor

@qxprakash qxprakash commented Mar 16, 2024

This Fixes #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

@qxprakash qxprakash requested a review from a team as a code owner March 16, 2024 16:54
@github-actions github-actions bot added the category: JS API OpenVino JS API Bindings label Mar 16, 2024
@qxprakash
Copy link
Contributor Author

qxprakash commented Mar 16, 2024

hello @vishniakov-nikolai , @almilosz can you please review ? let me know if any changes are required.

@qxprakash qxprakash changed the title implmented tensor.getSize() Expose Tensor.get_size() method to Node.js API Mar 16, 2024
@qxprakash qxprakash changed the title Expose Tensor.get_size() method to Node.js API Added Tensor.get_size() method to Node.js API Mar 16, 2024
@rkazants rkazants added the ExternalPR External contributor label Mar 17, 2024
@@ -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*/
Copy link
Contributor

@almilosz almilosz Mar 20, 2024

Choose a reason for hiding this comment

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

Suggested change
/**@return Napi::Number containing tensor size*/
/**@return Napi::Number containing tensor size as total number of elements.*/

Copy link
Contributor Author

@qxprakash qxprakash Mar 20, 2024

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 ?

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.

The TensorWrap::get_size() method has to be hooked up to the accessor when defining a class in the TensorWrap::get_class().

@qxprakash
Copy link
Contributor Author

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

reportError(info.Env(), "getSize() does not accept any arguments.");
return info.Env().Null();
}
size_t size = _tensor.get_size();
Copy link
Contributor

@almilosz almilosz Mar 20, 2024

Choose a reason for hiding this comment

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

Suggested change
size_t size = _tensor.get_size();
const auto size = static_cast<double>(_tensor.get_size());

}
size_t size = _tensor.get_size();

double jsSize = static_cast<double>(size);
Copy link
Contributor

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

Copy link
Contributor Author

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.

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.

Looks good! Minor comments

@qxprakash
Copy link
Contributor Author

Looks good! Minor comments

@almilosz Thanks for your feedback , I have made the suggested changes.

@qxprakash
Copy link
Contributor Author

qxprakash commented Mar 22, 2024

hello @almilosz Code , Style / clang-format test failed , looks like a token issue ?

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.

You need to run clang_format_fix_all to fix CI check.
There are some extra empty lines.

@qxprakash
Copy link
Contributor Author

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 clang-format-9 which was dependency for clang_format_fix_all.

@qxprakash qxprakash requested a review from almilosz March 23, 2024 10:23
@qxprakash
Copy link
Contributor Author

qxprakash commented Mar 23, 2024

Hey @almilosz I set up ubuntu 20.04 env inside a dev container , and was able to successfully ran clang_format_fix_all on my latest commit , I see no diffs , which means the tests should pass now right ?

here is the ouput

root ➜ .../ubuntu/openvino-fork/openvino/build (implement_getSize) $ cmake --build . --target clang_format_fix_all
[0%] [clang-format] openvino_template_extension_clang_fix
[ 0%] Built target openvino_template_extension_clang_fix
[ 0%] [clang-format] openvino_runtime_dev_clang_fix
[ 0%] Built target openvino_runtime_dev_clang_fix
[ 0%] [clang-format] openvino_itt_clang_fix
[ 0%] Built target openvino_itt_clang_fix
[ 0%] [clang-format] openvino_conditional_compilation_clang_fix
[ 0%] Built target openvino_conditional_compilation_clang_fix
[ 0%] [clang-format] openvino_util_clang_fix
[ 0%] Built target openvino_util_clang_fix
[ 0%] [clang-format] openvino_transformations_clang_fix
[ 0%] Built target openvino_transformations_clang_fix
[ 0%] [clang-format] openvino_offline_transformations_clang_fix
[ 0%] Built target openvino_offline_transformations_clang_fix
[ 0%] [clang-format] openvino_core_clang_fix
[ 0%] Built target openvino_core_clang_fix
[ 0%] [clang-format] openvino_reference_clang_fix
[ 0%] Built target openvino_reference_clang_fix
[ 0%] [clang-format] openvino_shape_inference_clang_fix
[ 0%] Built target openvino_shape_inference_clang_fix
[ 0%] [clang-format] openvino_frontend_common_clang_fix
[ 0%] Built target openvino_frontend_common_clang_fix
[ 33%] [clang-format] openvino_ir_frontend_clang_fix
[ 33%] Built target openvino_ir_frontend_clang_fix
[ 33%] [clang-format] openvino_onnx_common_clang_fix
[ 33%] Built target openvino_onnx_common_clang_fix
[ 66%] [clang-format] openvino_onnx_frontend_clang_fix
[ 66%] Built target openvino_onnx_frontend_clang_fix
[ 66%] [clang-format] openvino_pytorch_frontend_clang_fix
[ 66%] Built target openvino_pytorch_frontend_clang_fix
[ 66%] [clang-format] openvino_tensorflow_common_clang_fix
[ 66%] Built target openvino_tensorflow_common_clang_fix
[ 66%] [clang-format] openvino_tensorflow_frontend_clang_fix
[ 66%] Built target openvino_tensorflow_frontend_clang_fix
[ 66%] [clang-format] openvino_tensorflow_lite_frontend_clang_fix
[ 66%] Built target openvino_tensorflow_lite_frontend_clang_fix
[ 66%] [clang-format] openvino_paddle_frontend_clang_fix
[ 66%] Built target openvino_paddle_frontend_clang_fix
[ 66%] [clang-format] openvino_auto_batch_plugin_clang_fix
[ 66%] Built target openvino_auto_batch_plugin_clang_fix
[ 66%] [clang-format] openvino_hetero_plugin_clang_fix
[ 66%] Built target openvino_hetero_plugin_clang_fix
[ 66%] [clang-format] openvino_proxy_plugin_obj_clang_fix
[ 66%] Built target openvino_proxy_plugin_obj_clang_fix
[ 66%] [clang-format] openvino_runtime_clang_fix
[ 66%] Built target openvino_runtime_clang_fix
[ 66%] [clang-format] openvino_c_clang_fix
[ 66%] Built target openvino_c_clang_fix
[ 66%] [clang-format] ov_node_addon_clang_fix
[ 66%] Built target ov_node_addon_clang_fix
[ 66%] [clang-format] ie_samples_utils_clang_fix
[ 66%] Built target ie_samples_utils_clang_fix
[ 66%] [clang-format] format_reader_clang_fix
[ 66%] Built target format_reader_clang_fix
[ 66%] [clang-format] sync_benchmark_clang_fix
[ 66%] Built target sync_benchmark_clang_fix
[100%] [clang-format] throughput_benchmark_clang_fix
[100%] Built target throughput_benchmark_clang_fix
[100%] [clang-format] benchmark_app_clang_fix
[100%] Built target benchmark_app_clang_fix
[100%] [clang-format] classification_sample_async_clang_fix
[100%] Built target classification_sample_async_clang_fix
[100%] [clang-format] hello_classification_clang_fix
[100%] Built target hello_classification_clang_fix
[100%] [clang-format] hello_nv12_input_classification_clang_fix
[100%] Built target hello_nv12_input_classification_clang_fix
[100%] [clang-format] hello_query_device_clang_fix
[100%] Built target hello_query_device_clang_fix
[100%] [clang-format] hello_reshape_ssd_clang_fix
[100%] Built target hello_reshape_ssd_clang_fix
[100%] [clang-format] model_creation_sample_clang_fix
[100%] Built target model_creation_sample_clang_fix
[100%] [clang-format] opencv_c_wrapper_clang_fix
[100%] Built target opencv_c_wrapper_clang_fix
[100%] [clang-format] hello_classification_c_clang_fix
[100%] Built target hello_classification_c_clang_fix
[100%] [clang-format] hello_nv12_input_classification_c_clang_fix
[100%] Built target hello_nv12_input_classification_c_clang_fix
[100%] [clang-format] openvino_interpreter_backend_clang_fix
[100%] Built target openvino_interpreter_backend_clang_fix
[100%] [clang-format] openvino_template_plugin_clang_fix
[100%] Built target openvino_template_plugin_clang_fix
[100%] Built 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());
Copy link
Contributor

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?

Copy link
Contributor Author

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.


Napi::Value TensorWrap::get_size(const Napi::CallbackInfo& info) {
if (info.Length() > 0) {
reportError(info.Env(), "getSize() does not accept any arguments.");
Copy link
Contributor

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.

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();
Copy link
Contributor

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

Comment on lines 183 to 189
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);
});
Copy link
Contributor

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.

Copy link
Contributor Author

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]', () => {
Copy link
Contributor

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]', () => {
Copy link
Contributor

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.

@qxprakash
Copy link
Contributor Author

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

Copy link
Contributor

@vishniakov-nikolai vishniakov-nikolai left a 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.

@qxprakash
Copy link
Contributor Author

qxprakash commented Mar 26, 2024

@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 ? :(

@mlukasze
Copy link
Contributor

build_jenkins

@almilosz almilosz added this pull request to the merge queue Mar 28, 2024
Merged via the queue into openvinotoolkit:master with commit 496a5de Mar 28, 2024
108 checks passed
@mlukasze mlukasze added this to the 2024.1 milestone Mar 28, 2024
bbielawx pushed a commit to bbielawx/openvino that referenced this pull request Apr 12, 2024
### 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>
alvoron pushed a commit to alvoron/openvino that referenced this pull request Apr 29, 2024
### 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>
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]: Expose Tensor.get_size() method to Node.js API
5 participants