Skip to content

Commit f09b7cb

Browse files
rm get_work_group_size() by local cache for performance (ggml-org#8286)
Co-authored-by: arthw <[email protected]>
1 parent a38b884 commit f09b7cb

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

Diff for: ggml/src/ggml-sycl.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool ggml_backend_is_sycl(ggml_backend_t backend);
4949
int ggml_backend_sycl_get_device(ggml_backend_t backend);
5050
static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer);
5151
static inline int get_sycl_env(const char *env_name, int default_val);
52-
static inline int get_work_group_size(const sycl::device& device);
52+
5353

5454
void dev2dev_memcpy(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst,
5555
const void *ptr_src, size_t size) {
@@ -1912,9 +1912,9 @@ static void soft_max_f32_submitter(const float * x, const float * mask, float *
19121912
static void soft_max_f32_sycl(const float * x, const float * mask,
19131913
float * dst, const int ncols_x, const int nrows_x,
19141914
const int nrows_y, const float scale, const float max_bias,
1915-
queue_ptr stream) {
1915+
queue_ptr stream, int device) {
19161916
int nth = WARP_SIZE;
1917-
int max_block_size = get_work_group_size(stream->get_device());
1917+
int max_block_size = ggml_sycl_info().max_work_group_sizes[device];
19181918
while (nth < ncols_x && nth < max_block_size) nth *= 2;
19191919
if (nth>max_block_size) nth = max_block_size;
19201920

@@ -2156,6 +2156,8 @@ static ggml_sycl_device_info ggml_sycl_init() {
21562156

21572157
info.devices[i].cc =
21582158
100 * prop.get_major_version() + 10 * prop.get_minor_version();
2159+
2160+
info.max_work_group_sizes[i] = prop.get_max_work_group_size();
21592161
}
21602162

21612163
for (int id = 0; id < info.device_count; ++id) {
@@ -3031,7 +3033,7 @@ inline void ggml_sycl_op_soft_max(ggml_backend_sycl_context & ctx, const ggml_te
30313033
memcpy(&max_bias, dst->op_params + 1, sizeof(float));
30323034

30333035
soft_max_f32_sycl(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00,
3034-
nrows_x, nrows_y, scale, max_bias, main_stream);
3036+
nrows_x, nrows_y, scale, max_bias, main_stream, ctx.device);
30353037
}
30363038

30373039
inline void ggml_sycl_op_scale(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,

Diff for: ggml/src/ggml-sycl/common.hpp

+2-13
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ static int g_ggml_sycl_debug = 0;
4747
} \
4848
}()
4949

50-
// #define DEBUG_SYCL_MALLOC
51-
52-
static int g_work_group_size = 0;
53-
// typedef sycl::half ggml_fp16_t;
5450

5551
#define __SYCL_ARCH__ DPCT_COMPATIBILITY_TEMP
5652
#define VER_4VEC 610 // todo for hardward optimize.
@@ -193,6 +189,8 @@ struct ggml_sycl_device_info {
193189
sycl_device_info devices[GGML_SYCL_MAX_DEVICES] = {};
194190

195191
std::array<float, GGML_SYCL_MAX_DEVICES> default_tensor_split = {};
192+
193+
int max_work_group_sizes[GGML_SYCL_MAX_DEVICES] = {0};
196194
};
197195

198196
const ggml_sycl_device_info & ggml_sycl_info();
@@ -295,15 +293,6 @@ struct ggml_backend_sycl_context {
295293
}
296294
};
297295

298-
// common host functions
299-
300-
static inline int get_work_group_size(const sycl::device& device) {
301-
dpct::device_info prop;
302-
dpct::get_device_info(prop, device);
303-
return prop.get_max_work_group_size();
304-
}
305-
306-
307296
// common device functions
308297

309298
static __dpct_inline__ float warp_reduce_sum(float x,

Diff for: ggml/src/ggml-sycl/norm.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static void rms_norm_f32(const float* x, float* dst, const int ncols, const floa
181181

182182
static void norm_f32_sycl(const float* x, float* dst, const int ncols,
183183
const int nrows, const float eps,
184-
queue_ptr stream) {
184+
queue_ptr stream, int device) {
185185
GGML_ASSERT(ncols % WARP_SIZE == 0);
186186
if (ncols < 1024) {
187187
const sycl::range<3> block_dims(1, 1, WARP_SIZE);
@@ -197,7 +197,7 @@ static void norm_f32_sycl(const float* x, float* dst, const int ncols,
197197
});
198198
}
199199
else {
200-
const int work_group_size = get_work_group_size(stream->get_device());
200+
const int work_group_size = ggml_sycl_info().max_work_group_sizes[device];
201201
const sycl::range<3> block_dims(1, 1, work_group_size);
202202
/*
203203
DPCT1049:17: The work-group size passed to the SYCL kernel may exceed
@@ -222,7 +222,7 @@ static void norm_f32_sycl(const float* x, float* dst, const int ncols,
222222

223223
static void group_norm_f32_sycl(const float* x, float* dst,
224224
const int num_groups, const int group_size,
225-
const int ne_elements, queue_ptr stream) {
225+
const int ne_elements, queue_ptr stream, int device) {
226226
static const float eps = 1e-6f;
227227
if (group_size < 1024) {
228228
const sycl::range<3> block_dims(1, 1, WARP_SIZE);
@@ -240,7 +240,7 @@ static void group_norm_f32_sycl(const float* x, float* dst,
240240
});
241241
}
242242
else {
243-
const int work_group_size = get_work_group_size(stream->get_device());
243+
const int work_group_size = ggml_sycl_info().max_work_group_sizes[device];
244244
const sycl::range<3> block_dims(1, 1, work_group_size);
245245
/*
246246
DPCT1049:18: The work-group size passed to the SYCL kernel may exceed
@@ -269,7 +269,7 @@ static void group_norm_f32_sycl(const float* x, float* dst,
269269

270270
static void rms_norm_f32_sycl(const float* x, float* dst, const int ncols,
271271
const int nrows, const float eps,
272-
queue_ptr stream) {
272+
queue_ptr stream, int device) {
273273
GGML_ASSERT(ncols % WARP_SIZE == 0);
274274
// printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE);
275275
if (ncols < 1024) {
@@ -286,7 +286,7 @@ static void rms_norm_f32_sycl(const float* x, float* dst, const int ncols,
286286
});
287287
}
288288
else {
289-
const int work_group_size = get_work_group_size(stream->get_device());
289+
const int work_group_size = ggml_sycl_info().max_work_group_sizes[device];
290290
const sycl::range<3> block_dims(1, 1, work_group_size);
291291
/*
292292
DPCT1049:19: The work-group size passed to the SYCL kernel may exceed
@@ -322,7 +322,7 @@ void ggml_sycl_op_norm(ggml_backend_sycl_context& ctx, const ggml_tensor* src0,
322322
float eps;
323323
memcpy(&eps, dst->op_params, sizeof(float));
324324

325-
norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
325+
norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream, ctx.device);
326326

327327
(void)src1;
328328
(void)dst;
@@ -340,7 +340,7 @@ void ggml_sycl_op_group_norm(ggml_backend_sycl_context& ctx, const ggml_tensor*
340340

341341
int num_groups = dst->op_params[0];
342342
int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
343-
group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
343+
group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream, ctx.device);
344344

345345
(void)src1;
346346
(void)dst;
@@ -362,7 +362,7 @@ void ggml_sycl_op_rms_norm(ggml_backend_sycl_context& ctx, const ggml_tensor* sr
362362
float eps;
363363
memcpy(&eps, dst->op_params, sizeof(float));
364364

365-
rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
365+
rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream, ctx.device);
366366

367367
(void)src1;
368368
(void)dst;

0 commit comments

Comments
 (0)