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

186 lines · 5928 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_iq4_xs {
10 half d;
11 ushort scales_h;
12 uchar scales_l[QK_K/64];
13 uchar qs[QK_K/2];
14};
15
16#define LOAD_VEC_A 4
17#define LOAD_VEC_B 4
18
19#define BM 64
20#define BN 64
21#define BK 32
22#ifdef INTEL_GPU
23#define TM 8
24#define TN 8
25#else
26#define TM 4
27#define TN 8
28#endif
29
30kernel void kernel_mul_mm_iq4_xs_f32_l4_lm(
31 global uchar * src0_qs,
32 global uchar * src0_sl,
33 global ushort * src0_sh,
34 global half * src0_d,
35 ulong offset0,
36 global float4 * src1,
37 ulong offset1,
38 global float * dst,
39 ulong offsetd,
40
41 int ne00,
42 int ne01,
43 int ne02,
44 int ne11,
45 int ne12,
46
47 int stride_a,
48 int stride_b,
49 int stride_d,
50
51 int batch_stride_a,
52 int batch_stride_b,
53 int batch_stride_d,
54
55 int r2,
56 int r3
57) {
58 src1 = (global float4*)((global char*)src1 + offset1);
59 dst = (global float *)((global char*)dst + offsetd);
60
61 local float buf_a[BM * BK];
62 local float buf_b[BN * BK];
63
64 const int batch_idx = get_global_id(2);
65
66 const int i13 = batch_idx / ne12;
67 const int i12 = batch_idx % ne12;
68
69 const int i03 = i13 / r3;
70 const int i02 = i12 / r2;
71
72 const int batch_idx_a = i03 * ne02 + i02;
73
74 const int ir = get_group_id(0);
75 const int ic = get_group_id(1);
76
77 const int tid = get_local_id(0);
78 const int th_r = tid % (BM / TM);
79 const int th_c = tid / (BM / TM);
80
81 const int loadr_a = get_local_id(0) % (BK / LOAD_VEC_A);
82 const int loadc_a = get_local_id(0) / (BK / LOAD_VEC_A);
83 const int loadr_b = get_local_id(0) % (BK / LOAD_VEC_B);
84 const int loadc_b = get_local_id(0) / (BK / LOAD_VEC_B);
85
86 const int loadstride_a = get_local_size(0) * LOAD_VEC_A / BK;
87 const int loadstride_b = get_local_size(0) * LOAD_VEC_B / BK;
88
89 // pos_a counts elements, not blocks - one iq4_xs super block holds QK_K of them
90 int pos_a = batch_idx_a * batch_stride_a + ir * BM * stride_a;
91 int pos_b = (batch_idx * batch_stride_b + ic * BN * stride_b) / LOAD_VEC_B;
92
93 float sums[TM * TN];
94 float cache_a[TM];
95 float cache_b[TN];
96
97 for (int i = 0; i < TM * TN; i++) {
98 sums[i] = 0.0f;
99 }
100
101 for (int block = 0; block < ne00; block += BK) {
102 for (int l = 0; l < BM; l += loadstride_a) {
103 if (ir*BM + loadc_a + l < ne01) {
104 int idx = pos_a + (loadc_a + l) * stride_a + loadr_a * LOAD_VEC_A;
105 int ib = idx / QK_K;
106 ib += offset0/sizeof(struct block_iq4_xs);
107 int e = idx % QK_K;
108
109 int ib32 = e >> 5;
110 int j = e & 15;
111 int shift = (e & 16) >> 2;
112 int ls = ((src0_sl[4*ib + ib32/2] >> (4*(ib32 & 1))) & 15) | (((src0_sh[ib] >> (2*ib32)) & 3) << 4);
113 float db = (float)src0_d[ib] * (float)(ls - 32);
114 uchar4 q = vload4(0, src0_qs + 128*ib + 16*ib32 + j);
115 q = (q >> (uchar4)(shift)) & (uchar4)(15);
116
117 float4 v1;
118 v1.s0 = db * (float)iq4_xs_values[q.s0];
119 v1.s1 = db * (float)iq4_xs_values[q.s1];
120 v1.s2 = db * (float)iq4_xs_values[q.s2];
121 v1.s3 = db * (float)iq4_xs_values[q.s3];
122
123 buf_a[(loadr_a * LOAD_VEC_A + 0) * BM + loadc_a + l] = v1.s0;
124 buf_a[(loadr_a * LOAD_VEC_A + 1) * BM + loadc_a + l] = v1.s1;
125 buf_a[(loadr_a * LOAD_VEC_A + 2) * BM + loadc_a + l] = v1.s2;
126 buf_a[(loadr_a * LOAD_VEC_A + 3) * BM + loadc_a + l] = v1.s3;
127 } else {
128 buf_a[(loadr_a * LOAD_VEC_A + 0) * BM + loadc_a + l] = 0.0f;
129 buf_a[(loadr_a * LOAD_VEC_A + 1) * BM + loadc_a + l] = 0.0f;
130 buf_a[(loadr_a * LOAD_VEC_A + 2) * BM + loadc_a + l] = 0.0f;
131 buf_a[(loadr_a * LOAD_VEC_A + 3) * BM + loadc_a + l] = 0.0f;
132 }
133 }
134
135 for (int l = 0; l < BN; l += loadstride_b) {
136 if (ic*BN + loadc_b + l < ne11) {
137 int idx = pos_b + (loadc_b + l) * stride_b / LOAD_VEC_B + loadr_b;
138 buf_b[(loadr_b * LOAD_VEC_B + 0) * BN + loadc_b + l] = src1[idx].s0;
139 buf_b[(loadr_b * LOAD_VEC_B + 1) * BN + loadc_b + l] = src1[idx].s1;
140 buf_b[(loadr_b * LOAD_VEC_B + 2) * BN + loadc_b + l] = src1[idx].s2;
141 buf_b[(loadr_b * LOAD_VEC_B + 3) * BN + loadc_b + l] = src1[idx].s3;
142 } else {
143 buf_b[(loadr_b * LOAD_VEC_B + 0) * BN + loadc_b + l] = 0.0f;
144 buf_b[(loadr_b * LOAD_VEC_B + 1) * BN + loadc_b + l] = 0.0f;
145 buf_b[(loadr_b * LOAD_VEC_B + 2) * BN + loadc_b + l] = 0.0f;
146 buf_b[(loadr_b * LOAD_VEC_B + 3) * BN + loadc_b + l] = 0.0f;
147 }
148 }
149
150 barrier(CLK_LOCAL_MEM_FENCE);
151
152 pos_a += BK;
153 pos_b += BK / LOAD_VEC_B;
154
155 for (int i = 0; i < BK; i++) {
156 for (int j = 0; j < TM; j++) {
157 cache_a[j] = buf_a[(i) * BM + th_r * TM + j];
158 }
159
160 for (int j = 0; j < TN; j++) {
161 cache_b[j] = buf_b[(i) * BN + th_c * TN + j];
162 }
163
164 for (int cc = 0; cc < TN; cc++) {
165 for (int cr = 0; cr < TM; cr++) {
166 const int sums_idx = cc*TM + cr;
167 sums[sums_idx] = mad(cache_a[cr], cache_b[cc], sums[sums_idx]);
168 }
169 }
170 }
171 barrier(CLK_LOCAL_MEM_FENCE);
172 }
173
174 const int dr = ir * BM + th_r * TM;
175 const int dc = ic * BN + th_c * TN;
176
177 const ulong offsets = batch_idx * batch_stride_d;
178
179 for (int cc = 0; cc < TN; cc++) {
180 for (int cr = 0; cr < TM; cr++) {
181 if (dr + cr < ne01 && dc + cc < ne11) {
182 dst[offsets + (dc + cc) * stride_d + dr + cr] = sums[cc * TM + cr];
183 }
184 }
185 }
186}