Skip to content

Commit 32b4ae7

Browse files
[C API] support string size for char pointer (openvinotoolkit#19931)
* [C API] support string size for char pointer * rename function name * Add deprecated flag * Add macro to ignore deprecated * Fix build error in windows --------- Co-authored-by: Ilya Churaev <ilya.churaev@intel.com>
1 parent 850bf3d commit 32b4ae7

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

src/bindings/c/include/openvino/c/ov_core.h

+27-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#pragma once
1212

13+
#include "openvino/c/deprecated.h"
1314
#include "openvino/c/ov_common.h"
1415
#include "openvino/c/ov_compiled_model.h"
1516
#include "openvino/c/ov_model.h"
@@ -173,8 +174,9 @@ ov_core_read_model_unicode(const ov_core_t* core,
173174
/**
174175
* @brief Reads models from IR / ONNX / PDPD / TF / TFLite formats.
175176
* @ingroup ov_core_c_api
177+
* @deprecated Use ov_core_read_model_from_memory_buffer instead.
176178
* @param core A pointer to the ie_core_t instance.
177-
* @param model_str String with a model in IR / ONNX / PDPD / TF / TFLite format.
179+
* @param model_str String with a model in IR / ONNX / PDPD / TF / TFLite format, string is null-terminated.
178180
* @param weights Shared pointer to a constant tensor with weights.
179181
* @param model A pointer to the newly created model.
180182
* Reading ONNX / PDPD / TF / TFLite models does not support loading weights from the @p weights tensors.
@@ -183,12 +185,35 @@ ov_core_read_model_unicode(const ov_core_t* core,
183185
* constant data will point to an invalid memory.
184186
* @return Status code of the operation: OK(0) for success.
185187
*/
186-
OPENVINO_C_API(ov_status_e)
188+
OPENVINO_C_API(OPENVINO_DEPRECATED(
189+
"This API is deprecated and will be replaced by ov_core_read_model_from_memory_buffer") ov_status_e)
187190
ov_core_read_model_from_memory(const ov_core_t* core,
188191
const char* model_str,
189192
const ov_tensor_t* weights,
190193
ov_model_t** model);
191194

195+
/**
196+
* @brief Reads models from IR / ONNX / PDPD / TF / TFLite formats with models string size.
197+
* @ingroup ov_core_c_api
198+
* @param core A pointer to the ie_core_t instance.
199+
* @param model_str String with a model in IR / ONNX / PDPD / TF / TFLite format, support model string containing
200+
* several null chars.
201+
* @param str_len The length of model string.
202+
* @param weights Shared pointer to a constant tensor with weights.
203+
* @param model A pointer to the newly created model.
204+
* Reading ONNX / PDPD / TF / TFLite models does not support loading weights from the @p weights tensors.
205+
* @note Created model object shares the weights with the @p weights object.
206+
* Thus, do not create @p weights on temporary data that can be freed later, since the model
207+
* constant data will point to an invalid memory.
208+
* @return Status code of the operation: OK(0) for success.
209+
*/
210+
OPENVINO_C_API(ov_status_e)
211+
ov_core_read_model_from_memory_buffer(const ov_core_t* core,
212+
const char* model_str,
213+
const size_t str_len,
214+
const ov_tensor_t* weights,
215+
ov_model_t** model);
216+
192217
/**
193218
* @brief Creates a compiled model from a source model object.
194219
* Users can create as many compiled models as they need and use

src/bindings/c/src/ov_core.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,36 @@ ov_status_e ov_core_read_model(const ov_core_t* core,
8989
return ov_status_e::OK;
9090
}
9191

92-
ov_status_e ov_core_read_model_from_memory(const ov_core_t* core,
93-
const char* model_str,
94-
const ov_tensor_t* weights,
95-
ov_model_t** model) {
96-
if (!core || !model_str || !model) {
92+
ov_status_e ov_core_read_model_from_memory_buffer(const ov_core_t* core,
93+
const char* model_str,
94+
const size_t str_size,
95+
const ov_tensor_t* weights,
96+
ov_model_t** model) {
97+
if (!core || !model_str || !model || !str_size) {
9798
return ov_status_e::INVALID_C_PARAM;
9899
}
99100

100101
try {
101102
std::unique_ptr<ov_model_t> _model(new ov_model_t);
103+
std::string model_string(model_str, str_size);
102104
if (weights) {
103-
_model->object = core->object->read_model(model_str, *(weights->object));
105+
_model->object = core->object->read_model(model_string, *(weights->object));
104106
} else {
105-
_model->object = core->object->read_model(model_str, ov::Tensor());
107+
_model->object = core->object->read_model(model_string, ov::Tensor());
106108
}
107109
*model = _model.release();
108110
}
109111
CATCH_OV_EXCEPTIONS
110112
return ov_status_e::OK;
111113
}
112114

115+
ov_status_e ov_core_read_model_from_memory(const ov_core_t* core,
116+
const char* model_str,
117+
const ov_tensor_t* weights,
118+
ov_model_t** model) {
119+
return ov_core_read_model_from_memory_buffer(core, model_str, strlen(model_str), weights, model);
120+
}
121+
113122
ov_status_e ov_core_compile_model(const ov_core_t* core,
114123
const ov_model_t* model,
115124
const char* device_name,

src/bindings/c/tests/ov_core_test.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ TEST_P(ov_core_test, ov_core_read_model_no_bin) {
7777
ov_core_free(core);
7878
}
7979

80+
OPENVINO_SUPPRESS_DEPRECATED_START
8081
TEST_P(ov_core_test, ov_core_read_model_from_memory) {
8182
ov_core_t* core = nullptr;
8283
OV_EXPECT_OK(ov_core_create(&core));
@@ -102,6 +103,36 @@ TEST_P(ov_core_test, ov_core_read_model_from_memory) {
102103
ov_model_free(model);
103104
ov_core_free(core);
104105
}
106+
OPENVINO_SUPPRESS_DEPRECATED_END
107+
108+
TEST_P(ov_core_test, ov_core_read_model_from_memory_buffer_with_size) {
109+
ov_core_t* core = nullptr;
110+
OV_EXPECT_OK(ov_core_create(&core));
111+
EXPECT_NE(nullptr, core);
112+
113+
std::vector<uint8_t> weights_content(content_from_file(bin_file_name.c_str(), true));
114+
115+
ov_tensor_t* tensor = nullptr;
116+
ov_shape_t shape;
117+
int64_t dims[2] = {1, (int64_t)weights_content.size()};
118+
ov_shape_create(2, dims, &shape);
119+
OV_EXPECT_OK(ov_tensor_create_from_host_ptr(ov_element_type_e::U8, shape, weights_content.data(), &tensor));
120+
EXPECT_NE(nullptr, tensor);
121+
122+
std::vector<uint8_t> xml_content(content_from_file(xml_file_name.c_str(), false));
123+
ov_model_t* model = nullptr;
124+
OV_EXPECT_OK(ov_core_read_model_from_memory_buffer(core,
125+
reinterpret_cast<const char*>(xml_content.data()),
126+
xml_content.size(),
127+
tensor,
128+
&model));
129+
EXPECT_NE(nullptr, model);
130+
131+
ov_shape_free(&shape);
132+
ov_tensor_free(tensor);
133+
ov_model_free(model);
134+
ov_core_free(core);
135+
}
105136

106137
TEST_P(ov_core_test, ov_core_compile_model) {
107138
auto device_name = GetParam();

0 commit comments

Comments
 (0)