Skip to content

Commit 8293970

Browse files
authored
SYCL: Rename oneMKL to oneMath (#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 8bbf260 commit 8293970

File tree

5 files changed

+188
-180
lines changed

5 files changed

+188
-180
lines changed

docs/backend/SYCL.md

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
**oneAPI** is an open ecosystem and a standard-based specification, supporting multiple architectures including but not limited to intel CPUs, GPUs and FPGAs. The key components of the oneAPI ecosystem include:
2121

2222
- **DPCPP** *(Data Parallel C++)*: The primary oneAPI SYCL implementation, which includes the icpx/icx Compilers.
23-
- **oneAPI Libraries**: A set of highly optimized libraries targeting multiple domains *(e.g. oneMKL and oneDNN)*.
23+
- **oneAPI Libraries**: A set of highly optimized libraries targeting multiple domains *(e.g. Intel oneMKL, oneMath and oneDNN)*.
2424
- **oneAPI LevelZero**: A high performance low level interface for fine-grained control over intel iGPUs and dGPUs.
2525
- **Nvidia & AMD Plugins**: These are plugins extending oneAPI's DPCPP support to SYCL on Nvidia and AMD GPU targets.
2626

@@ -227,16 +227,6 @@ Upon a successful installation, SYCL is enabled for the available intel devices,
227227

228228
**oneAPI Plugin**: In order to enable SYCL support on Nvidia GPUs, please install the [Codeplay oneAPI Plugin for Nvidia GPUs](https://developer.codeplay.com/products/oneapi/nvidia/download). User should also make sure the plugin version matches the installed base toolkit one *(previous step)* for a seamless "oneAPI on Nvidia GPU" setup.
229229

230-
231-
**oneMKL for cuBlas**: The current oneMKL releases *(shipped with the oneAPI base-toolkit)* do not contain the cuBLAS backend. A build from source of the upstream [oneMKL](https://github.com/oneapi-src/oneMKL) with the *cuBLAS* backend enabled is thus required to run it on Nvidia GPUs.
232-
233-
```sh
234-
git clone https://github.com/oneapi-src/oneMKL
235-
cd oneMKL
236-
cmake -B buildWithCublas -DCMAKE_CXX_COMPILER=icpx -DCMAKE_C_COMPILER=icx -DENABLE_MKLGPU_BACKEND=OFF -DENABLE_MKLCPU_BACKEND=OFF -DENABLE_CUBLAS_BACKEND=ON -DTARGET_DOMAINS=blas
237-
cmake --build buildWithCublas --config Release
238-
```
239-
240230
**oneDNN**: The current oneDNN releases *(shipped with the oneAPI base-toolkit)* do not include the NVIDIA backend. Therefore, oneDNN must be compiled from source to enable the NVIDIA target:
241231

242232
```sh
@@ -250,16 +240,6 @@ cmake --build build-nvidia --config Release
250240

251241
**oneAPI Plugin**: In order to enable SYCL support on AMD GPUs, please install the [Codeplay oneAPI Plugin for AMD GPUs](https://developer.codeplay.com/products/oneapi/amd/download). As with Nvidia GPUs, the user should also make sure the plugin version matches the installed base toolkit.
252242

253-
**oneMKL for rocBlas**: The current oneMKL releases *(shipped with the oneAPI base-toolkit)* doesn't contain the rocBLAS backend. A build from source of the upstream [oneMKL](https://github.com/oneapi-src/oneMKL) with the *rocBLAS* backend enabled is thus required to run it on AMD GPUs.
254-
255-
```sh
256-
git clone https://github.com/oneapi-src/oneMKL
257-
cd oneMKL
258-
# Find your HIPTARGET with rocminfo, under the key 'Name:'
259-
cmake -B buildWithrocBLAS -DCMAKE_CXX_COMPILER=icpx -DCMAKE_C_COMPILER=icx -DENABLE_MKLGPU_BACKEND=OFF -DENABLE_MKLCPU_BACKEND=OFF -DENABLE_ROCBLAS_BACKEND=ON -DHIPTARGETS=${HIPTARGET} -DTARGET_DOMAINS=blas
260-
cmake --build buildWithrocBLAS --config Release
261-
```
262-
263243
3. **Verify installation and environment**
264244

265245
In order to check the available SYCL devices on the machine, please use the `sycl-ls` command.
@@ -324,13 +304,10 @@ cmake --build build --config Release -j -v
324304

325305
#### Nvidia GPU
326306

327-
```sh
328-
# Export relevant ENV variables
329-
export LD_LIBRARY_PATH=/path/to/oneMKL/buildWithCublas/lib:$LD_LIBRARY_PATH
330-
export LIBRARY_PATH=/path/to/oneMKL/buildWithCublas/lib:$LIBRARY_PATH
331-
export CPLUS_INCLUDE_DIR=/path/to/oneMKL/buildWithCublas/include:$CPLUS_INCLUDE_DIR
332-
export CPLUS_INCLUDE_DIR=/path/to/oneMKL/include:$CPLUS_INCLUDE_DIR
307+
The SYCL backend depends on [oneMath](https://github.com/uxlfoundation/oneMath) for Nvidia and AMD devices.
308+
By default it is automatically built along with the project. A specific build can be provided by setting the CMake flag `-DoneMath_DIR=/path/to/oneMath/install/lib/cmake/oneMath`.
333309

310+
```sh
334311
# Build LLAMA with Nvidia BLAS acceleration through SYCL
335312
# Setting GGML_SYCL_DEVICE_ARCH is optional but can improve performance
336313
GGML_SYCL_DEVICE_ARCH=sm_80 # Example architecture
@@ -347,12 +324,10 @@ cmake --build build --config Release -j -v
347324

348325
#### AMD GPU
349326

350-
```sh
351-
# Export relevant ENV variables
352-
export LD_LIBRARY_PATH=/path/to/oneMKL/buildWithrocBLAS/lib:$LD_LIBRARY_PATH
353-
export LIBRARY_PATH=/path/to/oneMKL/buildWithrocBLAS/lib:$LIBRARY_PATH
354-
export CPLUS_INCLUDE_DIR=/path/to/oneMKL/buildWithrocBLAS/include:$CPLUS_INCLUDE_DIR
327+
The SYCL backend depends on [oneMath](https://github.com/uxlfoundation/oneMath) for Nvidia and AMD devices.
328+
By default it is automatically built along with the project. A specific build can be provided by setting the CMake flag `-DoneMath_DIR=/path/to/oneMath/install/lib/cmake/oneMath`.
355329

330+
```sh
356331
# Build LLAMA with rocBLAS acceleration through SYCL
357332

358333
## AMD

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)