Skip to content

Commit d3fba0c

Browse files
authored
sycl : fix get_rows Q2_K, Q4_K, Q5_K (ggml-org#25656)
1 parent ae9291e commit d3fba0c

2 files changed

Lines changed: 195 additions & 19 deletions

File tree

ggml/src/ggml-sycl/dequantize.hpp

Lines changed: 115 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
typedef void (*dequantize_kernel_t)(const void * vx, const int64_t ib, const int iqs, dfloat2 & v);
2020
typedef void (*dequantize_kernel_t_reorder)(const void *d, const int64_t ib, const void *qs,
2121
const int iqs, dfloat2 &v);
22+
typedef void (*dequantize_kernel_f32_t)(const void * vx, const int64_t ib, const int iqs, float & v0, float & v1);
2223

2324
#if QK_K == 256
2425
static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m);
@@ -85,6 +86,21 @@ static __dpct_inline__ void dequantize_q1_0_reorder(const void *d_ptr, const int
8586
v.y() = (2 * bit_1 - 1) * d;
8687
}
8788

89+
static __dpct_inline__ void dequantize_q1_0(const void *vx, const int64_t ib,
90+
const int iqs, dfloat2 &v) {
91+
const block_q1_0 * x = (const block_q1_0 *) vx;
92+
const dfloat d = x[ib].d;
93+
94+
const int bit_index_0 = iqs + 0;
95+
const int bit_index_1 = iqs + 1;
96+
97+
const int bit_0 = (x[ib].qs[bit_index_0 / 8] >> (bit_index_0 % 8)) & 1;
98+
const int bit_1 = (x[ib].qs[bit_index_1 / 8] >> (bit_index_1 % 8)) & 1;
99+
100+
v.x() = (2 * bit_0 - 1) * d;
101+
v.y() = (2 * bit_1 - 1) * d;
102+
}
103+
88104
static __dpct_inline__ void dequantize_q4_1(const void *vx, const int64_t ib,
89105
const int iqs, dfloat2 &v) {
90106
const block_q4_1 * x = (const block_q4_1 *) vx;
@@ -140,6 +156,39 @@ static __dpct_inline__ void dequantize_q4_K(const void *vx, const int64_t ib,
140156
#endif
141157
}
142158

159+
static __dpct_inline__ void dequantize_q4_K_f32(const void *vx, const int64_t ib,
160+
const int iqs, float &v0, float &v1) {
161+
#if QK_K == 256
162+
const block_q4_K * x = (const block_q4_K *) vx;
163+
const sycl::half2 dm = x[ib].dm;
164+
const float dall = dm[0];
165+
const float dmin = dm[1];
166+
167+
auto dequantize_one = [&](const int idx) -> float {
168+
const int il = idx / 64;
169+
const int in = idx % 64;
170+
const int is = 2 * il + (in >= 32 ? 1 : 0);
171+
const int qsi = 32 * il + (in & 31);
172+
173+
uint8_t sc;
174+
uint8_t m;
175+
get_scale_min_k4(is, x[ib].scales, sc, m);
176+
177+
const float d = dall * sc;
178+
const float mn = dmin * m;
179+
const uint8_t q = x[ib].qs[qsi];
180+
const uint8_t qv = (in >= 32) ? (q >> 4) : (q & 0xF);
181+
182+
return d * qv - mn;
183+
};
184+
185+
v0 = dequantize_one(iqs + 0);
186+
v1 = dequantize_one(iqs + 1);
187+
#else
188+
GGML_ABORT("Q4_K dequantize not supported for QK_K != 256");
189+
#endif
190+
}
191+
143192
static __dpct_inline__ void dequantize_q2_K(const void *vx, const int64_t ib,
144193
const int iqs, dfloat2 &v) {
145194
#if QK_K == 256
@@ -159,7 +208,7 @@ static __dpct_inline__ void dequantize_q2_K(const void *vx, const int64_t ib,
159208
const float d = dall * (sc & 0xF);
160209
const float m = dmin * (sc >> 4);
161210

162-
return sycl::fma((dfloat) ((q >> (2 * g)) & 3), (dfloat) d, (dfloat) (-m));
211+
return (dfloat) d * (dfloat) ((q >> (2 * g)) & 3) - (dfloat) m;
163212
};
164213

165214
v.x() = dequantize_one(iqs + 0);
@@ -169,6 +218,35 @@ static __dpct_inline__ void dequantize_q2_K(const void *vx, const int64_t ib,
169218
#endif
170219
}
171220

221+
static __dpct_inline__ void dequantize_q2_K_f32(const void *vx, const int64_t ib,
222+
const int iqs, float &v0, float &v1) {
223+
#if QK_K == 256
224+
const block_q2_K * x = (const block_q2_K *) vx;
225+
const float dall = x[ib].dm[0];
226+
const float dmin = x[ib].dm[1];
227+
228+
auto dequantize_one = [&](const int idx) -> float {
229+
const int n = idx / 128;
230+
const int r = idx % 128;
231+
const int g = r / 32;
232+
const int l = r % 32;
233+
const int is = 8 * n + l / 16;
234+
235+
const uint8_t q = x[ib].qs[32 * n + l];
236+
const uint8_t sc = x[ib].scales[is + 2 * g];
237+
const float d = dall * (sc & 0xF);
238+
const float m = dmin * (sc >> 4);
239+
240+
return d * ((q >> (2 * g)) & 3) - m;
241+
};
242+
243+
v0 = dequantize_one(iqs + 0);
244+
v1 = dequantize_one(iqs + 1);
245+
#else
246+
GGML_ABORT("Q2_K dequantize not supported for QK_K != 256");
247+
#endif
248+
}
249+
172250
static __dpct_inline__ void dequantize_q3_K(const void *vx, const int64_t ib,
173251
const int iqs, dfloat2 &v) {
174252
#if QK_K == 256
@@ -242,6 +320,42 @@ static __dpct_inline__ void dequantize_q5_K(const void *vx, const int64_t ib,
242320
#endif
243321
}
244322

323+
static __dpct_inline__ void dequantize_q5_K_f32(const void *vx, const int64_t ib,
324+
const int iqs, float &v0, float &v1) {
325+
#if QK_K == 256
326+
const block_q5_K * x = (const block_q5_K *) vx;
327+
const float dall = x[ib].dm[0];
328+
const float dmin = x[ib].dm[1];
329+
330+
auto dequantize_one = [&](const int idx) -> float {
331+
const int il = idx / 64;
332+
const int in = idx % 64;
333+
const int is = 2 * il + (in >= 32 ? 1 : 0);
334+
const int ir = (in & 31) / 2;
335+
const int iq = in & 1;
336+
337+
const uint8_t q = x[ib].qs[32 * il + 2 * ir + iq];
338+
const uint8_t h = x[ib].qh[2 * ir + iq];
339+
const uint8_t qv = (in >= 32) ? (q >> 4) : (q & 0xF);
340+
341+
uint8_t sc;
342+
uint8_t m;
343+
get_scale_min_k4(is, x[ib].scales, sc, m);
344+
345+
const float d = dall * sc;
346+
const float mn = dmin * m;
347+
const uint8_t hm = 1 << (2 * il + (in >= 32 ? 1 : 0));
348+
349+
return (qv + ((h & hm) ? 16 : 0)) * d - mn;
350+
};
351+
352+
v0 = dequantize_one(iqs + 0);
353+
v1 = dequantize_one(iqs + 1);
354+
#else
355+
GGML_ABORT("Q5_K dequantize not supported for QK_K != 256");
356+
#endif
357+
}
358+
245359
static __dpct_inline__ void dequantize_q6_K(const void *vx, const int64_t ib,
246360
const int iqs, dfloat2 &v) {
247361
#if QK_K == 256
@@ -296,21 +410,6 @@ static __dpct_inline__ void dequantize_mxfp4(const void *vx, const int64_t ib,
296410
v.y() = d * kvalues_mxfp4[q >> 4] * 0.5f;
297411
}
298412

299-
static __dpct_inline__ void dequantize_q1_0(const void *vx, const int64_t ib,
300-
const int iqs, dfloat2 &v) {
301-
const block_q1_0 * x = (const block_q1_0 *) vx;
302-
const dfloat d = x[ib].d;
303-
304-
const int bit_index_0 = iqs + 0;
305-
const int bit_index_1 = iqs + 1;
306-
307-
const int bit_0 = (x[ib].qs[bit_index_0 / 8] >> (bit_index_0 % 8)) & 1;
308-
const int bit_1 = (x[ib].qs[bit_index_1 / 8] >> (bit_index_1 % 8)) & 1;
309-
310-
v.x() = (2 * bit_0 - 1) * d;
311-
v.y() = (2 * bit_1 - 1) * d;
312-
}
313-
314413
static __dpct_inline__ void dequantize_nvfp4(const void *vx, const int64_t ib,
315414
const int iqs, dfloat2 &v) {
316415
const block_nvfp4 & xb = ((const block_nvfp4 *) vx)[ib];

ggml/src/ggml-sycl/getrows.cpp

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,50 @@ static void k_get_rows(
6060
dst_row[iybs + iqs + y_offset] = v.y();
6161
}
6262

63+
template<int qk, int qr, dequantize_kernel_f32_t dequantize_kernel, typename dst_t>
64+
static void k_get_rows_f32(
65+
const void * src0, const int32_t * src1, dst_t * dst,
66+
int64_t ne00,
67+
int64_t ne12,
68+
size_t s1, size_t s2, size_t s3,
69+
size_t nb01, size_t nb02, size_t nb03,
70+
size_t s10, size_t s11, size_t s12,
71+
const sycl::nd_item<3> &item_ct1) {
72+
73+
const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
74+
item_ct1.get_local_id(2)) *
75+
2;
76+
const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
77+
item_ct1.get_local_id(1);
78+
const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
79+
item_ct1.get_local_id(0)) /
80+
ne12;
81+
const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
82+
item_ct1.get_local_id(0)) %
83+
ne12;
84+
85+
if (i00 >= ne00) {
86+
return;
87+
}
88+
89+
const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
90+
91+
dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
92+
const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
93+
94+
const int ib = i00/qk;
95+
const int iqs = (i00%qk)/qr;
96+
const int iybs = i00 - i00%qk;
97+
const int y_offset = qr == 1 ? 1 : qk/2;
98+
99+
float v0;
100+
float v1;
101+
dequantize_kernel(src0_row, ib, iqs, v0, v1);
102+
103+
dst_row[iybs + iqs + 0] = (dst_t) v0;
104+
dst_row[iybs + iqs + y_offset] = (dst_t) v1;
105+
}
106+
63107
template<typename src0_t, typename dst_t>
64108
static void k_get_rows_float(
65109
const src0_t * src0, const int32_t * src1, dst_t * dst,
@@ -129,6 +173,39 @@ static void get_rows_sycl(ggml_backend_sycl_context & ctx, const ggml_tensor *sr
129173
GGML_UNUSED(ctx);
130174
}
131175

176+
template <int qk, int qr, dequantize_kernel_f32_t dq>
177+
static void get_rows_sycl_f32(ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1,
178+
ggml_tensor *dst, const void *src0_dd,
179+
const int32_t *src1_dd, float *dst_dd,
180+
queue_ptr stream) {
181+
182+
GGML_TENSOR_BINARY_OP_LOCALS
183+
184+
const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
185+
const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
186+
const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
187+
188+
const size_t s1 = nb1 / ggml_element_size(dst);
189+
const size_t s2 = nb2 / ggml_element_size(dst);
190+
const size_t s3 = nb3 / ggml_element_size(dst);
191+
192+
const size_t s10 = nb10 / ggml_element_size(src1);
193+
const size_t s11 = nb11 / ggml_element_size(src1);
194+
const size_t s12 = nb12 / ggml_element_size(src1);
195+
196+
GGML_ASSERT(ne00 % 2 == 0);
197+
198+
stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
199+
[=](sycl::nd_item<3> item_ct1) {
200+
k_get_rows_f32<qk, qr, dq>(
201+
src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
202+
s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
203+
});
204+
205+
GGML_UNUSED(dst);
206+
GGML_UNUSED(ctx);
207+
}
208+
132209
template <typename src0_t, typename dst_t>
133210
static void get_rows_sycl_float(ggml_backend_sycl_context & ctx, const ggml_tensor *src0,
134211
const ggml_tensor *src1, ggml_tensor *dst,
@@ -244,7 +321,7 @@ void ggml_sycl_op_get_rows(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
244321
src1_i32, (float *)dst->data, ctx.stream());
245322
break;
246323
case GGML_TYPE_Q2_K:
247-
get_rows_sycl<QK_K, 1, dequantize_q2_K>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
324+
get_rows_sycl_f32<QK_K, 1, dequantize_q2_K_f32>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
248325
src1_i32, (float *)dst->data, ctx.stream());
249326
break;
250327
case GGML_TYPE_Q3_K:
@@ -260,7 +337,7 @@ void ggml_sycl_op_get_rows(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
260337
src1_i32, (float *)dst->data, ctx.stream());
261338
break;
262339
case GGML_TYPE_Q4_K:
263-
get_rows_sycl<QK_K, 1, dequantize_q4_K>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
340+
get_rows_sycl_f32<QK_K, 1, dequantize_q4_K_f32>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
264341
src1_i32, (float *)dst->data, ctx.stream());
265342
break;
266343
case GGML_TYPE_Q5_0:
@@ -272,7 +349,7 @@ void ggml_sycl_op_get_rows(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
272349
src1_i32, (float *)dst->data, ctx.stream());
273350
break;
274351
case GGML_TYPE_Q5_K:
275-
get_rows_sycl<QK_K, 1, dequantize_q5_K>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
352+
get_rows_sycl_f32<QK_K, 1, dequantize_q5_K_f32>(ctx, dst->src[0], dst->src[1], dst, (const float *)dst->src[0]->data,
276353
src1_i32, (float *)dst->data, ctx.stream());
277354
break;
278355
case GGML_TYPE_Q6_K:

0 commit comments

Comments
 (0)