Skip to content

Commit 995aea3

Browse files
committed
Use Intel oneMKL for Intel devices
1 parent 5f2525a commit 995aea3

File tree

2 files changed

+80
-67
lines changed

2 files changed

+80
-67
lines changed

ggml/src/ggml-sycl/CMakeLists.txt

+67-61
Original file line numberDiff line numberDiff line change
@@ -72,71 +72,77 @@ if (GGML_SYCL_GRAPH)
7272
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_GRAPH)
7373
endif()
7474

75-
find_package(oneMath QUIET)
76-
if (NOT oneMath_FOUND)
77-
message("-- oneMath not found: oneMath will be automatically downloaded")
78-
# Use FetchContent to automatically pull and build oneMath
79-
include(FetchContent)
80-
set(BUILD_FUNCTIONAL_TESTS False)
81-
set(BUILD_EXAMPLES False)
82-
set(TARGET_DOMAINS blas)
75+
# Link against Intel oneMKL or oneMath
76+
if (GGML_SYCL_TARGET STREQUAL "INTEL")
77+
# Intel devices use Intel oneMKL directly instead of oneMath to avoid the limitation of linking Intel oneMKL statically
78+
# See https://github.com./uxlfoundation/oneMath/issues/654
79+
find_package(MKL REQUIRED)
80+
target_link_libraries(ggml-sycl PRIVATE MKL::MKL MKL::MKL_SYCL)
81+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_USE_INTEL_ONEMKL)
82+
else()
83+
find_package(oneMath QUIET)
84+
if (NOT oneMath_FOUND)
85+
message("-- oneMath not found: oneMath will be automatically downloaded")
86+
# Use FetchContent to automatically pull and build oneMath
87+
include(FetchContent)
88+
set(BUILD_FUNCTIONAL_TESTS False)
89+
set(BUILD_EXAMPLES False)
90+
set(TARGET_DOMAINS blas)
91+
if (GGML_SYCL_TARGET STREQUAL "NVIDIA")
92+
set(ENABLE_MKLCPU_BACKEND False)
93+
set(ENABLE_MKLGPU_BACKEND False)
94+
set(ENABLE_CUBLAS_BACKEND True)
95+
elseif (GGML_SYCL_TARGET STREQUAL "AMD")
96+
set(ENABLE_MKLCPU_BACKEND False)
97+
set(ENABLE_MKLGPU_BACKEND False)
98+
set(ENABLE_ROCBLAS_BACKEND True)
99+
# Ensure setting a string variable here is not overriden by oneMath CACHE variables
100+
cmake_policy(SET CMP0126 NEW)
101+
# Setting the device architecture is only needed and useful for AMD devices in oneMath
102+
set(HIP_TARGETS ${GGML_SYCL_DEVICE_ARCH} CACHE STRING "oneMath HIP target" FORCE)
103+
endif()
104+
FetchContent_Declare(
105+
ONEMATH
106+
GIT_REPOSITORY https://github.com./uxlfoundation/oneMath.git
107+
GIT_TAG c255b1b4c41e2ee3059455c1f96a965d6a62568a
108+
)
109+
FetchContent_MakeAvailable(ONEMATH)
110+
# Create alias to match with find_package targets name
111+
function(onemath_alias target)
112+
if (TARGET ${target}_obj)
113+
# Silence verbose warnings from external libraries
114+
target_compile_options(${target}_obj PRIVATE -w)
115+
endif()
116+
if (TARGET ${target})
117+
add_library(ONEMATH::${target} ALIAS ${target})
118+
endif()
119+
endfunction()
120+
onemath_alias(onemath)
121+
onemath_alias(onemath_blas_mklcpu)
122+
onemath_alias(onemath_blas_mklgpu)
123+
onemath_alias(onemath_blas_cublas)
124+
onemath_alias(onemath_blas_rocblas)
125+
endif()
126+
127+
# Below oneMath compile-time dispatching is used for better performance
83128
if (GGML_SYCL_TARGET STREQUAL "NVIDIA")
84-
set(ENABLE_MKLCPU_BACKEND False)
85-
set(ENABLE_MKLGPU_BACKEND False)
86-
set(ENABLE_CUBLAS_BACKEND True)
129+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_cublas)
130+
target_compile_options(ggml-sycl PRIVATE "-fsycl-targets=nvptx64-nvidia-cuda")
131+
target_link_options(ggml-sycl PRIVATE "-fsycl-targets=nvptx64-nvidia-cuda")
132+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_NVIDIA)
87133
elseif (GGML_SYCL_TARGET STREQUAL "AMD")
88-
set(ENABLE_MKLCPU_BACKEND False)
89-
set(ENABLE_MKLGPU_BACKEND False)
90-
set(ENABLE_ROCBLAS_BACKEND True)
91-
# Ensure setting a string variable here is not overriden by oneMath CACHE variables
92-
cmake_policy(SET CMP0126 NEW)
93-
# Setting the device architecture is only needed and useful for AMD devices in oneMath
94-
set(HIP_TARGETS ${GGML_SYCL_DEVICE_ARCH} CACHE STRING "oneMath HIP target" FORCE)
95-
endif()
96-
FetchContent_Declare(
97-
ONEMATH
98-
GIT_REPOSITORY https://github.com./uxlfoundation/oneMath.git
99-
GIT_TAG c255b1b4c41e2ee3059455c1f96a965d6a62568a
100-
)
101-
FetchContent_MakeAvailable(ONEMATH)
102-
# Create alias to match with find_package targets name
103-
function(onemath_alias target)
104-
if (TARGET ${target}_obj)
105-
# Silence verbose warnings from external libraries
106-
target_compile_options(${target}_obj PRIVATE -w)
134+
if (NOT GGML_SYCL_DEVICE_ARCH)
135+
message(ERROR "Can't enable SYCL hip backend, GGML_SYCL_DEVICE_ARCH has not been set.")
107136
endif()
108-
if (TARGET ${target})
109-
add_library(ONEMATH::${target} ALIAS ${target})
110-
endif()
111-
endfunction()
112-
onemath_alias(onemath)
113-
onemath_alias(onemath_blas_mklcpu)
114-
onemath_alias(onemath_blas_mklgpu)
115-
onemath_alias(onemath_blas_cublas)
116-
onemath_alias(onemath_blas_rocblas)
117-
endif()
118-
119-
# Below oneMath compile-time dispatching is used for better performance
120-
if (GGML_SYCL_TARGET STREQUAL "INTEL")
121-
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_mklgpu)
122-
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_INTEL)
123-
elseif (GGML_SYCL_TARGET STREQUAL "NVIDIA")
124-
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_cublas)
125-
target_compile_options(ggml-sycl PRIVATE "-fsycl-targets=nvptx64-nvidia-cuda")
126-
target_link_options(ggml-sycl PRIVATE "-fsycl-targets=nvptx64-nvidia-cuda")
127-
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_NVIDIA)
128-
elseif (GGML_SYCL_TARGET STREQUAL "AMD")
129-
if (NOT GGML_SYCL_DEVICE_ARCH)
130-
message(ERROR "Can't enable SYCL hip backend, GGML_SYCL_DEVICE_ARCH has not been set.")
137+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_rocblas)
138+
target_compile_options(ggml-sycl PRIVATE "-fsycl-targets=amdgcn-amd-amdhsa")
139+
target_link_options(ggml-sycl PRIVATE "-fsycl-targets=amdgcn-amd-amdhsa")
140+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_AMD)
141+
else()
142+
# Fallback to oneMath runtime dispatcher
143+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath)
144+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_GENERIC)
131145
endif()
132-
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_rocblas)
133-
target_compile_options(ggml-sycl PRIVATE "-fsycl-targets=amdgcn-amd-amdhsa")
134-
target_link_options(ggml-sycl PRIVATE "-fsycl-targets=amdgcn-amd-amdhsa")
135-
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_AMD)
136-
else()
137-
# Fallback to oneMath runtime dispatcher
138-
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath)
139-
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_GENERIC)
140146
endif()
141147

