diff --git a/.ci/azure/linux_onnxruntime.yml b/.ci/azure/linux_onnxruntime.yml index ad5e630820d..4fd14f2c1b1 100644 --- a/.ci/azure/linux_onnxruntime.yml +++ b/.ci/azure/linux_onnxruntime.yml @@ -94,7 +94,7 @@ jobs: -DENABLE_PROFILING_ITT=OFF -DENABLE_SAMPLES=OFF -DNGRAPH_ONNX_FRONTEND_ENABLE=ON - -DNGRAPH_DEBUG_ENABLE=OFF + -DOPENVINO_DEBUG_ENABLE=OFF $(REPO_DIR) workingDirectory: $(BUILD_DIR) diff --git a/.ci/openvino-onnx/Dockerfile b/.ci/openvino-onnx/Dockerfile index 8e2365e4ebc..ada3c36adb1 100644 --- a/.ci/openvino-onnx/Dockerfile +++ b/.ci/openvino-onnx/Dockerfile @@ -68,7 +68,7 @@ RUN cmake .. \ -DENABLE_PYTHON=ON \ -DPYTHON_EXECUTABLE=/usr/bin/python3 \ -DNGRAPH_ONNX_FRONTEND_ENABLE=ON \ - -DNGRAPH_DEBUG_ENABLE=OFF \ + -DOPENVINO_DEBUG_ENABLE=OFF \ -DCMAKE_INSTALL_PREFIX=/openvino/dist \ -DNGRAPH_USE_PROTOBUF_LITE=${PROTOBUF_LITE} RUN make -j $(nproc) install diff --git a/cmake/features.cmake b/cmake/features.cmake index 450b0d255cc..ddc3dbdeb1d 100644 --- a/cmake/features.cmake +++ b/cmake/features.cmake @@ -130,7 +130,7 @@ ie_dependent_option(NGRAPH_USE_SYSTEM_PROTOBUF "Use system protobuf" OFF ie_dependent_option(NGRAPH_UNIT_TEST_ENABLE "Enables ngraph unit tests" ON "ENABLE_TESTS;NOT ANDROID" OFF) ie_dependent_option(NGRAPH_UNIT_TEST_BACKENDS_ENABLE "Control the building of unit tests using backends" ON "NGRAPH_UNIT_TEST_ENABLE" OFF) -ie_option(NGRAPH_DEBUG_ENABLE "Enable output for NGRAPH_DEBUG statements" OFF) +ie_option(OPENVINO_DEBUG_ENABLE "Enable output for OPENVINO_DEBUG statements" OFF) ie_option(ENABLE_REQUIREMENTS_INSTALL "Dynamic dependencies install" ON) # WA for ngraph python build on Windows debug diff --git a/docs/nGraph_DG/nGraph_debug_capabilities.md b/docs/nGraph_DG/nGraph_debug_capabilities.md index e2ae82743c1..bda4dff150c 100644 --- a/docs/nGraph_DG/nGraph_debug_capabilities.md +++ b/docs/nGraph_DG/nGraph_debug_capabilities.md @@ -3,7 +3,7 @@ nGraph representation provides an API to get detailed information about the graph structure. To receive additional messages about applied graph modifications, rebuild the nGraph library with -the `-DNGRAPH_DEBUG_ENABLE=ON` option. +the `-DOPENVINO_DEBUG_ENABLE=ON` option. To visualize the nGraph function to the xDot format or to an image file, use the `ngraph::pass::VisualizeTree` graph transformation pass: diff --git a/inference-engine/src/cldnn_engine/cldnn_config.cpp b/inference-engine/src/cldnn_engine/cldnn_config.cpp index 28aa8063e47..533e32271bd 100644 --- a/inference-engine/src/cldnn_engine/cldnn_config.cpp +++ b/inference-engine/src/cldnn_engine/cldnn_config.cpp @@ -28,7 +28,7 @@ namespace CLDNNPlugin { static void createDirectory(std::string _path) { #if defined(ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32) - std::wstring widepath = FileUtils::multiByteCharToWString(_path.c_str()); + std::wstring widepath = ov::util::string_to_wstring(_path.c_str()); const wchar_t* path = widepath.c_str(); #else const char* path = _path.c_str(); diff --git a/inference-engine/src/inference_engine/CMakeLists.txt b/inference-engine/src/inference_engine/CMakeLists.txt index 1162adf49c6..c772c5a2038 100644 --- a/inference-engine/src/inference_engine/CMakeLists.txt +++ b/inference-engine/src/inference_engine/CMakeLists.txt @@ -99,7 +99,7 @@ target_include_directories(${TARGET_NAME}_plugin_api INTERFACE $ ${PUBLIC_HEADERS_DIR} ${PUBLIC_HEADERS_DIR}/ie) -target_link_libraries(${TARGET_NAME}_plugin_api INTERFACE pugixml::static openvino::itt) +target_link_libraries(${TARGET_NAME}_plugin_api INTERFACE pugixml::static openvino::itt openvino::util) set_ie_threading_interface_for(${TARGET_NAME}_plugin_api) @@ -134,7 +134,7 @@ target_include_directories(${TARGET_NAME}_obj PRIVATE "${CMAKE_CURRENT_SOURCE_DI $ $) -target_link_libraries(${TARGET_NAME}_obj PRIVATE ${TARGET_NAME}_reader_api openvino::itt) +target_link_libraries(${TARGET_NAME}_obj PRIVATE ${TARGET_NAME}_reader_api openvino::itt openvino::util) set_ie_threading_interface_for(${TARGET_NAME}_obj) if (TBBBIND_2_4_FOUND) @@ -167,7 +167,7 @@ if (TBBBIND_2_4_FOUND) target_link_libraries(${TARGET_NAME} PRIVATE ${TBBBIND_2_4_IMPORTED_TARGETS}) endif() -target_link_libraries(${TARGET_NAME} PRIVATE pugixml::static openvino::itt ${CMAKE_DL_LIBS} Threads::Threads +target_link_libraries(${TARGET_NAME} PRIVATE pugixml::static openvino::itt openvino::util ${CMAKE_DL_LIBS} Threads::Threads frontend_manager::static inference_engine_transformations PUBLIC ngraph) diff --git a/inference-engine/src/inference_engine/src/file_utils.cpp b/inference-engine/src/inference_engine/src/file_utils.cpp index 3a72787063d..a794a396cd0 100644 --- a/inference-engine/src/inference_engine/src/file_utils.cpp +++ b/inference-engine/src/inference_engine/src/file_utils.cpp @@ -66,39 +66,9 @@ #endif -#ifdef ENABLE_UNICODE_PATH_SUPPORT - -std::string FileUtils::wStringtoMBCSstringChar(const std::wstring& wstr) { -# ifdef _WIN32 - int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); - std::string strTo(size_needed, 0); - WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); - return strTo; -# else - std::wstring_convert> wstring_decoder; - return wstring_decoder.to_bytes(wstr); -# endif -} - -std::wstring FileUtils::multiByteCharToWString(const char* str) { -# ifdef _WIN32 - int strSize = static_cast(std::strlen(str)); - int size_needed = MultiByteToWideChar(CP_UTF8, 0, str, strSize, NULL, 0); - std::wstring wstrTo(size_needed, 0); - MultiByteToWideChar(CP_UTF8, 0, str, strSize, &wstrTo[0], size_needed); - return wstrTo; -# else - std::wstring_convert> wstring_encoder; - std::wstring result = wstring_encoder.from_bytes(str); - return result; -# endif -} - -#endif // ENABLE_UNICODE_PATH_SUPPORT - long long FileUtils::fileSize(const char* charfilepath) { #if defined(ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32) - std::wstring widefilename = FileUtils::multiByteCharToWString(charfilepath); + std::wstring widefilename = ov::util::string_to_wstring(charfilepath); const wchar_t* fileName = widefilename.c_str(); #elif defined(__ANDROID__) || defined(ANDROID) std::string fileName = charfilepath; @@ -138,7 +108,7 @@ void FileUtils::createDirectoryRecursive(const std::string& dirPath) { return; } - std::size_t pos = dirPath.rfind(FileUtils::FileSeparator); + std::size_t pos = dirPath.rfind(ov::util::FileTraits::file_separator); if (pos != std::string::npos) { createDirectoryRecursive(dirPath.substr(0, pos)); } @@ -156,7 +126,7 @@ namespace { template > std::basic_string getPathName(const std::basic_string& s) { - size_t i = s.rfind(FileUtils::FileTraits::FileSeparator, s.length()); + size_t i = s.rfind(ov::util::FileTraits::file_separator, s.length()); if (i != std::string::npos) { return (s.substr(0, i)); } @@ -213,7 +183,7 @@ std::wstring getIELibraryPathW() { GetModuleFileNameW(hm, (LPWSTR)ie_library_path, sizeof(ie_library_path) / sizeof(ie_library_path[0])); return getPathName(std::wstring(ie_library_path)); # elif defined(__linux__) || defined(__APPLE__) - return ::FileUtils::multiByteCharToWString(getIELibraryPathA().c_str()); + return ::ov::util::string_to_wstring(getIELibraryPathA().c_str()); # else # error "Unsupported OS" # endif @@ -223,7 +193,7 @@ std::wstring getIELibraryPathW() { std::string getIELibraryPath() { #ifdef ENABLE_UNICODE_PATH_SUPPORT - return FileUtils::wStringtoMBCSstringChar(getIELibraryPathW()); + return ov::util::wstring_to_string(getIELibraryPathW()); #else return getIELibraryPathA(); #endif diff --git a/inference-engine/src/inference_engine/src/ie_core.cpp b/inference-engine/src/inference_engine/src/ie_core.cpp index cd15015fa82..9c7b14316dc 100644 --- a/inference-engine/src/inference_engine/src/ie_core.cpp +++ b/inference-engine/src/inference_engine/src/ie_core.cpp @@ -30,6 +30,7 @@ #include "ngraph/pass/constant_folding.hpp" #include "openvino/runtime/core.hpp" #include "openvino/runtime/executable_network.hpp" +#include "openvino/util/file_util.hpp" #include "xml_parse_utils.h" using namespace InferenceEngine::PluginConfigParams; @@ -48,9 +49,9 @@ std::string parseXmlConfig(const std::string& xmlFile) { std::string xmlConfigFile_ = xmlFile; if (xmlConfigFile_.empty()) { // register plugins from default plugins.xml config - FileUtils::FilePath xmlConfigFileDefault = - FileUtils::makePath(ie::getInferenceEngineLibraryPath(), FileUtils::toFilePath("plugins.xml")); - xmlConfigFile_ = FileUtils::fromFilePath(xmlConfigFileDefault); + ov::util::FilePath xmlConfigFileDefault = + FileUtils::makePath(ie::getInferenceEngineLibraryPath(), ov::util::to_file_path("plugins.xml")); + xmlConfigFile_ = ov::util::from_file_path(xmlConfigFileDefault); } return xmlConfigFile_; } @@ -165,9 +166,9 @@ class CoreImpl : public ie::ICore, public std::enable_shared_from_this defaultConfig; - std::vector listOfExtentions; + std::vector listOfExtentions; }; mutable std::unordered_set opsetNames; @@ -374,7 +375,7 @@ public: FOREACH_CHILD (pluginNode, devicesNode, "plugin") { std::string deviceName = GetStrAttr(pluginNode, "name"); - FileUtils::FilePath pluginPath = FileUtils::toFilePath(GetStrAttr(pluginNode, "location").c_str()); + ov::util::FilePath pluginPath = ov::util::to_file_path(GetStrAttr(pluginNode, "location").c_str()); if (deviceName.find('.') != std::string::npos) { IE_THROW() << "Device name must not contain dot '.' symbol"; @@ -382,7 +383,7 @@ public: // append IR library path for default IE plugins { - FileUtils::FilePath absFilePath = FileUtils::makePath(ie::getInferenceEngineLibraryPath(), pluginPath); + ov::util::FilePath absFilePath = FileUtils::makePath(ie::getInferenceEngineLibraryPath(), pluginPath); if (FileUtils::fileExist(absFilePath)) pluginPath = absFilePath; } @@ -401,12 +402,12 @@ public: // check extensions auto extensionsNode = pluginNode.child("extensions"); - std::vector listOfExtentions; + std::vector listOfExtentions; if (extensionsNode) { FOREACH_CHILD (extensionNode, extensionsNode, "extension") { - FileUtils::FilePath extensionLocation = - FileUtils::toFilePath(GetStrAttr(extensionNode, "location").c_str()); + ov::util::FilePath extensionLocation = + ov::util::to_file_path(GetStrAttr(extensionNode, "location").c_str()); listOfExtentions.push_back(extensionLocation); } } @@ -695,7 +696,7 @@ public: return result; } catch (const ie::Exception& ex) { - IE_THROW() << "Failed to create plugin " << FileUtils::fromFilePath(desc.libraryLocation) + IE_THROW() << "Failed to create plugin " << ov::util::from_file_path(desc.libraryLocation) << " for device " << deviceName << "\n" << "Please, check your environment\n" << ex.what() << "\n"; @@ -736,11 +737,11 @@ public: } // append IR library path for default IE plugins - FileUtils::FilePath pluginPath; + ov::util::FilePath pluginPath; { - pluginPath = FileUtils::makePluginLibraryName({}, FileUtils::toFilePath(pluginName.c_str())); + pluginPath = FileUtils::makePluginLibraryName({}, ov::util::to_file_path(pluginName.c_str())); - FileUtils::FilePath absFilePath = FileUtils::makePath(ie::getInferenceEngineLibraryPath(), pluginPath); + ov::util::FilePath absFilePath = FileUtils::makePath(ie::getInferenceEngineLibraryPath(), pluginPath); if (FileUtils::fileExist(absFilePath)) pluginPath = absFilePath; } @@ -980,7 +981,7 @@ std::map Core::GetVersions(const std::string& deviceName) #ifdef ENABLE_UNICODE_PATH_SUPPORT CNNNetwork Core::ReadNetwork(const std::wstring& modelPath, const std::wstring& binPath) const { - return ReadNetwork(FileUtils::wStringtoMBCSstringChar(modelPath), FileUtils::wStringtoMBCSstringChar(binPath)); + return ReadNetwork(ov::util::wstring_to_string(modelPath), ov::util::wstring_to_string(binPath)); } #endif @@ -1234,8 +1235,7 @@ std::map Core::get_versions(const std::string& deviceN #ifdef ENABLE_UNICODE_PATH_SUPPORT std::shared_ptr Core::read_model(const std::wstring& modelPath, const std::wstring& binPath) const { - return _impl - ->ReadNetwork(FileUtils::wStringtoMBCSstringChar(modelPath), FileUtils::wStringtoMBCSstringChar(binPath)) + return _impl->ReadNetwork(ov::util::wstring_to_string(modelPath), ov::util::wstring_to_string(binPath)) .getFunction(); } #endif diff --git a/inference-engine/src/inference_engine/src/ie_network_reader.cpp b/inference-engine/src/inference_engine/src/ie_network_reader.cpp index f9c796c93f1..cf182c26d33 100644 --- a/inference-engine/src/inference_engine/src/ie_network_reader.cpp +++ b/inference-engine/src/inference_engine/src/ie_network_reader.cpp @@ -45,13 +45,13 @@ class Reader : public IReader { InferenceEngine::details::SOPointer getReaderPtr() { std::call_once(readFlag, [&]() { - FileUtils::FilePath libraryName = FileUtils::toFilePath(location); - FileUtils::FilePath readersLibraryPath = + ov::util::FilePath libraryName = ov::util::to_file_path(location); + ov::util::FilePath readersLibraryPath = FileUtils::makePluginLibraryName(getInferenceEngineLibraryPath(), libraryName); if (!FileUtils::fileExist(readersLibraryPath)) { IE_THROW() << "Please, make sure that Inference Engine ONNX reader library " - << FileUtils::fromFilePath(::FileUtils::makePluginLibraryName({}, libraryName)) << " is in " + << ov::util::from_file_path(::FileUtils::makePluginLibraryName({}, libraryName)) << " is in " << getIELibraryPath(); } ptr = {readersLibraryPath}; @@ -111,8 +111,8 @@ void registerReaders() { // TODO: Read readers info from XML auto create_if_exists = [](const std::string name, const std::string library_name) { - FileUtils::FilePath libraryName = FileUtils::toFilePath(library_name); - FileUtils::FilePath readersLibraryPath = + ov::util::FilePath libraryName = ov::util::to_file_path(library_name); + ov::util::FilePath readersLibraryPath = FileUtils::makePluginLibraryName(getInferenceEngineLibraryPath(), libraryName); if (!FileUtils::fileExist(readersLibraryPath)) @@ -165,7 +165,7 @@ CNNNetwork details::ReadNetwork(const std::string& modelPath, // Fix unicode name #if defined(ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32) - std::wstring model_path = FileUtils::multiByteCharToWString(modelPath.c_str()); + std::wstring model_path = ov::util::string_to_wstring(modelPath.c_str()); #else std::string model_path = modelPath; #endif @@ -201,7 +201,7 @@ CNNNetwork details::ReadNetwork(const std::string& modelPath, if (!bPath.empty()) { // Open weights file #if defined(ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32) - std::wstring weights_path = FileUtils::multiByteCharToWString(bPath.c_str()); + std::wstring weights_path = ov::util::string_to_wstring(bPath.c_str()); #else std::string weights_path = bPath; #endif @@ -238,7 +238,7 @@ CNNNetwork details::ReadNetwork(const std::string& modelPath, ngraph::frontend::InputModel::Ptr inputModel; if (!binPath.empty()) { #if defined(ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32) - std::wstring weights_path = FileUtils::multiByteCharToWString(binPath.c_str()); + std::wstring weights_path = ov::util::string_to_wstring(binPath.c_str()); #else std::string weights_path = binPath; #endif diff --git a/inference-engine/src/inference_engine/src/os/lin/lin_shared_object_loader.cpp b/inference-engine/src/inference_engine/src/os/lin/lin_shared_object_loader.cpp index cf1d6c0af28..052bbfbb1fe 100644 --- a/inference-engine/src/inference_engine/src/os/lin/lin_shared_object_loader.cpp +++ b/inference-engine/src/inference_engine/src/os/lin/lin_shared_object_loader.cpp @@ -8,6 +8,7 @@ #include "details/ie_so_loader.h" #include "file_utils.h" +#include "openvino/util/file_util.hpp" #include "shared_object.hpp" namespace ov { @@ -28,7 +29,7 @@ std::shared_ptr load_shared_object(const char* path) { #ifdef ENABLE_UNICODE_PATH_SUPPORT std::shared_ptr load_shared_object(const wchar_t* path) { - return load_shared_object(FileUtils::wStringtoMBCSstringChar(path).c_str()); + return load_shared_object(ov::util::wstring_to_string(path).c_str()); } #endif // ENABLE_UNICODE_PATH_SUPPORT @@ -57,7 +58,7 @@ struct SharedObjectLoader::Impl { explicit Impl(const char* pluginName) : shared_object{ov::runtime::load_shared_object(pluginName)} {} #ifdef ENABLE_UNICODE_PATH_SUPPORT - explicit Impl(const wchar_t* pluginName) : Impl(FileUtils::wStringtoMBCSstringChar(pluginName).c_str()) {} + explicit Impl(const wchar_t* pluginName) : Impl(ov::util::wstring_to_string(pluginName).c_str()) {} #endif // ENABLE_UNICODE_PATH_SUPPORT void* get_symbol(const char* symbolName) const { @@ -93,4 +94,4 @@ std::shared_ptr SharedObjectLoader::get() const { } } // namespace details -} // namespace InferenceEngine \ No newline at end of file +} // namespace InferenceEngine diff --git a/inference-engine/src/inference_engine/src/os/win/win_shared_object_loader.cpp b/inference-engine/src/inference_engine/src/os/win/win_shared_object_loader.cpp index 525ab076094..5e36e2f04cf 100644 --- a/inference-engine/src/inference_engine/src/os/win/win_shared_object_loader.cpp +++ b/inference-engine/src/inference_engine/src/os/win/win_shared_object_loader.cpp @@ -159,7 +159,7 @@ std::shared_ptr load_shared_object(const wchar_t* path) { } if (!shared_object) { char cwd[1024]; - IE_THROW() << "Cannot load library '" << FileUtils::wStringtoMBCSstringChar(std::wstring(path)) << "': " << GetLastError() + IE_THROW() << "Cannot load library '" << ov::util::wstring_to_string(std::wstring(path)) << "': " << GetLastError() << " from cwd: " << _getcwd(cwd, sizeof(cwd)); } return {shared_object, diff --git a/inference-engine/src/offline_transformations/src/pruning/pruning.cpp b/inference-engine/src/offline_transformations/src/pruning/pruning.cpp index ad7f410b8f3..9d80dcb1403 100644 --- a/inference-engine/src/offline_transformations/src/pruning/pruning.cpp +++ b/inference-engine/src/offline_transformations/src/pruning/pruning.cpp @@ -2,14 +2,14 @@ // SPDX-License-Identifier: Apache-2.0 // -#include - #include "pruning.hpp" -#include "mask_attribute.hpp" -#include -#include +#include #include +#include +#include + +#include "mask_attribute.hpp" NGRAPH_RTTI_DEFINITION(ngraph::pass::Pruning, "Pruning", 0); @@ -21,8 +21,7 @@ bool ngraph::pass::Pruning::run_on_function(std::shared_ptr f) { manager.register_pass(); manager.register_pass(); - -#ifdef NGRAPH_DEBUG_ENABLE +#ifdef OPENVINO_DEBUG_ENABLE // VisualizeTree modifier helps to print Masks and mark nodes with masks /* auto modifier = [](const Node& node, std::vector& attributes) { @@ -57,11 +56,11 @@ bool ngraph::pass::Pruning::run_on_function(std::shared_ptr f) { manager.register_pass(); -#ifdef NGRAPH_DEBUG_ENABLE +#ifdef OPENVINO_DEBUG_ENABLE // Uncomment following line and change path to resulting svg file // manager.register_pass("/tmp/after.svg"); #endif manager.run_passes(f); return true; -} \ No newline at end of file +} diff --git a/inference-engine/src/plugin_api/file_utils.h b/inference-engine/src/plugin_api/file_utils.h index 74883f11635..cad183071b2 100644 --- a/inference-engine/src/plugin_api/file_utils.h +++ b/inference-engine/src/plugin_api/file_utils.h @@ -15,72 +15,10 @@ #include "ie_api.h" #include "details/ie_so_pointer.hpp" +#include "openvino/util/file_util.hpp" namespace FileUtils { -#ifdef ENABLE_UNICODE_PATH_SUPPORT - -/** - * @brief Conversion from wide character string to a single-byte chain. - * @param wstr A wide-char string - * @return A multi-byte string - */ -INFERENCE_ENGINE_API_CPP(std::string) wStringtoMBCSstringChar(const std::wstring& wstr); - -/** - * @brief Conversion from single-byte chain to wide character string. - * @param str A null-terminated string - * @return A wide-char string - */ -INFERENCE_ENGINE_API_CPP(std::wstring) multiByteCharToWString(const char* str); - -#endif // ENABLE_UNICODE_PATH_SUPPORT - -template struct FileTraits; - -#ifdef _WIN32 - -/// @brief File path separator -const char FileSeparator = '\\'; - -template<> struct FileTraits { - constexpr static const auto FileSeparator = ::FileUtils::FileSeparator; - static std::string PluginLibraryPrefix() { return { }; } - static std::string PluginLibraryExt() { return { "dll" }; } -}; -template<> struct FileTraits { - constexpr static const auto FileSeparator = L'\\'; - static std::wstring PluginLibraryPrefix() { return { }; } - static std::wstring PluginLibraryExt() { return { L"dll" }; } -}; -#elif defined __APPLE__ -/// @brief File path separator -const char FileSeparator = '/'; -template<> struct FileTraits { - constexpr static const auto FileSeparator = ::FileUtils::FileSeparator; - static std::string PluginLibraryPrefix() { return { "lib" }; } - static std::string PluginLibraryExt() { return { "so" }; } -}; -template<> struct FileTraits { - constexpr static const auto FileSeparator = L'/'; - static std::wstring PluginLibraryPrefix() { return { L"lib" }; } - static std::wstring PluginLibraryExt() { return { L"so" }; } -}; -#else -/// @brief File path separator -const char FileSeparator = '/'; -template<> struct FileTraits { - constexpr static const auto FileSeparator = ::FileUtils::FileSeparator; - static std::string PluginLibraryPrefix() { return { "lib" }; } - static std::string PluginLibraryExt() { return { "so" }; } -}; -template<> struct FileTraits { - constexpr static const auto FileSeparator = L'/'; - static std::wstring PluginLibraryPrefix() { return { L"lib" }; } - static std::wstring PluginLibraryExt() { return { L"so" }; } -}; -#endif - /** * @brief Interface function to get absolute path of file * @ingroup ie_dev_api_file_utils @@ -125,7 +63,7 @@ INFERENCE_ENGINE_API(long long) fileSize(const char *fileName); * @return { description_of_the_return_value } */ inline long long fileSize(const wchar_t* fileName) { - return fileSize(::FileUtils::wStringtoMBCSstringChar(fileName).c_str()); + return fileSize(::ov::util::wstring_to_string(fileName).c_str()); } #endif // ENABLE_UNICODE_PATH_SUPPORT @@ -175,13 +113,9 @@ template makePath(const std::basic_string &folder, const std::basic_string &file) { if (folder.empty()) return file; - return folder + FileTraits::FileSeparator + file; + return folder + ov::util::FileTraits::file_separator + file; } -template struct DotSymbol; -template <> struct DotSymbol { constexpr static const char value = '.'; }; -template <> struct DotSymbol { constexpr static const wchar_t value = L'.'; }; - /** * @brief CPP Interface function to extract extension from filename * @ingroup ie_dev_api_file_utils @@ -190,7 +124,7 @@ template <> struct DotSymbol { constexpr static const wchar_t value = L */ template > inline std::basic_string fileExt(const std::basic_string &filename) { - auto pos = filename.rfind(DotSymbol::value); + auto pos = filename.rfind(ov::util::FileTraits::dot_symbol); if (pos == std::string::npos) return {}; return filename.substr(pos + 1); @@ -198,38 +132,12 @@ inline std::basic_string fileExt(const std::basic_string &filename) { template > inline std::basic_string makePluginLibraryName(const std::basic_string &path, const std::basic_string &input) { - std::basic_string separator(1, FileTraits::FileSeparator); + std::basic_string separator(1, ov::util::FileTraits::file_separator); if (path.empty()) separator = {}; - return path + separator + FileTraits::PluginLibraryPrefix() + input + DotSymbol::value + FileTraits::PluginLibraryExt(); + return path + separator + ov::util::FileTraits::library_prefix() + input + ov::util::FileTraits::dot_symbol + ov::util::FileTraits::library_ext(); } -#ifdef ENABLE_UNICODE_PATH_SUPPORT - -using FilePath = std::wstring; - -inline std::string fromFilePath(const FilePath & path) { - return ::FileUtils::wStringtoMBCSstringChar(path); -} - -inline FilePath toFilePath(const std::string & path) { - return ::FileUtils::multiByteCharToWString(path.c_str()); -} - -#else - -using FilePath = std::string; - -inline std::string fromFilePath(const FilePath & path) { - return path; -} - -inline FilePath toFilePath(const std::string & path) { - return path; -} - -#endif // ENABLE_UNICODE_PATH_SUPPORT - } // namespace FileUtils // clang-format on @@ -251,13 +159,13 @@ INFERENCE_ENGINE_API_CPP(std::string) getIELibraryPath(); */ INFERENCE_ENGINE_API_CPP(std::wstring) getIELibraryPathW(); -inline ::FileUtils::FilePath getInferenceEngineLibraryPath() { +inline ::ov::util::FilePath getInferenceEngineLibraryPath() { return getIELibraryPathW(); } #else -inline ::FileUtils::FilePath getInferenceEngineLibraryPath() { +inline ::ov::util::FilePath getInferenceEngineLibraryPath() { return getIELibraryPath(); } diff --git a/inference-engine/src/plugin_api/xml_parse_utils.h b/inference-engine/src/plugin_api/xml_parse_utils.h index 7ce0ee46927..af70ff03bd6 100644 --- a/inference-engine/src/plugin_api/xml_parse_utils.h +++ b/inference-engine/src/plugin_api/xml_parse_utils.h @@ -255,7 +255,7 @@ struct parse_result { */ inline parse_result ParseXml(const char* file_path) { #ifdef ENABLE_UNICODE_PATH_SUPPORT - std::wstring wFilePath = FileUtils::multiByteCharToWString(file_path); + std::wstring wFilePath = ov::util::string_to_wstring(file_path); const wchar_t* resolvedFilepath = wFilePath.c_str(); #else const char* resolvedFilepath = file_path; diff --git a/inference-engine/src/preprocessing/CMakeLists.txt b/inference-engine/src/preprocessing/CMakeLists.txt index a118f638c6a..feee8b4cb84 100644 --- a/inference-engine/src/preprocessing/CMakeLists.txt +++ b/inference-engine/src/preprocessing/CMakeLists.txt @@ -123,7 +123,7 @@ ie_add_vs_version_file(NAME ${TARGET_NAME} set_ie_threading_interface_for(${TARGET_NAME}) -target_link_libraries(${TARGET_NAME} PRIVATE fluid openvino::itt +target_link_libraries(${TARGET_NAME} PRIVATE fluid openvino::itt openvino::util PUBLIC inference_engine) target_include_directories(${TARGET_NAME} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/inference-engine/src/preprocessing/ie_preprocess_data.hpp b/inference-engine/src/preprocessing/ie_preprocess_data.hpp index fab055000ca..24687d074b3 100644 --- a/inference-engine/src/preprocessing/ie_preprocess_data.hpp +++ b/inference-engine/src/preprocessing/ie_preprocess_data.hpp @@ -88,12 +88,12 @@ public: using PreProcessDataPtr = InferenceEngine::details::SOPointer; inline PreProcessDataPtr CreatePreprocDataHelper() { - FileUtils::FilePath libraryName = FileUtils::toFilePath(std::string("inference_engine_preproc") + std::string(IE_BUILD_POSTFIX)); - FileUtils::FilePath preprocLibraryPath = FileUtils::makePluginLibraryName(getInferenceEngineLibraryPath(), libraryName); + ov::util::FilePath libraryName = ov::util::to_file_path(std::string("inference_engine_preproc") + std::string(IE_BUILD_POSTFIX)); + ov::util::FilePath preprocLibraryPath = FileUtils::makePluginLibraryName(getInferenceEngineLibraryPath(), libraryName); if (!FileUtils::fileExist(preprocLibraryPath)) { IE_THROW() << "Please, make sure that pre-processing library " - << FileUtils::fromFilePath(::FileUtils::makePluginLibraryName({}, libraryName)) << " is in " + << ov::util::from_file_path(::FileUtils::makePluginLibraryName({}, libraryName)) << " is in " << getIELibraryPath(); } return {preprocLibraryPath}; diff --git a/inference-engine/src/readers/ir_reader_v7/ie_cnn_net_reader_impl.cpp b/inference-engine/src/readers/ir_reader_v7/ie_cnn_net_reader_impl.cpp index a839d2035b3..d7c1d2d9f66 100644 --- a/inference-engine/src/readers/ir_reader_v7/ie_cnn_net_reader_impl.cpp +++ b/inference-engine/src/readers/ir_reader_v7/ie_cnn_net_reader_impl.cpp @@ -72,7 +72,7 @@ void readAllFile(const std::string& string_file_name, void* buffer, size_t maxSi std::ifstream inputFile; #if defined(ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32) - std::wstring file_name = FileUtils::multiByteCharToWString(string_file_name.c_str()); + std::wstring file_name = ov::util::string_to_wstring(string_file_name.c_str()); #else std::string file_name = string_file_name; #endif diff --git a/inference-engine/tests/functional/inference_engine/CMakeLists.txt b/inference-engine/tests/functional/inference_engine/CMakeLists.txt index b6a91d48fd6..ef3dc6e7421 100644 --- a/inference-engine/tests/functional/inference_engine/CMakeLists.txt +++ b/inference-engine/tests/functional/inference_engine/CMakeLists.txt @@ -13,6 +13,7 @@ set(LINK_LIBRARIES lptNgraphFunctions inference_engine_transformations openvino::itt + openvino::util openvino::conditional_compilation sharedTestClasses inference_engine_snippets diff --git a/inference-engine/tests/functional/inference_engine/core_threading_tests.cpp b/inference-engine/tests/functional/inference_engine/core_threading_tests.cpp index 791cb220093..c59e3538bbc 100644 --- a/inference-engine/tests/functional/inference_engine/core_threading_tests.cpp +++ b/inference-engine/tests/functional/inference_engine/core_threading_tests.cpp @@ -95,16 +95,16 @@ TEST_F(CoreThreadingTests, RegisterPlugins) { auto getPluginXml = [&] () -> std::tuple { std::string indexStr = std::to_string(index++); std::string pluginsXML = InferenceEngine::getIELibraryPath() + - FileUtils::FileSeparator + + ov::util::FileTraits::file_separator + "test_plugins" + indexStr + ".xml"; std::ofstream file(pluginsXML); file << "::PluginLibraryPrefix(); + file << ov::util::FileTraits::library_prefix(); file << "mock_engine"; file << IE_BUILD_POSTFIX; - file << FileUtils::DotSymbol::value; - file << FileUtils::FileTraits::PluginLibraryExt(); + file << ov::util::FileTraits::dot_symbol; + file << ov::util::FileTraits::library_ext(); file << "\" name=\""; file << indexStr; file << "\">"; diff --git a/inference-engine/tests/functional/inference_engine/ngraph_reader/net_reader_test.cpp b/inference-engine/tests/functional/inference_engine/ngraph_reader/net_reader_test.cpp index d32b6ab07de..c9868fc01df 100644 --- a/inference-engine/tests/functional/inference_engine/ngraph_reader/net_reader_test.cpp +++ b/inference-engine/tests/functional/inference_engine/ngraph_reader/net_reader_test.cpp @@ -85,12 +85,12 @@ TEST_P(NetReaderTest, ReadCorrectModelWithWeightsUnicodePath) { is_copy_successfully = CommonTestUtils::copyFile(_modelPath, modelPath); if (!is_copy_successfully) { FAIL() << "Unable to copy from '" << _modelPath << "' to '" - << FileUtils::wStringtoMBCSstringChar(modelPath) << "'"; + << ov::util::wstring_to_string(modelPath) << "'"; } is_copy_successfully = CommonTestUtils::copyFile(_weightsPath, weightsPath); if (!is_copy_successfully) { FAIL() << "Unable to copy from '" << _weightsPath << "' to '" - << FileUtils::wStringtoMBCSstringChar(weightsPath) << "'"; + << ov::util::wstring_to_string(weightsPath) << "'"; } GTEST_COUT << "Test " << testIndex << std::endl; InferenceEngine::Core ie; diff --git a/inference-engine/tests/functional/inference_engine/onnx_reader/onnx_reader_external_data.cpp b/inference-engine/tests/functional/inference_engine/onnx_reader/onnx_reader_external_data.cpp index 70b0e7537cc..395274a627d 100644 --- a/inference-engine/tests/functional/inference_engine/onnx_reader/onnx_reader_external_data.cpp +++ b/inference-engine/tests/functional/inference_engine/onnx_reader/onnx_reader_external_data.cpp @@ -86,7 +86,7 @@ TEST(ONNX_Reader_Tests, ImportModelWithExternalDataFromWstringNamedFile) { bool is_copy_successfully = CommonTestUtils::copyFile(win_dir_path, wmodel); if (!is_copy_successfully) { FAIL() << "Unable to copy from '" << win_dir_path << "' to '" - << FileUtils::wStringtoMBCSstringChar(wmodel) << "'"; + << ov::util::wstring_to_string(wmodel) << "'"; } auto cnnNetwork = ie.ReadNetwork(wmodel, L""); diff --git a/inference-engine/tests/functional/inference_engine/paddle_reader/read_paddle_model_test.cpp b/inference-engine/tests/functional/inference_engine/paddle_reader/read_paddle_model_test.cpp index ead189023f2..b3c21d19cc4 100644 --- a/inference-engine/tests/functional/inference_engine/paddle_reader/read_paddle_model_test.cpp +++ b/inference-engine/tests/functional/inference_engine/paddle_reader/read_paddle_model_test.cpp @@ -55,7 +55,7 @@ TEST(PDPD_Reader_Tests, ImportBasicModelToCoreWstring) { bool is_copy_successfully = CommonTestUtils::copyFile(win_dir_path, wmodel); if (!is_copy_successfully) { FAIL() << "Unable to copy from '" << win_dir_path << "' to '" - << FileUtils::wStringtoMBCSstringChar(wmodel) << "'"; + << ov::util::wstring_to_string(wmodel) << "'"; } InferenceEngine::Core ie; auto cnnNetwork = ie.ReadNetwork(wmodel); diff --git a/inference-engine/tests/functional/plugin/gpu/behavior/cache.cpp b/inference-engine/tests/functional/plugin/gpu/behavior/cache.cpp index 389f2b4f57d..a79e7f0a7db 100644 --- a/inference-engine/tests/functional/plugin/gpu/behavior/cache.cpp +++ b/inference-engine/tests/functional/plugin/gpu/behavior/cache.cpp @@ -60,7 +60,7 @@ TEST_F(CompiledKernelsCacheTest, CanCreateCacheDirAndDumpBinariesUnicodePath) { std::wstring cache_path_w = CommonTestUtils::addUnicodePostfixToPath(cache_path, postfix); try { - auto cache_path_mb = FileUtils::wStringtoMBCSstringChar(cache_path_w); + auto cache_path_mb = ov::util::wstring_to_string(cache_path_w); std::map config = {{ CONFIG_KEY(CACHE_DIR), cache_path_mb }}; // Load CNNNetwork to target plugins auto execNet = ie->LoadNetwork(cnnNet, "GPU", config); diff --git a/inference-engine/tests/functional/plugin/shared/CMakeLists.txt b/inference-engine/tests/functional/plugin/shared/CMakeLists.txt index 5a270deee45..2b55ae93aef 100644 --- a/inference-engine/tests/functional/plugin/shared/CMakeLists.txt +++ b/inference-engine/tests/functional/plugin/shared/CMakeLists.txt @@ -26,6 +26,7 @@ addIeTarget( lptNgraphFunctions sharedTestClasses PRIVATE + openvino::util inference_engine_transformations DEPENDENCIES inference_engine diff --git a/inference-engine/tests/functional/plugin/shared/include/behavior/core_integration.hpp b/inference-engine/tests/functional/plugin/shared/include/behavior/core_integration.hpp index 27914608bbf..99525798c40 100644 --- a/inference-engine/tests/functional/plugin/shared/include/behavior/core_integration.hpp +++ b/inference-engine/tests/functional/plugin/shared/include/behavior/core_integration.hpp @@ -280,14 +280,14 @@ TEST_P(IEClassBasicTestP, smoke_registerPluginsXMLUnicodePath) { bool is_copy_successfully; is_copy_successfully = CommonTestUtils::copyFile(pluginXML, pluginsXmlW); if (!is_copy_successfully) { - FAIL() << "Unable to copy from '" << pluginXML << "' to '" << ::FileUtils::wStringtoMBCSstringChar(pluginsXmlW) << "'"; + FAIL() << "Unable to copy from '" << pluginXML << "' to '" << ::ov::util::wstring_to_string(pluginsXmlW) << "'"; } GTEST_COUT << "Test " << testIndex << std::endl; Core ie = createCoreWithTemplate(); GTEST_COUT << "Core created " << testIndex << std::endl; - ASSERT_NO_THROW(ie.RegisterPlugins(::FileUtils::wStringtoMBCSstringChar(pluginsXmlW))); + ASSERT_NO_THROW(ie.RegisterPlugins(::ov::util::wstring_to_string(pluginsXmlW))); CommonTestUtils::removeFile(pluginsXmlW); #if defined __linux__ && !defined(__APPLE__) ASSERT_NO_THROW(ie.GetVersions("mock")); // from pluginXML diff --git a/inference-engine/tests/functional/plugin/shared/include/behavior/ov_core_integration.hpp b/inference-engine/tests/functional/plugin/shared/include/behavior/ov_core_integration.hpp index 080bcf948e1..e15d29d8dfb 100644 --- a/inference-engine/tests/functional/plugin/shared/include/behavior/ov_core_integration.hpp +++ b/inference-engine/tests/functional/plugin/shared/include/behavior/ov_core_integration.hpp @@ -276,14 +276,14 @@ TEST_P(OVClassBasicTestP, smoke_registerPluginsXMLUnicodePath) { is_copy_successfully = CommonTestUtils::copyFile(pluginXML, pluginsXmlW); if (!is_copy_successfully) { FAIL() << "Unable to copy from '" << pluginXML << "' to '" - << ::FileUtils::wStringtoMBCSstringChar(pluginsXmlW) << "'"; + << ::ov::util::wstring_to_string(pluginsXmlW) << "'"; } GTEST_COUT << "Test " << testIndex << std::endl; ov::runtime::Core ie = createCoreWithTemplate(); GTEST_COUT << "Core created " << testIndex << std::endl; - ASSERT_NO_THROW(ie.register_plugins(::FileUtils::wStringtoMBCSstringChar(pluginsXmlW))); + ASSERT_NO_THROW(ie.register_plugins(::ov::util::wstring_to_string(pluginsXmlW))); CommonTestUtils::removeFile(pluginsXmlW); # if defined __linux__ && !defined(__APPLE__) ASSERT_NO_THROW(ie.get_versions("mock")); // from pluginXML diff --git a/inference-engine/tests/ie_test_utils/common_test_utils/CMakeLists.txt b/inference-engine/tests/ie_test_utils/common_test_utils/CMakeLists.txt index 9da03836041..833950c3d98 100644 --- a/inference-engine/tests/ie_test_utils/common_test_utils/CMakeLists.txt +++ b/inference-engine/tests/ie_test_utils/common_test_utils/CMakeLists.txt @@ -19,6 +19,8 @@ function(add_common_utils ADD_TARGET_NAME) gtest gtest_main inference_engine_transformations + PRIVATE + openvino::util ) # USE_STATIC_IE is passed diff --git a/inference-engine/tests/ie_test_utils/common_test_utils/file_utils.cpp b/inference-engine/tests/ie_test_utils/common_test_utils/file_utils.cpp index a272a97741a..cd71191637f 100644 --- a/inference-engine/tests/ie_test_utils/common_test_utils/file_utils.cpp +++ b/inference-engine/tests/ie_test_utils/common_test_utils/file_utils.cpp @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // -#include +#include #include #ifdef __APPLE__ @@ -20,7 +20,6 @@ # include #endif -NGRAPH_SUPPRESS_DEPRECATED_START namespace CommonTestUtils { std::string getExecutableDirectory() { @@ -41,11 +40,11 @@ std::string getExecutableDirectory() { throw "Can't get test executable path name"; } path = std::string(buffer, len); - return ngraph::file_util::get_directory(path); + return ov::util::get_directory(path); } std::string getModelFromTestModelZoo(const std::string & relModelPath) { - return ngraph::file_util::path_join(CommonTestUtils::getExecutableDirectory(), relModelPath); + return ov::util::path_join({CommonTestUtils::getExecutableDirectory(), relModelPath}); } } // namespace CommonTestUtils diff --git a/inference-engine/tests/ie_test_utils/common_test_utils/unicode_utils.hpp b/inference-engine/tests/ie_test_utils/common_test_utils/unicode_utils.hpp index ce0e31af860..7cb7242c96a 100644 --- a/inference-engine/tests/ie_test_utils/common_test_utils/unicode_utils.hpp +++ b/inference-engine/tests/ie_test_utils/common_test_utils/unicode_utils.hpp @@ -26,13 +26,13 @@ inline void fixSlashes(std::wstring &str) { } inline std::wstring stringToWString(std::string input) { - return ::FileUtils::multiByteCharToWString(input.c_str()); + return ::ov::util::string_to_wstring(input.c_str()); } inline bool copyFile(std::wstring source_path, std::wstring dest_path) { #ifndef _WIN32 - std::ifstream source(FileUtils::wStringtoMBCSstringChar(source_path), std::ios::binary); - std::ofstream dest(FileUtils::wStringtoMBCSstringChar(dest_path), std::ios::binary); + std::ifstream source(ov::util::wstring_to_string(source_path), std::ios::binary); + std::ofstream dest(ov::util::wstring_to_string(dest_path), std::ios::binary); #else fixSlashes(source_path); fixSlashes(dest_path); @@ -70,7 +70,7 @@ inline void removeFile(std::wstring path) { #ifdef _WIN32 result = _wremove(path.c_str()); #else - result = remove(FileUtils::wStringtoMBCSstringChar(path).c_str()); + result = remove(ov::util::wstring_to_string(path).c_str()); #endif } (void)result; @@ -107,8 +107,8 @@ inline int removeFilesWithExt(std::wstring path, std::wstring ext) { } #else struct dirent *ent; - auto path_mb = FileUtils::wStringtoMBCSstringChar(path); - auto ext_mb = FileUtils::wStringtoMBCSstringChar(ext); + auto path_mb = ov::util::wstring_to_string(path); + auto ext_mb = ov::util::wstring_to_string(ext); DIR *dir = opendir(path_mb.c_str()); if (dir != nullptr) { while ((ent = readdir(dir)) != NULL) { @@ -136,7 +136,7 @@ inline int removeDir(std::wstring path) { #ifdef _WIN32 result = _wrmdir(path.c_str()); #else - result = rmdir(FileUtils::wStringtoMBCSstringChar(path).c_str()); + result = rmdir(ov::util::wstring_to_string(path).c_str()); #endif } return result; @@ -150,7 +150,7 @@ inline bool directoryExists(const std::wstring &path) { } #else struct stat sb; - if (stat(FileUtils::wStringtoMBCSstringChar(path).c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) { + if (stat(ov::util::wstring_to_string(path).c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) { return true; } #endif diff --git a/inference-engine/tests/unit/frontends/onnx_import/CMakeLists.txt b/inference-engine/tests/unit/frontends/onnx_import/CMakeLists.txt index b7a8f3ae100..cc4ce43c727 100644 --- a/inference-engine/tests/unit/frontends/onnx_import/CMakeLists.txt +++ b/inference-engine/tests/unit/frontends/onnx_import/CMakeLists.txt @@ -12,6 +12,7 @@ addIeTargetTest( gtest_main commonTestUtils onnx_ngraph_frontend + openvino::util DEFINES ONNX_MODELS_DIR=\"${TEST_MODEL_ZOO}/onnx_import\" ADD_CPPLINT diff --git a/inference-engine/tests/unit/frontends/onnx_import/onnx_importer_test.cpp b/inference-engine/tests/unit/frontends/onnx_import/onnx_importer_test.cpp index a86ded8b8d4..5fc10c6e073 100644 --- a/inference-engine/tests/unit/frontends/onnx_import/onnx_importer_test.cpp +++ b/inference-engine/tests/unit/frontends/onnx_import/onnx_importer_test.cpp @@ -8,14 +8,12 @@ #include #include "common_test_utils/file_utils.hpp" -#include "ngraph/file_util.hpp" +#include "openvino/util/file_util.hpp" #include "onnx_import/onnx.hpp" -NGRAPH_SUPPRESS_DEPRECATED_START - TEST(ONNX_Importer_Tests, ImportBasicModel) { auto model_file_path = CommonTestUtils::getModelFromTestModelZoo( - ngraph::file_util::path_join(ONNX_MODELS_DIR, "add_abc_initializers.onnx")); + ov::util::path_join({ONNX_MODELS_DIR, "add_abc_initializers.onnx"})); auto function = ngraph::onnx_import::import_onnx_model(model_file_path); int count_additions = 0; @@ -40,7 +38,7 @@ TEST(ONNX_Importer_Tests, ImportBasicModel) { TEST(ONNX_Importer_Tests, ImportModelWithFusedOp) { auto model_file_path = CommonTestUtils::getModelFromTestModelZoo( - ngraph::file_util::path_join(ONNX_MODELS_DIR, "selu.onnx")); + ov::util::path_join({ONNX_MODELS_DIR, "selu.onnx"})); auto function = ngraph::onnx_import::import_onnx_model(model_file_path); int count_selu = 0; @@ -65,7 +63,7 @@ TEST(ONNX_Importer_Tests, ImportModelWithFusedOp) { TEST(ONNX_Importer_Tests, ImportModelWithMultiOutput) { auto model_file_path = CommonTestUtils::getModelFromTestModelZoo( - ngraph::file_util::path_join(ONNX_MODELS_DIR, "topk.onnx")); + ov::util::path_join({ONNX_MODELS_DIR, "topk.onnx"})); auto function = ngraph::onnx_import::import_onnx_model(model_file_path); int count_topk = 0; @@ -93,7 +91,7 @@ TEST(ONNX_Importer_Tests, ImportModelWithMultiOutput) { TEST(ONNX_Importer_Tests, ImportModelWithNotSupportedOp) { auto model_file_path = CommonTestUtils::getModelFromTestModelZoo( - ngraph::file_util::path_join(ONNX_MODELS_DIR, "not_supported.onnx")); + ov::util::path_join({ONNX_MODELS_DIR, "not_supported.onnx"})); try { auto function = ngraph::onnx_import::import_onnx_model(model_file_path); FAIL() << "Any expection was thrown despite the ONNX model is not supported"; @@ -108,7 +106,7 @@ TEST(ONNX_Importer_Tests, ImportModelWithNotSupportedOp) { TEST(ONNX_Importer_Tests, ImportModelWhenFileDoesNotExist) { auto model_file_path = CommonTestUtils::getModelFromTestModelZoo( - ngraph::file_util::path_join(ONNX_MODELS_DIR, "not_exist_file.onnx")); + ov::util::path_join({ONNX_MODELS_DIR, "not_exist_file.onnx"})); try { auto function = ngraph::onnx_import::import_onnx_model(model_file_path); FAIL() << "Any expection was thrown despite the ONNX model file does not exist"; @@ -124,7 +122,7 @@ TEST(ONNX_Importer_Tests, ImportModelWhenFileDoesNotExist) { // TODO: CVS-61224 TEST(ONNX_Importer_Tests, DISABLED_ImportModelFromStream) { auto model_file_path = CommonTestUtils::getModelFromTestModelZoo( - ngraph::file_util::path_join(ONNX_MODELS_DIR, "addmul_abc.onnx")); + ov::util::path_join({ONNX_MODELS_DIR, "addmul_abc.onnx"})); std::ifstream model_file_stream(model_file_path); ASSERT_TRUE(model_file_stream.is_open()); int count_adds = 0; diff --git a/inference-engine/tests_deprecated/fluid_preproc/CMakeLists.txt b/inference-engine/tests_deprecated/fluid_preproc/CMakeLists.txt index 703d9085c82..b18876cc101 100644 --- a/inference-engine/tests_deprecated/fluid_preproc/CMakeLists.txt +++ b/inference-engine/tests_deprecated/fluid_preproc/CMakeLists.txt @@ -23,7 +23,7 @@ target_include_directories(${TARGET} PRIVATE $ $) -target_link_libraries(${TARGET} PRIVATE opencv_core opencv_imgproc +target_link_libraries(${TARGET} PRIVATE opencv_core opencv_imgproc openvino::util inference_engine fluid_test_computations gtest gtest_main) if(GAPI_TEST_PERF) diff --git a/ngraph/core/CMakeLists.txt b/ngraph/core/CMakeLists.txt index 726bc4c7402..8f9772091d5 100644 --- a/ngraph/core/CMakeLists.txt +++ b/ngraph/core/CMakeLists.txt @@ -39,7 +39,7 @@ if(COMMAND ie_add_vs_version_file) FILEDESCRIPTION "nGraph library") endif() -target_link_libraries(ngraph PRIVATE ngraph::builder ngraph::reference) +target_link_libraries(ngraph PRIVATE ngraph::builder ngraph::reference openvino::util) ie_mark_target_as_cc(ngraph) diff --git a/ngraph/core/include/ngraph/log.hpp b/ngraph/core/include/ngraph/log.hpp index 858770683d3..80f529f78a3 100644 --- a/ngraph/core/include/ngraph/log.hpp +++ b/ngraph/core/include/ngraph/log.hpp @@ -120,7 +120,7 @@ void default_logger_handler_func(const std::string& s); ngraph::default_logger_handler_func) \ .stream() -#ifdef NGRAPH_DEBUG_ENABLE +#ifdef OPENVINO_DEBUG_ENABLE # define NGRAPH_DEBUG \ ngraph::LogHelper(ngraph::LOG_TYPE::_LOG_TYPE_DEBUG, \ ngraph::trim_file_name(PROJECT_ROOT_DIR, __FILE__), \ diff --git a/ngraph/core/include/ngraph/util.hpp b/ngraph/core/include/ngraph/util.hpp index d5a85793e5f..b7554c58e12 100644 --- a/ngraph/core/include/ngraph/util.hpp +++ b/ngraph/core/include/ngraph/util.hpp @@ -190,6 +190,7 @@ NGRAPH_DEPRECATED("This method is deprecated and will be removed soon") void ngraph_free(void*); NGRAPH_API +NGRAPH_DEPRECATED("This method is deprecated and will be removed soon") size_t round_up(size_t size, size_t alignment); NGRAPH_DEPRECATED("This method is deprecated and will be removed soon") diff --git a/ngraph/core/src/env_util.cpp b/ngraph/core/src/env_util.cpp index 7a561524f7f..eaa6f675e97 100644 --- a/ngraph/core/src/env_util.cpp +++ b/ngraph/core/src/env_util.cpp @@ -8,59 +8,18 @@ #include "ngraph/log.hpp" #include "ngraph/util.hpp" +#include "openvino/util/env_util.hpp" using namespace std; std::string ngraph::getenv_string(const char* env_var) { - const char* env_p = ::getenv(env_var); - return env_p != nullptr ? string(env_p) : ""; + return ov::util::getenv_string(env_var); } int32_t ngraph::getenv_int(const char* env_var, int32_t default_value) { - const char* env_p = ::getenv(env_var); - int32_t env = default_value; - // If env_var is not "" or undefined - if (env_p && *env_p) { - errno = 0; - char* err; - env = strtol(env_p, &err, 0); - // if conversion leads to an overflow - if (errno) { - std::stringstream ss; - ss << "Environment variable \"" << env_var << "\"=\"" << env_p << "\" converted to different value \"" - << env << "\" due to overflow." << std::endl; - throw runtime_error(ss.str()); - } - // if syntax error is there - conversion will still happen - // but warn user of syntax error - if (*err) { - std::stringstream ss; - ss << "Environment variable \"" << env_var << "\"=\"" << env_p << "\" converted to different value \"" - << env << "\" due to syntax error \"" << err << '\"' << std::endl; - throw runtime_error(ss.str()); - } - } else { - NGRAPH_DEBUG << "Environment variable (" << env_var << ") empty or undefined, " - << " defaulted to " << default_value << " here."; - } - return env; + return ov::util::getenv_int(env_var, default_value); } bool ngraph::getenv_bool(const char* env_var, bool default_value) { - string value = to_lower(getenv_string(env_var)); - set off = {"0", "false", "off"}; - set on = {"1", "true", "on"}; - bool rc; - if (value == "") { - rc = default_value; - } else if (off.find(value) != off.end()) { - rc = false; - } else if (on.find(value) != on.end()) { - rc = true; - } else { - stringstream ss; - ss << "environment variable '" << env_var << "' value '" << value << "' invalid. Must be boolean."; - throw runtime_error(ss.str()); - } - return rc; + return ov::util::getenv_bool(env_var, default_value); } diff --git a/ngraph/core/src/file_util.cpp b/ngraph/core/src/file_util.cpp index 827226d5345..d54db2bd201 100644 --- a/ngraph/core/src/file_util.cpp +++ b/ngraph/core/src/file_util.cpp @@ -2,45 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 // -#ifdef _WIN32 -# ifndef NOMINMAX -# define NOMINMAX -# endif -# include -#else -# include -# include -# include -# include -# include -#endif -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "ngraph/env_util.hpp" #include "ngraph/file_util.hpp" -#include "ngraph/log.hpp" -#ifdef _WIN32 -# define RMDIR(a) RemoveDirectoryA(a) -# define RMFILE(a) DeleteFileA(a) -#else -# define RMDIR(a) rmdir(a) -# define RMFILE(a) remove(a) -# ifdef ENABLE_UNICODE_PATH_SUPPORT -# include -# include -# endif -#endif +#include "openvino/util/file_util.hpp" NGRAPH_SUPPRESS_DEPRECATED_START @@ -48,202 +12,51 @@ using namespace std; using namespace ngraph; string file_util::get_file_name(const string& s) { - string rc = s; - auto pos = s.find_last_of('/'); - if (pos != string::npos) { - rc = s.substr(pos + 1); - } - return rc; + return ov::util::get_file_name(s); } string file_util::get_file_ext(const string& s) { - string rc = get_file_name(s); - auto pos = rc.find_last_of('.'); - if (pos != string::npos) { - rc = rc.substr(pos); - } else { - rc = ""; - } - return rc; + return ov::util::get_file_ext(s); } string file_util::get_directory(const string& s) { - string rc = s; - // Linux-style separator - auto pos = s.find_last_of('/'); - if (pos != string::npos) { - rc = s.substr(0, pos); - return rc; - } - // Windows-style separator - pos = s.find_last_of('\\'); - if (pos != string::npos) { - rc = s.substr(0, pos); - return rc; - } - return rc; + return ov::util::get_directory(s); } string file_util::path_join(const string& s1, const string& s2, const string& s3) { - return path_join(path_join(s1, s2), s3); + return ov::util::path_join({s1, s2, s3}); } string file_util::path_join(const string& s1, const string& s2, const string& s3, const string& s4) { - return path_join(path_join(path_join(s1, s2), s3), s4); + return ov::util::path_join({s1, s2, s3, s4}); } string file_util::path_join(const string& s1, const string& s2) { - string rc; - if (s2.size() > 0) { - if (s2[0] == '/') { - rc = s2; - } else if (s1.size() > 0) { - rc = s1; - if (rc[rc.size() - 1] != '/') { - rc += "/"; - } - rc += s2; - } else { - rc = s2; - } - } else { - rc = s1; - } - return rc; + return ov::util::path_join({s1, s2}); } -#ifndef _WIN32 -static void iterate_files_worker(const string& path, - function func, - bool recurse, - bool include_links) { - DIR* dir; - struct dirent* ent; - if ((dir = opendir(path.c_str())) != nullptr) { - try { - while ((ent = readdir(dir)) != nullptr) { - string name = ent->d_name; - string path_name = file_util::path_join(path, name); - switch (ent->d_type) { - case DT_DIR: - if (name != "." && name != "..") { - if (recurse) { - file_util::iterate_files(path_name, func, recurse); - } - func(path_name, true); - } - break; - case DT_LNK: - if (include_links) { - func(path_name, false); - } - break; - case DT_REG: - func(path_name, false); - break; - default: - break; - } - } - } catch (...) { - exception_ptr p = current_exception(); - closedir(dir); - rethrow_exception(p); - } - closedir(dir); - } else { - throw runtime_error("error enumerating file " + path); - } -} -#endif - void file_util::iterate_files(const string& path, function func, bool recurse, bool include_links) { - vector files; - vector dirs; -#ifdef _WIN32 - std::string file_match = path_join(path, "*"); - WIN32_FIND_DATAA data; - HANDLE hFind = FindFirstFileA(file_match.c_str(), &data); - if (hFind != INVALID_HANDLE_VALUE) { - do { - bool is_dir = data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY; - if (is_dir) { - if (string(data.cFileName) != "." && string(data.cFileName) != "..") { - string dir_path = path_join(path, data.cFileName); - if (recurse) { - iterate_files(dir_path, func, recurse); - } - func(dir_path, true); - } - } else { - string file_name = path_join(path, data.cFileName); - func(file_name, false); - } - } while (FindNextFileA(hFind, &data)); - FindClose(hFind); - } -#else - iterate_files_worker( - path, - [&files, &dirs](const string& file, bool is_dir) { - if (is_dir) { - dirs.push_back(file); - } else { - files.push_back(file); - } - }, - recurse, - include_links); -#endif - - for (auto f : files) { - func(f, false); - } - for (auto f : dirs) { - func(f, true); - } + return ov::util::iterate_files(path, func, recurse, include_links); } std::string file_util::sanitize_path(const std::string& path) { - const auto colon_pos = path.find(":"); - const auto sanitized_path = path.substr(colon_pos == std::string::npos ? 0 : colon_pos + 1); - const std::string to_erase = "/.\\"; - const auto start = sanitized_path.find_first_not_of(to_erase); - return (start == std::string::npos) ? "" : sanitized_path.substr(start); + return ov::util::sanitize_path(path); } -NGRAPH_API void file_util::convert_path_win_style(std::string& path) { - std::replace(path.begin(), path.end(), '/', '\\'); +void file_util::convert_path_win_style(std::string& path) { + ov::util::convert_path_win_style(path); } #ifdef ENABLE_UNICODE_PATH_SUPPORT std::string file_util::wstring_to_string(const std::wstring& wstr) { -# ifdef _WIN32 - int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); // NOLINT - std::string strTo(size_needed, 0); - WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); // NOLINT - return strTo; -# else - std::wstring_convert> wstring_decoder; - return wstring_decoder.to_bytes(wstr); -# endif + return ov::util::wstring_to_string(wstr); } std::wstring file_util::multi_byte_char_to_wstring(const char* str) { -# ifdef _WIN32 - int strSize = static_cast(std::strlen(str)); - int size_needed = MultiByteToWideChar(CP_UTF8, 0, str, strSize, NULL, 0); - std::wstring wstrTo(size_needed, 0); - MultiByteToWideChar(CP_UTF8, 0, str, strSize, &wstrTo[0], size_needed); - return wstrTo; -# else - std::wstring_convert> wstring_encoder; - std::wstring result = wstring_encoder.from_bytes(str); - return result; -# endif + return ov::util::string_to_wstring(str); } #endif // ENABLE_UNICODE_PATH_SUPPORT diff --git a/ngraph/core/src/pass/manager.cpp b/ngraph/core/src/pass/manager.cpp index 288003dd08a..8f3e0fd20d5 100644 --- a/ngraph/core/src/pass/manager.cpp +++ b/ngraph/core/src/pass/manager.cpp @@ -12,7 +12,6 @@ #include #include "itt.hpp" -#include "ngraph/env_util.hpp" #include "ngraph/function.hpp" #include "ngraph/graph_util.hpp" #include "ngraph/log.hpp" @@ -21,6 +20,7 @@ #include "ngraph/pass/pass.hpp" #include "ngraph/pass/visualize_tree.hpp" #include "ngraph/util.hpp" +#include "openvino/util/env_util.hpp" #include "perf_counters.hpp" using namespace std; @@ -38,7 +38,7 @@ PerfCounters& perf_counters() { ov::pass::Manager::Manager() : m_pass_config(std::make_shared()), - m_visualize(ngraph::getenv_bool("NGRAPH_ENABLE_VISUALIZE_TRACING")) {} + m_visualize(ov::util::getenv_bool("NGRAPH_ENABLE_VISUALIZE_TRACING")) {} ov::pass::Manager::~Manager() = default; @@ -48,7 +48,7 @@ void ov::pass::Manager::run_passes(shared_ptr func) { NGRAPH_SUPPRESS_DEPRECATED_START OV_ITT_SCOPED_TASK(ov::itt::domains::nGraph, "pass::Manager::run_passes"); - static bool profile_enabled = ngraph::getenv_bool("NGRAPH_PROFILE_PASS_ENABLE"); + static bool profile_enabled = ov::util::getenv_bool("NGRAPH_PROFILE_PASS_ENABLE"); size_t index = 0; ngraph::stopwatch pass_timer; @@ -114,7 +114,7 @@ void ov::pass::Manager::run_passes(shared_ptr func) { auto base_filename = func->get_name() + std::string("_") + index_str + std::string("_") + pass->get_name(); if (m_visualize) { - static const string format = ngraph::getenv_string("NGRAPH_VISUALIZE_TRACING_FORMAT"); + static const string format = ov::util::getenv_string("NGRAPH_VISUALIZE_TRACING_FORMAT"); auto file_ext = format.empty() ? "svg" : format; pass::VisualizeTree vt(base_filename + std::string(".") + file_ext); vt.run_on_function(func); diff --git a/ngraph/core/src/pattern/matcher.cpp b/ngraph/core/src/pattern/matcher.cpp index dc24e44ae54..14afad25e5d 100644 --- a/ngraph/core/src/pattern/matcher.cpp +++ b/ngraph/core/src/pattern/matcher.cpp @@ -7,11 +7,11 @@ #include #include -#include "ngraph/env_util.hpp" #include "ngraph/graph_util.hpp" #include "ngraph/log.hpp" #include "ngraph/op/parameter.hpp" #include "ngraph/op/util/op_types.hpp" +#include "openvino/util/env_util.hpp" namespace ov { namespace pass { @@ -109,7 +109,7 @@ bool Matcher::match_value(const ngraph::Output& pattern_value, const ngrap // This env var allows one to specify node name patterns to abort pattern matching // at particular nodes. The upshot is that one can quickly zero in on an offending // fusion by disabling individual fusions or optimizations that use Matcher. - static const std::string node_skip_cregex = ngraph::getenv_string("NGRAPH_FAIL_MATCH_AT"); + static const std::string node_skip_cregex = ov::util::getenv_string("NGRAPH_FAIL_MATCH_AT"); if (!node_skip_cregex.empty()) { static const std::regex node_skip_regex(node_skip_cregex); if (std::regex_match(graph_node->get_name(), node_skip_regex)) { diff --git a/ngraph/core/src/util.cpp b/ngraph/core/src/util.cpp index 4dcf3abb220..563c63bf794 100644 --- a/ngraph/core/src/util.cpp +++ b/ngraph/core/src/util.cpp @@ -21,6 +21,7 @@ #include "ngraph/op/result.hpp" #include "ngraph/partial_shape.hpp" #include "ngraph/shape.hpp" +#include "openvino/util/common_util.hpp" NGRAPH_SUPPRESS_DEPRECATED_START using namespace std; @@ -61,54 +62,19 @@ void ngraph::dump(ostream& out, const void* _data, size_t _size) { } std::string ngraph::to_lower(const std::string& s) { - std::string rc = s; - std::transform(rc.begin(), rc.end(), rc.begin(), ::tolower); - return rc; + return ov::util::to_lower(s); } std::string ngraph::to_upper(const std::string& s) { - std::string rc = s; - std::transform(rc.begin(), rc.end(), rc.begin(), ::toupper); - return rc; + return ov::util::to_upper(s); } string ngraph::trim(const string& s) { - string rc = s; - // trim trailing spaces - size_t pos = rc.find_last_not_of(" \t"); - if (string::npos != pos) { - rc = rc.substr(0, pos + 1); - } - - // trim leading spaces - pos = rc.find_first_not_of(" \t"); - if (string::npos != pos) { - rc = rc.substr(pos); - } - return rc; + return ov::util::trim(s); } vector ngraph::split(const string& src, char delimiter, bool do_trim) { - size_t pos; - string token; - size_t start = 0; - vector rc; - while ((pos = src.find(delimiter, start)) != std::string::npos) { - token = src.substr(start, pos - start); - start = pos + 1; - if (do_trim) { - token = trim(token); - } - rc.push_back(token); - } - if (start <= src.size()) { - token = src.substr(start); - if (do_trim) { - token = trim(token); - } - rc.push_back(token); - } - return rc; + return ov::util::split(src, delimiter, do_trim); } size_t ngraph::hash_combine(const std::vector& list) { diff --git a/ngraph/frontend/frontend_manager/CMakeLists.txt b/ngraph/frontend/frontend_manager/CMakeLists.txt index feee88b695b..e91f0527d11 100644 --- a/ngraph/frontend/frontend_manager/CMakeLists.txt +++ b/ngraph/frontend/frontend_manager/CMakeLists.txt @@ -18,7 +18,7 @@ source_group("public include" FILES ${LIBRARY_PUBLIC_HEADERS}) add_library(${TARGET_NAME}_static STATIC ${LIBRARY_SRC} ${LIBRARY_HEADERS} ${LIBRARY_PUBLIC_HEADERS}) add_library(${TARGET_NAME}::static ALIAS ${TARGET_NAME}_static) -target_link_libraries(${TARGET_NAME}_static PRIVATE ${CMAKE_DL_LIBS} PUBLIC ngraph) +target_link_libraries(${TARGET_NAME}_static PRIVATE ${CMAKE_DL_LIBS} openvino::util PUBLIC ngraph) target_include_directories(${TARGET_NAME}_static PUBLIC ${FRONTEND_INCLUDE_DIR}) target_include_directories(${TARGET_NAME}_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) target_compile_definitions(${TARGET_NAME}_static PUBLIC USE_STATIC_FRONTEND_MANAGER) @@ -34,7 +34,7 @@ target_include_directories(${TARGET_NAME} PUBLIC $ $) target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) -target_link_libraries(${TARGET_NAME} PRIVATE ${CMAKE_DL_LIBS} PUBLIC ngraph) +target_link_libraries(${TARGET_NAME} PRIVATE ${CMAKE_DL_LIBS} openvino::util PUBLIC ngraph) add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME}) diff --git a/ngraph/frontend/frontend_manager/src/frontend_manager.cpp b/ngraph/frontend/frontend_manager/src/frontend_manager.cpp index e48403678c6..cf2246cbb6c 100644 --- a/ngraph/frontend/frontend_manager/src/frontend_manager.cpp +++ b/ngraph/frontend/frontend_manager/src/frontend_manager.cpp @@ -5,8 +5,8 @@ #include "frontend_manager/frontend_manager.hpp" #include -#include #include +#include #include "frontend_manager/frontend_exceptions.hpp" #include "plugin_loader.hpp" @@ -72,7 +72,7 @@ private: } } }; - std::string env_path = ngraph::getenv_string("OV_FRONTEND_PATH"); + std::string env_path = ov::util::getenv_string("OV_FRONTEND_PATH"); if (!env_path.empty()) { auto start = 0u; auto sep_pos = env_path.find(PathSeparator, start); diff --git a/ngraph/frontend/frontend_manager/src/plugin_loader.cpp b/ngraph/frontend/frontend_manager/src/plugin_loader.cpp index 36100e89882..a69d8dff4a9 100644 --- a/ngraph/frontend/frontend_manager/src/plugin_loader.cpp +++ b/ngraph/frontend/frontend_manager/src/plugin_loader.cpp @@ -19,7 +19,7 @@ #include #include -#include "ngraph/file_util.hpp" +#include "openvino/util/file_util.hpp" #include "plugin_loader.hpp" using namespace ngraph; @@ -40,7 +40,7 @@ static std::vector list_files(const std::string& path) { NGRAPH_SUPPRESS_DEPRECATED_START std::vector res; try { - ngraph::file_util::iterate_files( + ov::util::iterate_files( path, [&res](const std::string& file, bool is_dir) { if (!is_dir && file.find("_ngraph_frontend") != std::string::npos) { diff --git a/ngraph/frontend/onnx/frontend/CMakeLists.txt b/ngraph/frontend/onnx/frontend/CMakeLists.txt index 72838ee735e..43c3e4b362e 100644 --- a/ngraph/frontend/onnx/frontend/CMakeLists.txt +++ b/ngraph/frontend/onnx/frontend/CMakeLists.txt @@ -46,7 +46,7 @@ if(COMMAND ie_add_vs_version_file) FILEDESCRIPTION "nGraph ONNX frontend library") endif() -target_link_libraries(${TARGET_NAME} PUBLIC ngraph PRIVATE frontend_manager ngraph::builder onnx_common inference_engine_transformations) +target_link_libraries(${TARGET_NAME} PUBLIC ngraph PRIVATE frontend_manager ngraph::builder openvino::util onnx_common inference_engine_transformations) target_include_directories(${TARGET_NAME} PUBLIC $ $) diff --git a/ngraph/frontend/onnx/frontend/src/utils/tensor_external_data.cpp b/ngraph/frontend/onnx/frontend/src/utils/tensor_external_data.cpp index e0bc8fd33fd..71f51ff1e2e 100644 --- a/ngraph/frontend/onnx/frontend/src/utils/tensor_external_data.cpp +++ b/ngraph/frontend/onnx/frontend/src/utils/tensor_external_data.cpp @@ -10,6 +10,7 @@ #include "exceptions.hpp" #include "ngraph/file_util.hpp" #include "ngraph/log.hpp" +#include "openvino/util/file_util.hpp" namespace ngraph { namespace onnx_import { @@ -30,7 +31,7 @@ TensorExternalData::TensorExternalData(const ONNX_NAMESPACE::TensorProto& tensor std::string TensorExternalData::load_external_data() const { NGRAPH_SUPPRESS_DEPRECATED_START #if defined(ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32) - std::wstring path = file_util::multi_byte_char_to_wstring(m_data_location.c_str()); + std::wstring path = ov::util::string_to_wstring(m_data_location); #else std::string path = m_data_location; #endif diff --git a/ngraph/test/frontend/shared/CMakeLists.txt b/ngraph/test/frontend/shared/CMakeLists.txt index c4dda394fb1..0c1f6f133e2 100644 --- a/ngraph/test/frontend/shared/CMakeLists.txt +++ b/ngraph/test/frontend/shared/CMakeLists.txt @@ -12,6 +12,6 @@ add_library(${TARGET_NAME} STATIC ${LIBRARY_SRC} ${LIBRARY_HEADERS}) target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../..) target_link_libraries(${TARGET_NAME} PUBLIC frontend_manager - ngraph::ngraph cnpy ie_backend ngraph_test_util commonTestUtils) + ngraph::ngraph cnpy ie_backend ngraph_test_util commonTestUtils openvino::util) add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME}) diff --git a/ngraph/test/frontend/shared/include/utils.hpp b/ngraph/test/frontend/shared/include/utils.hpp index a335b0a0d97..b07137d64fa 100644 --- a/ngraph/test/frontend/shared/include/utils.hpp +++ b/ngraph/test/frontend/shared/include/utils.hpp @@ -10,8 +10,8 @@ #include "backend.hpp" #include "common_test_utils/file_utils.hpp" -#include "ngraph/env_util.hpp" -#include "ngraph/file_util.hpp" +#include "openvino/util/env_util.hpp" +#include "openvino/util/file_util.hpp" // Helper functions namespace FrontEndTestUtils { @@ -54,7 +54,7 @@ inline int set_test_env(const char* name, const char* value) { inline void setupTestEnv() { NGRAPH_SUPPRESS_DEPRECATED_START std::string fePath = - ngraph::file_util::get_directory(ngraph::runtime::Backend::get_backend_shared_library_search_directory()); + ov::util::get_directory(ngraph::runtime::Backend::get_backend_shared_library_search_directory()); set_test_env("OV_FRONTEND_PATH", fePath.c_str()); NGRAPH_SUPPRESS_DEPRECATED_END } diff --git a/ngraph/test/runtime/ie/ie_executable.cpp b/ngraph/test/runtime/ie/ie_executable.cpp index cd31f1e0a7e..bce24306031 100644 --- a/ngraph/test/runtime/ie/ie_executable.cpp +++ b/ngraph/test/runtime/ie/ie_executable.cpp @@ -3,6 +3,7 @@ // #include "ie_executable.hpp" + #include "ie_tensor.hpp" #include "ngraph/opsets/opset.hpp" #include "ngraph/pass/manager.hpp" @@ -14,95 +15,122 @@ using namespace ngraph; NGRAPH_SUPPRESS_DEPRECATED_START -namespace -{ - InferenceEngine::Blob::Ptr fill_blob(InferenceEngine::SizeVector shape, - const void* data, - size_t data_size, - const element::Type& elem_type) - { - InferenceEngine::Layout layout; - switch (shape.size()) - { - case 0: layout = InferenceEngine::Layout::SCALAR; break; - case 1: layout = InferenceEngine::Layout::C; break; - case 2: layout = InferenceEngine::Layout::NC; break; - case 3: layout = InferenceEngine::Layout::CHW; break; - case 4: layout = InferenceEngine::Layout::NCHW; break; - case 5: layout = InferenceEngine::Layout::NCDHW; break; - case 6: layout = InferenceEngine::Layout::GOIDHW; break; - default: IE_THROW() << "Can't convert dims " << shape.size() << " to Layout!"; - } +namespace { +InferenceEngine::Blob::Ptr fill_blob(InferenceEngine::SizeVector shape, + const void* data, + size_t data_size, + const element::Type& elem_type) { + InferenceEngine::Layout layout; + switch (shape.size()) { + case 0: + layout = InferenceEngine::Layout::SCALAR; + break; + case 1: + layout = InferenceEngine::Layout::C; + break; + case 2: + layout = InferenceEngine::Layout::NC; + break; + case 3: + layout = InferenceEngine::Layout::CHW; + break; + case 4: + layout = InferenceEngine::Layout::NCHW; + break; + case 5: + layout = InferenceEngine::Layout::NCDHW; + break; + case 6: + layout = InferenceEngine::Layout::GOIDHW; + break; + default: + IE_THROW() << "Can't convert dims " << shape.size() << " to Layout!"; + } - InferenceEngine::MemoryBlob::Ptr blob; + InferenceEngine::MemoryBlob::Ptr blob; -#define MAKE_IE_TBLOB(type_, precision_, shape_, layout_) \ - make_shared>( \ +#define MAKE_IE_TBLOB(type_, precision_, shape_, layout_) \ + make_shared>( \ InferenceEngine::TensorDesc{InferenceEngine::Precision::precision_, shape_, layout_}) - switch (elem_type) - { - case element::Type_t::f32: blob = MAKE_IE_TBLOB(float, FP32, shape, layout); break; - case element::Type_t::f64: blob = MAKE_IE_TBLOB(double, FP64, shape, layout); break; - case element::Type_t::i16: blob = MAKE_IE_TBLOB(int16_t, I16, shape, layout); break; - case element::Type_t::u8: blob = MAKE_IE_TBLOB(uint8_t, U8, shape, layout); break; - case element::Type_t::i8: blob = MAKE_IE_TBLOB(int8_t, I8, shape, layout); break; - case element::Type_t::u16: blob = MAKE_IE_TBLOB(uint16_t, U16, shape, layout); break; - case element::Type_t::i32: blob = MAKE_IE_TBLOB(int32_t, I32, shape, layout); break; - case element::Type_t::u32: blob = MAKE_IE_TBLOB(uint32_t, U32, shape, layout); break; - case element::Type_t::i64: blob = MAKE_IE_TBLOB(int64_t, I64, shape, layout); break; - case element::Type_t::u64: blob = MAKE_IE_TBLOB(uint64_t, U64, shape, layout); break; - case element::Type_t::boolean: blob = MAKE_IE_TBLOB(uint8_t, BOOL, shape, layout); break; - default: IE_THROW() << "Can't convert type " << elem_type << " to IE Precision!"; - } + switch (elem_type) { + case element::Type_t::f32: + blob = MAKE_IE_TBLOB(float, FP32, shape, layout); + break; + case element::Type_t::f64: + blob = MAKE_IE_TBLOB(double, FP64, shape, layout); + break; + case element::Type_t::i16: + blob = MAKE_IE_TBLOB(int16_t, I16, shape, layout); + break; + case element::Type_t::u8: + blob = MAKE_IE_TBLOB(uint8_t, U8, shape, layout); + break; + case element::Type_t::i8: + blob = MAKE_IE_TBLOB(int8_t, I8, shape, layout); + break; + case element::Type_t::u16: + blob = MAKE_IE_TBLOB(uint16_t, U16, shape, layout); + break; + case element::Type_t::i32: + blob = MAKE_IE_TBLOB(int32_t, I32, shape, layout); + break; + case element::Type_t::u32: + blob = MAKE_IE_TBLOB(uint32_t, U32, shape, layout); + break; + case element::Type_t::i64: + blob = MAKE_IE_TBLOB(int64_t, I64, shape, layout); + break; + case element::Type_t::u64: + blob = MAKE_IE_TBLOB(uint64_t, U64, shape, layout); + break; + case element::Type_t::boolean: + blob = MAKE_IE_TBLOB(uint8_t, BOOL, shape, layout); + break; + default: + IE_THROW() << "Can't convert type " << elem_type << " to IE Precision!"; + } #undef MAKE_IE_TBLOB - blob->allocate(); - uint8_t* blob_ptr = blob->rwmap().as(); - memcpy(blob_ptr, data, data_size * elem_type.size()); - return blob; - } -} // namespace + blob->allocate(); + uint8_t* blob_ptr = blob->rwmap().as(); + memcpy(blob_ptr, data, data_size * elem_type.size()); + return blob; +} +} // namespace -namespace -{ - std::set get_ie_ops() - { - std::set ie_ops = get_opset1().get_type_info_set(); - auto& opset2 = get_opset2().get_type_info_set(); - ie_ops.insert(opset2.begin(), opset2.end()); - auto& opset3 = get_opset3().get_type_info_set(); - ie_ops.insert(opset3.begin(), opset3.end()); - auto& opset4 = get_opset4().get_type_info_set(); - ie_ops.insert(opset4.begin(), opset4.end()); - auto& opset5 = get_opset5().get_type_info_set(); - ie_ops.insert(opset5.begin(), opset5.end()); - auto& opset6 = get_opset6().get_type_info_set(); - ie_ops.insert(opset6.begin(), opset6.end()); - auto& opset7 = get_opset7().get_type_info_set(); - ie_ops.insert(opset7.begin(), opset7.end()); - return ie_ops; - } -} // namespace +namespace { +std::set get_ie_ops() { + std::set ie_ops = get_opset1().get_type_info_set(); + auto& opset2 = get_opset2().get_type_info_set(); + ie_ops.insert(opset2.begin(), opset2.end()); + auto& opset3 = get_opset3().get_type_info_set(); + ie_ops.insert(opset3.begin(), opset3.end()); + auto& opset4 = get_opset4().get_type_info_set(); + ie_ops.insert(opset4.begin(), opset4.end()); + auto& opset5 = get_opset5().get_type_info_set(); + ie_ops.insert(opset5.begin(), opset5.end()); + auto& opset6 = get_opset6().get_type_info_set(); + ie_ops.insert(opset6.begin(), opset6.end()); + auto& opset7 = get_opset7().get_type_info_set(); + ie_ops.insert(opset7.begin(), opset7.end()); + return ie_ops; +} +} // namespace -runtime::ie::IE_Executable::IE_Executable(shared_ptr func, string device) - : m_device{device} -{ +runtime::ie::IE_Executable::IE_Executable(shared_ptr func, string device) : m_device{device} { static std::set ie_ops = get_ie_ops(); - for (const auto& node : func->get_ops()) - { - if (ie_ops.find(node->get_type_info()) == ie_ops.end()) - { + for (const auto& node : func->get_ops()) { + if (ie_ops.find(node->get_type_info()) == ie_ops.end()) { cout << "UNSUPPORTED OP DETECTED: " << node->get_type_info().name << endl; IE_THROW() << "Detected op not belonging to opset1!"; } } -#ifdef NGRAPH_DEBUG_ENABLE +#ifdef OPENVINO_DEBUG_ENABLE cout << "Nodes in test: "; - for (const auto& node : func->get_ops()) - { + for (const auto& node : func->get_ops()) { cout << node << endl; } cout << endl; @@ -113,8 +141,7 @@ runtime::ie::IE_Executable::IE_Executable(shared_ptr func, string devi } bool runtime::ie::IE_Executable::call(const vector>& outputs, - const vector>& inputs) -{ + const vector>& inputs) { InferenceEngine::Core ie; // Loading model to the plugin (BACKEND_NAME) @@ -124,16 +151,13 @@ bool runtime::ie::IE_Executable::call(const vector>& // Prepare input and output blobs InferenceEngine::InputsDataMap input_info = m_network.getInputsInfo(); - if (input_info.size() != inputs.size()) - { + if (input_info.size() != inputs.size()) { IE_THROW() << "Function inputs number differ from number of given inputs"; } size_t i = 0; - for (const auto& it : input_info) - { - shared_ptr tv = - static_pointer_cast(inputs[i]); + for (const auto& it : input_info) { + shared_ptr tv = static_pointer_cast(inputs[i]); infer_request.SetBlob(it.first, fill_blob(it.second->getTensorDesc().getDims(), tv->get_data_ptr(), @@ -151,10 +175,8 @@ bool runtime::ie::IE_Executable::call(const vector>& infer_request.Infer(); InferenceEngine::Blob::Ptr output = infer_request.GetBlob(output_name); - InferenceEngine::MemoryBlob::Ptr moutput = - InferenceEngine::as(output); - if (!moutput) - { + InferenceEngine::MemoryBlob::Ptr moutput = InferenceEngine::as(output); + if (!moutput) { IE_THROW() << "Cannot get output MemoryBlob in call_with_validate()"; } diff --git a/openvino/CMakeLists.txt b/openvino/CMakeLists.txt index a5b9d3ef7bd..e13df8efbf0 100644 --- a/openvino/CMakeLists.txt +++ b/openvino/CMakeLists.txt @@ -5,5 +5,6 @@ add_subdirectory(pp) add_subdirectory(itt) add_subdirectory(conditional_compilation) +add_subdirectory(util) openvino_developer_export_targets(COMPONENT openvino_common TARGETS openvino::pp openvino::itt openvino::conditional_compilation) diff --git a/openvino/util/.clang-format b/openvino/util/.clang-format new file mode 100644 index 00000000000..ebe747b7838 --- /dev/null +++ b/openvino/util/.clang-format @@ -0,0 +1,28 @@ +BasedOnStyle: Google +IndentWidth: 4 +UseTab: Never +ColumnLimit: 120 + +Language: Cpp +Standard: Cpp11 + +AccessModifierOffset: -4 +AlignConsecutiveMacros: true +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: Empty +AllowShortLoopsOnASingleLine: false +AlwaysBreakBeforeMultilineStrings: false +BinPackArguments: false +BinPackParameters: false +CommentPragmas: '^#' +DerivePointerAlignment: false +FixNamespaceComments: true +IndentCaseLabels: false +IndentPPDirectives: AfterHash +ForEachMacros: + - foreach + - FOREACH_CHILD diff --git a/openvino/util/CMakeLists.txt b/openvino/util/CMakeLists.txt new file mode 100644 index 00000000000..2a9c7dd91cd --- /dev/null +++ b/openvino/util/CMakeLists.txt @@ -0,0 +1,30 @@ +# Copyright (C) 2018-2021 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 +# + +set(TARGET_NAME util) + +file(GLOB_RECURSE LIBRARY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp) +file(GLOB_RECURSE PUBLIC_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp) + +set(UTIL_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include/) + +# Create named folders for the sources within the .vcproj +# Empty name lists them directly under the .vcproj + +source_group("src" FILES ${LIBRARY_SRC}) +source_group("include" FILES ${PUBLIC_HEADERS}) + +# Create shared library +add_library(${TARGET_NAME} STATIC ${LIBRARY_SRC} ${PUBLIC_HEADERS}) + +add_library(openvino::util ALIAS ${TARGET_NAME}) + +target_link_libraries(${TARGET_NAME} PRIVATE ${CMAKE_DL_LIBS}) +target_include_directories(${TARGET_NAME} PUBLIC ${UTIL_INCLUDE_DIR}) + +add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME}) +ov_ncc_naming_style(FOR_TARGET ${TARGET_NAME} + INCLUDE_DIRECTORY ${UTIL_INCLUDE_DIR}) + +openvino_developer_export_targets(COMPONENT openvino_common TARGETS ${TARGET_NAME}) diff --git a/openvino/util/include/openvino/util/common_util.hpp b/openvino/util/include/openvino/util/common_util.hpp new file mode 100644 index 00000000000..6c519f06611 --- /dev/null +++ b/openvino/util/include/openvino/util/common_util.hpp @@ -0,0 +1,101 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include +#include +#include + +namespace ov { +namespace util { + +template +std::string join(const T& v, const std::string& sep = ", ") { + std::ostringstream ss; + size_t count = 0; + for (const auto& x : v) { + if (count++ > 0) { + ss << sep; + } + ss << x; + } + return ss.str(); +} + +template +std::string vector_to_string(const T& v) { + std::ostringstream os; + os << "[ " << ov::util::join(v) << " ]"; + return os.str(); +} + +std::string to_lower(const std::string& s); + +std::string to_upper(const std::string& s); + +/** + * @brief trim from start (in place) + * @param s - string to trim + */ +inline std::string ltrim(const std::string& s) { + std::string ret = s; + ret.erase(ret.begin(), std::find_if(ret.begin(), ret.end(), [](int c) { + return !std::isspace(c); + })); + return ret; +} + +/** + * @brief trim from end (in place) + * @param s - string to trim + */ +inline std::string rtrim(const std::string& s) { + std::string ret = s; + ret.erase(std::find_if(ret.rbegin(), + ret.rend(), + [](int c) { + return !std::isspace(c); + }) + .base(), + ret.end()); + return ret; +} + +/** + * @brief Trims std::string from both ends (in place) + * @ingroup ie_dev_api_error_debug + * @param s A reference to a std::tring to trim + * @return A reference to a trimmed std::string + */ +inline std::string trim(const std::string& s) { + std::string ret = ltrim(s); + ret = rtrim(ret); + return ret; +} + +/** + * @brief check string end with given substring + * @param src - string to check + * @param with - given substring + * @return true if string end with given substring + */ +inline bool ends_with(const std::string& src, const char* with) { + int wl = static_cast(strlen(with)); + int so = static_cast(src.length()) - wl; + if (so < 0) + return false; + return 0 == strncmp(with, &src[so], wl); +} + +std::vector split(const std::string& s, char delimiter, bool trim = false); + +template +T ceil_div(const T& x, const T& y) { + return (x == 0 ? 0 : (1 + (x - 1) / y)); +} +} // namespace util +} // namespace ov diff --git a/openvino/util/include/openvino/util/env_util.hpp b/openvino/util/include/openvino/util/env_util.hpp new file mode 100644 index 00000000000..14ca064e121 --- /dev/null +++ b/openvino/util/include/openvino/util/env_util.hpp @@ -0,0 +1,34 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +namespace ov { +namespace util { +/// \brief Get the names environment variable as a string. +/// \param env_var The string name of the environment variable to get. +/// \return Returns string by value or an empty string if the environment +/// variable is not set. +std::string getenv_string(const char* env_var); + +/// \brief Get the names environment variable as an integer. If the value is not a +/// valid integer then an exception is thrown. +/// \param env_var The string name of the environment variable to get. +/// \param default_value The value to return if the environment variable is not set. +/// \return Returns value or default_value if the environment variable is not set. +int32_t getenv_int(const char* env_var, int32_t default_value = -1); + +/// \brief Get the names environment variable as a boolean. If the value is not a +/// valid boolean then an exception is thrown. Valid booleans are one of +/// 1, 0, on, off, true, false +/// All values are case insensitive. +/// If the environment variable is not set the default_value is returned. +/// \param env_var The string name of the environment variable to get. +/// \param default_value The value to return if the environment variable is not set. +/// \return Returns the boolean value of the environment variable. +bool getenv_bool(const char* env_var, bool default_value = false); +} // namespace util +} // namespace ov diff --git a/openvino/util/include/openvino/util/file_util.hpp b/openvino/util/include/openvino/util/file_util.hpp new file mode 100644 index 00000000000..f8d2014cba9 --- /dev/null +++ b/openvino/util/include/openvino/util/file_util.hpp @@ -0,0 +1,251 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include +#include + +#ifndef ENABLE_UNICODE_PATH_SUPPORT +# ifdef _WIN32 +# if defined __INTEL_COMPILER || defined _MSC_VER +# define ENABLE_UNICODE_PATH_SUPPORT +# endif +# elif defined(__GNUC__) && (__GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 2)) || defined(__clang__) +# define ENABLE_UNICODE_PATH_SUPPORT +# endif +#endif + +namespace ov { +namespace util { + +/// OS specific file traits +template +struct FileTraits; + +template <> +struct FileTraits { + static constexpr const auto file_separator = +#ifdef _WIN32 + '\\'; +#else + '/'; +#endif + static constexpr const auto dot_symbol = '.'; + static std::string library_ext() { +#ifdef _WIN32 + return {"dll"}; +#else + return {"so"}; +#endif + } + static std::string library_prefix() { +#ifdef _WIN32 + return {""}; +#else + return {"lib"}; +#endif + } +}; + +template <> +struct FileTraits { + static constexpr const auto file_separator = +#ifdef _WIN32 + L'\\'; +#else + L'/'; +#endif + static constexpr const auto dot_symbol = L'.'; + static std::wstring library_ext() { +#ifdef _WIN32 + return {L"dll"}; +#else + return {L"so"}; +#endif + } + static std::wstring library_prefix() { +#ifdef _WIN32 + return {L""}; +#else + return {L"lib"}; +#endif + } +}; + +#ifdef ENABLE_UNICODE_PATH_SUPPORT +/** + * @brief Conversion from wide character string to a single-byte chain. + * @param wstr A wide-char string + * @return A multi-byte string + */ +std::string wstring_to_string(const std::wstring& wstr); +/** + * @brief Conversion from single-byte chain to wide character string. + * @param str A null-terminated string + * @return A wide-char string + */ +std::wstring string_to_wstring(const std::string& str); + +#endif + +/// \brief Remove path components which would allow traversing up a directory tree. +/// \param path A path to file +/// \return A sanitiazed path +std::string sanitize_path(const std::string& path); + +/// \brief Returns the name with extension for a given path +/// \param path The path to the output file +std::string get_file_name(const std::string& path); + +/** + * @brief Interface function to get absolute path of file + * @param path - path to file, can be relative to current working directory + * @return Absolute path of file + * @throw runtime_exception if any error occurred + */ +std::string get_absolute_file_path(const std::string& path); +/** + * @brief Interface function to create directorty recursively by given path + * @param path - path to file, can be relative to current working directory + * @throw runtime_exception if any error occurred + */ +void create_directory_recursive(const std::string& path); + +/** + * @brief Interface function to check if directory exists for given path + * @param path - path to directory + * @return true if directory exists, false otherwise + */ +bool directory_exists(const std::string& path); + +/** + * @brief Returns file size for file + * @param[in] path The file name + * @return file size + */ +inline uint64_t file_size(const char* path) { +#if defined(ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32) + std::wstring widefilename = ov::util::string_to_wstring(path); + const wchar_t* file_name = widefilename.c_str(); +#elif defined(__ANDROID__) || defined(ANDROID) + std::string file_name = path; + std::string::size_type pos = file_name.find('!'); + if (pos != std::string::npos) { + file_name = file_name.substr(0, pos); + } +#else + const char* file_name = path; +#endif + std::ifstream in(file_name, std::ios_base::binary | std::ios_base::ate); + return in.tellg(); +} + +#ifdef ENABLE_UNICODE_PATH_SUPPORT + +/** + * @brief Returns file size for file + * @param[in] path The file name + * @return file size + */ +inline uint64_t file_size(const std::wstring& path) { + return file_size(wstring_to_string(path).c_str()); +} + +#endif // ENABLE_UNICODE_PATH_SUPPORT + +/** + * @brief Returns file size for file + * @param[in] path The file name + * @return file size + */ +inline uint64_t file_size(const std::string& path) { + return file_size(path.c_str()); +} + +/** + * @brief Returns true if file exists + * @param[in] path The path to file + * @return true if file exists + */ +template ::value || std::is_same::value)>::type> +inline bool file_exists(const std::basic_string& path) { + return file_size(path) > 0; +} + +std::string get_file_ext(const std::string& path); +std::string get_directory(const std::string& path); +std::string path_join(const std::vector& paths); + +void iterate_files(const std::string& path, + const std::function& func, + bool recurse = false, + bool include_links = false); + +void convert_path_win_style(std::string& path); + +std::string get_ov_lib_path(); + +#ifdef ENABLE_UNICODE_PATH_SUPPORT + +using FilePath = std::wstring; + +inline std::string from_file_path(const FilePath& path) { + return wstring_to_string(path); +} + +inline FilePath to_file_path(const std::string& path) { + return string_to_wstring(path); +} + +#else + +using FilePath = std::string; + +inline std::string from_file_path(const FilePath& path) { + return path; +} + +inline FilePath to_file_path(const std::string& path) { + return path; +} + +#endif // ENABLE_UNICODE_PATH_SUPPORT + +#ifdef ENABLE_UNICODE_PATH_SUPPORT + +/** + * @brief Returns a unicode path to openvino libraries + * @return A `std::wstring` path to openvino libraries + */ +std::wstring get_ov_lib_path_w(); + +inline std::wstring get_ov_library_path() { + return get_ov_lib_path_w(); +} + +#else + +inline std::string get_ov_library_path() { + return get_ov_lib_path(); +} + +#endif // ENABLE_UNICODE_PATH_SUPPORT + +template ::value || std::is_same::value)>::type> +inline std::basic_string make_plugin_library_name(const std::basic_string& path, + const std::basic_string& input) { + std::basic_string separator(1, FileTraits::file_separator); + if (path.empty()) + separator = {}; + return path + separator + FileTraits::library_prefix() + input + FileTraits::dot_symbol + + FileTraits::library_ext(); +} + +} // namespace util +} // namespace ov diff --git a/openvino/util/include/openvino/util/log.hpp b/openvino/util/include/openvino/util/log.hpp new file mode 100644 index 00000000000..aca1e0150f5 --- /dev/null +++ b/openvino/util/include/openvino/util/log.hpp @@ -0,0 +1,144 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include +#include + +namespace ov { +namespace util { +class ConstString { +public: + template + constexpr ConstString(const char (&p)[SIZE]) : m_string(p), + m_size(SIZE) {} + + constexpr char operator[](size_t i) const { + return i < m_size ? m_string[i] : throw std::out_of_range(""); + } + constexpr const char* get_ptr(size_t offset) const { + return offset < m_size ? &m_string[offset] : m_string; + } + constexpr size_t size() const { + return m_size; + } + +private: + const char* m_string; + size_t m_size; +}; + +constexpr const char* find_last(ConstString s, size_t offset, char ch) { + return offset == 0 ? s.get_ptr(0) : (s[offset] == ch ? s.get_ptr(offset + 1) : find_last(s, offset - 1, ch)); +} + +constexpr const char* find_last(ConstString s, char ch) { + return find_last(s, s.size() - 1, ch); +} + +constexpr const char* get_file_name(ConstString s) { + return find_last(s, '/'); +} +constexpr const char* trim_file_name(ConstString root, ConstString s) { + return s.get_ptr(root.size()); +} +enum class LOG_TYPE { + _LOG_TYPE_ERROR, + _LOG_TYPE_WARNING, + _LOG_TYPE_INFO, + _LOG_TYPE_DEBUG, +}; + +class LogHelper { +public: + LogHelper(LOG_TYPE, const char* file, int line, std::function m_handler_func); + ~LogHelper(); + + std::ostream& stream() { + return m_stream; + } + +private: + std::function m_handler_func; + std::stringstream m_stream; +}; + +class Logger { + friend class LogHelper; + +public: + static void set_log_path(const std::string& path); + static void start(); + static void stop(); + +private: + static void log_item(const std::string& s); + static void process_event(const std::string& s); + static void thread_entry(void* param); + static std::string m_log_path; + static std::deque m_queue; +}; + +void default_logger_handler_func(const std::string& s); + +#ifndef PROJECT_ROOT_DIR +# define PROJECT_ROOT_DIR "" +#endif + +#define OPENVINO_ERR \ + ::ov::util::LogHelper(::ov::util::LOG_TYPE::_LOG_TYPE_ERROR, \ + ::ov::util::trim_file_name(PROJECT_ROOT_DIR, __FILE__), \ + __LINE__, \ + ::ov::util::default_logger_handler_func) \ + .stream() + +#define OPENVINO_WARN \ + ::ov::util::LogHelper(::ov::util::LOG_TYPE::_LOG_TYPE_WARNING, \ + ::ov::util::trim_file_name(PROJECT_ROOT_DIR, __FILE__), \ + __LINE__, \ + ::ov::util::default_logger_handler_func) \ + .stream() + +#define OPENVINO_INFO \ + ::ov::util::LogHelper(::ov::util::LOG_TYPE::_LOG_TYPE_INFO, \ + ::ov::util::trim_file_name(PROJECT_ROOT_DIR, __FILE__), \ + __LINE__, \ + ::ov::util::default_logger_handler_func) \ + .stream() + +#ifdef OPENVINO_DEBUG_ENABLE +# define OPENVINO_DEBUG \ + ::ov::util::LogHelper(::ov::util::LOG_TYPE::_LOG_TYPE_DEBUG, \ + ::ov::util::trim_file_name(PROJECT_ROOT_DIR, __FILE__), \ + __LINE__, \ + ::ov::util::default_logger_handler_func) \ + .stream() +#else + +struct NullLogger {}; + +template +NullLogger&& operator<<(NullLogger&& logger, T&&) { + return std::move(logger); +} + +template +NullLogger&& operator<<(NullLogger&& logger, const T&) { + return std::move(logger); +} + +inline NullLogger&& operator<<( + NullLogger&& logger, + std::basic_ostream>& (&)(std::basic_ostream>&)) { + return std::move(logger); +} + +# define OPENVINO_DEBUG \ + ::ov::util::NullLogger {} +#endif +} // namespace util +} // namespace ov diff --git a/openvino/util/src/common_util.cpp b/openvino/util/src/common_util.cpp new file mode 100644 index 00000000000..b3aba13d41f --- /dev/null +++ b/openvino/util/src/common_util.cpp @@ -0,0 +1,42 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "openvino/util/common_util.hpp" + +#include + +std::string ov::util::to_lower(const std::string& s) { + std::string rc = s; + std::transform(rc.begin(), rc.end(), rc.begin(), ::tolower); + return rc; +} + +std::string ov::util::to_upper(const std::string& s) { + std::string rc = s; + std::transform(rc.begin(), rc.end(), rc.begin(), ::toupper); + return rc; +} + +std::vector ov::util::split(const std::string& src, char delimiter, bool do_trim) { + size_t pos; + std::string token; + size_t start = 0; + std::vector rc; + while ((pos = src.find(delimiter, start)) != std::string::npos) { + token = src.substr(start, pos - start); + start = pos + 1; + if (do_trim) { + token = trim(token); + } + rc.push_back(token); + } + if (start <= src.size()) { + token = src.substr(start); + if (do_trim) { + token = trim(token); + } + rc.push_back(token); + } + return rc; +} diff --git a/openvino/util/src/env_util.cpp b/openvino/util/src/env_util.cpp new file mode 100644 index 00000000000..eb005fdedeb --- /dev/null +++ b/openvino/util/src/env_util.cpp @@ -0,0 +1,65 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "openvino/util/env_util.hpp" + +#include +#include + +#include "openvino/util/common_util.hpp" +#include "openvino/util/log.hpp" + +std::string ov::util::getenv_string(const char* env_var) { + const char* env_p = ::getenv(env_var); + return env_p != nullptr ? std::string(env_p) : ""; +} + +int32_t ov::util::getenv_int(const char* env_var, int32_t default_value) { + const char* env_p = ::getenv(env_var); + int32_t env = default_value; + // If env_var is not "" or undefined + if (env_p && *env_p) { + errno = 0; + char* err; + env = strtol(env_p, &err, 0); + // if conversion leads to an overflow + if (errno) { + std::stringstream ss; + ss << "Environment variable \"" << env_var << "\"=\"" << env_p << "\" converted to different value \"" + << env << "\" due to overflow." << std::endl; + throw std::runtime_error(ss.str()); + } + // if syntax error is there - conversion will still happen + // but warn user of syntax error + if (*err) { + std::stringstream ss; + ss << "Environment variable \"" << env_var << "\"=\"" << env_p << "\" converted to different value \"" + << env << "\" due to syntax error \"" << err << '\"' << std::endl; + throw std::runtime_error(ss.str()); + } + } else { + OPENVINO_DEBUG << "Environment variable (" << env_var << ") empty or undefined, " + << " defaulted to " << default_value << " here."; + } + return env; +} + +bool ov::util::getenv_bool(const char* env_var, bool default_value) { + std::string value = ov::util::to_lower(ov::util::getenv_string(env_var)); + std::set off = {"0", "false", "off"}; + std::set on = {"1", "true", "on"}; + bool rc; + if (value == "") { + rc = default_value; + } else if (off.find(value) != off.end()) { + rc = false; + } else if (on.find(value) != on.end()) { + rc = true; + } else { + std::stringstream ss; + ss << "environment variable '" << env_var << "' value '" << value << "' invalid. Must be boolean."; + throw std::runtime_error(ss.str()); + } + return rc; +} diff --git a/openvino/util/src/file_util.cpp b/openvino/util/src/file_util.cpp new file mode 100644 index 00000000000..f3b1653d0a2 --- /dev/null +++ b/openvino/util/src/file_util.cpp @@ -0,0 +1,372 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// +#include "openvino/util/file_util.hpp" + +#include + +#include +#include +#include +#include +#include + +#include "openvino/util/common_util.hpp" + +#ifdef _WIN32 +# ifndef NOMINMAX +# define NOMINMAX +# endif +# include +# include +/// @brief Max length of absolute file path +# define MAX_ABS_PATH _MAX_PATH +/// @brief Get absolute file path, returns NULL in case of error +# define get_absolute_path(result, path) _fullpath(result, path.c_str(), MAX_ABS_PATH) +/// @brief Windows-specific 'stat' wrapper +# define stat _stat +/// @brief Windows-specific 'mkdir' wrapper +# define makedir(dir) _mkdir(dir) +// Copied from linux libc sys/stat.h: +# define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR) +#else +# include +# include +# include +# include +# include +# include + +# ifdef ENABLE_UNICODE_PATH_SUPPORT +# include +# include +# endif + +/// @brief Max length of absolute file path +# define MAX_ABS_PATH PATH_MAX +/// @brief Get absolute file path, returns NULL in case of error +# define get_absolute_path(result, path) realpath(path.c_str(), result) +/// @brief mkdir wrapper +# define makedir(dir) mkdir(dir, 0755) +#endif + +std::string ov::util::get_file_name(const std::string& s) { + std::string rc = s; + auto pos = s.find_last_of('/'); + if (pos != std::string::npos) { + rc = s.substr(pos + 1); + } + return rc; +} + +std::string ov::util::get_file_ext(const std::string& s) { + std::string rc = get_file_name(s); + auto pos = rc.find_last_of('.'); + if (pos != std::string::npos) { + rc = rc.substr(pos); + } else { + rc = ""; + } + return rc; +} + +std::string ov::util::get_directory(const std::string& s) { + std::string rc = s; + // Linux-style separator + auto pos = s.find_last_of('/'); + if (pos != std::string::npos) { + rc = s.substr(0, pos); + return rc; + } + // Windows-style separator + pos = s.find_last_of('\\'); + if (pos != std::string::npos) { + rc = s.substr(0, pos); + return rc; + } + return rc; +} + +namespace { + +std::string join_paths(const std::string& s1, const std::string& s2) { + std::string rc; + if (s2.size() > 0) { + if (s2[0] == '/') { + rc = s2; + } else if (s1.size() > 0) { + rc = s1; + if (rc[rc.size() - 1] != '/') { + rc += '/'; + } + rc += s2; + } else { + rc = s2; + } + } else { + rc = s1; + } + return rc; +} +} // namespace + +std::string ov::util::path_join(const std::vector& paths) { + std::string result; + if (paths.empty()) { + return result; + } + result = paths[0]; + for (size_t i = 1; i < paths.size(); i++) { + result = join_paths(result, paths[i]); + } + return result; +} + +#ifndef _WIN32 +static void iterate_files_worker(const std::string& path, + const std::function& func, + bool recurse, + bool include_links) { + DIR* dir; + struct dirent* ent; + if ((dir = opendir(path.c_str())) != nullptr) { + try { + while ((ent = readdir(dir)) != nullptr) { + std::string name = ent->d_name; + std::string path_name = ov::util::path_join({path, name}); + switch (ent->d_type) { + case DT_DIR: + if (name != "." && name != "..") { + if (recurse) { + ov::util::iterate_files(path_name, func, recurse); + } + func(path_name, true); + } + break; + case DT_LNK: + if (include_links) { + func(path_name, false); + } + break; + case DT_REG: + func(path_name, false); + break; + default: + break; + } + } + } catch (...) { + std::exception_ptr p = std::current_exception(); + closedir(dir); + std::rethrow_exception(p); + } + closedir(dir); + } else { + throw std::runtime_error("error enumerating file " + path); + } +} +#endif + +void ov::util::iterate_files(const std::string& path, + const std::function& func, + bool recurse, + bool include_links) { + std::vector files; + std::vector dirs; +#ifdef _WIN32 + std::string file_match = path_join({path, "*"}); + WIN32_FIND_DATAA data; + HANDLE hFind = FindFirstFileA(file_match.c_str(), &data); + if (hFind != INVALID_HANDLE_VALUE) { + do { + bool is_dir = data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY; + if (is_dir) { + if (std::string(data.cFileName) != "." && std::string(data.cFileName) != "..") { + std::string dir_path = path_join({path, data.cFileName}); + if (recurse) { + iterate_files(dir_path, func, recurse); + } + func(dir_path, true); + } + } else { + std::string file_name = path_join({path, data.cFileName}); + func(file_name, false); + } + } while (FindNextFileA(hFind, &data)); + FindClose(hFind); + } +#else + iterate_files_worker( + path, + [&files, &dirs](const std::string& file, bool is_dir) { + if (is_dir) { + dirs.push_back(file); + } else { + files.push_back(file); + } + }, + recurse, + include_links); +#endif + + for (const auto& f : files) { + func(f, false); + } + for (const auto& f : dirs) { + func(f, true); + } +} + +std::string ov::util::sanitize_path(const std::string& path) { + const auto colon_pos = path.find(':'); + const auto sanitized_path = path.substr(colon_pos == std::string::npos ? 0 : colon_pos + 1); + const std::string to_erase = "/.\\"; + const auto start = sanitized_path.find_first_not_of(to_erase); + return (start == std::string::npos) ? "" : sanitized_path.substr(start); +} + +void ov::util::convert_path_win_style(std::string& path) { + std::replace(path.begin(), path.end(), '/', '\\'); +} + +#ifdef ENABLE_UNICODE_PATH_SUPPORT +std::string ov::util::wstring_to_string(const std::wstring& wstr) { +# ifdef _WIN32 + int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); + std::string strTo(size_needed, 0); + WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); + return strTo; +# else + std::wstring_convert> wstring_decoder; + return wstring_decoder.to_bytes(wstr); +# endif +} + +std::wstring ov::util::string_to_wstring(const std::string& string) { + const char* str = string.c_str(); +# ifdef _WIN32 + int strSize = static_cast(std::strlen(str)); + int size_needed = MultiByteToWideChar(CP_UTF8, 0, str, strSize, NULL, 0); + std::wstring wstrTo(size_needed, 0); + MultiByteToWideChar(CP_UTF8, 0, str, strSize, &wstrTo[0], size_needed); + return wstrTo; +# else + std::wstring_convert> wstring_encoder; + std::wstring result = wstring_encoder.from_bytes(str); + return result; +# endif +} +#endif + +std::string ov::util::get_absolute_file_path(const std::string& path) { + std::string absolutePath; + absolutePath.resize(MAX_ABS_PATH); + auto absPath = get_absolute_path(&absolutePath[0], path); + if (!absPath) { + std::stringstream ss; + ss << "Can't get absolute file path for [" << path << "], err = " << strerror(errno); + throw std::runtime_error(ss.str()); + } + absolutePath.resize(strlen(absPath)); + return absolutePath; +} + +void ov::util::create_directory_recursive(const std::string& path) { + if (path.empty() || directory_exists(path)) { + return; + } + + std::size_t pos = path.rfind(ov::util::FileTraits::file_separator); + if (pos != std::string::npos) { + create_directory_recursive(path.substr(0, pos)); + } + + int err = makedir(path.c_str()); + if (err != 0 && errno != EEXIST) { + std::stringstream ss; + // TODO: in case of exception it may be needed to remove all created sub-directories + ss << "Couldn't create directory [" << path << "], err=" << strerror(errno) << ")"; + throw std::runtime_error(ss.str()); + } +} + +bool ov::util::directory_exists(const std::string& path) { + struct stat sb; + + if (stat(path.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) { + return true; + } + return false; +} + +namespace { + +template ::value || std::is_same::value)>::type> +std::basic_string get_path_name(const std::basic_string& s) { + size_t i = s.rfind(ov::util::FileTraits::file_separator, s.length()); + if (i != std::string::npos) { + return (s.substr(0, i)); + } + + return {}; +} + +static std::string get_ov_library_path_a() { +#ifdef _WIN32 + CHAR ov_library_path[MAX_PATH]; + HMODULE hm = NULL; + if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(ov::util::get_ov_lib_path), + &hm)) { + std::stringstream ss; + ss << "GetModuleHandle returned " << GetLastError(); + throw std::runtime_error(ss.str()); + } + GetModuleFileNameA(hm, (LPSTR)ov_library_path, sizeof(ov_library_path)); + return get_path_name(std::string(ov_library_path)); +#elif defined(__APPLE__) || defined(__linux__) + Dl_info info; + dladdr(reinterpret_cast(ov::util::get_ov_lib_path), &info); + std::string result = get_path_name(std::string(info.dli_fname)).c_str(); + if (!ov::util::ends_with(result, "/lib") && !ov::util::ends_with(result, "/lib/")) + result = ov::util::path_join({result, "lib"}); + return result; +#else +# error "Unsupported OS" +#endif // _WIN32 +} + +} // namespace + +std::string ov::util::get_ov_lib_path() { +#ifdef ENABLE_UNICODE_PATH_SUPPORT + return ov::util::wstring_to_string(ov::util::get_ov_lib_path_w()); +#else + return get_ov_library_path_a(); +#endif +} + +#ifdef ENABLE_UNICODE_PATH_SUPPORT + +std::wstring ov::util::get_ov_lib_path_w() { +# ifdef _WIN32 + WCHAR ov_library_path[MAX_PATH]; + HMODULE hm = NULL; + if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(get_ov_lib_path), + &hm)) { + std::stringstream ss; + ss << "GetModuleHandle returned " << GetLastError(); + throw std::runtime_error(ss.str()); + } + GetModuleFileNameW(hm, (LPWSTR)ov_library_path, sizeof(ov_library_path) / sizeof(ov_library_path[0])); + return get_path_name(std::wstring(ov_library_path)); +# elif defined(__linux__) || defined(__APPLE__) + return ov::util::string_to_wstring(get_ov_library_path_a()); +# else +# error "Unsupported OS" +# endif +} + +#endif // ENABLE_UNICODE_PATH_SUPPORT diff --git a/openvino/util/src/log.cpp b/openvino/util/src/log.cpp new file mode 100644 index 00000000000..a16c5f2af6b --- /dev/null +++ b/openvino/util/src/log.cpp @@ -0,0 +1,54 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "openvino/util/log.hpp" + +#include +#include +#include +#include + +void ov::util::default_logger_handler_func(const std::string& s) { + std::cout << s << std::endl; +} + +ov::util::LogHelper::LogHelper(LOG_TYPE type, + const char* file, + int line, + std::function handler_func) + : m_handler_func(std::move(handler_func)) { + switch (type) { + case LOG_TYPE::_LOG_TYPE_ERROR: + m_stream << "[ERR] "; + break; + case LOG_TYPE::_LOG_TYPE_WARNING: + m_stream << "[WARN] "; + break; + case LOG_TYPE::_LOG_TYPE_INFO: + m_stream << "[INFO] "; + break; + case LOG_TYPE::_LOG_TYPE_DEBUG: + m_stream << "[DEBUG] "; + break; + } + + time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + auto tm = gmtime(&tt); + if (tm) { + char buffer[256]; + strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%Sz", tm); + m_stream << buffer << " "; + } + + m_stream << file; + m_stream << " " << line; + m_stream << "\t"; +} + +ov::util::LogHelper::~LogHelper() { + if (m_handler_func) { + m_handler_func(m_stream.str()); + } + // Logger::log_item(m_stream.str()); +}