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

199 lines · 6734 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_flat(
53 global uchar * src0_qs,
54 global uchar * src0_hm,
55 global uchar * src0_sc,
56 global half * src0_d,
57 ulong offset0,
58 global char * src1,
59 ulong offset1,
60 global char * dst,
61 ulong offsetd,
62 int ne00,
63 int ne01,
64 ulong nb01,
65 ulong nb02,
66 ulong nb03,
67 int ne12,
68 ulong nb11,
69 ulong nb12,
70 ulong nb13,
71 int ne0,
72 int ne1,
73 int r2,
74 int r3
75) {
76 src1 = src1 + offset1;
77 dst = dst + offsetd;
78
79 int nb = ne00/QK_K;
80
81 int r0 = get_group_id(0);
82 int r1 = get_group_id(1);
83 int im = get_group_id(2);
84 int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST;
85
86 int i12 = im%ne12;
87 int i13 = im/ne12;
88
89 ulong offset_src0 = (offset0 + first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03)/sizeof(block_q3_K);
90 ulong row_stride = nb01/sizeof(block_q3_K);
91 ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13;
92
93 global float * yy = (global float *) (src1 + offset_src1);
94
95 float yl[32];
96
97 // Eight lanes cover one super-block.
98 int ix = get_sub_group_local_id() % BLOCK_STRIDE;
99 int tid = get_sub_group_local_id() / BLOCK_STRIDE;
100
101 int ip = tid/4; // 0 or 1
102 int il = 2*((tid%4)/2); // 0 or 2
103 int ir = tid%2;
104 int l0 = 8*ir;
105
106 // Shift the masks instead of indexing private arrays to avoid spills.
107 ushort4 hm = (ushort4)(0x0001, 0x0100, 0x0002, 0x0200) << (ushort4)(2*(2*ip + il/2));
108 int4 qm = (int4) (0x0003, 0x0300, 0x000c, 0x0c00) << (int4) (4*(il/2));
109
110 int shift = 2*il;
111
112 float v1 = il == 0 ? 4.f : 64.f;
113 float v2 = 4.f * v1;
114
115 int s_shift1 = 4*ip;
116 int s_shift2 = s_shift1 + il;
117
118 int q_offset = 32*ip + l0;
119 int y_offset = 128*ip + 32*il + l0;
120
121 global float * y1 = yy + ix*QK_K + y_offset;
122
123 float sumf1[N_DST] = {0.f};
124 float sumf2[N_DST] = {0.f};
125 float all_sum;
126
127 for (int i = ix; i < nb; i += BLOCK_STRIDE) {
128 for (int l = 0; l < 8; ++l) {
129 yl[l+ 0] = y1[l+ 0];
130 yl[l+ 8] = y1[l+16];
131 yl[l+16] = y1[l+32];
132 yl[l+24] = y1[l+48];
133 }
134
135 global ushort * q = (global ushort *)(src0_qs + 64*(offset_src0 + i) + q_offset);
136 global ushort * h = (global ushort *)(src0_hm + 32*(offset_src0 + i) + l0);
137 global ushort * a = (global ushort *)(src0_sc + 12*(offset_src0 + i));
138 global half * dh = src0_d + offset_src0 + i;
139
140 // only ne01 output rows exist; reading past them can pull in an inf scale
141 for (int row = 0; row < N_DST && first_row + row < ne01; ++row) {
142 float d_all = dh[0];
143
144 uint s32 = (uint)a[4] | ((uint)a[5] << 16);
145 uint aux32 = ((s32 >> s_shift2) << 4) & 0x30303030u;
146 s32 = (uint)a[il+0] | ((uint)a[il+1] << 16);
147 s32 = ((s32 >> s_shift1) & 0x0f0f0f0fu) | aux32;
148 char4 sc = as_char4(s32);
149
150 float s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
151 for (int l = 0; l < 8; l += 2) {
152 int qs = q[l/2];
153 s1 += yl[l+0] * (qs & qm.s0);
154 s2 += yl[l+1] * (qs & qm.s1);
155 s3 += ((h[l/2] & hm.s0) ? 0.f : yl[l+0]) + ((h[l/2] & hm.s1) ? 0.f : yl[l+1]);
156 s4 += yl[l+16] * (qs & qm.s2);
157 s5 += yl[l+17] * (qs & qm.s3);
158 s6 += ((h[l/2] & hm.s2) ? 0.f : yl[l+16]) + ((h[l/2] & hm.s3) ? 0.f : yl[l+17]);
159 }
160 float d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
161 float d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
162 sumf1[row] += d1 * (sc.s0 - 32);
163 sumf2[row] += d2 * (sc.s2 - 32);
164
165 s1 = s2 = s3 = s4 = s5 = s6 = 0;
166 for (int l = 0; l < 8; l += 2) {
167 int qs = q[l/2+8];
168 s1 += yl[l+8] * (qs & qm.s0);
169 s2 += yl[l+9] * (qs & qm.s1);
170 s3 += ((h[l/2+8] & hm.s0) ? 0.f : yl[l+8]) + ((h[l/2+8] & hm.s1) ? 0.f : yl[l+9]);
171 s4 += yl[l+24] * (qs & qm.s2);
172 s5 += yl[l+25] * (qs & qm.s3);
173 s6 += ((h[l/2+8] & hm.s2) ? 0.f : yl[l+24]) + ((h[l/2+8] & hm.s3) ? 0.f : yl[l+25]);
174 }
175 d1 = d_all * (s1 + 1.f/256.f * s2 - s3*v1);
176 d2 = d_all * (s4 + 1.f/256.f * s5 - s6*v2);
177 sumf1[row] += d1 * (sc.s1 - 32);
178 sumf2[row] += d2 * (sc.s3 - 32);
179
180 q += row_stride*32;
181 h += row_stride*16;
182 a += row_stride*6;
183 dh += row_stride*1;
184 }
185
186 y1 += BLOCK_STRIDE * QK_K;
187 }
188
189 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0;
190
191 for (int row = 0; row < N_DST; ++row) {
192 all_sum = sub_group_reduce_add((sumf1[row] + 0.25f * sumf2[row]) / (1 << shift));
193 if (first_row + row < ne01) {
194 if (get_sub_group_local_id() == 0) {
195 dst_f32[first_row + row] = all_sum;
196 }
197 }
198 }
199}