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

146 lines · 5473 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2#pragma OPENCL EXTENSION cl_khr_subgroups : enable
3#ifdef cl_khr_integer_dot_product
4#pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable
5#endif
6
7#define QK_K 256
8
9#ifndef TILESIZE_N
10#define TILESIZE_N 32
11#endif
12
13inline uint q2_k_pack(uint pk) {
14 return ( (pk ) & 3u)
15 | (((pk >> 2) & 3u) << 8)
16 | (((pk >> 4) & 3u) << 16)
17 | (((pk >> 6) & 3u) << 24);
18}
19
20// Pass staged operands by value; local dp4a operands miscompile on X2.
21#define KQ_STAGE(p) vload4(0, (p))
22
23inline int dot4_q8a_v(uint4 qw, uint4 a) {
24 int r = 0;
25 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a.x, r);
26 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a.y, r);
27 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a.z, r);
28 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a.w, r);
29 return r;
30}
31
32__attribute__((qcom_wave_pair_mode(1)))
33kernel void kernel_gemm_noshuffle_q2_k_q8_1_dp4a(
34 __global const uchar * src0_qs,
35 __global const uchar * src0_sc,
36 __global const half * src0_dm, // half pairs: d, dmin
37 __global const uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) [N, K]
38 __global const half * src1_da, // q8_1 per-block scale [N, K/32]
39 __global float * dst,
40 ulong offsetd,
41 int m,
42 int n_no_padding,
43 int k
44) {
45 dst = (global float *)((global char *)dst + offsetd);
46
47 const uint lid = get_local_id(0);
48 const uint block_id_m = get_global_id(1);
49 const uint block_id_n = get_global_id(2);
50
51 const uint row = block_id_m * 64 + lid;
52 const uint col_base = block_id_n * TILESIZE_N;
53 const bool row_valid = row < (uint)m;
54 const uint rrow = row_valid ? row : 0;
55
56 const uint k_u = (uint)k >> 2;
57 const uint k_b = (uint)k >> 5;
58
59 __local uint4 sh_qa4[TILESIZE_N][2];
60 __local float sh_s [TILESIZE_N][2]; // per-16 activation sums, in qa units
61 __local half sh_d [TILESIZE_N];
62
63#define NGROUPS (TILESIZE_N / 4)
64 float4 acc[NGROUPS];
65 #pragma unroll
66 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
67
68 for (uint step = 0; step < (uint)k; step += 32) {
69 const uint sub = step >> 5;
70 const uint ib = sub >> 3;
71
72 const half2 dm = vload2(rrow + ib * (uint)m, src0_dm);
73 const float dv = (float)dm.s0;
74 const float dmv = (float)dm.s1;
75
76 const uint scb = rrow + (sub << 1) * (uint)m;
77 const uint sc0 = (uint)src0_sc[scb + 0u * (uint)m];
78 const uint sc1 = (uint)src0_sc[scb + 1u * (uint)m];
79 const float dl0 = dv * (float)( sc0 & 0xFu);
80 const float ml0 = dmv * (float)( sc0 >> 4);
81 const float dl1 = dv * (float)( sc1 & 0xFu);
82 const float ml1 = dmv * (float)( sc1 >> 4);
83
84 const uint qsb = rrow + (step >> 2) * (uint)m;
85 uint4 qlo, qhi;
86 qlo.s0 = q2_k_pack((uint)src0_qs[qsb + 0u * (uint)m]);
87 qlo.s1 = q2_k_pack((uint)src0_qs[qsb + 1u * (uint)m]);
88 qlo.s2 = q2_k_pack((uint)src0_qs[qsb + 2u * (uint)m]);
89 qlo.s3 = q2_k_pack((uint)src0_qs[qsb + 3u * (uint)m]);
90 qhi.s0 = q2_k_pack((uint)src0_qs[qsb + 4u * (uint)m]);
91 qhi.s1 = q2_k_pack((uint)src0_qs[qsb + 5u * (uint)m]);
92 qhi.s2 = q2_k_pack((uint)src0_qs[qsb + 6u * (uint)m]);
93 qhi.s3 = q2_k_pack((uint)src0_qs[qsb + 7u * (uint)m]);
94
95 for (uint idx = lid; idx < TILESIZE_N * 2u; idx += 64u) {
96 const uint t = idx >> 1;
97 const uint h = idx & 1u;
98 const uint c = col_base + t;
99 const bool ok = c < (uint)n_no_padding;
100 const uint4 w = ok ? KQ_STAGE(src1_qa + c * k_u + (step >> 2) + (h << 2))
101 : (uint4)(0u);
102 sh_qa4[t][h] = w;
103 int s = 0;
104 s = dot_acc_sat_4x8packed_ss_int(w.x, 0x01010101u, s);
105 s = dot_acc_sat_4x8packed_ss_int(w.y, 0x01010101u, s);
106 s = dot_acc_sat_4x8packed_ss_int(w.z, 0x01010101u, s);
107 s = dot_acc_sat_4x8packed_ss_int(w.w, 0x01010101u, s);
108 sh_s[t][h] = (float)s;
109 if (h == 0u) {
110 sh_d[t] = ok ? src1_da[c * k_b + sub] : (half)0;
111 }
112 }
113 barrier(CLK_LOCAL_MEM_FENCE);
114
115#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
116#define Q2_K_COL(b) (dl0 * (float)dot4_q8a_v(qlo, (uint4)(sh_qa4[b][0])) - ml0 * sh_s[b][0] + \
117 dl1 * (float)dot4_q8a_v(qhi, (uint4)(sh_qa4[b][1])) - ml1 * sh_s[b][1])
118 #pragma unroll
119 for (int g = 0; g < NGROUPS; ++g) {
120 const int b = g * 4;
121 float4 rf;
122 rf.s0 = Q2_K_COL(b+0); rf.s1 = Q2_K_COL(b+1);
123 rf.s2 = Q2_K_COL(b+2); rf.s3 = Q2_K_COL(b+3);
124 acc[g] += LD4(sh_d, b) * rf;
125 }
126#undef Q2_K_COL
127#undef LD4
128 barrier(CLK_LOCAL_MEM_FENCE);
129 }
130
131 if (!row_valid) {
132 return;
133 }
134
135 #pragma unroll
136 for (int g = 0; g < NGROUPS; ++g) {
137 const uint b = (uint)(g * 4);
138 const float4 a = acc[g];
139 const uint c0 = col_base + b;
140 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
141 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
142 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
143 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
144 }
145#undef NGROUPS
146}