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

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