Skip to content

Commit 0078ae4

Browse files
committed
seperate threaded read test, slower somehow
1 parent 158ab15 commit 0078ae4

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs.comp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#if defined(A_TYPE_PACKED16)
88
layout (binding = 0) readonly buffer A_PACKED16 {A_TYPE_PACKED16 data_a_packed16[];};
99
#endif
10+
#if defined(A_TYPE_PACKED16_FLAT)
11+
layout (binding = 0) readonly buffer A_PACKED16_FLAT {A_TYPE_PACKED16_FLAT data_a_packed16_flat[];};
12+
#endif
1013
#if defined(A_TYPE_PACKED32)
1114
layout (binding = 0) readonly buffer A_PACKED32 {A_TYPE_PACKED32 data_a_packed32[];};
1215
#endif

ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q6_k.comp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ layout (constant_id = 0) const uint BLOCK_SIZE = 32;
1010
layout (constant_id = 1) const uint NUM_ROWS = 1;
1111

1212
shared FLOAT_TYPE tmpsh[NUM_ROWS][BLOCK_SIZE];
13-
shared block_q6_K_packed16 blkcache[BLOCK_SIZE/16];
13+
shared block_q6_K_packed16_flat blkcache[BLOCK_SIZE/16];
14+
shared FLOAT_TYPE scales[NUM_ROWS/4][16];
1415

1516
void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
1617
uint a_offset, b_offset, d_offset;
1718
get_offsets(a_offset, b_offset, d_offset);
1819

1920
const uint num_blocks_per_row = p.ncols / QUANT_K;
2021

21-
// 16 threads are used to process each block
22+
// 16 thread groups are used to process each block
2223
const uint it_size = gl_WorkGroupSize.x/16;
2324
const uint tid = gl_LocalInvocationID.x;
2425
const uint itid = tid%16; // 0...15
@@ -34,14 +35,16 @@ void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
3435
const uint qh_offset = 32*v_im + l0;
3536
const uint s_offset = 8*v_im + is;
3637
const uint y_offset = 128*v_im + l0;
38+
const uint shift = (itid%2 == 1) ? 8 : 0;
3739

3840
FLOAT_TYPE temp[NUM_ROWS];
3941

4042
[[unroll]] for (uint i = 0; i < NUM_ROWS; ++i) {
4143
temp[i] = FLOAT_TYPE(0);
4244
}
4345

44-
[[unroll]] for (uint i = ix; i < num_blocks_per_row; i += it_size) {
46+
[[unroll]] for (uint i0 = 0; i0 < num_blocks_per_row; i0 += it_size) {
47+
uint i = i0 + ix; // 16 thread group specific counter
4548
const uint y_idx = i * QUANT_K + y_offset;
4649

4750
B_TYPE_VEC4 by0 = data_b_v4[(b_offset + y_idx) / 4];
@@ -55,24 +58,29 @@ void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
5558
ibi += num_blocks_per_row;
5659

5760
// cache full superblock into shared memory with coalesced reads
58-
[[unroll]] for (int l = 0; l < 4; ++l)
59-
blkcache[ix].ql[itid + 16*l] = data_a_packed16[ib0 + i].ql[itid + 16*l];
60-
[[unroll]] for (int l = 0; l < 2; ++l)
61-
blkcache[ix].qh[itid + 16*l] = data_a_packed16[ib0 + i].qh[itid + 16*l];
62-
blkcache[ix].scales[itid] = data_a_packed16[ib0 + i].scales[itid];
61+
// we assume 64 threads here!
62+
[[unroll]] for (int l = 0; (l < 4) && (i0 + l < num_blocks_per_row); ++l) {
63+
blkcache[l].blkd[tid] = data_a_packed16_flat[ib0 + i0 + l].blkd[tid];
64+
// we read beyond the struct size but it looks like vulkan doesn't care? o_O
65+
// it's faster than using a branch to reduce the number of threads though
66+
blkcache[l].blkd[64 + tid] = data_a_packed16_flat[ib0 + i0 + l].blkd[64 + tid];
67+
}
68+
scales[ix][itid] = FLOAT_TYPE(int8_t((blkcache[ix].blkd[96 + itid/2] >> shift) & 0xFF));
6369
barrier();
70+
if (i >= num_blocks_per_row)
71+
continue;
6472

65-
const FLOAT_TYPE d = FLOAT_TYPE(data_a[ib0 + i].d);
73+
const FLOAT_TYPE d = FLOAT_TYPE(blkcache[ix][ib0 + i][104]);
6674

67-
uint32_t ql0_u32 = uint32_t(blkcache[ix].ql[ql_offset / 2]) | (uint32_t(blkcache[ix].ql[ql_offset / 2 + 1]) << 16);
68-
uint32_t ql32_u32 = uint32_t(blkcache[ix].ql[ql_offset / 2 + 16]) | (uint32_t(blkcache[ix].ql[ql_offset / 2 + 17]) << 16);
75+
uint32_t ql0_u32 = uint32_t(blkcache[ix].blkd[ql_offset / 2]) | (uint32_t(blkcache[ix].blkd[ql_offset / 2 + 1]) << 16);
76+
uint32_t ql32_u32 = uint32_t(blkcache[ix].blkd[ql_offset / 2 + 16]) | (uint32_t(blkcache[ix].blkd[ql_offset / 2 + 17]) << 16);
6977

7078
uint32_t ql0_u32_lo4 = ql0_u32 & 0x0F0F0F0F;
7179
uint32_t ql0_u32_hi4 = (ql0_u32 >> 4) & 0x0F0F0F0F;
7280
uint32_t ql32_u32_lo4 = ql32_u32 & 0x0F0F0F0F;
7381
uint32_t ql32_u32_hi4 = (ql32_u32 >> 4) & 0x0F0F0F0F;
7482

75-
uint32_t qh_u32 = uint32_t(blkcache[ix].qh[qh_offset / 2]) | (uint32_t(blkcache[ix].qh[qh_offset / 2 + 1]) << 16);
83+
uint32_t qh_u32 = uint32_t(blkcache[ix].blkd[64 + qh_offset / 2]) | (uint32_t(blkcache[ix].blkd[64 + qh_offset / 2 + 1]) << 16);
7684
uint32_t qh0_u32 = (qh_u32 & 0x03030303) << 4;
7785
uint32_t qh2_u32 = (qh_u32 & 0x0C0C0C0C) << 2;
7886
uint32_t qh4_u32 = (qh_u32 & 0x30303030);
@@ -97,7 +105,7 @@ void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
97105
}
98106

99107
[[unroll]] for (int l = 0; l < 4; ++l)
100-
sum[l] *= FLOAT_TYPE(blkcache[ix].scales[s_offset + l*2]);
108+
sum[l] *= scales[ix][s_offset + l*2];
101109
temp[n] += (sum[0] + sum[1] + sum[2] + sum[3]) * d;
102110
}
103111
}

ggml/src/ggml-vulkan/vulkan-shaders/types.comp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,16 @@ struct block_q6_K_packed16
273273
float16_t d;
274274
};
275275

276+
struct block_q6_K_packed16_flat
277+
{
278+
uint16_t blkd[105];
279+
};
280+
276281
#if defined(DATA_A_Q6_K)
277282
#define QUANT_K QUANT_K_Q6_K
278283
#define A_TYPE block_q6_K
279284
#define A_TYPE_PACKED16 block_q6_K_packed16
285+
#define A_TYPE_PACKED16_FLAT block_q6_K_packed16_flat
280286
#endif
281287

282288
// IQuants

0 commit comments

Comments
 (0)