Skip to content

Commit

Permalink
Merge pull request #50 from microsoft/Cjian/shared_lib
Browse files Browse the repository at this point in the history
  • Loading branch information
jchen351 authored Feb 4, 2024
2 parents 9f0765b + 68a7af4 commit 52dab33
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux-cpu-arm64-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
--docker-build-args "--build-arg BUILD_UID=$( id -u )" \
--container-registry onnxruntimebuildcache \
--repository onnxruntimecpubuild
docker run --rm --volume $GITHUB_WORKSPACE:/onnxruntime_src --volume $RUNNER_TEMP:/build -w /build onnxruntimecpubuild bash -c "echo $PATH && /usr/bin/cmake -G Ninja /onnxruntime_src -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release && ninja"
docker run --rm --volume $GITHUB_WORKSPACE:/onnxruntime_src --volume $RUNNER_TEMP:/build -w /build onnxruntimecpubuild bash -c "echo $PATH && /usr/bin/cmake -G Ninja /onnxruntime_src -DCMAKE_BUILD_TYPE=Release && ninja"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linux-cpu-x64-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
set -e -x
mkdir build
cd build
cmake -G "Unix Makefiles" .. -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release
cmake -G "Unix Makefiles" .. -DCMAKE_BUILD_TYPE=Release
cmake --build . --config Release --parallel
cd ..
rm -rf build
Expand All @@ -48,7 +48,7 @@ jobs:
set -e -x
mkdir build
cd build
CC=clang CXX=clang++ cmake -G "Unix Makefiles" .. -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_CLANG_TIDY=clang-tidy
CC=clang CXX=clang++ cmake -G "Unix Makefiles" .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_CLANG_TIDY=clang-tidy
cmake --build . --config Release --parallel
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linux-gpu-x64-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ jobs:
--rm \
--volume $GITHUB_WORKSPACE:/onnxruntime_src \
--volume $RUNNER_TEMP:/build \
-w /build onnxruntimegpubuild bash -c "/usr/bin/cmake -G Ninja /onnxruntime_src -DBUILD_SHARED_LIBS=ON -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_CUDA_ARCHITECTURES=75 -DUSE_CUDA=1 -DCMAKE_BUILD_TYPE=Release && ninja"
-w /build onnxruntimegpubuild bash -c "/usr/bin/cmake -G Ninja /onnxruntime_src -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_CUDA_ARCHITECTURES=75 -DUSE_CUDA=1 -DCMAKE_BUILD_TYPE=Release && ninja"
42 changes: 21 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,20 @@ else()
list(REMOVE_ITEM generator_srcs ${generator_cuda_srcs})
endif()

## TODO: this is a hack to get the shared library to export .lib files on windows. We should find a better way when we need to compile dynamic libraries
#if(WIN32)
# set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
#endif()

add_library(onnxruntime-genai ${generator_srcs})
add_library(onnxruntime-genai SHARED ${generator_srcs})
add_library(onnxruntime-genai-static STATIC ${generator_srcs})
target_include_directories(onnxruntime-genai PRIVATE ${CMAKE_SOURCE_DIR}/ort/include)
target_include_directories(onnxruntime-genai-static PRIVATE ${CMAKE_SOURCE_DIR}/ort/include)


