Skip to content

Commit c5f6c97

Browse files
committed
sycl: add native PQ2_0 MMVQ vector dot kernel with DP4A
1 parent 016be85 commit c5f6c97

3 files changed

Lines changed: 100 additions & 1 deletion

File tree

‎ggml/src/ggml-sycl/ggml-sycl.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4488,7 +4488,6 @@ static bool can_use_dequantize_mul_mat_vec(const ggml_tensor * src0, const ggml_
44884488

44894489
static bool can_use_mul_mat_vec_q(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
44904490
return ggml_is_quantized(src0->type) &&
4491-
src0->type != GGML_TYPE_PQ2_0 &&
44924491
src1->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32 &&
44934492
src1->ne[1] <= MMVQ_MAX_BATCH_SIZE;
44944493
}

‎ggml/src/ggml-sycl/mmvq.cpp‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,66 @@ static void mul_mat_vec_ptq1_0_q8_1_sycl_switch_ncols(
13381338
}
13391339
}
13401340

1341+
static void mul_mat_vec_pq2_0_q8_1_sycl(const void * vx, const void * vy,
1342+
float * dst, const int ncols,
1343+
const int nrows,
1344+
dpct::queue_ptr stream) {
1345+
GGML_ASSERT(ncols % QK_PQ2_0 == 0);
1346+
const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1347+
const sycl::range<3> block_nums(1, 1, block_num_y);
1348+
const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1349+
1350+
stream->submit([&](sycl::handler & cgh) {
1351+
cgh.parallel_for(
1352+
sycl::nd_range<3>(block_nums * block_dims, block_dims),
1353+
[=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1354+
mul_mat_vec_q<QK_PQ2_0, QI_PQ2_0, block_pq2_0,
1355+
VDR_PQ2_0_Q8_1_MMVQ, vec_dot_pq2_0_q8_1>(
1356+
vx, vy, dst, ncols, nrows, item_ct1);
1357+
});
1358+
});
1359+
}
1360+
1361+
template <int ncols_dst>
1362+
static void mul_mat_vec_pq2_0_q8_1_sycl_ncols(
1363+
const void * vx, const void * vy, float * dst,
1364+
const int ncols, const int nrows,
1365+
const int stride_col_y, const int stride_col_dst,
1366+
dpct::queue_ptr stream) {
1367+
GGML_ASSERT(ncols % QK_PQ2_0 == 0);
1368+
const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
1369+
const sycl::range<3> block_nums(1, 1, block_num_y);
1370+
const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
1371+
1372+
stream->submit([&](sycl::handler & cgh) {
1373+
cgh.parallel_for(
1374+
sycl::nd_range<3>(block_nums * block_dims, block_dims),
1375+
[=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(WARP_SIZE)]] {
1376+
mul_mat_vec_q_ncols<QK_PQ2_0, QI_PQ2_0, block_pq2_0,
1377+
VDR_PQ2_0_Q8_1_MMVQ, vec_dot_pq2_0_q8_1, ncols_dst>(
1378+
vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, item_ct1);
1379+
});
1380+
});
1381+
}
1382+
1383+
static void mul_mat_vec_pq2_0_q8_1_sycl_switch_ncols(
1384+
const void * vx, const void * vy, float * dst,
1385+
const int ncols, const int nrows, const int ncols_dst,
1386+
const int stride_col_y, const int stride_col_dst,
1387+
dpct::queue_ptr stream) {
1388+
switch (ncols_dst) {
1389+
case 1: mul_mat_vec_pq2_0_q8_1_sycl(vx, vy, dst, ncols, nrows, stream); break;
1390+
case 2: mul_mat_vec_pq2_0_q8_1_sycl_ncols<2>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
1391+
case 3: mul_mat_vec_pq2_0_q8_1_sycl_ncols<3>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
1392+
case 4: mul_mat_vec_pq2_0_q8_1_sycl_ncols<4>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
1393+
case 5: mul_mat_vec_pq2_0_q8_1_sycl_ncols<5>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
1394+
case 6: mul_mat_vec_pq2_0_q8_1_sycl_ncols<6>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
1395+
case 7: mul_mat_vec_pq2_0_q8_1_sycl_ncols<7>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
1396+
case 8: mul_mat_vec_pq2_0_q8_1_sycl_ncols<8>(vx, vy, dst, ncols, nrows, stride_col_y, stride_col_dst, stream); break;
1397+
default: GGML_ABORT("unsupported ncols_dst=%d for PQ2_0 multi-col MMVQ", ncols_dst);
1398+
}
1399+
}
1400+
13411401
static void mul_mat_vec_q2_0_q8_1_sycl(const void * vx, const void * vy,
13421402
float * dst, const int ncols,
13431403
const int nrows,
@@ -2415,6 +2475,20 @@ void ggml_sycl_op_mul_mat_vec_q(ggml_backend_sycl_context & ctx, const ggml_tens
24152475
mul_mat_vec_ptq1_0_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
24162476
}
24172477
break;
2478+
case GGML_TYPE_PQ2_0:
2479+
if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
2480+
const int stride_col_y = src1_padded_col_size / QK8_1;
2481+
const int stride_col_dst = dst->ne[0];
2482+
GGML_SYCL_DEBUG("Calling mul_mat_vec_pq2_0_q8_1_sycl_switch_ncols ncols=%d\n", (int)src1_ncols);
2483+
mul_mat_vec_pq2_0_q8_1_sycl_switch_ncols(
2484+
src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff,
2485+
src1_ncols, stride_col_y, stride_col_dst, stream);
2486+
return;
2487+
} else if (i == 0 || src1_ncols == 1) {
2488+
GGML_SYCL_DEBUG("Calling mul_mat_vec_pq2_0_q8_1_sycl\n");
2489+
mul_mat_vec_pq2_0_q8_1_sycl(src0_dd_i, src1_ddq_i_bs, dst_dd_i_bs, ne00, row_diff, stream);
2490+
}
2491+
break;
24182492
case GGML_TYPE_Q2_0:
24192493
if (i == 0 && src1_ncols > 1 && src1_ncols <= 8) {
24202494
const int stride_col_y = src1_padded_col_size / QK8_1;

‎ggml/src/ggml-sycl/vecdotq.hpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,32 @@ vec_dot_ptq1_0_q8_1(const void *__restrict__ vbq,
427427
return (float) bq->d * acc;
428428
}
429429

430+
#define VDR_PQ2_0_Q8_1_MMVQ 1
431+
432+
static __dpct_inline__ float
433+
vec_dot_pq2_0_q8_1(const void *__restrict__ vbq,
434+
const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
435+
const block_pq2_0 * bq2_0 = (const block_pq2_0 *) vbq;
436+
const block_q8_1 * bq8_1_chunk = bq8_1 + iqs;
437+
const float d2 = bq2_0->d;
438+
const uint8_t * qs_chunk = bq2_0->qs + iqs * 8;
439+
440+
int sumi = 0;
441+
#pragma unroll
442+
for (int j = 0; j < 8; ++j) {
443+
const uint8_t q = qs_chunk[j];
444+
const uint32_t vi = ((q >> 0) & 0x3) |
445+
(((q >> 2) & 0x3) << 8) |
446+
(((q >> 4) & 0x3) << 16) |
447+
(((q >> 6) & 0x3) << 24);
448+
const int w = byte_sub_4(vi, 0x01010101);
449+
const int u = get_int_from_int8_aligned(bq8_1_chunk->qs, j);
450+
sumi = dpct::dp4a(w, u, sumi);
451+
}
452+
453+
return d2 * ((const float) bq8_1_chunk->ds[0]) * (float) sumi;
454+
}
455+
430456
// VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
431457
// MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
432458

0 commit comments

Comments
 (0)