|
| 1 | +#version 450 |
| 2 | +#extension GL_EXT_shader_explicit_arithmetic_types_int32 : require |
| 3 | + |
| 4 | +#include "mul_mat_vec_base.comp" |
| 5 | + |
| 6 | +layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; |
| 7 | + |
| 8 | +FLOAT_TYPE temp[NUM_COLS][NUM_ROWS]; |
| 9 | + |
| 10 | +void calc_superblock(const uint a_offset, const uint b_offset, const uint itid, const uint i, const uint num_blocks_per_row, const uint first_row, const uint num_rows) { |
| 11 | + const uint y_idx = i * QUANT_K + 16 * itid; |
| 12 | + const uint ib32 = itid / 2; // 0..7 |
| 13 | + |
| 14 | + uint ibi = a_offset / QUANT_K + first_row * num_blocks_per_row + i; |
| 15 | + [[unroll]] for (uint n = 0; n < num_rows; ++n) { |
| 16 | + const float d = float(data_a[ibi].d); |
| 17 | + const uint signscale = pack32(u16vec2( |
| 18 | + data_a_packed16[ibi].qs[QUANT_K / 8 + 2 * ib32], |
| 19 | + data_a_packed16[ibi].qs[QUANT_K / 8 + 2 * ib32 + 1])); |
| 20 | + const float db = d * 0.5 * (0.5 + (signscale >> 28)); |
| 21 | + [[unroll]] for (uint l = 0; l < 2; ++l) { |
| 22 | + const uint qs0 = data_a[ibi].qs[8 * ib32 + 4 * (itid & 1) + 2 * l]; |
| 23 | + const uint qs1 = data_a[ibi].qs[8 * ib32 + 4 * (itid & 1) + 2 * l + 1]; |
| 24 | + const uint sign = bitfieldExtract(signscale, 7 * int(2 * (itid & 1) + l), 7); |
| 25 | + const uint sign7 = bitCount(sign); |
| 26 | + const vec4 grid0 = vec4(unpack8(iq3xxs_grid[qs0])); |
| 27 | + const vec4 grid1 = vec4(unpack8(iq3xxs_grid[qs1])); |
| 28 | + |
| 29 | + [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { |
| 30 | + const vec4 b0 = vec4(data_b_v4[(j*p.batch_stride_b + b_offset + y_idx) / 4 + 2*l + 0]); |
| 31 | + const vec4 b4 = vec4(data_b_v4[(j*p.batch_stride_b + b_offset + y_idx) / 4 + 2*l + 1]); |
| 32 | + |
| 33 | + FLOAT_TYPE sum = |
| 34 | + fma(FLOAT_TYPE(b0.x), FLOAT_TYPE((sign & 1) != 0 ? -grid0.x : grid0.x), |
| 35 | + fma(FLOAT_TYPE(b0.y), FLOAT_TYPE((sign & 2) != 0 ? -grid0.y : grid0.y), |
| 36 | + fma(FLOAT_TYPE(b0.z), FLOAT_TYPE((sign & 4) != 0 ? -grid0.z : grid0.z), |
| 37 | + fma(FLOAT_TYPE(b0.w), FLOAT_TYPE((sign & 8) != 0 ? -grid0.w : grid0.w), |
| 38 | + fma(FLOAT_TYPE(b4.x), FLOAT_TYPE((sign & 16) != 0 ? -grid1.x : grid1.x), |
| 39 | + fma(FLOAT_TYPE(b4.y), FLOAT_TYPE((sign & 32) != 0 ? -grid1.y : grid1.y), |
| 40 | + fma(FLOAT_TYPE(b4.z), FLOAT_TYPE((sign & 64) != 0 ? -grid1.z : grid1.z), |
| 41 | + fma(FLOAT_TYPE(b4.w), FLOAT_TYPE((sign7 & 1) != 0 ? -grid1.w : grid1.w), |
| 42 | + FLOAT_TYPE(0.0))))))))); |
| 43 | + temp[j][n] = fma(db, sum, temp[j][n]); |
| 44 | + } |
| 45 | + } |
| 46 | + ibi += num_blocks_per_row; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void compute_outputs(const uint32_t first_row, const uint32_t num_rows) { |
| 51 | + uint a_offset, b_offset, d_offset; |
| 52 | + get_offsets(a_offset, b_offset, d_offset); |
| 53 | + |
| 54 | + const uint num_blocks_per_row = p.ncols / QUANT_K; |
| 55 | + |
| 56 | + // 16 threads are used to process each block |
| 57 | + const uint blocks_per_wg = gl_WorkGroupSize.x/16; |
| 58 | + const uint tid = gl_LocalInvocationID.x; |
| 59 | + const uint itid = tid % 16; // 0...15 |
| 60 | + const uint ix = tid / 16; |
| 61 | + |
| 62 | + [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { |
| 63 | + [[unroll]] for (uint i = 0; i < NUM_ROWS; ++i) { |
| 64 | + temp[j][i] = FLOAT_TYPE(0); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + [[unroll]] for (uint i = ix; i < num_blocks_per_row; i += blocks_per_wg) |
| 69 | + calc_superblock(a_offset, b_offset, itid, i, num_blocks_per_row, first_row, num_rows); |
| 70 | + |
| 71 | + reduce_result(temp, d_offset, first_row, num_rows, tid); |
| 72 | +} |
| 73 | + |
| 74 | +void main() { |
| 75 | + const uint first_row = NUM_ROWS * (gl_WorkGroupID.x + gl_NumWorkGroups.x * gl_WorkGroupID.z); |
| 76 | + |
| 77 | + init_iq_shmem(gl_WorkGroupSize); |
| 78 | + |
| 79 | + // do NUM_ROWS at a time, unless there aren't enough remaining rows |
| 80 | + if (first_row + NUM_ROWS <= p.stride_d) { |
| 81 | + compute_outputs(first_row, NUM_ROWS); |
| 82 | + } else { |
| 83 | + if (first_row >= p.stride_d) { |
| 84 | + return; |
| 85 | + } |
| 86 | + compute_outputs(first_row, p.stride_d - first_row); |
| 87 | + } |
| 88 | +} |
0 commit comments