Skip to content

Commit 1731d42

Browse files
authored
[SYCL] Add oneDNN primitive support (#9091)
* add onednn * add sycl_f16 * add dnnl stream * add engine map * use dnnl for intel only * use fp16fp16fp16 * update doc
1 parent a1631e5 commit 1731d42

File tree

6 files changed

+186
-10
lines changed

6 files changed

+186
-10
lines changed

CMakePresets.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
{ "name": "release", "hidden": true, "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } },
2929
{ "name": "reldbg", "hidden": true, "cacheVariables": { "CMAKE_BUILD_TYPE": "RelWithDebInfo" } },
3030
{ "name": "static", "hidden": true, "cacheVariables": { "GGML_STATIC": "ON" } },
31+
{ "name": "sycl_f16", "hidden": true, "cacheVariables": { "GGML_SYCL_F16": "ON" } },
3132

3233
{
3334
"name": "arm64-windows-msvc", "hidden": true,
@@ -60,6 +61,8 @@
6061
{ "name": "x64-windows-msvc+static-release", "inherits": [ "base", "reldbg", "static" ] },
6162

6263
{ "name": "x64-windows-sycl-debug" , "inherits": [ "sycl-base", "debug" ] },
63-
{ "name": "x64-windows-sycl-release", "inherits": [ "sycl-base", "release" ] }
64+
{ "name": "x64-windows-sycl-debug-f16", "inherits": [ "sycl-base", "debug", "sycl_f16" ] },
65+
{ "name": "x64-windows-sycl-release", "inherits": [ "sycl-base", "release" ] },
66+
{ "name": "x64-windows-sycl-release-f16", "inherits": [ "sycl-base", "release", "sycl_f16" ] }
6467
]
6568
}

docs/backend/SYCL.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@
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 - Math Kernel Library)*.
23+
- **oneAPI Libraries**: A set of highly optimized libraries targeting multiple domains *(e.g. oneMKL 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

2727
### Llama.cpp + SYCL
2828

2929
The llama.cpp SYCL backend is designed to support **Intel GPU** firstly. Based on the cross-platform feature of SYCL, it could support other vendor GPUs: Nvidia GPU (*AMD GPU coming*).
3030

31-
When targeting **Intel CPU**, it is recommended to use llama.cpp for [Intel oneMKL](README.md#intel-onemkl) backend.
32-
33-
It has the similar design of other llama.cpp BLAS-based paths such as *OpenBLAS, cuBLAS, etc..*. In beginning work, the oneAPI's [SYCLomatic](https://github.com/oneapi-src/SYCLomatic) open-source migration tool (Commercial release [Intel® DPC++ Compatibility Tool](https://www.intel.com/content/www/us/en/developer/tools/oneapi/dpc-compatibility-tool.html)) was used for this purpose.
34-
3531
## Recommended Release
3632

3733
The SYCL backend would be broken by some PRs due to no online CI.
@@ -45,6 +41,10 @@ The following release is verified with good quality:
4541

4642
## News
4743

44+
45+
- 2024.8
46+
- Use oneDNN as the default GEMM library, improve the compatibility for new Intel GPUs.
47+
4848
- 2024.5
4949
- Performance is increased: 34 -> 37 tokens/s of llama-2-7b.Q4_0 on Arc770.
5050
- Arch Linux is verified successfully.
@@ -196,7 +196,7 @@ Please follow the instructions for downloading and installing the Toolkit for Li
196196

197197
Following guidelines/code snippets assume the default installation values. Otherwise, please make sure the necessary changes are reflected where applicable.
198198

199-
Upon a successful installation, SYCL is enabled for the available intel devices, along with relevant libraries such as oneAPI MKL for intel GPUs.
199+
Upon a successful installation, SYCL is enabled for the available intel devices, along with relevant libraries such as oneAPI oneDNN for Intel GPUs.
200200

201201
- **Adding support to Nvidia GPUs**
202202

@@ -255,8 +255,6 @@ or
255255
# Export relevant ENV variables
256256
source /opt/intel/oneapi/setvars.sh
257257

258-
# Build LLAMA with MKL BLAS acceleration for intel GPU
259-
260258
# Option 1: Use FP32 (recommended for better performance in most cases)
261259
cmake -B build -DGGML_SYCL=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx
262260

ggml/src/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,13 @@ if (GGML_SYCL)
549549
file(GLOB GGML_SOURCES_SYCL "ggml-sycl/*.cpp")
550550
list(APPEND GGML_SOURCES_SYCL "ggml-sycl.cpp")
551551

552+
find_package(DNNL)
553+
message("-- DNNL found:"${DNNL_FOUND})
554+
if (GGML_SYCL_TARGET STREQUAL "INTEL")
555+
add_compile_definitions(GGML_SYCL_DNNL=${DNNL_FOUND})
556+
else()
557+
add_compile_definitions(GGML_SYCL_DNNL=0)
558+
endif()
552559
if (WIN32)
553560
find_package(IntelSYCL REQUIRED)
554561
find_package(MKL REQUIRED)
@@ -561,6 +568,9 @@ if (GGML_SYCL)
561568
set(GGML_EXTRA_LIBS ${GGML_EXTRA_LIBS} -fsycl pthread m dl onemkl)
562569
endif()
563570
endif()
571+
if (${DNNL_FOUND} AND GGML_SYCL_TARGET STREQUAL "INTEL")
572+
list(APPEND GGML_EXTRA_LIBS DNNL::dnnl)
573+
endif()
564574
endif()
565575

566576
if (GGML_RPC)

ggml/src/ggml-sycl.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include "ggml-sycl/backend.hpp"
4040
#include "ggml-sycl/presets.hpp"
41+
#include "ggml-sycl/gemm.hpp"
4142

4243
bool ggml_sycl_loaded(void);
4344
void ggml_sycl_free_data(struct ggml_tensor * tensor);
@@ -2482,6 +2483,7 @@ inline void ggml_sycl_op_mul_mat_sycl(
24822483

24832484
const sycl::half alpha_f16 = 1.0f;
24842485
const sycl::half beta_f16 = 0.0f;
2486+
#if !GGML_SYCL_DNNL
24852487
SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
24862488
*stream, oneapi::mkl::transpose::trans,
24872489
oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
@@ -2491,6 +2493,13 @@ inline void ggml_sycl_op_mul_mat_sycl(
24912493
dpct::library_data_t::real_half)));
24922494
const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
24932495
to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
2496+
#else
2497+
auto dnnl_stream = ctx.stream_dnnl(stream);
2498+
DnnlGemmWrapper::row_gemm(dnnl_stream, false, true, src1_ncols, row_diff, ne10, src1_ptr, DnnlGemmWrapper::to_dt<sycl::half>(),
2499+
src0_ptr, DnnlGemmWrapper::to_dt<sycl::half>(), dst_f16.get(), DnnlGemmWrapper::to_dt<sycl::half>());
2500+
const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
2501+
to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff* src1_ncols, stream);
2502+
#endif
24942503
}
24952504
else {
24962505
// GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
@@ -2513,13 +2522,18 @@ inline void ggml_sycl_op_mul_mat_sycl(
25132522

25142523
const float alpha = 1.0f;
25152524
const float beta = 0.0f;
2516-
2525+
#if !GGML_SYCL_DNNL
25172526
SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
25182527
*stream, oneapi::mkl::transpose::trans,
25192528
oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
25202529
dpct::get_value(&alpha, *stream), src0_ddf_i, ne00,
25212530
src1_ddf1_i, ne10, dpct::get_value(&beta, *stream),
25222531
dst_dd_i, ldc)));
2532+
#else
2533+
auto dnnl_stream = ctx.stream_dnnl(stream);
2534+
DnnlGemmWrapper::row_gemm(dnnl_stream, false, true, src1_ncols, row_diff, ne10, src1_ddf1_i, DnnlGemmWrapper::to_dt<float>(),
2535+
src0_ddf_i, DnnlGemmWrapper::to_dt<float>(), dst_dd_i, DnnlGemmWrapper::to_dt<float>());
2536+
#endif
25232537
}
25242538
(void) dst;
25252539
(void) src1_ddq_i;

