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

203 lines · 6571 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 QK_K 256
8
9struct block_q2_K {
10 uchar scales[QK_K/16];
11 uchar qs[QK_K/4];
12 half d;
13 half dmin;
14};
15
16#define LOAD_VEC_A 4
17#define LOAD_VEC_B 4
18
19#define BM 64
20#define BN 64
21#ifndef BK
22#define BK 32
23#endif
24#ifdef INTEL_GPU
25#define TM 8
26#else
27#define TM 4
28#endif
29#define TN 8
30
31#ifndef LM_HALF
32#define LM_HALF 0
33#endif
34#if LM_HALF
35typedef half lm_st;
36#define LM_LD4(p, i) convert_float4(vload4((i), (p)))
37#else
38typedef float lm_st;
39#define LM_LD4(p, i) vload4((i), (p))
40#endif
41
42kernel void kernel_mul_mm_q2_k_f32_l4_lm(
43 global uchar * src0_qs,
44 global uchar * src0_sc,
45 global half * src0_dm,
46 ulong offset0,
47 global float4 * src1,
48 ulong offset1,
49 global float * dst,
50 ulong offsetd,
51
52 int ne00,
53 int ne01,
54 int ne02,
55 int ne11,
56 int ne12,
57
58 int stride_a,
59 int stride_b,
60 int stride_d,
61
62 int batch_stride_a,
63 int batch_stride_b,
64 int batch_stride_d,
65
66 int r2,
67 int r3
68) {
69 src1 = (global float4*)((global char*)src1 + offset1);
70 dst = (global float *)((global char*)dst + offsetd);
71
72 local lm_st buf_a[BM * BK];
73 local lm_st buf_b[BN * BK];
74
75 const int batch_idx = get_global_id(2);
76
77 const int i13 = batch_idx / ne12;
78 const int i12 = batch_idx % ne12;
79
80 const int i03 = i13 / r3;
81 const int i02 = i12 / r2;
82
83 const int batch_idx_a = i03 * ne02 + i02;
84
85 const int ir = get_group_id(0);
86 const int ic = get_group_id(1);
87
88 const int tid = get_local_id(0);
89 const int th_r = tid % (BM / TM);
90 const int th_c = tid / (BM / TM);
91
92 const int loadr_a = get_local_id(0) % (BK / LOAD_VEC_A);
93 const int loadc_a = get_local_id(0) / (BK / LOAD_VEC_A);
94 const int loadr_b = get_local_id(0) % (BK / LOAD_VEC_B);
95 const int loadc_b = get_local_id(0) / (BK / LOAD_VEC_B);
96
97 const int loadstride_a = get_local_size(0) * LOAD_VEC_A / BK;
98 const int loadstride_b = get_local_size(0) * LOAD_VEC_B / BK;
99
100 // pos_a counts elements, not blocks - one q2_K super block holds QK_K of them
101 int pos_a = batch_idx_a * batch_stride_a + ir * BM * stride_a;
102 int pos_b = (batch_idx * batch_stride_b + ic * BN * stride_b) / LOAD_VEC_B;
103
104 float4 sums4[(TM/4) * TN];
105 float4 cache_a4[TM/4];
106
107 for (int i = 0; i < (TM/4) * TN; i++) {
108 sums4[i] = (float4)(0.0f);
109 }
110
111 for (int block = 0; block < ne00; block += BK) {
112 for (int l = 0; l < BM; l += loadstride_a) {
113 if (ir*BM + loadc_a + l < ne01) {
114 int idx = pos_a + (loadc_a + l) * stride_a + loadr_a * LOAD_VEC_A;
115 int ib = idx / QK_K;
116 ib += offset0/sizeof(struct block_q2_K);
117 int e = idx % QK_K;
118
119 // dequantize_row_q2_K walks 2 halves of 128, each as 4 shifts of
120 // 2 groups of 16. Recover that position from the element index.
121 int n = e >> 7;
122 int rem = e & 127;
123 int j = rem >> 5;
124 int s = (rem >> 4) & 1;
125 int l16 = rem & 15;
126 int isc = 8*n + 2*j + s;
127 int shift = 2*j;
128
129 uchar sc = src0_sc[16*ib + isc];
130 float d = (float)src0_dm[2*ib] * (float)(sc & 0xF);
131 float m = -(float)src0_dm[2*ib + 1] * (float)(sc >> 4);
132
133 uchar4 q = vload4(0, src0_qs + 64*ib + 32*n + 16*s + l16);
134 float4 v1 = convert_float4((uchar4)((q.s0 >> shift) & 3,
135 (q.s1 >> shift) & 3,
136 (q.s2 >> shift) & 3,
137 (q.s3 >> shift) & 3)) * 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;
169 pos_b += BK / LOAD_VEC_B;
170
171 for (int i = 0; i < BK; i++) {
172 for (int a = 0; a < TM/4; a++) {
173 cache_a4[a] = LM_LD4(buf_a + (i) * BM + th_r * TM, a);
174 }
175
176 for (int cc = 0; cc < TN; cc++) {
177 const float cache_b = buf_b[(i) * BN + th_c * TN + cc];
178 for (int a = 0; a < TM/4; a++) {
179 const int sums_idx = cc*(TM/4) + a;
180 sums4[sums_idx] = mad(cache_a4[a], (float4)cache_b, sums4[sums_idx]);
181 }
182 }
183 }
184 barrier(CLK_LOCAL_MEM_FENCE);
185 }
186
187 const int dr = ir * BM + th_r * TM;
188 const int dc = ic * BN + th_c * TN;
189
190 const ulong offsets = batch_idx * batch_stride_d;
191
192 for (int cc = 0; cc < TN; cc++) {
193 for (int a = 0; a < TM/4; a++) {
194 const float4 v = sums4[cc * (TM/4) + a];
195 const float vs[4] = { v.s0, v.s1, v.s2, v.s3 };
196 for (int k = 0; k < 4; k++) {
197 if (dr + 4*a + k < ne01 && dc + cc < ne11) {
198 dst[offsets + (dc + cc) * stride_d + dr + 4*a + k] = vs[k];
199 }
200 }
201 }
202 }
203}