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

202 lines · 7010 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#ifdef cl_intel_required_subgroup_size
4#define INTEL_GPU 1
5#endif
6
7#define LOAD_VEC_A 4
8#define LOAD_VEC_B 4
9
10#define BM 64
11#define BN 64
12#define BK 32
13#ifdef INTEL_GPU
14// Intel Xe iGPU: 8x8 microtile (WG=64)
15#define TM 8
16#define TN 8
17#else
18#define TM 4
19#define TN 8
20#endif
21
22kernel void kernel_mul_mm_q5_k_f32_l4_lm(
23 global uchar4 * src0_q,
24 global uchar * src0_qh,
25 global uchar * src0_s,
26 global half * src0_d,
27 global half * src0_dm,
28 global float4 * src1,
29 ulong offset1,
30 global float * dst,
31 ulong offsetd,
32
33 int ne00,
34 int ne01,
35 int ne02,
36 int ne11,
37 int ne12,
38
39 int stride_a,
40 int stride_b,
41 int stride_d,
42
43 int batch_stride_a,
44 int batch_stride_b,
45 int batch_stride_d,
46
47 int r2,
48 int r3
49) {
50 src1 = (global float4*)((global char*)src1 + offset1);
51 dst = (global float *)((global char*)dst + offsetd);
52
53 local float buf_a[BM * BK];
54 local float buf_b[BN * BK];
55
56 const int batch_idx = get_global_id(2);
57
58 const int i13 = batch_idx / ne12;
59 const int i12 = batch_idx % ne12;
60
61 const int i03 = i13 / r3;
62 const int i02 = i12 / r2;
63
64 const int batch_idx_a = i03 * ne02 + i02;
65
66 const int ir = get_group_id(0);
67 const int ic = get_group_id(1);
68
69 const int tid = get_local_id(0);
70 const int th_r = tid % (BM / TM);
71 const int th_c = tid / (BM / TM);
72
73 const int loadr_a = get_local_id(0) % (BK / LOAD_VEC_A);
74 const int loadc_a = get_local_id(0) / (BK / LOAD_VEC_A);
75 const int loadr_b = get_local_id(0) % (BK / LOAD_VEC_B);
76 const int loadc_b = get_local_id(0) / (BK / LOAD_VEC_B);
77
78 const int loadstride_a = get_local_size(0) * LOAD_VEC_A / BK;
79 const int loadstride_b = get_local_size(0) * LOAD_VEC_B / BK;
80
81 int pos_a = (batch_idx_a * batch_stride_a + ir * BM * stride_a) / LOAD_VEC_A;
82 int pos_b = (batch_idx * batch_stride_b + ic * BN * stride_b) / LOAD_VEC_B;
83
84 float sums[TM * TN];
85 float cache_a[TM];
86 float cache_b[TN];
87
88 for (int i = 0; i < TM * TN; i++) {
89 sums[i] = 0.0f;
90 }
91
92 for (int block = 0; block < ne00; block += BK) {
93 for (int l = 0; l < BM; l += loadstride_a) {
94 if (ir*BM + loadc_a + l < ne01) {
95 int idx = pos_a + (loadc_a + l) * stride_a / LOAD_VEC_A + loadr_a;
96 int ib = idx / 64;
97 int iqs = (idx % 64) * 2;
98
99 int n = iqs / 32;
100 int b = (iqs % 32) / 16;
101 int is = 2 * n + b;
102 int qsi = n * 32 + (iqs % 16) * 2;
103
104 global uchar * scales = src0_s + ib * 12;
105
106 int scidx0 = (is < 4) ? is : (is + 4);
107 int scidx1 = (is < 4) ? is : (is - 4);
108 int scidxmask1 = (is < 4) ? 0x30 : 0xC0;
109 int scidxshift1 = (is < 4) ? 0 : 2;
110 int mbidx0 = is + 4;
111 int mbidx1 = (is < 4) ? is + 4 : is;
112 int mbidxmask0 = (is < 4) ? 0xF : 0xF0;
113 int mbidxshift0 = (is < 4) ? 0 : 4;
114 int mbidxmask1 = (is < 4) ? 0x30 : 0xC0;
115 int mbidxshift1 = (is < 4) ? 0 : 2;
116
117 uchar sc = (scales[scidx0] & 0xF) | ((scales[scidx1] & scidxmask1) >> scidxshift1);
118 uchar mbyte = ((scales[mbidx0] & mbidxmask0) >> mbidxshift0) | ((scales[mbidx1] & mbidxmask1) >> mbidxshift1);
119
120 float d = (float)src0_d[ib] * (float)sc;
121 float m = -(float)src0_dm[ib] * (float)mbyte;
122
123 int qh_base = (iqs % 16) * 2;
124 int bit_pos = 2*n + b;
125 uchar h0 = (src0_qh[ib*32 + qh_base + 0] >> bit_pos) & 1;
126 uchar h1 = (src0_qh[ib*32 + qh_base + 1] >> bit_pos) & 1;
127 uchar h2 = (src0_qh[ib*32 + qh_base + 2] >> bit_pos) & 1;
128 uchar h3 = (src0_qh[ib*32 + qh_base + 3] >> bit_pos) & 1;
129
130 global uchar4 * qs = src0_q + ib*32 + (qsi >> 2);
131 uchar4 q = *qs;
132 float4 v1 = (convert_float4((uchar4)(
133 ((q.s0 >> (b * 4))&0x0F) | (h0 << 4),
134 ((q.s1 >> (b * 4))&0x0F) | (h1 << 4),
135 ((q.s2 >> (b * 4))&0x0F) | (h2 << 4),
136 ((q.s3 >> (b * 4))&0x0F) | (h3 << 4)
137 )))*d + m;
138
139 buf_a[(loadr_a * LOAD_VEC_A + 0) * BM + loadc_a + l] = v1.s0;
140 buf_a[(loadr_a * LOAD_VEC_A + 1) * BM + loadc_a + l] = v1.s1;
141 buf_a[(loadr_a * LOAD_VEC_A + 2) * BM + loadc_a + l] = v1.s2;
142 buf_a[(loadr_a * LOAD_VEC_A + 3) * BM + loadc_a + l] = v1.s3;
143 } else {
144 buf_a[(loadr_a * LOAD_VEC_A + 0) * BM + loadc_a + l] = 0.0f;
145 buf_a[(loadr_a * LOAD_VEC_A + 1) * BM + loadc_a + l] = 0.0f;
146 buf_a[(loadr_a * LOAD_VEC_A + 2) * BM + loadc_a + l] = 0.0f;
147 buf_a[(loadr_a * LOAD_VEC_A + 3) * BM + loadc_a + l] = 0.0f;
148 }
149 }
150
151 for (int l = 0; l < BN; l += loadstride_b) {
152 if (ic*BN + loadc_b + l < ne11) {
153 int idx = pos_b + (loadc_b + l) * stride_b / LOAD_VEC_B + loadr_b;
154 buf_b[(loadr_b * LOAD_VEC_B + 0) * BN + loadc_b + l] = src1[idx].s0;
155 buf_b[(loadr_b * LOAD_VEC_B + 1) * BN + loadc_b + l] = src1[idx].s1;
156 buf_b[(loadr_b * LOAD_VEC_B + 2) * BN + loadc_b + l] = src1[idx].s2;
157 buf_b[(loadr_b * LOAD_VEC_B + 3) * BN + loadc_b + l] = src1[idx].s3;
158 } else {
159 buf_b[(loadr_b * LOAD_VEC_B + 0) * BN + loadc_b + l] = 0.0f;
160 buf_b[(loadr_b * LOAD_VEC_B + 1) * BN + loadc_b + l] = 0.0f;
161 buf_b[(loadr_b * LOAD_VEC_B + 2) * BN + loadc_b + l] = 0.0f;
162 buf_b[(loadr_b * LOAD_VEC_B + 3) * BN + loadc_b + l] = 0.0f;
163 }
164 }
165
166 barrier(CLK_LOCAL_MEM_FENCE);
167
168 pos_a += BK / LOAD_VEC_A;
169 pos_b += BK / LOAD_VEC_B;
170
171 for (int i = 0; i < BK; i++) {
172 for (int j = 0; j < TM; j++) {
173 cache_a[j] = buf_a[(i) * BM + th_r * TM + j];
174 }
175
176 for (int j = 0; j < TN; j++) {
177 cache_b[j] = buf_b[(i) * BN + th_c * TN + j];
178 }
179
180 for (int cc = 0; cc < TN; cc++) {
181 for (int cr = 0; cr < TM; cr++) {
182 const int sums_idx = cc*TM + cr;
183 sums[sums_idx] = mad(cache_a[cr], cache_b[cc], sums[sums_idx]);
184 }
185 }
186 }
187 barrier(CLK_LOCAL_MEM_FENCE);
188 }
189
190 const int dr = ir * BM + th_r * TM;
191 const int dc = ic * BN + th_c * TN;
192
193 const int offsets = batch_idx * batch_stride_d;
194
195 for (int cc = 0; cc < TN; cc++) {
196 for (int cr = 0; cr < TM; cr++) {
197 if (dr + cr < ne01 && dc + cc < ne11) {
198 dst[offsets + (dc + cc) * stride_d + dr + cr] = sums[cc * TM + cr];
199 }
200 }
201 }
202}