2024-06-16 05:17:31 +00:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
#ifdef FLOAT16
|
|
|
|
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "mul_mat_vec_base.comp"
|
|
|
|
|
|
|
|
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
|
|
|
|
|
|
|
|
layout (constant_id = 0) const uint BLOCK_SIZE = 32;
|
|
|
|
|
|
|
|
shared FLOAT_TYPE tmp[BLOCK_SIZE];
|
|
|
|
|
|
|
|
void main() {
|
2024-06-23 08:21:25 +00:00
|
|
|
const uint row = gl_WorkGroupID.x + gl_NumWorkGroups.x * gl_WorkGroupID.z;
|
2024-06-16 05:17:31 +00:00
|
|
|
const uint tid = gl_LocalInvocationID.x;
|
|
|
|
|
2024-08-05 05:52:55 +00:00
|
|
|
// There are not enough cols to use all threads
|
|
|
|
if (tid >= p.ncols) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint block_size = min(p.ncols, BLOCK_SIZE);
|
|
|
|
|
2024-06-16 05:17:31 +00:00
|
|
|
uint a_offset, b_offset, d_offset;
|
|
|
|
get_offsets(a_offset, b_offset, d_offset);
|
|
|
|
|
|
|
|
const uint y_offset = QUANT_R == 1 ? 1 : QUANT_K/2;
|
|
|
|
|
|
|
|
tmp[tid] = FLOAT_TYPE(0.0f);
|
|
|
|
|
2024-08-05 05:52:55 +00:00
|
|
|
[[unroll]] for (uint i = 0; i < p.ncols/block_size; i += 2) {
|
|
|
|
const uint col = i*block_size + 2*tid;
|
2024-06-16 05:17:31 +00:00
|
|
|
const uint ib = (row*p.ncols + col)/QUANT_K; // block index
|
|
|
|
const uint iqs = (col%QUANT_K)/QUANT_R; // quant index
|
|
|
|
const uint iybs = col - col%QUANT_K; // y block start index
|
|
|
|
|
|
|
|
vec2 v = dequantize(ib, iqs, a_offset / QUANT_K);
|
|
|
|
|
|
|
|
// matrix multiplication
|
2024-08-14 16:32:53 +00:00
|
|
|
tmp[tid] = fma(FLOAT_TYPE(v.x), FLOAT_TYPE(data_b[b_offset + iybs + iqs]), fma(FLOAT_TYPE(v.y), FLOAT_TYPE(data_b[b_offset + iybs + iqs + y_offset]), tmp[tid]));
|
2024-06-16 05:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sum up partial sums and write back result
|
|
|
|
barrier();
|
2024-08-05 05:52:55 +00:00
|
|
|
[[unroll]] for (uint s = block_size/2; s > 0; s >>= 1) {
|
2024-06-16 05:17:31 +00:00
|
|
|
if (tid < s) {
|
|
|
|
tmp[tid] += tmp[tid + s];
|
|
|
|
}
|
|
|
|
barrier();
|
|
|
|
}
|
|
|
|
if (tid == 0) {
|
|
|
|
data_d[d_offset + row] = D_TYPE(tmp[0]);
|
|
|
|
}
|
|
|
|
}
|