@@ -12,7 +12,6 @@ layout (constant_id = 1) const uint NUM_ROWS = 1;
1212shared FLOAT_TYPE tmpsh[NUM_ROWS][BLOCK_SIZE];
1313shared FLOAT_TYPE sccache[BLOCK_SIZE/16][16];
1414shared block_q6_K_packed16 blkcache[BLOCK_SIZE/16];
15- shared B_TYPE bycache[BLOCK_SIZE/16][QUANT_K];
1615
1716void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
1817 uint a_offset, b_offset, d_offset;
@@ -35,6 +34,7 @@ void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
3534 const uint ql_offset = 64*v_im + l0;
3635 const uint qh_offset = 32*v_im + l0;
3736 const uint s_offset = 8*v_im + is;
37+ const uint y_offset = 128*v_im + l0;
3838 const uint bcs_offset = (itid%2 == 1) ? 8 : 0;
3939
4040 FLOAT_TYPE temp[NUM_ROWS];
@@ -45,16 +45,12 @@ void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
4545
4646 [[unroll]] for (uint i0 = 0; i0 < num_blocks_per_row; i0 += it_size) {
4747 uint i = i0 + ix; // 16 thread group specific counter
48- const uint y_idx = i0 * QUANT_K;
49- const int blim = min(int(num_blocks_per_row) - int(i0), 4);
48+ const uint y_idx = i * QUANT_K + y_offset;
5049
51- // assume 64 threads
52- [[unroll]] for (int n = 0; n < blim; ++n) {
53- [[unroll]] for (int l = 0; l < 4; ++l) {
54- bycache[n][tid + 64*l] = data_b[b_offset + y_idx + QUANT_K*n + tid + 64*l];
55- }
56- }
57- barrier();
50+ B_TYPE_VEC4 by0 = data_b_v4[(b_offset + y_idx) / 4];
51+ B_TYPE_VEC4 by32 = data_b_v4[(b_offset + y_idx) / 4 + 8];
52+ B_TYPE_VEC4 by64 = data_b_v4[(b_offset + y_idx) / 4 + 16];
53+ B_TYPE_VEC4 by96 = data_b_v4[(b_offset + y_idx) / 4 + 24];
5854
5955 uint ibi = first_row*num_blocks_per_row;
6056 [[unroll]] for (uint n = 0; n < num_rows; ++n) {
@@ -63,6 +59,7 @@ void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
6359
6460 // cache full superblock into shared memory with coalesced reads
6561 // we assume 64 threads here!
62+ const int blim = min(int(num_blocks_per_row) - int(i0), 4);
6663 // this is required as this loop is super sensitive to unrolling with hardcoded 4
6764 if (blim == 4) {
6865 if (tid < 52) {
@@ -86,14 +83,41 @@ void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
8683
8784 const FLOAT_TYPE d = FLOAT_TYPE(data_a[ib0 + i].d);
8885
89- FLOAT_TYPE dq[16];
90- FLOAT_TYPE sum = 0;
91- [[unroll]] for (uint l = 0; l < 16; ++l) {
92- dq[l] = bitfieldExtract(blkcache[ix].blk[l/2], 1, 4) | (bitfieldExtract(blkcache[ix].blk[64 + l], 2, 2) << 4);
93- sum = fma(FLOAT_TYPE(bycache[ix][16*itid + l]), dq[l], sum);
94- }
86+ uint32_t ql0_u32 = uint32_t(blkcache[ix].blk[ql_offset / 2]) | (uint32_t(blkcache[ix].blk[ql_offset / 2 + 1]) << 16);
87+ uint32_t ql32_u32 = uint32_t(blkcache[ix].blk[ql_offset / 2 + 16]) | (uint32_t(blkcache[ix].blk[ql_offset / 2 + 17]) << 16);
88+
89+ uint32_t ql0_u32_lo4 = ql0_u32 & 0x0F0F0F0F;
90+ uint32_t ql0_u32_hi4 = (ql0_u32 >> 4) & 0x0F0F0F0F;
91+ uint32_t ql32_u32_lo4 = ql32_u32 & 0x0F0F0F0F;
92+ uint32_t ql32_u32_hi4 = (ql32_u32 >> 4) & 0x0F0F0F0F;
93+
94+ uint32_t qh_u32 = uint32_t(blkcache[ix].blk[64 + qh_offset / 2]) | (uint32_t(blkcache[ix].blk[64 + qh_offset / 2 + 1]) << 16);
95+ uint32_t qh0_u32 = (qh_u32 & 0x03030303) << 4;
96+ uint32_t qh2_u32 = (qh_u32 & 0x0C0C0C0C) << 2;
97+ uint32_t qh4_u32 = (qh_u32 & 0x30303030);
98+ uint32_t qh6_u32 = (qh_u32 & 0xC0C0C0C0) >> 2;
99+
100+ uint32_t q0_u32 = ql0_u32_lo4 | qh0_u32;
101+ uint32_t q1_u32 = ql32_u32_lo4 | qh2_u32;
102+ uint32_t q2_u32 = ql0_u32_hi4 | qh4_u32;
103+ uint32_t q3_u32 = ql32_u32_hi4 | qh6_u32;
104+
105+ uvec4 q0 = uvec4(unpack8(q0_u32));
106+ uvec4 q1 = uvec4(unpack8(q1_u32));
107+ uvec4 q2 = uvec4(unpack8(q2_u32));
108+ uvec4 q3 = uvec4(unpack8(q3_u32));
109+
110+ FLOAT_TYPE sum[4] = {0, 0, 0, 0};
111+ [[unroll]] for (uint l = 0; l < 4; ++l) {
112+ sum[0] = fma(FLOAT_TYPE(by0[l]), FLOAT_TYPE(int8_t(q0[l]) - 32), sum[0]);
113+ sum[1] = fma(FLOAT_TYPE(by32[l]), FLOAT_TYPE(int8_t(q1[l]) - 32), sum[1]);
114+ sum[2] = fma(FLOAT_TYPE(by64[l]), FLOAT_TYPE(int8_t(q2[l]) - 32), sum[2]);
115+ sum[3] = fma(FLOAT_TYPE(by96[l]), FLOAT_TYPE(int8_t(q3[l]) - 32), sum[3]);
116+ }
95117
96- temp[n] += sum * sccache[ix][itid] * d;
118+ [[unroll]] for (uint l = 0; l < 4; ++l)
119+ sum[l] *= sccache[ix][s_offset + l*2];
120+ temp[n] += (sum[0] + sum[1] + sum[2] + sum[3]) * d;
97121 }
98122 }
99123
0 commit comments