@@ -10,15 +10,16 @@ layout (constant_id = 0) const uint BLOCK_SIZE = 32;
1010layout (constant_id = 1) const uint NUM_ROWS = 1;
1111
1212shared 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
1516void 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 }
0 commit comments