Skip to content

Commit e2d3353

Browse files
committed
rocm: Automatically build externally
A separate CMake project allows setting a separate compiler specifically for ROCm. This also makes the compile config easier eliminating the need for setting the ROCm compiler path. Most importantly, this lets me build llama.cpp with GCC as I get errors while trying to embed this in a Qt application otherwise.
1 parent 7ddf185 commit e2d3353

File tree

6 files changed

+118
-28
lines changed

6 files changed

+118
-28
lines changed

CMakeLists.txt

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -372,41 +372,63 @@ if (LLAMA_CLBLAST)
372372
endif()
373373

374374
if (LLAMA_HIPBLAS)
375-
list(APPEND CMAKE_PREFIX_PATH /opt/rocm)
375+
if (WIN32)
376+
# todo: also allow building with a separate compiler on windows
377+
list(APPEND CMAKE_PREFIX_PATH /opt/rocm)
376378

377-
if (NOT ${CMAKE_C_COMPILER_ID} MATCHES "Clang")
378-
message(WARNING "Only LLVM is supported for HIP, hint: CC=/opt/rocm/llvm/bin/clang")
379-
endif()
380-
if (NOT ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
381-
message(WARNING "Only LLVM is supported for HIP, hint: CXX=/opt/rocm/llvm/bin/clang++")
382-
endif()
383-
384-
find_package(hip)
385-
find_package(hipblas)
386-
find_package(rocblas)
387-
388-
if (${hipblas_FOUND} AND ${hip_FOUND})
389-
message(STATUS "HIP and hipBLAS found")
390-
add_compile_definitions(GGML_USE_HIPBLAS GGML_USE_CUBLAS)
391-
add_library(ggml-rocm OBJECT ggml-cuda.cu ggml-cuda.h)
392-
if (BUILD_SHARED_LIBS)
393-
set_target_properties(ggml-rocm PROPERTIES POSITION_INDEPENDENT_CODE ON)
379+
if (NOT ${CMAKE_C_COMPILER_ID} MATCHES "Clang")
380+
message(WARNING "Only LLVM is supported for HIP, hint: CC=/opt/rocm/llvm/bin/clang")
394381
endif()
395-
if (LLAMA_CUDA_FORCE_DMMV)
396-
target_compile_definitions(ggml-rocm PRIVATE GGML_CUDA_FORCE_DMMV)
382+
if (NOT ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
383+
message(WARNING "Only LLVM is supported for HIP, hint: CXX=/opt/rocm/llvm/bin/clang++")
397384
endif()
398-
target_compile_definitions(ggml-rocm PRIVATE GGML_CUDA_DMMV_X=${LLAMA_CUDA_DMMV_X})
399-
target_compile_definitions(ggml-rocm PRIVATE GGML_CUDA_MMV_Y=${LLAMA_CUDA_MMV_Y})
400-
target_compile_definitions(ggml-rocm PRIVATE K_QUANTS_PER_ITERATION=${LLAMA_CUDA_KQUANTS_ITER})
401-
set_source_files_properties(ggml-cuda.cu PROPERTIES LANGUAGE CXX)
402-
target_link_libraries(ggml-rocm PRIVATE hip::device PUBLIC hip::host roc::rocblas roc::hipblas)
403385

386+
find_package(hip)
387+
find_package(hipblas)
388+
find_package(rocblas)
389+
390+
if (${hipblas_FOUND} AND ${hip_FOUND})
391+
message(STATUS "HIP and hipBLAS found")
392+
add_compile_definitions(GGML_USE_HIPBLAS GGML_USE_CUBLAS)
393+
add_library(ggml-rocm OBJECT ggml-cuda.cu ggml-cuda.h)
394+
if (LLAMA_CUDA_FORCE_DMMV)
395+
target_compile_definitions(ggml-rocm PRIVATE GGML_CUDA_FORCE_DMMV)
396+
endif()
397+
target_compile_definitions(ggml-rocm PRIVATE GGML_CUDA_DMMV_X=${LLAMA_CUDA_DMMV_X})
398+
target_compile_definitions(ggml-rocm PRIVATE GGML_CUDA_MMV_Y=${LLAMA_CUDA_MMV_Y})
399+
target_compile_definitions(ggml-rocm PRIVATE K_QUANTS_PER_ITERATION=${LLAMA_CUDA_KQUANTS_ITER})
400+
target_compile_definitions(ggml-rocm PRIVATE CC_TURING=1000000000)
401+
set_source_files_properties(ggml-cuda.cu PROPERTIES LANGUAGE CXX)
402+
target_link_libraries(ggml-rocm PRIVATE hip::device PUBLIC hip::host roc::rocblas roc::hipblas)
403+
404+
if (LLAMA_STATIC)
405+
message(FATAL_ERROR "Static linking not supported for HIP/ROCm")
406+
endif()
407+
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ggml-rocm)
408+
else()
409+
message(WARNING "hipBLAS or HIP not found. Try setting CMAKE_PREFIX_PATH=/opt/rocm")
410+
endif()
411+
else()
404412
if (LLAMA_STATIC)
405413
message(FATAL_ERROR "Static linking not supported for HIP/ROCm")
406414
endif()
415+
include(ExternalProject)
416+
if (NOT DEFINED LLAMA_ROCM_PROJECT_CMAKE_ARGS)
417+
set(LLAMA_ROCM_PROJECT_CMAKE_ARGS -DCMAKE_CXX_COMPILER=/opt/rocm/llvm/bin/clang++ -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
418+
endif()
419+
ExternalProject_Add(ggml-rocm-project
420+
SOURCE_DIR ../ggml-rocm
421+
PREFIX ggml-rocm
422+
INSTALL_COMMAND ""
423+
CMAKE_ARGS ${LLAMA_ROCM_PROJECT_CMAKE_ARGS}
424+
BUILD_BYPRODUCTS ${CMAKE_BINARY_DIR}/bin/libggml-rocm.so
425+
)
426+
add_compile_definitions(GGML_USE_CUBLAS)
427+
add_library(ggml-rocm SHARED IMPORTED)
428+
add_dependencies(ggml-rocm ggml-rocm-project)
429+
set_target_properties(ggml-rocm PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/bin/libggml-rocm.so)
407430
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ggml-rocm)
408-
else()
409-
message(WARNING "hipBLAS or HIP not found. Try setting CMAKE_PREFIX_PATH=/opt/rocm")
431+
set(GGML_SOURCES_EXTRA ${GGML_SOURCES_EXTRA} ggml-cuda.h)
410432
endif()
411433
endif()
412434

@@ -762,6 +784,14 @@ if (LLAMA_METAL)
762784
WORLD_READ
763785
DESTINATION ${CMAKE_INSTALL_BINDIR})
764786
endif()
787+
install(
788+
FILES ${CMAKE_BINARY_DIR}/../ggml-rocm/src/ggml-rocm-project-build/libggml-rocm.so
789+
PERMISSIONS
790+
OWNER_READ
791+
OWNER_WRITE
792+
GROUP_READ
793+
WORLD_READ
794+
DESTINATION ${CMAKE_INSTALL_BINDIR})
765795

