Skip to content

Commit 59a0f01

Browse files
Pey-cryptoalmilosz
andauthored
Implemented getOutputElementType (openvinotoolkit#25760)
Implemented Method on c++ side. Updated typescript definitions. Created unit tests. For Issue [https://github.com/openvinotoolkit/openvino/issues/25406](https://github.com/openvinotoolkit/openvino/issues/25406) Resolved merge errors --------- Co-authored-by: Alicja Miloszewska <alicja.miloszewska@intel.com>
1 parent d29948c commit 59a0f01

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ class ModelWrap : public Napi::ObjectWrap<ModelWrap> {
108108
* @return Napi::Array containing a shape of requested output.
109109
*/
110110
Napi::Value get_output_shape(const Napi::CallbackInfo& info);
111+
112+
/**
113+
* @brief Helper function to access model output elements types.
114+
* @return Napi::String representing the element type of the requested output.
115+
*
116+
*/
117+
Napi::Value get_output_element_type(const Napi::CallbackInfo& info);
111118

112119
private:
113120
std::shared_ptr<ov::Model> _model;

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

+5
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ interface Model {
234234
* It returns the number of the model outputs.
235235
*/
236236
getOutputSize(): number;
237+
/**
238+
* It gets the element type of a specific output of the model.
239+
* @param index The index of the output.
240+
*/
241+
getOutputElementType(index: number): string;
237242
/**
238243
* It gets the input of the model.
239244
* If a model has more than one input, this method throws an exception.

src/bindings/js/node/src/model_wrap.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "node/include/errors.hpp"
88
#include "node/include/helper.hpp"
99
#include "node/include/node_output.hpp"
10+
#include "node/include/type_validation.hpp"
1011

1112
ModelWrap::ModelWrap(const Napi::CallbackInfo& info)
1213
: Napi::ObjectWrap<ModelWrap>(info),
@@ -25,6 +26,7 @@ Napi::Function ModelWrap::get_class(Napi::Env env) {
2526
InstanceMethod("setFriendlyName", &ModelWrap::set_friendly_name),
2627
InstanceMethod("getFriendlyName", &ModelWrap::get_friendly_name),
2728
InstanceMethod("getOutputShape", &ModelWrap::get_output_shape),
29+
InstanceMethod("getOutputElementType", &ModelWrap::get_output_element_type),
2830
InstanceAccessor<&ModelWrap::get_inputs>("inputs"),
2931
InstanceAccessor<&ModelWrap::get_outputs>("outputs")});
3032
}
@@ -171,3 +173,19 @@ Napi::Value ModelWrap::get_output_shape(const Napi::CallbackInfo& info) {
171173
return info.Env().Undefined();
172174
}
173175
}
176+
177+
Napi::Value ModelWrap::get_output_element_type(const Napi::CallbackInfo& info) {
178+
std::vector<std::string> allowed_signatures;
179+
try {
180+
if (ov::js::validate<int>(info, allowed_signatures)) {
181+
auto idx = info[0].As<Napi::Number>().Int32Value();
182+
const auto& output = _model->output(idx);
183+
return cpp_to_js<ov::element::Type_t, Napi::String>(info, output.get_element_type());
184+
} else {
185+
OPENVINO_THROW("'getOutputElementType'", ov::js::get_parameters_error_msg(info, allowed_signatures));
186+
}
187+
} catch (const std::exception& e) {
188+
reportError(info.Env(), e.what());
189+
return info.Env().Undefined();
190+
}
191+
}

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

+45
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,48 @@ describe('Model.getOutputSize()', () => {
112112
assert.strictEqual(model.getOutputSize(), 1, 'Expected getOutputSize to return 1 for the default model');
113113
});
114114
});
115+
116+
describe('Model.getOutputElementType()', () => {
117+
it('should return a string for the element type ', () => {
118+
const result = model.getOutputElementType(0);
119+
assert.strictEqual(typeof result, 'string',
120+
'getOutputElementType() should return a string');
121+
});
122+
123+
it('should accept a single integer argument', () => {
124+
assert.throws(() => {
125+
model.getOutputElementType();
126+
}, /'getOutputElementType' method called with incorrect parameters/,
127+
'Should throw when called without arguments');
128+
129+
assert.throws(() => {
130+
model.getOutputElementType('unexpected argument');
131+
}, /'getOutputElementType' method called with incorrect parameters/,
132+
'Should throw on non-number argument');
133+
134+
assert.throws(() => {
135+
model.getOutputElementType(0, 1);
136+
}, /'getOutputElementType' method called with incorrect parameters/,
137+
'Should throw on multiple arguments');
138+
139+
assert.throws(() => {
140+
model.getOutputElementType(3.14);
141+
}, /'getOutputElementType' method called with incorrect parameters/,
142+
'Should throw on non-integer number');
143+
});
144+
145+
it('should return a valid element type for the default model', () => {
146+
const elementType = model.getOutputElementType(0);
147+
assert.ok(typeof elementType === 'string' && elementType.length > 0,
148+
`Expected a non-empty string, got ${elementType}`);
149+
});
150+
151+
it('should throw an error for out-of-range index', () => {
152+
const outputSize = model.getOutputSize();
153+
assert.throws(
154+
() => { model.getOutputElementType(outputSize); },
155+
/^Error: /,
156+
'Should throw for out-of-range index'
157+
);
158+
});
159+
});

0 commit comments

Comments
 (0)