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

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