Skip to content
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

Initial Java bindings. #461

Merged
merged 14 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ examples/python/hf_cache
!test/test_models/hf-internal-testing/
!test/test_models/hf-internal-testing/tiny-random-gpt2*/*.onnx

.ipynb_checkpoints/
.ipynb_checkpoints/
/src/java/.gradle
/src/java/local.properties
/src/java/build
36 changes: 19 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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)

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)
Expand All @@ -19,24 +18,21 @@ include(cmake/check_dml.cmake)

include(cmake/cxx_standard.cmake)



if (ANDROID)
# Paths are based on the directory structure of the ORT Android AAR.
set(ORT_HEADER_DIR ${ORT_HOME}/headers)
set(ORT_LIB_DIR ${ORT_HOME}/jni/${ANDROID_ABI})
else()
endif()

add_compile_definitions(BUILDING_ORT_GENAI_C)
if(MSVC)
# set updated value for __cplusplus macro instead of 199711L
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>)
endif()
if(ENABLE_TESTS AND TEST_PHI2)
add_compile_definitions(TEST_PHI2=1)
else()
add_compile_definitions(TEST_PHI2=0)

if(ENABLE_TESTS)
# call enable_testing so we can add tests from subdirectories (e.g. test and src/java)
# it applies recursively to all subdirectories
enable_testing()
if (TEST_PHI2)
add_compile_definitions(TEST_PHI2=1)
else()
add_compile_definitions(TEST_PHI2=0)
endif()
endif()


Expand Down Expand Up @@ -114,18 +110,24 @@ if(USE_DML)
endif()

if(ENABLE_TESTS)
add_subdirectory("${CMAKE_SOURCE_DIR}/test")
message("------------------Enabling tests------------------")
add_subdirectory("${REPO_ROOT}/test")
endif()

if(ENABLE_PYTHON)
add_subdirectory("${CMAKE_SOURCE_DIR}/src/python")
message("------------------Enabling Python Wheel------------------")
add_subdirectory("${SRC_ROOT}/python")
endif()

if (ENABLE_JAVA)
message("------------------Enabling Java Jar------------------")
add_subdirectory("${SRC_ROOT}/java")
endif()


if(ENABLE_MODEL_BENCHMARK)
add_subdirectory("${CMAKE_SOURCE_DIR}/benchmark/c")
message("------------------Enabling model benchmark------------------")
add_subdirectory("${REPO_ROOT}/benchmark/c")
endif()

# Copy the onnxruntime binaries into the build folder so it's found on launch
Expand Down
10 changes: 7 additions & 3 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def _parse_args():
parser.add_argument("--skip_wheel", action="store_true", help="Skip building the Python wheel.")
parser.add_argument("--skip_csharp", action="store_true", help="Skip building the C# API.")

# Default to not building the Java bindings
parser.add_argument("--build_java", action="store_true", help="Build Java bindings.")

parser.add_argument("--parallel", action="store_true", help="Enable parallel build.")

# CI's sometimes explicitly set the path to the CMake and CTest executables.
Expand Down Expand Up @@ -308,8 +311,9 @@ def update(args: argparse.Namespace, env: dict[str, str]):
"-B",
str(args.build_dir),
"-DCMAKE_POSITION_INDEPENDENT_CODE=ON",
"-DUSE_CUDA=ON" if args.use_cuda else "-DUSE_CUDA=OFF",
"-DUSE_DML=ON" if args.use_dml else "-DUSE_DML=OFF",
f"-DUSE_CUDA={'ON' if args.use_cuda else 'OFF'}",
f"-DUSE_DML={'ON' if args.use_dml else 'OFF'}",
f"-DENABLE_JAVA={'ON' if args.build_java else 'OFF'}",
f"-DBUILD_WHEEL={build_wheel}",
]

Expand All @@ -328,7 +332,7 @@ def update(args: argparse.Namespace, env: dict[str, str]):
+ str((args.android_ndk_path / "build" / "cmake" / "android.toolchain.cmake").resolve(strict=True)),
f"-DANDROID_PLATFORM=android-{args.android_api}",
f"-DANDROID_ABI={args.android_abi}",
f"-DENABLE_PYTHON=OFF"
"-DENABLE_PYTHON=OFF"
]

util.run(command, env=env)
Expand Down
30 changes: 27 additions & 3 deletions cmake/global_variables.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ message("Building onnxruntime-genai for version ${VERSION_INFO}")


# Define the project directories
set(GENERATORS_ROOT ${PROJECT_SOURCE_DIR}/src)
set(MODELS_ROOT ${PROJECT_SOURCE_DIR}/src/models)
set(ORT_HOME ${CMAKE_SOURCE_DIR}/ort CACHE PATH "Path to the onnxruntime root directory.")
set(REPO_ROOT ${PROJECT_SOURCE_DIR})
set(SRC_ROOT ${REPO_ROOT}/src)
set(GENERATORS_ROOT ${SRC_ROOT})
set(MODELS_ROOT ${SRC_ROOT}/models)
set(ORT_HOME ${REPO_ROOT}/ort CACHE PATH "Path to the onnxruntime root directory.")

if (ANDROID)
# Paths are based on the directory structure of the ORT Android AAR.
Expand Down Expand Up @@ -53,4 +55,26 @@ if(NOT EXISTS "${ORT_LIB_DIR}/${ONNXRUNTIME_LIB}")
endif()
if(NOT EXISTS "${ORT_HEADER_DIR}/onnxruntime_c_api.h")
message(FATAL_ERROR "Expected the ONNX Runtime C API header to be found at \"${ORT_HEADER_DIR}/onnxruntime_c_api.h\". Actual: Not found.")
endif()


# normalize the target platform
if (MSVC)
if (CMAKE_VS_PLATFORM_NAME)
# Multi-platform generator
set(genai_target_platform ${CMAKE_VS_PLATFORM_NAME})
else()
set(genai_target_platform ${CMAKE_SYSTEM_PROCESSOR})
endif()

if (genai_target_platform STREQUAL "ARM64")
# pass
elseif (genai_target_platform STREQUAL "x64" OR
genai_target_platform STREQUAL "x86_64" OR
genai_target_platform STREQUAL "AMD64" OR
CMAKE_GENERATOR MATCHES "Win64")
set(genai_target_platform "x64")
else()
message(FATAL_ERROR "Unknown CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
endif()
10 changes: 9 additions & 1 deletion cmake/options.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
include(CMakeDependentOption)

# features
option(USE_CUDA "Build with CUDA support" ON)
option(USE_DML "Build with DML support" OFF)

# bindings
option(ENABLE_JAVA "Build the Java API." OFF)
option(ENABLE_PYTHON "Build the Python API." ON)
cmake_dependent_option(BUILD_WHEEL "Build the python wheel" ON "ENABLE_PYTHON" OFF)

# testing
option(ENABLE_TESTS "Enable tests" ON)
option(TEST_PHI2 "Enable tests for Phi2" OFF)

# performance
option(ENABLE_MODEL_BENCHMARK "Build model benchmark program" ON)

cmake_dependent_option(BUILD_WHEEL "Build the python wheel" ON "ENABLE_PYTHON" OFF)
Loading
Loading