Skip to content

Commit 79d2005

Browse files
committed
add dnnl stream
1 parent bed2c0c commit 79d2005

File tree

3 files changed

+69
-35
lines changed

3 files changed

+69
-35
lines changed

ggml/src/ggml-sycl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,7 +2557,8 @@ inline void ggml_sycl_op_mul_mat_sycl(
25572557
const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
25582558
to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
25592559
#else
2560-
DnnlGemmWrapper::row_gemm(*stream, false, true, src1_ncols, row_diff, ne10, src1_ptr, DnnlGemmWrapper::to_dt<sycl::half>(),
2560+
auto dnnl_stream = ctx.stream_dnnl(stream);
2561+
DnnlGemmWrapper::row_gemm(dnnl_stream, false, true, src1_ncols, row_diff, ne10, src1_ptr, DnnlGemmWrapper::to_dt<sycl::half>(),
25612562
src0_ptr, DnnlGemmWrapper::to_dt<sycl::half>(), dst_f16.get(), DnnlGemmWrapper::to_dt<sycl::half>());
25622563
const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
25632564
to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff* src1_ncols, stream);
@@ -2592,7 +2593,8 @@ inline void ggml_sycl_op_mul_mat_sycl(
25922593
src1_ddf1_i, ne10, dpct::get_value(&beta, *stream),
25932594
dst_dd_i, ldc)));
25942595
#else
2595-
DnnlGemmWrapper::row_gemm(*stream, false, true, src1_ncols, row_diff, ne10, src1_ddf1_i, DnnlGemmWrapper::to_dt<float>(),
2596+
auto dnnl_stream = ctx.stream_dnnl(stream);
2597+
DnnlGemmWrapper::row_gemm(dnnl_stream, false, true, src1_ncols, row_diff, ne10, src1_ddf1_i, DnnlGemmWrapper::to_dt<float>(),
25962598
src0_ddf_i, DnnlGemmWrapper::to_dt<float>(), dst_dd_i, DnnlGemmWrapper::to_dt<float>());
25972599
#endif
25982600
}

ggml/src/ggml-sycl/common.hpp

Lines changed: 62 additions & 31 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
@@ -59,7 +63,7 @@ static int g_ggml_sycl_debug = 0;
5963
// define for XMX in Intel GPU
6064
// TODO: currently, it's not used for XMX really.
6165
#if !defined(GGML_SYCL_FORCE_MMQ)
62-
#define SYCL_USE_XMX
66+
#define SYCL_USE_XMX
6367
#endif
6468

6569
// max batch size to use MMQ kernels when tensor cores are available
@@ -80,16 +84,16 @@ static int g_ggml_sycl_debug = 0;
8084
typedef sycl::queue *queue_ptr;
8185

8286
enum ggml_sycl_backend_gpu_mode {
83-
SYCL_UNSET_GPU_MODE = -1,
84-
SYCL_SINGLE_GPU_MODE = 0,
85-
SYCL_MUL_GPU_MODE
87+
SYCL_UNSET_GPU_MODE = -1,
88+
SYCL_SINGLE_GPU_MODE = 0,
89+
SYCL_MUL_GPU_MODE
8690
};
8791

8892
static_assert(sizeof(sycl::half) == sizeof(ggml_fp16_t), "wrong fp16 size");
8993

9094
static void crash() {
91-
int* ptr = NULL;
92-
*ptr = 0;
95+
int* ptr = NULL;
96+
*ptr = 0;
9397
}
9498