766796
#
767797
# programs, examples and tests

ggml-rocm/CMakeLists.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
cmake_minimum_required(VERSION 3.12) # Don't bump this version for no reason
2+
project(ggml-rocm CXX)
3+
4+
set(LLAMA_CUDA_DMMV_X "32" CACHE STRING "llama: x stride for dmmv CUDA kernels")
5+
set(LLAMA_CUDA_MMV_Y "1" CACHE STRING "llama: y block size for mmv CUDA kernels")
6+
set(LLAMA_CUDA_KQUANTS_ITER "2" CACHE STRING "llama: iters./thread per block for Q2_K/Q6_K")
7+
8+
set(CMAKE_CXX_STANDARD 11)
9+
set(CMAKE_CXX_STANDARD_REQUIRED true)
10+
set(THREADS_PREFER_PTHREAD_FLAG ON)
11+
find_package(Threads REQUIRED)
12+
13+
list(APPEND CMAKE_PREFIX_PATH /opt/rocm)
14+
15+
if (NOT ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
16+
message(FATAL_ERROR "Only LLVM is supported for HIP, hint: CXX=/opt/rocm/llvm/bin/clang++")
17+
endif()
18+
19+
find_package(hip)
20+
find_package(hipblas)
21+
find_package(rocblas)
22+
23+
if (${hipblas_FOUND} AND ${hip_FOUND})
24+
message(STATUS "HIP and hipBLAS found")
25+
add_compile_definitions(GGML_USE_HIPBLAS GGML_USE_CUBLAS)
26+
add_library(ggml-rocm SHARED ggml-cuda.cu ggml-cuda.h ggml.h)
27+
set_target_properties(ggml-rocm PROPERTIES POSITION_INDEPENDENT_CODE ON)
28+
if (LLAMA_CUDA_FORCE_DMMV)
29+
target_compile_definitions(ggml-rocm PRIVATE GGML_CUDA_FORCE_DMMV)
30+
endif()
31+
target_compile_definitions(ggml-rocm PRIVATE GGML_CUDA_DMMV_X=${LLAMA_CUDA_DMMV_X})
32+
target_compile_definitions(ggml-rocm PRIVATE GGML_CUDA_MMV_Y=${LLAMA_CUDA_MMV_Y})
33+
target_compile_definitions(ggml-rocm PRIVATE K_QUANTS_PER_ITERATION=${LLAMA_CUDA_KQUANTS_ITER})
34+
target_compile_definitions(ggml-rocm PRIVATE CC_TURING=1000000000)
35+
set_source_files_properties(ggml-cuda.cu PROPERTIES LANGUAGE CXX)
36+
set_target_properties(ggml-rocm PROPERTIES LINKER_LANGUAGE CXX LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../../../bin)
37+
target_link_libraries(ggml-rocm PUBLIC hip::device hip::host roc::rocblas roc::hipblas)
38+
else()
39+
message(FATAL_ERROR "hipBLAS or HIP not found. Try setting CMAKE_PREFIX_PATH=/opt/rocm")
40+
endif()
41+
42+
if (NOT MSVC)
43+
set(cxx_flags
44+
-Wall
45+
-Wextra
46+
-Wpedantic
47+
-Wcast-qual
48+
-Wno-unused-function
49+
-Wno-multichar
50+
)
51+
else()
52+
# todo : msvc
53+
endif()
54+
55+
add_compile_options(
56+
"$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
57+
)

ggml-rocm/ggml-cuda.cu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../ggml-cuda.cu

ggml-rocm/ggml-cuda.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../ggml-cuda.h

ggml-rocm/ggml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../ggml.h

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ llama_build_and_test_executable(test-grad0.cpp) # SLOW
4040
# dummy executable - not installed
4141
get_filename_component(TEST_TARGET test-c.c NAME_WE)
4242
add_executable(${TEST_TARGET} test-c.c)
43-
target_link_libraries(${TEST_TARGET} PRIVATE llama)
43+
target_link_libraries(${TEST_TARGET} PRIVATE llama ggml)

0 commit comments

Comments
 (0)