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

191 lines · 6309 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_iq1_m {
10 uchar qs[QK_K/8];
11 uchar qh[QK_K/16];
12 uchar scales[QK_K/32];
13};
14
15#define LOAD_VEC_A 4
16#define LOAD_VEC_B 4
17
18#define BM 64
19#define BN 64
20#define BK 32
21#ifdef INTEL_GPU
22#define TM 8
23#define TN 8
24#else
25#define TM 4
26#define TN 8
27#endif
28
29kernel void kernel_mul_mm_iq1_m_f32_l4_lm(
30 global uchar * src0_qs,
31 global uchar * src0_qh,
32 global ushort * src0_sc,
33 ulong offset0,
34 global float4 * src1,
35 ulong offset1,
36 global float * dst,
37 ulong offsetd,
38
39 int ne00,
40 int ne01,
41 int ne02,
42 int ne11,
43 int ne12,
44
45 int stride_a,
46 int stride_b,
47 int stride_d,
48
49 int batch_stride_a,
50 int batch_stride_b,
51 int batch_stride_d,
52
53 int r2,
54 int r3
55) {
56 src1 = (global float4*)((global char*)src1 + offset1);
57 dst = (global float *)((global char*)dst + offsetd);
58
59 local float buf_a[BM * BK];
60 local float buf_b[BN * BK];
61
62 const int batch_idx = get_global_id(2);
63
64 const int i13 = batch_idx / ne12;
65 const int i12 = batch_idx % ne12;
66
67 const int i03 = i13 / r3;
68 const int i02 = i12 / r2;
69
70 const int batch_idx_a = i03 * ne02 + i02;
71
72 const int ir = get_group_id(0);
73 const int ic = get_group_id(1);
74
75 const int tid = get_local_id(0);
76 const int th_r = tid % (BM / TM);
77 const int th_c = tid / (BM / TM);
78
79 const int loadr_a = get_local_id(0) % (BK / LOAD_VEC_A);
80 const int loadc_a = get_local_id(0) / (BK / LOAD_VEC_A);
81 const int loadr_b = get_local_id(0) % (BK / LOAD_VEC_B);
82 const int loadc_b = get_local_id(0) / (BK / LOAD_VEC_B);
83
84 const int loadstride_a = get_local_size(0) * LOAD_VEC_A / BK;
85 const int loadstride_b = get_local_size(0) * LOAD_VEC_B / BK;
86
87 // pos_a counts elements, not blocks - one iq1_m super block holds QK_K of them
88 int pos_a = batch_idx_a * batch_stride_a + ir * BM * stride_a;
89 int pos_b = (batch_idx * batch_stride_b + ic * BN * stride_b) / LOAD_VEC_B;
90
91 float sums[TM * TN];
92 float cache_a[TM];
93 float cache_b[TN];
94
95 for (int i = 0; i < TM * TN; i++) {
96 sums[i] = 0.0f;
97 }
98
99 for (int block = 0; block < ne00; block += BK) {
100 for (int l = 0; l < BM; l += loadstride_a) {
101 if (ir*BM + loadc_a + l < ne01) {
102 int idx = pos_a + (loadc_a + l) * stride_a + loadr_a * LOAD_VEC_A;
103 int ib = idx / QK_K;
104 ib += offset0/sizeof(struct block_iq1_m);
105 int e = idx % QK_K;
106
107 int ib32 = e >> 5;
108 int rem = e & 31;
109 int lg = rem >> 3;
110 int j0 = rem & 7;
111
112 global ushort * sc = src0_sc + 4*ib;
113 ushort u16 = (ushort)((sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0)
114 | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000));
115 float dl = (float)as_half(u16) * (float)(2*((sc[ib32/2] >> (6*(ib32%2) + 3*(lg/2))) & 7) + 1);
116 uchar qhb = src0_qh[16*ib + 2*ib32 + lg/2];
117 float dlt = (qhb & (0x08 << (4*(lg%2)))) ? -1.125f : -0.875f;
118 uint gi = (uint)src0_qs[32*ib + 4*ib32 + lg] | ((((uint)qhb >> (4*(lg%2))) & 7) << 8);
119 uint g = iq1_s_grid[gi];
120 int sh = (j0 >> 2) * 4;
121
122 float4 v1;
123 v1.s0 = dl * ((float)((g >> ( 0 + sh)) & 0xF) + dlt);
124 v1.s1 = dl * ((float)((g >> ( 8 + sh)) & 0xF) + dlt);
125 v1.s2 = dl * ((float)((g >> (16 + sh)) & 0xF) + dlt);
126 v1.s3 = dl * ((float)((g >> (24 + sh)) & 0xF) + dlt);
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}