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

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