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

205 lines · 7680 bytes Raw Blame History
1// Cooperative-K DP4A GEMM for 2 to 4 columns with Q4_0 weights and Q8_1 activations.
2// Named vector components avoid dynamic private-array indexing, which can cause register spills.
3
4#pragma OPENCL EXTENSION cl_khr_fp16 : enable
5#ifdef cl_khr_integer_dot_product
6#pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable
7#endif
8
9#ifndef COOPK_NSG
10#define COOPK_NSG 4
11#endif
12#define COOPK_SG 64
13
14#ifndef COOPK_ROWS
15#define COOPK_ROWS 4
16#endif
17#ifndef COOPK_COLS
18#define COOPK_COLS 4
19#endif
20
21#if COOPK_COLS == 4
22typedef float4 coopk_accv;
23typedef int4 coopk_dotv;
24#define COOPK_CONVF convert_float4
25#else
26typedef float2 coopk_accv;
27typedef int2 coopk_dotv;
28#define COOPK_CONVF convert_float2
29#endif
30
31// Pack four Q4_0 weights as signed bytes after subtracting 8.
32#define EXP40(l) \
33 ( (((uint)((int)(( (l) & 0x000Fu) - 8) & 0xFF))) \
34 | (((uint)((int)((((l) & 0x00F0u) >> 4) - 8) & 0xFF)) << 8) \
35 | (((uint)((int)((((l) & 0x0F00u) >> 8) - 8) & 0xFF)) << 16) \
36 | (((uint)((int)((((l) & 0xF000u) >> 12) - 8) & 0xFF)) << 24) )
37
38#if COOPK_ROWS == 4
39#define COOPK_DOT(ci, t) \
40 d0.s##ci = dot_acc_sat_4x8packed_ss_int(w0, A##ci.s##t, d0.s##ci); \
41 d1.s##ci = dot_acc_sat_4x8packed_ss_int(w1, A##ci.s##t, d1.s##ci); \
42 d2.s##ci = dot_acc_sat_4x8packed_ss_int(w2, A##ci.s##t, d2.s##ci); \
43 d3.s##ci = dot_acc_sat_4x8packed_ss_int(w3, A##ci.s##t, d3.s##ci);
44#else
45#define COOPK_DOT(ci, t) \
46 d0.s##ci = dot_acc_sat_4x8packed_ss_int(w0, A##ci.s##t, d0.s##ci); \
47 d1.s##ci = dot_acc_sat_4x8packed_ss_int(w1, A##ci.s##t, d1.s##ci);
48#endif
49
50#if COOPK_COLS == 4
51#define COOPK_DOTS_AT(t) COOPK_DOT(0,t) COOPK_DOT(1,t) COOPK_DOT(2,t) COOPK_DOT(3,t)
52#define COOPK_FOR_COLS(F) F(0) F(1) F(2) F(3)
53#else
54#define COOPK_DOTS_AT(t) COOPK_DOT(0,t) COOPK_DOT(1,t)
55#define COOPK_FOR_COLS(F) F(0) F(1)
56#endif
57
58#if COOPK_ROWS == 4
59#define COOPK_KSTEP(t) \
60 { \
61 ushort4 bl = vload4(0, src0_q + row0 + (ku0 + t) * m); \
62 const uint w0 = EXP40(bl.s0); \
63 const uint w1 = EXP40(bl.s1); \
64 const uint w2 = EXP40(bl.s2); \
65 const uint w3 = EXP40(bl.s3); \
66 COOPK_DOTS_AT(t) \
67 }
68#else
69#define COOPK_KSTEP(t) \
70 { \
71 ushort2 bl = vload2(0, src0_q + row0 + (ku0 + t) * m); \
72 const uint w0 = EXP40(bl.s0); \
73 const uint w1 = EXP40(bl.s1); \
74 COOPK_DOTS_AT(t) \
75 }
76#endif
77
78// Weights use the untiled Adreno noshuffle SoA layout, with the output row contiguous in each plane.
79// Q8_1 activation planes are K-major (K contiguous); no activation transpose is used.
80kernel void kernel_gemm_coopk_q4_0_q8_1_dp4a(
81 global const ushort * src0_q, // four consecutive K nibbles per ushort, [k/4][m]
82 global const half * src0_d, // block scales, [k/32][m]
83 global const uint * src1_qa, // four signed activation bytes per uint, [n][k/4]
84 global const half * src1_da, // activation scales, [n][k/32]
85 global float * dst,
86 ulong offsetd,
87 int m,
88 int n,
89 int k,
90 int n_no_padding
91) {
92 dst = (global float *)((global char *)dst + offsetd);
93
94 const int gx = get_global_id(0);
95 const int sg = get_local_id(1);
96 const int lane = get_local_id(0);
97
98 const int row0 = gx * COOPK_ROWS;
99 const int num_32blk = k / 32;
100 const int k_u = k >> 2;
101 const int k_b = k >> 5;
102
103 // The host pads activations to COOPK_COLS; stores mask columns beyond n_no_padding.
104 const int c0 = 0, c1 = 1;
105#if COOPK_COLS == 4
106 const int c2 = 2, c3 = 3;
107#endif
108
109 coopk_accv acc0 = (coopk_accv)(0.0f), acc1 = (coopk_accv)(0.0f);
110#if COOPK_ROWS == 4
111 coopk_accv acc2 = (coopk_accv)(0.0f), acc3 = (coopk_accv)(0.0f);
112#endif
113
114 for (int blk = sg; blk < num_32blk; blk += COOPK_NSG) {
115#if COOPK_ROWS == 4
116 half4 scale = vload4(0, src0_d + row0 + blk * m);
117#else
118 half2 scale = vload2(0, src0_d + row0 + blk * m);
119#endif
120
121 coopk_accv da;
122 da.s0 = (float)src1_da[c0*k_b + blk];
123 da.s1 = (float)src1_da[c1*k_b + blk];
124#if COOPK_COLS == 4
125 da.s2 = (float)src1_da[c2*k_b + blk];
126 da.s3 = (float)src1_da[c3*k_b + blk];
127#endif
128
129 // Q4_0 uses one weight scale per 32 K values.
130 coopk_dotv d0 = (coopk_dotv)(0), d1 = (coopk_dotv)(0);
131#if COOPK_ROWS == 4
132 coopk_dotv d2 = (coopk_dotv)(0), d3 = (coopk_dotv)(0);
133#endif
134 for (int half_idx = 0; half_idx < 2; ++half_idx) {
135 const int ku0 = (blk << 3) + half_idx * 4;
136
137 uint4 A0 = vload4(0, src1_qa + (uint)c0 * k_u + ku0);
138 uint4 A1 = vload4(0, src1_qa + (uint)c1 * k_u + ku0);
139#if COOPK_COLS == 4
140 uint4 A2 = vload4(0, src1_qa + (uint)c2 * k_u + ku0);
141 uint4 A3 = vload4(0, src1_qa + (uint)c3 * k_u + ku0);
142#endif
143 COOPK_KSTEP(0)
144 COOPK_KSTEP(1)
145 COOPK_KSTEP(2)
146 COOPK_KSTEP(3)
147 }
148
149 acc0 += (float)scale.s0 * da * COOPK_CONVF(d0);
150 acc1 += (float)scale.s1 * da * COOPK_CONVF(d1);
151#if COOPK_ROWS == 4
152 acc2 += (float)scale.s2 * da * COOPK_CONVF(d2);
153 acc3 += (float)scale.s3 * da * COOPK_CONVF(d3);
154#endif
155 }
156
157 // Reuse one local buffer for all rows; the first barrier protects the previous row's reads.
158 local coopk_accv reduceLM[COOPK_SG * (COOPK_NSG - 1)];
159 coopk_accv out0 = (coopk_accv)(0.0f), out1 = (coopk_accv)(0.0f);
160#if COOPK_ROWS == 4
161 coopk_accv out2 = (coopk_accv)(0.0f), out3 = (coopk_accv)(0.0f);
162#endif
163
164#define COOPK_REDUCE(accv, outv) \
165 barrier(CLK_LOCAL_MEM_FENCE); \
166 if (sg > 0) { reduceLM[(sg - 1) * COOPK_SG + lane] = (accv); } \
167 barrier(CLK_LOCAL_MEM_FENCE); \
168 if (sg == 0) { \
169 coopk_accv sum = (accv); \
170 for (int s = 0; s < COOPK_NSG - 1; s++) { \
171 sum += reduceLM[s * COOPK_SG + lane]; \
172 } \
173 (outv) = sum; \
174 }
175
176 COOPK_REDUCE(acc0, out0)
177 COOPK_REDUCE(acc1, out1)
178#if COOPK_ROWS == 4
179 COOPK_REDUCE(acc2, out2)
180 COOPK_REDUCE(acc3, out3)
181#endif
182
183#undef COOPK_REDUCE
184
185#if COOPK_ROWS == 4
186#define COOPK_STORE_COL(ci) \
187 if (idx < m*n_no_padding) { \
188 vstore4((float4)(out0.s##ci, out1.s##ci, out2.s##ci, out3.s##ci), 0, dst + idx); \
189 idx += m; \
190 }
191#else
192#define COOPK_STORE_COL(ci) \
193 if (idx < m*n_no_padding) { \
194 vstore2((float2)(out0.s##ci, out1.s##ci), 0, dst + idx); \
195 idx += m; \
196 }
197#endif
198
199 if (sg == 0) {
200 int idx = row0;
201 COOPK_FOR_COLS(COOPK_STORE_COL)
202 }
203
204#undef COOPK_STORE_COL
205}