9599
[[noreturn]] static void ggml_sycl_error(
@@ -98,9 +102,9 @@ static void crash() {
98102
const char* file,
99103
const int line,
100104
const char* msg) {
101-
fprintf(stderr, "SYCL error: %s: %s\n", stmt, msg);
102-
fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
103-
GGML_ABORT("SYCL error");
105+
fprintf(stderr, "SYCL error: %s: %s\n", stmt, msg);
106+
fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
107+
GGML_ABORT("SYCL error");
104108
}
105109

106110
#define SYCL_CHECK(err) \
@@ -137,40 +141,40 @@ static int g_all_sycl_device_count = -1;
137141
static bool g_ggml_backend_sycl_buffer_type_initialized = false;
138142

139143
static ggml_sycl_backend_gpu_mode g_ggml_sycl_backend_gpu_mode =
140-
SYCL_UNSET_GPU_MODE;
144+
SYCL_UNSET_GPU_MODE;
141145

142146
static void* g_scratch_buffer = nullptr;
143147
static size_t g_scratch_size = 0; // disabled by default
144148
static size_t g_scratch_offset = 0;
145149

146150
[[noreturn]] static inline void bad_arch(const sycl::stream& stream_ct1) {
147-
stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
148-
"current GPU architecture.\n";
149-
// __trap();
150-
std::exit(1);
151+
stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
152+
"current GPU architecture.\n";
153+
// __trap();
154+
std::exit(1);
151155

152-
(void)bad_arch; // suppress unused function warning
156+
(void)bad_arch; // suppress unused function warning
153157
}
154158

155159
int get_current_device_id();
156160

157161
inline dpct::err0 ggml_sycl_set_device(const int device) try {
158162

159-
int current_device_id;
160-
SYCL_CHECK(CHECK_TRY_ERROR(current_device_id = get_current_device_id()));
163+
int current_device_id;
164+
SYCL_CHECK(CHECK_TRY_ERROR(current_device_id = get_current_device_id()));
161165

162-
// GGML_SYCL_DEBUG("ggml_sycl_set_device device_id=%d,
163-
// current_device_id=%d\n", device, current_device);
164-
if (device == current_device_id) {
165-
return 0;
166-
}
166+
// GGML_SYCL_DEBUG("ggml_sycl_set_device device_id=%d,
167+
// current_device_id=%d\n", device, current_device);
168+
if (device == current_device_id) {
169+
return 0;
170+
}
167171

168-
return CHECK_TRY_ERROR(dpct::select_device(device));
172+
return CHECK_TRY_ERROR(dpct::select_device(device));
169173
} catch (sycl::exception const& exc) {
170-
std::cerr << exc.what() << "Exception caught at file:" << __FILE__
171-
<< ", line:" << __LINE__ << std::endl;
172-
crash();
173-
std::exit(1);
174+
std::cerr << exc.what() << "Exception caught at file:" << __FILE__
175+
<< ", line:" << __LINE__ << std::endl;
176+
crash();
177+
std::exit(1);
174178
}
175179

176180
//////////////////////
@@ -248,10 +252,10 @@ struct ggml_sycl_pool_alloc {
248252
// backend interface
249253

250254
struct ggml_tensor_extra_gpu {
251-
void* data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split
252-
// tensors
253-
dpct::event_ptr events[GGML_SYCL_MAX_DEVICES]
254-
[GGML_SYCL_MAX_STREAMS]; // events for synchronizing multiple GPUs
255+
void* data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split
256+
// tensors
257+
dpct::event_ptr events[GGML_SYCL_MAX_DEVICES]
258+
[GGML_SYCL_MAX_STREAMS]; // events for synchronizing multiple GPUs
255259
};
256260

257261
struct ggml_backend_sycl_context {
@@ -276,6 +280,33 @@ struct ggml_backend_sycl_context {
276280
return stream(device, 0);
277281
}
278282

283+
#if GGML_SYCL_DNNL
284+
dnnl::stream make_stream(sycl::queue& q) {
285+
// Get the device associated with the queue
286+
sycl::device dev = q.get_device();
287+
// Get the context associated with the queue
288+
sycl::context ctx = q.get_context();
289+
const dnnl::engine eng = dnnl::sycl_interop::make_engine(dev, ctx);
290+
dnnl::stream stream = dnnl::sycl_interop::make_stream(eng, q);
291+
return stream;
292+
}
293+
std::unordered_map<sycl::queue*, dnnl::stream> stream_map;
294+
dnnl::stream stream_dnnl(int device, int _stream) {
295+
auto q = stream(device, _stream);
296+
return stream_dnnl(q);
297+
}
298+
dnnl::stream stream_dnnl(sycl::queue* qptr) {
299+
auto it = stream_map.find(qptr);
300+
if (it == stream_map.end()) {
301+
stream_map[qptr] = make_stream(*qptr);
302+
}
303+
return it->second;
304+
}
305+
dnnl::stream stream_dnnl() {
306+
return stream_dnnl(device, 0);
307+
}
308+
#endif
309+
279310
// pool
280311
std::unique_ptr<ggml_sycl_pool> pools[GGML_SYCL_MAX_DEVICES];
281312

ggml/src/ggml-sycl/gemm.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
#include <iostream>
1818

1919
#include "ggml-sycl.h"
20-
#include "dnnl.hpp"
21-
#include "dnnl_sycl.hpp"
2220

2321
#if GGML_SYCL_DNNL
2422

23+
#include "dnnl.hpp"
24+
#include "dnnl_sycl.hpp"
25+
2526
class DnnlGemmWrapper {
2627
public:
2728
using dt = dnnl::memory::data_type;

0 commit comments

Comments
 (0)