@@ -23,6 +23,23 @@ ggml_add_backend_library(ggml-sycl
23
23
../../include/ggml-sycl.h
24
24
)
25
25
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
26
43
find_package (DNNL )
27
44
set (GGML_SYCL_DNNL 0 )
28
45
if (DNNL_FOUND )
@@ -62,8 +79,6 @@ if (GGML_SYCL_F16)
62
79
add_compile_definitions (GGML_SYCL_F16 )
63
80
endif ()
64
81
65
- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-narrowing -fsycl" )
66
-
67
82
if (GGML_SYCL_TARGET STREQUAL "NVIDIA" )
68
83
add_compile_definitions (GGML_SYCL_WARP_SIZE=32 )
69
84
elseif (GGML_SYCL_TARGET STREQUAL "AMD" )
@@ -76,34 +91,84 @@ else()
76
91
add_compile_definitions (GGML_SYCL_WARP_SIZE=16 )
77
92
endif ()
78
93
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 ()
83
97
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
86
102
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 )
88
105
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 )
91
148
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 )
98
156
elseif (GGML_SYCL_TARGET STREQUAL "AMD" )
99
157
if (NOT GGML_SYCL_DEVICE_ARCH )
100
158
message (ERROR "Can't enable SYCL hip backend, GGML_SYCL_DEVICE_ARCH has not been set." )
101
159
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 )
104
168
endif ()
169
+ endif ()
105
170
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} )
109
174
endif ()
0 commit comments