Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-vulkan/vulkan-shaders/mul_mat_vecq.comp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-vulkan / vulkan-shaders / mul_mat_vecq.comp` 142 lines ยท 4990 bytes Raw Blame History 1 #version 450 2 3 #extension GL_EXT_shader_explicit_arithmetic_types_int32 : require 4 #extension GL_EXT_integer_dot_product : require 5 6 #define MMQ 7 #define NEEDS_IQ1S_GRID_GPU 8 #define KVALUES_IQ4NL_I8 9 #define B_TYPE block_q8_1_x4 10 11 #include "mul_mat_vec_base.glsl" 12 13 layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; 14 15 #if defined(DATA_A_Q2_0) || defined(DATA_A_QUANT_K) 16 #define K_PER_ITER 16 17 #elif defined(DATA_A_QUANT_LEGACY) || defined(DATA_A_MXFP4) 18 #define K_PER_ITER 8 19 #elif defined(DATA_A_IQ1_S) || defined(DATA_A_IQ1_M) || defined(DATA_A_IQ4_XS) 20 #define K_PER_ITER 32 21 #else 22 #error unimplemented 23 #endif 24 25 uint a_offset, b_offset, d_offset; 26 27 int32_t cache_b_qs[K_PER_ITER / 4]; 28 vec2 cache_b_ds; 29 30 #include "mul_mat_vecq_funcs.glsl" 31 32 void iter(inout FLOAT_TYPE temp[NUM_COLS][NUM_ROWS], const uint first_row, const uint num_rows, const uint col, const uint b_qs_idx) { 33 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { 34 // Preload data_b block 35 const uint b_block_idx = (j*p.batch_stride_b + col) / QUANT_K_Q8_1 + b_offset; 36 const uint b_block_idx_outer = b_block_idx / 4; 37 const uint b_block_idx_inner = b_block_idx % 4; 38 cache_b_ds = vec2(data_b[b_block_idx_outer].ds[b_block_idx_inner]); 39 40 #if QUANT_R == 2 41 // Assumes K_PER_ITER == 8 42 cache_b_qs[0] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + b_qs_idx]; 43 cache_b_qs[1] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + b_qs_idx + 4]; 44 #else 45 #if K_PER_ITER == 8 46 cache_b_qs[0] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + b_qs_idx * 2]; 47 cache_b_qs[1] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + b_qs_idx * 2 + 1]; 48 #elif K_PER_ITER == 16 49 cache_b_qs[0] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + b_qs_idx * 4 ]; 50 cache_b_qs[1] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + b_qs_idx * 4 + 1]; 51 cache_b_qs[2] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + b_qs_idx * 4 + 2]; 52 cache_b_qs[3] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + b_qs_idx * 4 + 3]; 53 #elif K_PER_ITER == 32 54 cache_b_qs[0] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 ]; 55 cache_b_qs[1] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + 1]; 56 cache_b_qs[2] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + 2]; 57 cache_b_qs[3] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + 3]; 58 cache_b_qs[4] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + 4]; 59 cache_b_qs[5] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + 5]; 60 cache_b_qs[6] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + 6]; 61 cache_b_qs[7] = data_b[b_block_idx_outer].qs[b_block_idx_inner * 8 + 7]; 62 #else 63 #error unimplemented 64 #endif 65 #endif 66 67 uint ibi = first_row*p.ncols; 68 [[unroll]] for (uint n = 0; n < num_rows; ++n) { 69 const uint a_block_idx = (ibi + col)/QUANT_K_Q8_1 + a_offset; 70 ibi += p.ncols; 71 72 temp[j][n] += mmvq_dot_product(a_block_idx, b_qs_idx); 73 } 74 } 75 } 76 77 void compute_outputs(const uint32_t first_row, const uint32_t num_rows) { 78 const uint tid = gl_LocalInvocationID.x; 79 80 get_offsets(a_offset, b_offset, d_offset); 81 a_offset *= QUANT_K / QUANT_K_Q8_1; 82 b_offset /= QUANT_K_Q8_1; 83 84 FLOAT_TYPE temp[NUM_COLS][NUM_ROWS]; 85 86 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { 87 [[unroll]] for (uint n = 0; n < num_rows; ++n) { 88 temp[j][n] = FLOAT_TYPE(0.0f); 89 } 90 } 91 92 const uint col_stride = K_PER_ITER * BLOCK_SIZE; 93 uint num_iters = p.ncols / col_stride; 94 if (num_iters * col_stride + K_PER_ITER * tid < p.ncols) { 95 num_iters++; 96 } 97 98 const uint b_qs_idx = tid % (32 / K_PER_ITER); 99 uint col = tid * K_PER_ITER; 100 while (num_iters >= 4) { 101 // Manually partially unroll the loop 102 [[unroll]] for (uint k = 0; k < 4; ++k) { 103 iter(temp, first_row, num_rows, col, b_qs_idx); 104 col += col_stride; 105 } 106 107 num_iters -= 4; 108 } 109 110 if (num_iters >= 2) { 111 // Manually partially unroll the loop 112 iter(temp, first_row, num_rows, col, b_qs_idx); 113 col += col_stride; 114 iter(temp, first_row, num_rows, col, b_qs_idx); 115 col += col_stride; 116 num_iters -= 2; 117 } 118 119 if (num_iters > 0) { 120 iter(temp, first_row, num_rows, col, b_qs_idx); 121 } 122 123 reduce_result(temp, d_offset, first_row, num_rows, tid); 124 } 125 126 void main() { 127 const uint first_row = NUM_ROWS * (gl_WorkGroupID.x + gl_NumWorkGroups.x * gl_WorkGroupID.z); 128 129 #ifdef NEEDS_INIT_IQ_SHMEM 130 init_iq_shmem(gl_WorkGroupSize); 131 #endif 132 133 // do NUM_ROWS at a time, unless there aren't enough remaining rows 134 if (first_row + NUM_ROWS <= p.stride_d) { 135 compute_outputs(first_row, NUM_ROWS); 136 } else { 137 if (first_row >= p.stride_d) { 138 return; 139 } 140 compute_outputs(first_row, p.stride_d - first_row); 141 } 142 }