ggml/src/ggml-sycl/common.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "dpct/helper.hpp"
2020
#include "ggml-sycl.h"
2121
#include "presets.hpp"
22+
#if GGML_SYCL_DNNL
23+
#include "dnnl.hpp"
24+
#include "dnnl_sycl.hpp"
25+
#endif
2226

2327
#define GGML_COMMON_DECL_SYCL
2428
#define GGML_COMMON_IMPL_SYCL
@@ -277,6 +281,52 @@ struct ggml_backend_sycl_context {
277281
return stream(device, 0);
278282
}
279283

284+
#if GGML_SYCL_DNNL
285+
dnnl::engine make_engine(sycl::queue* q) {
286+
// Get the device associated with the queue
287+
sycl::device dev = q->get_device();
288+
// Get the context associated with the queue
289+
sycl::context ctx = q->get_context();
290+
const dnnl::engine eng = dnnl::sycl_interop::make_engine(dev, ctx);
291+
return eng;
292+
}
293+
294+
std::unordered_map<sycl::queue*, dnnl::stream> stream_map;
295+
std::unordered_map<sycl::queue*, dnnl::engine> engine_map;
296+
dnnl::stream stream_dnnl(int device, int _stream) {
297+
auto q = stream(device, _stream);
298+
return stream_dnnl(q);
299+
}
300+
dnnl::engine engine_dnnl(sycl::queue* qptr) {
301+
auto it = engine_map.find(qptr);
302+
if (it == engine_map.end()) {
303+
auto eng = make_engine(qptr);
304+
engine_map[qptr] = eng;
305+
return eng;
306+
}
307+
else
308+
{
309+
return it->second;
310+
}
311+
}
312+
dnnl::stream stream_dnnl(sycl::queue* qptr) {
313+
auto it = stream_map.find(qptr);
314+
if (it == stream_map.end()) {
315+
auto eng = engine_dnnl(qptr);
316+
auto stream = dnnl::sycl_interop::make_stream(eng, *qptr);
317+
stream_map[qptr] = stream;
318+
return stream;
319+
}
320+
else
321+
{
322+
return it->second;
323+
}
324+
}
325+
dnnl::stream stream_dnnl() {
326+
return stream_dnnl(device, 0);
327+
}
328+
#endif
329+
280330
// pool
281331
std::unique_ptr<ggml_sycl_pool> pools[GGML_SYCL_MAX_DEVICES];
282332

