Skip to content

Commit 424404a

Browse files
[OV JS] Support element string type (openvinotoolkit#24176)
### Tickets: - 138768 --------- Co-authored-by: Alicja Miloszewska <alicja.miloszewska@intel.com>
1 parent 0ae81fb commit 424404a

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ type elementTypeString =
1818
| 'i32'
1919
| 'i16'
2020
| 'f64'
21-
| 'f32';
21+
| 'f32'
22+
| 'string';
2223

2324
interface Core {
2425
compileModel(
@@ -38,7 +39,7 @@ interface Core {
3839
readModelSync(modelBuffer: Uint8Array, weightsBuffer?: Uint8Array): Model;
3940
importModelSync(modelStream: Buffer, device: string): CompiledModel;
4041
importModelSync(
41-
modelStream: Buffer,
42+
modelStream: Buffer,
4243
device: string,
4344
props: { [key: string]: string | number | boolean }
4445
): CompiledModel;
@@ -181,6 +182,7 @@ declare enum element {
181182
i64,
182183
f32,
183184
f64,
185+
string,
184186
}
185187

186188
declare enum resizeAlgorithm {

src/bindings/js/node/src/helper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
const std::vector<std::string>& get_supported_types() {
1010
static const std::vector<std::string> supported_element_types =
11-
{"i8", "u8", "i16", "u16", "i32", "u32", "f32", "f64", "i64", "u64"};
11+
{"i8", "u8", "i16", "u16", "i32", "u32", "f32", "f64", "i64", "u64", "string"};
1212
return supported_element_types;
1313
}
1414

src/bindings/js/node/src/preprocess/input_tensor_info.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ Napi::Value InputTensorInfo::set_shape(const Napi::CallbackInfo& info) {
4747
}
4848

4949
Napi::Value InputTensorInfo::set_element_type(const Napi::CallbackInfo& info) {
50-
if (info.Length() == 1) {
51-
try {
52-
auto type = js_to_cpp<ov::element::Type_t>(info, 0, {napi_string});
53-
_tensor_info->set_element_type(type);
54-
} catch (std::exception& e) {
55-
reportError(info.Env(), e.what());
56-
}
57-
} else {
58-
reportError(info.Env(), "Error in setElementType(). Wrong number of parameters.");
50+
try {
51+
OPENVINO_ASSERT(info.Length() == 1, "Error in setElementType(). Wrong number of parameters.");
52+
53+
auto type = js_to_cpp<ov::element::Type_t>(info, 0, {napi_string});
54+
55+
OPENVINO_ASSERT(type != ov::element::string, "String tensors are not supported in JS API.");
56+
57+
_tensor_info->set_element_type(type);
58+
} catch (std::exception& e) {
59+
reportError(info.Env(), e.what());
5960
}
61+
6062
return info.This();
6163
}
6264

src/bindings/js/node/src/preprocess/output_tensor_info.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ Napi::Value OutputTensorInfo::set_layout(const Napi::CallbackInfo& info) {
3232
}
3333

3434
Napi::Value OutputTensorInfo::set_element_type(const Napi::CallbackInfo& info) {
35-
if (info.Length() != 1) {
36-
reportError(info.Env(), "Error in setElementType(). Wrong number of parameters.");
37-
return info.Env().Undefined();
38-
}
3935
try {
36+
OPENVINO_ASSERT(info.Length() == 1, "Error in setElementType(). Wrong number of parameters.");
37+
4038
auto type = js_to_cpp<ov::element::Type_t>(info, 0, {napi_string});
39+
40+
OPENVINO_ASSERT(type != ov::element::string, "String tensors are not supported in JS API.");
41+
4142
_tensor_info->set_element_type(type);
4243
} catch (std::exception& e) {
4344
reportError(info.Env(), e.what());
44-
return info.Env().Undefined();
4545
}
46+
4647
return info.This();
4748
}
4849

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

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ TensorWrap::TensorWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Tensor
2121

2222
try {
2323
const auto type = js_to_cpp<ov::element::Type_t>(info, 0, {napi_string});
24+
25+
OPENVINO_ASSERT(type != ov::element::string, "String tensors are not supported in JS API.");
26+
2427
const auto shape_vec = js_to_cpp<std::vector<size_t>>(info, 1, {napi_int32_array, napi_uint32_array, js_array});
2528
const auto& shape = ov::Shape(shape_vec);
2629

@@ -74,6 +77,8 @@ Napi::Object TensorWrap::wrap(Napi::Env env, ov::Tensor tensor) {
7477
Napi::Value TensorWrap::get_data(const Napi::CallbackInfo& info) {
7578
auto type = _tensor.get_element_type();
7679

80+
OPENVINO_ASSERT(type != ov::element::string, "String tensors are not supported in JS API.");
81+
7782
switch (type) {
7883
case ov::element::Type_t::i8: {
7984
auto arr = Napi::Int8Array::New(info.Env(), _tensor.get_size());

0 commit comments

Comments
 (0)