Skip to content

Commit 235febc

Browse files
Move Cuda usage into a shared library similar to onnxruntime (#973)
This separates out the cuda code into a /src/cuda folder and builds onnxruntime_genai_cuda.dll/.so. It is only this shared library that depends on the cuda runtimes. There is an interface inside the core genai code to route functions to this cuda library, but my next step is to refactor the code to separate out provider specific, so this interface will shrink. There is a new type to manage provider specific memory, DeviceMemory<T> and DeviceMemorySpan<T>, this will replace RoamingArray once the refactoring is complete.
1 parent 98448e3 commit 235febc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+766
-213
lines changed

.github/workflows/linux-cpu-arm64-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ jobs:
9292
run: |
9393
docker run --rm \
9494
--volume $GITHUB_WORKSPACE:/onnxruntime_src \
95-
-w /onnxruntime_src ort_genai_linux_arm64_gha bash -c "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/onnxruntime_src/ort/lib/ /onnxruntime_src/build/cpu/test/unit_tests"
95+
-w /onnxruntime_src ort_genai_linux_arm64_gha bash -c "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/onnxruntime_src/ort/lib/ /onnxruntime_src/build/cpu/unit_tests"

.github/workflows/linux-cpu-x64-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,4 @@ jobs:
9999
set -e -x
100100
export ORTGENAI_LOG_ORT_LIB=1
101101
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GITHUB_WORKSPACE/ort/lib
102-
./build/cpu/test/unit_tests
102+
./build/cpu/unit_tests

.github/workflows/linux-gpu-x64-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,4 @@ jobs:
147147
--rm \
148148
--volume /data/ortgenai_pytorch_models:/data/ortgenai_pytorch_models \
149149
--volume $GITHUB_WORKSPACE:/ort_genai_src \
150-
-w /ort_genai_src onnxruntimecudabuildx64 bash -c "ORTGENAI_LOG_ORT_LIB=1 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/ort_genai_src/build/cuda/ /ort_genai_src/build/cuda/test/unit_tests"
150+
-w /ort_genai_src onnxruntimecudabuildx64 bash -c "ORTGENAI_LOG_ORT_LIB=1 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/ort_genai_src/build/cuda/ /ort_genai_src/build/cuda/unit_tests"

.github/workflows/mac-cpu-arm64-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ jobs:
8282
set -e -x
8383
export ORTGENAI_LOG_ORT_LIB=1
8484
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$GITHUB_WORKSPACE/build/cpu/osx-arm64
85-
./build/cpu/osx-arm64/test/unit_tests
85+
./build/cpu/osx-arm64/unit_tests

.github/workflows/win-cpu-arm64-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,5 @@ jobs:
8585
8686
- name: Run tests
8787
run: |
88-
copy $env:GITHUB_WORKSPACE\ort\lib\* .\$env:binaryDir\test\Release
89-
& .\$env:binaryDir\test\Release\unit_tests.exe
88+
copy $env:GITHUB_WORKSPACE\ort\lib\* .\$env:binaryDir\Release
89+
& .\$env:binaryDir\Release\unit_tests.exe

.github/workflows/win-cpu-x64-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ jobs:
100100
101101
- name: Run tests
102102
run: |
103-
copy $env:GITHUB_WORKSPACE\ort\lib\* .\$env:binaryDir\test\Release
104-
& .\$env:binaryDir\test\Release\unit_tests.exe
103+
copy $env:GITHUB_WORKSPACE\ort\lib\* .\$env:binaryDir\Release
104+
& .\$env:binaryDir\Release\unit_tests.exe
105105
106106
- name: Perform CodeQL Analysis
107107
uses: github/codeql-action/analyze@v3

.github/workflows/win-cuda-x64-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,5 @@ jobs:
103103
run: |
104104
$env:PATH = "${{ env.cuda_dir }}\\v${{ env.cuda_version }}\\bin;" + $env:PATH
105105
echo "Current PATH variable is: $env:PATH"
106-
copy $env:GITHUB_WORKSPACE\ort\lib\* .\$env:binaryDir\test\Release
107-
& .\$env:binaryDir\test\Release\unit_tests.exe
106+
copy $env:GITHUB_WORKSPACE\ort\lib\* .\$env:binaryDir\Release
107+
& .\$env:binaryDir\Release\unit_tests.exe

.github/workflows/win-directml-x64-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,5 @@ jobs:
100100
101101
- name: Run tests
102102
run: |
103-
copy $env:GITHUB_WORKSPACE\ort\lib\* $env:GITHUB_WORKSPACE\$env:binaryDir\test\Release
104-
& .\$env:binaryDir\test\Release\unit_tests.exe
103+
copy $env:GITHUB_WORKSPACE\ort\lib\* $env:GITHUB_WORKSPACE\$env:binaryDir\Release
104+
& .\$env:binaryDir\Release\unit_tests.exe

CMakeLists.txt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,25 @@ if(APPLE)
110110
target_link_libraries(onnxruntime-genai PRIVATE "-framework Foundation" "-framework CoreML")
111111
endif()
112112

113-
set_target_properties(onnxruntime-genai PROPERTIES FOLDER "Sources")
114-
set_target_properties(onnxruntime-genai-static PROPERTIES FOLDER "Sources")
115-
source_group(TREE ${PROJECT_SOURCE_DIR} FILES ${generator_srcs})
116113

114+
# Build all source files using CUDA as a separate shared library we dynamically load at runtime
117115
if(USE_CUDA AND CMAKE_CUDA_COMPILER)
118-
set_target_properties(onnxruntime-genai PROPERTIES LINKER_LANGUAGE CUDA)
119-
target_link_libraries(onnxruntime-genai PRIVATE cublasLt cublas curand cufft cudart)
120-
# onnxruntime-genai-static is statically linked under Windows
121-
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
122-
set_target_properties(onnxruntime-genai-static PROPERTIES LINKER_LANGUAGE CUDA)
123-
target_link_libraries(onnxruntime-genai-static PRIVATE cublasLt cublas curand cufft cudart)
116+
add_library(onnxruntime-genai-cuda SHARED ${generator_cudalib_srcs})
117+
target_include_directories(onnxruntime-genai-cuda PRIVATE ${ORT_HEADER_DIR})
118+
target_include_directories(onnxruntime-genai-cuda PRIVATE ${GENERATORS_ROOT})
119+
target_link_libraries(onnxruntime-genai-cuda PRIVATE cublasLt cublas curand cufft cudart)
120+
set_target_properties(onnxruntime-genai-cuda PROPERTIES LINKER_LANGUAGE CUDA)
121+
add_dependencies(onnxruntime-genai onnxruntime-genai-cuda)
122+
source_group(TREE ${GENERATORS_ROOT}/cuda FILES ${generator_cudalib_srcs})
123+
list(APPEND ortgenai_embed_libs "$<TARGET_FILE:onnxruntime-genai-cuda>")
124+
if(APPLE)
125+
set_property(TARGET onnxruntime-genai-cuda APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker -exported_symbols_list ${GENERATORS_ROOT}/cuda/exported_symbols.lst")
126+
elseif(UNIX)
127+
set_property(TARGET onnxruntime-genai-cuda APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker --version-script=${GENERATORS_ROOT}/cuda/version_script.lds -Xlinker --gc-sections")
128+
elseif(WIN32)
129+
set_property(TARGET onnxruntime-genai-cuda APPEND_STRING PROPERTY LINK_FLAGS "-DEF:${GENERATORS_ROOT}/cuda/symbols.def")
130+
else()
131+
message(FATAL_ERROR "${target} unknown platform, need to specify shared library exports for it")
124132
endif()
125133
endif()
126134

@@ -202,7 +210,6 @@ if (ENABLE_JAVA)
202210
add_subdirectory("${SRC_ROOT}/java")
203211
endif()
204212

205-
206213
if(ENABLE_MODEL_BENCHMARK)
207214
message("------------------Enabling model benchmark------------------")
208215
add_subdirectory("${REPO_ROOT}/benchmark/c")

cmake/check_cuda.cmake

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ if(USE_CUDA AND CMAKE_CUDA_COMPILER)
3939

4040
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcudafe --diag_suppress=2803 --expt-relaxed-constexpr")
4141

42-
file(GLOB generator_cuda_srcs CONFIGURE_DEPENDS
43-
"${GENERATORS_ROOT}/*.cu"
44-
"${GENERATORS_ROOT}/*.cuh"
45-
"${MODELS_ROOT}/*.cu"
46-
"${MODELS_ROOT}/*.cuh"
42+
file(GLOB generator_cudalib_srcs CONFIGURE_DEPENDS
43+
"${GENERATORS_ROOT}/cuda/*.cpp"
44+
"${GENERATORS_ROOT}/cuda/*.h"
45+
"${GENERATORS_ROOT}/cuda/*.cu"
46+
"${GENERATORS_ROOT}/cuda/*.cuh"
4747
)
48+
4849
file(GLOB test_cuda_srcs CONFIGURE_DEPENDS
4950
"${TESTS_ROOT}/*.cu"
5051
"${TESTS_ROOT}/*.cuh"
5152
)
5253
list(APPEND test_srcs ${test_cuda_srcs})
53-
list(APPEND generator_srcs ${generator_cuda_srcs})
5454
add_compile_definitions(USE_CUDA=1)
5555
include_directories("${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}")
5656
elseif(USE_CUDA)

0 commit comments

Comments
 (0)