-
Notifications
You must be signed in to change notification settings - Fork 333
Enable GENAI with FetchContent or find_package() for client projects
#1858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 31 commits
47a5965
05f5749
0691161
68a545d
4e628e3
5f7aeea
5305384
a7b6ff2
9285680
fdbaffc
006bf1a
0d7b08e
4ac8295
893560a
f24dbf1
38a4346
b84ab59
e9b479a
3401949
4e623ea
a7d4a51
83c18bd
6f9e3b8
1fd3a6d
a6a3a85
6009a52
e8311ee
c1241ff
b77750e
0d461a4
37e63ed
7f04a01
11311dd
9546c37
4e23a0e
1fcce6e
beee6b5
c82b6d7
3c768e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,13 @@ else() | |
| cmake_minimum_required(VERSION 3.26) | ||
| endif() | ||
|
|
||
| include(cmake/version.cmake) | ||
|
|
||
| project(Generators VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} LANGUAGES C CXX) | ||
|
|
||
| include(GNUInstallDirs) | ||
| include(FetchContent) | ||
| include(CMakeDependentOption) | ||
| project(Generators LANGUAGES C CXX) | ||
|
|
||
| # All Options should be defined in cmake/options.cmake This must be included before any other cmake file is included | ||
| include(cmake/options.cmake) | ||
|
|
@@ -98,7 +102,6 @@ endif() | |
|
|
||
| include(cmake/ortlib.cmake) | ||
|
|
||
|
|
||
| include(cmake/external/onnxruntime_external_deps.cmake) | ||
| # All Global variables, including GLOB, for the top level CMakeLists.txt should be defined here | ||
| include(cmake/global_variables.cmake) | ||
|
|
@@ -158,23 +161,36 @@ find_package(Threads REQUIRED) | |
| if(WIN32) | ||
| add_library(onnxruntime-genai SHARED ${generator_srcs} "${GENERATORS_ROOT}/dll/onnxruntime-genai.rc") | ||
| target_compile_definitions(onnxruntime-genai PRIVATE VERSION_INFO=\"${VERSION_INFO}\") | ||
| target_compile_definitions(onnxruntime-genai PRIVATE VERSION_MAJOR=${VERSION_MAJOR}) | ||
| target_compile_definitions(onnxruntime-genai PRIVATE VERSION_MINOR=${VERSION_MINOR}) | ||
| target_compile_definitions(onnxruntime-genai PRIVATE VERSION_PATCH=${VERSION_PATCH}) | ||
| target_compile_definitions(onnxruntime-genai PRIVATE VERSION_MAJOR=${PROJECT_VERSION_MAJOR}) | ||
| target_compile_definitions(onnxruntime-genai PRIVATE VERSION_MINOR=${PROJECT_VERSION_MINOR}) | ||
| target_compile_definitions(onnxruntime-genai PRIVATE VERSION_PATCH=${PROJECT_VERSION_PATCH}) | ||
| target_compile_definitions(onnxruntime-genai PRIVATE VERSION_SUFFIX=${VERSION_SUFFIX}) | ||
| target_compile_definitions(onnxruntime-genai PRIVATE FILE_NAME=\"onnxruntime-genai.dll\") | ||
| else() | ||
| add_library(onnxruntime-genai SHARED ${generator_srcs}) | ||
| set_target_properties(onnxruntime-genai PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}) | ||
| endif() | ||
|
|
||
| target_include_directories(onnxruntime-genai INTERFACE | ||
| $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src> $<INSTALL_INTERFACE:include>) | ||
|
Comment on lines
+174
to
+175
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this leak all internal headers to consumers?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, only the headers marked public and interface.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those are all the files installed on Windows with the |
||
|
|
||
| target_include_directories(onnxruntime-genai PRIVATE ${ORT_HEADER_DIR}) | ||
| target_include_directories(onnxruntime-genai PRIVATE ${onnxruntime_extensions_SOURCE_DIR}/shared/api) | ||
| target_link_libraries(onnxruntime-genai PRIVATE onnxruntime_extensions) | ||
| target_link_directories(onnxruntime-genai PRIVATE ${ORT_LIB_DIR}) | ||
| target_link_libraries(onnxruntime-genai PRIVATE Threads::Threads) | ||
|
|
||
| # The genai library itself is always embedded in the shared library | ||
| list(APPEND ortgenai_embed_libs "$<TARGET_FILE:onnxruntime-genai>") | ||
| if(WIN32 OR ANDROID OR IOS) | ||
| list(APPEND ortgenai_embed_libs "$<TARGET_FILE:onnxruntime-genai>") | ||
| else() | ||
| if (ENABLE_PYTHON) | ||
| list(APPEND python_embed_libs "$<TARGET_SONAME_FILE:onnxruntime-genai>") | ||
| endif() | ||
| if (ENABLE_JAVA) | ||
| list(APPEND java_embed_libs "$<TARGET_LINKER_FILE:onnxruntime-genai>") | ||
| endif() | ||
| endif() | ||
|
|
||
| # we keep the shared libraries disconnected on Android as they will come from separate AARs and we don't want to force | ||
| # the ORT version to match in both. | ||
|
|
@@ -191,7 +207,6 @@ if(APPLE) | |
| target_link_libraries(onnxruntime-genai PRIVATE "-framework Foundation" "-framework CoreML") | ||
| endif() | ||
|
|
||
|
|
||
| # Build all source files using CUDA as a separate shared library we dynamically load at runtime | ||
| if((USE_CUDA OR USE_TRT_RTX) AND CMAKE_CUDA_COMPILER) | ||
| # Suppress nvcc warnings: | ||
|
|
@@ -234,9 +249,8 @@ if((USE_CUDA OR USE_TRT_RTX) AND CMAKE_CUDA_COMPILER) | |
| endif() | ||
| endif() | ||
|
|
||
|
|
||
| if(USE_GUIDANCE) | ||
| target_include_directories(onnxruntime-genai PUBLIC ${llguidance_SOURCE_DIR}/parser/) | ||
| target_include_directories(onnxruntime-genai PRIVATE ${llguidance_SOURCE_DIR}/parser/) | ||
| target_link_libraries(onnxruntime-genai PRIVATE llguidance) | ||
| if (WIN32) | ||
| # bcrypt is needed for the rust std lib | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,7 @@ class HelpFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescript | |
|
|
||
| # Default to not building the language bindings | ||
| parser.add_argument("--build_csharp", action="store_true", help="Build the C# API.") | ||
| parser.add_argument("--msbuild_extra_options", nargs="+", action="extend", default=[], help="Extra MSBuild properties (/p:key=value). Provide as key=value.") | ||
| parser.add_argument("--build_java", action="store_true", help="Build Java bindings.") | ||
| parser.add_argument( | ||
| "--publish_java_maven_local", | ||
|
|
@@ -402,9 +403,11 @@ def _get_csharp_properties(args: argparse.Namespace, ort_lib_dir: Path): | |
| ) | ||
| ort_lib_path = f"/p:OrtLibDir={ort_lib_dir}" | ||
|
|
||
| props = [configuration, platform, native_lib_path, ort_lib_path] | ||
| msbuild_extra_options = list(args.msbuild_extra_options) | ||
| if args.config == "Release" and not [opt for opt in msbuild_extra_options if 'IsReleaseBuild' in opt]: | ||
| msbuild_extra_options.append("IsReleaseBuild=True") | ||
|
|
||
| return props | ||
| return [configuration, platform, native_lib_path, ort_lib_path] + ["/p:" + option for option in msbuild_extra_options] | ||
|
|
||
|
|
||
| def _run_android_tests(args: argparse.Namespace): | ||
|
|
@@ -651,6 +654,9 @@ def _get_opencv_toolchain_file(): | |
| ) | ||
| args.test = False | ||
|
|
||
| if util.is_windows() and 'Ninja' in args.cmake_generator: | ||
| command += ["--compile-no-warning-as-error"] | ||
|
|
||
| if args.cmake_extra_defines != []: | ||
| command += args.cmake_extra_defines | ||
|
|
||
|
|
@@ -761,13 +767,18 @@ def build_examples(args: argparse.Namespace, env: dict[str, str]): | |
| Build the examples. | ||
| """ | ||
| examples_dir = REPO_ROOT / "examples" / "c" | ||
| build_dir = examples_dir / "build" | ||
|
|
||
| if build_dir.exists(): | ||
| log.info(f"Removing existing build directory: {build_dir}") | ||
| shutil.rmtree(build_dir) | ||
|
|
||
| build_dir.mkdir() | ||
| build_dir = examples_dir / "build" / args.config | ||
|
|
||
| # Removing the build directory will no longer work because of | ||
| # the FetchContent used in the examples. It creates the _deps/ | ||
| # directory, which contains .git/ subdirectories. Removing those | ||
| # requires elevated privileges. | ||
| # if build_dir.exists(): | ||
| # log.info(f"Removing existing build directory: {build_dir}") | ||
| # shutil.rmtree(build_dir) | ||
| # | ||
| # Do not need to create build dir manually. CMake will do that. | ||
| # build_dir.mkdir(parents=True) | ||
|
|
||
| samples_to_build = [ | ||
| "-DMODEL_QA=ON", | ||
|
|
@@ -777,13 +788,37 @@ def build_examples(args: argparse.Namespace, env: dict[str, str]): | |
| "-DNEMOTRON_SPEECH=ON" | ||
| ] | ||
|
|
||
| ort_include_dir = REPO_ROOT / "ort" / "include" | ||
| ort_lib_dir = REPO_ROOT / "ort" / "lib" | ||
| oga_include_dir = REPO_ROOT / "src" | ||
| oga_lib_dir = args.build_dir | ||
| if util.is_windows(): | ||
| # On Windows, the library files are in a subdirectory named after the configuration (e.g. Debug, Release, etc.) | ||
| oga_lib_dir = oga_lib_dir / args.config | ||
| cmake_prefix_path = str(args.build_dir) | ||
| if args.ort_home: | ||
| cmake_prefix_path += ";" + str(args.ort_home / args.config) | ||
| else: | ||
| ortlib_src_dir = args.build_dir / "_deps" / "ortlib-src" | ||
| ort_include_dir = REPO_ROOT / "ort" / "include" | ||
| if not ort_include_dir.exists(): | ||
| ort_include_dir = ortlib_src_dir / "build" / "native" / "include" | ||
| ortlib_runtimes_dir = ortlib_src_dir / "runtimes" | ||
| ort_lib_dir = REPO_ROOT / "ort" / "lib" | ||
| if not ort_lib_dir.exists(): | ||
| if util.is_windows(): | ||
| ort_lib_dir = ortlib_runtimes_dir / "win-x64" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implicit assumption that is_windows() is always win-x64? Haven't looked at is_windows(), does it only return true if it is win-x64?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change is needed for CI to pass when building examples, and the ONNX Runtime is extracted from a .nuget package. There's no Windows 32bit variant anymore. In the .nuget package, there are only win-x64 and win-arm64 subdirectories. But you're right, the if will consume both win-x64 and win-arm64 and hardcode the path for win-x64. I will make win-arm64 the first to be checked. |
||
| elif util.is_windows_arm() or (util.is_windows() and (args.arm64 or args.arm64ec)): | ||
| ort_lib_dir = ortlib_runtimes_dir / "win-arm64" | ||
| elif args.android: | ||
| ort_lib_dir = ortlib_runtimes_dir / "android" | ||
| elif util.is_linux(): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change is to make CI pass with the ONNX Runtime extracted from the .nuget package. When used with |
||
| ort_lib_dir = ortlib_runtimes_dir / "linux-x64" | ||
| elif util.is_linux_arm() or (util.is_linux() and (args.arm64 or args.arm64ec)): | ||
| ort_lib_dir = ortlib_runtimes_dir / "linux-arm64" | ||
| elif util.is_mac() or args.macos: | ||
| ort_lib_dir = ortlib_runtimes_dir / "osx-arm64" | ||
| elif args.ios: | ||
| ort_lib_dir = ortlib_runtimes_dir / "ios" | ||
| elif not util.is_aix(): | ||
| raise RuntimeError("Unsupported operating system to build examples for") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this error be cleaner (explicitly mention the build platform)? Any reason to not build here?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part is only when ONNX Runtime has been extracted from the .nuget package. There are no ONNXRuntime libraries for AIX in the .nuget package. The script is not blocking on building AIX here. It only blocks on unknown operating systems. The |
||
| samples_to_build += [ | ||
| "-DORT_INCLUDE_DIR=" + str(ort_include_dir), | ||
| "-DORT_LIB_DIR=" + str(ort_lib_dir / "native") | ||
| ] | ||
|
|
||
| cmake_command = ( | ||
| [ | ||
|
|
@@ -794,14 +829,10 @@ def build_examples(args: argparse.Namespace, env: dict[str, str]): | |
| str(build_dir), | ||
| "-G", | ||
| args.cmake_generator, | ||
| "-DCMAKE_PREFIX_PATH=" + cmake_prefix_path | ||
|
|
||
| ] | ||
| + samples_to_build | ||
| + [ | ||
| "-DORT_INCLUDE_DIR=" + str(ort_include_dir), | ||
| "-DORT_LIB_DIR=" + str(ort_lib_dir), | ||
| "-DOGA_INCLUDE_DIR=" + str(oga_include_dir), | ||
| "-DOGA_LIB_DIR=" + str(oga_lib_dir), | ||
| ] | ||
| ) | ||
|
|
||
| if args.cmake_generator.startswith("Visual Studio"): | ||
|
|
@@ -810,7 +841,7 @@ def build_examples(args: argparse.Namespace, env: dict[str, str]): | |
| elif args.arm64ec: | ||
| cmake_command += ["-A", "ARM64EC"] | ||
|
|
||
| if args.cmake_extra_defines != []: | ||
| if args.cmake_extra_defines: | ||
| cmake_command += args.cmake_extra_defines | ||
|
|
||
| util.run(cmake_command, env=env) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| @PACKAGE_INIT@ | ||
| if (NOT TARGET onnxruntime::onnxruntime-genai) | ||
| include(${CMAKE_CURRENT_LIST_DIR}/onnxruntime-genai-targets.cmake) | ||
| endif() | ||
| check_required_components(onnxruntime-genai) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate?