Skip to content

Commit ddf7e6a

Browse files
Rbiessyggerganov
authored andcommitted
SYCL: Rename oneMKL to oneMath (llama/12192)
* Rename oneMKL Interface to oneMath * Use oneMath for Intel vendor * Rename occurences to mkl * clang-format * Silence verbose warnings * Set oneMath HIP_TARGETS * Fix silence warnings * Remove step to build oneMath from build instructions * Use fixed oneMath version * Remove INTEL_CPU * Fold CMake oneDNN conditions * Use Intel oneMKL for Intel devices * Improve CMake message * Link against MKL::MKL_SYCL::BLAS only * Move oneMath documentation to Nvidia and AMD sections
1 parent 0d42097 commit ddf7e6a

File tree

4 files changed

+181
-148
lines changed

4 files changed

+181
-148
lines changed

ggml/src/ggml-sycl/CMakeLists.txt

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ ggml_add_backend_library(ggml-sycl
2323
../../include/ggml-sycl.h
2424
)
2525

26+
file(GLOB GGML_HEADERS_SYCL "*.hpp")
27+
file(GLOB GGML_SOURCES_SYCL "*.cpp")
28+
target_sources(ggml-sycl PRIVATE ${GGML_HEADERS_SYCL} ${GGML_SOURCES_SYCL})
29+
30+
find_package(IntelSYCL)
31+
if (IntelSYCL_FOUND)
32+
# Use oneAPI CMake when possible
33+
target_link_libraries(ggml-sycl PRIVATE IntelSYCL::SYCL_CXX)
34+
else()
35+
# Fallback to the simplest way of enabling SYCL when using intel/llvm nightly for instance
36+
target_compile_options(ggml-sycl PRIVATE "-fsycl")
37+
target_link_options(ggml-sycl PRIVATE "-fsycl")
38+
endif()
39+
40+
target_compile_options(ggml-sycl PRIVATE "-Wno-narrowing")
41+
42+
# Link against oneDNN
2643
find_package(DNNL)
2744
set(GGML_SYCL_DNNL 0)
2845
if(DNNL_FOUND)
@@ -62,8 +79,6 @@ if (GGML_SYCL_F16)
6279
add_compile_definitions(GGML_SYCL_F16)
6380
endif()
6481

65-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-narrowing -fsycl")
66-
6782
if (GGML_SYCL_TARGET STREQUAL "NVIDIA")
6883
add_compile_definitions(GGML_SYCL_WARP_SIZE=32)
6984
elseif (GGML_SYCL_TARGET STREQUAL "AMD")
@@ -76,34 +91,84 @@ else()
7691
add_compile_definitions(GGML_SYCL_WARP_SIZE=16)
7792
endif()
7893

79-
file(GLOB GGML_HEADERS_SYCL "*.hpp")
80-
file(GLOB GGML_SOURCES_SYCL "*.cpp")
81-
target_sources(ggml-sycl PRIVATE ${GGML_HEADERS_SYCL} ${GGML_SOURCES_SYCL})
82-
94+
if (GGML_SYCL_GRAPH)
95+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_GRAPH)
96+
endif()
8397

84-
if (WIN32)
85-
find_package(IntelSYCL REQUIRED)
98+
# Link against Intel oneMKL or oneMath
99+
if (GGML_SYCL_TARGET STREQUAL "INTEL")
100+
# Intel devices use Intel oneMKL directly instead of oneMath to avoid the limitation of linking Intel oneMKL statically
101+
# See https://github.com/uxlfoundation/oneMath/issues/654
86102
find_package(MKL REQUIRED)
87-
target_link_libraries(ggml-sycl PRIVATE IntelSYCL::SYCL_CXX MKL::MKL MKL::MKL_SYCL)
103+
target_link_libraries(ggml-sycl PRIVATE MKL::MKL_SYCL::BLAS)
104+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_USE_INTEL_ONEMKL)
88105
else()
89-
if (GGML_SYCL_GRAPH)
90-
add_compile_definitions(GGML_SYCL_GRAPH)
106+
find_package(oneMath QUIET)
107+
if (NOT oneMath_FOUND)
108+
message(STATUS "oneMath not found: oneMath will be automatically downloaded")
109+
# Use FetchContent to automatically pull and build oneMath
110+
include(FetchContent)
111+
set(BUILD_FUNCTIONAL_TESTS False)
112+
set(BUILD_EXAMPLES False)
113+
set(TARGET_DOMAINS blas)
114+
if (GGML_SYCL_TARGET STREQUAL "NVIDIA")
115+
set(ENABLE_MKLCPU_BACKEND False)
116+
set(ENABLE_MKLGPU_BACKEND False)
117+
set(ENABLE_CUBLAS_BACKEND True)
118+
elseif (GGML_SYCL_TARGET STREQUAL "AMD")
119+
set(ENABLE_MKLCPU_BACKEND False)
120+
set(ENABLE_MKLGPU_BACKEND False)
121+
set(ENABLE_ROCBLAS_BACKEND True)
122+
# Ensure setting a string variable here is not overriden by oneMath CACHE variables
123+
cmake_policy(SET CMP0126 NEW)
124+
# Setting the device architecture is only needed and useful for AMD devices in oneMath
125+
set(HIP_TARGETS ${GGML_SYCL_DEVICE_ARCH} CACHE STRING "oneMath HIP target" FORCE)
126+
endif()
127+
FetchContent_Declare(
128+
ONEMATH
129+
GIT_REPOSITORY https://github.com/uxlfoundation/oneMath.git
130+
GIT_TAG c255b1b4c41e2ee3059455c1f96a965d6a62568a
131+
)
132+
FetchContent_MakeAvailable(ONEMATH)
133+
# Create alias to match with find_package targets name
134+
function(onemath_alias target)
135+
if (TARGET ${target}_obj)
136+
# Silence verbose warnings from external libraries
137+
target_compile_options(${target}_obj PRIVATE -w)
138+
endif()
139+
if (TARGET ${target})
140+
add_library(ONEMATH::${target} ALIAS ${target})
141+
endif()
142+
endfunction()
143+
onemath_alias(onemath)
144+
onemath_alias(onemath_blas_mklcpu)
145+
onemath_alias(onemath_blas_mklgpu)
146+
onemath_alias(onemath_blas_cublas)
147+
onemath_alias(onemath_blas_rocblas)
91148
endif()
92-
if (GGML_SYCL_TARGET STREQUAL "INTEL")
93-
target_link_libraries(ggml-sycl PRIVATE sycl OpenCL mkl_core pthread m dl mkl_sycl_blas mkl_intel_ilp64 mkl_tbb_thread)
94-
elseif (GGML_SYCL_TARGET STREQUAL "NVIDIA")
95-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl-targets=nvptx64-nvidia-cuda")
96-
add_compile_definitions(GGML_SYCL_NVIDIA)
97-
target_link_libraries(ggml-sycl PRIVATE sycl pthread m dl onemkl_blas_cublas)
149+
150+
# Below oneMath compile-time dispatching is used for better performance
151+
if (GGML_SYCL_TARGET STREQUAL "NVIDIA")
152+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_cublas)
153+
target_compile_options(ggml-sycl PRIVATE "-fsycl-targets=nvptx64-nvidia-cuda")
154+
target_link_options(ggml-sycl PRIVATE "-fsycl-targets=nvptx64-nvidia-cuda")
155+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_NVIDIA)
98156
elseif (GGML_SYCL_TARGET STREQUAL "AMD")
99157
if (NOT GGML_SYCL_DEVICE_ARCH)
100158
message(ERROR "Can't enable SYCL hip backend, GGML_SYCL_DEVICE_ARCH has not been set.")
101159
endif()
102-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl-targets=amdgcn-amd-amdhsa")
103-
target_link_libraries(ggml-sycl PRIVATE sycl pthread m dl onemkl)
160+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_rocblas)
161+
target_compile_options(ggml-sycl PRIVATE "-fsycl-targets=amdgcn-amd-amdhsa")
162+
target_link_options(ggml-sycl PRIVATE "-fsycl-targets=amdgcn-amd-amdhsa")
163+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_AMD)
164+
else()
165+
# Fallback to oneMath runtime dispatcher
166+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath)
167+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_GENERIC)
104168
endif()
169+
endif()
105170

106-
if (GGML_SYCL_DEVICE_ARCH)
107-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xsycl-target-backend --offload-arch=${GGML_SYCL_DEVICE_ARCH}")
108-
endif()
171+
if (GGML_SYCL_DEVICE_ARCH)
172+
target_compile_options(ggml-sycl PRIVATE -Xsycl-target-backend --offload-arch=${GGML_SYCL_DEVICE_ARCH})
173+
target_link_options(ggml-sycl PRIVATE -Xsycl-target-backend --offload-arch=${GGML_SYCL_DEVICE_ARCH})
109174
endif()

0 commit comments

Comments
 (0)