Skip to content

Commit

Permalink
Removed unnecessary '--android' flag as we can determine the android …
Browse files Browse the repository at this point in the history
…build by inspecting the '--android_ndk' etc. Also adding the Windows related macros for exporting the C++ class.
  • Loading branch information
Avijit committed Feb 20, 2025
1 parent bccc3a2 commit 38e8c84
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
17 changes: 11 additions & 6 deletions examples/slm_engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -229,7 +228,7 @@ The following example illustrates how to cross compile the dependencies for Andr
$ export ANDROID_SDK_ROOT=<Android SDK Directory>
$ export NDK_ROOT=$ANDROID_SDK_ROOT/ndk/<Version Number>
$ export QNN_SDK_ROOT=<qualcomm/qairt/VERSION>
$ 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
Expand All @@ -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
Expand All @@ -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/<TARGET_NAME>/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.
Expand Down
40 changes: 30 additions & 10 deletions examples/slm_engine/build_scripts/build.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import os
import sys
import argparse
import platform
import subprocess
Expand Down Expand Up @@ -27,14 +28,19 @@ 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",
type=str,
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()
Expand All @@ -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}",
Expand All @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions examples/slm_engine/build_scripts/build_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -244,8 +246,6 @@ def build_ort(args):
"arm64-v8a",
"--android_api",
args.api_level,
"--ort_home",
ort_home,
]
)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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__)))
Expand Down
1 change: 1 addition & 0 deletions examples/slm_engine/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
23 changes: 22 additions & 1 deletion examples/slm_engine/src/cpp/slm_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 38e8c84

Please sign in to comment.