142148
if (GGML_SYCL_DEVICE_ARCH)

ggml/src/ggml-sycl/dpct/helper.hpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@
1616
#include <sycl/sycl.hpp>
1717
#include <sycl/half_type.hpp>
1818
#include <syclcompat/math.hpp>
19-
#include <oneapi/math.hpp>
2019
#include <map>
2120

21+
#ifdef GGML_SYCL_USE_INTEL_ONEMKL
22+
#include <oneapi/mkl.hpp>
23+
// Allow to use the same namespace for Intel oneMKL and oneMath
24+
namespace oneapi {
25+
namespace math = mkl;
26+
}
27+
#else
28+
#include <oneapi/math.hpp>
29+
#endif
30+
2231
#include "ggml.h"
2332

2433
#if defined(__linux__)
@@ -91,20 +100,18 @@ template <typename Ts> struct matrix_info_t {
91100
};
92101

93102
inline auto get_onemath_backend(sycl::queue& queue)
94-
#ifdef GGML_SYCL_GENERIC
103+
#if defined(GGML_SYCL_GENERIC) || defined(GGML_SYCL_USE_INTEL_ONEMKL)
95104
-> sycl::queue&
96105
#endif
97106
{
98107
// If the backend is known at compile-time, use oneMath backend_selector to use
99108
// compile-time dispatching and avoid the need to dlopen libraries. Otherwise
100109
// fallback to runtime dispatching.
101-
#if defined(GGML_SYCL_INTEL)
102-
return oneapi::math::backend_selector<oneapi::math::backend::mklgpu>{ queue };
103-
#elif defined(GGML_SYCL_NVIDIA)
110+
#if defined(GGML_SYCL_NVIDIA)
104111
return oneapi::math::backend_selector<oneapi::math::backend::cublas>{ queue };
105112
#elif defined(GGML_SYCL_AMD)
106113
return oneapi::math::backend_selector<oneapi::math::backend::rocblas>{ queue };
107-
#elif defined(GGML_SYCL_GENERIC)
114+
#elif defined(GGML_SYCL_GENERIC) || defined(GGML_SYCL_USE_INTEL_ONEMKL)
108115
return queue;
109116
#else
110117
static_assert(false, "Unsupported backend");

0 commit comments

Comments
 (0)