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_iq4_xs.comp

97 lines · 3796 bytes Raw Blame History
1#version 450
2
3#extension GL_EXT_shader_explicit_arithmetic_types_int32 : require
4
5#include "mul_mat_vec_base.glsl"
6
7layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
8
9FLOAT_TYPE temp[NUM_COLS][NUM_ROWS];
10
11// dedicated iq4_xs mat-vec, mirrors mul_mat_vec_iq3_s.comp
12// one packed32 word per l, so the 6-bit subblock scale is hoisted to a single fma after register accumulation
13
14void calc_superblock(const uint a_offset, const uint b_offset, const uint ib32, const uint i, const uint num_blocks_per_row, const uint first_row, const uint num_rows) {
15 const uint y_idx = i * QUANT_K + 32 * ib32;
16
17 uint ibi = a_offset + first_row * num_blocks_per_row + i;
18 [[unroll]] for (uint n = 0; n < num_rows; ++n) {
19 const float d = float(data_a[ibi].d);
20 const uint sl = (data_a[ibi].scales_l[ib32/2] >> (4 * (ib32 & 1))) & 0xF;
21 const uint sh = (data_a[ibi].scales_h >> (2 * ib32)) & 3;
22 const float dscale = d * float(int(sl | (sh << 4)) - 32);
23
24 FLOAT_TYPE sum[NUM_COLS];
25 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
26 sum[j] = FLOAT_TYPE(0);
27 }
28
29 [[unroll]] for (uint l = 0; l < 4; ++l) {
30 const uint w = data_a_packed32[ibi].qs[4 * ib32 + l];
31 const u8vec4 q0 = unpack8(w & 0x0F0F0F0F);
32 const u8vec4 q1 = unpack8((w >> 4) & 0x0F0F0F0F);
33
34 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
35 const vec4 b0 = vec4(data_b_v4[(j*p.batch_stride_b + b_offset + y_idx) / 4 + l]);
36 const vec4 b1 = vec4(data_b_v4[(j*p.batch_stride_b + b_offset + y_idx) / 4 + 4 + l]);
37
38 sum[j] = fma(FLOAT_TYPE(b0.x), FLOAT_TYPE(kvalues_iq4nl[q0.x]),
39 fma(FLOAT_TYPE(b0.y), FLOAT_TYPE(kvalues_iq4nl[q0.y]),
40 fma(FLOAT_TYPE(b0.z), FLOAT_TYPE(kvalues_iq4nl[q0.z]),
41 fma(FLOAT_TYPE(b0.w), FLOAT_TYPE(kvalues_iq4nl[q0.w]),
42 fma(FLOAT_TYPE(b1.x), FLOAT_TYPE(kvalues_iq4nl[q1.x]),
43 fma(FLOAT_TYPE(b1.y), FLOAT_TYPE(kvalues_iq4nl[q1.y]),
44 fma(FLOAT_TYPE(b1.z), FLOAT_TYPE(kvalues_iq4nl[q1.z]),
45 fma(FLOAT_TYPE(b1.w), FLOAT_TYPE(kvalues_iq4nl[q1.w]),
46 sum[j]))))))));
47 }
48 }
49
50 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
51 temp[j][n] = fma(dscale, sum[j], temp[j][n]);
52 }
53
54 ibi += num_blocks_per_row;
55 }
56}
57
58void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
59 uint a_offset, b_offset, d_offset;
60
61 get_offsets(a_offset, b_offset, d_offset);
62
63 const uint num_blocks_per_row = p.ncols / QUANT_K;
64
65 // 8 threads are used to process each block
66 const uint blocks_per_wg = gl_WorkGroupSize.x/8;
67 const uint tid = gl_LocalInvocationID.x;
68 const uint itid = tid % 8; // 0...7
69 const uint ix = tid / 8;
70
71 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
72 [[unroll]] for (uint i = 0; i < NUM_ROWS; ++i) {
73 temp[j][i] = FLOAT_TYPE(0);
74 }
75 }
76
77 [[unroll]] for (uint i = ix; i < num_blocks_per_row; i += blocks_per_wg)
78 calc_superblock(a_offset, b_offset, itid, i, num_blocks_per_row, first_row, num_rows);
79
80 reduce_result(temp, d_offset, first_row, num_rows, tid);
81}
82
83void main() {
84 const uint first_row = NUM_ROWS * (gl_WorkGroupID.x + gl_NumWorkGroups.x * gl_WorkGroupID.z);
85
86 init_iq_shmem(gl_WorkGroupSize);
87
88 // do NUM_ROWS at a time, unless there aren't enough remaining rows
89 if (first_row + NUM_ROWS <= p.stride_d) {
90 compute_outputs(first_row, NUM_ROWS);
91 } else {
92 if (first_row >= p.stride_d) {
93 return;
94 }
95 compute_outputs(first_row, p.stride_d - first_row);
96 }
97}