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 / mul_mv_q3_k_f32.cl

197 lines · 6550 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#ifdef cl_intel_required_subgroup_size
4#pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable
5#define INTEL_GPU 1
6#define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16)))
7#define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32)))
8#elif defined(cl_qcom_reqd_sub_group_size)
9#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
10#define ADRENO_GPU 1
11#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
12#define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full")))
13#endif
14
15//------------------------------------------------------------------------------
16// block_q3_K
17//------------------------------------------------------------------------------
18#define QK_K 256
19
20// 16 blocks of 16 elements each
21// weight is represented as x = a * q
22typedef struct {
23 uchar hmask[QK_K/8]; // quants - high bit
24 uchar qs[QK_K/4]; // quants - low 2 bits
25 uchar scales[12]; // scales, quantized with 6 bits
26 half d; // super-block scale
27} block_q3_K;
28
29#undef N_DST
30#undef N_SIMDGROUP
31#undef N_SIMDWIDTH
32
33#ifdef INTEL_GPU
34#define N_DST 2 // number of rows each SIMD group works on
35#define N_SIMDGROUP 1 // number of SIMD groups in a thread group
36#define N_SIMDWIDTH 16 // SIMD group size
37#elif defined (ADRENO_GPU)
38#define N_DST 2
39#define N_SIMDGROUP 1
40#define N_SIMDWIDTH 64
41#endif
42
43#undef BLOCK_STRIDE
44// 8 threads cover one super block, so a wave covers N_SIMDWIDTH/8 of them
45#define BLOCK_STRIDE (N_SIMDWIDTH/8)
46
47#ifdef INTEL_GPU
48REQD_SUBGROUP_SIZE_16
49#elif defined (ADRENO_GPU)
50REQD_SUBGROUP_SIZE_64
51#endif
52kernel void kernel_mul_mv_q3_K_f32(
53 global char * src0,
54 ulong offset0,
55 global char * src1,
56 ulong offset1,
57 global char * dst,
58 ulong offsetd,
59 int ne00,
60 int ne01,
61 ulong nb01,
62 ulong nb02,
63 ulong nb03,
64 int ne12,
65 ulong nb11,
66 ulong nb12,
67 ulong nb13,
68 int ne0,
69 int ne1,
70 int r2,
71 int r3
72) {
73 src0 = src0 + offset0;
74 src1 = src1 + offset1;
75 dst = dst + offsetd;
76
77 int nb = ne00/QK_K;
78
79 int r0 = get_group_id(0);
80 int r1 = get_group_id(1);
81 int im = get_group_id(2);
82 int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST;
83
84 int i12 = im%ne12;
85 int i13 = im/ne12;
86
87 ulong offset_src0 = first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03;
88 ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13;
89
90 global block_q3_K * x = (global block_q3_K *) (src0 + offset_src0);
91 global float * yy = (global float *) (src1 + offset_src1);
92
93 float yl[32];
94
95 // Eight lanes cover one super-block.
96 int ix = get_sub_group_local_id() % BLOCK_STRIDE;
97 int tid = get_sub_group_local_id() / BLOCK_STRIDE;
98
99 int ip = tid/4; // 0 or 1
100 int il = 2*((tid%4)/2); // 0 or 2
101 int ir = tid%2;
102 int l0 = 8*ir;
103
104 // Shift the masks instead of indexing private arrays to avoid spills.
105 ushort4 hm = (ushort4)(0x0001, 0x0100, 0x0002, 0x0200) << (ushort4)(2*(2*ip + il/2));
106 int4 qm = (int4) (0x0003, 0x0300, 0x000c, 0x0c00) << (int4) (4*(il/2));
107
108 int shift = 2*il;
109
110 float v1 = il == 0 ? 4.f : 64.f;
111 float v2 = 4.f * v1;
112
113 int s_shift1 = 4*ip;
114 int s_shift2 = s_shift1 + il;
115
116 int q_offset = 32*ip + l0;
117 int y_offset = 128*ip + 32*il + l0;
118
119 global float * y1 = yy + ix*QK_K + y_offset;
120
121 float sumf1[N_DST] = {0.f};
122 float sumf2[N_DST] = {0.f};
123 float all_sum;
124
125 for (int i = ix; i < nb; i += BLOCK_STRIDE) {
126 for (int l = 0; l < 8; ++l) {
127 yl[l+ 0] = y1[l+ 0];
128 yl[l+ 8] = y1[l+16];
129 yl[l+16] = y1[l+32];
130 yl[l+24] = y1[l+48];
131 }
132
133 global ushort * q = (global ushort *)(x[i].qs + q_offset);
134 global ushort * h = (global ushort *)(x[i].hmask + l0);
135 global ushort * a = (global ushort *)(x[i].scales);
136 global half * dh = &x[i].d;
137
138 // only ne01 output rows exist; reading past them can pull in an inf scale
139 for (int row = 0; row < N_DST && first_row + row < ne01; ++row) {
140 float d_all = dh[0];
141
142 uint s32 = (uint)a[4] | ((uint)a[5] << 16);
143 uint aux32 = ((s32 >> s_shift2) << 4) & 0x30303030u;
144 s32 = (uint)a[il+0] | ((uint)a[il+1] << 16);
145 s32 = ((s32 >> s_shift1) & 0x0f0f0f0fu) | aux32;
146 char4 sc = as_char4(s32);
147
148 float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
149 for (int l = 0; l < 8; l += 2) {
150 int qs = q[l/2];
151 s1 += yl[l+0] * (qs & qm.s0);
152 s2 += yl[l+1] * (qs & qm.s1);
153 s3 += ((h[l/2] & hm.s0) ? 0.f : yl[l+0]) + ((h[l/2] & hm.s1) ? 0.f : yl[l+1]);
154 s4 += yl[l+16] * (qs & qm.s2);
155 s5 += yl[l+17] * (qs & qm.s3);
156 s6 += ((h[l/2] & hm.s2) ? 0.f : yl[l+16]) + ((h[l/2] & hm.s3) ? 0.f : yl[l+17]);
157 }
158 float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
159 float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
160 sumf1[row] += d1 * (sc.s0 - 32);
161 sumf2[row] += d2 * (sc.s2 - 32);
162
163 s1 = s2 = s3 = s4 = s5 = s6 = 0;
164 for (int l = 0; l < 8; l += 2) {
165 int qs = q[l/2+8];
166 s1 += yl[l+8] * (qs & qm.s0);
167 s2 += yl[l+9] * (qs & qm.s1);
168 s3 += ((h[l/2+8] & hm.s0) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm.s1) ? 0.f : yl[l+9]);
169 s4 += yl[l+24] * (qs & qm.s2);
170 s5 += yl[l+25] * (qs & qm.s3);
171 s6 += ((h[l/2+8] & hm.s2) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm.s3) ? 0.f : yl[l+25]);
172 }
173 d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
174 d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
175 sumf1[row] += d1 * (sc.s1 - 32);
176 sumf2[row] += d2 * (sc.s3 - 32);
177
178 q += nb01/2;
179 h += nb01/2;
180 a += nb01/2;
181 dh += nb01/2;
182 }
183
184 y1 += BLOCK_STRIDE * QK_K;
185 }
186
187 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0;
188
189 for (int row = 0; row < N_DST; ++row) {
190 all_sum = sub_group_reduce_add((sumf1[row] + 0.25f * sumf2[row]) / (1 << shift));
191 if (first_row + row < ne01) {
192 if (get_sub_group_local_id() == 0) {
193 dst_f32[first_row + row] = all_sum;
194 }
195 }
196 }
197}