From 38e8c84f558b39d9f512a48d74a78623c6172053 Mon Sep 17 00:00:00 2001 From: Avijit Date: Thu, 20 Feb 2025 08:32:12 -0800 Subject: [PATCH] Removed unnecessary '--android' flag as we can determine the android build by inspecting the '--android_ndk' etc. Also adding the Windows related macros for exporting the C++ class. --- examples/slm_engine/README.md | 17 +++++--- examples/slm_engine/build_scripts/build.py | 40 ++++++++++++++----- .../slm_engine/build_scripts/build_deps.py | 10 +++-- examples/slm_engine/src/CMakeLists.txt | 1 + examples/slm_engine/src/cpp/slm_engine.h | 23 ++++++++++- 5 files changed, 71 insertions(+), 20 deletions(-) diff --git a/examples/slm_engine/README.md b/examples/slm_engine/README.md index dc714321b..7d85380ea 100644 --- a/examples/slm_engine/README.md +++ b/examples/slm_engine/README.md @@ -199,14 +199,13 @@ For example, if you are building on MacOS then the built artifacts will be store Following are the command line options applicable for the dependency build: ```bash -usage: build_deps.py [-h] [--android] [--android_sdk_path ANDROID_SDK_PATH] [--android_ndk_path ANDROID_NDK_PATH] +usage: build_deps.py [-h] [--android_sdk_path ANDROID_SDK_PATH] [--android_ndk_path ANDROID_NDK_PATH] [--api_level API_LEVEL] [--qnn_sdk_path QNN_SDK_PATH] [--build_type BUILD_TYPE] [--skip_ort_build] Build script for dependency libraries options: -h, --help show this help message and exit - --android Build for Android --android_sdk_path ANDROID_SDK_PATH Path to ANDROID SDK --android_ndk_path ANDROID_NDK_PATH @@ -229,7 +228,7 @@ The following example illustrates how to cross compile the dependencies for Andr $ export ANDROID_SDK_ROOT= $ export NDK_ROOT=$ANDROID_SDK_ROOT/ndk/ $ export QNN_SDK_ROOT= -$ python build_deps.py --android \ +$ python build_deps.py \ --android_sdk_path $ANDROID_SDK_ROOT \ --android_ndk_path $NDK_ROOT \ --qnn_sdk_path $QNN_SDK_ROOT @@ -252,13 +251,12 @@ $ python build.py For Android build, the following commandline options are important: ```bash -usage: build.py [-h] [--android] [--android_ndk_path ANDROID_NDK_PATH] [--build_type BUILD_TYPE] +usage: build.py [-h] [--android_ndk_path ANDROID_NDK_PATH] [--build_type BUILD_TYPE] Build script for this repo options: -h, --help show this help message and exit - --android Build for Android --android_ndk_path ANDROID_NDK_PATH Path to ANDROID NDK --build_type BUILD_TYPE @@ -269,12 +267,19 @@ options: For Android builds - use the following example: ```bash -$ python build_deps.py --android --android_ndk_path $NDK_ROOT +$ python build.py --android_ndk_path $NDK_ROOT ... ``` Notice that no need to specify any QNN flags as QNN device is handled by the ONNX Runtime via the [Execution Provider](https://onnxruntime.ai/docs/execution-providers/) mechanism. +For building on a Linux host we also provide a Dockerfile and a shell script to build using docker. Use the following command: + +```bash +$ ./build_linux.sh +... +``` + ## Testing the build After the build is complete, the binaries are available in the build_scripts/builds//install/bin directory. To test the build, download the ONNX model first and then run following command to execute SLM engine with a sample input file. diff --git a/examples/slm_engine/build_scripts/build.py b/examples/slm_engine/build_scripts/build.py index 957e6135c..ccf2c7bd8 100755 --- a/examples/slm_engine/build_scripts/build.py +++ b/examples/slm_engine/build_scripts/build.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import os +import sys import argparse import platform import subprocess @@ -27,7 +28,6 @@ def main(): parser = argparse.ArgumentParser(description="Build script for this repo") # Adding arguments - parser.add_argument("--android", action="store_true", help="Build for Android") parser.add_argument("--android_ndk_path", type=str, help="Path to ANDROID NDK") parser.add_argument( "--build_type", @@ -35,6 +35,12 @@ def main(): default="Release", help="{Release|RelWithDebInfo|Debug}", ) + parser.add_argument( + "--cmake_generator", + type=str, + default="Unix Makefiles", + help="{Unix Makefiles|Ninja|Visual Studio 17 2022|Xcode}", + ) # Parsing arguments args = parser.parse_args() @@ -46,11 +52,25 @@ def main(): # We need to get the name of the toplevel/src directory TOPLEVEL_DIR = f"{TOPLEVEL_DIR}/src" + # Set up the cmake generator + cmake_generator = args.cmake_generator + if cmake_generator is None: + if sys.platform.startswith("win"): + cmake_generator = "Visual Studio 17 2022" + elif sys.platform.startswith("linux"): + cmake_generator = "Ninja" + elif sys.platform.startswith("darwin"): + cmake_generator = "Ninja" + else: + cmake_generator = "Unix Makefiles" + + print(f"Using CMake generator: {cmake_generator}") + artifacts_dir = os.path.abspath(f"deps/artifacts/") cmake_options = [ "cmake", "-G", - "Ninja", + cmake_generator, TOPLEVEL_DIR, f"-DARTIFACTS_DIR={artifacts_dir}", f"-DCMAKE_BUILD_TYPE={args.build_type}", @@ -60,15 +80,15 @@ def main(): # platform.system() call which maps 1:1 with the Linux uname -s command. # When cross-compiling for Android, we use Android as the prefix. - # Use list comprehension to get the platform specific build directory prefix - dir_prefix = platform.system() if not args.android else "Android" - build_dir = f"builds/{dir_prefix}-{get_machine_type(args)}" - if args.android: - # Check to make sure that the other two options are also defined - if args.android_ndk_path is None: - print(f"Need to define android_ndk_path for Android builds") - return + dir_prefix = platform.system() + if args.android_ndk_path: cmake_options.extend(cmake_options_android(args.android_ndk_path)) + dir_prefix = "Android" + args.android = True + else: + args.android = False + + build_dir = f"builds/{dir_prefix}-{get_machine_type(args)}" # Launch build print(f"BUILD Dir:", build_dir) diff --git a/examples/slm_engine/build_scripts/build_deps.py b/examples/slm_engine/build_scripts/build_deps.py index da494eee8..589e68a29 100755 --- a/examples/slm_engine/build_scripts/build_deps.py +++ b/examples/slm_engine/build_scripts/build_deps.py @@ -231,6 +231,8 @@ def build_ort(args): args.build_type, "--cmake_extra_defines", "ENABLE_PYTHON=OFF", + "--ort_home", + ort_home, ] if args.android: cmd_args.extend( @@ -244,8 +246,6 @@ def build_ort(args): "arm64-v8a", "--android_api", args.api_level, - "--ort_home", - ort_home, ] ) @@ -408,7 +408,6 @@ def main(): ) # Adding arguments - parser.add_argument("--android", action="store_true", help="Build for Android") parser.add_argument("--android_sdk_path", type=str, help="Path to ANDROID SDK") parser.add_argument("--android_ndk_path", type=str, help="Path to ANDROID NDK") parser.add_argument( @@ -442,6 +441,11 @@ def main(): # Parsing arguments args = parser.parse_args() + if args.android_sdk_path or args.android_ndk_path: + args.android = True + else: + args.android = False + # Change directory to where this Python file is located to avoid any issues # related to running this script from another directory os.chdir(os.path.dirname(os.path.realpath(__file__))) diff --git a/examples/slm_engine/src/CMakeLists.txt b/examples/slm_engine/src/CMakeLists.txt index ef277a8ae..7b4d33be0 100644 --- a/examples/slm_engine/src/CMakeLists.txt +++ b/examples/slm_engine/src/CMakeLists.txt @@ -24,6 +24,7 @@ file ( LIMIT_COUNT 1) message(STATUS "SW-VERSION: ${VERSION_NUMBER}") add_compile_definitions(SW_VERSION_NUMBER="v${VERSION_NUMBER}") +add_compile_definitions(BUILDING_SLM_ENGINE) if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") set(CMAKE_INSTALL_RPATH "@loader_path") diff --git a/examples/slm_engine/src/cpp/slm_engine.h b/examples/slm_engine/src/cpp/slm_engine.h index a289f4153..74b590911 100644 --- a/examples/slm_engine/src/cpp/slm_engine.h +++ b/examples/slm_engine/src/cpp/slm_engine.h @@ -10,6 +10,27 @@ #include "input_decoder.h" #include "ort_genai.h" +#ifdef MYLIBRARY_EXPORTS +#define MYLIBRARY_API __declspec(dllexport) +#else +#define MYLIBRARY_API __declspec(dllimport) +#endif + +#ifdef _WIN32 +#ifdef BUILDING_SLM_ENGINE +#define SLM_ENGINE_EXPORT __declspec(dllexport) +#else +#define SLM_ENGINE_EXPORT __declspec(dllimport) +#endif +#else +// To make symbols visible on macOS/iOS +#ifdef __APPLE__ +#define SLM_ENGINE_EXPORT __attribute__((visibility("default"))) +#else +#define SLM_ENGINE_EXPORT +#endif +#endif + namespace microsoft { namespace aias { @@ -46,7 +67,7 @@ namespace aias { /// @endcode /// -class SLMEngine { +class SLM_ENGINE_EXPORT SLMEngine { public: /// @brief Enum to define the supported model types enum class SupportedModelType { PHI3,