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

143 lines · 5874 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// Weight layout, feature-major:
8// src0_q[row + (k/4)*m] ushort = 4 nibbles (K = 4*grp .. +3)
9// src0_d[row + (k/32)*m] half = per-32-block scale
10
11#define TILESIZE_N 32
12
13// IQ4_NL non-linear codebook as signed int8, packed 4 codes per uint.
14// divergent nibble lookups read a small __constant uint array + shift,
15// never a byte array because byte-indexed __constant loads serialize on Adreno and tank perf
16// idx 0-3: -127,-104,-83,-65 = 0x81,0x98,0xAD,0xBF
17// idx 4-7: -49,-35,-22,-10 = 0xCF,0xDD,0xEA,0xF6
18// idx 8-11: 1, 13, 25, 38 = 0x01,0x0D,0x19,0x26
19// idx 12-15: 53, 69, 89,113 = 0x35,0x45,0x59,0x71
20__constant uint kvalues_iq4nl_i8x4[4] = {
21 0xBFAD9881u, 0xF6EADDCFu, 0x26190D01u, 0x71594535u
22};
23
24// nibble (0..15) -> its codebook byte in the low 8 bits.
25inline uint iq4nl_code(uint n) {
26 return (kvalues_iq4nl_i8x4[n >> 2] >> ((n & 3u) * 8u)) & 0xFFu;
27}
28
29// 4 nibbles in low 16 bits of u -> 4 codebook int8, packed for dp4a.
30inline uint iq4nl_pack(ushort u) {
31 return iq4nl_code((uint)( u & 0xF))
32 | (iq4nl_code((uint)((u >> 4) & 0xF)) << 8)
33 | (iq4nl_code((uint)((u >> 8) & 0xF)) << 16)
34 | (iq4nl_code((uint)((u >> 12) & 0xF)) << 24);
35}
36
37inline int dot8_q8a(uint8 qw, __local const uint * a) {
38 int r = 0;
39 r = dot_acc_sat_4x8packed_ss_int(qw.s0, a[0], r);
40 r = dot_acc_sat_4x8packed_ss_int(qw.s1, a[1], r);
41 r = dot_acc_sat_4x8packed_ss_int(qw.s2, a[2], r);
42 r = dot_acc_sat_4x8packed_ss_int(qw.s3, a[3], r);
43 r = dot_acc_sat_4x8packed_ss_int(qw.s4, a[4], r);
44 r = dot_acc_sat_4x8packed_ss_int(qw.s5, a[5], r);
45 r = dot_acc_sat_4x8packed_ss_int(qw.s6, a[6], r);
46 r = dot_acc_sat_4x8packed_ss_int(qw.s7, a[7], r);
47 return r;
48}
49
50__attribute__((qcom_wave_pair_mode(1)))
51kernel void kernel_gemm_noshuffle_iq4_nl_q8_1_dp4a(
52 __global const ushort * src0_q, // IQ4_NL nibbles (4/ushort, feature-major)
53 __global const half * src0_d, // per-32-block scale, feature-major
54 __global const uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) [N, K]
55 __global const half * src1_da, // q8_1 per-block scale [N, K/32]
56 __global float * dst,
57 ulong offsetd,
58 int m, // output features (rows)
59 int n_no_padding, // tokens (cols)
60 int k // K (== ne00)
61) {
62 dst = (global float *)((global char *)dst + offsetd);
63
64 const uint lid = get_local_id(0); // 0..63 -> row within the M-tile
65 const uint block_id_m = get_global_id(1);
66 const uint block_id_n = get_global_id(2);
67
68 const uint row = block_id_m * 64 + lid;
69 const uint col_base = block_id_n * TILESIZE_N;
70 const bool row_valid = row < (uint)m;
71 const uint rrow = row_valid ? row : 0; // clamp OOB rows; their writes are masked
72
73 const uint k_u = (uint)k >> 2; // K in uint (int8x4) units
74 const uint k_b = (uint)k >> 5; // blocks-of-32 along K
75
76 __local uint sh_qa[TILESIZE_N][8];
77 __local half sh_d[TILESIZE_N];
78
79#define NGROUPS (TILESIZE_N / 4)
80 float4 acc[NGROUPS];
81 #pragma unroll
82 for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f);
83
84 for (uint step = 0; step < (uint)k; step += 32) {
85 const uint sub = step >> 5;
86
87 const float d_w = (float)src0_d[rrow + sub * (uint)m];
88
89 // 8 weight uints (32 codebook int8) for this row, this 32-block.
90 const uint qsbase = rrow + (step >> 2) * (uint)m;
91 uint8 qw;
92 qw.s0 = iq4nl_pack(src0_q[qsbase + 0 * m]);
93 qw.s1 = iq4nl_pack(src0_q[qsbase + 1 * m]);
94 qw.s2 = iq4nl_pack(src0_q[qsbase + 2 * m]);
95 qw.s3 = iq4nl_pack(src0_q[qsbase + 3 * m]);
96 qw.s4 = iq4nl_pack(src0_q[qsbase + 4 * m]);
97 qw.s5 = iq4nl_pack(src0_q[qsbase + 5 * m]);
98 qw.s6 = iq4nl_pack(src0_q[qsbase + 6 * m]);
99 qw.s7 = iq4nl_pack(src0_q[qsbase + 7 * m]);
100
101 // cooperatively stage the 32-token x 32-K int8 activations to lm
102 for (uint idx = lid; idx < TILESIZE_N * 8; idx += 64) {
103 const uint t = idx >> 3;
104 const uint u = idx & 7;
105 const uint c = col_base + t;
106 sh_qa[t][u] = (c < (uint)n_no_padding) ? src1_qa[c * k_u + (step >> 2) + u] : 0u;
107 }
108 if (lid < TILESIZE_N) {
109 const uint c = col_base + lid;
110 sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0;
111 }
112 barrier(CLK_LOCAL_MEM_FENCE);
113
114#define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3]))
115 #pragma unroll
116 for (int g = 0; g < NGROUPS; ++g) {
117 const int b = g * 4;
118 float4 rf;
119 rf.s0 = (float)dot8_q8a(qw, sh_qa[b+0]); rf.s1 = (float)dot8_q8a(qw, sh_qa[b+1]);
120 rf.s2 = (float)dot8_q8a(qw, sh_qa[b+2]); rf.s3 = (float)dot8_q8a(qw, sh_qa[b+3]);
121 acc[g] += d_w * LD4(sh_d, b) * rf;
122 }
123#undef LD4
124 barrier(CLK_LOCAL_MEM_FENCE);
125 }
126
127 if (!row_valid) {
128 return;
129 }
130
131 // dst is [token, feature] row-major (stride m): dst[col*m + row].
132 #pragma unroll
133 for (int g = 0; g < NGROUPS; ++g) {
134 const uint b = (uint)(g * 4);
135 const float4 a = acc[g];
136 const uint c0 = col_base + b;
137 if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0;
138 if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1;
139 if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2;
140 if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3;
141 }
142#undef NGROUPS
143}