Skip to content

Commit dccc9f8

Browse files
committed
Use oneMath for Intel vendor
1 parent a856351 commit dccc9f8

File tree

4 files changed

+77
-33
lines changed

4 files changed

+77
-33
lines changed

docs/backend/SYCL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ use 1 SYCL GPUs: [0] with Max compute units:512
648648
| Name | Value | Function |
649649
|--------------------|---------------------------------------|---------------------------------------------|
650650
| GGML_SYCL | ON (mandatory) | Enable build with SYCL code path.<br>FP32 path - recommended for better perforemance than FP16 on quantized model|
651-
| GGML_SYCL_TARGET | INTEL *(default)* \| NVIDIA \| AMD | Set the SYCL target device type. |
651+
| GGML_SYCL_TARGET | INTEL *(default)* \| INTEL_CPU \| INTEL_GPU \| NVIDIA \| AMD | Set the SYCL target device type. |
652652
| GGML_SYCL_DEVICE_ARCH | Optional (except for AMD) | Set the SYCL device architecture, optional except for AMD. Setting the device architecture can improve the performance. See the table [--offload-arch](https://github.com/intel/llvm/blob/sycl/sycl/doc/design/OffloadDesign.md#--offload-arch) for a list of valid architectures. |
653653
| GGML_SYCL_F16 | OFF *(default)* \|ON *(optional)* | Enable FP16 build with SYCL code path. |
654654
| CMAKE_C_COMPILER | `icx` *(Linux)*, `icx/cl` *(Windows)* | Set `icx` compiler for SYCL code path. |

examples/sycl/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ List all SYCL devices with ID, compute capability, max work group size, ect.
1414

1515
1. Build the llama.cpp for SYCL for the specified target *(using GGML_SYCL_TARGET)*.
1616

17-
2. Enable oneAPI running environment *(if GGML_SYCL_TARGET is set to INTEL -default-)*
17+
2. Enable oneAPI running environment *(if GGML_SYCL_TARGET is set to INTEL -default-, INTEL_CPU or INTEL_GPU)*
1818

1919
```
2020
source /opt/intel/oneapi/setvars.sh

ggml/cmake/ggml-config.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ if (NOT GGML_SHARED_LIB)
7878

7979
if (GGML_SYCL)
8080
find_package(DNNL)
81-
if (${DNNL_FOUND} AND GGML_SYCL_TARGET STREQUAL "INTEL")
81+
if (${DNNL_FOUND} AND GGML_SYCL_TARGET MATCHES "INTEL.*")
8282
list(APPEND GGML_SYCL_INTERFACE_LINK_LIBRARIES DNNL::dnnl)
8383
endif()
8484
if (WIN32)

ggml/src/ggml-sycl/CMakeLists.txt

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
message(STATUS "GGML_SYCL_TARGET=${GGML_SYCL_TARGET}")
22

3-
if (NOT GGML_SYCL_TARGET MATCHES "^(INTEL|NVIDIA|AMD)$")
3+
if (NOT GGML_SYCL_TARGET MATCHES "^(INTEL.*|NVIDIA|AMD)$")
44
message(FATAL_ERROR "Invalid backend chosen, supported options are INTEL, NVIDIA, or AMD")
55
endif()
66

@@ -30,8 +30,6 @@ if (GGML_SYCL_F16)
3030
add_compile_definitions(GGML_SYCL_F16)
3131
endif()
3232

33-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-narrowing -fsycl")
34-
3533
if (GGML_SYCL_TARGET STREQUAL "NVIDIA")
3634
add_compile_definitions(GGML_SYCL_WARP_SIZE=32)
3735
elseif (GGML_SYCL_TARGET STREQUAL "AMD")
@@ -51,44 +49,90 @@ target_sources(ggml-sycl PRIVATE ${GGML_HEADERS_SYCL} ${GGML_SOURCES_SYCL})
5149
find_package(DNNL)
5250
message("-- DNNL found:" ${DNNL_FOUND})
5351

54-
if (GGML_SYCL_TARGET STREQUAL "INTEL")
52+
if (GGML_SYCL_TARGET MATCHES "INTEL.*")
5553
add_compile_definitions(GGML_SYCL_DNNL=${DNNL_FOUND})
5654
else()
5755
add_compile_definitions(GGML_SYCL_DNNL=0)
5856
endif()
5957

60-
if (${DNNL_FOUND} AND GGML_SYCL_TARGET STREQUAL "INTEL")
58+
if (${DNNL_FOUND} AND GGML_SYCL_TARGET MATCHES "INTEL.*")
6159
target_link_libraries(ggml-sycl PRIVATE DNNL::dnnl)
6260
endif()
6361

64-
if (WIN32)
65-
find_package(IntelSYCL REQUIRED)
66-
find_package(MKL REQUIRED)
67-
target_link_libraries(ggml-sycl PRIVATE IntelSYCL::SYCL_CXX MKL::MKL MKL::MKL_SYCL)
62+
find_package(IntelSYCL)
63+
if (IntelSYCL_FOUND)
64+
# Use oneAPI CMake when possible
65+
target_link_libraries(ggml-sycl PRIVATE IntelSYCL::SYCL_CXX)
6866
else()
69-
if (GGML_SYCL_TARGET STREQUAL "INTEL")
70-
target_link_libraries(ggml-sycl PRIVATE sycl OpenCL mkl_core pthread m dl mkl_sycl_blas mkl_intel_ilp64 mkl_tbb_thread)
71-
elseif (GGML_SYCL_TARGET STREQUAL "NVIDIA")
72-
add_compile_definitions(GGML_SYCL_NVIDIA)
73-
find_package(oneMath REQUIRED)
74-
target_link_libraries(ggml-sycl PRIVATE sycl pthread m dl ONEMATH::onemath_blas_cublas)
75-
target_compile_options(ggml-sycl PRIVATE "-fsycl-targets=nvptx64-nvidia-cuda")
76-
# Disable warnings for using deprecated oneapi::mkl namespace in oneMath
77-
# Using the deprecated API in oneMath is useful to have a similar API than Intel oneMKL
78-
target_compile_options(ggml-sycl PRIVATE "-Wno-deprecated-declarations")
67+
# Fallback to the simplest way of enabling SYCL when using intel/llvm nightly for instance
68+
target_compile_options(ggml-sycl PRIVATE "-fsycl")
69+
target_link_options(ggml-sycl PRIVATE "-fsycl")
70+
endif()
71+
72+
target_compile_options(ggml-sycl PRIVATE "-Wno-narrowing")
73+
74+
find_package(oneMath QUIET)
75+
if (NOT oneMath_FOUND)
76+
message("-- oneMath not found: oneMath will be automatically downloaded")
77+
# Use FetchContent to automatically pull and build oneMath
78+
include(FetchContent)
79+
set(BUILD_FUNCTIONAL_TESTS False)
80+
set(BUILD_EXAMPLES False)
81+
set(TARGET_DOMAINS blas)
82+
if (GGML_SYCL_TARGET STREQUAL "NVIDIA")
83+
set(ENABLE_MKLCPU_BACKEND False)
84+
set(ENABLE_MKLGPU_BACKEND False)
85+
set(ENABLE_CUBLAS_BACKEND True)
7986
elseif (GGML_SYCL_TARGET STREQUAL "AMD")
80-
if (NOT GGML_SYCL_DEVICE_ARCH)
81-
message(ERROR "Can't enable SYCL hip backend, GGML_SYCL_DEVICE_ARCH has not been set.")
82-
endif()
83-
find_package(oneMath REQUIRED)
84-
target_link_libraries(ggml-sycl PRIVATE sycl pthread m dl onemath)
85-
target_compile_options(ggml-sycl PRIVATE "-fsycl-targets=amdgcn-amd-amdhsa")
86-
# Disable warnings for using deprecated oneapi::mkl namespace in oneMath
87-
# Using the deprecated API in oneMath is useful to have a similar API than Intel oneMKL
88-
target_compile_options(ggml-sycl PRIVATE "-Wno-deprecated-declarations")
87+
set(ENABLE_MKLCPU_BACKEND False)
88+
set(ENABLE_MKLGPU_BACKEND False)
89+
set(ENABLE_ROCBLAS_BACKEND True)
8990
endif()
91+
FetchContent_Declare(
92+
ONEMATH
93+
GIT_REPOSITORY https://github.com/uxlfoundation/oneMath.git
94+
GIT_TAG develop
95+
)
96+
FetchContent_MakeAvailable(ONEMATH)
97+
# Create alias to match with find_package targets name
98+
function(onemath_alias target)
99+
if (TARGET ${target})
100+
add_library(ONEMATH::${target} ALIAS ${target})
101+
endif()
102+
endfunction()
103+
onemath_alias(onemath_blas_mklcpu)
104+
onemath_alias(onemath_blas_mklgpu)
105+
onemath_alias(onemath_blas_cublas)
106+
onemath_alias(onemath_blas_rocblas)
107+
endif()
90108

91-
if (GGML_SYCL_DEVICE_ARCH)
92-
target_compile_options(ggml-sycl PRIVATE "-Xsycl-target-backend --offload-arch=${GGML_SYCL_DEVICE_ARCH}")
109+
# Below oneMath compile-time dispatching is used for better performance
110+
if (GGML_SYCL_TARGET STREQUAL "INTEL_CPU")
111+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_mklcpu)
112+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_INTEL GGML_SYCL_INTEL_CPU)
113+
elseif (GGML_SYCL_TARGET STREQUAL "INTEL_GPU")
114+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_mklgpu)
115+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_INTEL GGML_SYCL_INTEL_GPU)
116+
elseif (GGML_SYCL_TARGET STREQUAL "NVIDIA")
117+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_cublas)
118+
target_compile_options(ggml-sycl PRIVATE "-fsycl-targets=nvptx64-nvidia-cuda")
119+
target_link_options(ggml-sycl PRIVATE "-fsycl-targets=nvptx64-nvidia-cuda")
120+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_NVIDIA)
121+
elseif (GGML_SYCL_TARGET STREQUAL "AMD")
122+
if (NOT GGML_SYCL_DEVICE_ARCH)
123+
message(ERROR "Can't enable SYCL hip backend, GGML_SYCL_DEVICE_ARCH has not been set.")
93124
endif()
125+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath_blas_rocblas)
126+
target_compile_options(ggml-sycl PRIVATE "-fsycl-targets=amdgcn-amd-amdhsa")
127+
target_link_options(ggml-sycl PRIVATE "-fsycl-targets=amdgcn-amd-amdhsa")
128+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_AMD)
129+
else()
130+
# Fallback to oneMath runtime dispatcher
131+
target_link_libraries(ggml-sycl PRIVATE ONEMATH::onemath)
132+
target_compile_definitions(ggml-sycl PRIVATE GGML_SYCL_GENERIC)
133+
endif()
134+
135+
if (GGML_SYCL_DEVICE_ARCH)
136+
target_compile_options(ggml-sycl PRIVATE -Xsycl-target-backend --offload-arch=${GGML_SYCL_DEVICE_ARCH})
137+
target_link_options(ggml-sycl PRIVATE -Xsycl-target-backend --offload-arch=${GGML_SYCL_DEVICE_ARCH})
94138
endif()

0 commit comments

Comments
 (0)