interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-vulkan / vulkan-shaders / mul_mat_vec_iq3_s.comp

93 lines · 4132 bytes Raw Blame History
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
10// invocations per superblock. with many columns, 8 invocations need too many
11// registers and spill, so use 16 to halve the per-invocation B working set
12const uint TPB = NUM_COLS <= 4 ? 8 : 16;
13const uint NL = 32 / TPB; // l steps per invocation
14
15void 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) {
16 const uint ib32 = itid / (TPB / 8);
17 const uint l0 = (itid % (TPB / 8)) * NL;
18 const uint y_idx = i * QUANT_K + 32 * ib32;
19
20 uint ibi = a_offset + first_row * num_blocks_per_row + i;
21 [[unroll]] for (uint n = 0; n < num_rows; ++n) {
22 const float d = float(data_a[ibi].d);
23 const uint scale = (data_a[ibi].scales[ib32/2] >> (4 * (ib32 & 1))) & 0xF;
24 const float dscale = d * (1 + 2 * scale);
25 const uint qh = data_a[ibi].qh[ib32];
26 [[unroll]] for (uint ll = 0; ll < NL; ++ll) {
27 const uint l = l0 + ll;
28 const u8vec2 qs = unpack8(uint32_t(data_a_packed16[ibi].qs[4 * ib32 + l])).xy; // vec4 used due to #12147
29 const uint sign = data_a[ibi].signs[4 * ib32 + l];
30 const vec4 grid0 = vec4(unpack8(iq3s_grid[qs.x | ((qh << (8 - 2*l)) & 0x100)]));
31 const vec4 grid1 = vec4(unpack8(iq3s_grid[qs.y | ((qh << (7 - 2*l)) & 0x100)]));
32
33 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
34 const vec4 b0 = vec4(data_b_v4[(j*p.batch_stride_b + b_offset + y_idx) / 4 + 2*l + 0]);
35 const vec4 b4 = vec4(data_b_v4[(j*p.batch_stride_b + b_offset + y_idx) / 4 + 2*l + 1]);
36
37 const FLOAT_TYPE sum =
38 fma(FLOAT_TYPE(b0.x), FLOAT_TYPE((sign & 1) != 0 ? -grid0.x : grid0.x),
39 fma(FLOAT_TYPE(b0.y), FLOAT_TYPE((sign & 2) != 0 ? -grid0.y : grid0.y),
40 fma(FLOAT_TYPE(b0.z), FLOAT_TYPE((sign & 4) != 0 ? -grid0.z : grid0.z),
41 fma(FLOAT_TYPE(b0.w), FLOAT_TYPE((sign & 8) != 0 ? -grid0.w : grid0.w),
42 fma(FLOAT_TYPE(b4.x), FLOAT_TYPE((sign & 16) != 0 ? -grid1.x : grid1.x),
43 fma(FLOAT_TYPE(b4.y), FLOAT_TYPE((sign & 32) != 0 ? -grid1.y : grid1.y),
44 fma(FLOAT_TYPE(b4.z), FLOAT_TYPE((sign & 64) != 0 ? -grid1.z : grid1.z),
45 fma(FLOAT_TYPE(b4.w), FLOAT_TYPE((sign & 128) != 0 ? -grid1.w : grid1.w),
46 FLOAT_TYPE(0.0)))))))));
47
48 temp[j][n] = fma(dscale, sum, temp[j][n]);
49 }
50 }
51 ibi += num_blocks_per_row;
52 }
53}
54
55void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
56 uint a_offset, b_offset, d_offset;
57 get_offsets(a_offset, b_offset, d_offset);
58
59 const uint num_blocks_per_row = p.ncols / QUANT_K;
60
61 // TPB invocations are used to process each block
62 const uint blocks_per_wg = gl_WorkGroupSize.x/TPB;
63 const uint tid = gl_LocalInvocationID.x;
64 const uint itid = tid % TPB;
65 const uint ix = tid / TPB;
66
67 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
68 [[unroll]] for (uint i = 0; i < NUM_ROWS; ++i) {
69 temp[j][i] = FLOAT_TYPE(0);
70 }
71 }
72
73 [[unroll]] for (uint i = ix; i < num_blocks_per_row; i += blocks_per_wg)
74 calc_superblock(a_offset, b_offset, itid, i, num_blocks_per_row, first_row, num_rows);
75
76 reduce_result(temp, d_offset, first_row, num_rows, tid);
77}
78
79void main() {
80 const uint first_row = NUM_ROWS * (gl_WorkGroupID.x + gl_NumWorkGroups.x * gl_WorkGroupID.z);
81
82 init_iq_shmem(gl_WorkGroupSize);
83
84 // do NUM_ROWS at a time, unless there aren't enough remaining rows
85 if (first_row + NUM_ROWS <= p.stride_d) {
86 compute_outputs(first_row, NUM_ROWS);
87 } else {
88 if (first_row >= p.stride_d) {
89 return;
90 }
91 compute_outputs(first_row, p.stride_d - first_row);
92 }
93}