1#version 450
2#extension GL_EXT_shader_explicit_arithmetic_types_int32 : require
3
4#include "mul_mat_vec_base.glsl"
5
6layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
7
8FLOAT_TYPE temp[NUM_COLS][NUM_ROWS];
9
10void calc_superblock(const uint a_offset, const uint b_offset, const uint ib32, const uint i,
11 const uint num_blocks_per_row, const uint first_row, const uint num_rows) {
12 const uint y_idx_base = i * QUANT_K + 32 * ib32;
13 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
14 const uint base_b_idx = (j * p.batch_stride_b + b_offset + y_idx_base) / 4;
15 [[unroll]] for (uint l = 0; l < 4; ++l) {
16 const vec4 b_val_0 = vec4(data_b_v4[base_b_idx + 2 * l]);
17 const vec4 b_val_1 = vec4(data_b_v4[base_b_idx + 2 * l + 1]);
18
19 // index for data_a
20 uint ibi = a_offset + first_row * num_blocks_per_row + i;
21
22 [[unroll]] for (uint n = 0; n < num_rows; ++n) {
23 const float d = float(data_a[ibi].d);
24 const uint qh = data_a[ibi].qh[ib32];
25
26 const float dl = d * float(2 * bitfieldExtract(qh, 12, 3) + 1);
27 const uint qs = data_a[ibi].qs[4 * ib32 + l];
28 const uint idxhi = bitfieldExtract(qh, 3 * int(l), 3);
29 const uint16_t grid = uint16_t(iq1s_grid[qs | (idxhi << 8)]);
30
31 const float delta_val = ((qh & 0x8000) != 0) ? -IQ1S_DELTA : IQ1S_DELTA;
32 const vec4 delta_v = vec4(delta_val);
33 const vec4 fbits0 = vec4(
34 float(bitfieldExtract(grid, 0, 2)),
35 float(bitfieldExtract(grid, 2, 2)),
36 float(bitfieldExtract(grid, 4, 2)),
37 float(bitfieldExtract(grid, 6, 2))
38 );
39 const vec4 fbits1 = vec4(
40 float(bitfieldExtract(grid, 8, 2)),
41 float(bitfieldExtract(grid, 10, 2)),
42 float(bitfieldExtract(grid, 12, 2)),
43 float(bitfieldExtract(grid, 14, 2))
44 );
45
46 vec4 sum_v = fma(b_val_0, fbits0 + delta_v, vec4(0.0));
47 sum_v = fma(b_val_1, fbits1 + delta_v, sum_v);
48 FLOAT_TYPE sum = dot(sum_v, vec4(1.0));
49
50 temp[j][n] = fma(dl, sum, temp[j][n]);
51 ibi += num_blocks_per_row;
52 }
53 }
54 }
55}
56
57void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
58 uint a_offset, b_offset, d_offset;
59 get_offsets(a_offset, b_offset, d_offset);
60
61 const uint num_blocks_per_row = p.ncols / QUANT_K;
62
63 // 8 threads are used to process each block
64 const uint blocks_per_wg = gl_WorkGroupSize.x/8;
65 const uint tid = gl_LocalInvocationID.x;
66 const uint itid = tid % 8; // 0...7
67 const uint ix = tid / 8;
68
69 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
70 [[unroll]] for (uint i = 0; i < NUM_ROWS; ++i) {
71 temp[j][i] = FLOAT_TYPE(0);
72 }
73 }
74
75 [[unroll]] for (uint i = ix; i < num_blocks_per_row; i += blocks_per_wg)
76 calc_superblock(a_offset, b_offset, itid, i, num_blocks_per_row, first_row, num_rows);
77
78 reduce_result(temp, d_offset, first_row, num_rows, tid);
79}
80
81void main() {
82 const uint first_row = NUM_ROWS * (gl_WorkGroupID.x + gl_NumWorkGroups.x * gl_WorkGroupID.z);
83
84 init_iq_shmem(gl_WorkGroupSize);
85
86 // do NUM_ROWS at a time, unless there aren't enough remaining rows
87 if (first_row + NUM_ROWS <= p.stride_d) {
88 compute_outputs(first_row, NUM_ROWS);
89 } else {
90 if (first_row >= p.stride_d) {
91 return;
92 }
93 compute_outputs(first_row, p.stride_d - first_row);
94 }
95}