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_q4_k_f32_32b_trans.cl

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