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

85 lines · 3670 bytes Raw Blame History
1#version 450
2#extension GL_EXT_shader_explicit_arithmetic_types : 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// Walks the packed bytes directly (byte m, digit t) rather than via
11// tq1_0_byte_of()/tq1_0_digit_of(): one byte per thread, expanded in place.
12void compute_outputs(const uint32_t first_row, const uint32_t num_rows) {
13 uint a_offset, b_offset, d_offset;
14 get_offsets(a_offset, b_offset, d_offset);
15
16 const uint num_blocks_per_row = p.ncols / QUANT_K;
17 const uint tid = gl_LocalInvocationID.x;
18
19 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
20 [[unroll]] for (uint i = 0; i < NUM_ROWS; ++i) {
21 temp[j][i] = FLOAT_TYPE(0);
22 }
23 }
24
25 for (uint nrow = 0; nrow < num_rows; ++nrow) {
26 const uint ib0 = a_offset + (first_row + nrow) * num_blocks_per_row;
27 for (uint jcol = 0; jcol < NUM_COLS; ++jcol) {
28 const uint b_base = (jcol * p.batch_stride_b);
29 for (uint i = tid/8; i < num_blocks_per_row; i += gl_WorkGroupSize.x/8) {
30 const FLOAT_TYPE d = float(data_a[ib0 + i].d);
31
32 // First qs chunk: 32 bytes (5*32 elements)
33 [[unroll]] for (uint m = tid%8; m < 32; m += 8) {
34 const uint q_byte = uint(data_a[ib0 + i].qs[m]);
35 [[unroll]] for (uint t = 0; t < 5; ++t) {
36 const uint xi = tq1_0_trit(q_byte, t);
37 const FLOAT_TYPE dequant_val = FLOAT_TYPE(d * (float(xi) - 1.0f));
38 const uint elem = t * 32u + m;
39 const uint b_idx = i * QUANT_K + elem;
40 temp[jcol][nrow] += dequant_val * FLOAT_TYPE(data_b[b_base + b_offset + b_idx]);
41 }
42 }
43
44 // Second qs chunk: 16 bytes (5*16 elements)
45 [[unroll]] for (uint m = tid%8; m < 16; m += 8) {
46 const uint q_byte = uint(data_a[ib0 + i].qs[32u + m]);
47 [[unroll]] for (uint t = 0; t < 5; ++t) {
48 const uint xi = tq1_0_trit(q_byte, t);
49 const FLOAT_TYPE dequant_val = FLOAT_TYPE(d * (float(xi) - 1.0f));
50 const uint elem = 160u + t * 16u + m;
51 const uint b_idx = i * QUANT_K + elem;
52 temp[jcol][nrow] += dequant_val * FLOAT_TYPE(data_b[b_base + b_offset + b_idx]);
53 }
54 }
55
56 // qh bytes: 4 bytes (4*4 elements)
57 [[unroll]] for (uint j = tid%8; j < 4; j += 8) {
58 const uint qh_byte = uint(data_a[ib0 + i].qh[j]);
59 [[unroll]] for (uint t = 0; t < 4; ++t) {
60 const uint xi = tq1_0_trit(qh_byte, t);
61 const FLOAT_TYPE dequant_val = FLOAT_TYPE(d * (float(xi) - 1.0f));
62 const uint elem = 240u + t * 4u + j;
63 const uint b_idx = i * QUANT_K + elem;
64 temp[jcol][nrow] += dequant_val * FLOAT_TYPE(data_b[b_base + b_offset + b_idx]);
65 }
66 }
67 }
68 }
69 }
70
71 reduce_result(temp, d_offset, first_row, num_rows, tid);
72}
73
74void main() {
75 const uint first_row = NUM_ROWS * (gl_WorkGroupID.x + gl_NumWorkGroups.x * gl_WorkGroupID.z);
76
77 if (first_row + NUM_ROWS <= p.stride_d) {
78 compute_outputs(first_row, NUM_ROWS);
79 } else {
80 if (first_row >= p.stride_d) {
81 return;
82 }
83 compute_outputs(first_row, p.stride_d - first_row);
84 }
85}