ggml/src/ggml-sycl/gemm.hpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//
2+
// MIT license
3+
// Copyright (C) 2024 Intel Corporation
4+
// SPDX-License-Identifier: MIT
5+
//
6+
7+
//
8+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9+
// See https://llvm.org/LICENSE.txt for license information.
10+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11+
//
12+
13+
#ifndef GGML_SYCL_GEMM_HPP
14+
#define GGML_SYCL_GEMM_HPP
15+
16+
#include <fstream>
17+
#include <iostream>
18+
19+
#include "ggml-sycl.h"
20+
21+
#if GGML_SYCL_DNNL
22+
23+
#include "dnnl.hpp"
24+
#include "dnnl_sycl.hpp"
25+
26+
class DnnlGemmWrapper {
27+
public:
28+
using dt = dnnl::memory::data_type;
29+
using tag = dnnl::memory::format_tag;
30+
31+
template<typename T>
32+
static constexpr dt to_dt() {
33+
if constexpr (std::is_same_v<T, float>) return dt::f32;
34+
else if constexpr (std::is_same_v<T, sycl::half>) return dt::f16;
35+
else static_assert(0);
36+
}
37+
38+
static inline void row_gemm(sycl::queue& q, bool a_trans,
39+
bool b_trans, int m, int n, int k,
40+
const void* a, dt at, const void* b, dt bt, void* c, dt ct)
41+
{
42+
// Get the device associated with the queue
43+
sycl::device dev = q.get_device();
44+
// Get the context associated with the queue
45+
sycl::context ctx = q.get_context();
46+
const dnnl::engine eng = dnnl::sycl_interop::make_engine(dev, ctx);
47+
const dnnl::stream stream = dnnl::sycl_interop::make_stream(eng, q);
48+
dnnl::memory::dims a_dims = { m, k };
49+
dnnl::memory::dims b_dims = { k, n };
50+
dnnl::memory::dims c_dims = { m, n };
51+
const auto a_in_md = dnnl::memory::desc(a_dims, at, a_trans ? tag::ba : tag::ab);
52+
const auto b_in_md = dnnl::memory::desc(b_dims, bt, b_trans ? tag::ba : tag::ab);
53+
const auto c_md = dnnl::memory::desc(c_dims, ct, tag::ab);
54+
auto a_mem = dnnl::memory(a_in_md, eng, (void*)a);
55+
auto b_mem = dnnl::memory(b_in_md, eng, (void*)b);
56+
auto matmul_pd = dnnl::matmul::primitive_desc(eng, a_in_md, b_in_md, c_md);
57+
auto c_mem = dnnl::memory(matmul_pd.dst_desc(), eng, c);
58+
59+
// Create the primitive.
60+
auto matmul_prim = dnnl::matmul(matmul_pd);
61+
// Primitive arguments.
62+
std::unordered_map<int, dnnl::memory> matmul_args;
63+
matmul_args.insert({ DNNL_ARG_SRC, a_mem });
64+
matmul_args.insert({ DNNL_ARG_WEIGHTS, b_mem });
65+
matmul_args.insert({ DNNL_ARG_DST, c_mem });
66+
67+
matmul_prim.execute(stream, matmul_args);
68+
}
69+
70+
71+
static inline void row_gemm(const dnnl::stream& stream, bool a_trans,
72+
bool b_trans, int m, int n, int k,
73+
const void* a, dt at, const void* b, dt bt, void* c, dt ct)
74+
{
75+
auto const eng = stream.get_engine();
76+
dnnl::memory::dims a_dims = { m, k };
77+
dnnl::memory::dims b_dims = { k, n };
78+
dnnl::memory::dims c_dims = { m, n };
79+
const auto a_in_md = dnnl::memory::desc(a_dims, at, a_trans ? tag::ba : tag::ab);
80+
const auto b_in_md = dnnl::memory::desc(b_dims, bt, b_trans ? tag::ba : tag::ab);
81+
const auto c_md = dnnl::memory::desc(c_dims, ct, tag::ab);
82+
auto a_mem = dnnl::memory(a_in_md, eng, (void*)a);
83+
auto b_mem = dnnl::memory(b_in_md, eng, (void*)b);
84+
auto matmul_pd = dnnl::matmul::primitive_desc(eng, a_in_md, b_in_md, c_md);
85+
auto c_mem = dnnl::memory(matmul_pd.dst_desc(), eng, c);
86+
87+
// Create the primitive.
88+
auto matmul_prim = dnnl::matmul(matmul_pd);
89+
// Primitive arguments.
90+
std::unordered_map<int, dnnl::memory> matmul_args;
91+
matmul_args.insert({ DNNL_ARG_SRC, a_mem });
92+
matmul_args.insert({ DNNL_ARG_WEIGHTS, b_mem });
93+
matmul_args.insert({ DNNL_ARG_DST, c_mem });
94+
95+
matmul_prim.execute(stream, matmul_args);
96+
}
97+
};
98+
99+
#endif
100+
101+
#endif // GGML_SYCL_GEMM_HPP

0 commit comments

Comments
 (0)