mul_mat_vec.comp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #version 450
  2. #ifdef FLOAT16
  3. #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
  4. #endif
  5. #include "mul_mat_vec_base.comp"
  6. layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
  7. layout (constant_id = 0) const uint BLOCK_SIZE = 32;
  8. shared FLOAT_TYPE tmp[BLOCK_SIZE];
  9. void main() {
  10. const uint row = gl_WorkGroupID.x;
  11. const uint tid = gl_LocalInvocationID.x;
  12. uint a_offset, b_offset, d_offset;
  13. get_offsets(a_offset, b_offset, d_offset);
  14. const uint y_offset = QUANT_R == 1 ? 1 : QUANT_K/2;
  15. tmp[tid] = FLOAT_TYPE(0.0f);
  16. [[unroll]] for (uint i = 0; i < p.ncols/BLOCK_SIZE; i += 2) {
  17. const uint col = i*BLOCK_SIZE + 2*tid;
  18. const uint ib = (row*p.ncols + col)/QUANT_K; // block index
  19. const uint iqs = (col%QUANT_K)/QUANT_R; // quant index
  20. const uint iybs = col - col%QUANT_K; // y block start index
  21. vec2 v = dequantize(ib, iqs, a_offset / QUANT_K);
  22. // matrix multiplication
  23. tmp[tid] += FLOAT_TYPE(v.x) * FLOAT_TYPE(data_b[b_offset + iybs + iqs]) +
  24. FLOAT_TYPE(v.y) * FLOAT_TYPE(data_b[b_offset + iybs + iqs + y_offset]);
  25. }
  26. // sum up partial sums and write back result
  27. barrier();
  28. [[unroll]] for (uint s = BLOCK_SIZE/2; s > 0; s >>= 1) {
  29. if (tid < s) {
  30. tmp[tid] += tmp[tid + s];
  31. }
  32. barrier();
  33. }
  34. if (tid == 0) {
  35. data_d[d_offset + row] = D_TYPE(tmp[0]);
  36. }
  37. }