if(USE_TOKENIZER)
add_subdirectory("${CMAKE_SOURCE_DIR}/src/tokenizer")
message("Using Tokenizer")
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set_target_properties(onnxruntime-genai PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(onnxruntime-genai-static PROPERTIES POSITION_INDEPENDENT_CODE ON)

endif()

if(WIN32)
Expand All @@ -110,25 +109,28 @@ endif()
add_executable(Tests ${test_srcs})
target_include_directories(Tests PRIVATE ${CMAKE_SOURCE_DIR}/ort/include)
target_link_directories(Tests PRIVATE ${CMAKE_SOURCE_DIR}/ort/lib)
target_link_libraries(Tests PRIVATE onnxruntime-genai ${ONNXRUNTIME_LIB})
target_link_libraries(Tests PRIVATE onnxruntime-genai-static ${ONNXRUNTIME_LIB})
#
pybind11_add_module(python ${python_srcs})
target_include_directories(python PRIVATE ${CMAKE_SOURCE_DIR}/ort/include)
target_link_directories(python PRIVATE ${CMAKE_SOURCE_DIR}/ort/lib)
target_link_libraries(python PRIVATE onnxruntime-genai ${ONNXRUNTIME_LIB})
target_link_libraries(python PRIVATE onnxruntime-genai-static ${ONNXRUNTIME_LIB})
set_target_properties(python PROPERTIES OUTPUT_NAME "onnxruntime_genai")

# TODO: Windows is statically linking CUDA libraries, so remove the 'NOT WIN32' once it's using shared libraries
if(NOT WIN32 AND USE_CUDA AND CMAKE_CUDA_COMPILER)
if(USE_CUDA AND CMAKE_CUDA_COMPILER)
set_target_properties(onnxruntime-genai PROPERTIES LINKER_LANGUAGE CUDA)
set_target_properties(python PROPERTIES LINKER_LANGUAGE CUDA)
set_target_properties(Tests PROPERTIES LINKER_LANGUAGE CUDA)
set_target_properties(python PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(onnxruntime-genai PRIVATE cublasLt cublas cudnn curand cufft cudart)
target_link_libraries(Tests PRIVATE cublasLt cublas cudnn curand cufft cudart)
target_link_libraries(python PRIVATE cublasLt cublas cudnn curand cufft cudart)
# onnxruntime-genai-static is statically linked under Windows
if(NOT WIN32)
set_target_properties(onnxruntime-genai-static PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(onnxruntime-genai-static PRIVATE cublasLt cublas cudnn curand cufft cudart)
endif()
endif()


# Visual C++ - /MP Multiple process build, /WX Warnings as errors
# Enable:
# C5038: data member 'member1' will be initialized after data member 'member2'
Expand All @@ -142,34 +144,32 @@ endif()
file(GLOB onnxruntime_libs "${CMAKE_SOURCE_DIR}/ort/${ONNXRUNTIME_FILES}")
foreach(DLL_FILE ${onnxruntime_libs})
add_custom_command(
TARGET onnxruntime-genai POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${DLL_FILE} ${CMAKE_BINARY_DIR}/$<CONFIG>/${DLL_FILE_NAME}
TARGET onnxruntime-genai POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${DLL_FILE} ${CMAKE_BINARY_DIR}/$<CONFIG>/${DLL_FILE_NAME}
)
endforeach()

if(USE_TOKENIZER)
add_compile_definitions(USE_TOKENIZER=1)
target_include_directories(onnxruntime-genai PRIVATE ${TOKENIZER_ROOT})
target_include_directories(onnxruntime-genai-static PRIVATE ${TOKENIZER_ROOT})
target_include_directories(Tests PRIVATE ${TOKENIZER_ROOT})
target_include_directories(python PRIVATE ${TOKENIZER_ROOT})
target_link_libraries(Tests PRIVATE tokenizer)
target_link_libraries(python PRIVATE tokenizer)
target_link_libraries(onnxruntime-genai PRIVATE tokenizer)
target_link_libraries(onnxruntime-genai-static PRIVATE tokenizer)
message("Linking Tokenizer with TOKENIZER_ROOT: ${TOKENIZER_ROOT}")
endif()

if(BUILD_WHEEL)
set(WHEEL_FILES_DIR "${CMAKE_BINARY_DIR}/wheel")
message("Setting up wheel files in : ${WHEEL_FILES_DIR}")

set(TARGET_NAME "onnxruntime_genai")

configure_file(${PYTHON_ROOT}/setup.py.in ${WHEEL_FILES_DIR}/setup.py @ONLY)
configure_file(${PYTHON_ROOT}/py/__init__.py.in ${WHEEL_FILES_DIR}/${TARGET_NAME}/__init__.py @ONLY)

file(GLOB onnxruntime_libs "${CMAKE_SOURCE_DIR}/ort/${ONNXRUNTIME_FILES}")
foreach(DLL_FILE ${onnxruntime_libs})
add_custom_command(
TARGET onnxruntime-genai
TARGET onnxruntime-genai-static
COMMAND ${CMAKE_COMMAND} -E copy ${DLL_FILE} ${WHEEL_FILES_DIR}/${TARGET_NAME}/${DLL_FILE_NAME}
)
endforeach()
Expand Down
35 changes: 20 additions & 15 deletions src/tokenizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
set(TOKENIZER_ROOT ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE)
file(GLOB tokenizer_srcs CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/*.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/utils/*.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/c_api/*.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/*.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/utils/*.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/c_api/*.cc"
)
FetchContent_Declare(spm URL https://github.com/google/sentencepiece/archive/refs/tags/v0.1.99.zip)
set(SPM_USE_EXTERNAL_ABSL OFF CACHE BOOL "Use external absl")
set(SPM_USE_BUILTIN_PROTOBUF ON CACHE BOOL "Use built-in protobuf")
set(SPM_ENABLE_SHARED OFF)

FetchContent_GetProperties(spm)
if (NOT spm_POPULATED)
FetchContent_Populate(spm)
add_subdirectory(${spm_SOURCE_DIR} ${spm_BINARY_DIR} EXCLUDE_FROM_ALL)
endif ()
if(NOT spm_POPULATED)
FetchContent_Populate(spm)
add_subdirectory(${spm_SOURCE_DIR} ${spm_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

FetchContent_Declare(GSL URL https://github.com/microsoft/GSL/archive/refs/tags/v4.0.0.zip)
FetchContent_MakeAvailable(GSL)

FetchContent_Declare(simdjson URL https://github.com/simdjson/simdjson/archive/refs/tags/v3.6.3.zip URL_HASH SHA1=2b063a2e81f74a5d1cb937fadf3d2fca0f1edb09)
FetchContent_MakeAvailable(simdjson)

add_library(tokenizer ${tokenizer_srcs})
add_library(tokenizer STATIC ${tokenizer_srcs})
message(STATUS "GSL_SOURCE_DIR: ${GSL_SOURCE_DIR}")
target_include_directories(tokenizer PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${GSL_SOURCE_DIR}/include
${simdjson_SOURCE_DIR}/singleheader/
${spm_SOURCE_DIR}/src
${spm_SOURCE_DIR}/src/builtin_pb
${spm_SOURCE_DIR}/third_party/absl
${spm_SOURCE_DIR}/third_party/protobuf-lite
${CMAKE_CURRENT_SOURCE_DIR}
${GSL_SOURCE_DIR}/include
${simdjson_SOURCE_DIR}/singleheader/
${spm_SOURCE_DIR}/src
${spm_SOURCE_DIR}/src/builtin_pb
${spm_SOURCE_DIR}/third_party/absl
${spm_SOURCE_DIR}/third_party/protobuf-lite
)
target_compile_definitions(tokenizer PRIVATE _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set_target_properties(tokenizer PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(simdjson PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()

add_dependencies(tokenizer sentencepiece-static)
target_link_libraries(tokenizer PUBLIC sentencepiece-static simdjson)

0 comments on commit 52dab33

Please sign in to comment.