diff --git a/src/bindings/c/include/c_api/ov_c_api.h b/src/bindings/c/include/c_api/ov_c_api.h index 8dd7d8883a642c..7c8d02636b1cf2 100644 --- a/src/bindings/c/include/c_api/ov_c_api.h +++ b/src/bindings/c/include/c_api/ov_c_api.h @@ -78,27 +78,27 @@ typedef struct{ /** * @brief The absolute time, in microseconds, that the node ran (in total). */ - double real_time; + int64_t real_time; /** * @brief The net host CPU time that the node ran. */ - double cpu_time; + int64_t cpu_time; /** * @brief Name of a node. */ - char node_name[128]; + char* node_name; /** * @brief Execution type of a unit. */ - char exec_type[128]; + char* exec_type; /** * @brief Node type. */ - char node_type[128]; + char* node_type; }ov_profiling_info_t; /** @@ -325,7 +325,7 @@ typedef enum { typedef union { uint32_t value_u; - char value_s[256]; + char value_s[320]; ov_performance_mode_e value_performance_mode; }ov_property_value; @@ -381,7 +381,7 @@ OPENVINO_C_API(ov_status_e) ov_core_read_model(const ov_core_t *core, /** * @brief Reads models from IR/ONNX/PDPD formats. * @param core A pointer to the ie_core_t instance. - * @param model_path String with a model in IR/ONNX/PDPD format. + * @param model_str String with a model in IR/ONNX/PDPD format. * @param weights Shared pointer to a constant tensor with weights. * @param model A pointer to the newly created model. * Reading ONNX/PDPD models does not support loading weights from the @p weights tensors. @@ -391,7 +391,7 @@ OPENVINO_C_API(ov_status_e) ov_core_read_model(const ov_core_t *core, * @return Status code of the operation: OK(0) for success. */ OPENVINO_C_API(ov_status_e) ov_core_read_model_from_memory(const ov_core_t *core, - const char *model_path, + const char *model_str, const ov_tensor_t *weights, ov_model_t **model); @@ -856,7 +856,7 @@ OPENVINO_C_API(ov_status_e) ov_compiled_model_set_property(const ov_compiled_mod /** * @brief Gets properties for current compiled model. * @param compiled_model A pointer to the ov_compiled_model_t. - * @param property_name Property name. + * @param property_name Property name. * @param property_value A pointer to property value. * @return Status code of the operation: OK(0) for success. */ @@ -864,6 +864,15 @@ OPENVINO_C_API(ov_status_e) ov_compiled_model_get_property(const ov_compiled_mod const ov_property_key_e property_name, ov_property_value* property_value); +/** + * @brief Exports the current compiled model to an output stream `std::ostream`. + * The exported model can also be imported via the ov::Core::import_model method. + * @param compiled_model A pointer to the ov_compiled_model_t. + * @param export_model_path Path to the file. + * @return Status code of the operation: OK(0) for success. + */ +OPENVINO_C_API(ov_status_e) ov_compiled_model_export(const ov_compiled_model_t* compiled_model, + const char* export_model_path); /** * @brief Sets an input/output tensor to infer on. * @param infer_request A pointer to the ov_infer_request_t. diff --git a/src/bindings/c/src/ov_c_api.cpp b/src/bindings/c/src/ov_c_api.cpp index f10aa788298d05..414d4567b79eef 100644 --- a/src/bindings/c/src/ov_c_api.cpp +++ b/src/bindings/c/src/ov_c_api.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include "c_api/ov_c_api.h" #include "openvino/openvino.hpp" @@ -177,9 +177,10 @@ ov_element_type_e find_ov_element_type_e(ov::element::Type type) { CATCH_OV_EXCEPTION(INFER_CANCELLED, InferCancelled) \ catch (...) {return ov_status_e::UNEXPECTED;} -void str_to_char_array(const std::string& str, char** char_array) { - *char_array = new char[str.length() + 1]; - std::copy_n(str.begin(), str.length() + 1, *char_array); +char* str_to_char_array(const std::string& str) { + char *char_array = new char[str.length() + 1]; + std::copy_n(str.begin(), str.length() + 1, char_array); + return char_array; } ov_status_e ov_get_version(ov_version_t *version) { @@ -191,10 +192,10 @@ ov_status_e ov_get_version(ov_version_t *version) { ov::Version object = ov::get_openvino_version(); std::string version_builderNumber = object.buildNumber; - str_to_char_array(version_builderNumber, &(version->buildNumber)); + version->buildNumber = str_to_char_array(version_builderNumber); std::string version_description = object.description; - str_to_char_array(version_description, &version->description); + version->description = str_to_char_array(version_description); } CATCH_OV_EXCEPTIONS return ov_status_e::OK; } @@ -245,16 +246,16 @@ ov_status_e ov_core_read_model(const ov_core_t *core, } ov_status_e ov_core_read_model_from_memory(const ov_core_t *core, - const char *model_path, + const char *model_str, const ov_tensor_t *weights, ov_model_t **model) { - if (!core || !model_path || !weights || !model) { + if (!core || !model_str || !weights || !model) { return ov_status_e::GENERAL_ERROR; } try { *model = new ov_model_t; - (*model)->object = core->object->read_model(model_path, *(weights->object)); + (*model)->object = core->object->read_model(model_str, *(weights->object)); } CATCH_OV_EXCEPTIONS return ov_status_e::OK; } @@ -274,11 +275,14 @@ ov_status_e ov_core_compile_model(const ov_core_t* core, try { std::string dev_name = ""; + ov::CompiledModel object; if (device_name) { dev_name = device_name; + object = core->object->compile_model(model->object, dev_name); + } else { + object = core->object->compile_model(model->object); } *compiled_model = new ov_compiled_model_t; - auto object = core->object->compile_model(model->object, dev_name); (*compiled_model)->object = std::make_shared(std::move(object)); } CATCH_OV_EXCEPTIONS return ov_status_e::OK; @@ -294,12 +298,15 @@ ov_status_e ov_core_compile_model_from_file(const ov_core_t* core, } try { + ov::CompiledModel object; std::string dev_name = ""; if (device_name) { dev_name = device_name; + object = core->object->compile_model(model_path, dev_name); + } else { + object = core->object->compile_model(model_path); } *compiled_model = new ov_compiled_model_t; - auto object = core->object->compile_model(model_path, dev_name); (*compiled_model)->object = std::make_shared(std::move(object)); } CATCH_OV_EXCEPTIONS return ov_status_e::OK; @@ -359,7 +366,7 @@ ov_status_e ov_core_get_property(const ov_core_t* core, const char* device_name, for (const auto& i : supported_properties) { tmp_s = tmp_s + "\n" + i; } - if (tmp_s.length() + 1 > 256) { + if (tmp_s.length() + 1 > 512) { return ov_status_e::GENERAL_ERROR; } std::copy_n(tmp_s.begin(), tmp_s.length() + 1, property_value->value_s); @@ -372,18 +379,8 @@ ov_status_e ov_core_get_property(const ov_core_t* core, const char* device_name, return ov_status_e::OK; } -ov_status_e ov_core_add_extension(const ov_core_t* core, const char* library_path) { - if (!core || !library_path) { - return ov_status_e::GENERAL_ERROR; - } - try { - core->object->add_extension(library_path); - } CATCH_OV_EXCEPTIONS - return ov_status_e::OK; -} - ov_status_e ov_core_get_available_devices(const ov_core_t* core, ov_available_devices_t* devices) { - if (!core || !devices) { + if (!core) { return ov_status_e::GENERAL_ERROR; } try { @@ -391,7 +388,7 @@ ov_status_e ov_core_get_available_devices(const ov_core_t* core, ov_available_de devices->num_devices = available_devices.size(); auto tmp_devices(new char*[available_devices.size()]); for (int i = 0; i < available_devices.size(); i++) { - str_to_char_array(available_devices[i], &(tmp_devices[i])); + tmp_devices[i] = str_to_char_array(available_devices[i]); } devices->devices = tmp_devices; } CATCH_OV_EXCEPTIONS @@ -445,13 +442,13 @@ ov_status_e ov_core_get_versions(const ov_core_t* core, auto iter = object.cbegin(); for (int i = 0; i < object.size(); i++, iter++) { const auto& tmp_version_name = iter->first; - str_to_char_array(tmp_version_name, &(tmp_versions[i].device_name)); + tmp_versions[i].device_name = str_to_char_array(tmp_version_name); const auto tmp_version_build_number = iter->second.buildNumber; - str_to_char_array(tmp_version_build_number, &(tmp_versions[i].buildNumber)); + tmp_versions[i].buildNumber = str_to_char_array(tmp_version_build_number); const auto tmp_version_description = iter->second.description; - str_to_char_array(tmp_version_description, &(tmp_versions[i].description)); + tmp_versions[i].description = str_to_char_array(tmp_version_description); } versions->versions = tmp_versions; } CATCH_OV_EXCEPTIONS @@ -467,7 +464,7 @@ void ov_core_versions_free(ov_core_version_list_t *versions) { delete[] versions->versions[i].buildNumber; delete[] versions->versions[i].description; } - delete versions->versions; + delete[] versions->versions; versions->versions = nullptr; } @@ -580,7 +577,7 @@ ov_status_e ov_model_get_friendly_name(const ov_model_t* model, char **friendly_ } try { auto& result = model->object->get_friendly_name(); - str_to_char_array(result, friendly_name); + *friendly_name = str_to_char_array(result); } CATCH_OV_EXCEPTIONS return ov_status_e::OK; } @@ -987,6 +984,22 @@ ov_status_e ov_compiled_model_get_property(const ov_compiled_model_t* compiled_m return ov_status_e::OK; } +ov_status_e ov_compiled_model_export(const ov_compiled_model_t* compiled_model, + const char* export_model_path) { + if (!compiled_model || !export_model_path) { + return ov_status_e::GENERAL_ERROR; + } + try { + std::ofstream model_file(export_model_path, std::ios::out | std::ios::binary); + if (model_file.is_open()) { + compiled_model->object->export_model(model_file); + } else { + return ov_status_e::GENERAL_ERROR; + } + } CATCH_OV_EXCEPTIONS + return ov_status_e::OK; +} + void ov_infer_request_free(ov_infer_request_t *infer_request) { delete infer_request; } @@ -1071,6 +1084,46 @@ ov_status_e ov_infer_request_set_callback(ov_infer_request_t* infer_request, return ov_status_e::OK; } +ov_status_e ov_infer_request_get_profiling_info(ov_infer_request_t* infer_request, + ov_profiling_info_list_t* profiling_infos) { + if (!infer_request || !profiling_infos) { + return ov_status_e::GENERAL_ERROR; + } + + try { + auto infos = infer_request->object->get_profiling_info(); + int num = infos.size(); + profiling_infos->num = num; + ov_profiling_info_t *profiling_info_arr = new ov_profiling_info_t[num]; + for (int i = 0; i < num; i++) { + profiling_info_arr[i].status = (ov_profiling_info_t::Status)infos[i].status; + profiling_info_arr[i].real_time = infos[i].real_time.count(); + profiling_info_arr[i].cpu_time = infos[i].cpu_time.count(); + + profiling_info_arr[i].node_name = str_to_char_array(infos[i].node_name); + profiling_info_arr[i].exec_type = str_to_char_array(infos[i].exec_type); + profiling_info_arr[i].node_type = str_to_char_array(infos[i].node_type); + } + profiling_infos->profiling_infos = profiling_info_arr; + } CATCH_OV_EXCEPTIONS + + return ov_status_e::OK; +} + +void ov_profiling_info_list_free(ov_profiling_info_list_t *profiling_infos) { + if (!profiling_infos) { + return; + } + for (int i = 0; i < profiling_infos->num; i++) { + delete[] profiling_infos->profiling_infos[i].node_name; + delete[] profiling_infos->profiling_infos[i].exec_type; + delete[] profiling_infos->profiling_infos[i].node_type; + } + delete[] profiling_infos->profiling_infos; + profiling_infos->profiling_infos = nullptr; + profiling_infos->num = 0; +} + ov_status_e ov_tensor_create(const ov_element_type_e type, const ov_shape_t shape, ov_tensor_t **tensor) { if (!tensor || !shape || element_type_map.find(type) == element_type_map.end()) { return ov_status_e::GENERAL_ERROR; diff --git a/src/bindings/c/tests/ov_c_api_test.cpp b/src/bindings/c/tests/ov_c_api_test.cpp index abf5cceb9f4565..b5fd29b2b3ebbd 100644 --- a/src/bindings/c/tests/ov_c_api_test.cpp +++ b/src/bindings/c/tests/ov_c_api_test.cpp @@ -45,6 +45,25 @@ const char* plugins_xml = plugins_xml_std.c_str(); #define OV_EXPECT_NOT_OK(...) EXPECT_NE(ov_status_e::OK, __VA_ARGS__) #define OV_EXPECT_ARREQ(arr1, arr2) EXPECT_TRUE(std::equal(std::begin(arr1), std::end(arr1), std::begin(arr2))) +std::map element_type_size_map = { + {ov_element_type_e::BOOLEAN, 8}, + {ov_element_type_e::BF16, 16}, + {ov_element_type_e::F16, 16}, + {ov_element_type_e::F32, 32}, + {ov_element_type_e::F64, 64}, + {ov_element_type_e::I4, 4}, + {ov_element_type_e::I8, 8}, + {ov_element_type_e::I16, 16}, + {ov_element_type_e::I32, 32}, + {ov_element_type_e::I64, 64}, + {ov_element_type_e::U1, 1}, + {ov_element_type_e::U4, 4}, + {ov_element_type_e::U8, 8}, + {ov_element_type_e::U16, 16}, + {ov_element_type_e::U32, 32}, + {ov_element_type_e::U64, 64}}; +#define GET_ELEMENT_TYPE_SIZE(a) element_type_size_map[a] + size_t read_image_from_file(const char* img_path, unsigned char *img_data, size_t size) { FILE *fp = fopen(img_path, "rb+"); size_t read_size = 0; @@ -102,6 +121,211 @@ TEST(ov_c_api_version, api_version) { ov_version_free(&version); } +class ov_core :public::testing::TestWithParam{}; +INSTANTIATE_TEST_CASE_P(device_name, ov_core, ::testing::Values("CPU")); + +TEST(ov_core, ov_core_create_with_config) { + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create(plugins_xml, &core)); + ASSERT_NE(nullptr, core); + ov_core_free(core); +} + +TEST(ov_core, ov_core_create_with_no_config) { + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + ov_core_free(core); +} + +TEST(ov_core, ov_core_read_model) { + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + ov_model_t* model = nullptr; + OV_ASSERT_OK(ov_core_read_model(core, xml, bin, &model)); + ASSERT_NE(nullptr, model); + + ov_model_free(model); + ov_core_free(core); +} + +TEST(ov_core, ov_core_read_model_no_bin) { + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + ov_model_t* model = nullptr; + OV_ASSERT_OK(ov_core_read_model(core, xml, nullptr, &model)); + ASSERT_NE(nullptr, model); + + ov_model_free(model); + ov_core_free(core); +} + +static std::vector content_from_file(const char * filename, bool is_binary) { + std::vector result; + { + std::ifstream is(filename, is_binary ? std::ifstream::binary | std::ifstream::in : std::ifstream::in); + if (is) { + is.seekg(0, std::ifstream::end); + result.resize(is.tellg()); + if (result.size() > 0) { + is.seekg(0, std::ifstream::beg); + is.read(reinterpret_cast(&result[0]), result.size()); + } + } + } + return result; +} + +TEST(ov_core, ov_core_read_model_from_memory) { + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + std::vector weights_content(content_from_file(bin, true)); + + ov_tensor_t* tensor = nullptr; + ov_shape_t shape = {1, weights_content.size()}; + OV_ASSERT_OK(ov_tensor_create_from_host_ptr(ov_element_type_e::U8, shape, weights_content.data(), &tensor)); + ASSERT_NE(nullptr, tensor); + + std::vector xml_content(content_from_file(xml, false)); + ov_model_t* model = nullptr; + OV_ASSERT_OK(ov_core_read_model_from_memory(core, reinterpret_cast(xml_content.data()), tensor, &model)); + ASSERT_NE(nullptr, model); + + ov_tensor_free(tensor); + ov_model_free(model); + ov_core_free(core); +} + +TEST_P(ov_core, ov_core_compile_model) { + auto devece_name = GetParam(); + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + ov_model_t* model = nullptr; + OV_ASSERT_OK(ov_core_read_model(core, xml, nullptr, &model)); + ASSERT_NE(nullptr, model); + + ov_compiled_model_t* compiled_model = nullptr; + ov_property_t property = {}; + OV_ASSERT_OK(ov_core_compile_model(core, model, devece_name.c_str(), &compiled_model, &property)); + ASSERT_NE(nullptr, compiled_model); + + ov_compiled_model_free(compiled_model); + ov_model_free(model); + ov_core_free(core); +} + +TEST_P(ov_core, ov_core_compile_model_from_file) { + auto devece_name = GetParam(); + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + ov_compiled_model_t* compiled_model = nullptr; + ov_property_t property = {}; + OV_ASSERT_OK(ov_core_compile_model_from_file(core, xml, devece_name.c_str(), &compiled_model, &property)); + ASSERT_NE(nullptr, compiled_model); + + ov_compiled_model_free(compiled_model); + ov_core_free(core); +} + +TEST_P(ov_core, ov_core_set_property) { + auto devece_name = GetParam(); + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + ov_property_t property{ov_property_key_e::PERFORMANCE_HINT, ov_performance_mode_e::THROUGHPUT, nullptr}; + OV_ASSERT_OK(ov_core_set_property(core, devece_name.c_str(), &property)); + ov_core_free(core); +} + +TEST_P(ov_core, ov_core_get_property) { + auto devece_name = GetParam(); + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + ov_property_value property_value; + OV_ASSERT_OK(ov_core_get_property(core, devece_name.c_str(), ov_property_key_e::SUPPORTED_PROPERTIES, &property_value)); + ov_core_free(core); +} + +TEST(ov_core, ov_core_get_available_devices) { + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + ov_available_devices_t devices; + OV_ASSERT_OK(ov_core_get_available_devices(core, &devices)); + + ov_available_devices_free(&devices); + ov_core_free(core); +} + +TEST_P(ov_core, ov_compiled_model_export) { + auto devece_name = GetParam(); + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + ov_compiled_model_t* compiled_model = nullptr; + ov_property_t property = {}; + OV_ASSERT_OK(ov_core_compile_model_from_file(core, xml, devece_name.c_str(), &compiled_model, &property)); + ASSERT_NE(nullptr, compiled_model); + + std::string export_path = TestDataHelpers::generate_model_path("test_model", "exported_model.blob"); + OV_ASSERT_OK(ov_compiled_model_export(compiled_model, export_path.c_str())); + + ov_compiled_model_free(compiled_model); + ov_core_free(core); +} + +TEST_P(ov_core, ov_core_import_model) { + auto devece_name = GetParam(); + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + ov_compiled_model_t* compiled_model = nullptr; + ov_property_t property = {}; + OV_ASSERT_OK(ov_core_compile_model_from_file(core, xml, devece_name.c_str(), &compiled_model, &property)); + ASSERT_NE(nullptr, compiled_model); + + std::string export_path = TestDataHelpers::generate_model_path("test_model", "exported_model.blob"); + OV_ASSERT_OK(ov_compiled_model_export(compiled_model, export_path.c_str())); + ov_compiled_model_free(compiled_model); + + std::vector buffer(content_from_file(export_path.c_str(), true)); + ov_compiled_model_t* compiled_model_imported = nullptr; + OV_ASSERT_OK(ov_core_import_model(core, reinterpret_cast(buffer.data()), buffer.size(), devece_name.c_str(), &compiled_model_imported)); + ASSERT_NE(nullptr, compiled_model_imported); + ov_compiled_model_free(compiled_model_imported); + ov_core_free(core); +} + +TEST_P(ov_core, ov_core_get_versions) { + auto devece_name = GetParam(); + ov_core_t* core = nullptr; + OV_ASSERT_OK(ov_core_create("", &core)); + ASSERT_NE(nullptr, core); + + ov_core_version_list_t version_list; + OV_ASSERT_OK(ov_core_get_versions(core, devece_name.c_str(), &version_list)); + EXPECT_EQ(version_list.num_vers, 1); + + ov_core_versions_free(&version_list); + ov_core_free(core); +} + TEST(ov_tensor_create, tensor_create) { ov_element_type_e type = ov_element_type_e::U8; ov_shape_t shape = {10, 20, 30, 40}; @@ -123,14 +347,14 @@ TEST(ov_tensor_create_from_host_ptr, tensor_create_from_host_ptr) { TEST(ov_tensor_get_shape, tensor_get_shape) { ov_element_type_e type = ov_element_type_e::U8; - ov_shape_t shape_t = {10, 20, 30, 40}; + ov_shape_t shape = {10, 20, 30, 40}; ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape_t, &tensor)); + OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); EXPECT_NE(nullptr, tensor); ov_shape_t shape_res; OV_EXPECT_OK(ov_tensor_get_shape(tensor, &shape_res)); - OV_EXPECT_ARREQ(shape_t, shape_res); + OV_EXPECT_ARREQ(shape, shape_res); ov_tensor_free(tensor); } @@ -142,20 +366,20 @@ TEST(ov_tensor_set_shape, tensor_set_shape) { OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); EXPECT_NE(nullptr, tensor); - ov_shape_t shape_t = {10, 20, 30, 40}; - OV_EXPECT_OK(ov_tensor_set_shape(tensor, shape_t)); + ov_shape_t shape_update = {10, 20, 30, 40}; + OV_EXPECT_OK(ov_tensor_set_shape(tensor, shape_update)); ov_shape_t shape_res; OV_EXPECT_OK(ov_tensor_get_shape(tensor, &shape_res)); - OV_EXPECT_ARREQ(shape_t, shape_res); + OV_EXPECT_ARREQ(shape_update, shape_res); ov_tensor_free(tensor); } TEST(ov_tensor_get_element_type, tensor_get_element_type) { ov_element_type_e type = ov_element_type_e::U8; - ov_shape_t shape_t = {10, 20, 30, 40}; + ov_shape_t shape = {10, 20, 30, 40}; ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape_t, &tensor)); + OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); EXPECT_NE(nullptr, tensor); ov_element_type_e type_res; @@ -165,39 +389,59 @@ TEST(ov_tensor_get_element_type, tensor_get_element_type) { ov_tensor_free(tensor); } +static size_t product(const std::vector& dims) { + if (dims.empty()) + return 0; + return std::accumulate(std::begin(dims), std::end(dims), (size_t)1, std::multiplies()); +} + +size_t calculate_size(ov_shape_t shape) { + std::vector tmp_shape; + std::copy_if(shape, shape + 4, + std::back_inserter(tmp_shape), + [](size_t x) { return x != 0;}); + return product(tmp_shape); +} + +size_t calculate_byteSize(ov_shape_t shape, ov_element_type_e type) { + return (calculate_size(shape) * GET_ELEMENT_TYPE_SIZE(type) + 7) >> 3; +} + TEST(ov_tensor_get_size, tensor_get_size) { ov_element_type_e type = ov_element_type_e::I16; - ov_shape_t shape_t = {1, 3, 4, 4}; + ov_shape_t shape = {1, 3, 4, 4}; ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape_t, &tensor)); + OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); EXPECT_NE(nullptr, tensor); + size_t size = calculate_size(shape); size_t size_res; OV_EXPECT_OK(ov_tensor_get_size(tensor, &size_res)); - EXPECT_EQ(size_res, 48); + EXPECT_EQ(size_res, size); ov_tensor_free(tensor); } TEST(ov_tensor_get_byte_size, tensor_get_byte_size) { ov_element_type_e type = ov_element_type_e::I16; - ov_shape_t shape_t = {1, 3, 4, 4}; + ov_shape_t shape = {1, 3, 4, 4}; ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape_t, &tensor)); + OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); EXPECT_NE(nullptr, tensor); + size_t size = calculate_byteSize(shape, type); size_t size_res; OV_EXPECT_OK(ov_tensor_get_byte_size(tensor, &size_res)); - EXPECT_EQ(size_res, 96); + EXPECT_EQ(size_res, size); ov_tensor_free(tensor); } TEST(ov_tensor_get_data, tensor_get_data) { ov_element_type_e type = ov_element_type_e::U8; - ov_shape_t shape_t = {10, 20, 30, 40}; - ov_tensor_t* tensor = nullptr; - OV_EXPECT_OK(ov_tensor_create(type, shape_t, &tensor)); + ov_shape_t shape = {10, 20, 30, 40}; + ov_tensor_t *tensor = nullptr; + OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor)); EXPECT_NE(nullptr, tensor); void *data = nullptr;