From 0dd71c281a24c952b0cef5480ea4274d5bdb79c9 Mon Sep 17 00:00:00 2001 From: sunxiaoxia2022 Date: Sun, 15 May 2022 17:08:33 +0800 Subject: [PATCH 1/6] supplement two infer request interface functions --- src/bindings/c/src/ov_c_api.cpp | 51 +++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/bindings/c/src/ov_c_api.cpp b/src/bindings/c/src/ov_c_api.cpp index 71146622c03bc8..850220b2097a5b 100644 --- a/src/bindings/c/src/ov_c_api.cpp +++ b/src/bindings/c/src/ov_c_api.cpp @@ -1071,6 +1071,57 @@ ov_status_e ov_infer_request_set_callback(ov_infer_request_t* infer_request, return ov_status_e::OK; } +bool str_copy(char* dst, std::string src) { + if (src.size() > 128) { + return false; + } + + std::copy_n(src.c_str(), src.size(), dst); + + return true; +} + +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 = static_cast(infos[i].real_time.count()); + profiling_info_arr[i].cpu_time = static_cast(infos[i].cpu_time.count()); + + if (!str_copy(profiling_info_arr[i].node_name, infos[i].node_name)) { + return ov_status_e::GENERAL_ERROR; + } + if (!str_copy(profiling_info_arr[i].exec_type, infos[i].exec_type)) { + return ov_status_e::GENERAL_ERROR; + } + if (!str_copy(profiling_info_arr[i].node_type, infos[i].node_type)) { + return ov_status_e::GENERAL_ERROR; + } + } + 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; + } + 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; From a7c25cc566eb248cd0ca1bf40b1b28fe72b605a0 Mon Sep 17 00:00:00 2001 From: sunxiaoxia2022 Date: Mon, 16 May 2022 14:06:05 +0800 Subject: [PATCH 2/6] modify ov_infer_request_get_profiling_info --- src/bindings/c/include/c_api/ov_c_api.h | 4 ++-- src/bindings/c/src/ov_c_api.cpp | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) 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 b61d33f6b8f6af..8099cd1b851c71 100644 --- a/src/bindings/c/include/c_api/ov_c_api.h +++ b/src/bindings/c/include/c_api/ov_c_api.h @@ -78,12 +78,12 @@ 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. diff --git a/src/bindings/c/src/ov_c_api.cpp b/src/bindings/c/src/ov_c_api.cpp index 850220b2097a5b..81e2ae0cbcc02a 100644 --- a/src/bindings/c/src/ov_c_api.cpp +++ b/src/bindings/c/src/ov_c_api.cpp @@ -1071,12 +1071,12 @@ ov_status_e ov_infer_request_set_callback(ov_infer_request_t* infer_request, return ov_status_e::OK; } -bool str_copy(char* dst, std::string src) { - if (src.size() > 128) { +bool str_copy(char* dst, const std::string &src) { + if (src.size() + 1 > 128) { return false; } - std::copy_n(src.c_str(), src.size(), dst); + std::copy_n(src.c_str(), src.size() + 1, dst); return true; } @@ -1094,16 +1094,19 @@ ov_status_e ov_infer_request_get_profiling_info(ov_infer_request_t* infer_reques 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 = static_cast(infos[i].real_time.count()); - profiling_info_arr[i].cpu_time = static_cast(infos[i].cpu_time.count()); + profiling_info_arr[i].real_time = infos[i].real_time.count(); + profiling_info_arr[i].cpu_time = infos[i].cpu_time.count(); if (!str_copy(profiling_info_arr[i].node_name, infos[i].node_name)) { + delete [] profiling_info_arr; return ov_status_e::GENERAL_ERROR; } if (!str_copy(profiling_info_arr[i].exec_type, infos[i].exec_type)) { + delete [] profiling_info_arr; return ov_status_e::GENERAL_ERROR; } if (!str_copy(profiling_info_arr[i].node_type, infos[i].node_type)) { + delete [] profiling_info_arr; return ov_status_e::GENERAL_ERROR; } } From f7bff5422608d67755d22910a1ed1c5456950299 Mon Sep 17 00:00:00 2001 From: sunxiaoxia2022 Date: Mon, 16 May 2022 19:48:07 +0800 Subject: [PATCH 3/6] add tests dir --- src/bindings/c/tests/CMakeLists.txt | 49 +++++++++++ src/bindings/c/tests/ov_c_api_test.cpp | 102 +++++++++++++++++++++++ src/bindings/c/tests/test_model_repo.hpp | 53 ++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 src/bindings/c/tests/CMakeLists.txt create mode 100644 src/bindings/c/tests/ov_c_api_test.cpp create mode 100644 src/bindings/c/tests/test_model_repo.hpp diff --git a/src/bindings/c/tests/CMakeLists.txt b/src/bindings/c/tests/CMakeLists.txt new file mode 100644 index 00000000000000..443b0300e0eb05 --- /dev/null +++ b/src/bindings/c/tests/CMakeLists.txt @@ -0,0 +1,49 @@ +# Copyright (C) 2018-2022 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 +# + +set(TARGET_NAME "OpenVinoCAPITests") + +# Find OpenCV components if exist +find_package(OpenCV COMPONENTS core imgproc imgcodecs QUIET) +if(NOT OpenCV_FOUND) + message(WARNING "OPENCV is disabled or not found, ${TARGET_NAME} is disabled") + return() +endif() + +add_executable(${TARGET_NAME} ov_c_api_test.cpp test_model_repo.hpp) + +target_link_libraries(${TARGET_NAME} PRIVATE openvino_c2 ${OpenCV_LIBRARIES} + commonTestUtils gtest_main) + +target_include_directories(${TARGET_NAME} PUBLIC + $) + +target_compile_definitions(${TARGET_NAME} + PRIVATE + $<$:ENABLE_GAPI_PREPROCESSING> + DATA_PATH=\"${DATA_PATH}\" + MODELS_PATH=\"${MODELS_PATH}\" ) + +if(ENABLE_AUTO OR ENABLE_MULTI) + add_dependencies(${TARGET_NAME} openvino_auto_plugin) +endif() + +if(ENABLE_AUTO_BATCH) + add_dependencies(${TARGET_NAME} openvino_auto_batch_plugin) +endif() + +if(ENABLE_INTEL_CPU) + add_dependencies(${TARGET_NAME} openvino_intel_cpu_plugin) +endif() + +if(ENABLE_INTEL_GPU) + add_dependencies(${TARGET_NAME} openvino_intel_gpu_plugin) +endif() + +add_cpplint_target(${TARGET_NAME}_cpplint FOR_TARGETS ${TARGET_NAME}) + +install(TARGETS ${TARGET_NAME} + RUNTIME DESTINATION tests + COMPONENT tests + EXCLUDE_FROM_ALL) diff --git a/src/bindings/c/tests/ov_c_api_test.cpp b/src/bindings/c/tests/ov_c_api_test.cpp new file mode 100644 index 00000000000000..742c40ed4e2e95 --- /dev/null +++ b/src/bindings/c/tests/ov_c_api_test.cpp @@ -0,0 +1,102 @@ +// Copyright (C) 2018-2022 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include +#include +#include +#include +#include +#include +#include "test_model_repo.hpp" +#include + +#include "c_api/ov_c_api.h" +#include "openvino/openvino.hpp" + +std::string xml_std = TestDataHelpers::generate_model_path("test_model", "test_model_fp32.xml"), + bin_std = TestDataHelpers::generate_model_path("test_model", "test_model_fp32.bin"), + input_image_std = TestDataHelpers::generate_image_path("224x224", "dog.bmp"), + input_image_nv12_std = TestDataHelpers::generate_image_path("224x224", "dog6.yuv"); + +const char* xml = xml_std.c_str(); +const char* bin = bin_std.c_str(); +const char* input_image = input_image_std.c_str(); +const char* input_image_nv12 = input_image_nv12_std.c_str(); + +std::mutex m; +bool ready = false; +std::condition_variable condVar; +#ifdef _WIN32 + #ifdef __MINGW32__ + std::string plugins_xml_std = TestDataHelpers::generate_ieclass_xml_path("plugins_mingw.xml"); + #else + std::string plugins_xml_std = TestDataHelpers::generate_ieclass_xml_path("plugins_win.xml"); + #endif +#elif defined __APPLE__ + std::string plugins_xml_std = TestDataHelpers::generate_ieclass_xml_path("plugins_apple.xml"); +#else + std::string plugins_xml_std = TestDataHelpers::generate_ieclass_xml_path("plugins.xml"); +#endif +const char* plugins_xml = plugins_xml_std.c_str(); + +#define OV_EXPECT_OK(...) EXPECT_EQ(ov_status_e::OK, __VA_ARGS__) +#define OV_ASSERT_OK(...) ASSERT_EQ(ov_status_e::OK, __VA_ARGS__) +#define OV_EXPECT_NOT_OK(...) EXPECT_NE(ov_status_e::OK, __VA_ARGS__) + +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; + + if (fp) { + fseek(fp, 0, SEEK_END); + if (ftell(fp) >= size) { + fseek(fp, 0, SEEK_SET); + read_size = fread(img_data, 1, size, fp); + } + fclose(fp); + } + return read_size; +} + +void mat_2_tensor(const cv::Mat& img, ov_tensor_t* tensor) +{ + ov_shape_t shape; + OV_EXPECT_OK(ov_tensor_get_shape(tensor, &shape)); + size_t channels = shape[1]; + size_t width = shape[3]; + size_t height = shape[2]; + void* tensor_data = NULL; + OV_EXPECT_OK(ov_tensor_get_data(tensor, &tensor_data)); + uint8_t *tmp_data = (uint8_t *)(tensor_data); + cv::Mat resized_image; + cv::resize(img, resized_image, cv::Size(width, height)); + + for (size_t c = 0; c < channels; c++) { + for (size_t h = 0; h < height; h++) { + for (size_t w = 0; w < width; w++) { + tmp_data[c * width * height + h * width + w] = + resized_image.at(h, w)[c]; + } + } + } +} + +size_t find_device(ov_available_devices_t avai_devices, const char *device_name) { + for (size_t i = 0; i < avai_devices.num_devices; ++i) { + if (strstr(avai_devices.devices[i], device_name)) + return i; + } + + return -1; +} + +TEST(ov_c_api_version, api_version) { + ov_version_t version; + ov_get_version(&version); + auto ver = ov::get_openvino_version(); + std::string ver_str = ver.buildNumber; + + EXPECT_STREQ(version.buildNumber, ver.buildNumber); + ov_version_free(&version); +} diff --git a/src/bindings/c/tests/test_model_repo.hpp b/src/bindings/c/tests/test_model_repo.hpp new file mode 100644 index 00000000000000..02a078c16182c0 --- /dev/null +++ b/src/bindings/c/tests/test_model_repo.hpp @@ -0,0 +1,53 @@ +// Copyright (C) 2018-2022 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +namespace TestDataHelpers { + +static const char kPathSeparator = +#if defined _WIN32 || defined __CYGWIN__ + '\\'; +#else + '/'; +#endif + +std::string getModelPathNonFatal() noexcept { + if (const auto envVar = std::getenv("MODELS_PATH")) { + return envVar; + } + +#ifdef MODELS_PATH + return MODELS_PATH; +#else + return ""; +#endif +} + +std::string get_models_path() { + return getModelPathNonFatal() + kPathSeparator + std::string("models"); +}; + +std::string get_data_path() { + if (const auto envVar = std::getenv("DATA_PATH")) { + return envVar; + } + +#ifdef DATA_PATH + return DATA_PATH; +#else + return ""; +#endif +} + +std::string generate_model_path(std::string dir, std::string filename) { + return get_models_path() + kPathSeparator + dir + kPathSeparator + filename; +} + +std::string generate_image_path(std::string dir, std::string filename) { + return get_data_path() + kPathSeparator + "validation_set" + kPathSeparator + dir + kPathSeparator + filename; +} + +std::string generate_ieclass_xml_path(std::string filename) { + return getModelPathNonFatal() + kPathSeparator + "ie_class" + kPathSeparator + filename; +} +} // namespace TestDataHelpers From 11fe1a4669e2344c04452e783c8c7bef6b0493bb Mon Sep 17 00:00:00 2001 From: sunxiaoxia2022 Date: Mon, 16 May 2022 20:00:10 +0800 Subject: [PATCH 4/6] restore CMakeLists.txt --- src/bindings/c/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bindings/c/CMakeLists.txt b/src/bindings/c/CMakeLists.txt index c599e12f1b9529..2667ff9b1dc13d 100644 --- a/src/bindings/c/CMakeLists.txt +++ b/src/bindings/c/CMakeLists.txt @@ -6,6 +6,6 @@ project(InferenceEngine_C_API) add_subdirectory(src) -#if(ENABLE_TESTS) -# add_subdirectory(tests) -#endif() +if(ENABLE_TESTS) + add_subdirectory(tests) +endif() From 376b998e35ad7f650143ffc3a769cc3f17e1b41f Mon Sep 17 00:00:00 2001 From: xuejun Date: Tue, 17 May 2022 17:18:24 +0800 Subject: [PATCH 5/6] [OV 2.0 C API] Finished core related function unite test Signed-off-by: xuejun --- src/bindings/c/include/c_api/ov_c_api.h | 17 +- src/bindings/c/src/ov_c_api.cpp | 48 +++--- src/bindings/c/tests/ov_c_api_test.cpp | 205 ++++++++++++++++++++++++ 3 files changed, 248 insertions(+), 22 deletions(-) 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 0c6e341967b66e..a7145812101178 100644 --- a/src/bindings/c/include/c_api/ov_c_api.h +++ b/src/bindings/c/include/c_api/ov_c_api.h @@ -325,7 +325,7 @@ typedef enum { typedef union { uint32_t value_u; - char value_s[256]; + char value_s[512]; 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 fec8747b79a38c..e3a13ed1c00def 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" @@ -246,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; } @@ -275,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; @@ -295,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; @@ -360,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); @@ -373,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 { @@ -988,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; } diff --git a/src/bindings/c/tests/ov_c_api_test.cpp b/src/bindings/c/tests/ov_c_api_test.cpp index 742c40ed4e2e95..7b6b2c1596afa7 100644 --- a/src/bindings/c/tests/ov_c_api_test.cpp +++ b/src/bindings/c/tests/ov_c_api_test.cpp @@ -37,8 +37,10 @@ std::condition_variable condVar; std::string plugins_xml_std = TestDataHelpers::generate_ieclass_xml_path("plugins_apple.xml"); #else std::string plugins_xml_std = TestDataHelpers::generate_ieclass_xml_path("plugins.xml"); + std::string extension_std = TestDataHelpers::generate_ieclass_xml_path("libopenvino_template_extension.so"); #endif const char* plugins_xml = plugins_xml_std.c_str(); +const char* extension = extension_std.c_str(); #define OV_EXPECT_OK(...) EXPECT_EQ(ov_status_e::OK, __VA_ARGS__) #define OV_ASSERT_OK(...) ASSERT_EQ(ov_status_e::OK, __VA_ARGS__) @@ -100,3 +102,206 @@ TEST(ov_c_api_version, api_version) { EXPECT_STREQ(version.buildNumber, ver.buildNumber); 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_core_free(core); + ov_model_free(model); +} + +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_core_free(core); + ov_model_free(model); +} + +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); +} \ No newline at end of file From 64e8cebcc7d964f7c02592fc6b153735d696ee5a Mon Sep 17 00:00:00 2001 From: xuejun Date: Wed, 18 May 2022 11:07:25 +0800 Subject: [PATCH 6/6] [OV 2.0 C API] fix some review issues Signed-off-by: xuejun --- src/bindings/c/include/c_api/ov_c_api.h | 2 +- src/bindings/c/tests/ov_c_api_test.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) 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 a7145812101178..168e8aebf96413 100644 --- a/src/bindings/c/include/c_api/ov_c_api.h +++ b/src/bindings/c/include/c_api/ov_c_api.h @@ -325,7 +325,7 @@ typedef enum { typedef union { uint32_t value_u; - char value_s[512]; + char value_s[320]; ov_performance_mode_e value_performance_mode; }ov_property_value; diff --git a/src/bindings/c/tests/ov_c_api_test.cpp b/src/bindings/c/tests/ov_c_api_test.cpp index 7b6b2c1596afa7..8860363c178eaf 100644 --- a/src/bindings/c/tests/ov_c_api_test.cpp +++ b/src/bindings/c/tests/ov_c_api_test.cpp @@ -37,10 +37,8 @@ std::condition_variable condVar; std::string plugins_xml_std = TestDataHelpers::generate_ieclass_xml_path("plugins_apple.xml"); #else std::string plugins_xml_std = TestDataHelpers::generate_ieclass_xml_path("plugins.xml"); - std::string extension_std = TestDataHelpers::generate_ieclass_xml_path("libopenvino_template_extension.so"); #endif const char* plugins_xml = plugins_xml_std.c_str(); -const char* extension = extension_std.c_str(); #define OV_EXPECT_OK(...) EXPECT_EQ(ov_status_e::OK, __VA_ARGS__) #define OV_ASSERT_OK(...) ASSERT_EQ(ov_status_e::OK, __VA_ARGS__) @@ -129,8 +127,8 @@ TEST(ov_core, ov_core_read_model) { OV_ASSERT_OK(ov_core_read_model(core, xml, bin, &model)); ASSERT_NE(nullptr, model); - ov_core_free(core); ov_model_free(model); + ov_core_free(core); } TEST(ov_core, ov_core_read_model_no_bin) { @@ -142,8 +140,8 @@ TEST(ov_core, ov_core_read_model_no_bin) { OV_ASSERT_OK(ov_core_read_model(core, xml, nullptr, &model)); ASSERT_NE(nullptr, model); - ov_core_free(core); ov_model_free(model); + ov_core_free(core); } static std::vector content_from_file(const char * filename, bool is_binary) { @@ -247,7 +245,8 @@ TEST(ov_core, ov_core_get_available_devices) { ASSERT_NE(nullptr, core); ov_available_devices_t devices; - OV_ASSERT_OK(ov_core_get_available_devices(core, &devices)); + OV_ASSERT_OK(ov_core_get_available_devices(core, &devices)); + ov_available_devices_free(&devices); ov_core_free(core); } @@ -265,6 +264,7 @@ TEST_P(ov_core, ov_compiled_model_export) { 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); }