From 4511001c465e69644d55d5956c2c4fd46bebc670 Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Tue, 12 Dec 2023 12:50:10 +0400 Subject: [PATCH 1/2] Supported different installation methods like archives, conda-forge --- modules/custom_operations/pyproject.toml | 5 ++- .../user_ie_extensions/CMakeLists.txt | 40 +++++++++++-------- .../tokenizer/CMakeLists.txt | 9 ++++- .../python/openvino_tokenizers/__init__.py | 30 ++++++++------ 4 files changed, 50 insertions(+), 34 deletions(-) diff --git a/modules/custom_operations/pyproject.toml b/modules/custom_operations/pyproject.toml index e1b1e99da..e8dd595f1 100644 --- a/modules/custom_operations/pyproject.toml +++ b/modules/custom_operations/pyproject.toml @@ -52,9 +52,10 @@ lines-after-imports = 2 [tool.scikit-build] cmake.minimum-version = "3.15" cmake.build-type = "Release" -cmake.args = ["-DCUSTOM_OPERATIONS:STRING=tokenizer", "-DBUILD_FAST_TOKENIZERS=OFF"] +cmake.args = ["-DCUSTOM_OPERATIONS=tokenizer", "-DBUILD_FAST_TOKENIZERS=OFF"] +cmake.targets = ["user_ov_extensions"] wheel.packages = ["user_ie_extensions/tokenizer/python/openvino_tokenizers"] -wheel.install-dir = "openvino_tokenizers/libs" +wheel.install-dir = "openvino_tokenizers" wheel.py-api = "py3" wheel.license-files = ['../../LICENSE', '../../third-party-programs.txt', '../../SECURITY.md'] sdist.cmake = true diff --git a/modules/custom_operations/user_ie_extensions/CMakeLists.txt b/modules/custom_operations/user_ie_extensions/CMakeLists.txt index 4105b8006..d8976e743 100644 --- a/modules/custom_operations/user_ie_extensions/CMakeLists.txt +++ b/modules/custom_operations/user_ie_extensions/CMakeLists.txt @@ -109,23 +109,29 @@ target_compile_definitions(${TARGET_NAME} PRIVATE IMPLEMENT_OPENVINO_EXTENSION_A # TODO: remove target_include_directories(${TARGET_NAME} PUBLIC ./include/) -# Wheel packaging using skbuild -if(DEFINED SKBUILD) - # Installing the extension module to the root of the package - if(LINUX) - set_target_properties(${TARGET_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN") - install(TARGETS ${TARGET_NAME} LIBRARY DESTINATION .) - elseif(APPLE) - set_target_properties(${TARGET_NAME} PROPERTIES INSTALL_RPATH "@loader_path") - install(TARGETS ${TARGET_NAME} LIBRARY DESTINATION .) - elseif(WIN32 AND X86_64) - install(TARGETS ${TARGET_NAME} RUNTIME DESTINATION .) - else() - message(FATAL_ERROR "Unsupported build platform") - endif() +# +# Installation rules +# - if(extra_libs) - install(FILES ${extra_libs} DESTINATION .) - endif() +include(GNUInstallDirs) +# setting RPATH / LC_RPATH depending on platform +if(LINUX) + set_target_properties(${TARGET_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN") +elseif(APPLE) + set_target_properties(${TARGET_NAME} PROPERTIES INSTALL_RPATH "@loader_path") +endif() + +# Installing the extension module to the root of the package +install(TARGETS ${TARGET_NAME} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT core + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT core) + +if(extra_libs) + if(WIN32) + set(extra_libs_location ${CMAKE_INSTALL_BINDIR}) + else() + set(extra_libs_location ${CMAKE_INSTALL_LIBDIR}) + endif() + install(FILES ${extra_libs} DESTINATION ${extra_libs_location} COMPONENT core) endif() diff --git a/modules/custom_operations/user_ie_extensions/tokenizer/CMakeLists.txt b/modules/custom_operations/user_ie_extensions/tokenizer/CMakeLists.txt index c4885a268..29c4961c9 100644 --- a/modules/custom_operations/user_ie_extensions/tokenizer/CMakeLists.txt +++ b/modules/custom_operations/user_ie_extensions/tokenizer/CMakeLists.txt @@ -120,8 +120,13 @@ else() message(FATAL_ERROR "Platform ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_PROCESSOR} does not have prebuilt Fast Tokenizer" "Please, use -DBUILD_FAST_TOKENIZERS=ON cmake option to enable build from soures") endif() + FetchContent_MakeAvailable(fast_tokenizer) + # to allow find_library to work with conda-forge env + set(_old_CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ${CMAKE_FIND_ROOT_PATH_MODE_LIBRARY}) + set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER) include("${fast_tokenizer_SOURCE_DIR}/FastTokenizer.cmake") + set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ${_old_CMAKE_FIND_ROOT_PATH_MODE_LIBRARY}) set(fast_tokenizer_SOURCE_DIR "${fast_tokenizer_SOURCE_DIR}" PARENT_SCOPE) @@ -168,8 +173,8 @@ if(BUILD_FAST_TOKENIZERS) else() if(WIN32 AND X86_64) set(extra_libs "${fast_tokenizer_SOURCE_DIR}/lib/core_tokenizers.dll" - "${fast_tokenizer_SOURCE_DIR}/third_party/lib/icudt70.dll" - "${fast_tokenizer_SOURCE_DIR}/third_party/lib/icuuc70.dll" PARENT_SCOPE) + "${fast_tokenizer_SOURCE_DIR}/third_party/lib/icudt70.dll" + "${fast_tokenizer_SOURCE_DIR}/third_party/lib/icuuc70.dll" PARENT_SCOPE) elseif(LINUX) set(extra_libs "${fast_tokenizer_SOURCE_DIR}/lib/libcore_tokenizers.so" PARENT_SCOPE) elseif(APPLE) diff --git a/modules/custom_operations/user_ie_extensions/tokenizer/python/openvino_tokenizers/__init__.py b/modules/custom_operations/user_ie_extensions/tokenizer/python/openvino_tokenizers/__init__.py index f4571c9ae..f98113ffb 100644 --- a/modules/custom_operations/user_ie_extensions/tokenizer/python/openvino_tokenizers/__init__.py +++ b/modules/custom_operations/user_ie_extensions/tokenizer/python/openvino_tokenizers/__init__.py @@ -16,38 +16,42 @@ _extension_path = os.environ.get("OV_TOKENIZER_PREBUILD_EXTENSION_PATH") -_ext_name = "user_ov_extensions" if _extension_path: - # when the path to extension set manually + # when the path to the extension set manually _ext_libs_path = Path(_extension_path).parent else: # python installation case _ext_libs_path = Path(sysconfig.get_paths()["purelib"]) / __name__ / "libs" +_ext_name = "user_ov_extensions" if sys.platform == "win32": - _ext_path = _ext_libs_path / f"{_ext_name}.dll" - if _ext_libs_path.is_dir(): - # On Windows, with Python >= 3.8, DLLs are no longer imported from the PATH. - os.add_dll_directory(str(_ext_libs_path.absolute())) - else: - sys.exit(f"Error: extention libriary path {_ext_libs_path} not found") + _ext_name = f"{_ext_name}.dll" elif sys.platform == "darwin": - _ext_path = _ext_libs_path / f"lib{_ext_name}.dylib" + _ext_name = f"lib{_ext_name}.dylib" elif sys.platform == "linux": - _ext_path = _ext_libs_path / f"lib{_ext_name}.so" + _ext_name = f"lib{_ext_name}.so" +else: + sys.exit(f"Error: extension does not support the platform {sys.platform}") + +# conda-forge case +conda_forge_lib_path = Path(os.path.join(os.path.dirname(__file__), "..", "..", "..")) +if os.path.exists(conda_forge_lib_path / _ext_name): + _ext_path = conda_forge_lib_path / _ext_name else: - sys.exit(f"Error: extension does not support platform {sys.platform}") + _ext_path = _ext_libs_path / _ext_name + +del _ext_name +del _ext_libs_path +del conda_forge_lib_path # patching openvino old_core_init = openvino.runtime.Core.__init__ - @functools.wraps(old_core_init) def new_core_init(self, *args, **kwargs): old_core_init(self, *args, **kwargs) self.add_extension(str(_ext_path)) # Core.add_extension doesn't support Path object - openvino.runtime.Core.__init__ = new_core_init _factory = NodeFactory() From fb7f89558293938ef5860dff50c52183fd2f2c3c Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Tue, 12 Dec 2023 14:59:07 +0400 Subject: [PATCH 2/2] Update CMakeLists.txt renamed component core => tokenizers --- modules/custom_operations/pyproject.toml | 2 +- modules/custom_operations/user_ie_extensions/CMakeLists.txt | 6 +++--- .../tokenizer/python/openvino_tokenizers/__init__.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/custom_operations/pyproject.toml b/modules/custom_operations/pyproject.toml index e8dd595f1..53c7b285d 100644 --- a/modules/custom_operations/pyproject.toml +++ b/modules/custom_operations/pyproject.toml @@ -52,7 +52,7 @@ lines-after-imports = 2 [tool.scikit-build] cmake.minimum-version = "3.15" cmake.build-type = "Release" -cmake.args = ["-DCUSTOM_OPERATIONS=tokenizer", "-DBUILD_FAST_TOKENIZERS=OFF"] +cmake.args = ["-DCUSTOM_OPERATIONS=tokenizer", "-DCMAKE_INSTALL_BINDIR=lib"] cmake.targets = ["user_ov_extensions"] wheel.packages = ["user_ie_extensions/tokenizer/python/openvino_tokenizers"] wheel.install-dir = "openvino_tokenizers" diff --git a/modules/custom_operations/user_ie_extensions/CMakeLists.txt b/modules/custom_operations/user_ie_extensions/CMakeLists.txt index d8976e743..b14006a2b 100644 --- a/modules/custom_operations/user_ie_extensions/CMakeLists.txt +++ b/modules/custom_operations/user_ie_extensions/CMakeLists.txt @@ -124,8 +124,8 @@ endif() # Installing the extension module to the root of the package install(TARGETS ${TARGET_NAME} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT core - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT core) + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT tokenizers + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT tokenizers) if(extra_libs) if(WIN32) @@ -133,5 +133,5 @@ if(extra_libs) else() set(extra_libs_location ${CMAKE_INSTALL_LIBDIR}) endif() - install(FILES ${extra_libs} DESTINATION ${extra_libs_location} COMPONENT core) + install(FILES ${extra_libs} DESTINATION ${extra_libs_location} COMPONENT tokenizers) endif() diff --git a/modules/custom_operations/user_ie_extensions/tokenizer/python/openvino_tokenizers/__init__.py b/modules/custom_operations/user_ie_extensions/tokenizer/python/openvino_tokenizers/__init__.py index f98113ffb..b6a998eb7 100644 --- a/modules/custom_operations/user_ie_extensions/tokenizer/python/openvino_tokenizers/__init__.py +++ b/modules/custom_operations/user_ie_extensions/tokenizer/python/openvino_tokenizers/__init__.py @@ -21,7 +21,7 @@ _ext_libs_path = Path(_extension_path).parent else: # python installation case - _ext_libs_path = Path(sysconfig.get_paths()["purelib"]) / __name__ / "libs" + _ext_libs_path = Path(sysconfig.get_paths()["purelib"]) / __name__ / "lib" _ext_name = "user_ov_extensions" if sys.platform == "win32":