Skip to content

Commit 517ad68

Browse files
[OV][JS] Expose the Tensor.isContinuous to Node.js API (#27710)
### Details: * Add a TensorWrap::is_continuous function: Calls the underlying Tensor.isContinous function * Update the addon.ts file with the isContinuous method * Add unit tests for the isContinuous Api ### Tickets: Closes: #27701 Signed-off-by: Nashez Zubair <nashezzubair@gmail.com> Co-authored-by: Vishniakov Nikolai <nikolai.vishniakov@intel.com>
1 parent 3ce61d6 commit 517ad68

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

src/bindings/js/node/include/tensor.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class TensorWrap : public Napi::ObjectWrap<TensorWrap> {
5959
Napi::Value get_element_type(const Napi::CallbackInfo& info);
6060
/** @return Napi::Number containing tensor size as total number of elements. */
6161
Napi::Value get_size(const Napi::CallbackInfo& info);
62+
/**
63+
* @brief Getter to check if tensor is continuous
64+
* @return Napi::Boolean
65+
*/
66+
Napi::Value is_continuous(const Napi::CallbackInfo& info);
6267

6368
private:
6469
ov::Tensor _tensor;

src/bindings/js/node/lib/addon.ts

+4
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ interface Tensor {
419419
* It gets the tensor size as a total number of elements.
420420
*/
421421
getSize(): number;
422+
/**
423+
* Reports whether the tensor is continuous or not.
424+
*/
425+
isContinuous(): boolean;
422426
}
423427

424428
/**

src/bindings/js/node/src/tensor.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ Napi::Function TensorWrap::get_class(Napi::Env env) {
4343
InstanceMethod("getData", &TensorWrap::get_data),
4444
InstanceMethod("getShape", &TensorWrap::get_shape),
4545
InstanceMethod("getElementType", &TensorWrap::get_element_type),
46-
InstanceMethod("getSize", &TensorWrap::get_size)});
46+
InstanceMethod("getSize", &TensorWrap::get_size),
47+
InstanceMethod("isContinuous", &TensorWrap::is_continuous)});
4748
}
4849

4950
ov::Tensor TensorWrap::get_tensor() const {
@@ -181,3 +182,12 @@ Napi::Value TensorWrap::get_size(const Napi::CallbackInfo& info) {
181182
const auto size = static_cast<double>(_tensor.get_size());
182183
return Napi::Number::New(env, size);
183184
}
185+
186+
Napi::Value TensorWrap::is_continuous(const Napi::CallbackInfo& info) {
187+
Napi::Env env = info.Env();
188+
if (info.Length() > 0) {
189+
reportError(env, "isContinuous() does not accept any arguments.");
190+
return env.Undefined();
191+
}
192+
return Napi::Boolean::New(env, _tensor.is_continuous());
193+
}

src/bindings/js/node/tests/unit/tensor.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,18 @@ describe('ov.Tensor tests', () => {
297297
assert.strictEqual(tensor.getSize(), expectedSize);
298298
});
299299
});
300+
301+
describe('Tensor isContinuous', () => {
302+
it('isContinuous returns true if tensor is continuous', () => {
303+
const tensor = new ov.Tensor(ov.element.f32, [3, 2, 2]);
304+
assert.strictEqual(tensor.isContinuous(), true);
305+
});
306+
307+
it('isContinuous should throw an error if arguments are provided', () => {
308+
const tensor = new ov.Tensor(ov.element.f32, shape, data);
309+
assert.throws(() => tensor.isContinuous(1), {
310+
message: 'isContinuous() does not accept any arguments.',
311+
});
312+
});
313+
});
300314
});

0 commit comments

Comments
 (0)