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-opencl / kernels / gemv_noshuffle_q5_k_f32_32b_trans.cl

141 lines · 5399 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2#pragma OPENCL EXTENSION cl_khr_subgroups : enable
3#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
4
5#define QK_K 256
6#define K_SCALE_SIZE 12
7#define N_SIMDGROUP 8
8#define SIMDGROUP_WIDTH 64
9
10inline void get_scale_min_k4(
11 int j,
12 global const uchar * q,
13 uint stride,
14 uchar * d,
15 uchar * m
16) {
17 if (j < 4) {
18 *d = q[j*stride] & 63;
19 *m = q[(j+4)*stride] & 63;
20 } else {
21 *d = (q[(j+4)*stride] & 0x0F) | ((q[(j-4)*stride] & 0xC0) >> 2);
22 *m = ((q[(j+4)*stride] >> 4) & 0x0F) | ((q[j*stride] & 0xC0) >> 2);
23 }
24}
25
26static inline float8 q5_k_to_fp32_packed8(ushort2 q4x8, uint qh_byte, float scale, float minv) {
27 float8 fp32x8;
28 fp32x8.s0 = (float)(( q4x8.s0 & 0x000F) | (((qh_byte >> 0) & 1) << 4)) * scale - minv;
29 fp32x8.s1 = (float)(((q4x8.s0 >> 4) & 0x000F) | (((qh_byte >> 1) & 1) << 4)) * scale - minv;
30 fp32x8.s2 = (float)(((q4x8.s0 >> 8) & 0x000F) | (((qh_byte >> 2) & 1) << 4)) * scale - minv;
31 fp32x8.s3 = (float)(((q4x8.s0 >> 12) & 0x000F) | (((qh_byte >> 3) & 1) << 4)) * scale - minv;
32 fp32x8.s4 = (float)(( q4x8.s1 & 0x000F) | (((qh_byte >> 4) & 1) << 4)) * scale - minv;
33 fp32x8.s5 = (float)(((q4x8.s1 >> 4) & 0x000F) | (((qh_byte >> 5) & 1) << 4)) * scale - minv;
34 fp32x8.s6 = (float)(((q4x8.s1 >> 8) & 0x000F) | (((qh_byte >> 6) & 1) << 4)) * scale - minv;
35 fp32x8.s7 = (float)(((q4x8.s1 >> 12) & 0x000F) | (((qh_byte >> 7) & 1) << 4)) * scale - minv;
36 return fp32x8;
37}
38
39__attribute__((qcom_reqd_sub_group_size("half")))
40__kernel void gemv_noshuffle_q5_k_f32_32b_trans(
41 read_only image1d_buffer_t src0_q,
42 __global uchar * src0_qh,
43 __global half * src0_d,
44 __global half * src0_dm,
45 __global uchar * src0_s,
46 __read_only image1d_buffer_t src1,
47 __global float * dst,
48 ulong offsetd,
49 int ne00,
50 int ne01
51) {
52 uint i01 = get_global_id(0);
53 uint sgid = get_local_id(1);
54 uint slid = get_sub_group_local_id();
55
56 int num_subblocks = ne00 / 32;
57
58 __private float sum = 0.0f;
59
60 // Loop over sub-blocks of 32 elements, N_SIMDGROUP sub-blocks per iter.
61 for (uint ib = sgid; ib < num_subblocks; ib += N_SIMDGROUP) {
62 uint sb = ib / 8;
63 uint j = ib % 8;
64
65 // Load d and dmin for this super-block.
66 half d_val = src0_d[sb * ne01 + i01];
67 half dm_val = src0_dm[sb * ne01 + i01];
68
69 // Load sub-block scale and min. s is transposed [nb][12][M]; stride ne01 per code.
70 global const uchar * sc = src0_s + sb * K_SCALE_SIZE * ne01 + i01;
71 uchar sv, mn;
72 get_scale_min_k4(j, sc, ne01, &sv, &mn);
73
74 float scale = (float)d_val * (float)sv;
75 float minv = (float)dm_val * (float)mn;
76
77 // Load 4 uints of quants (32 nibbles = 32 elements), column-major stride ne01.
78 uint q_base = ib * ne01 * 4 + i01;
79
80 uint4 regQ;
81 regQ.s0 = read_imageui(src0_q, q_base).x;
82 regQ.s1 = read_imageui(src0_q, q_base + ne01).x;
83 regQ.s2 = read_imageui(src0_q, q_base + ne01 * 2).x;
84 regQ.s3 = read_imageui(src0_q, q_base + ne01 * 3).x;
85
86 uint qh_grp = ib * 4;
87 uint qh_word = (uint)src0_qh[(qh_grp + 0) * ne01 + i01]
88 | ((uint)src0_qh[(qh_grp + 1) * ne01 + i01] << 8)
89 | ((uint)src0_qh[(qh_grp + 2) * ne01 + i01] << 16)
90 | ((uint)src0_qh[(qh_grp + 3) * ne01 + i01] << 24);
91
92 // Load activations: 32 floats = 8 float4s.
93 uint y_offset = ib * 8;
94
95 float4 y_local = (slid < 8) ? read_imagef(src1, (y_offset + slid)) : (float4)0.0f;
96 float4 y0 = sub_group_broadcast(y_local, 0);
97 float4 y1 = sub_group_broadcast(y_local, 1);
98 float4 y2 = sub_group_broadcast(y_local, 2);
99 float4 y3 = sub_group_broadcast(y_local, 3);
100 float4 y4 = sub_group_broadcast(y_local, 4);
101 float4 y5 = sub_group_broadcast(y_local, 5);
102 float4 y6 = sub_group_broadcast(y_local, 6);
103 float4 y7 = sub_group_broadcast(y_local, 7);
104
105 float8 fp32x8 = q5_k_to_fp32_packed8(as_ushort2(regQ.s0), qh_word & 0xFF, scale, minv);
106 float4 acc = y0 * fp32x8.lo;
107 acc += y1 * fp32x8.hi;
108
109 fp32x8 = q5_k_to_fp32_packed8(as_ushort2(regQ.s1), (qh_word >> 8) & 0xFF, scale, minv);
110 acc += y2 * fp32x8.lo;
111 acc += y3 * fp32x8.hi;
112
113 fp32x8 = q5_k_to_fp32_packed8(as_ushort2(regQ.s2), (qh_word >> 16) & 0xFF, scale, minv);
114 acc += y4 * fp32x8.lo;
115 acc += y5 * fp32x8.hi;
116
117 fp32x8 = q5_k_to_fp32_packed8(as_ushort2(regQ.s3), (qh_word >> 24) & 0xFF, scale, minv);
118 acc += y6 * fp32x8.lo;
119 acc += y7 * fp32x8.hi;
120
121 sum += ((acc.s0 + acc.s1) + (acc.s2 + acc.s3));
122 }
123
124 // reduction in local memory over N_SIMDGROUP subgroups
125 __local float reduceLM[SIMDGROUP_WIDTH * (N_SIMDGROUP - 1)];
126 if (sgid > 0) {
127 reduceLM[SIMDGROUP_WIDTH * (sgid - 1) + slid] = sum;
128 }
129 barrier(CLK_LOCAL_MEM_FENCE);
130 if (sgid == 0) {
131 for (uint i = 0; i < N_SIMDGROUP - 1; ++i) {
132 sum += reduceLM[SIMDGROUP_WIDTH * i + slid];
133 }
134 }
135
136 // 1 output per thread in subgroup 0
137 if (sgid == 0) {
138 dst = dst + (offsetd >> 2);
139 dst[i01] = sum;
140 }
141}