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

202 lines · 6509 bytes Raw Blame History
1#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2
3#ifdef cl_intel_subgroups
4#pragma OPENCL EXTENSION cl_intel_subgroups : enable
5#else
6#pragma OPENCL EXTENSION cl_khr_subgroups : enable
7#endif
8
9#ifdef cl_intel_required_subgroup_size
10#pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable
11#define INTEL_GPU 1
12#define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16)))
13#define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32)))
14#elif defined(cl_qcom_reqd_sub_group_size)
15#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
16#define ADRENO_GPU 1
17#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
18#define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full")))
19#endif
20
21#define QK4_NL 32
22
23typedef char int8_t;
24typedef uchar uint8_t;
25typedef short int16_t;
26typedef ushort uint16_t;
27typedef int int32_t;
28typedef uint uint32_t;
29
30constant float kvalues_iq4nl[16] = {
31 -127.f, -104.f, -83.f, -65.f, -49.f, -35.f, -22.f, -10.f,
32 1.f, 13.f, 25.f, 38.f, 53.f, 69.f, 89.f, 113.f
33};
34
35//------------------------------------------------------------------------------
36// block_iq4_nl
37//------------------------------------------------------------------------------
38struct block_iq4_nl
39{
40 half d;
41 uint8_t qs[QK4_NL / 2];
42};
43
44// Compute dot product between half a block of iq4_nl quants and activations.
45// x points to the quant bytes, dh points to the scale.
46// yl has 16 activation values: [0..7] for low nibbles, [8..15] for high nibbles.
47// il indicates offset into the quant bytes (0 or 8).
48inline float block_iq4_nl_dot_y_flat(
49 global uchar * x,
50 global half * dh,
51 private float * yl,
52 int il
53) {
54 float d = *dh;
55 global uchar * qs = x + il;
56 float acc = 0.f;
57 for (int i = 0; i < 8; ++i) {
58 acc += yl[i] * kvalues_iq4nl[qs[i] & 0x0F];
59 acc += yl[i+8] * kvalues_iq4nl[qs[i] >> 4];
60 }
61 return d * acc;
62}
63
64#undef N_DST
65#undef N_SIMDGROUP
66#undef N_SIMDWIDTH
67
68#ifdef INTEL_GPU
69#define N_DST 8 // each subgroup works on 8 rows
70#define N_SUBGROUP 1 // number of subgroups in a thread group
71#define N_SUBGROUP_SIZE 16 // assuming subgroup size is 16
72#elif defined (ADRENO_GPU)
73#define N_DST 8
74#define N_SUBGROUP 1
75#define N_SUBGROUP_SIZE 64
76#endif
77
78inline void mul_vec_q_n_f32_8x_flat(
79 global uchar * src0_q,
80 global half * src0_d,
81 global float * src1,
82 global float * dst,
83 int ne00,
84 int ne01,
85 int ne02,
86 int ne10,
87 int ne12,
88 int ne0,
89 int ne1,
90 int r2,
91 int r3
92) {
93 const ulong nb = ne00/QK4_NL;
94
95 int r0 = get_group_id(0);
96 int r1 = get_group_id(1);
97 int im = get_group_id(2);
98
99 int first_row = (r0 * N_SUBGROUP + get_sub_group_id()) * N_DST;
100
101 int i12 = im%ne12;
102 int i13 = im/ne12;
103
104 // The number of scales is the same as the number of blocks.
105 ulong offset0_d = first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02);
106 // Each block contains QK4_NL/2 uchars, hence offset for qs is as follows.
107 ulong offset0_q = (first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02)) * QK4_NL/2;
108
109 global uchar * x = (global uchar *) src0_q + offset0_q;
110 global half * d = (global half *) src0_d + offset0_d;
111 global float * y = (global float *) src1 + r1*ne10 + im*ne00*ne1;
112
113 float yl[16];
114 float8 sumf = 0.f;
115
116 int ix = get_sub_group_local_id()/2;
117 int il = 8*(get_sub_group_local_id()%2);
118
119 global float * yb = y + ix*QK4_NL + il;
120
121 for (int ib = ix; ib < nb; ib += N_SUBGROUP_SIZE/2) {
122 for (int i = 0; i < 8; ++i) {
123 yl[i] = yb[i];
124 yl[i+8] = yb[i+16];
125 }
126
127 sumf.s0 += block_iq4_nl_dot_y_flat(x + ib*QK4_NL/2 + 0*nb*QK4_NL/2, d + ib + 0*nb, yl, il);
128 sumf.s1 += block_iq4_nl_dot_y_flat(x + ib*QK4_NL/2 + 1*nb*QK4_NL/2, d + ib + 1*nb, yl, il);
129 sumf.s2 += block_iq4_nl_dot_y_flat(x + ib*QK4_NL/2 + 2*nb*QK4_NL/2, d + ib + 2*nb, yl, il);
130 sumf.s3 += block_iq4_nl_dot_y_flat(x + ib*QK4_NL/2 + 3*nb*QK4_NL/2, d + ib + 3*nb, yl, il);
131
132 sumf.s4 += block_iq4_nl_dot_y_flat(x + ib*QK4_NL/2 + 4*nb*QK4_NL/2, d + ib + 4*nb, yl, il);
133 sumf.s5 += block_iq4_nl_dot_y_flat(x + ib*QK4_NL/2 + 5*nb*QK4_NL/2, d + ib + 5*nb, yl, il);
134 sumf.s6 += block_iq4_nl_dot_y_flat(x + ib*QK4_NL/2 + 6*nb*QK4_NL/2, d + ib + 6*nb, yl, il);
135 sumf.s7 += block_iq4_nl_dot_y_flat(x + ib*QK4_NL/2 + 7*nb*QK4_NL/2, d + ib + 7*nb, yl, il);
136
137 yb += QK4_NL * (N_SUBGROUP_SIZE/2);
138 }
139
140 float8 tot = (float8)(
141 sub_group_reduce_add(sumf.s0), sub_group_reduce_add(sumf.s1),
142 sub_group_reduce_add(sumf.s2), sub_group_reduce_add(sumf.s3),
143 sub_group_reduce_add(sumf.s4), sub_group_reduce_add(sumf.s5),
144 sub_group_reduce_add(sumf.s6), sub_group_reduce_add(sumf.s7)
145 );
146
147 if (get_sub_group_local_id() == 0) {
148 if (first_row + 0 < ne01) {
149 dst[r1*ne0 + im*ne0*ne1 + first_row + 0] = tot.s0;
150 }
151 if (first_row + 1 < ne01) {
152 dst[r1*ne0 + im*ne0*ne1 + first_row + 1] = tot.s1;
153 }
154 if (first_row + 2 < ne01) {
155 dst[r1*ne0 + im*ne0*ne1 + first_row + 2] = tot.s2;
156 }
157 if (first_row + 3 < ne01) {
158 dst[r1*ne0 + im*ne0*ne1 + first_row + 3] = tot.s3;
159 }
160
161 if (first_row + 4 < ne01) {
162 dst[r1*ne0 + im*ne0*ne1 + first_row + 4] = tot.s4;
163 }
164 if (first_row + 5 < ne01) {
165 dst[r1*ne0 + im*ne0*ne1 + first_row + 5] = tot.s5;
166 }
167 if (first_row + 6 < ne01) {
168 dst[r1*ne0 + im*ne0*ne1 + first_row + 6] = tot.s6;
169 }
170 if (first_row + 7 < ne01) {
171 dst[r1*ne0 + im*ne0*ne1 + first_row + 7] = tot.s7;
172 }
173 }
174}
175
176#ifdef INTEL_GPU
177REQD_SUBGROUP_SIZE_16
178#elif defined (ADRENO_GPU)
179REQD_SUBGROUP_SIZE_64
180#endif
181kernel void kernel_mul_mv_iq4_nl_f32_flat(
182 global uchar * src0_q,
183 global half * src0_d,
184 global float * src1,
185 ulong offset1,
186 global float * dst,
187 ulong offsetd,
188 int ne00,
189 int ne01,
190 int ne02,
191 int ne10,
192 int ne12,
193 int ne0,
194 int ne1,
195 int r2,
196 int r3
197) {
198 src1 = (global float*)((global char*)src1 + offset1);
199 dst = (global float*)((global char*)dst + offsetd);
200
201 mul_vec_q_n_f32_8x_flat(src0_q, src0_d, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3);
202}