/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp
Line | Count | Source |
1 | | #include "ops.h" |
2 | | |
3 | | #include "ggml-cpu.h" |
4 | | #include "ggml-impl.h" |
5 | | #include "binary-ops.h" |
6 | | #include "simd-gemm.h" |
7 | | #include "ggml.h" |
8 | | #include "unary-ops.h" |
9 | | #include "vec.h" |
10 | | |
11 | | #include <algorithm> |
12 | | #include <cfloat> |
13 | | #include <cmath> |
14 | | |
15 | | // ggml_compute_forward_dup |
16 | | |
17 | | static void ggml_compute_forward_dup_same_cont( |
18 | | const ggml_compute_params * params, |
19 | 0 | ggml_tensor * dst) { |
20 | |
|
21 | 0 | const ggml_tensor * src0 = dst->src[0]; |
22 | |
|
23 | 0 | GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
24 | 0 | GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
25 | 0 | GGML_ASSERT(src0->type == dst->type); |
26 | |
|
27 | 0 | const size_t nb0 = ggml_type_size(src0->type); |
28 | |
|
29 | 0 | const int ith = params->ith; // thread index |
30 | 0 | const int nth = params->nth; // number of threads |
31 | | |
32 | | // parallelize by blocks |
33 | 0 | const int nk = ggml_nelements(src0)/ggml_blck_size(src0->type); |
34 | 0 | const int dr = (nk + nth - 1) / nth; |
35 | 0 | const int k0 = dr * ith; |
36 | 0 | const int k1 = MIN(k0 + dr, nk); |
37 | |
|
38 | 0 | if (k0 < k1) { |
39 | 0 | memcpy( |
40 | 0 | ((char *) dst->data + k0*nb0), |
41 | 0 | ((char *) src0->data + k0*nb0), |
42 | 0 | (k1 - k0) * nb0); |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | | template<typename src_t, typename dst_t> |
47 | | static void ggml_compute_forward_dup_flt( |
48 | | const ggml_compute_params * params, |
49 | 0 | ggml_tensor * dst) { |
50 | |
|
51 | 0 | const ggml_tensor * src0 = dst->src[0]; |
52 | |
|
53 | 0 | GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
54 | 0 | GGML_ASSERT(!ggml_is_quantized(src0->type) && !ggml_is_quantized(dst->type)); |
55 | |
|
56 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
57 | |
|
58 | 0 | const int ith = params->ith; // thread index |
59 | 0 | const int nth = params->nth; // number of threads |
60 | | |
61 | | // parallelize by rows |
62 | 0 | const int nr = ne01; |
63 | | // number of rows per thread |
64 | 0 | const int dr = (nr + nth - 1) / nth; |
65 | | // row range for this thread |
66 | 0 | const int ir0 = dr * ith; |
67 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
68 | | |
69 | | // case: type & row size equal |
70 | 0 | if (src0->type == dst->type && |
71 | 0 | ne00 == ne0 && |
72 | 0 | nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) { |
73 | | // copy by rows |
74 | 0 | const size_t rs = ne00*nb00; |
75 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
76 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
77 | 0 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
78 | 0 | memcpy( |
79 | 0 | ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3), |
80 | 0 | ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03), |
81 | 0 | rs); |
82 | 0 | } |
83 | 0 | } |
84 | 0 | } |
85 | 0 | return; |
86 | 0 | } |
87 | | |
88 | | // case: dst tensor is contiguous |
89 | 0 | if (ggml_is_contiguous(dst)) { |
90 | 0 | if (nb00 == sizeof(src_t)) { |
91 | 0 | if constexpr (std::is_same_v<dst_t, src_t>) { |
92 | | // same type |
93 | 0 | size_t id = 0; |
94 | 0 | const size_t rs = ne00 * nb00; |
95 | 0 | char * dst_ptr = (char *) dst->data; |
96 | |
|
97 | 0 | for (int i03 = 0; i03 < ne03; i03++) { |
98 | 0 | for (int i02 = 0; i02 < ne02; i02++) { |
99 | 0 | id += rs * ir0; |
100 | 0 | for (int i01 = ir0; i01 < ir1; i01++) { |
101 | 0 | const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03; |
102 | 0 | memcpy(dst_ptr + id, src0_ptr, rs); |
103 | 0 | id += rs; |
104 | 0 | } |
105 | 0 | id += rs * (ne01 - ir1); |
106 | 0 | } |
107 | 0 | } |
108 | 0 | } else { |
109 | | // casting between non-quantized types |
110 | 0 | size_t id = 0; |
111 | 0 | dst_t * dst_ptr = (dst_t *) dst->data; |
112 | |
|
113 | 0 | for (int i03 = 0; i03 < ne03; i03++) { |
114 | 0 | for (int i02 = 0; i02 < ne02; i02++) { |
115 | 0 | id += ne00 * ir0; |
116 | 0 | for (int i01 = ir0; i01 < ir1; i01++) { |
117 | 0 | const src_t * src0_ptr = (src_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
118 | 0 | for (int i00 = 0; i00 < ne00; i00++) { |
119 | 0 | float tmp = type_conversion_table<src_t>::to_f32(src0_ptr[i00]); |
120 | 0 | dst_ptr[id] = type_conversion_table<dst_t>::from_f32(tmp); |
121 | 0 | id++; |
122 | 0 | } |
123 | 0 | } |
124 | 0 | id += ne00 * (ne01 - ir1); |
125 | 0 | } |
126 | 0 | } |
127 | 0 | } |
128 | 0 | } else { |
129 | | //printf("%s: this is not optimal - fix me\n", __func__); |
130 | |
|
131 | 0 | size_t id = 0; |
132 | 0 | dst_t * dst_ptr = (dst_t *) dst->data; |
133 | |
|
134 | 0 | for (int i03 = 0; i03 < ne03; i03++) { |
135 | 0 | for (int i02 = 0; i02 < ne02; i02++) { |
136 | 0 | id += ne00 * ir0; |
137 | 0 | for (int i01 = ir0; i01 < ir1; i01++) { |
138 | 0 | for (int i00 = 0; i00 < ne00; i00++) { |
139 | 0 | const src_t * src0_ptr = (src_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
140 | |
|
141 | 0 | float tmp = type_conversion_table<src_t>::to_f32(*src0_ptr); |
142 | 0 | dst_ptr[id] = type_conversion_table<dst_t>::from_f32(tmp); |
143 | 0 | id++; |
144 | 0 | } |
145 | 0 | } |
146 | 0 | id += ne00 * (ne01 - ir1); |
147 | 0 | } |
148 | 0 | } |
149 | 0 | } |
150 | 0 | return; |
151 | 0 | } |
152 | | |
153 | | // dst counters |
154 | 0 | int64_t i10 = 0; |
155 | 0 | int64_t i11 = 0; |
156 | 0 | int64_t i12 = 0; |
157 | 0 | int64_t i13 = 0; |
158 | |
|
159 | 0 | if constexpr (std::is_same_v<dst_t, src_t>) { |
160 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
161 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
162 | 0 | i10 += ne00 * ir0; |
163 | 0 | while (i10 >= ne0) { |
164 | 0 | i10 -= ne0; |
165 | 0 | if (++i11 == ne1) { |
166 | 0 | i11 = 0; |
167 | 0 | if (++i12 == ne2) { |
168 | 0 | i12 = 0; |
169 | 0 | if (++i13 == ne3) { |
170 | 0 | i13 = 0; |
171 | 0 | } |
172 | 0 | } |
173 | 0 | } |
174 | 0 | } |
175 | 0 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
176 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
177 | 0 | const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
178 | 0 | char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3); |
179 | |
|
180 | 0 | memcpy(dst_ptr, src0_ptr, sizeof(dst_t)); |
181 | |
|
182 | 0 | if (++i10 == ne00) { |
183 | 0 | i10 = 0; |
184 | 0 | if (++i11 == ne01) { |
185 | 0 | i11 = 0; |
186 | 0 | if (++i12 == ne02) { |
187 | 0 | i12 = 0; |
188 | 0 | if (++i13 == ne03) { |
189 | 0 | i13 = 0; |
190 | 0 | } |
191 | 0 | } |
192 | 0 | } |
193 | 0 | } |
194 | 0 | } |
195 | 0 | } |
196 | 0 | i10 += ne00 * (ne01 - ir1); |
197 | 0 | while (i10 >= ne0) { |
198 | 0 | i10 -= ne0; |
199 | 0 | if (++i11 == ne1) { |
200 | 0 | i11 = 0; |
201 | 0 | if (++i12 == ne2) { |
202 | 0 | i12 = 0; |
203 | 0 | if (++i13 == ne3) { |
204 | 0 | i13 = 0; |
205 | 0 | } |
206 | 0 | } |
207 | 0 | } |
208 | 0 | } |
209 | 0 | } |
210 | 0 | } |
211 | |
|
212 | 0 | } else { |
213 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
214 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
215 | 0 | i10 += ne00 * ir0; |
216 | 0 | while (i10 >= ne0) { |
217 | 0 | i10 -= ne0; |
218 | 0 | if (++i11 == ne1) { |
219 | 0 | i11 = 0; |
220 | 0 | if (++i12 == ne2) { |
221 | 0 | i12 = 0; |
222 | 0 | if (++i13 == ne3) { |
223 | 0 | i13 = 0; |
224 | 0 | } |
225 | 0 | } |
226 | 0 | } |
227 | 0 | } |
228 | 0 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
229 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
230 | 0 | const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
231 | 0 | char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3); |
232 | |
|
233 | 0 | float tmp = type_conversion_table<src_t>::to_f32(*(const src_t *) src0_ptr); |
234 | 0 | *(dst_t *) dst_ptr = type_conversion_table<dst_t>::from_f32(tmp); |
235 | |
|
236 | 0 | if (++i10 == ne0) { |
237 | 0 | i10 = 0; |
238 | 0 | if (++i11 == ne1) { |
239 | 0 | i11 = 0; |
240 | 0 | if (++i12 == ne2) { |
241 | 0 | i12 = 0; |
242 | 0 | if (++i13 == ne3) { |
243 | 0 | i13 = 0; |
244 | 0 | } |
245 | 0 | } |
246 | 0 | } |
247 | 0 | } |
248 | 0 | } |
249 | 0 | } |
250 | 0 | i10 += ne00 * (ne01 - ir1); |
251 | 0 | while (i10 >= ne0) { |
252 | 0 | i10 -= ne0; |
253 | 0 | if (++i11 == ne1) { |
254 | 0 | i11 = 0; |
255 | 0 | if (++i12 == ne2) { |
256 | 0 | i12 = 0; |
257 | 0 | if (++i13 == ne3) { |
258 | 0 | i13 = 0; |
259 | 0 | } |
260 | 0 | } |
261 | 0 | } |
262 | 0 | } |
263 | 0 | } |
264 | 0 | } |
265 | 0 | } |
266 | 0 | } Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<unsigned short, unsigned short>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<unsigned short, ggml_bf16_t>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<unsigned short, float>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<ggml_bf16_t, unsigned short>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<ggml_bf16_t, ggml_bf16_t>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<ggml_bf16_t, float>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<float, unsigned short>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<float, ggml_bf16_t>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<float, float>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<float, int>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_flt<int, float>(ggml_compute_params const*, ggml_tensor*) |
267 | | |
268 | | |
269 | | template<typename src_t> |
270 | | static void ggml_compute_forward_dup_to_q( |
271 | | const ggml_compute_params * params, |
272 | 0 | ggml_tensor * dst) { |
273 | |
|
274 | 0 | const ggml_tensor * src0 = dst->src[0]; |
275 | |
|
276 | 0 | GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
277 | 0 | GGML_ASSERT(!ggml_is_quantized(src0->type)); |
278 | |
|
279 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
280 | |
|
281 | 0 | const int ith = params->ith; // thread index |
282 | 0 | const int nth = params->nth; // number of threads |
283 | | |
284 | | // parallelize by rows |
285 | 0 | const int nr = ne01; |
286 | | // number of rows per thread |
287 | 0 | const int dr = (nr + nth - 1) / nth; |
288 | | // row range for this thread |
289 | 0 | const int ir0 = dr * ith; |
290 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
291 | |
|
292 | 0 | if (ggml_is_contiguous(dst) && |
293 | 0 | nb00 == sizeof(src_t) && |
294 | 0 | ggml_get_type_traits_cpu(dst->type)->from_float) { |
295 | | // casting non-quantized types --> intermediate f32 --> quantized |
296 | 0 | ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float; |
297 | 0 | float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
298 | |
|
299 | 0 | size_t id = 0; |
300 | 0 | size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type)); |
301 | 0 | char * dst_ptr = (char *) dst->data; |
302 | |
|
303 | 0 | for (int i03 = 0; i03 < ne03; i03++) { |
304 | 0 | for (int i02 = 0; i02 < ne02; i02++) { |
305 | 0 | id += rs * ir0; |
306 | 0 | for (int i01 = ir0; i01 < ir1; i01++) { |
307 | 0 | const src_t * src0_ptr = (src_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
308 | |
|
309 | 0 | for (int i00 = 0; i00 < ne00; i00++) { |
310 | 0 | src0_f32[i00] = type_conversion_table<src_t>::to_f32(src0_ptr[i00]); |
311 | 0 | } |
312 | |
|
313 | 0 | quantize_row_q(src0_f32, dst_ptr + id, ne00); |
314 | 0 | id += rs; |
315 | 0 | } |
316 | 0 | id += rs * (ne01 - ir1); |
317 | 0 | } |
318 | 0 | } |
319 | 0 | } else { |
320 | | // printf("%s %s\n", ggml_type_name(src0->type), ggml_type_name(dst->type)); |
321 | 0 | GGML_ABORT("not implemented"); |
322 | 0 | } |
323 | 0 | } Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_to_q<unsigned short>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_to_q<ggml_bf16_t>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_dup_to_q<float>(ggml_compute_params const*, ggml_tensor*) |
324 | | |
325 | | // A simplified version of ggml_compute_forward_dup that doesn't do float upcasting, and just plain old memcpy. |
326 | | static void ggml_compute_forward_dup_bytes( |
327 | | const ggml_compute_params * params, |
328 | 0 | ggml_tensor * dst) { |
329 | 0 | const ggml_tensor * src0 = dst->src[0]; |
330 | |
|
331 | 0 | GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
332 | 0 | GGML_ASSERT(src0->type == dst->type); |
333 | |
|
334 | 0 | GGML_TENSOR_UNARY_OP_LOCALS; |
335 | |
|
336 | 0 | if (ggml_is_contiguous(src0) && ggml_is_contiguous(dst)) { |
337 | 0 | ggml_compute_forward_dup_same_cont(params, dst); |
338 | 0 | return; |
339 | 0 | } |
340 | | |
341 | 0 | const size_t type_size = ggml_type_size(src0->type); |
342 | |
|
343 | 0 | const int ith = params->ith; // thread index |
344 | 0 | const int nth = params->nth; // number of threads |
345 | | |
346 | | // parallelize by rows |
347 | 0 | const int nr = ne01; |
348 | | // number of rows per thread |
349 | 0 | const int dr = (nr + nth - 1) / nth; |
350 | | // row range for this thread |
351 | 0 | const int ir0 = dr * ith; |
352 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
353 | |
|
354 | 0 | if (src0->type == dst->type && |
355 | 0 | ggml_are_same_shape(src0, dst) && |
356 | 0 | nb00 == type_size && nb0 == type_size) { |
357 | | // copy by rows |
358 | 0 | const size_t rs = ggml_row_size(src0->type, ne00); |
359 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
360 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
361 | 0 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
362 | 0 | memcpy( |
363 | 0 | ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3), |
364 | 0 | ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03), |
365 | 0 | rs); |
366 | 0 | } |
367 | 0 | } |
368 | 0 | } |
369 | 0 | return; |
370 | 0 | } |
371 | | |
372 | 0 | if (ggml_is_contiguous(dst)) { |
373 | 0 | size_t id = 0; |
374 | 0 | char * dst_ptr = (char *) dst->data; |
375 | 0 | const size_t rs = ne00 * type_size; |
376 | |
|
377 | 0 | if (nb00 == type_size) { |
378 | | // src0 is contiguous on first dimension, copy by rows |
379 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
380 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
381 | 0 | id += rs * ir0; |
382 | 0 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
383 | 0 | const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03; |
384 | 0 | memcpy(dst_ptr + id, src0_ptr, rs); |
385 | 0 | id += rs; |
386 | 0 | } |
387 | 0 | id += rs * (ne01 - ir1); |
388 | 0 | } |
389 | 0 | } |
390 | 0 | } else { |
391 | | //printf("%s: this is not optimal - fix me\n", __func__); |
392 | |
|
393 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
394 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
395 | 0 | id += rs * ir0; |
396 | 0 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
397 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
398 | 0 | const char * src0_ptr = (char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03; |
399 | 0 | memcpy(dst_ptr + id, src0_ptr, type_size); |
400 | |
|
401 | 0 | id += type_size; |
402 | 0 | } |
403 | 0 | } |
404 | 0 | id += rs * (ne01 - ir1); |
405 | 0 | } |
406 | 0 | } |
407 | 0 | } |
408 | |
|
409 | 0 | return; |
410 | 0 | } |
411 | | |
412 | | // dst counters |
413 | 0 | int64_t k10 = 0; |
414 | 0 | int64_t i11 = 0; |
415 | 0 | int64_t i12 = 0; |
416 | 0 | int64_t i13 = 0; |
417 | | |
418 | | // number of blocks in a row |
419 | 0 | const int64_t nk00 = ne00 / ggml_blck_size(src0->type); |
420 | 0 | const int64_t nk0 = ne0 / ggml_blck_size(dst->type); |
421 | |
|
422 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
423 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
424 | 0 | k10 += nk00 * ir0; |
425 | 0 | while (k10 >= nk0) { |
426 | 0 | k10 -= nk0; |
427 | 0 | if (++i11 == ne1) { |
428 | 0 | i11 = 0; |
429 | 0 | if (++i12 == ne2) { |
430 | 0 | i12 = 0; |
431 | 0 | if (++i13 == ne3) { |
432 | 0 | i13 = 0; |
433 | 0 | } |
434 | 0 | } |
435 | 0 | } |
436 | 0 | } |
437 | 0 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
438 | 0 | for (int64_t k00 = 0; k00 < nk00; k00++) { |
439 | 0 | const char * src0_ptr = ((char *) src0->data + k00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
440 | 0 | char * dst_ptr = ((char *) dst->data + k10*nb0 + i11*nb1 + i12*nb2 + i13*nb3); |
441 | |
|
442 | 0 | memcpy(dst_ptr, src0_ptr, type_size); |
443 | |
|
444 | 0 | if (++k10 == nk0) { |
445 | 0 | k10 = 0; |
446 | 0 | if (++i11 == ne1) { |
447 | 0 | i11 = 0; |
448 | 0 | if (++i12 == ne2) { |
449 | 0 | i12 = 0; |
450 | 0 | if (++i13 == ne3) { |
451 | 0 | i13 = 0; |
452 | 0 | } |
453 | 0 | } |
454 | 0 | } |
455 | 0 | } |
456 | 0 | } |
457 | 0 | } |
458 | 0 | k10 += nk00 * (ne01 - ir1); |
459 | 0 | while (k10 >= nk0) { |
460 | 0 | k10 -= nk0; |
461 | 0 | if (++i11 == ne1) { |
462 | 0 | i11 = 0; |
463 | 0 | if (++i12 == ne2) { |
464 | 0 | i12 = 0; |
465 | 0 | if (++i13 == ne3) { |
466 | 0 | i13 = 0; |
467 | 0 | } |
468 | 0 | } |
469 | 0 | } |
470 | 0 | } |
471 | 0 | } |
472 | 0 | } |
473 | 0 | } |
474 | | |
475 | | static void ggml_compute_forward_dup_from_q( |
476 | | const ggml_compute_params * params, |
477 | 0 | ggml_tensor * dst) { |
478 | |
|
479 | 0 | const ggml_tensor * src0 = dst->src[0]; |
480 | 0 | const ggml_tensor * src1 = dst->src[1]; |
481 | |
|
482 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
483 | |
|
484 | 0 | const ggml_type type = src0->type; |
485 | 0 | ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
486 | |
|
487 | 0 | size_t qk = ggml_blck_size(type); |
488 | 0 | const int64_t nr = ggml_nelements(src1) / qk; |
489 | | |
490 | | // destination must be contiguous in the first dimension |
491 | 0 | GGML_ASSERT(nb10 == ggml_type_size(dst->type)); |
492 | | // must either have first dimension large enough to hold a row, or fully contiguous |
493 | 0 | GGML_ASSERT((ne10 % qk) == 0 || ggml_is_contiguous(dst)); |
494 | |
|
495 | 0 | const int ith = params->ith; |
496 | 0 | const int nth = params->nth; |
497 | |
|
498 | 0 | const int dr = (nr + nth - 1)/nth; |
499 | | |
500 | | // row range for this thread |
501 | 0 | const int ir0 = dr*ith; |
502 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
503 | |
|
504 | 0 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
505 | |
|
506 | 0 | uint32_t i = ir * qk; |
507 | |
|
508 | 0 | const int64_t i03 = i/(ne00 * ne01 * ne02); |
509 | 0 | const int64_t i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01); |
510 | 0 | const int64_t i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00; |
511 | 0 | const int64_t i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00; |
512 | 0 | const int64_t x_offset = (i00/qk)*nb00 + i01*nb01 + i02*nb02 + i03 * nb03; |
513 | |
|
514 | 0 | const int64_t i13 = i/(ne10 * ne11 * ne12); |
515 | 0 | const int64_t i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11); |
516 | 0 | const int64_t i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10; |
517 | 0 | const int64_t i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10; |
518 | 0 | const int64_t dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13*nb13; |
519 | |
|
520 | 0 | dequantize_row_q( |
521 | 0 | (const void *) ((char *) src0->data + x_offset), |
522 | 0 | (float *) ((char *) dst->data + dst_offset), qk); |
523 | 0 | } |
524 | 0 | } |
525 | | |
526 | | void ggml_compute_forward_dup( |
527 | | const ggml_compute_params * params, |
528 | 0 | ggml_tensor * dst) { |
529 | |
|
530 | 0 | const ggml_tensor * src0 = dst->src[0]; |
531 | |
|
532 | 0 | if (src0->type == dst->type) { |
533 | 0 | ggml_compute_forward_dup_bytes(params, dst); |
534 | 0 | return; |
535 | 0 | } |
536 | | |
537 | 0 | switch (src0->type) { |
538 | 0 | case GGML_TYPE_F16: |
539 | 0 | { |
540 | 0 | /**/ if (dst->type == GGML_TYPE_F16) ggml_compute_forward_dup_flt<ggml_fp16_t, ggml_fp16_t>(params, dst); |
541 | 0 | else if (dst->type == GGML_TYPE_BF16) ggml_compute_forward_dup_flt<ggml_fp16_t, ggml_bf16_t>(params, dst); |
542 | 0 | else if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<ggml_fp16_t, float >(params, dst); |
543 | 0 | else ggml_compute_forward_dup_to_q<ggml_fp16_t>(params, dst); |
544 | 0 | } break; |
545 | 0 | case GGML_TYPE_BF16: |
546 | 0 | { |
547 | 0 | /**/ if (dst->type == GGML_TYPE_F16) ggml_compute_forward_dup_flt<ggml_bf16_t, ggml_fp16_t>(params, dst); |
548 | 0 | else if (dst->type == GGML_TYPE_BF16) ggml_compute_forward_dup_flt<ggml_bf16_t, ggml_bf16_t>(params, dst); |
549 | 0 | else if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<ggml_bf16_t, float >(params, dst); |
550 | 0 | else ggml_compute_forward_dup_to_q<ggml_bf16_t>(params, dst); |
551 | 0 | } break; |
552 | 0 | case GGML_TYPE_F32: |
553 | 0 | { |
554 | 0 | /**/ if (dst->type == GGML_TYPE_F16) ggml_compute_forward_dup_flt<float, ggml_fp16_t>(params, dst); |
555 | 0 | else if (dst->type == GGML_TYPE_BF16) ggml_compute_forward_dup_flt<float, ggml_bf16_t>(params, dst); |
556 | 0 | else if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<float, float >(params, dst); |
557 | 0 | else if (dst->type == GGML_TYPE_I32) ggml_compute_forward_dup_flt<float, int32_t >(params, dst); |
558 | 0 | else ggml_compute_forward_dup_to_q<float>(params, dst); |
559 | 0 | } break; |
560 | 0 | case GGML_TYPE_I32: |
561 | 0 | { |
562 | 0 | if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<int32_t, float>(params, dst); |
563 | 0 | else GGML_ABORT("not implemented"); |
564 | 0 | } break; |
565 | 0 | default: |
566 | 0 | { |
567 | 0 | if (ggml_is_quantized(src0->type) && dst->type == GGML_TYPE_F32) { |
568 | 0 | ggml_compute_forward_dup_from_q(params, dst); |
569 | 0 | break; |
570 | 0 | } |
571 | 0 | GGML_ABORT("fatal error"); |
572 | 0 | } |
573 | 0 | } |
574 | 0 | } |
575 | | |
576 | | // ggml_compute_forward_add |
577 | | |
578 | | static void ggml_compute_forward_add_q_f32( |
579 | | const ggml_compute_params * params, |
580 | 0 | ggml_tensor * dst) { |
581 | |
|
582 | 0 | const ggml_tensor * src0 = dst->src[0]; |
583 | 0 | const ggml_tensor * src1 = dst->src[1]; |
584 | |
|
585 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst)); |
586 | |
|
587 | 0 | const int nr = ggml_nrows(src0); |
588 | |
|
589 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
590 | |
|
591 | 0 | const int ith = params->ith; |
592 | 0 | const int nth = params->nth; |
593 | |
|
594 | 0 | const ggml_type type = src0->type; |
595 | 0 | const ggml_type dtype = dst->type; |
596 | 0 | ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
597 | 0 | ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dtype)->from_float; |
598 | | |
599 | | // we don't support permuted src0 or src1 |
600 | 0 | GGML_ASSERT(nb00 == ggml_type_size(type)); |
601 | 0 | GGML_ASSERT(nb10 == sizeof(float)); |
602 | | |
603 | | // dst cannot be transposed or permuted |
604 | 0 | GGML_ASSERT(nb0 <= nb1); |
605 | 0 | GGML_ASSERT(nb1 <= nb2); |
606 | 0 | GGML_ASSERT(nb2 <= nb3); |
607 | |
|
608 | 0 | GGML_ASSERT(ggml_is_quantized(src0->type)); |
609 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
610 | | |
611 | | // rows per thread |
612 | 0 | const int dr = (nr + nth - 1)/nth; |
613 | | |
614 | | // row range for this thread |
615 | 0 | const int ir0 = dr*ith; |
616 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
617 | |
|
618 | 0 | float * wdata = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
619 | |
|
620 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
621 | | // src0 indices |
622 | 0 | const int i03 = ir/(ne02*ne01); |
623 | 0 | const int i02 = (ir - i03*ne02*ne01)/ne01; |
624 | 0 | const int i01 = (ir - i03*ne02*ne01 - i02*ne01); |
625 | | |
626 | | // src1 and dst are same shape as src0 => same indices |
627 | 0 | const int i13 = i03; |
628 | 0 | const int i12 = i02; |
629 | 0 | const int i11 = i01; |
630 | |
|
631 | 0 | const int i3 = i03; |
632 | 0 | const int i2 = i02; |
633 | 0 | const int i1 = i01; |
634 | |
|
635 | 0 | void * src0_row = (void *) ((char *) src0->data + (i01*nb01 + i02*nb02 + i03*nb03)); |
636 | 0 | float * src1_row = (float *)((char *) src1->data + (i11*nb11 + i12*nb12 + i13*nb13)); |
637 | 0 | void * dst_row = (void *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
638 | |
|
639 | 0 | assert(ne00 % 32 == 0); |
640 | | |
641 | | // unquantize row from src0 to temp buffer |
642 | 0 | dequantize_row_q(src0_row, wdata, ne00); |
643 | | // add src1 |
644 | 0 | ggml_vec_acc_f32(ne00, wdata, src1_row); |
645 | | // quantize row to dst |
646 | 0 | if (quantize_row_q != NULL) { |
647 | 0 | quantize_row_q(wdata, dst_row, ne00); |
648 | 0 | } else { |
649 | 0 | memcpy(dst_row, wdata, ne0*nb0); |
650 | 0 | } |
651 | 0 | } |
652 | 0 | } |
653 | | |
654 | | void ggml_compute_forward_add( |
655 | | const ggml_compute_params * params, |
656 | 0 | ggml_tensor * dst) { |
657 | |
|
658 | 0 | const ggml_tensor * src0 = dst->src[0]; |
659 | |
|
660 | 0 | switch (src0->type) { |
661 | 0 | case GGML_TYPE_F32: |
662 | 0 | case GGML_TYPE_F16: |
663 | 0 | case GGML_TYPE_BF16: |
664 | 0 | { |
665 | 0 | ggml_compute_forward_add_non_quantized(params, dst); |
666 | 0 | } break; |
667 | 0 | case GGML_TYPE_Q1_0: |
668 | 0 | case GGML_TYPE_Q2_0: |
669 | 0 | case GGML_TYPE_Q4_0: |
670 | 0 | case GGML_TYPE_Q4_1: |
671 | 0 | case GGML_TYPE_Q5_0: |
672 | 0 | case GGML_TYPE_Q5_1: |
673 | 0 | case GGML_TYPE_Q8_0: |
674 | 0 | case GGML_TYPE_MXFP4: |
675 | 0 | case GGML_TYPE_NVFP4: |
676 | 0 | case GGML_TYPE_Q2_K: |
677 | 0 | case GGML_TYPE_Q3_K: |
678 | 0 | case GGML_TYPE_Q4_K: |
679 | 0 | case GGML_TYPE_Q5_K: |
680 | 0 | case GGML_TYPE_Q6_K: |
681 | 0 | case GGML_TYPE_TQ1_0: |
682 | 0 | case GGML_TYPE_TQ2_0: |
683 | 0 | case GGML_TYPE_IQ2_XXS: |
684 | 0 | case GGML_TYPE_IQ2_XS: |
685 | 0 | case GGML_TYPE_IQ3_XXS: |
686 | 0 | case GGML_TYPE_IQ1_S: |
687 | 0 | case GGML_TYPE_IQ1_M: |
688 | 0 | case GGML_TYPE_IQ4_NL: |
689 | 0 | case GGML_TYPE_IQ4_XS: |
690 | 0 | case GGML_TYPE_IQ3_S: |
691 | 0 | case GGML_TYPE_IQ2_S: |
692 | 0 | { |
693 | 0 | ggml_compute_forward_add_q_f32(params, dst); |
694 | 0 | } break; |
695 | 0 | default: |
696 | 0 | { |
697 | 0 | GGML_ABORT("fatal error"); |
698 | 0 | } |
699 | 0 | } |
700 | 0 | } |
701 | | |
702 | | // ggml_compute_forward_add_id |
703 | | |
704 | | static void ggml_compute_forward_add_id_f32( |
705 | | const ggml_compute_params * params, |
706 | 0 | ggml_tensor * dst) { |
707 | |
|
708 | 0 | const ggml_tensor * src0 = dst->src[0]; |
709 | 0 | const ggml_tensor * src1 = dst->src[1]; |
710 | 0 | const ggml_tensor * src2 = dst->src[2]; |
711 | |
|
712 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
713 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
714 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
715 | 0 | GGML_ASSERT(src2->type == GGML_TYPE_I32); |
716 | |
|
717 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
718 | 0 | GGML_ASSERT(src1->nb[0] == sizeof(float)); |
719 | |
|
720 | 0 | const int ith = params->ith; |
721 | 0 | const int nth = params->nth; |
722 | |
|
723 | 0 | const int nr = ggml_nrows(src0); |
724 | |
|
725 | 0 | GGML_TENSOR_TERNARY_OP_LOCALS |
726 | |
|
727 | 0 | GGML_ASSERT( nb0 == sizeof(float)); |
728 | 0 | GGML_ASSERT(nb10 == sizeof(float)); |
729 | | |
730 | | // rows per thread |
731 | 0 | const int dr = (nr + nth - 1)/nth; |
732 | | |
733 | | // row range for this thread |
734 | 0 | const int ir0 = dr*ith; |
735 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
736 | |
|
737 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
738 | | // src0 indices |
739 | 0 | const int i3 = ir/(ne2*ne1); |
740 | 0 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
741 | 0 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
742 | | |
743 | | // src1 indices |
744 | 0 | const int i11 = *(int32_t *) ((char *) src2->data + i1*nb20 + i2*nb21); |
745 | |
|
746 | 0 | GGML_ASSERT(i11 >= 0 && i11 < ne11); |
747 | |
|
748 | 0 | ggml_vec_add_f32(ne0, |
749 | 0 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), |
750 | 0 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), |
751 | 0 | (float *) ((char *) src1->data + i11*nb11)); |
752 | 0 | } |
753 | 0 | } |
754 | | |
755 | | void ggml_compute_forward_add_id( |
756 | | const ggml_compute_params * params, |
757 | 0 | ggml_tensor * dst) { |
758 | |
|
759 | 0 | const ggml_tensor * src0 = dst->src[0]; |
760 | |
|
761 | 0 | switch (src0->type) { |
762 | 0 | case GGML_TYPE_F32: |
763 | 0 | { |
764 | 0 | ggml_compute_forward_add_id_f32(params, dst); |
765 | 0 | } break; |
766 | 0 | default: |
767 | 0 | { |
768 | 0 | GGML_ABORT("unsupported type for ggml_compute_forward_add_id: %s", ggml_type_name(src0->type)); |
769 | 0 | } |
770 | 0 | } |
771 | 0 | } |
772 | | |
773 | | // ggml_compute_forward_add1 |
774 | | |
775 | | static void ggml_compute_forward_add1_f32( |
776 | | const ggml_compute_params * params, |
777 | 0 | ggml_tensor * dst) { |
778 | |
|
779 | 0 | const ggml_tensor * src0 = dst->src[0]; |
780 | 0 | const ggml_tensor * src1 = dst->src[1]; |
781 | |
|
782 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
783 | 0 | GGML_ASSERT(ggml_is_scalar(src1)); |
784 | |
|
785 | 0 | const int ith = params->ith; |
786 | 0 | const int nth = params->nth; |
787 | |
|
788 | 0 | const int nr = ggml_nrows(src0); |
789 | |
|
790 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
791 | |
|
792 | 0 | GGML_ASSERT( nb0 == sizeof(float)); |
793 | 0 | GGML_ASSERT(nb00 == sizeof(float)); |
794 | | |
795 | | // rows per thread |
796 | 0 | const int dr = (nr + nth - 1)/nth; |
797 | | |
798 | | // row range for this thread |
799 | 0 | const int ir0 = dr*ith; |
800 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
801 | |
|
802 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
803 | | // src0 and dst are same shape => same indices |
804 | 0 | const int i3 = ir/(ne2*ne1); |
805 | 0 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
806 | 0 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
807 | |
|
808 | | #ifdef GGML_USE_ACCELERATE |
809 | | GGML_UNUSED(ggml_vec_add1_f32); |
810 | | |
811 | | vDSP_vadd( |
812 | | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), 1, |
813 | | (float *) ((char *) src1->data), 0, |
814 | | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), 1, |
815 | | ne0); |
816 | | #else |
817 | 0 | ggml_vec_add1_f32(ne0, |
818 | 0 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), |
819 | 0 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), |
820 | 0 | *(float *) src1->data); |
821 | 0 | #endif |
822 | 0 | } |
823 | 0 | } |
824 | | |
825 | | static void ggml_compute_forward_add1_f16_f32( |
826 | | const ggml_compute_params * params, |
827 | 0 | ggml_tensor * dst) { |
828 | |
|
829 | 0 | const ggml_tensor * src0 = dst->src[0]; |
830 | 0 | const ggml_tensor * src1 = dst->src[1]; |
831 | |
|
832 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
833 | 0 | GGML_ASSERT(ggml_is_scalar(src1)); |
834 | | |
835 | | // scalar to add |
836 | 0 | const float v = *(float *) src1->data; |
837 | |
|
838 | 0 | const int ith = params->ith; |
839 | 0 | const int nth = params->nth; |
840 | |
|
841 | 0 | const int nr = ggml_nrows(src0); |
842 | |
|
843 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
844 | |
|
845 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F16); |
846 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
847 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_F16); |
848 | |
|
849 | 0 | GGML_ASSERT( nb0 == sizeof(ggml_fp16_t)); |
850 | 0 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
851 | | |
852 | | // rows per thread |
853 | 0 | const int dr = (nr + nth - 1)/nth; |
854 | | |
855 | | // row range for this thread |
856 | 0 | const int ir0 = dr*ith; |
857 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
858 | |
|
859 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
860 | | // src0 and dst are same shape => same indices |
861 | 0 | const int i3 = ir/(ne2*ne1); |
862 | 0 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
863 | 0 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
864 | |
|
865 | 0 | ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
866 | 0 | ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
867 | 0 | for (int i = 0; i < ne0; i++) { |
868 | 0 | dst_ptr[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(src0_ptr[i]) + v); |
869 | 0 | } |
870 | 0 | } |
871 | 0 | } |
872 | | |
873 | | static void ggml_compute_forward_add1_f16_f16( |
874 | | const ggml_compute_params * params, |
875 | 0 | ggml_tensor * dst) { |
876 | |
|
877 | 0 | const ggml_tensor * src0 = dst->src[0]; |
878 | 0 | const ggml_tensor * src1 = dst->src[1]; |
879 | |
|
880 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
881 | 0 | GGML_ASSERT(ggml_is_scalar(src1)); |
882 | | |
883 | | // scalar to add |
884 | 0 | const float v = GGML_CPU_FP16_TO_FP32(*(ggml_fp16_t *) src1->data); |
885 | |
|
886 | 0 | const int ith = params->ith; |
887 | 0 | const int nth = params->nth; |
888 | |
|
889 | 0 | const int nr = ggml_nrows(src0); |
890 | |
|
891 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
892 | |
|
893 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F16); |
894 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F16); |
895 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_F16); |
896 | |
|
897 | 0 | GGML_ASSERT( nb0 == sizeof(ggml_fp16_t)); |
898 | 0 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
899 | | |
900 | | // rows per thread |
901 | 0 | const int dr = (nr + nth - 1)/nth; |
902 | | |
903 | | // row range for this thread |
904 | 0 | const int ir0 = dr*ith; |
905 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
906 | |
|
907 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
908 | | // src0 and dst are same shape => same indices |
909 | 0 | const int i3 = ir/(ne2*ne1); |
910 | 0 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
911 | 0 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
912 | |
|
913 | 0 | ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
914 | 0 | ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
915 | 0 | for (int i = 0; i < ne0; i++) { |
916 | 0 | dst_ptr[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(src0_ptr[i]) + v); |
917 | 0 | } |
918 | 0 | } |
919 | 0 | } |
920 | | |
921 | | static void ggml_compute_forward_add1_q_f32( |
922 | | const ggml_compute_params * params, |
923 | 0 | ggml_tensor * dst) { |
924 | |
|
925 | 0 | const ggml_tensor * src0 = dst->src[0]; |
926 | 0 | const ggml_tensor * src1 = dst->src[1]; |
927 | |
|
928 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
929 | 0 | GGML_ASSERT(ggml_is_scalar(src1)); |
930 | | |
931 | | // scalar to add |
932 | 0 | const float v = *(float *) src1->data; |
933 | |
|
934 | 0 | const int ith = params->ith; |
935 | 0 | const int nth = params->nth; |
936 | |
|
937 | 0 | const int nr = ggml_nrows(src0); |
938 | |
|
939 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
940 | |
|
941 | 0 | const ggml_type type = src0->type; |
942 | 0 | ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
943 | 0 | ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(type)->from_float; |
944 | | |
945 | | // we don't support permuted src0 |
946 | 0 | GGML_ASSERT(nb00 == ggml_type_size(type)); |
947 | | |
948 | | // dst cannot be transposed or permuted |
949 | 0 | GGML_ASSERT(nb0 <= nb1); |
950 | 0 | GGML_ASSERT(nb1 <= nb2); |
951 | 0 | GGML_ASSERT(nb2 <= nb3); |
952 | |
|
953 | 0 | GGML_ASSERT(ggml_is_quantized(src0->type)); |
954 | 0 | GGML_ASSERT(dst->type == src0->type); |
955 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
956 | | |
957 | | // rows per thread |
958 | 0 | const int dr = (nr + nth - 1)/nth; |
959 | | |
960 | | // row range for this thread |
961 | 0 | const int ir0 = dr*ith; |
962 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
963 | |
|
964 | 0 | float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith; |
965 | |
|
966 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
967 | | // src0 and dst are same shape => same indices |
968 | 0 | const int i3 = ir/(ne2*ne1); |
969 | 0 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
970 | 0 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
971 | |
|
972 | 0 | void * src0_row = (void *) ((char *) src0->data + (i1*nb01 + i2*nb02 + i3*nb03)); |
973 | 0 | void * dst_row = (void *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb0 )); |
974 | |
|
975 | 0 | assert(ne0 % 32 == 0); |
976 | | |
977 | | // unquantize row from src0 to temp buffer |
978 | 0 | dequantize_row_q(src0_row, wdata, ne0); |
979 | | // add src1 |
980 | 0 | ggml_vec_acc1_f32(ne0, wdata, v); |
981 | | // quantize row to dst |
982 | 0 | quantize_row_q(wdata, dst_row, ne0); |
983 | 0 | } |
984 | 0 | } |
985 | | |
986 | | static void ggml_compute_forward_add1_bf16_f32( |
987 | | const ggml_compute_params * params, |
988 | 0 | ggml_tensor * dst) { |
989 | |
|
990 | 0 | const ggml_tensor * src0 = dst->src[0]; |
991 | 0 | const ggml_tensor * src1 = dst->src[1]; |
992 | |
|
993 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
994 | 0 | GGML_ASSERT(ggml_is_scalar(src1)); |
995 | | |
996 | | // scalar to add |
997 | 0 | const float v = *(float *) src1->data; |
998 | |
|
999 | 0 | const int ith = params->ith; |
1000 | 0 | const int nth = params->nth; |
1001 | |
|
1002 | 0 | const int nr = ggml_nrows(src0); |
1003 | |
|
1004 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
1005 | |
|
1006 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_BF16); |
1007 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
1008 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_BF16); |
1009 | |
|
1010 | 0 | GGML_ASSERT( nb0 == sizeof(ggml_bf16_t)); |
1011 | 0 | GGML_ASSERT(nb00 == sizeof(ggml_bf16_t)); |
1012 | | |
1013 | | // rows per thread |
1014 | 0 | const int dr = (nr + nth - 1)/nth; |
1015 | | |
1016 | | // row range for this thread |
1017 | 0 | const int ir0 = dr*ith; |
1018 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
1019 | |
|
1020 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
1021 | | // src0 and dst are same shape => same indices |
1022 | 0 | const int i3 = ir/(ne2*ne1); |
1023 | 0 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
1024 | 0 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
1025 | |
|
1026 | 0 | ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
1027 | 0 | ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
1028 | 0 | for (int i = 0; i < ne0; i++) { |
1029 | 0 | dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v); |
1030 | 0 | } |
1031 | 0 | } |
1032 | 0 | } |
1033 | | |
1034 | | static void ggml_compute_forward_add1_bf16_bf16( |
1035 | | const ggml_compute_params * params, |
1036 | 0 | ggml_tensor * dst) { |
1037 | |
|
1038 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1039 | 0 | const ggml_tensor * src1 = dst->src[1]; |
1040 | |
|
1041 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
1042 | 0 | GGML_ASSERT(ggml_is_scalar(src1)); |
1043 | | |
1044 | | // scalar to add |
1045 | 0 | const float v = GGML_BF16_TO_FP32(*(ggml_bf16_t *) src1->data); |
1046 | |
|
1047 | 0 | const int ith = params->ith; |
1048 | 0 | const int nth = params->nth; |
1049 | |
|
1050 | 0 | const int nr = ggml_nrows(src0); |
1051 | |
|
1052 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
1053 | |
|
1054 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_BF16); |
1055 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_BF16); |
1056 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_BF16); |
1057 | |
|
1058 | 0 | GGML_ASSERT( nb0 == sizeof(ggml_bf16_t)); |
1059 | 0 | GGML_ASSERT(nb00 == sizeof(ggml_bf16_t)); |
1060 | | |
1061 | | // rows per thread |
1062 | 0 | const int dr = (nr + nth - 1)/nth; |
1063 | | |
1064 | | // row range for this thread |
1065 | 0 | const int ir0 = dr*ith; |
1066 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
1067 | |
|
1068 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
1069 | | // src0 and dst are same shape => same indices |
1070 | 0 | const int i3 = ir/(ne2*ne1); |
1071 | 0 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
1072 | 0 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
1073 | |
|
1074 | 0 | ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
1075 | 0 | ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
1076 | 0 | for (int i = 0; i < ne0; i++) { |
1077 | 0 | dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v); |
1078 | 0 | } |
1079 | 0 | } |
1080 | 0 | } |
1081 | | |
1082 | | void ggml_compute_forward_add1( |
1083 | | const ggml_compute_params * params, |
1084 | 0 | ggml_tensor * dst) { |
1085 | |
|
1086 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1087 | 0 | const ggml_tensor * src1 = dst->src[1]; |
1088 | |
|
1089 | 0 | switch (src0->type) { |
1090 | 0 | case GGML_TYPE_F32: |
1091 | 0 | { |
1092 | 0 | ggml_compute_forward_add1_f32(params, dst); |
1093 | 0 | } break; |
1094 | 0 | case GGML_TYPE_F16: |
1095 | 0 | { |
1096 | 0 | if (src1->type == GGML_TYPE_F16) { |
1097 | 0 | ggml_compute_forward_add1_f16_f16(params, dst); |
1098 | 0 | } |
1099 | 0 | else if (src1->type == GGML_TYPE_F32) { |
1100 | 0 | ggml_compute_forward_add1_f16_f32(params, dst); |
1101 | 0 | } |
1102 | 0 | else { |
1103 | 0 | GGML_ABORT("fatal error"); |
1104 | 0 | } |
1105 | 0 | } break; |
1106 | 0 | case GGML_TYPE_BF16: |
1107 | 0 | { |
1108 | 0 | if (src1->type == GGML_TYPE_BF16) { |
1109 | 0 | ggml_compute_forward_add1_bf16_bf16(params, dst); |
1110 | 0 | } |
1111 | 0 | else if (src1->type == GGML_TYPE_F32) { |
1112 | 0 | ggml_compute_forward_add1_bf16_f32(params, dst); |
1113 | 0 | } |
1114 | 0 | else { |
1115 | 0 | GGML_ABORT("fatal error"); |
1116 | 0 | } |
1117 | 0 | } break; |
1118 | 0 | case GGML_TYPE_Q1_0: |
1119 | 0 | case GGML_TYPE_Q2_0: |
1120 | 0 | case GGML_TYPE_Q4_0: |
1121 | 0 | case GGML_TYPE_Q4_1: |
1122 | 0 | case GGML_TYPE_Q5_0: |
1123 | 0 | case GGML_TYPE_Q5_1: |
1124 | 0 | case GGML_TYPE_Q8_0: |
1125 | 0 | case GGML_TYPE_Q8_1: |
1126 | 0 | case GGML_TYPE_MXFP4: |
1127 | 0 | case GGML_TYPE_NVFP4: |
1128 | 0 | case GGML_TYPE_Q2_K: |
1129 | 0 | case GGML_TYPE_Q3_K: |
1130 | 0 | case GGML_TYPE_Q4_K: |
1131 | 0 | case GGML_TYPE_Q5_K: |
1132 | 0 | case GGML_TYPE_Q6_K: |
1133 | 0 | case GGML_TYPE_TQ1_0: |
1134 | 0 | case GGML_TYPE_TQ2_0: |
1135 | 0 | case GGML_TYPE_IQ2_XXS: |
1136 | 0 | case GGML_TYPE_IQ2_XS: |
1137 | 0 | case GGML_TYPE_IQ3_XXS: |
1138 | 0 | case GGML_TYPE_IQ1_S: |
1139 | 0 | case GGML_TYPE_IQ1_M: |
1140 | 0 | case GGML_TYPE_IQ4_NL: |
1141 | 0 | case GGML_TYPE_IQ4_XS: |
1142 | 0 | case GGML_TYPE_IQ3_S: |
1143 | 0 | case GGML_TYPE_IQ2_S: |
1144 | 0 | { |
1145 | 0 | ggml_compute_forward_add1_q_f32(params, dst); |
1146 | 0 | } break; |
1147 | 0 | default: |
1148 | 0 | { |
1149 | 0 | GGML_ABORT("fatal error"); |
1150 | 0 | } |
1151 | 0 | } |
1152 | 0 | } |
1153 | | |
1154 | | // ggml_compute_forward_acc |
1155 | | |
1156 | | static void ggml_compute_forward_acc_f32( |
1157 | | const ggml_compute_params * params, |
1158 | 0 | ggml_tensor * dst) { |
1159 | |
|
1160 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1161 | 0 | const ggml_tensor * src1 = dst->src[1]; |
1162 | |
|
1163 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
1164 | 0 | GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
1165 | | |
1166 | | // view src0 and dst with these strides and data offset inbytes during acc |
1167 | | // nb0 is implicitly element_size because src0 and dst are contiguous |
1168 | 0 | size_t nb1 = ((int32_t *) dst->op_params)[0]; |
1169 | 0 | size_t nb2 = ((int32_t *) dst->op_params)[1]; |
1170 | 0 | size_t nb3 = ((int32_t *) dst->op_params)[2]; |
1171 | 0 | size_t offset = ((int32_t *) dst->op_params)[3]; |
1172 | 0 | bool inplace = (bool) ((int32_t *) dst->op_params)[4]; |
1173 | |
|
1174 | 0 | if (!inplace) { |
1175 | 0 | if (params->ith == 0) { |
1176 | | // memcpy needs to be synchronized across threads to avoid race conditions. |
1177 | | // => do it in INIT phase |
1178 | 0 | memcpy( |
1179 | 0 | ((char *) dst->data), |
1180 | 0 | ((char *) src0->data), |
1181 | 0 | ggml_nbytes(dst)); |
1182 | 0 | } |
1183 | 0 | ggml_barrier(params->threadpool); |
1184 | 0 | } |
1185 | |
|
1186 | 0 | const int ith = params->ith; |
1187 | 0 | const int nth = params->nth; |
1188 | |
|
1189 | 0 | const int nr = ggml_nrows(src1); |
1190 | 0 | const int nc = src1->ne[0]; |
1191 | |
|
1192 | 0 | GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) |
1193 | 0 | GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) |
1194 | | |
1195 | | // src0 and dst as viewed during acc |
1196 | 0 | const size_t nb0 = ggml_element_size(src0); |
1197 | |
|
1198 | 0 | const size_t nb00 = nb0; |
1199 | 0 | const size_t nb01 = nb1; |
1200 | 0 | const size_t nb02 = nb2; |
1201 | 0 | const size_t nb03 = nb3; |
1202 | |
|
1203 | 0 | GGML_ASSERT(offset + (ne10 == 0 ? 0 : ne10-1)*nb0 + (ne11 == 0 ? 0 : ne11-1)*nb1 + (ne12 == 0 ? 0 : ne12-1)*nb2 + (ne13 == 0 ? 0 : ne13-1)*nb3 < ggml_nbytes(dst)); |
1204 | 0 | GGML_ASSERT(offset + (ne10 == 0 ? 0 : ne10-1)*nb00 + (ne11 == 0 ? 0 : ne11-1)*nb01 + (ne12 == 0 ? 0 : ne12-1)*nb02 + (ne13 == 0 ? 0 : ne13-1)*nb03 < ggml_nbytes(src0)); |
1205 | |
|
1206 | 0 | GGML_ASSERT(nb10 == sizeof(float)); |
1207 | | |
1208 | | // rows per thread |
1209 | 0 | const int dr = (nr + nth - 1)/nth; |
1210 | | |
1211 | | // row range for this thread |
1212 | 0 | const int ir0 = dr*ith; |
1213 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
1214 | |
|
1215 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
1216 | | // src0 and dst are viewed with shape of src1 and offset |
1217 | | // => same indices |
1218 | 0 | const int i3 = ir/(ne12*ne11); |
1219 | 0 | const int i2 = (ir - i3*ne12*ne11)/ne11; |
1220 | 0 | const int i1 = (ir - i3*ne12*ne11 - i2*ne11); |
1221 | |
|
1222 | | #ifdef GGML_USE_ACCELERATE |
1223 | | vDSP_vadd( |
1224 | | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset), 1, |
1225 | | (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11), 1, |
1226 | | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), 1, nc); |
1227 | | #else |
1228 | 0 | ggml_vec_add_f32(nc, |
1229 | 0 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), |
1230 | 0 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset), |
1231 | 0 | (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11)); |
1232 | 0 | #endif |
1233 | 0 | } |
1234 | 0 | } |
1235 | | |
1236 | | void ggml_compute_forward_acc( |
1237 | | const ggml_compute_params * params, |
1238 | 0 | ggml_tensor * dst) { |
1239 | |
|
1240 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1241 | |
|
1242 | 0 | switch (src0->type) { |
1243 | 0 | case GGML_TYPE_F32: |
1244 | 0 | { |
1245 | 0 | ggml_compute_forward_acc_f32(params, dst); |
1246 | 0 | } break; |
1247 | 0 | case GGML_TYPE_F16: |
1248 | 0 | case GGML_TYPE_BF16: |
1249 | 0 | case GGML_TYPE_Q1_0: |
1250 | 0 | case GGML_TYPE_Q2_0: |
1251 | 0 | case GGML_TYPE_Q4_0: |
1252 | 0 | case GGML_TYPE_Q4_1: |
1253 | 0 | case GGML_TYPE_Q5_0: |
1254 | 0 | case GGML_TYPE_Q5_1: |
1255 | 0 | case GGML_TYPE_Q8_0: |
1256 | 0 | case GGML_TYPE_Q8_1: |
1257 | 0 | case GGML_TYPE_MXFP4: |
1258 | 0 | case GGML_TYPE_NVFP4: |
1259 | 0 | case GGML_TYPE_Q2_K: |
1260 | 0 | case GGML_TYPE_Q3_K: |
1261 | 0 | case GGML_TYPE_Q4_K: |
1262 | 0 | case GGML_TYPE_Q5_K: |
1263 | 0 | case GGML_TYPE_Q6_K: |
1264 | 0 | case GGML_TYPE_TQ1_0: |
1265 | 0 | case GGML_TYPE_TQ2_0: |
1266 | 0 | case GGML_TYPE_IQ2_XXS: |
1267 | 0 | case GGML_TYPE_IQ2_XS: |
1268 | 0 | case GGML_TYPE_IQ3_XXS: |
1269 | 0 | case GGML_TYPE_IQ1_S: |
1270 | 0 | case GGML_TYPE_IQ1_M: |
1271 | 0 | case GGML_TYPE_IQ4_NL: |
1272 | 0 | case GGML_TYPE_IQ4_XS: |
1273 | 0 | case GGML_TYPE_IQ3_S: |
1274 | 0 | case GGML_TYPE_IQ2_S: |
1275 | 0 | default: |
1276 | 0 | { |
1277 | 0 | GGML_ABORT("fatal error"); |
1278 | 0 | } |
1279 | 0 | } |
1280 | 0 | } |
1281 | | |
1282 | | // ggml_compute_forward_sum |
1283 | | |
1284 | | static void ggml_compute_forward_sum_f32( |
1285 | | const ggml_compute_params * params, |
1286 | 0 | ggml_tensor * dst) { |
1287 | |
|
1288 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1289 | |
|
1290 | 0 | if (params->ith != 0) { |
1291 | 0 | return; |
1292 | 0 | } |
1293 | | |
1294 | 0 | assert(ggml_is_scalar(dst)); |
1295 | 0 | assert(src0->nb[0] == sizeof(float)); |
1296 | |
|
1297 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
1298 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
1299 | |
|
1300 | 0 | ggml_float sum = 0; |
1301 | 0 | ggml_float row_sum = 0; |
1302 | |
|
1303 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
1304 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
1305 | 0 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
1306 | 0 | ggml_vec_sum_f32_ggf(ne00, |
1307 | 0 | &row_sum, |
1308 | 0 | (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03)); |
1309 | 0 | sum += row_sum; |
1310 | 0 | } |
1311 | 0 | } |
1312 | 0 | } |
1313 | 0 | ((float *) dst->data)[0] = sum; |
1314 | 0 | } |
1315 | | |
1316 | | static void ggml_compute_forward_sum_f16( |
1317 | | const ggml_compute_params * params, |
1318 | 0 | ggml_tensor * dst) { |
1319 | |
|
1320 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1321 | |
|
1322 | 0 | if (params->ith != 0) { |
1323 | 0 | return; |
1324 | 0 | } |
1325 | | |
1326 | 0 | assert(ggml_is_scalar(dst)); |
1327 | |
|
1328 | 0 | assert(src0->nb[0] == sizeof(ggml_fp16_t)); |
1329 | |
|
1330 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
1331 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
1332 | |
|
1333 | 0 | float sum = 0; |
1334 | 0 | float row_sum = 0; |
1335 | |
|
1336 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
1337 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
1338 | 0 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
1339 | 0 | ggml_vec_sum_f16_ggf(ne00, |
1340 | 0 | &row_sum, |
1341 | 0 | (ggml_fp16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03)); |
1342 | 0 | sum += row_sum; |
1343 | 0 | } |
1344 | 0 | } |
1345 | 0 | } |
1346 | 0 | ((ggml_fp16_t *) dst->data)[0] = GGML_CPU_FP32_TO_FP16(sum); |
1347 | 0 | } |
1348 | | |
1349 | | static void ggml_compute_forward_sum_bf16( |
1350 | | const ggml_compute_params * params, |
1351 | 0 | ggml_tensor * dst) { |
1352 | |
|
1353 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1354 | |
|
1355 | 0 | if (params->ith != 0) { |
1356 | 0 | return; |
1357 | 0 | } |
1358 | | |
1359 | 0 | assert(ggml_is_scalar(dst)); |
1360 | |
|
1361 | 0 | assert(src0->nb[0] == sizeof(ggml_bf16_t)); |
1362 | |
|
1363 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
1364 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
1365 | |
|
1366 | 0 | float sum = 0; |
1367 | 0 | float row_sum = 0; |
1368 | |
|
1369 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
1370 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
1371 | 0 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
1372 | 0 | ggml_vec_sum_bf16_ggf(ne00, |
1373 | 0 | &row_sum, |
1374 | 0 | (ggml_bf16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03)); |
1375 | 0 | sum += row_sum; |
1376 | 0 | } |
1377 | 0 | } |
1378 | 0 | } |
1379 | 0 | ((ggml_bf16_t *) dst->data)[0] = GGML_FP32_TO_BF16(sum); |
1380 | 0 | } |
1381 | | |
1382 | | void ggml_compute_forward_sum( |
1383 | | const ggml_compute_params * params, |
1384 | 0 | ggml_tensor * dst) { |
1385 | |
|
1386 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1387 | |
|
1388 | 0 | switch (src0->type) { |
1389 | 0 | case GGML_TYPE_F32: |
1390 | 0 | { |
1391 | 0 | ggml_compute_forward_sum_f32(params, dst); |
1392 | 0 | } break; |
1393 | 0 | case GGML_TYPE_F16: |
1394 | 0 | { |
1395 | 0 | ggml_compute_forward_sum_f16(params, dst); |
1396 | 0 | } break; |
1397 | 0 | case GGML_TYPE_BF16: |
1398 | 0 | { |
1399 | 0 | ggml_compute_forward_sum_bf16(params, dst); |
1400 | 0 | } break; |
1401 | 0 | default: |
1402 | 0 | { |
1403 | 0 | GGML_ABORT("fatal error"); |
1404 | 0 | } |
1405 | 0 | } |
1406 | 0 | } |
1407 | | |
1408 | | // ggml_compute_forward_cumsum |
1409 | | |
1410 | | static void ggml_compute_forward_cumsum_f32( |
1411 | | const ggml_compute_params * params, |
1412 | 0 | ggml_tensor * dst) { |
1413 | |
|
1414 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1415 | |
|
1416 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
1417 | 0 | GGML_ASSERT(dst->nb[0] == sizeof(float)); |
1418 | |
|
1419 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
1420 | |
|
1421 | 0 | GGML_ASSERT(ne0 == ne00); |
1422 | 0 | GGML_ASSERT(ne1 == ne01); |
1423 | 0 | GGML_ASSERT(ne2 == ne02); |
1424 | 0 | GGML_ASSERT(ne3 == ne03); |
1425 | |
|
1426 | 0 | const auto [ir0, ir1] = get_thread_range(params, src0); |
1427 | |
|
1428 | 0 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
1429 | 0 | const int64_t i03 = ir/(ne02*ne01); |
1430 | 0 | const int64_t i02 = (ir - i03*ne02*ne01)/ne01; |
1431 | 0 | const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01); |
1432 | |
|
1433 | 0 | float * src_row = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
1434 | 0 | float * dst_row = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
1435 | |
|
1436 | 0 | ggml_vec_cumsum_f32(ne00, dst_row, src_row); |
1437 | 0 | } |
1438 | 0 | } |
1439 | | |
1440 | | void ggml_compute_forward_cumsum( |
1441 | | const ggml_compute_params * params, |
1442 | 0 | ggml_tensor * dst) { |
1443 | |
|
1444 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1445 | |
|
1446 | 0 | switch (src0->type) { |
1447 | 0 | case GGML_TYPE_F32: |
1448 | 0 | { |
1449 | 0 | ggml_compute_forward_cumsum_f32(params, dst); |
1450 | 0 | } break; |
1451 | 0 | default: |
1452 | 0 | { |
1453 | 0 | GGML_ABORT("fatal error"); |
1454 | 0 | } |
1455 | 0 | } |
1456 | 0 | } |
1457 | | |
1458 | | // ggml_compute_forward_sum_rows |
1459 | | |
1460 | | static void ggml_compute_forward_sum_rows_f32( |
1461 | | const ggml_compute_params * params, |
1462 | 0 | ggml_tensor * dst) { |
1463 | |
|
1464 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1465 | |
|
1466 | 0 | if (params->ith != 0) { |
1467 | 0 | return; |
1468 | 0 | } |
1469 | | |
1470 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
1471 | 0 | GGML_ASSERT(dst->nb[0] == sizeof(float)); |
1472 | |
|
1473 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
1474 | |
|
1475 | 0 | GGML_ASSERT(ne0 == 1); |
1476 | 0 | GGML_ASSERT(ne1 == ne01); |
1477 | 0 | GGML_ASSERT(ne2 == ne02); |
1478 | 0 | GGML_ASSERT(ne3 == ne03); |
1479 | |
|
1480 | 0 | for (int64_t i3 = 0; i3 < ne03; i3++) { |
1481 | 0 | for (int64_t i2 = 0; i2 < ne02; i2++) { |
1482 | 0 | for (int64_t i1 = 0; i1 < ne01; i1++) { |
1483 | 0 | float * src_row = (float *) ((char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03); |
1484 | 0 | float * dst_row = (float *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3); |
1485 | 0 | float row_sum = 0; |
1486 | 0 | ggml_vec_sum_f32(ne00, &row_sum, src_row); |
1487 | 0 | dst_row[0] = row_sum; |
1488 | 0 | } |
1489 | 0 | } |
1490 | 0 | } |
1491 | 0 | } |
1492 | | |
1493 | | void ggml_compute_forward_sum_rows( |
1494 | | const ggml_compute_params * params, |
1495 | 0 | ggml_tensor * dst) { |
1496 | |
|
1497 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1498 | |
|
1499 | 0 | switch (src0->type) { |
1500 | 0 | case GGML_TYPE_F32: |
1501 | 0 | { |
1502 | 0 | ggml_compute_forward_sum_rows_f32(params, dst); |
1503 | 0 | } break; |
1504 | 0 | default: |
1505 | 0 | { |
1506 | 0 | GGML_ABORT("fatal error"); |
1507 | 0 | } |
1508 | 0 | } |
1509 | 0 | } |
1510 | | |
1511 | | // ggml_compute_forward_mean |
1512 | | |
1513 | | static void ggml_compute_forward_mean_f32( |
1514 | | const ggml_compute_params * params, |
1515 | 0 | ggml_tensor * dst) { |
1516 | |
|
1517 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1518 | |
|
1519 | 0 | if (params->ith != 0) { |
1520 | 0 | return; |
1521 | 0 | } |
1522 | | |
1523 | 0 | assert(src0->nb[0] == sizeof(float)); |
1524 | |
|
1525 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
1526 | |
|
1527 | 0 | assert(ne0 == 1); |
1528 | 0 | assert(ne1 == ne01); |
1529 | 0 | assert(ne2 == ne02); |
1530 | 0 | assert(ne3 == ne03); |
1531 | |
|
1532 | 0 | GGML_UNUSED(ne0); |
1533 | 0 | GGML_UNUSED(ne1); |
1534 | 0 | GGML_UNUSED(ne2); |
1535 | 0 | GGML_UNUSED(ne3); |
1536 | |
|
1537 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
1538 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
1539 | 0 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
1540 | 0 | ggml_vec_sum_f32(ne00, |
1541 | 0 | (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3), |
1542 | 0 | (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03)); |
1543 | |
|
1544 | 0 | *(float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3) /= (float) ne00; |
1545 | 0 | } |
1546 | 0 | } |
1547 | 0 | } |
1548 | 0 | } |
1549 | | |
1550 | | void ggml_compute_forward_mean( |
1551 | | const ggml_compute_params * params, |
1552 | 0 | ggml_tensor * dst) { |
1553 | |
|
1554 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1555 | |
|
1556 | 0 | switch (src0->type) { |
1557 | 0 | case GGML_TYPE_F32: |
1558 | 0 | { |
1559 | 0 | ggml_compute_forward_mean_f32(params, dst); |
1560 | 0 | } break; |
1561 | 0 | default: |
1562 | 0 | { |
1563 | 0 | GGML_ABORT("fatal error"); |
1564 | 0 | } |
1565 | 0 | } |
1566 | 0 | } |
1567 | | |
1568 | | // ggml_compute_forward_argmax |
1569 | | |
1570 | | static void ggml_compute_forward_argmax_f32( |
1571 | | const ggml_compute_params * params, |
1572 | 0 | ggml_tensor * dst) { |
1573 | |
|
1574 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1575 | |
|
1576 | 0 | if (params->ith != 0) { |
1577 | 0 | return; |
1578 | 0 | } |
1579 | | |
1580 | 0 | assert(src0->nb[0] == sizeof(float)); |
1581 | 0 | assert(dst->nb[0] == sizeof(float)); |
1582 | |
|
1583 | 0 | const int64_t ne00 = src0->ne[0]; |
1584 | 0 | const int64_t ne01 = src0->ne[1]; |
1585 | |
|
1586 | 0 | const size_t nb01 = src0->nb[1]; |
1587 | 0 | const size_t nb0 = dst->nb[0]; |
1588 | |
|
1589 | 0 | for (int64_t i1 = 0; i1 < ne01; i1++) { |
1590 | 0 | float * src = (float *) ((char *) src0->data + i1*nb01); |
1591 | 0 | int32_t * dst_ = (int32_t *) ((char *) dst->data + i1*nb0); |
1592 | 0 | int v = 0; |
1593 | 0 | ggml_vec_argmax_f32(ne00, &v, src); |
1594 | 0 | dst_[0] = v; |
1595 | 0 | } |
1596 | 0 | } |
1597 | | |
1598 | | void ggml_compute_forward_argmax( |
1599 | | const ggml_compute_params * params, |
1600 | 0 | ggml_tensor * dst) { |
1601 | |
|
1602 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1603 | |
|
1604 | 0 | switch (src0->type) { |
1605 | 0 | case GGML_TYPE_F32: |
1606 | 0 | { |
1607 | 0 | ggml_compute_forward_argmax_f32(params, dst); |
1608 | 0 | } break; |
1609 | 0 | default: |
1610 | 0 | { |
1611 | 0 | GGML_ABORT("fatal error"); |
1612 | 0 | } |
1613 | 0 | } |
1614 | 0 | } |
1615 | | |
1616 | | // ggml_compute_forward_count_equal |
1617 | | |
1618 | | static void ggml_compute_forward_count_equal_i32( |
1619 | | const ggml_compute_params * params, |
1620 | 0 | ggml_tensor * dst) { |
1621 | |
|
1622 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1623 | 0 | const ggml_tensor * src1 = dst->src[1]; |
1624 | |
|
1625 | 0 | GGML_TENSOR_BINARY_OP_LOCALS; |
1626 | |
|
1627 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_I32); |
1628 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_I32); |
1629 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, src1)); |
1630 | 0 | GGML_ASSERT(ggml_is_scalar(dst)); |
1631 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_I64); |
1632 | |
|
1633 | 0 | const int64_t nr = ggml_nrows(src0); |
1634 | |
|
1635 | 0 | const int ith = params->ith; |
1636 | 0 | const int nth = params->nth; |
1637 | |
|
1638 | 0 | int64_t * sums = (int64_t *) params->wdata; |
1639 | 0 | int64_t sum_thread = 0; |
1640 | | |
1641 | | // rows per thread |
1642 | 0 | const int64_t dr = (nr + nth - 1)/nth; |
1643 | | |
1644 | | // row range for this thread |
1645 | 0 | const int64_t ir0 = dr*ith; |
1646 | 0 | const int64_t ir1 = MIN(ir0 + dr, nr); |
1647 | |
|
1648 | 0 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
1649 | 0 | const int64_t i03 = ir / (ne02*ne01); |
1650 | 0 | const int64_t i02 = (ir - i03*ne03) / ne01; |
1651 | 0 | const int64_t i01 = ir - i03*ne03 - i02*ne02; |
1652 | |
|
1653 | 0 | const char * data0 = (const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01; |
1654 | 0 | const char * data1 = (const char *) src1->data + i03*nb13 + i02*nb12 + i01*nb11; |
1655 | |
|
1656 | 0 | for (int64_t i00 = 0; i00 < ne00; ++i00) { |
1657 | 0 | const int32_t val0 = *((const int32_t *) (data0 + i00*nb00)); |
1658 | 0 | const int32_t val1 = *((const int32_t *) (data1 + i00*nb10)); |
1659 | |
|
1660 | 0 | sum_thread += val0 == val1; |
1661 | 0 | } |
1662 | 0 | } |
1663 | 0 | if (ith != 0) { |
1664 | 0 | sums[ith] = sum_thread; |
1665 | 0 | } |
1666 | 0 | ggml_barrier(params->threadpool); |
1667 | |
|
1668 | 0 | if (ith != 0) { |
1669 | 0 | return; |
1670 | 0 | } |
1671 | | |
1672 | 0 | for (int ith_other = 1; ith_other < nth; ++ith_other) { |
1673 | 0 | sum_thread += sums[ith_other]; |
1674 | 0 | } |
1675 | 0 | *((int64_t *) dst->data) = sum_thread; |
1676 | 0 | } |
1677 | | |
1678 | | void ggml_compute_forward_count_equal( |
1679 | | const ggml_compute_params * params, |
1680 | 0 | ggml_tensor * dst) { |
1681 | |
|
1682 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1683 | |
|
1684 | 0 | switch (src0->type) { |
1685 | 0 | case GGML_TYPE_I32: |
1686 | 0 | { |
1687 | 0 | ggml_compute_forward_count_equal_i32(params, dst); |
1688 | 0 | } break; |
1689 | 0 | default: |
1690 | 0 | { |
1691 | 0 | GGML_ABORT("fatal error"); |
1692 | 0 | } |
1693 | 0 | } |
1694 | 0 | } |
1695 | | |
1696 | | // ggml_compute_forward_repeat |
1697 | | |
1698 | | static void ggml_compute_forward_repeat_f32( |
1699 | | const ggml_compute_params * params, |
1700 | 0 | ggml_tensor * dst) { |
1701 | |
|
1702 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1703 | |
|
1704 | 0 | if (params->ith != 0) { |
1705 | 0 | return; |
1706 | 0 | } |
1707 | | |
1708 | 0 | GGML_ASSERT(ggml_can_repeat(src0, dst)); |
1709 | |
|
1710 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
1711 | | |
1712 | | // guaranteed to be an integer due to the check in ggml_can_repeat |
1713 | 0 | const int nr0 = (int)(ne0/ne00); |
1714 | 0 | const int nr1 = (int)(ne1/ne01); |
1715 | 0 | const int nr2 = (int)(ne2/ne02); |
1716 | 0 | const int nr3 = (int)(ne3/ne03); |
1717 | | |
1718 | | // TODO: support for transposed / permuted tensors |
1719 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
1720 | 0 | GGML_ASSERT(nb00 == sizeof(float)); |
1721 | | |
1722 | | // TODO: maybe this is not optimal? |
1723 | 0 | for (int i3 = 0; i3 < nr3; i3++) { |
1724 | 0 | for (int k3 = 0; k3 < ne03; k3++) { |
1725 | 0 | for (int i2 = 0; i2 < nr2; i2++) { |
1726 | 0 | for (int k2 = 0; k2 < ne02; k2++) { |
1727 | 0 | for (int i1 = 0; i1 < nr1; i1++) { |
1728 | 0 | for (int k1 = 0; k1 < ne01; k1++) { |
1729 | 0 | for (int i0 = 0; i0 < nr0; i0++) { |
1730 | 0 | ggml_vec_cpy_f32(ne00, |
1731 | 0 | (float *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0), |
1732 | 0 | (float *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01)); |
1733 | 0 | } |
1734 | 0 | } |
1735 | 0 | } |
1736 | 0 | } |
1737 | 0 | } |
1738 | 0 | } |
1739 | 0 | } |
1740 | 0 | } |
1741 | | |
1742 | | static void ggml_compute_forward_repeat_f16( |
1743 | | const ggml_compute_params * params, |
1744 | 0 | ggml_tensor * dst) { |
1745 | |
|
1746 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1747 | |
|
1748 | 0 | if (params->ith != 0) { |
1749 | 0 | return; |
1750 | 0 | } |
1751 | | |
1752 | 0 | GGML_ASSERT(ggml_can_repeat(src0, dst)); |
1753 | |
|
1754 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
1755 | | |
1756 | | // guaranteed to be an integer due to the check in ggml_can_repeat |
1757 | 0 | const int nr0 = (int)(ne0/ne00); |
1758 | 0 | const int nr1 = (int)(ne1/ne01); |
1759 | 0 | const int nr2 = (int)(ne2/ne02); |
1760 | 0 | const int nr3 = (int)(ne3/ne03); |
1761 | | |
1762 | | // TODO: support for transposed / permuted tensors |
1763 | 0 | GGML_ASSERT(nb0 == sizeof(ggml_fp16_t)); |
1764 | 0 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
1765 | | |
1766 | | // TODO: maybe this is not optimal? |
1767 | 0 | for (int i3 = 0; i3 < nr3; i3++) { |
1768 | 0 | for (int k3 = 0; k3 < ne03; k3++) { |
1769 | 0 | for (int i2 = 0; i2 < nr2; i2++) { |
1770 | 0 | for (int k2 = 0; k2 < ne02; k2++) { |
1771 | 0 | for (int i1 = 0; i1 < nr1; i1++) { |
1772 | 0 | for (int k1 = 0; k1 < ne01; k1++) { |
1773 | 0 | for (int i0 = 0; i0 < nr0; i0++) { |
1774 | 0 | ggml_fp16_t * y = (ggml_fp16_t *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0); |
1775 | 0 | ggml_fp16_t * x = (ggml_fp16_t *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01); |
1776 | | // ggml_vec_cpy_f16(ne00, y, x) |
1777 | 0 | for (int i = 0; i < ne00; ++i) { |
1778 | 0 | y[i] = x[i]; |
1779 | 0 | } |
1780 | 0 | } |
1781 | 0 | } |
1782 | 0 | } |
1783 | 0 | } |
1784 | 0 | } |
1785 | 0 | } |
1786 | 0 | } |
1787 | 0 | } |
1788 | | |
1789 | | void ggml_compute_forward_repeat( |
1790 | | const ggml_compute_params * params, |
1791 | 0 | ggml_tensor * dst) { |
1792 | |
|
1793 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1794 | |
|
1795 | 0 | switch (src0->type) { |
1796 | 0 | case GGML_TYPE_F16: |
1797 | 0 | case GGML_TYPE_BF16: |
1798 | 0 | case GGML_TYPE_I16: |
1799 | 0 | { |
1800 | 0 | ggml_compute_forward_repeat_f16(params, dst); |
1801 | 0 | } break; |
1802 | 0 | case GGML_TYPE_F32: |
1803 | 0 | case GGML_TYPE_I32: |
1804 | 0 | { |
1805 | 0 | ggml_compute_forward_repeat_f32(params, dst); |
1806 | 0 | } break; |
1807 | | // TODO: templateify the implementation and support for I64 |
1808 | | // ref https://github.com/ggml-org/llama.cpp/pull/14274#discussion_r2169492225 |
1809 | | //case GGML_TYPE_I64: |
1810 | | // { |
1811 | | // ggml_compute_forward_repeat_i64(params, dst); |
1812 | | // } break; |
1813 | 0 | default: |
1814 | 0 | { |
1815 | 0 | GGML_ABORT("fatal error"); |
1816 | 0 | } |
1817 | 0 | } |
1818 | 0 | } |
1819 | | |
1820 | | // ggml_compute_forward_repeat_back |
1821 | | |
1822 | | static void ggml_compute_forward_repeat_back_f32( |
1823 | | const ggml_compute_params * params, |
1824 | 0 | ggml_tensor * dst) { |
1825 | |
|
1826 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1827 | |
|
1828 | 0 | if (params->ith != 0) { |
1829 | 0 | return; |
1830 | 0 | } |
1831 | | |
1832 | 0 | GGML_ASSERT(ggml_can_repeat(dst, src0)); |
1833 | |
|
1834 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
1835 | | |
1836 | | // guaranteed to be an integer due to the check in ggml_can_repeat |
1837 | 0 | const int nr0 = (int)(ne00/ne0); |
1838 | 0 | const int nr1 = (int)(ne01/ne1); |
1839 | 0 | const int nr2 = (int)(ne02/ne2); |
1840 | 0 | const int nr3 = (int)(ne03/ne3); |
1841 | | |
1842 | | // TODO: support for transposed / permuted tensors |
1843 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
1844 | 0 | GGML_ASSERT(nb00 == sizeof(float)); |
1845 | |
|
1846 | 0 | if (ggml_is_contiguous(dst)) { |
1847 | 0 | ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
1848 | 0 | } else { |
1849 | 0 | for (int k3 = 0; k3 < ne3; k3++) { |
1850 | 0 | for (int k2 = 0; k2 < ne2; k2++) { |
1851 | 0 | for (int k1 = 0; k1 < ne1; k1++) { |
1852 | 0 | ggml_vec_set_f32(ne0, |
1853 | 0 | (float *) ((char *) dst->data + k1*nb1 + k2*nb2 + k3*nb3), |
1854 | 0 | 0); |
1855 | 0 | } |
1856 | 0 | } |
1857 | 0 | } |
1858 | 0 | } |
1859 | | |
1860 | | // TODO: maybe this is not optimal? |
1861 | 0 | for (int i3 = 0; i3 < nr3; i3++) { |
1862 | 0 | for (int k3 = 0; k3 < ne3; k3++) { |
1863 | 0 | for (int i2 = 0; i2 < nr2; i2++) { |
1864 | 0 | for (int k2 = 0; k2 < ne2; k2++) { |
1865 | 0 | for (int i1 = 0; i1 < nr1; i1++) { |
1866 | 0 | for (int k1 = 0; k1 < ne1; k1++) { |
1867 | 0 | for (int i0 = 0; i0 < nr0; i0++) { |
1868 | 0 | ggml_vec_acc_f32(ne0, |
1869 | 0 | (float *) ((char *) dst->data + ( k3)*nb3 + ( k2)*nb2 + ( k1)*nb1), |
1870 | 0 | (float *) ((char *) src0->data + (i3*ne3 + k3)*nb03 + (i2*ne2 + k2)*nb02 + (i1*ne1 + k1)*nb01 + (i0*ne0)*nb00)); |
1871 | 0 | } |
1872 | 0 | } |
1873 | 0 | } |
1874 | 0 | } |
1875 | 0 | } |
1876 | 0 | } |
1877 | 0 | } |
1878 | 0 | } |
1879 | | |
1880 | | void ggml_compute_forward_repeat_back( |
1881 | | const ggml_compute_params * params, |
1882 | 0 | ggml_tensor * dst) { |
1883 | |
|
1884 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1885 | |
|
1886 | 0 | switch (src0->type) { |
1887 | 0 | case GGML_TYPE_F32: |
1888 | 0 | { |
1889 | 0 | ggml_compute_forward_repeat_back_f32(params, dst); |
1890 | 0 | } break; |
1891 | 0 | default: |
1892 | 0 | { |
1893 | 0 | GGML_ABORT("fatal error"); |
1894 | 0 | } |
1895 | 0 | } |
1896 | 0 | } |
1897 | | |
1898 | | // ggml_compute_forward_concat |
1899 | | |
1900 | | static void ggml_compute_forward_concat_any( |
1901 | | const ggml_compute_params * params, |
1902 | 0 | ggml_tensor * dst) { |
1903 | |
|
1904 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1905 | 0 | const ggml_tensor * src1 = dst->src[1]; |
1906 | |
|
1907 | 0 | const size_t len = ggml_type_size(src0->type); |
1908 | |
|
1909 | 0 | const int ith = params->ith; |
1910 | 0 | const int nth = params->nth; |
1911 | |
|
1912 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
1913 | |
|
1914 | 0 | const int32_t dim = ggml_get_op_params_i32(dst, 0); |
1915 | |
|
1916 | 0 | GGML_ASSERT(dim >= 0 && dim < 4); |
1917 | |
|
1918 | 0 | int64_t o[4] = {0, 0, 0, 0}; |
1919 | 0 | if (dim == 0) { |
1920 | 0 | o[dim] = src0->ne[dim]/ggml_blck_size(src0->type); |
1921 | 0 | } else { |
1922 | 0 | o[dim] = src0->ne[dim]; |
1923 | 0 | } |
1924 | |
|
1925 | 0 | const char * x; |
1926 | | |
1927 | | // TODO: smarter multi-theading |
1928 | 0 | for (int i3 = 0; i3 < ne3; i3++) { |
1929 | 0 | for (int i2 = ith; i2 < ne2; i2 += nth) { |
1930 | 0 | for (int i1 = 0; i1 < ne1; i1++) { |
1931 | 0 | for (int i0 = 0; i0 < ne0/ggml_blck_size(dst->type); i0++) { |
1932 | 0 | if (i0 < ne00/ggml_blck_size(src0->type) && i1 < ne01 && i2 < ne02 && i3 < ne03) { |
1933 | 0 | x = (const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03; |
1934 | 0 | } else { |
1935 | 0 | x = (const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13; |
1936 | 0 | } |
1937 | |
|
1938 | 0 | char * y = (char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3; |
1939 | |
|
1940 | 0 | memcpy(y, x, len); |
1941 | 0 | } |
1942 | 0 | } |
1943 | 0 | } |
1944 | 0 | } |
1945 | 0 | } |
1946 | | |
1947 | | static void ggml_compute_forward_concat_i8( |
1948 | | const ggml_compute_params * params, |
1949 | 0 | ggml_tensor * dst) { |
1950 | |
|
1951 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1952 | 0 | const ggml_tensor * src1 = dst->src[1]; |
1953 | |
|
1954 | 0 | GGML_ASSERT(ggml_type_size(src0->type) == sizeof(int8_t)); |
1955 | |
|
1956 | 0 | const int ith = params->ith; |
1957 | 0 | const int nth = params->nth; |
1958 | |
|
1959 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
1960 | |
|
1961 | 0 | const int32_t dim = ggml_get_op_params_i32(dst, 0); |
1962 | |
|
1963 | 0 | GGML_ASSERT(dim >= 0 && dim < 4); |
1964 | |
|
1965 | 0 | int64_t o[4] = {0, 0, 0, 0}; |
1966 | 0 | o[dim] = src0->ne[dim]; |
1967 | |
|
1968 | 0 | const int8_t * x; |
1969 | | |
1970 | | // TODO: smarter multi-theading |
1971 | 0 | for (int i3 = 0; i3 < ne3; i3++) { |
1972 | 0 | for (int i2 = ith; i2 < ne2; i2 += nth) { |
1973 | 0 | for (int i1 = 0; i1 < ne1; i1++) { |
1974 | 0 | for (int i0 = 0; i0 < ne0; i0++) { |
1975 | 0 | if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) { |
1976 | 0 | x = (const int8_t *) ((const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03); |
1977 | 0 | } else { |
1978 | 0 | x = (const int8_t *) ((const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13); |
1979 | 0 | } |
1980 | |
|
1981 | 0 | int8_t * y = (int8_t *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
1982 | |
|
1983 | 0 | *y = *x; |
1984 | 0 | } |
1985 | 0 | } |
1986 | 0 | } |
1987 | 0 | } |
1988 | 0 | } |
1989 | | |
1990 | | static void ggml_compute_forward_concat_f16( |
1991 | | const ggml_compute_params * params, |
1992 | 0 | ggml_tensor * dst) { |
1993 | |
|
1994 | 0 | const ggml_tensor * src0 = dst->src[0]; |
1995 | 0 | const ggml_tensor * src1 = dst->src[1]; |
1996 | |
|
1997 | 0 | GGML_ASSERT(ggml_type_size(src0->type) == sizeof(ggml_fp16_t)); |
1998 | |
|
1999 | 0 | const int ith = params->ith; |
2000 | 0 | const int nth = params->nth; |
2001 | |
|
2002 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
2003 | |
|
2004 | 0 | const int32_t dim = ggml_get_op_params_i32(dst, 0); |
2005 | |
|
2006 | 0 | GGML_ASSERT(dim >= 0 && dim < 4); |
2007 | |
|
2008 | 0 | int64_t o[4] = {0, 0, 0, 0}; |
2009 | 0 | o[dim] = src0->ne[dim]; |
2010 | |
|
2011 | 0 | const ggml_fp16_t * x; |
2012 | | |
2013 | | // TODO: smarter multi-theading |
2014 | 0 | for (int i3 = 0; i3 < ne3; i3++) { |
2015 | 0 | for (int i2 = ith; i2 < ne2; i2 += nth) { |
2016 | 0 | for (int i1 = 0; i1 < ne1; i1++) { |
2017 | 0 | for (int i0 = 0; i0 < ne0; i0++) { |
2018 | 0 | if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) { |
2019 | 0 | x = (const ggml_fp16_t *) ((const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03); |
2020 | 0 | } else { |
2021 | 0 | x = (const ggml_fp16_t *) ((const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13); |
2022 | 0 | } |
2023 | |
|
2024 | 0 | ggml_fp16_t * y = (ggml_fp16_t *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
2025 | |
|
2026 | 0 | *y = *x; |
2027 | 0 | } |
2028 | 0 | } |
2029 | 0 | } |
2030 | 0 | } |
2031 | 0 | } |
2032 | | |
2033 | | static void ggml_compute_forward_concat_f32( |
2034 | | const ggml_compute_params * params, |
2035 | 0 | ggml_tensor * dst) { |
2036 | |
|
2037 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2038 | 0 | const ggml_tensor * src1 = dst->src[1]; |
2039 | |
|
2040 | 0 | GGML_ASSERT(ggml_type_size(src0->type) == sizeof(float)); |
2041 | |
|
2042 | 0 | const int ith = params->ith; |
2043 | 0 | const int nth = params->nth; |
2044 | |
|
2045 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
2046 | |
|
2047 | 0 | const int32_t dim = ggml_get_op_params_i32(dst, 0); |
2048 | |
|
2049 | 0 | GGML_ASSERT(dim >= 0 && dim < 4); |
2050 | |
|
2051 | 0 | int64_t o[4] = {0, 0, 0, 0}; |
2052 | 0 | o[dim] = src0->ne[dim]; |
2053 | |
|
2054 | 0 | const float * x; |
2055 | | |
2056 | | // TODO: smarter multi-theading |
2057 | 0 | for (int i3 = 0; i3 < ne3; i3++) { |
2058 | 0 | for (int i2 = ith; i2 < ne2; i2 += nth) { |
2059 | 0 | for (int i1 = 0; i1 < ne1; i1++) { |
2060 | 0 | for (int i0 = 0; i0 < ne0; i0++) { |
2061 | 0 | if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) { |
2062 | 0 | x = (const float *) ((const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03); |
2063 | 0 | } else { |
2064 | 0 | x = (const float *) ((const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13); |
2065 | 0 | } |
2066 | |
|
2067 | 0 | float * y = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
2068 | |
|
2069 | 0 | *y = *x; |
2070 | 0 | } |
2071 | 0 | } |
2072 | 0 | } |
2073 | 0 | } |
2074 | 0 | } |
2075 | | |
2076 | | void ggml_compute_forward_concat( |
2077 | | const ggml_compute_params * params, |
2078 | 0 | ggml_tensor * dst) { |
2079 | |
|
2080 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2081 | 0 | const ggml_tensor * src1 = dst->src[1]; |
2082 | |
|
2083 | 0 | if (ggml_is_quantized(src0->type)) { |
2084 | 0 | GGML_ASSERT(ggml_is_contiguous_rows(src0)); |
2085 | 0 | GGML_ASSERT(ggml_is_contiguous_rows(src1)); |
2086 | 0 | GGML_ASSERT(src0->ne[0] % ggml_blck_size(src0->type) == 0); |
2087 | 0 | GGML_ASSERT(src1->ne[0] % ggml_blck_size(src1->type) == 0); |
2088 | 0 | } |
2089 | |
|
2090 | 0 | switch (src0->type) { |
2091 | 0 | case GGML_TYPE_F16: |
2092 | 0 | case GGML_TYPE_BF16: |
2093 | 0 | case GGML_TYPE_I16: |
2094 | 0 | { |
2095 | 0 | ggml_compute_forward_concat_f16(params, dst); |
2096 | 0 | } break; |
2097 | 0 | case GGML_TYPE_I8: |
2098 | 0 | { |
2099 | 0 | ggml_compute_forward_concat_i8(params, dst); |
2100 | 0 | } break; |
2101 | 0 | case GGML_TYPE_F32: |
2102 | 0 | case GGML_TYPE_I32: |
2103 | 0 | { |
2104 | 0 | ggml_compute_forward_concat_f32(params, dst); |
2105 | 0 | } break; |
2106 | 0 | default: |
2107 | 0 | { |
2108 | 0 | ggml_compute_forward_concat_any(params, dst); |
2109 | 0 | } |
2110 | 0 | } |
2111 | 0 | } |
2112 | | |
2113 | | // ggml_compute_forward_gelu |
2114 | | |
2115 | | static void ggml_compute_forward_gelu_f32( |
2116 | | const ggml_compute_params * params, |
2117 | 0 | ggml_tensor * dst) { |
2118 | |
|
2119 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2120 | |
|
2121 | 0 | assert(ggml_is_contiguous_rows(src0)); |
2122 | 0 | assert(ggml_are_same_shape(src0, dst)); |
2123 | |
|
2124 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
2125 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
2126 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
2127 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
2128 | |
|
2129 | 0 | const int ith = params->ith; |
2130 | 0 | const int nth = params->nth; |
2131 | |
|
2132 | 0 | const int nc = src0->ne[0]; |
2133 | 0 | const int nr = ggml_nrows(src0); |
2134 | | |
2135 | | // rows per thread |
2136 | 0 | const int dr = (nr + nth - 1)/nth; |
2137 | | |
2138 | | // row range for this thread |
2139 | 0 | const int ir0 = dr*ith; |
2140 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2141 | |
|
2142 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
2143 | 0 | const int i3 = ir/(ne02*ne01); |
2144 | 0 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
2145 | 0 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
2146 | |
|
2147 | 0 | ggml_vec_gelu_f32(nc, |
2148 | 0 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
2149 | 0 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
2150 | |
|
2151 | | #ifndef NDEBUG |
2152 | | for (int k = 0; k < nc; k++) { |
2153 | | const float x = ((float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*(dst->nb[1])))[k]; |
2154 | | GGML_UNUSED(x); |
2155 | | assert(!isnan(x)); |
2156 | | assert(!isinf(x)); |
2157 | | } |
2158 | | #endif // NDEBUG |
2159 | 0 | } |
2160 | 0 | } |
2161 | | |
2162 | | static void ggml_compute_forward_gelu_f16( |
2163 | | const ggml_compute_params * params, |
2164 | 0 | ggml_tensor * dst) { |
2165 | |
|
2166 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2167 | |
|
2168 | 0 | assert(ggml_is_contiguous_rows(src0)); |
2169 | 0 | assert(ggml_are_same_shape(src0, dst)); |
2170 | |
|
2171 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
2172 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
2173 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
2174 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
2175 | |
|
2176 | 0 | const int ith = params->ith; |
2177 | 0 | const int nth = params->nth; |
2178 | |
|
2179 | 0 | const int nc = src0->ne[0]; |
2180 | 0 | const int nr = ggml_nrows(src0); |
2181 | | |
2182 | | // rows per thread |
2183 | 0 | const int dr = (nr + nth - 1)/nth; |
2184 | | |
2185 | | // row range for this thread |
2186 | 0 | const int ir0 = dr*ith; |
2187 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2188 | |
|
2189 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
2190 | 0 | const int i3 = ir/(ne02*ne01); |
2191 | 0 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
2192 | 0 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
2193 | |
|
2194 | 0 | ggml_vec_gelu_f16(nc, |
2195 | 0 | (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
2196 | 0 | (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
2197 | |
|
2198 | | #ifndef NDEBUG |
2199 | | for (int k = 0; k < nc; k++) { |
2200 | | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*( dst->nb[1])))[k]; |
2201 | | const float v = GGML_CPU_FP16_TO_FP32(x); |
2202 | | GGML_UNUSED(v); |
2203 | | assert(!isnan(v)); |
2204 | | assert(!isinf(v)); |
2205 | | } |
2206 | | #endif // NDEBUG |
2207 | 0 | } |
2208 | 0 | } |
2209 | | |
2210 | | static void ggml_compute_forward_gelu( |
2211 | | const ggml_compute_params * params, |
2212 | 0 | ggml_tensor * dst) { |
2213 | |
|
2214 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2215 | |
|
2216 | 0 | switch (src0->type) { |
2217 | 0 | case GGML_TYPE_F32: |
2218 | 0 | { |
2219 | 0 | ggml_compute_forward_gelu_f32(params, dst); |
2220 | 0 | } break; |
2221 | 0 | case GGML_TYPE_F16: |
2222 | 0 | { |
2223 | 0 | ggml_compute_forward_gelu_f16(params, dst); |
2224 | 0 | } break; |
2225 | 0 | default: |
2226 | 0 | { |
2227 | 0 | GGML_ABORT("fatal error"); |
2228 | 0 | } |
2229 | 0 | } |
2230 | 0 | } |
2231 | | |
2232 | | // ggml_compute_fill |
2233 | | |
2234 | 0 | static void ggml_compute_forward_fill_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
2235 | 0 | const float c = ggml_get_op_params_f32(dst, 0); |
2236 | |
|
2237 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne); |
2238 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb); |
2239 | |
|
2240 | 0 | const auto [ir0, ir1] = get_thread_range(params, dst); |
2241 | |
|
2242 | 0 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
2243 | 0 | const int64_t i03 = ir/(ne2*ne1); |
2244 | 0 | const int64_t i02 = (ir - i03*ne2*ne1)/ne1; |
2245 | 0 | const int64_t i01 = (ir - i03*ne2*ne1 - i02*ne1); |
2246 | |
|
2247 | 0 | float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1); |
2248 | |
|
2249 | 0 | ggml_vec_set_f32(ne0, dst_ptr, c); |
2250 | 0 | } |
2251 | 0 | } |
2252 | | |
2253 | 0 | static void ggml_compute_forward_fill_f16(const ggml_compute_params * params, ggml_tensor * dst) { |
2254 | 0 | const ggml_fp16_t c = GGML_CPU_FP32_TO_FP16(ggml_get_op_params_f32(dst, 0)); |
2255 | |
|
2256 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne); |
2257 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb); |
2258 | |
|
2259 | 0 | const auto [ir0, ir1] = get_thread_range(params, dst); |
2260 | |
|
2261 | 0 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
2262 | 0 | const int64_t i03 = ir/(ne2*ne1); |
2263 | 0 | const int64_t i02 = (ir - i03*ne2*ne1)/ne1; |
2264 | 0 | const int64_t i01 = (ir - i03*ne2*ne1 - i02*ne1); |
2265 | |
|
2266 | 0 | ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1); |
2267 | |
|
2268 | 0 | ggml_vec_set_f16(ne0, dst_ptr, c); |
2269 | 0 | } |
2270 | 0 | } |
2271 | | |
2272 | 0 | void ggml_compute_forward_fill(const ggml_compute_params * params, ggml_tensor * dst) { |
2273 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2274 | |
|
2275 | 0 | switch (src0->type) { |
2276 | 0 | case GGML_TYPE_F32: |
2277 | 0 | { |
2278 | 0 | ggml_compute_forward_fill_f32(params, dst); |
2279 | 0 | } break; |
2280 | 0 | case GGML_TYPE_F16: |
2281 | 0 | { |
2282 | 0 | ggml_compute_forward_fill_f16(params, dst); |
2283 | 0 | } break; |
2284 | 0 | default: |
2285 | 0 | { |
2286 | 0 | GGML_ABORT("unsupported type for ggml_compute_forward_fill: %s", ggml_type_name(src0->type)); |
2287 | 0 | } |
2288 | 0 | } |
2289 | 0 | } |
2290 | | |
2291 | | // ggml_compute_tri |
2292 | | |
2293 | 0 | static void ggml_compute_forward_tri_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
2294 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2295 | |
|
2296 | 0 | const ggml_tri_type ttype = (ggml_tri_type) ggml_get_op_params_i32(dst, 0); |
2297 | |
|
2298 | 0 | GGML_ASSERT(ggml_is_contiguous(src0)); |
2299 | |
|
2300 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
2301 | |
|
2302 | 0 | const auto [ir0, ir1] = get_thread_range(params, src0); |
2303 | |
|
2304 | 0 | bool (*bipred)(int, int); |
2305 | |
|
2306 | 0 | switch (ttype) { |
2307 | 0 | case GGML_TRI_TYPE_LOWER: bipred = [](int i, int r) { return i < r; }; break; |
2308 | 0 | case GGML_TRI_TYPE_LOWER_DIAG: bipred = [](int i, int r) { return i <= r; }; break; |
2309 | 0 | case GGML_TRI_TYPE_UPPER: bipred = [](int i, int r) { return i > r; }; break; |
2310 | 0 | case GGML_TRI_TYPE_UPPER_DIAG: bipred = [](int i, int r) { return i >= r; }; break; |
2311 | 0 | default: GGML_ABORT("invalid tri type"); |
2312 | 0 | } |
2313 | | |
2314 | 0 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
2315 | 0 | const int64_t i03 = ir/(ne02*ne01); |
2316 | 0 | const int64_t i02 = (ir - i03*ne02*ne01)/ne01; |
2317 | 0 | const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01); |
2318 | |
|
2319 | 0 | const float * src_ptr = (const float *) ((const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01); |
2320 | 0 | float * dst_ptr = ( float *) (( char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1); |
2321 | |
|
2322 | 0 | for (int i0 = 0; i0 < ne0; ++i0) { |
2323 | 0 | dst_ptr[i0] = bipred(i0, i01) ? src_ptr[i0] : 0.0f; |
2324 | 0 | } |
2325 | 0 | } |
2326 | 0 | } |
2327 | | |
2328 | 0 | void ggml_compute_forward_tri(const ggml_compute_params * params, ggml_tensor * dst) { |
2329 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2330 | |
|
2331 | 0 | switch (src0->type) { |
2332 | 0 | case GGML_TYPE_F32: |
2333 | 0 | { |
2334 | 0 | ggml_compute_forward_tri_f32(params, dst); |
2335 | 0 | } break; |
2336 | 0 | default: |
2337 | 0 | { |
2338 | 0 | GGML_ABORT("fatal error"); |
2339 | 0 | } |
2340 | 0 | } |
2341 | 0 | } |
2342 | | |
2343 | | // ggml_compute_forward_gelu_erf |
2344 | | |
2345 | | static void ggml_compute_forward_gelu_erf_f32( |
2346 | | const ggml_compute_params * params, |
2347 | 0 | ggml_tensor * dst) { |
2348 | |
|
2349 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2350 | |
|
2351 | 0 | assert(ggml_is_contiguous_rows(src0)); |
2352 | 0 | assert(ggml_are_same_shape(src0, dst)); |
2353 | |
|
2354 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
2355 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
2356 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
2357 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
2358 | |
|
2359 | 0 | const int ith = params->ith; |
2360 | 0 | const int nth = params->nth; |
2361 | |
|
2362 | 0 | const int nc = src0->ne[0]; |
2363 | 0 | const int nr = ggml_nrows(src0); |
2364 | | |
2365 | | // rows per thread |
2366 | 0 | const int dr = (nr + nth - 1)/nth; |
2367 | | |
2368 | | // row range for this thread |
2369 | 0 | const int ir0 = dr*ith; |
2370 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2371 | |
|
2372 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
2373 | 0 | const int i3 = ir/(ne02*ne01); |
2374 | 0 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
2375 | 0 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
2376 | |
|
2377 | 0 | ggml_vec_gelu_erf_f32(nc, |
2378 | 0 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
2379 | 0 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
2380 | |
|
2381 | | #ifndef NDEBUG |
2382 | | for (int k = 0; k < nc; k++) { |
2383 | | const float x = ((float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*(dst->nb[1])))[k]; |
2384 | | GGML_UNUSED(x); |
2385 | | assert(!isnan(x)); |
2386 | | assert(!isinf(x)); |
2387 | | } |
2388 | | #endif // NDEBUG |
2389 | 0 | } |
2390 | 0 | } |
2391 | | |
2392 | | static void ggml_compute_forward_gelu_erf_f16( |
2393 | | const ggml_compute_params * params, |
2394 | 0 | ggml_tensor * dst) { |
2395 | |
|
2396 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2397 | |
|
2398 | 0 | assert(ggml_is_contiguous_rows(src0)); |
2399 | 0 | assert(ggml_are_same_shape(src0, dst)); |
2400 | |
|
2401 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
2402 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
2403 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
2404 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
2405 | |
|
2406 | 0 | const int ith = params->ith; |
2407 | 0 | const int nth = params->nth; |
2408 | |
|
2409 | 0 | const int nc = src0->ne[0]; |
2410 | 0 | const int nr = ggml_nrows(src0); |
2411 | | |
2412 | | // rows per thread |
2413 | 0 | const int dr = (nr + nth - 1)/nth; |
2414 | | |
2415 | | // row range for this thread |
2416 | 0 | const int ir0 = dr*ith; |
2417 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2418 | |
|
2419 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
2420 | 0 | const int i3 = ir/(ne02*ne01); |
2421 | 0 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
2422 | 0 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
2423 | |
|
2424 | 0 | ggml_vec_gelu_erf_f16(nc, |
2425 | 0 | (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
2426 | 0 | (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
2427 | |
|
2428 | | #ifndef NDEBUG |
2429 | | for (int k = 0; k < nc; k++) { |
2430 | | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*( dst->nb[1])))[k]; |
2431 | | const float v = GGML_CPU_FP16_TO_FP32(x); |
2432 | | GGML_UNUSED(v); |
2433 | | assert(!isnan(v)); |
2434 | | assert(!isinf(v)); |
2435 | | } |
2436 | | #endif // NDEBUG |
2437 | 0 | } |
2438 | 0 | } |
2439 | | |
2440 | | static void ggml_compute_forward_gelu_erf( |
2441 | | const ggml_compute_params * params, |
2442 | 0 | ggml_tensor * dst) { |
2443 | |
|
2444 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2445 | |
|
2446 | 0 | switch (src0->type) { |
2447 | 0 | case GGML_TYPE_F32: |
2448 | 0 | { |
2449 | 0 | ggml_compute_forward_gelu_erf_f32(params, dst); |
2450 | 0 | } break; |
2451 | 0 | case GGML_TYPE_F16: |
2452 | 0 | { |
2453 | 0 | ggml_compute_forward_gelu_erf_f16(params, dst); |
2454 | 0 | } break; |
2455 | 0 | default: |
2456 | 0 | { |
2457 | 0 | GGML_ABORT("fatal error"); |
2458 | 0 | } |
2459 | 0 | } |
2460 | 0 | } |
2461 | | |
2462 | | // ggml_compute_forward_gelu_quick |
2463 | | |
2464 | | static void ggml_compute_forward_gelu_quick_f32( |
2465 | | const ggml_compute_params * params, |
2466 | 0 | ggml_tensor * dst) { |
2467 | |
|
2468 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2469 | |
|
2470 | 0 | assert(ggml_is_contiguous_rows(src0)); |
2471 | 0 | assert(ggml_are_same_shape(src0, dst)); |
2472 | |
|
2473 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
2474 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
2475 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
2476 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
2477 | |
|
2478 | 0 | const int ith = params->ith; |
2479 | 0 | const int nth = params->nth; |
2480 | |
|
2481 | 0 | const int nc = src0->ne[0]; |
2482 | 0 | const int nr = ggml_nrows(src0); |
2483 | | |
2484 | | // rows per thread |
2485 | 0 | const int dr = (nr + nth - 1)/nth; |
2486 | | |
2487 | | // row range for this thread |
2488 | 0 | const int ir0 = dr*ith; |
2489 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2490 | |
|
2491 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
2492 | 0 | const int i3 = ir/(ne02*ne01); |
2493 | 0 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
2494 | 0 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
2495 | |
|
2496 | 0 | ggml_vec_gelu_quick_f32(nc, |
2497 | 0 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
2498 | 0 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
2499 | |
|
2500 | | #ifndef NDEBUG |
2501 | | for (int k = 0; k < nc; k++) { |
2502 | | const float x = ((float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*(dst->nb[1])))[k]; |
2503 | | GGML_UNUSED(x); |
2504 | | assert(!isnan(x)); |
2505 | | assert(!isinf(x)); |
2506 | | } |
2507 | | #endif // NDEBUG |
2508 | 0 | } |
2509 | 0 | } |
2510 | | |
2511 | | static void ggml_compute_forward_gelu_quick_f16( |
2512 | | const ggml_compute_params * params, |
2513 | 0 | ggml_tensor * dst) { |
2514 | |
|
2515 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2516 | |
|
2517 | 0 | assert(ggml_is_contiguous_rows(src0)); |
2518 | 0 | assert(ggml_are_same_shape(src0, dst)); |
2519 | |
|
2520 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
2521 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
2522 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
2523 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
2524 | |
|
2525 | 0 | const int ith = params->ith; |
2526 | 0 | const int nth = params->nth; |
2527 | |
|
2528 | 0 | const int nc = src0->ne[0]; |
2529 | 0 | const int nr = ggml_nrows(src0); |
2530 | | |
2531 | | // rows per thread |
2532 | 0 | const int dr = (nr + nth - 1)/nth; |
2533 | | |
2534 | | // row range for this thread |
2535 | 0 | const int ir0 = dr*ith; |
2536 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2537 | |
|
2538 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
2539 | 0 | const int i3 = ir/(ne02*ne01); |
2540 | 0 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
2541 | 0 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
2542 | |
|
2543 | 0 | ggml_vec_gelu_quick_f16(nc, |
2544 | 0 | (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
2545 | 0 | (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
2546 | |
|
2547 | | #ifndef NDEBUG |
2548 | | for (int k = 0; k < nc; k++) { |
2549 | | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*( dst->nb[1])))[k]; |
2550 | | const float v = GGML_CPU_FP16_TO_FP32(x); |
2551 | | GGML_UNUSED(v); |
2552 | | assert(!isnan(v)); |
2553 | | assert(!isinf(v)); |
2554 | | } |
2555 | | #endif // NDEBUG |
2556 | 0 | } |
2557 | 0 | } |
2558 | | |
2559 | | static void ggml_compute_forward_gelu_quick( |
2560 | | const ggml_compute_params * params, |
2561 | 0 | ggml_tensor * dst) { |
2562 | |
|
2563 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2564 | |
|
2565 | 0 | switch (src0->type) { |
2566 | 0 | case GGML_TYPE_F32: |
2567 | 0 | { |
2568 | 0 | ggml_compute_forward_gelu_quick_f32(params, dst); |
2569 | 0 | } break; |
2570 | 0 | case GGML_TYPE_F16: |
2571 | 0 | { |
2572 | 0 | ggml_compute_forward_gelu_quick_f16(params, dst); |
2573 | 0 | } break; |
2574 | 0 | default: |
2575 | 0 | { |
2576 | 0 | GGML_ABORT("fatal error"); |
2577 | 0 | } |
2578 | 0 | } |
2579 | 0 | } |
2580 | | |
2581 | | // ggml_compute_forward_silu |
2582 | | |
2583 | | static void ggml_compute_forward_silu_f32( |
2584 | | const ggml_compute_params * params, |
2585 | 0 | ggml_tensor * dst) { |
2586 | |
|
2587 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2588 | |
|
2589 | 0 | assert(ggml_is_contiguous_rows(src0)); |
2590 | 0 | assert(ggml_are_same_shape(src0, dst)); |
2591 | |
|
2592 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
2593 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
2594 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
2595 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
2596 | |
|
2597 | 0 | const int ith = params->ith; |
2598 | 0 | const int nth = params->nth; |
2599 | |
|
2600 | 0 | const int nc = src0->ne[0]; |
2601 | 0 | const int nr = ggml_nrows(src0); |
2602 | | |
2603 | | // rows per thread |
2604 | 0 | const int dr = (nr + nth - 1)/nth; |
2605 | | |
2606 | | // row range for this thread |
2607 | 0 | const int ir0 = dr*ith; |
2608 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2609 | |
|
2610 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
2611 | 0 | const int i3 = ir/(ne02*ne01); |
2612 | 0 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
2613 | 0 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
2614 | |
|
2615 | 0 | ggml_vec_silu_f32(nc, |
2616 | 0 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
2617 | 0 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
2618 | |
|
2619 | | #ifndef NDEBUG |
2620 | | for (int k = 0; k < nc; k++) { |
2621 | | const float x = ((float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*(dst->nb[1])))[k]; |
2622 | | GGML_UNUSED(x); |
2623 | | assert(!isnan(x)); |
2624 | | assert(!isinf(x)); |
2625 | | } |
2626 | | #endif // NDEBUG |
2627 | 0 | } |
2628 | 0 | } |
2629 | | |
2630 | | static void ggml_compute_forward_silu_f16( |
2631 | | const ggml_compute_params * params, |
2632 | 0 | ggml_tensor * dst) { |
2633 | |
|
2634 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2635 | |
|
2636 | 0 | assert(ggml_is_contiguous_rows(src0)); |
2637 | 0 | assert(ggml_are_same_shape(src0, dst)); |
2638 | |
|
2639 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
2640 | 0 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
2641 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
2642 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
2643 | |
|
2644 | 0 | const int ith = params->ith; |
2645 | 0 | const int nth = params->nth; |
2646 | |
|
2647 | 0 | const int nc = src0->ne[0]; |
2648 | 0 | const int nr = ggml_nrows(src0); |
2649 | | |
2650 | | // rows per thread |
2651 | 0 | const int dr = (nr + nth - 1)/nth; |
2652 | | |
2653 | | // row range for this thread |
2654 | 0 | const int ir0 = dr*ith; |
2655 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2656 | |
|
2657 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
2658 | 0 | const int i3 = ir/(ne02*ne01); |
2659 | 0 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
2660 | 0 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
2661 | |
|
2662 | 0 | ggml_vec_silu_f16(nc, |
2663 | 0 | (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
2664 | 0 | (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
2665 | |
|
2666 | | #ifndef NDEBUG |
2667 | | for (int k = 0; k < nc; k++) { |
2668 | | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*( dst->nb[1])))[k]; |
2669 | | const float v = GGML_CPU_FP16_TO_FP32(x); |
2670 | | GGML_UNUSED(v); |
2671 | | assert(!isnan(v)); |
2672 | | assert(!isinf(v)); |
2673 | | } |
2674 | | #endif // NDEBUG |
2675 | 0 | } |
2676 | 0 | } |
2677 | | |
2678 | | static void ggml_compute_forward_silu( |
2679 | | const ggml_compute_params * params, |
2680 | 0 | ggml_tensor * dst) { |
2681 | |
|
2682 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2683 | |
|
2684 | 0 | switch (src0->type) { |
2685 | 0 | case GGML_TYPE_F32: |
2686 | 0 | { |
2687 | 0 | ggml_compute_forward_silu_f32(params, dst); |
2688 | 0 | } break; |
2689 | 0 | case GGML_TYPE_F16: |
2690 | 0 | { |
2691 | 0 | ggml_compute_forward_silu_f16(params, dst); |
2692 | 0 | } break; |
2693 | 0 | default: |
2694 | 0 | { |
2695 | 0 | GGML_ABORT("fatal error"); |
2696 | 0 | } |
2697 | 0 | } |
2698 | 0 | } |
2699 | | // ggml_compute_forward_leaky_relu |
2700 | | |
2701 | | static void ggml_compute_forward_leaky_relu_f32( |
2702 | | const ggml_compute_params * params, |
2703 | 0 | ggml_tensor * dst) { |
2704 | |
|
2705 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2706 | |
|
2707 | 0 | if (params->ith != 0) { |
2708 | 0 | return; |
2709 | 0 | } |
2710 | | |
2711 | 0 | assert(ggml_is_contiguous_1(src0)); |
2712 | 0 | assert(ggml_is_contiguous_1(dst)); |
2713 | 0 | assert(ggml_are_same_shape(src0, dst)); |
2714 | |
|
2715 | 0 | const int n = ggml_nrows(src0); |
2716 | 0 | const int nc = src0->ne[0]; |
2717 | |
|
2718 | 0 | float negative_slope; |
2719 | 0 | memcpy(&negative_slope, dst->op_params, sizeof(float)); |
2720 | |
|
2721 | 0 | assert(dst->nb[0] == sizeof(float)); |
2722 | 0 | assert(src0->nb[0] == sizeof(float)); |
2723 | |
|
2724 | 0 | for (int i = 0; i < n; i++) { |
2725 | 0 | ggml_vec_leaky_relu_f32(nc, |
2726 | 0 | (float *) ((char *) dst->data + i*( dst->nb[1])), |
2727 | 0 | (float *) ((char *) src0->data + i*(src0->nb[1])), negative_slope); |
2728 | 0 | } |
2729 | 0 | } |
2730 | | |
2731 | | static void ggml_compute_forward_leaky_relu_f16( |
2732 | | const ggml_compute_params * params, |
2733 | 0 | ggml_tensor * dst) { |
2734 | |
|
2735 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2736 | |
|
2737 | 0 | if (params->ith != 0) { |
2738 | 0 | return; |
2739 | 0 | } |
2740 | | |
2741 | 0 | assert(ggml_is_contiguous_1(src0)); |
2742 | 0 | assert(ggml_is_contiguous_1(dst)); |
2743 | 0 | assert(ggml_are_same_shape(src0, dst)); |
2744 | |
|
2745 | 0 | const int n = ggml_nrows(src0); |
2746 | 0 | const int nc = src0->ne[0]; |
2747 | |
|
2748 | 0 | float negative_slope; |
2749 | 0 | memcpy(&negative_slope, dst->op_params, sizeof(float)); |
2750 | |
|
2751 | 0 | assert(dst->nb[0] == sizeof(ggml_fp16_t)); |
2752 | 0 | assert(src0->nb[0] == sizeof(ggml_fp16_t)); |
2753 | |
|
2754 | 0 | for (int i = 0; i < n; i++) { |
2755 | 0 | ggml_vec_leaky_relu_f16(nc, |
2756 | 0 | (ggml_fp16_t *) ((char *) dst->data + i*( dst->nb[1])), |
2757 | 0 | (ggml_fp16_t *) ((char *) src0->data + i*(src0->nb[1])), negative_slope); |
2758 | 0 | } |
2759 | 0 | } |
2760 | | |
2761 | | void ggml_compute_forward_leaky_relu( |
2762 | | const ggml_compute_params * params, |
2763 | 0 | ggml_tensor * dst) { |
2764 | |
|
2765 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2766 | |
|
2767 | 0 | switch (src0->type) { |
2768 | 0 | case GGML_TYPE_F32: |
2769 | 0 | { |
2770 | 0 | ggml_compute_forward_leaky_relu_f32(params, dst); |
2771 | 0 | } break; |
2772 | 0 | case GGML_TYPE_F16: |
2773 | 0 | { |
2774 | 0 | ggml_compute_forward_leaky_relu_f16(params, dst); |
2775 | 0 | } break; |
2776 | 0 | default: |
2777 | 0 | { |
2778 | 0 | GGML_ABORT("fatal error"); |
2779 | 0 | } |
2780 | 0 | } |
2781 | 0 | } |
2782 | | |
2783 | | // ggml_compute_forward_silu_back |
2784 | | |
2785 | | static void ggml_compute_forward_silu_back_f32( |
2786 | | const ggml_compute_params * params, |
2787 | 0 | ggml_tensor * dst) { |
2788 | |
|
2789 | 0 | const ggml_tensor * grad = dst->src[0]; |
2790 | 0 | const ggml_tensor * src1 = dst->src[1]; |
2791 | |
|
2792 | 0 | assert(ggml_is_contiguous_1(grad)); |
2793 | 0 | assert(ggml_is_contiguous_1(src1)); |
2794 | 0 | assert(ggml_is_contiguous_1(dst)); |
2795 | 0 | assert(ggml_are_same_shape(src1, dst)); |
2796 | 0 | assert(ggml_are_same_shape(src1, grad)); |
2797 | |
|
2798 | 0 | const int ith = params->ith; |
2799 | 0 | const int nth = params->nth; |
2800 | |
|
2801 | 0 | const int nc = src1->ne[0]; |
2802 | 0 | const int nr = ggml_nrows(src1); |
2803 | | |
2804 | | // rows per thread |
2805 | 0 | const int dr = (nr + nth - 1)/nth; |
2806 | | |
2807 | | // row range for this thread |
2808 | 0 | const int ir0 = dr*ith; |
2809 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2810 | |
|
2811 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
2812 | 0 | ggml_vec_silu_backward_f32(nc, |
2813 | 0 | (float *) ((char *) dst->data + i1*( dst->nb[1])), |
2814 | 0 | (float *) ((char *) src1->data + i1*(src1->nb[1])), |
2815 | 0 | (float *) ((char *) grad->data + i1*(grad->nb[1]))); |
2816 | |
|
2817 | | #ifndef NDEBUG |
2818 | | for (int k = 0; k < nc; k++) { |
2819 | | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
2820 | | GGML_UNUSED(x); |
2821 | | assert(!isnan(x)); |
2822 | | assert(!isinf(x)); |
2823 | | } |
2824 | | #endif // NDEBUG |
2825 | 0 | } |
2826 | 0 | } |
2827 | | |
2828 | | static void ggml_compute_forward_silu_back_f16( |
2829 | | const ggml_compute_params * params, |
2830 | 0 | ggml_tensor * dst) { |
2831 | |
|
2832 | 0 | const ggml_tensor * grad = dst->src[0]; |
2833 | 0 | const ggml_tensor * src1 = dst->src[1]; |
2834 | |
|
2835 | 0 | assert(ggml_is_contiguous_1(grad)); |
2836 | 0 | assert(ggml_is_contiguous_1(src1)); |
2837 | 0 | assert(ggml_is_contiguous_1(dst)); |
2838 | 0 | assert(ggml_are_same_shape(src1, dst)); |
2839 | 0 | assert(ggml_are_same_shape(src1, grad)); |
2840 | |
|
2841 | 0 | const int ith = params->ith; |
2842 | 0 | const int nth = params->nth; |
2843 | |
|
2844 | 0 | const int nc = src1->ne[0]; |
2845 | 0 | const int nr = ggml_nrows(src1); |
2846 | | |
2847 | | // rows per thread |
2848 | 0 | const int dr = (nr + nth - 1)/nth; |
2849 | | |
2850 | | // row range for this thread |
2851 | 0 | const int ir0 = dr*ith; |
2852 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2853 | |
|
2854 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
2855 | 0 | ggml_vec_silu_backward_f16(nc, |
2856 | 0 | (ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])), |
2857 | 0 | (ggml_fp16_t *) ((char *) src1->data + i1*(src1->nb[1])), |
2858 | 0 | (ggml_fp16_t *) ((char *) grad->data + i1*(grad->nb[1]))); |
2859 | |
|
2860 | | #ifndef NDEBUG |
2861 | | for (int k = 0; k < nc; k++) { |
2862 | | const float x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
2863 | | const float v = GGML_CPU_FP16_TO_FP32(x); |
2864 | | GGML_UNUSED(v); |
2865 | | assert(!isnan(v)); |
2866 | | assert(!isinf(v)); |
2867 | | } |
2868 | | #endif // NDEBUG |
2869 | 0 | } |
2870 | 0 | } |
2871 | | |
2872 | | void ggml_compute_forward_silu_back( |
2873 | | const ggml_compute_params * params, |
2874 | 0 | ggml_tensor * dst) { |
2875 | |
|
2876 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2877 | |
|
2878 | 0 | switch (src0->type) { |
2879 | 0 | case GGML_TYPE_F32: |
2880 | 0 | { |
2881 | 0 | ggml_compute_forward_silu_back_f32(params, dst); |
2882 | 0 | } break; |
2883 | 0 | case GGML_TYPE_F16: |
2884 | 0 | { |
2885 | 0 | ggml_compute_forward_silu_back_f16(params, dst); |
2886 | 0 | } break; |
2887 | 0 | default: |
2888 | 0 | { |
2889 | 0 | GGML_ABORT("fatal error"); |
2890 | 0 | } |
2891 | 0 | } |
2892 | 0 | } |
2893 | | |
2894 | | // ggml_compute_forward_reglu |
2895 | | |
2896 | | static void ggml_compute_forward_reglu_f32( |
2897 | | const ggml_compute_params * params, |
2898 | 0 | ggml_tensor * dst) { |
2899 | |
|
2900 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2901 | 0 | const ggml_tensor * src1 = dst->src[1]; |
2902 | 0 | char * src0_d = (char *) src0->data; |
2903 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
2904 | 0 | const size_t src0_o = src0->nb[1]; |
2905 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
2906 | |
|
2907 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
2908 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
2909 | |
|
2910 | 0 | if (src1) { |
2911 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
2912 | 0 | GGML_ASSERT(src0->type == src1->type); |
2913 | 0 | } |
2914 | |
|
2915 | 0 | const int ith = params->ith; |
2916 | 0 | const int nth = params->nth; |
2917 | |
|
2918 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
2919 | 0 | const int nr = ggml_nrows(src0); |
2920 | |
|
2921 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
2922 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
2923 | |
|
2924 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
2925 | | |
2926 | | // rows per thread |
2927 | 0 | const int dr = (nr + nth - 1)/nth; |
2928 | | |
2929 | | // row range for this thread |
2930 | 0 | const int ir0 = dr*ith; |
2931 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2932 | |
|
2933 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
2934 | 0 | float * src0_p = (float *) (src0_d + i1*src0_o); |
2935 | 0 | float * src1_p = (float *) (src1_d + i1*src1_o); |
2936 | |
|
2937 | 0 | if (!src1) { |
2938 | 0 | src0_p += swapped ? nc : 0; |
2939 | 0 | src1_p += swapped ? 0 : nc; |
2940 | 0 | } |
2941 | |
|
2942 | 0 | ggml_vec_reglu_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
2943 | |
|
2944 | | #ifndef NDEBUG |
2945 | | for (int k = 0; k < nc; k++) { |
2946 | | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
2947 | | GGML_UNUSED(x); |
2948 | | assert(!isnan(x)); |
2949 | | assert(!isinf(x)); |
2950 | | } |
2951 | | #endif // NDEBUG |
2952 | 0 | } |
2953 | 0 | } |
2954 | | |
2955 | | static void ggml_compute_forward_reglu_f16( |
2956 | | const ggml_compute_params * params, |
2957 | 0 | ggml_tensor * dst) { |
2958 | |
|
2959 | 0 | const ggml_tensor * src0 = dst->src[0]; |
2960 | 0 | const ggml_tensor * src1 = dst->src[1]; |
2961 | 0 | char * src0_d = (char *) src0->data; |
2962 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
2963 | 0 | const size_t src0_o = src0->nb[1]; |
2964 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
2965 | |
|
2966 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
2967 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
2968 | |
|
2969 | 0 | if (src1) { |
2970 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
2971 | 0 | GGML_ASSERT(src0->type == src1->type); |
2972 | 0 | } |
2973 | |
|
2974 | 0 | const int ith = params->ith; |
2975 | 0 | const int nth = params->nth; |
2976 | |
|
2977 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
2978 | 0 | const int nr = ggml_nrows(src0); |
2979 | |
|
2980 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
2981 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
2982 | |
|
2983 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
2984 | | |
2985 | | // rows per thread |
2986 | 0 | const int dr = (nr + nth - 1)/nth; |
2987 | | |
2988 | | // row range for this thread |
2989 | 0 | const int ir0 = dr*ith; |
2990 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
2991 | |
|
2992 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
2993 | 0 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
2994 | 0 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
2995 | |
|
2996 | 0 | if (!src1) { |
2997 | 0 | src0_p += swapped ? nc : 0; |
2998 | 0 | src1_p += swapped ? 0 : nc; |
2999 | 0 | } |
3000 | |
|
3001 | 0 | ggml_vec_reglu_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
3002 | |
|
3003 | | #ifndef NDEBUG |
3004 | | for (int k = 0; k < nc; k++) { |
3005 | | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
3006 | | const float v = GGML_FP16_TO_FP32(x); |
3007 | | GGML_UNUSED(v); |
3008 | | assert(!isnan(v)); |
3009 | | assert(!isinf(v)); |
3010 | | } |
3011 | | #endif // NDEBUG |
3012 | 0 | } |
3013 | 0 | } |
3014 | | |
3015 | | static void ggml_compute_forward_reglu( |
3016 | | const ggml_compute_params * params, |
3017 | 0 | ggml_tensor * dst) { |
3018 | |
|
3019 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3020 | |
|
3021 | 0 | switch (src0->type) { |
3022 | 0 | case GGML_TYPE_F32: |
3023 | 0 | { |
3024 | 0 | ggml_compute_forward_reglu_f32(params, dst); |
3025 | 0 | } break; |
3026 | 0 | case GGML_TYPE_F16: |
3027 | 0 | { |
3028 | 0 | ggml_compute_forward_reglu_f16(params, dst); |
3029 | 0 | } break; |
3030 | 0 | default: |
3031 | 0 | { |
3032 | 0 | GGML_ABORT("fatal error"); |
3033 | 0 | } |
3034 | 0 | } |
3035 | 0 | } |
3036 | | |
3037 | | // ggml_compute_forward_geglu |
3038 | | |
3039 | | static void ggml_compute_forward_geglu_f32( |
3040 | | const ggml_compute_params * params, |
3041 | 0 | ggml_tensor * dst) { |
3042 | |
|
3043 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3044 | 0 | const ggml_tensor * src1 = dst->src[1]; |
3045 | 0 | char * src0_d = (char *) src0->data; |
3046 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
3047 | 0 | const size_t src0_o = src0->nb[1]; |
3048 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
3049 | |
|
3050 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
3051 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
3052 | |
|
3053 | 0 | if (src1) { |
3054 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
3055 | 0 | GGML_ASSERT(src0->type == src1->type); |
3056 | 0 | } |
3057 | |
|
3058 | 0 | const int ith = params->ith; |
3059 | 0 | const int nth = params->nth; |
3060 | |
|
3061 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
3062 | 0 | const int nr = ggml_nrows(src0); |
3063 | |
|
3064 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
3065 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
3066 | |
|
3067 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
3068 | | |
3069 | | // rows per thread |
3070 | 0 | const int dr = (nr + nth - 1)/nth; |
3071 | | |
3072 | | // row range for this thread |
3073 | 0 | const int ir0 = dr*ith; |
3074 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
3075 | |
|
3076 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
3077 | 0 | float * src0_p = (float *) (src0_d + i1*src0_o); |
3078 | 0 | float * src1_p = (float *) (src1_d + i1*src1_o); |
3079 | |
|
3080 | 0 | if (!src1) { |
3081 | 0 | src0_p += swapped ? nc : 0; |
3082 | 0 | src1_p += swapped ? 0 : nc; |
3083 | 0 | } |
3084 | |
|
3085 | 0 | ggml_vec_geglu_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
3086 | |
|
3087 | | #ifndef NDEBUG |
3088 | | for (int k = 0; k < nc; k++) { |
3089 | | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
3090 | | GGML_UNUSED(x); |
3091 | | assert(!isnan(x)); |
3092 | | assert(!isinf(x)); |
3093 | | } |
3094 | | #endif // NDEBUG |
3095 | 0 | } |
3096 | 0 | } |
3097 | | |
3098 | | static void ggml_compute_forward_geglu_f16( |
3099 | | const ggml_compute_params * params, |
3100 | 0 | ggml_tensor * dst) { |
3101 | |
|
3102 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3103 | 0 | const ggml_tensor * src1 = dst->src[1]; |
3104 | 0 | char * src0_d = (char *) src0->data; |
3105 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
3106 | 0 | const size_t src0_o = src0->nb[1]; |
3107 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
3108 | |
|
3109 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
3110 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
3111 | |
|
3112 | 0 | if (src1) { |
3113 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
3114 | 0 | GGML_ASSERT(src0->type == src1->type); |
3115 | 0 | } |
3116 | |
|
3117 | 0 | const int ith = params->ith; |
3118 | 0 | const int nth = params->nth; |
3119 | |
|
3120 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
3121 | 0 | const int nr = ggml_nrows(src0); |
3122 | |
|
3123 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
3124 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
3125 | |
|
3126 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
3127 | | |
3128 | | // rows per thread |
3129 | 0 | const int dr = (nr + nth - 1)/nth; |
3130 | | |
3131 | | // row range for this thread |
3132 | 0 | const int ir0 = dr*ith; |
3133 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
3134 | |
|
3135 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
3136 | 0 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
3137 | 0 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
3138 | |
|
3139 | 0 | if (!src1) { |
3140 | 0 | src0_p += swapped ? nc : 0; |
3141 | 0 | src1_p += swapped ? 0 : nc; |
3142 | 0 | } |
3143 | |
|
3144 | 0 | ggml_vec_geglu_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
3145 | |
|
3146 | | #ifndef NDEBUG |
3147 | | for (int k = 0; k < nc; k++) { |
3148 | | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
3149 | | const float v = GGML_FP16_TO_FP32(x); |
3150 | | GGML_UNUSED(v); |
3151 | | assert(!isnan(v)); |
3152 | | assert(!isinf(v)); |
3153 | | } |
3154 | | #endif // NDEBUG |
3155 | 0 | } |
3156 | 0 | } |
3157 | | |
3158 | | static void ggml_compute_forward_geglu( |
3159 | | const ggml_compute_params * params, |
3160 | 0 | ggml_tensor * dst) { |
3161 | |
|
3162 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3163 | |
|
3164 | 0 | switch (src0->type) { |
3165 | 0 | case GGML_TYPE_F32: |
3166 | 0 | { |
3167 | 0 | ggml_compute_forward_geglu_f32(params, dst); |
3168 | 0 | } break; |
3169 | 0 | case GGML_TYPE_F16: |
3170 | 0 | { |
3171 | 0 | ggml_compute_forward_geglu_f16(params, dst); |
3172 | 0 | } break; |
3173 | 0 | default: |
3174 | 0 | { |
3175 | 0 | GGML_ABORT("fatal error"); |
3176 | 0 | } |
3177 | 0 | } |
3178 | 0 | } |
3179 | | |
3180 | | // ggml_compute_forward_swiglu |
3181 | | |
3182 | | static void ggml_compute_forward_swiglu_f32( |
3183 | | const ggml_compute_params * params, |
3184 | 0 | ggml_tensor * dst) { |
3185 | |
|
3186 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3187 | 0 | const ggml_tensor * src1 = dst->src[1]; |
3188 | 0 | char * src0_d = (char *) src0->data; |
3189 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
3190 | 0 | const size_t src0_o = src0->nb[1]; |
3191 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
3192 | |
|
3193 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
3194 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
3195 | |
|
3196 | 0 | if (src1) { |
3197 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
3198 | 0 | GGML_ASSERT(src0->type == src1->type); |
3199 | 0 | } |
3200 | |
|
3201 | 0 | const int ith = params->ith; |
3202 | 0 | const int nth = params->nth; |
3203 | |
|
3204 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
3205 | 0 | const int nr = ggml_nrows(src0); |
3206 | |
|
3207 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
3208 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
3209 | |
|
3210 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
3211 | | |
3212 | | // rows per thread |
3213 | 0 | const int dr = (nr + nth - 1)/nth; |
3214 | | |
3215 | | // row range for this thread |
3216 | 0 | const int ir0 = dr*ith; |
3217 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
3218 | |
|
3219 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
3220 | 0 | float * src0_p = (float *) (src0_d + i1*src0_o); |
3221 | 0 | float * src1_p = (float *) (src1_d + i1*src1_o); |
3222 | |
|
3223 | 0 | if (!src1) { |
3224 | 0 | src0_p += swapped ? nc : 0; |
3225 | 0 | src1_p += swapped ? 0 : nc; |
3226 | 0 | } |
3227 | |
|
3228 | 0 | ggml_vec_swiglu_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
3229 | |
|
3230 | | #ifndef NDEBUG |
3231 | | for (int k = 0; k < nc; k++) { |
3232 | | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
3233 | | GGML_UNUSED(x); |
3234 | | assert(!isnan(x)); |
3235 | | assert(!isinf(x)); |
3236 | | } |
3237 | | #endif // NDEBUG |
3238 | 0 | } |
3239 | 0 | } |
3240 | | |
3241 | | static void ggml_compute_forward_swiglu_f16( |
3242 | | const ggml_compute_params * params, |
3243 | 0 | ggml_tensor * dst) { |
3244 | |
|
3245 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3246 | 0 | const ggml_tensor * src1 = dst->src[1]; |
3247 | 0 | char * src0_d = (char *) src0->data; |
3248 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
3249 | 0 | const size_t src0_o = src0->nb[1]; |
3250 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
3251 | |
|
3252 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
3253 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
3254 | |
|
3255 | 0 | if (src1) { |
3256 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
3257 | 0 | GGML_ASSERT(src0->type == src1->type); |
3258 | 0 | } |
3259 | |
|
3260 | 0 | const int ith = params->ith; |
3261 | 0 | const int nth = params->nth; |
3262 | |
|
3263 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
3264 | 0 | const int nr = ggml_nrows(src0); |
3265 | |
|
3266 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
3267 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
3268 | |
|
3269 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
3270 | | |
3271 | | // rows per thread |
3272 | 0 | const int dr = (nr + nth - 1)/nth; |
3273 | | |
3274 | | // row range for this thread |
3275 | 0 | const int ir0 = dr*ith; |
3276 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
3277 | |
|
3278 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
3279 | 0 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
3280 | 0 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
3281 | |
|
3282 | 0 | if (!src1) { |
3283 | 0 | src0_p += swapped ? nc : 0; |
3284 | 0 | src1_p += swapped ? 0 : nc; |
3285 | 0 | } |
3286 | |
|
3287 | 0 | ggml_vec_swiglu_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
3288 | |
|
3289 | | #ifndef NDEBUG |
3290 | | for (int k = 0; k < nc; k++) { |
3291 | | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
3292 | | const float v = GGML_FP16_TO_FP32(x); |
3293 | | GGML_UNUSED(v); |
3294 | | assert(!isnan(v)); |
3295 | | assert(!isinf(v)); |
3296 | | } |
3297 | | #endif // NDEBUG |
3298 | 0 | } |
3299 | 0 | } |
3300 | | |
3301 | | static void ggml_compute_forward_swiglu( |
3302 | | const ggml_compute_params * params, |
3303 | 0 | ggml_tensor * dst) { |
3304 | |
|
3305 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3306 | |
|
3307 | 0 | switch (src0->type) { |
3308 | 0 | case GGML_TYPE_F32: |
3309 | 0 | { |
3310 | 0 | ggml_compute_forward_swiglu_f32(params, dst); |
3311 | 0 | } break; |
3312 | 0 | case GGML_TYPE_F16: |
3313 | 0 | { |
3314 | 0 | ggml_compute_forward_swiglu_f16(params, dst); |
3315 | 0 | } break; |
3316 | 0 | default: |
3317 | 0 | { |
3318 | 0 | GGML_ABORT("fatal error"); |
3319 | 0 | } |
3320 | 0 | } |
3321 | 0 | } |
3322 | | |
3323 | | // ggml_compute_forward_swiglu_oai |
3324 | | |
3325 | | static void ggml_compute_forward_swiglu_oai_f32( |
3326 | | const ggml_compute_params * params, |
3327 | 0 | ggml_tensor * dst) { |
3328 | |
|
3329 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3330 | 0 | const ggml_tensor * src1 = dst->src[1]; |
3331 | 0 | char * src0_d = (char *) src0->data; |
3332 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
3333 | 0 | const size_t src0_o = src0->nb[1]; |
3334 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
3335 | |
|
3336 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
3337 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
3338 | |
|
3339 | 0 | if (src1) { |
3340 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
3341 | 0 | GGML_ASSERT(src0->type == src1->type); |
3342 | 0 | } |
3343 | |
|
3344 | 0 | const int ith = params->ith; |
3345 | 0 | const int nth = params->nth; |
3346 | |
|
3347 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
3348 | 0 | const int nr = ggml_nrows(src0); |
3349 | |
|
3350 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
3351 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
3352 | |
|
3353 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
3354 | 0 | const float alpha = ggml_get_op_params_f32(dst, 2); |
3355 | 0 | const float limit = ggml_get_op_params_f32(dst, 3); |
3356 | | |
3357 | | // rows per thread |
3358 | 0 | const int dr = (nr + nth - 1)/nth; |
3359 | | |
3360 | | // row range for this thread |
3361 | 0 | const int ir0 = dr*ith; |
3362 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
3363 | |
|
3364 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
3365 | 0 | float * src0_p = (float *) (src0_d + i1*src0_o); |
3366 | 0 | float * src1_p = (float *) (src1_d + i1*src1_o); |
3367 | 0 | float * dst_p = (float *) ((char *) dst->data + i1*(dst->nb[1])); |
3368 | |
|
3369 | 0 | if (!src1) { |
3370 | 0 | src0_p += swapped ? nc : 0; |
3371 | 0 | src1_p += swapped ? 0 : nc; |
3372 | 0 | } |
3373 | |
|
3374 | 0 | for (int k = 0; k < nc; k++) { |
3375 | 0 | const float x = std::min(src0_p[k], limit); |
3376 | 0 | const float y = std::clamp(src1_p[k], -limit, limit); |
3377 | 0 | const float out_glu = x / (1.f + expf(alpha * (-x))); |
3378 | 0 | dst_p[k] = out_glu * (y + 1.f); |
3379 | 0 | } |
3380 | |
|
3381 | | #ifndef NDEBUG |
3382 | | for (int k = 0; k < nc; k++) { |
3383 | | const float x = dst_p[k]; |
3384 | | GGML_UNUSED(x); |
3385 | | assert(!isnan(x)); |
3386 | | assert(!isinf(x)); |
3387 | | } |
3388 | | #endif // NDEBUG |
3389 | 0 | } |
3390 | 0 | } |
3391 | | |
3392 | | static void ggml_compute_forward_swiglu_oai( |
3393 | | const ggml_compute_params * params, |
3394 | 0 | ggml_tensor * dst) { |
3395 | |
|
3396 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3397 | |
|
3398 | 0 | switch (src0->type) { |
3399 | 0 | case GGML_TYPE_F32: |
3400 | 0 | { |
3401 | 0 | ggml_compute_forward_swiglu_oai_f32(params, dst); |
3402 | 0 | } break; |
3403 | 0 | default: |
3404 | 0 | { |
3405 | 0 | GGML_ABORT("fatal error"); |
3406 | 0 | } |
3407 | 0 | } |
3408 | 0 | } |
3409 | | |
3410 | | // ggml_compute_forward_geglu_erf |
3411 | | |
3412 | | static void ggml_compute_forward_geglu_erf_f32( |
3413 | | const ggml_compute_params * params, |
3414 | 0 | ggml_tensor * dst) { |
3415 | |
|
3416 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3417 | 0 | const ggml_tensor * src1 = dst->src[1]; |
3418 | 0 | char * src0_d = (char *) src0->data; |
3419 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
3420 | 0 | const size_t src0_o = src0->nb[1]; |
3421 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
3422 | |
|
3423 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
3424 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
3425 | |
|
3426 | 0 | if (src1) { |
3427 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
3428 | 0 | GGML_ASSERT(src0->type == src1->type); |
3429 | 0 | } |
3430 | |
|
3431 | 0 | const int ith = params->ith; |
3432 | 0 | const int nth = params->nth; |
3433 | |
|
3434 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
3435 | 0 | const int nr = ggml_nrows(src0); |
3436 | |
|
3437 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
3438 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
3439 | |
|
3440 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
3441 | | |
3442 | | // rows per thread |
3443 | 0 | const int dr = (nr + nth - 1)/nth; |
3444 | | |
3445 | | // row range for this thread |
3446 | 0 | const int ir0 = dr*ith; |
3447 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
3448 | |
|
3449 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
3450 | 0 | float * src0_p = (float *) (src0_d + i1*src0_o); |
3451 | 0 | float * src1_p = (float *) (src1_d + i1*src1_o); |
3452 | |
|
3453 | 0 | if (!src1) { |
3454 | 0 | src0_p += swapped ? nc : 0; |
3455 | 0 | src1_p += swapped ? 0 : nc; |
3456 | 0 | } |
3457 | |
|
3458 | 0 | ggml_vec_geglu_erf_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
3459 | |
|
3460 | | #ifndef NDEBUG |
3461 | | for (int k = 0; k < nc; k++) { |
3462 | | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
3463 | | GGML_UNUSED(x); |
3464 | | assert(!isnan(x)); |
3465 | | assert(!isinf(x)); |
3466 | | } |
3467 | | #endif // NDEBUG |
3468 | 0 | } |
3469 | 0 | } |
3470 | | |
3471 | | static void ggml_compute_forward_geglu_erf_f16( |
3472 | | const ggml_compute_params * params, |
3473 | 0 | ggml_tensor * dst) { |
3474 | |
|
3475 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3476 | 0 | const ggml_tensor * src1 = dst->src[1]; |
3477 | 0 | char * src0_d = (char *) src0->data; |
3478 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
3479 | 0 | const size_t src0_o = src0->nb[1]; |
3480 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
3481 | |
|
3482 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
3483 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
3484 | |
|
3485 | 0 | if (src1) { |
3486 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
3487 | 0 | GGML_ASSERT(src0->type == src1->type); |
3488 | 0 | } |
3489 | |
|
3490 | 0 | const int ith = params->ith; |
3491 | 0 | const int nth = params->nth; |
3492 | |
|
3493 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
3494 | 0 | const int nr = ggml_nrows(src0); |
3495 | |
|
3496 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
3497 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
3498 | |
|
3499 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
3500 | | |
3501 | | // rows per thread |
3502 | 0 | const int dr = (nr + nth - 1)/nth; |
3503 | | |
3504 | | // row range for this thread |
3505 | 0 | const int ir0 = dr*ith; |
3506 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
3507 | |
|
3508 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
3509 | 0 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
3510 | 0 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
3511 | |
|
3512 | 0 | if (!src1) { |
3513 | 0 | src0_p += swapped ? nc : 0; |
3514 | 0 | src1_p += swapped ? 0 : nc; |
3515 | 0 | } |
3516 | |
|
3517 | 0 | ggml_vec_geglu_erf_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
3518 | |
|
3519 | | #ifndef NDEBUG |
3520 | | for (int k = 0; k < nc; k++) { |
3521 | | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
3522 | | const float v = GGML_FP16_TO_FP32(x); |
3523 | | GGML_UNUSED(v); |
3524 | | assert(!isnan(v)); |
3525 | | assert(!isinf(v)); |
3526 | | } |
3527 | | #endif // NDEBUG |
3528 | 0 | } |
3529 | 0 | } |
3530 | | |
3531 | | static void ggml_compute_forward_geglu_erf( |
3532 | | const ggml_compute_params * params, |
3533 | 0 | ggml_tensor * dst) { |
3534 | |
|
3535 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3536 | |
|
3537 | 0 | switch (src0->type) { |
3538 | 0 | case GGML_TYPE_F32: |
3539 | 0 | { |
3540 | 0 | ggml_compute_forward_geglu_erf_f32(params, dst); |
3541 | 0 | } break; |
3542 | 0 | case GGML_TYPE_F16: |
3543 | 0 | { |
3544 | 0 | ggml_compute_forward_geglu_erf_f16(params, dst); |
3545 | 0 | } break; |
3546 | 0 | default: |
3547 | 0 | { |
3548 | 0 | GGML_ABORT("fatal error"); |
3549 | 0 | } |
3550 | 0 | } |
3551 | 0 | } |
3552 | | |
3553 | | // ggml_compute_forward_geglu_quick |
3554 | | |
3555 | | static void ggml_compute_forward_geglu_quick_f32( |
3556 | | const ggml_compute_params * params, |
3557 | 0 | ggml_tensor * dst) { |
3558 | |
|
3559 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3560 | 0 | const ggml_tensor * src1 = dst->src[1]; |
3561 | 0 | char * src0_d = (char *) src0->data; |
3562 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
3563 | 0 | const size_t src0_o = src0->nb[1]; |
3564 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
3565 | |
|
3566 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
3567 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
3568 | |
|
3569 | 0 | if (src1) { |
3570 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
3571 | 0 | GGML_ASSERT(src0->type == src1->type); |
3572 | 0 | } |
3573 | |
|
3574 | 0 | const int ith = params->ith; |
3575 | 0 | const int nth = params->nth; |
3576 | |
|
3577 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
3578 | 0 | const int nr = ggml_nrows(src0); |
3579 | |
|
3580 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
3581 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
3582 | |
|
3583 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
3584 | | |
3585 | | // rows per thread |
3586 | 0 | const int dr = (nr + nth - 1)/nth; |
3587 | | |
3588 | | // row range for this thread |
3589 | 0 | const int ir0 = dr*ith; |
3590 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
3591 | |
|
3592 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
3593 | 0 | float * src0_p = (float *) (src0_d + i1*src0_o); |
3594 | 0 | float * src1_p = (float *) (src1_d + i1*src1_o); |
3595 | |
|
3596 | 0 | if (!src1) { |
3597 | 0 | src0_p += swapped ? nc : 0; |
3598 | 0 | src1_p += swapped ? 0 : nc; |
3599 | 0 | } |
3600 | |
|
3601 | 0 | ggml_vec_geglu_quick_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
3602 | |
|
3603 | | #ifndef NDEBUG |
3604 | | for (int k = 0; k < nc; k++) { |
3605 | | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
3606 | | GGML_UNUSED(x); |
3607 | | assert(!isnan(x)); |
3608 | | assert(!isinf(x)); |
3609 | | } |
3610 | | #endif // NDEBUG |
3611 | 0 | } |
3612 | 0 | } |
3613 | | |
3614 | | static void ggml_compute_forward_geglu_quick_f16( |
3615 | | const ggml_compute_params * params, |
3616 | 0 | ggml_tensor * dst) { |
3617 | |
|
3618 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3619 | 0 | const ggml_tensor * src1 = dst->src[1]; |
3620 | 0 | char * src0_d = (char *) src0->data; |
3621 | 0 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
3622 | 0 | const size_t src0_o = src0->nb[1]; |
3623 | 0 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
3624 | |
|
3625 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
3626 | 0 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
3627 | |
|
3628 | 0 | if (src1) { |
3629 | 0 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
3630 | 0 | GGML_ASSERT(src0->type == src1->type); |
3631 | 0 | } |
3632 | |
|
3633 | 0 | const int ith = params->ith; |
3634 | 0 | const int nth = params->nth; |
3635 | |
|
3636 | 0 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
3637 | 0 | const int nr = ggml_nrows(src0); |
3638 | |
|
3639 | 0 | GGML_ASSERT(dst->ne[0] == nc); |
3640 | 0 | GGML_ASSERT(ggml_nrows(dst) == nr); |
3641 | |
|
3642 | 0 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
3643 | | |
3644 | | // rows per thread |
3645 | 0 | const int dr = (nr + nth - 1)/nth; |
3646 | | |
3647 | | // row range for this thread |
3648 | 0 | const int ir0 = dr*ith; |
3649 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
3650 | |
|
3651 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
3652 | 0 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
3653 | 0 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
3654 | |
|
3655 | 0 | if (!src1) { |
3656 | 0 | src0_p += swapped ? nc : 0; |
3657 | 0 | src1_p += swapped ? 0 : nc; |
3658 | 0 | } |
3659 | |
|
3660 | 0 | ggml_vec_geglu_quick_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
3661 | |
|
3662 | | #ifndef NDEBUG |
3663 | | for (int k = 0; k < nc; k++) { |
3664 | | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
3665 | | const float v = GGML_FP16_TO_FP32(x); |
3666 | | GGML_UNUSED(v); |
3667 | | assert(!isnan(v)); |
3668 | | assert(!isinf(v)); |
3669 | | } |
3670 | | #endif // NDEBUG |
3671 | 0 | } |
3672 | 0 | } |
3673 | | |
3674 | | static void ggml_compute_forward_geglu_quick( |
3675 | | const ggml_compute_params * params, |
3676 | 0 | ggml_tensor * dst) { |
3677 | |
|
3678 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3679 | |
|
3680 | 0 | switch (src0->type) { |
3681 | 0 | case GGML_TYPE_F32: |
3682 | 0 | { |
3683 | 0 | ggml_compute_forward_geglu_quick_f32(params, dst); |
3684 | 0 | } break; |
3685 | 0 | case GGML_TYPE_F16: |
3686 | 0 | { |
3687 | 0 | ggml_compute_forward_geglu_quick_f16(params, dst); |
3688 | 0 | } break; |
3689 | 0 | default: |
3690 | 0 | { |
3691 | 0 | GGML_ABORT("fatal error"); |
3692 | 0 | } |
3693 | 0 | } |
3694 | 0 | } |
3695 | | |
3696 | | // ggml_compute_forward_norm |
3697 | | |
3698 | | static void ggml_compute_forward_norm_f32( |
3699 | | const ggml_compute_params * params, |
3700 | 0 | ggml_tensor * dst) { |
3701 | |
|
3702 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3703 | |
|
3704 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
3705 | |
|
3706 | 0 | const int ith = params->ith; |
3707 | 0 | const int nth = params->nth; |
3708 | |
|
3709 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
3710 | |
|
3711 | 0 | float eps; |
3712 | 0 | memcpy(&eps, dst->op_params, sizeof(float)); |
3713 | |
|
3714 | 0 | GGML_ASSERT(eps >= 0.0f); |
3715 | |
|
3716 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
3717 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
3718 | 0 | for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
3719 | 0 | const char * x = (const char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03; |
3720 | 0 | char * y = (char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3; |
3721 | |
|
3722 | 0 | if (nb00 == sizeof(float) && nb0 == sizeof(float)) { |
3723 | 0 | const float * xf = (const float *) x; |
3724 | |
|
3725 | 0 | float sum = 0.0; |
3726 | 0 | ggml_vec_sum_f32(ne00, &sum, xf); |
3727 | 0 | float mean = sum/ne00; |
3728 | |
|
3729 | 0 | float * yf = (float *) y; |
3730 | 0 | float variance = 0; |
3731 | |
|
3732 | | #ifdef GGML_USE_ACCELERATE |
3733 | | mean = -mean; |
3734 | | vDSP_vsadd(xf, 1, &mean, yf, 1, ne00); |
3735 | | vDSP_measqv(yf, 1, &variance, ne00); |
3736 | | #else |
3737 | 0 | variance = ggml_vec_cvar_f32(ne00, yf, xf, mean); |
3738 | 0 | #endif //GGML_USE_ACCELERATE |
3739 | |
|
3740 | 0 | const float scale = 1.0f/sqrtf(variance + eps); |
3741 | 0 | ggml_vec_scale_f32(ne00, yf, scale); |
3742 | 0 | } else { |
3743 | 0 | float sum = 0.0; |
3744 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
3745 | 0 | sum += *(const float *) (x + i00*nb00); |
3746 | 0 | } |
3747 | 0 | const float mean = sum/ne00; |
3748 | |
|
3749 | 0 | float variance = 0.0f; |
3750 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
3751 | 0 | const float v = *(const float *) (x + i00*nb00) - mean; |
3752 | 0 | *(float *) (y + i00*nb0) = v; |
3753 | 0 | variance += v * v; |
3754 | 0 | } |
3755 | 0 | variance /= ne00; |
3756 | |
|
3757 | 0 | const float scale = 1.0f/sqrtf(variance + eps); |
3758 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
3759 | 0 | *(float *) (y + i00*nb0) *= scale; |
3760 | 0 | } |
3761 | 0 | } |
3762 | 0 | } |
3763 | 0 | } |
3764 | 0 | } |
3765 | 0 | } |
3766 | | |
3767 | | void ggml_compute_forward_norm( |
3768 | | const ggml_compute_params * params, |
3769 | 0 | ggml_tensor * dst) { |
3770 | |
|
3771 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3772 | |
|
3773 | 0 | switch (src0->type) { |
3774 | 0 | case GGML_TYPE_F32: |
3775 | 0 | { |
3776 | 0 | ggml_compute_forward_norm_f32(params, dst); |
3777 | 0 | } break; |
3778 | 0 | default: |
3779 | 0 | { |
3780 | 0 | GGML_ABORT("fatal error"); |
3781 | 0 | } |
3782 | 0 | } |
3783 | 0 | } |
3784 | | |
3785 | | // ggml_compute_forward_group_rms_norm |
3786 | | |
3787 | | // fusion kinds that can be combined with the rms_norm computation in a single pass. |
3788 | | // extend this enum when adding new fused variants (e.g. FUSE_ADD, FUSE_MUL_ADD, ...). |
3789 | | enum ggml_rms_norm_fuse_op { |
3790 | | GGML_RMS_NORM_FUSE_OP_NONE, |
3791 | | GGML_RMS_NORM_FUSE_OP_MUL, |
3792 | | }; |
3793 | | |
3794 | | template <ggml_rms_norm_fuse_op FUSE_OP> |
3795 | | static void ggml_compute_forward_rms_norm_f32( |
3796 | | const ggml_compute_params * params, |
3797 | | ggml_tensor * dst_rms_norm, |
3798 | 0 | ggml_tensor * dst_fused = nullptr) { |
3799 | |
|
3800 | 0 | const ggml_tensor * src0 = dst_rms_norm->src[0]; |
3801 | 0 | const ggml_tensor * src1 = nullptr; |
3802 | 0 | ggml_tensor * dst = dst_rms_norm; |
3803 | |
|
3804 | 0 | if constexpr (FUSE_OP == GGML_RMS_NORM_FUSE_OP_MUL) { |
3805 | 0 | src1 = (dst_fused->src[0] == dst_rms_norm) ? dst_fused->src[1] : dst_fused->src[0]; |
3806 | 0 | dst = dst_fused; |
3807 | 0 | } |
3808 | |
|
3809 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
3810 | |
|
3811 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
3812 | |
|
3813 | 0 | const int ith = params->ith; |
3814 | 0 | const int nth = params->nth; |
3815 | |
|
3816 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
3817 | |
|
3818 | 0 | float eps; |
3819 | 0 | memcpy(&eps, dst_rms_norm->op_params, sizeof(float)); |
3820 | 0 | GGML_ASSERT(eps >= 0.0f); |
3821 | | |
3822 | | // TODO: optimize |
3823 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
3824 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
3825 | 0 | for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
3826 | 0 | const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
3827 | |
|
3828 | 0 | ggml_float sum = 0.0; |
3829 | | // worth switching to explicit SIMD? |
3830 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
3831 | 0 | sum += (ggml_float)(x[i00] * x[i00]); |
3832 | 0 | } |
3833 | |
|
3834 | 0 | const float mean = sum/ne00; |
3835 | 0 | const float scale = 1.0f/sqrtf(mean + eps); |
3836 | | |
3837 | | // if you hit this, likely you got an inf somewhere earlier |
3838 | 0 | assert(scale > 0.0f); |
3839 | |
|
3840 | 0 | float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
3841 | |
|
3842 | 0 | if constexpr (FUSE_OP == GGML_RMS_NORM_FUSE_OP_MUL) { |
3843 | 0 | const int64_t i11 = i01 % ne11; |
3844 | 0 | const int64_t i12 = i02 % ne12; |
3845 | 0 | const int64_t i13 = i03 % ne13; |
3846 | 0 | const float * w = (float *) ((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13); |
3847 | |
|
3848 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
3849 | 0 | y[i00] = x[i00] * scale * w[i00]; |
3850 | 0 | } |
3851 | 0 | } else { |
3852 | 0 | memcpy(y, x, ne00 * sizeof(float)); |
3853 | 0 | ggml_vec_scale_f32(ne00, y, scale); |
3854 | 0 | } |
3855 | 0 | } |
3856 | 0 | } |
3857 | 0 | } |
3858 | 0 | } Unexecuted instantiation: ops.cpp:void ggml_compute_forward_rms_norm_f32<(ggml_rms_norm_fuse_op)0>(ggml_compute_params const*, ggml_tensor*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_rms_norm_f32<(ggml_rms_norm_fuse_op)1>(ggml_compute_params const*, ggml_tensor*, ggml_tensor*) |
3859 | | |
3860 | | void ggml_compute_forward_rms_norm( |
3861 | | const ggml_compute_params * params, |
3862 | 0 | ggml_tensor * dst) { |
3863 | |
|
3864 | 0 | const ggml_tensor * src0 = dst->src[0]; |
3865 | |
|
3866 | 0 | switch (src0->type) { |
3867 | 0 | case GGML_TYPE_F32: |
3868 | 0 | { |
3869 | 0 | ggml_compute_forward_rms_norm_f32<GGML_RMS_NORM_FUSE_OP_NONE>(params, dst); |
3870 | 0 | } break; |
3871 | 0 | default: |
3872 | 0 | { |
3873 | 0 | GGML_ABORT("fatal error"); |
3874 | 0 | } |
3875 | 0 | } |
3876 | 0 | } |
3877 | | |
3878 | | // Fused RMS_NORM + MUL: computes dst = rms_norm(src0) * src1 in a single pass. |
3879 | | // This avoids materializing the intermediate rms_norm result in memory. |
3880 | | void ggml_compute_forward_rms_norm_mul_fused( |
3881 | | const ggml_compute_params * params, |
3882 | | ggml_tensor * dst_rms_norm, |
3883 | 0 | ggml_tensor * dst_mul) { |
3884 | |
|
3885 | 0 | GGML_ASSERT(dst_mul != nullptr); |
3886 | 0 | GGML_ASSERT(dst_mul->src[0] == dst_rms_norm || dst_mul->src[1] == dst_rms_norm); |
3887 | |
|
3888 | 0 | const ggml_tensor * src0 = dst_rms_norm->src[0]; |
3889 | |
|
3890 | 0 | switch (src0->type) { |
3891 | 0 | case GGML_TYPE_F32: |
3892 | 0 | { |
3893 | 0 | ggml_compute_forward_rms_norm_f32<GGML_RMS_NORM_FUSE_OP_MUL>(params, dst_rms_norm, dst_mul); |
3894 | 0 | } break; |
3895 | 0 | default: |
3896 | 0 | { |
3897 | 0 | GGML_ABORT("fatal error"); |
3898 | 0 | } |
3899 | 0 | } |
3900 | 0 | } |
3901 | | |
3902 | | static void ggml_compute_forward_rms_norm_back_f32( |
3903 | | const ggml_compute_params * params, |
3904 | 0 | ggml_tensor * dst) { |
3905 | |
|
3906 | 0 | const ggml_tensor * src0 = dst->src[0]; // gradients from forward pass output |
3907 | 0 | const ggml_tensor * src1 = dst->src[1]; // src1 from forward pass |
3908 | |
|
3909 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst) && ggml_are_same_shape(src0, src1)); |
3910 | |
|
3911 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
3912 | 0 | GGML_ASSERT(src1->nb[0] == sizeof(float)); |
3913 | |
|
3914 | 0 | const int ith = params->ith; |
3915 | 0 | const int nth = params->nth; |
3916 | |
|
3917 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
3918 | |
|
3919 | 0 | float eps; |
3920 | 0 | memcpy(&eps, dst->op_params, sizeof(float)); |
3921 | | |
3922 | | // TODO: optimize |
3923 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
3924 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
3925 | 0 | for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
3926 | | // src1 is same shape as src0 => same indices |
3927 | 0 | const int64_t i11 = i01; |
3928 | 0 | const int64_t i12 = i02; |
3929 | 0 | const int64_t i13 = i03; |
3930 | |
|
3931 | 0 | const float * dz = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
3932 | 0 | const float * x = (float *) ((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13); |
3933 | |
|
3934 | 0 | ggml_float sum_xx = 0.0; |
3935 | 0 | ggml_float sum_xdz = 0.0; |
3936 | |
|
3937 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
3938 | 0 | sum_xx += (ggml_float)(x[i00] * x[i00]); |
3939 | 0 | sum_xdz += (ggml_float)(x[i00] * dz[i00]); |
3940 | 0 | } |
3941 | | |
3942 | | //const float mean = (float)(sum_xx)/ne00; |
3943 | 0 | const float mean_eps = (float)(sum_xx)/ne00 + eps; |
3944 | 0 | const float sum_eps = (float)(sum_xx) + eps*ne00; |
3945 | | //const float mean_xdz = (float)(sum_xdz)/ne00; |
3946 | | // we could cache rms from forward pass to improve performance. |
3947 | | // to do this implement ggml_rms and compose ggml_rms_norm using ggml_rms. |
3948 | | //const float rms = sqrtf(mean_eps); |
3949 | 0 | const float rrms = 1.0f / sqrtf(mean_eps); |
3950 | | //const float scale = -rrms/(ne00 * mean_eps); // -1/(n*rms**3) |
3951 | |
|
3952 | 0 | { |
3953 | | // z = rms_norm(x) |
3954 | | // |
3955 | | // rms_norm(src1) = |
3956 | | // scale( |
3957 | | // src1, |
3958 | | // div( |
3959 | | // 1, |
3960 | | // sqrt( |
3961 | | // add( |
3962 | | // scale( |
3963 | | // sum( |
3964 | | // sqr( |
3965 | | // src1)), |
3966 | | // (1.0/N)), |
3967 | | // eps)))); |
3968 | | |
3969 | | // postorder: |
3970 | | // ## op args grad |
3971 | | // 00 param src1 grad[#00] |
3972 | | // 01 const 1 |
3973 | | // 02 sqr (#00) grad[#02] |
3974 | | // 03 sum (#02) grad[#03] |
3975 | | // 04 const 1/N |
3976 | | // 05 scale (#03, #04) grad[#05] |
3977 | | // 06 const eps |
3978 | | // 07 add (#05, #06) grad[#07] |
3979 | | // 08 sqrt (#07) grad[#08] |
3980 | | // 09 div (#01,#08) grad[#09] |
3981 | | // 10 scale (#00,#09) grad[#10] |
3982 | | // |
3983 | | // backward pass, given grad[#10] |
3984 | | // #10: scale |
3985 | | // grad[#00] += scale(grad[#10],#09) |
3986 | | // grad[#09] += sum(mul(grad[#10],#00)) |
3987 | | // #09: div |
3988 | | // grad[#08] += neg(mul(grad[#09], div(#09,#08))) |
3989 | | // #08: sqrt |
3990 | | // grad[#07] += mul(grad[#08], div(0.5, #08)) |
3991 | | // #07: add |
3992 | | // grad[#05] += grad[#07] |
3993 | | // #05: scale |
3994 | | // grad[#03] += scale(grad[#05],#04) |
3995 | | // #03: sum |
3996 | | // grad[#02] += repeat(grad[#03], #02) |
3997 | | // #02: |
3998 | | // grad[#00] += scale(mul(#00, grad[#02]), 2.0) |
3999 | | // |
4000 | | // substitute and simplify: |
4001 | | // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0) |
4002 | | // grad[#02] = repeat(grad[#03], #02) |
4003 | | // grad[#02] = repeat(scale(grad[#05],#04), #02) |
4004 | | // grad[#02] = repeat(scale(grad[#07],#04), #02) |
4005 | | // grad[#02] = repeat(scale(mul(grad[#08], div(0.5, #08)),#04), #02) |
4006 | | // grad[#02] = repeat(scale(mul(neg(mul(grad[#09], div(#09,#08))), div(0.5, #08)),#04), #02) |
4007 | | // grad[#02] = repeat(scale(mul(neg(mul(sum(mul(grad[#10],#00)), div(#09,#08))), div(0.5, #08)),#04), #02) |
4008 | | // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(#09,#08) * div(0.5, #08) * (1/N)), #02) |
4009 | | // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(div(#01,#08),#08) * div(0.5, #08) * (1/N)), #02) |
4010 | | // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#08*#08) * div(0.5, #08) * (1/N)), #02) |
4011 | | // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02) |
4012 | | // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0) |
4013 | | // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02)), 2.0) |
4014 | | // grad[#00] = scale(grad(#10), #09) + scale(scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N))), 2.0) |
4015 | | // grad[#00] = scale(grad(#10), #09) + scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(1,#08) * (1/N))) |
4016 | | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N)) |
4017 | | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N)) |
4018 | | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,mean_eps*rms) * (-1/N)) |
4019 | | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*mean_eps)) |
4020 | | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*(sum_xx/N+eps))) |
4021 | | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*sum_xx+rms*N*eps)) |
4022 | | // grad[#00] = scale(dz, rrms) + scale(x, sum(mul(dz,x)) * div(-1,rms*N*mean_eps)) |
4023 | | // grad[#00] = scale(dz, rrms) + scale(x, sum_xdz * div(-1,rms*N*mean_eps)) |
4024 | | // a = b*c + d*e |
4025 | | // a = b*c*f/f + d*e*f/f |
4026 | | // a = (b*c*f + d*e*f)*(1/f) |
4027 | | // a = (b*c*(1/c) + d*e*(1/c))*(1/(1/c)) |
4028 | | // a = (b + d*e/c)*c |
4029 | | // b = dz, c = rrms, d = x, e = sum_xdz * div(-1,rms*N*mean_eps) |
4030 | | // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)/rrms)*rrms |
4031 | | // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)*rms)*rrms |
4032 | | // a = (dz + x*sum_xdz * div(-rms,rms*N*mean_eps))*rrms |
4033 | | // a = (dz + x*sum_xdz * div(-1,N*mean_eps))*rrms |
4034 | | // a = (dz + x*div(-sum_xdz,N*mean_eps))*rrms |
4035 | | // a = (dz + x*div(-mean_xdz,mean_eps))*rrms |
4036 | | // grad[#00] = scale(dz + scale(x, div(-mean_xdz,mean_eps)),rrms) |
4037 | | // grad[#00] = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) |
4038 | | // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) |
4039 | 0 | } |
4040 | | // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) |
4041 | | // post-order: |
4042 | | // dx := x |
4043 | | // dx := scale(dx,-mean_xdz/mean_eps) |
4044 | | // dx := add(dx, dz) |
4045 | | // dx := scale(dx, rrms) |
4046 | 0 | float * dx = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
4047 | | |
4048 | | // dx[i00] = (dz + x*(-sum_xdz/sum_eps)) * rrms |
4049 | | // note: https://github.com/ggml-org/ggml/issues/1491 |
4050 | 0 | const float scale_x = (float) (-sum_xdz) / sum_eps; |
4051 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
4052 | 0 | dx[i00] = (dz[i00] + x[i00] * scale_x) * rrms; |
4053 | 0 | } |
4054 | 0 | } |
4055 | 0 | } |
4056 | 0 | } |
4057 | 0 | } |
4058 | | |
4059 | | void ggml_compute_forward_rms_norm_back( |
4060 | | const ggml_compute_params * params, |
4061 | 0 | ggml_tensor * dst) { |
4062 | |
|
4063 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4064 | |
|
4065 | 0 | switch (src0->type) { |
4066 | 0 | case GGML_TYPE_F32: |
4067 | 0 | { |
4068 | 0 | ggml_compute_forward_rms_norm_back_f32(params, dst); |
4069 | 0 | } break; |
4070 | 0 | default: |
4071 | 0 | { |
4072 | 0 | GGML_ABORT("fatal error"); |
4073 | 0 | } |
4074 | 0 | } |
4075 | 0 | } |
4076 | | |
4077 | | // ggml_compute_forward_group_norm |
4078 | | |
4079 | | static void ggml_compute_forward_group_norm_f32( |
4080 | | const ggml_compute_params * params, |
4081 | 0 | ggml_tensor * dst) { |
4082 | |
|
4083 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4084 | |
|
4085 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
4086 | |
|
4087 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
4088 | |
|
4089 | 0 | const int ith = params->ith; |
4090 | 0 | const int nth = params->nth; |
4091 | |
|
4092 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
4093 | | |
4094 | | // TODO: optimize |
4095 | |
|
4096 | 0 | float eps; |
4097 | 0 | memcpy(&eps, dst->op_params + 1, sizeof(float)); |
4098 | |
|
4099 | 0 | int n_channels = src0->ne[2]; |
4100 | 0 | int n_groups = dst->op_params[0]; |
4101 | 0 | int n_channels_per_group = (n_channels + n_groups - 1) / n_groups; |
4102 | 0 | for (int i = ith; i < n_groups; i += nth) { |
4103 | 0 | int start = i * n_channels_per_group; |
4104 | 0 | int end = start + n_channels_per_group; |
4105 | 0 | if (end > n_channels) { |
4106 | 0 | end = n_channels; |
4107 | 0 | } |
4108 | 0 | int step = end - start; |
4109 | |
|
4110 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
4111 | 0 | ggml_float sum = 0.0; |
4112 | 0 | for (int64_t i02 = start; i02 < end; i02++) { |
4113 | 0 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
4114 | 0 | const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03); |
4115 | |
|
4116 | 0 | ggml_float sumr = 0.0; |
4117 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
4118 | 0 | sumr += (ggml_float)x[i00]; |
4119 | 0 | } |
4120 | 0 | sum += sumr; |
4121 | 0 | } |
4122 | 0 | } |
4123 | 0 | const float mean = sum / (ne00 * ne01 * step); |
4124 | |
|
4125 | 0 | ggml_float sum2 = 0.0; |
4126 | 0 | for (int64_t i02 = start; i02 < end; i02++) { |
4127 | 0 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
4128 | 0 | const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03); |
4129 | |
|
4130 | 0 | float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3); |
4131 | |
|
4132 | 0 | ggml_float sumr = 0.0; |
4133 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
4134 | 0 | float v = x[i00] - mean; |
4135 | 0 | y[i00] = v; |
4136 | 0 | sumr += (ggml_float)(v * v); |
4137 | 0 | } |
4138 | 0 | sum2 += sumr; |
4139 | 0 | } |
4140 | 0 | } |
4141 | 0 | const float variance = sum2 / (ne00 * ne01 * step); |
4142 | 0 | const float scale = 1.0f / sqrtf(variance + eps); |
4143 | |
|
4144 | 0 | for (int64_t i02 = start; i02 < end; i02++) { |
4145 | 0 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
4146 | 0 | float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3); |
4147 | 0 | ggml_vec_scale_f32(ne00, y, scale); |
4148 | 0 | } |
4149 | 0 | } |
4150 | 0 | } |
4151 | 0 | } |
4152 | 0 | } |
4153 | | |
4154 | | void ggml_compute_forward_group_norm( |
4155 | | const ggml_compute_params * params, |
4156 | 0 | ggml_tensor * dst) { |
4157 | |
|
4158 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4159 | |
|
4160 | 0 | switch (src0->type) { |
4161 | 0 | case GGML_TYPE_F32: |
4162 | 0 | { |
4163 | 0 | ggml_compute_forward_group_norm_f32(params, dst); |
4164 | 0 | } break; |
4165 | 0 | default: |
4166 | 0 | { |
4167 | 0 | GGML_ABORT("fatal error"); |
4168 | 0 | } |
4169 | 0 | } |
4170 | 0 | } |
4171 | | |
4172 | | // ggml_compute_forward_l2_norm |
4173 | | |
4174 | | static void ggml_compute_forward_l2_norm_f32( |
4175 | | const ggml_compute_params * params, |
4176 | 0 | ggml_tensor * dst) { |
4177 | |
|
4178 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4179 | |
|
4180 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
4181 | |
|
4182 | 0 | const int ith = params->ith; |
4183 | 0 | const int nth = params->nth; |
4184 | |
|
4185 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
4186 | |
|
4187 | 0 | float eps; |
4188 | 0 | memcpy(&eps, dst->op_params, sizeof(float)); |
4189 | |
|
4190 | 0 | GGML_ASSERT(eps >= 0.0f); |
4191 | | |
4192 | | // TODO: optimize |
4193 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
4194 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
4195 | 0 | for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
4196 | 0 | const char * x = (const char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03; |
4197 | |
|
4198 | 0 | ggml_float sum = 0.0; |
4199 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
4200 | 0 | const float xi = *(const float *) (x + i00*nb00); |
4201 | 0 | sum += (ggml_float)(xi * xi); |
4202 | 0 | } |
4203 | |
|
4204 | 0 | const float scale = 1.0f/fmaxf(sqrtf(sum), eps); |
4205 | |
|
4206 | 0 | char * y = (char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3; |
4207 | |
|
4208 | 0 | if (nb00 == sizeof(float) && nb0 == sizeof(float)) { |
4209 | 0 | memcpy(y, x, ne00 * sizeof(float)); |
4210 | 0 | ggml_vec_scale_f32(ne00, (float *) y, scale); |
4211 | 0 | } else { |
4212 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
4213 | 0 | const float xi = *(const float *) (x + i00*nb00); |
4214 | 0 | *(float *) (y + i00*nb0) = xi * scale; |
4215 | 0 | } |
4216 | 0 | } |
4217 | 0 | } |
4218 | 0 | } |
4219 | 0 | } |
4220 | 0 | } |
4221 | | |
4222 | | void ggml_compute_forward_l2_norm( |
4223 | | const ggml_compute_params * params, |
4224 | 0 | ggml_tensor * dst) { |
4225 | |
|
4226 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4227 | |
|
4228 | 0 | switch (src0->type) { |
4229 | 0 | case GGML_TYPE_F32: |
4230 | 0 | { |
4231 | 0 | ggml_compute_forward_l2_norm_f32(params, dst); |
4232 | 0 | } break; |
4233 | 0 | default: |
4234 | 0 | { |
4235 | 0 | GGML_ABORT("fatal error"); |
4236 | 0 | } |
4237 | 0 | } |
4238 | 0 | } |
4239 | | |
4240 | | // ggml_compute_forward_out_prod |
4241 | | |
4242 | | static void ggml_compute_forward_out_prod_f32( |
4243 | | const ggml_compute_params * params, |
4244 | 0 | ggml_tensor * dst) { |
4245 | |
|
4246 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4247 | 0 | const ggml_tensor * src1 = dst->src[1]; |
4248 | |
|
4249 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
4250 | |
|
4251 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
4252 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
4253 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
4254 | |
|
4255 | 0 | const int ith = params->ith; |
4256 | 0 | const int nth = params->nth; |
4257 | |
|
4258 | 0 | GGML_ASSERT(ne0 == ne00); |
4259 | 0 | GGML_ASSERT(ne1 == ne10); |
4260 | 0 | GGML_ASSERT(ne2 == ne12); |
4261 | 0 | GGML_ASSERT(ne3 == ne13); |
4262 | |
|
4263 | 0 | GGML_ASSERT(ne2 % ne02 == 0); |
4264 | 0 | GGML_ASSERT(ne3 % ne03 == 0); |
4265 | | |
4266 | | // we don't support permuted src0 or src1 |
4267 | 0 | GGML_ASSERT(nb00 == sizeof(float)); |
4268 | | |
4269 | | // dst cannot be transposed or permuted |
4270 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
4271 | | // GGML_ASSERT(nb0 <= nb1); |
4272 | | // GGML_ASSERT(nb1 <= nb2); |
4273 | | // GGML_ASSERT(nb2 <= nb3); |
4274 | | |
4275 | | // nb01 >= nb00 - src0 is not transposed |
4276 | | // compute by src0 rows |
4277 | |
|
4278 | 0 | if (ith == 0) { |
4279 | 0 | ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
4280 | 0 | } |
4281 | 0 | ggml_barrier(params->threadpool); |
4282 | | |
4283 | | // dst[:,:,:,:] = 0 |
4284 | | // for i2,i3: |
4285 | | // for i1: |
4286 | | // for i01: |
4287 | | // for i0: |
4288 | | // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3] |
4289 | | |
4290 | | // parallelize by last three dimensions |
4291 | | |
4292 | | // total rows in dst |
4293 | 0 | const int64_t nr = ne1*ne2*ne3; |
4294 | | |
4295 | | // rows per thread |
4296 | 0 | const int64_t dr = (nr + nth - 1)/nth; |
4297 | | |
4298 | | // row range for this thread |
4299 | 0 | const int64_t ir0 = dr*ith; |
4300 | 0 | const int64_t ir1 = MIN(ir0 + dr, nr); |
4301 | | |
4302 | | // block-tiling attempt |
4303 | 0 | const int64_t blck_0 = MAX(GGML_VEC_MAD_UNROLL, 32); |
4304 | 0 | const int64_t blck_1 = 16; |
4305 | | |
4306 | | // dps == dst per src0, used for group query attention |
4307 | 0 | const int64_t dps2 = ne2 / ne02; |
4308 | 0 | const int64_t dps3 = ne3 / ne03; |
4309 | |
|
4310 | 0 | for (int64_t bir = ir0; bir < ir1; bir += blck_1) { |
4311 | 0 | const int64_t bir1 = MIN(bir + blck_1, ir1); |
4312 | 0 | for (int64_t bi01 = 0; bi01 < ne01; bi01 += blck_0) { |
4313 | 0 | const int64_t bne01 = MIN(bi01 + blck_0, ne01); |
4314 | 0 | for (int64_t ir = bir; ir < bir1; ++ir) { |
4315 | | // dst indices |
4316 | 0 | const int64_t i3 = ir/(ne2*ne1); |
4317 | 0 | const int64_t i2 = (ir - i3*ne2*ne1)/ne1; |
4318 | 0 | const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1); |
4319 | |
|
4320 | 0 | const int64_t i02 = i2 / dps2; |
4321 | 0 | const int64_t i03 = i3 / dps3; |
4322 | | |
4323 | | //const int64_t i10 = i1; |
4324 | 0 | const int64_t i12 = i2; |
4325 | 0 | const int64_t i13 = i3; |
4326 | |
|
4327 | 0 | #if GGML_VEC_MAD_UNROLL > 2 |
4328 | 0 | const int64_t bne01_unroll = bne01 - (bne01 % GGML_VEC_MAD_UNROLL); |
4329 | 0 | for (int64_t i01 = bi01; i01 < bne01_unroll; i01 += GGML_VEC_MAD_UNROLL) { |
4330 | 0 | const int64_t i11 = i01; |
4331 | |
|
4332 | 0 | float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
4333 | 0 | float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
4334 | 0 | float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
4335 | |
|
4336 | 0 | ggml_vec_mad_f32_unroll(ne0, nb01, nb11, d, s0, s1); |
4337 | 0 | } |
4338 | 0 | for (int64_t i01 = bne01_unroll; i01 < bne01; ++i01) { |
4339 | 0 | const int64_t i11 = i01; |
4340 | |
|
4341 | 0 | float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
4342 | 0 | float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
4343 | 0 | float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
4344 | |
|
4345 | 0 | ggml_vec_mad_f32(ne0, d, s0, *s1); |
4346 | 0 | } |
4347 | | #else |
4348 | | for (int64_t i01 = bi01; i01 < bne01; ++i01) { |
4349 | | const int64_t i11 = i01; |
4350 | | |
4351 | | float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
4352 | | float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
4353 | | float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
4354 | | |
4355 | | ggml_vec_mad_f32(ne0, d, s0, *s1); |
4356 | | } |
4357 | | #endif |
4358 | 0 | } |
4359 | 0 | } |
4360 | 0 | } |
4361 | 0 | } |
4362 | | |
4363 | | static void ggml_compute_forward_out_prod_q_f32( |
4364 | | const ggml_compute_params * params, |
4365 | 0 | ggml_tensor * dst) { |
4366 | |
|
4367 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4368 | 0 | const ggml_tensor * src1 = dst->src[1]; |
4369 | |
|
4370 | 0 | GGML_TENSOR_BINARY_OP_LOCALS; |
4371 | |
|
4372 | 0 | const int ith = params->ith; |
4373 | 0 | const int nth = params->nth; |
4374 | |
|
4375 | 0 | const ggml_type type = src0->type; |
4376 | 0 | ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
4377 | |
|
4378 | 0 | GGML_ASSERT(ne02 == ne12); |
4379 | 0 | GGML_ASSERT(ne03 == ne13); |
4380 | 0 | GGML_ASSERT(ne2 == ne12); |
4381 | 0 | GGML_ASSERT(ne3 == ne13); |
4382 | | |
4383 | | // we don't support permuted src0 dim0 |
4384 | 0 | GGML_ASSERT(nb00 == ggml_type_size(type)); |
4385 | | |
4386 | | // dst dim0 cannot be transposed or permuted |
4387 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
4388 | | // GGML_ASSERT(nb0 <= nb1); |
4389 | | // GGML_ASSERT(nb1 <= nb2); |
4390 | | // GGML_ASSERT(nb2 <= nb3); |
4391 | |
|
4392 | 0 | GGML_ASSERT(ne0 == ne00); |
4393 | 0 | GGML_ASSERT(ne1 == ne10); |
4394 | 0 | GGML_ASSERT(ne2 == ne02); |
4395 | 0 | GGML_ASSERT(ne3 == ne03); |
4396 | | |
4397 | | // nb01 >= nb00 - src0 is not transposed |
4398 | | // compute by src0 rows |
4399 | |
|
4400 | 0 | if (ith == 0) { |
4401 | 0 | ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
4402 | 0 | } |
4403 | 0 | ggml_barrier(params->threadpool); |
4404 | | |
4405 | | // parallelize by last three dimensions |
4406 | | |
4407 | | // total rows in dst |
4408 | 0 | const int64_t nr = ne1*ne2*ne3; |
4409 | | |
4410 | | // rows per thread |
4411 | 0 | const int64_t dr = (nr + nth - 1)/nth; |
4412 | | |
4413 | | // row range for this thread |
4414 | 0 | const int64_t ir0 = dr*ith; |
4415 | 0 | const int64_t ir1 = MIN(ir0 + dr, nr); |
4416 | | |
4417 | | // dst[:,:,:,:] = 0 |
4418 | | // for i2,i3: |
4419 | | // for i1: |
4420 | | // for i01: |
4421 | | // for i0: |
4422 | | // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3] |
4423 | |
|
4424 | 0 | float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith; |
4425 | |
|
4426 | 0 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
4427 | | // dst indices |
4428 | 0 | const int64_t i3 = ir/(ne2*ne1); |
4429 | 0 | const int64_t i2 = (ir - i3*ne2*ne1)/ne1; |
4430 | 0 | const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1); |
4431 | |
|
4432 | 0 | const int64_t i02 = i2; |
4433 | 0 | const int64_t i03 = i3; |
4434 | | |
4435 | | //const int64_t i10 = i1; |
4436 | 0 | const int64_t i12 = i2; |
4437 | 0 | const int64_t i13 = i3; |
4438 | |
|
4439 | 0 | for (int64_t i01 = 0; i01 < ne01; ++i01) { |
4440 | 0 | const int64_t i11 = i01; |
4441 | |
|
4442 | 0 | float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
4443 | 0 | float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
4444 | 0 | float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
4445 | |
|
4446 | 0 | dequantize_row_q(s0, wdata, ne0); |
4447 | 0 | ggml_vec_mad_f32(ne0, d, wdata, *s1); |
4448 | 0 | } |
4449 | 0 | } |
4450 | 0 | } |
4451 | | |
4452 | | static void ggml_compute_forward_out_prod_f16_f32( |
4453 | | const ggml_compute_params * params, |
4454 | 0 | ggml_tensor * dst) { |
4455 | |
|
4456 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4457 | 0 | const ggml_tensor * src1 = dst->src[1]; |
4458 | |
|
4459 | 0 | GGML_TENSOR_BINARY_OP_LOCALS; |
4460 | |
|
4461 | 0 | const int ith = params->ith; |
4462 | 0 | const int nth = params->nth; |
4463 | |
|
4464 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F16); |
4465 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
4466 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
4467 | |
|
4468 | 0 | GGML_ASSERT(ne02 == ne12); |
4469 | 0 | GGML_ASSERT(ne03 == ne13); |
4470 | 0 | GGML_ASSERT(ne2 == ne12); |
4471 | 0 | GGML_ASSERT(ne3 == ne13); |
4472 | |
|
4473 | 0 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
4474 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
4475 | |
|
4476 | 0 | GGML_ASSERT(ne0 == ne00); |
4477 | 0 | GGML_ASSERT(ne1 == ne10); |
4478 | 0 | GGML_ASSERT(ne2 == ne02); |
4479 | 0 | GGML_ASSERT(ne3 == ne03); |
4480 | |
|
4481 | 0 | if (ith == 0) { |
4482 | 0 | ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
4483 | 0 | } |
4484 | 0 | ggml_barrier(params->threadpool); |
4485 | |
|
4486 | 0 | const int64_t nr = ne1*ne2*ne3; |
4487 | 0 | const int64_t dr = (nr + nth - 1)/nth; |
4488 | 0 | const int64_t ir0 = dr*ith; |
4489 | 0 | const int64_t ir1 = MIN(ir0 + dr, nr); |
4490 | |
|
4491 | 0 | float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith; |
4492 | |
|
4493 | 0 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
4494 | 0 | const int64_t i3 = ir/(ne2*ne1); |
4495 | 0 | const int64_t i2 = (ir - i3*ne2*ne1)/ne1; |
4496 | 0 | const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1); |
4497 | |
|
4498 | 0 | const int64_t i02 = i2; |
4499 | 0 | const int64_t i03 = i3; |
4500 | |
|
4501 | 0 | const int64_t i12 = i2; |
4502 | 0 | const int64_t i13 = i3; |
4503 | |
|
4504 | 0 | float * d = (float *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb3)); |
4505 | |
|
4506 | 0 | for (int64_t i01 = 0; i01 < ne01; ++i01) { |
4507 | 0 | const int64_t i11 = i01; |
4508 | 0 | ggml_fp16_t * s0 = (ggml_fp16_t *) ((char *) src0->data + (i01*nb01 + i02*nb02 + i03*nb03)); |
4509 | 0 | float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
4510 | 0 | ggml_fp16_to_fp32_row(s0, wdata, ne0); |
4511 | 0 | ggml_vec_mad_f32(ne0, d, wdata, *s1); |
4512 | 0 | } |
4513 | 0 | } |
4514 | 0 | } |
4515 | | |
4516 | | void ggml_compute_forward_out_prod( |
4517 | | const ggml_compute_params * params, |
4518 | 0 | ggml_tensor * dst) { |
4519 | |
|
4520 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4521 | |
|
4522 | 0 | switch (src0->type) { |
4523 | 0 | case GGML_TYPE_Q1_0: |
4524 | 0 | case GGML_TYPE_Q2_0: |
4525 | 0 | case GGML_TYPE_Q4_0: |
4526 | 0 | case GGML_TYPE_Q4_1: |
4527 | 0 | case GGML_TYPE_Q5_0: |
4528 | 0 | case GGML_TYPE_Q5_1: |
4529 | 0 | case GGML_TYPE_Q8_0: |
4530 | 0 | case GGML_TYPE_MXFP4: |
4531 | 0 | case GGML_TYPE_NVFP4: |
4532 | 0 | case GGML_TYPE_Q2_K: |
4533 | 0 | case GGML_TYPE_Q3_K: |
4534 | 0 | case GGML_TYPE_Q4_K: |
4535 | 0 | case GGML_TYPE_Q5_K: |
4536 | 0 | case GGML_TYPE_Q6_K: |
4537 | 0 | case GGML_TYPE_TQ1_0: |
4538 | 0 | case GGML_TYPE_TQ2_0: |
4539 | 0 | case GGML_TYPE_IQ2_XXS: |
4540 | 0 | case GGML_TYPE_IQ2_XS: |
4541 | 0 | case GGML_TYPE_IQ3_XXS: |
4542 | 0 | case GGML_TYPE_IQ1_S: |
4543 | 0 | case GGML_TYPE_IQ1_M: |
4544 | 0 | case GGML_TYPE_IQ4_NL: |
4545 | 0 | case GGML_TYPE_IQ4_XS: |
4546 | 0 | case GGML_TYPE_IQ3_S: |
4547 | 0 | case GGML_TYPE_IQ2_S: |
4548 | 0 | { |
4549 | 0 | ggml_compute_forward_out_prod_q_f32(params, dst); |
4550 | 0 | } break; |
4551 | 0 | case GGML_TYPE_F16: |
4552 | 0 | { |
4553 | 0 | ggml_compute_forward_out_prod_f16_f32(params, dst); |
4554 | 0 | } break; |
4555 | 0 | case GGML_TYPE_F32: |
4556 | 0 | { |
4557 | 0 | ggml_compute_forward_out_prod_f32(params, dst); |
4558 | 0 | } break; |
4559 | 0 | default: |
4560 | 0 | { |
4561 | 0 | GGML_ABORT("fatal error"); |
4562 | 0 | } |
4563 | 0 | } |
4564 | 0 | } |
4565 | | |
4566 | | // ggml_compute_forward_scale |
4567 | | |
4568 | | static void ggml_compute_forward_scale_f32( |
4569 | | const ggml_compute_params * params, |
4570 | 0 | ggml_tensor * dst) { |
4571 | |
|
4572 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4573 | |
|
4574 | 0 | GGML_ASSERT(ggml_is_contiguous(src0)); |
4575 | 0 | GGML_ASSERT(ggml_is_contiguous(dst)); |
4576 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
4577 | |
|
4578 | 0 | float s; // scale factor |
4579 | 0 | float b; // bias |
4580 | |
|
4581 | 0 | memcpy(&s, (float *) dst->op_params + 0, sizeof(float)); |
4582 | 0 | memcpy(&b, (float *) dst->op_params + 1, sizeof(float)); |
4583 | |
|
4584 | 0 | const int ith = params->ith; |
4585 | 0 | const int nth = params->nth; |
4586 | |
|
4587 | 0 | const int nc = src0->ne[0]; |
4588 | 0 | const int nr = ggml_nrows(src0); |
4589 | | |
4590 | | // rows per thread |
4591 | 0 | const int dr = (nr + nth - 1)/nth; |
4592 | | |
4593 | | // row range for this thread |
4594 | 0 | const int ir0 = dr*ith; |
4595 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
4596 | |
|
4597 | 0 | const size_t nb01 = src0->nb[1]; |
4598 | |
|
4599 | 0 | const size_t nb1 = dst->nb[1]; |
4600 | |
|
4601 | 0 | if (b == 0.0f) { |
4602 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
4603 | 0 | if (dst->data != src0->data) { |
4604 | | // src0 is same shape as dst => same indices |
4605 | | // TODO: add x parameter to ggml_vec_scale_f32 and remove this memcpy |
4606 | 0 | memcpy((char *)dst->data + i1*nb1, (char *)src0->data + i1*nb01, nc * sizeof(float)); |
4607 | 0 | } |
4608 | 0 | ggml_vec_scale_f32(nc, (float *) ((char *) dst->data + i1*nb1), s); |
4609 | 0 | } |
4610 | 0 | } else { |
4611 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
4612 | 0 | ggml_vec_mad1_f32(nc, |
4613 | 0 | (float *) ((char *) dst->data + i1*nb1), |
4614 | 0 | (float *) ((char *) src0->data + i1*nb1), |
4615 | 0 | s, b); |
4616 | 0 | } |
4617 | 0 | } |
4618 | 0 | } |
4619 | | |
4620 | | void ggml_compute_forward_scale( |
4621 | | const ggml_compute_params * params, |
4622 | 0 | ggml_tensor * dst) { |
4623 | |
|
4624 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4625 | |
|
4626 | 0 | switch (src0->type) { |
4627 | 0 | case GGML_TYPE_F32: |
4628 | 0 | { |
4629 | 0 | ggml_compute_forward_scale_f32(params, dst); |
4630 | 0 | } break; |
4631 | 0 | default: |
4632 | 0 | { |
4633 | 0 | GGML_ABORT("fatal error"); |
4634 | 0 | } |
4635 | 0 | } |
4636 | 0 | } |
4637 | | |
4638 | | // ggml_compute_forward_set |
4639 | | |
4640 | | static void ggml_compute_forward_set_f32( |
4641 | | const ggml_compute_params * params, |
4642 | 0 | ggml_tensor * dst) { |
4643 | |
|
4644 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4645 | 0 | const ggml_tensor * src1 = dst->src[1]; |
4646 | |
|
4647 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
4648 | 0 | GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
4649 | | |
4650 | | // view src0 and dst with these strides and data offset inbytes during set |
4651 | | // nb0 is implicitly element_size because src0 and dst are contiguous |
4652 | 0 | size_t nb1 = ((int32_t *) dst->op_params)[0]; |
4653 | 0 | size_t nb2 = ((int32_t *) dst->op_params)[1]; |
4654 | 0 | size_t nb3 = ((int32_t *) dst->op_params)[2]; |
4655 | 0 | size_t offset = ((int32_t *) dst->op_params)[3]; |
4656 | 0 | bool inplace = (bool) ((int32_t *) dst->op_params)[4]; |
4657 | |
|
4658 | 0 | if (!inplace) { |
4659 | 0 | if (params->ith == 0) { |
4660 | | // memcpy needs to be synchronized across threads to avoid race conditions. |
4661 | | // => do it in INIT phase |
4662 | 0 | memcpy( |
4663 | 0 | ((char *) dst->data), |
4664 | 0 | ((char *) src0->data), |
4665 | 0 | ggml_nbytes(dst)); |
4666 | 0 | } |
4667 | 0 | ggml_barrier(params->threadpool); |
4668 | 0 | } |
4669 | |
|
4670 | 0 | const int ith = params->ith; |
4671 | 0 | const int nth = params->nth; |
4672 | |
|
4673 | 0 | const int nr = ggml_nrows(src1); |
4674 | 0 | const int nc = src1->ne[0]; |
4675 | |
|
4676 | 0 | GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) |
4677 | 0 | GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) |
4678 | | |
4679 | | // src0 and dst as viewed during set |
4680 | 0 | const size_t nb0 = ggml_element_size(src0); |
4681 | |
|
4682 | 0 | const int im0 = (ne10 == 0 ? 0 : ne10-1); |
4683 | 0 | const int im1 = (ne11 == 0 ? 0 : ne11-1); |
4684 | 0 | const int im2 = (ne12 == 0 ? 0 : ne12-1); |
4685 | 0 | const int im3 = (ne13 == 0 ? 0 : ne13-1); |
4686 | |
|
4687 | 0 | GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst)); |
4688 | |
|
4689 | 0 | GGML_ASSERT(nb10 == sizeof(float)); |
4690 | | |
4691 | | // rows per thread |
4692 | 0 | const int dr = (nr + nth - 1)/nth; |
4693 | | |
4694 | | // row range for this thread |
4695 | 0 | const int ir0 = dr*ith; |
4696 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
4697 | |
|
4698 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
4699 | | // src0 and dst are viewed with shape of src1 and offset |
4700 | | // => same indices |
4701 | 0 | const int i3 = ir/(ne12*ne11); |
4702 | 0 | const int i2 = (ir - i3*ne12*ne11)/ne11; |
4703 | 0 | const int i1 = (ir - i3*ne12*ne11 - i2*ne11); |
4704 | |
|
4705 | 0 | ggml_vec_cpy_f32(nc, |
4706 | 0 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), |
4707 | 0 | (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11)); |
4708 | 0 | } |
4709 | 0 | } |
4710 | | |
4711 | | static void ggml_compute_forward_set_i32( |
4712 | | const ggml_compute_params * params, |
4713 | 0 | ggml_tensor * dst) { |
4714 | |
|
4715 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4716 | 0 | const ggml_tensor * src1 = dst->src[1]; |
4717 | |
|
4718 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
4719 | 0 | GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
4720 | | |
4721 | | // view src0 and dst with these strides and data offset inbytes during set |
4722 | | // nb0 is implicitly element_size because src0 and dst are contiguous |
4723 | 0 | size_t nb1 = ((int32_t *) dst->op_params)[0]; |
4724 | 0 | size_t nb2 = ((int32_t *) dst->op_params)[1]; |
4725 | 0 | size_t nb3 = ((int32_t *) dst->op_params)[2]; |
4726 | 0 | size_t offset = ((int32_t *) dst->op_params)[3]; |
4727 | 0 | bool inplace = (bool) ((int32_t *) dst->op_params)[4]; |
4728 | |
|
4729 | 0 | if (!inplace) { |
4730 | 0 | if (params->ith == 0) { |
4731 | | // memcpy needs to be synchronized across threads to avoid race conditions. |
4732 | | // => do it in INIT phase |
4733 | 0 | memcpy( |
4734 | 0 | ((char *) dst->data), |
4735 | 0 | ((char *) src0->data), |
4736 | 0 | ggml_nbytes(dst)); |
4737 | 0 | } |
4738 | 0 | ggml_barrier(params->threadpool); |
4739 | 0 | } |
4740 | |
|
4741 | 0 | const int ith = params->ith; |
4742 | 0 | const int nth = params->nth; |
4743 | |
|
4744 | 0 | const int nr = ggml_nrows(src1); |
4745 | 0 | const int nc = src1->ne[0]; |
4746 | |
|
4747 | 0 | GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) |
4748 | 0 | GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) |
4749 | | |
4750 | | // src0 and dst as viewed during set |
4751 | 0 | const size_t nb0 = ggml_element_size(src0); |
4752 | |
|
4753 | 0 | const int im0 = (ne10 == 0 ? 0 : ne10-1); |
4754 | 0 | const int im1 = (ne11 == 0 ? 0 : ne11-1); |
4755 | 0 | const int im2 = (ne12 == 0 ? 0 : ne12-1); |
4756 | 0 | const int im3 = (ne13 == 0 ? 0 : ne13-1); |
4757 | |
|
4758 | 0 | GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst)); |
4759 | |
|
4760 | 0 | GGML_ASSERT(nb10 == sizeof(int32_t)); |
4761 | | |
4762 | | // rows per thread |
4763 | 0 | const int dr = (nr + nth - 1)/nth; |
4764 | | |
4765 | | // row range for this thread |
4766 | 0 | const int ir0 = dr*ith; |
4767 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
4768 | |
|
4769 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
4770 | | // src0 and dst are viewed with shape of src1 and offset |
4771 | | // => same indices |
4772 | 0 | const int i3 = ir/(ne12*ne11); |
4773 | 0 | const int i2 = (ir - i3*ne12*ne11)/ne11; |
4774 | 0 | const int i1 = (ir - i3*ne12*ne11 - i2*ne11); |
4775 | |
|
4776 | 0 | ggml_vec_cpy_i32(nc, |
4777 | 0 | (int32_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), |
4778 | 0 | (int32_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11)); |
4779 | 0 | } |
4780 | 0 | } |
4781 | | |
4782 | | void ggml_compute_forward_set( |
4783 | | const ggml_compute_params * params, |
4784 | 0 | ggml_tensor * dst) { |
4785 | |
|
4786 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4787 | |
|
4788 | 0 | switch (src0->type) { |
4789 | 0 | case GGML_TYPE_F32: |
4790 | 0 | { |
4791 | 0 | ggml_compute_forward_set_f32(params, dst); |
4792 | 0 | } break; |
4793 | 0 | case GGML_TYPE_I32: |
4794 | 0 | { |
4795 | 0 | ggml_compute_forward_set_i32(params, dst); |
4796 | 0 | } break; |
4797 | 0 | case GGML_TYPE_F16: |
4798 | 0 | case GGML_TYPE_BF16: |
4799 | 0 | case GGML_TYPE_Q1_0: |
4800 | 0 | case GGML_TYPE_Q2_0: |
4801 | 0 | case GGML_TYPE_Q4_0: |
4802 | 0 | case GGML_TYPE_Q4_1: |
4803 | 0 | case GGML_TYPE_Q5_0: |
4804 | 0 | case GGML_TYPE_Q5_1: |
4805 | 0 | case GGML_TYPE_Q8_0: |
4806 | 0 | case GGML_TYPE_Q8_1: |
4807 | 0 | case GGML_TYPE_MXFP4: |
4808 | 0 | case GGML_TYPE_NVFP4: |
4809 | 0 | case GGML_TYPE_Q2_K: |
4810 | 0 | case GGML_TYPE_Q3_K: |
4811 | 0 | case GGML_TYPE_Q4_K: |
4812 | 0 | case GGML_TYPE_Q5_K: |
4813 | 0 | case GGML_TYPE_Q6_K: |
4814 | 0 | case GGML_TYPE_TQ1_0: |
4815 | 0 | case GGML_TYPE_TQ2_0: |
4816 | 0 | case GGML_TYPE_IQ2_XXS: |
4817 | 0 | case GGML_TYPE_IQ2_XS: |
4818 | 0 | case GGML_TYPE_IQ3_XXS: |
4819 | 0 | case GGML_TYPE_IQ1_S: |
4820 | 0 | case GGML_TYPE_IQ1_M: |
4821 | 0 | case GGML_TYPE_IQ4_NL: |
4822 | 0 | case GGML_TYPE_IQ4_XS: |
4823 | 0 | case GGML_TYPE_IQ3_S: |
4824 | 0 | case GGML_TYPE_IQ2_S: |
4825 | 0 | default: |
4826 | 0 | { |
4827 | 0 | GGML_ABORT("fatal error"); |
4828 | 0 | } |
4829 | 0 | } |
4830 | 0 | } |
4831 | | |
4832 | | // ggml_compute_forward_cpy |
4833 | | |
4834 | | void ggml_compute_forward_cpy( |
4835 | | const ggml_compute_params * params, |
4836 | 0 | ggml_tensor * dst) { |
4837 | 0 | ggml_compute_forward_dup(params, dst); |
4838 | 0 | } |
4839 | | |
4840 | | // ggml_compute_forward_cont |
4841 | | |
4842 | | void ggml_compute_forward_cont( |
4843 | | const ggml_compute_params * params, |
4844 | 0 | ggml_tensor * dst) { |
4845 | 0 | ggml_compute_forward_dup(params, dst); |
4846 | 0 | } |
4847 | | |
4848 | | // ggml_compute_forward_get_rows |
4849 | | |
4850 | | static void ggml_compute_forward_get_rows_q( |
4851 | | const ggml_compute_params * params, |
4852 | 0 | ggml_tensor * dst) { |
4853 | |
|
4854 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4855 | 0 | const ggml_tensor * src1 = dst->src[1]; |
4856 | |
|
4857 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
4858 | |
|
4859 | 0 | const int64_t nc = ne00; |
4860 | 0 | const int64_t nr = ggml_nelements(src1); |
4861 | |
|
4862 | 0 | const ggml_type type = src0->type; |
4863 | 0 | ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
4864 | |
|
4865 | 0 | assert(ne0 == nc); |
4866 | 0 | assert(ne02 == ne11); |
4867 | 0 | assert(nb00 == ggml_type_size(type)); |
4868 | 0 | assert(ggml_nrows(dst) == nr); |
4869 | |
|
4870 | 0 | const int ith = params->ith; |
4871 | 0 | const int nth = params->nth; |
4872 | | |
4873 | | // rows per thread |
4874 | 0 | const int dr = (nr + nth - 1)/nth; |
4875 | | |
4876 | | // row range for this thread |
4877 | 0 | const int ir0 = dr*ith; |
4878 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
4879 | |
|
4880 | 0 | for (int64_t i = ir0; i < ir1; ++i) { |
4881 | 0 | const int64_t i12 = i/(ne11*ne10); |
4882 | 0 | const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
4883 | 0 | const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
4884 | 0 | const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
4885 | |
|
4886 | 0 | GGML_ASSERT(i01 >= 0 && i01 < ne01); |
4887 | |
|
4888 | 0 | dequantize_row_q( |
4889 | 0 | (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03), |
4890 | 0 | (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc); |
4891 | 0 | } |
4892 | 0 | } |
4893 | | |
4894 | | static void ggml_compute_forward_get_rows_f16( |
4895 | | const ggml_compute_params * params, |
4896 | 0 | ggml_tensor * dst) { |
4897 | |
|
4898 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4899 | 0 | const ggml_tensor * src1 = dst->src[1]; |
4900 | |
|
4901 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
4902 | |
|
4903 | 0 | const int64_t nc = ne00; |
4904 | 0 | const int64_t nr = ggml_nelements(src1); |
4905 | |
|
4906 | 0 | assert(ne0 == nc); |
4907 | 0 | assert(ne02 == ne11); |
4908 | 0 | assert(nb00 == sizeof(ggml_fp16_t)); |
4909 | 0 | assert(ggml_nrows(dst) == nr); |
4910 | |
|
4911 | 0 | const int ith = params->ith; |
4912 | 0 | const int nth = params->nth; |
4913 | | |
4914 | | // rows per thread |
4915 | 0 | const int dr = (nr + nth - 1)/nth; |
4916 | | |
4917 | | // row range for this thread |
4918 | 0 | const int ir0 = dr*ith; |
4919 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
4920 | |
|
4921 | 0 | for (int64_t i = ir0; i < ir1; ++i) { |
4922 | 0 | const int64_t i12 = i/(ne11*ne10); |
4923 | 0 | const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
4924 | 0 | const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
4925 | 0 | const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
4926 | |
|
4927 | 0 | GGML_ASSERT(i01 >= 0 && i01 < ne01); |
4928 | |
|
4929 | 0 | ggml_cpu_fp16_to_fp32( |
4930 | 0 | (const ggml_fp16_t*) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03), |
4931 | 0 | (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc); |
4932 | 0 | } |
4933 | 0 | } |
4934 | | |
4935 | | static void ggml_compute_forward_get_rows_bf16( |
4936 | | const ggml_compute_params * params, |
4937 | 0 | ggml_tensor * dst) { |
4938 | |
|
4939 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4940 | 0 | const ggml_tensor * src1 = dst->src[1]; |
4941 | |
|
4942 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
4943 | |
|
4944 | 0 | const int64_t nc = ne00; |
4945 | 0 | const int64_t nr = ggml_nelements(src1); |
4946 | |
|
4947 | 0 | assert(ne0 == nc); |
4948 | 0 | assert(ne02 == ne11); |
4949 | 0 | assert(nb00 == sizeof(ggml_bf16_t)); |
4950 | 0 | assert(ggml_nrows(dst) == nr); |
4951 | |
|
4952 | 0 | const int ith = params->ith; |
4953 | 0 | const int nth = params->nth; |
4954 | | |
4955 | | // rows per thread |
4956 | 0 | const int dr = (nr + nth - 1)/nth; |
4957 | | |
4958 | | // row range for this thread |
4959 | 0 | const int ir0 = dr*ith; |
4960 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
4961 | |
|
4962 | 0 | for (int64_t i = ir0; i < ir1; ++i) { |
4963 | 0 | const int64_t i12 = i/(ne11*ne10); |
4964 | 0 | const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
4965 | 0 | const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
4966 | 0 | const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
4967 | |
|
4968 | 0 | GGML_ASSERT(i01 >= 0 && i01 < ne01); |
4969 | |
|
4970 | 0 | ggml_cpu_bf16_to_fp32( |
4971 | 0 | (const ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03), |
4972 | 0 | (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc); |
4973 | 0 | } |
4974 | 0 | } |
4975 | | |
4976 | | static void ggml_compute_forward_get_rows_f32( |
4977 | | const ggml_compute_params * params, |
4978 | 0 | ggml_tensor * dst) { |
4979 | |
|
4980 | 0 | const ggml_tensor * src0 = dst->src[0]; |
4981 | 0 | const ggml_tensor * src1 = dst->src[1]; |
4982 | |
|
4983 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
4984 | |
|
4985 | 0 | const int64_t nc = ne00; |
4986 | 0 | const int64_t nr = ggml_nelements(src1); |
4987 | |
|
4988 | 0 | assert(ne0 == nc); |
4989 | 0 | assert(ne02 == ne11); |
4990 | 0 | assert(nb00 == sizeof(float)); |
4991 | 0 | assert(ggml_nrows(dst) == nr); |
4992 | |
|
4993 | 0 | const int ith = params->ith; |
4994 | 0 | const int nth = params->nth; |
4995 | | |
4996 | | // rows per thread |
4997 | 0 | const int dr = (nr + nth - 1)/nth; |
4998 | | |
4999 | | // row range for this thread |
5000 | 0 | const int ir0 = dr*ith; |
5001 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
5002 | |
|
5003 | 0 | for (int64_t i = ir0; i < ir1; ++i) { |
5004 | 0 | const int64_t i12 = i/(ne11*ne10); |
5005 | 0 | const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
5006 | 0 | const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
5007 | 0 | const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
5008 | |
|
5009 | 0 | GGML_ASSERT(i01 >= 0 && i01 < ne01); |
5010 | |
|
5011 | 0 | ggml_vec_cpy_f32(nc, |
5012 | 0 | (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), |
5013 | 0 | (float *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03)); |
5014 | 0 | } |
5015 | 0 | } |
5016 | | |
5017 | | void ggml_compute_forward_get_rows( |
5018 | | const ggml_compute_params * params, |
5019 | 0 | ggml_tensor * dst) { |
5020 | |
|
5021 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5022 | |
|
5023 | 0 | switch (src0->type) { |
5024 | 0 | case GGML_TYPE_Q1_0: |
5025 | 0 | case GGML_TYPE_Q2_0: |
5026 | 0 | case GGML_TYPE_Q4_0: |
5027 | 0 | case GGML_TYPE_Q4_1: |
5028 | 0 | case GGML_TYPE_Q5_0: |
5029 | 0 | case GGML_TYPE_Q5_1: |
5030 | 0 | case GGML_TYPE_Q8_0: |
5031 | 0 | case GGML_TYPE_Q8_1: |
5032 | 0 | case GGML_TYPE_MXFP4: |
5033 | 0 | case GGML_TYPE_NVFP4: |
5034 | 0 | case GGML_TYPE_Q2_K: |
5035 | 0 | case GGML_TYPE_Q3_K: |
5036 | 0 | case GGML_TYPE_Q4_K: |
5037 | 0 | case GGML_TYPE_Q5_K: |
5038 | 0 | case GGML_TYPE_Q6_K: |
5039 | 0 | case GGML_TYPE_TQ1_0: |
5040 | 0 | case GGML_TYPE_TQ2_0: |
5041 | 0 | case GGML_TYPE_IQ2_XXS: |
5042 | 0 | case GGML_TYPE_IQ2_XS: |
5043 | 0 | case GGML_TYPE_IQ3_XXS: |
5044 | 0 | case GGML_TYPE_IQ1_S: |
5045 | 0 | case GGML_TYPE_IQ1_M: |
5046 | 0 | case GGML_TYPE_IQ4_NL: |
5047 | 0 | case GGML_TYPE_IQ4_XS: |
5048 | 0 | case GGML_TYPE_IQ3_S: |
5049 | 0 | case GGML_TYPE_IQ2_S: |
5050 | 0 | { |
5051 | 0 | ggml_compute_forward_get_rows_q(params, dst); |
5052 | 0 | } break; |
5053 | 0 | case GGML_TYPE_F16: |
5054 | 0 | { |
5055 | 0 | ggml_compute_forward_get_rows_f16(params, dst); |
5056 | 0 | } break; |
5057 | 0 | case GGML_TYPE_BF16: |
5058 | 0 | { |
5059 | 0 | ggml_compute_forward_get_rows_bf16(params, dst); |
5060 | 0 | } break; |
5061 | 0 | case GGML_TYPE_F32: |
5062 | 0 | case GGML_TYPE_I32: |
5063 | 0 | { |
5064 | 0 | ggml_compute_forward_get_rows_f32(params, dst); |
5065 | 0 | } break; |
5066 | 0 | default: |
5067 | 0 | { |
5068 | 0 | GGML_ABORT("fatal error"); |
5069 | 0 | } |
5070 | 0 | } |
5071 | | |
5072 | | //static bool first = true; |
5073 | | //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]); |
5074 | | //if (first) { |
5075 | | // first = false; |
5076 | | //} else { |
5077 | | // for (int k = 0; k < dst->ne[1]; ++k) { |
5078 | | // for (int j = 0; j < dst->ne[0]/16; ++j) { |
5079 | | // for (int i = 0; i < 16; ++i) { |
5080 | | // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]); |
5081 | | // } |
5082 | | // printf("\n"); |
5083 | | // } |
5084 | | // printf("\n"); |
5085 | | // } |
5086 | | // printf("\n"); |
5087 | | // exit(0); |
5088 | | //} |
5089 | 0 | } |
5090 | | |
5091 | | template<typename src_t, typename idx_t> |
5092 | | static void ggml_compute_forward_set_rows_impl( |
5093 | | const ggml_compute_params * params, |
5094 | 0 | ggml_tensor * dst) { |
5095 | |
|
5096 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5097 | 0 | const ggml_tensor * src1 = dst->src[1]; |
5098 | |
|
5099 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
5100 | |
|
5101 | 0 | const int64_t nc = ne00; |
5102 | 0 | const int64_t nr = ne01; |
5103 | |
|
5104 | 0 | assert(ne0 == nc); |
5105 | 0 | assert(ne2 == ne02); |
5106 | 0 | assert(ne3 == ne03); |
5107 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); |
5108 | 0 | assert(ne02 % ne11 == 0); |
5109 | 0 | assert(ne03 % ne12 == 0); |
5110 | |
|
5111 | 0 | const int ith = params->ith; |
5112 | 0 | const int nth = params->nth; |
5113 | | |
5114 | | // rows per thread |
5115 | 0 | const int64_t dr = (nr + nth - 1)/nth; |
5116 | | |
5117 | | // row range for this thread |
5118 | 0 | const int64_t ir0 = dr*ith; |
5119 | 0 | const int64_t ir1 = std::min(ir0 + dr, nr); |
5120 | |
|
5121 | 0 | const size_t rs = ggml_row_size(src0->type, nc); |
5122 | |
|
5123 | 0 | ggml_from_float_t const from_float = ggml_get_type_traits_cpu(dst->type)->from_float; |
5124 | |
|
5125 | 0 | for (int64_t i03 = 0; i03 < ne03; ++i03) { |
5126 | 0 | for (int64_t i02 = 0; i02 < ne02; ++i02) { |
5127 | 0 | for (int64_t i = ir0; i < ir1; ++i) { |
5128 | 0 | const int64_t i12 = i03%ne12; |
5129 | 0 | const int64_t i11 = i02%ne11; |
5130 | 0 | const int64_t i10 = i; |
5131 | |
|
5132 | 0 | const int64_t i1 = *(idx_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
5133 | |
|
5134 | 0 | GGML_ASSERT(i1 >= 0 && i1 < ne1); |
5135 | |
|
5136 | 0 | if constexpr (std::is_same_v<src_t, float>) { |
5137 | 0 | from_float( |
5138 | 0 | (const float *) ((char *) src0->data + i*nb01 + i02*nb02 + i03*nb03), |
5139 | 0 | ((char *) dst->data + i1*nb1 + i02*nb2 + i03*nb3), nc); |
5140 | 0 | } else if constexpr (std::is_same_v<src_t, ggml_fp16_t>) { |
5141 | 0 | if (dst->type == GGML_TYPE_F16) { |
5142 | 0 | memcpy( |
5143 | 0 | ((char *) dst->data + i1*nb1 + i02*nb2 + i03*nb3), |
5144 | 0 | ((char *) src0->data + i*nb01 + i02*nb02 + i03*nb03), |
5145 | 0 | rs); |
5146 | 0 | } else { |
5147 | 0 | float * wdata = (float *) params->wdata + (nc + CACHE_LINE_SIZE_F32) * ith; |
5148 | 0 | ggml_fp16_to_fp32_row( |
5149 | 0 | (const ggml_fp16_t *) ((char *) src0->data + i*nb01 + i02*nb02 + i03*nb03), |
5150 | 0 | wdata, nc); |
5151 | 0 | from_float(wdata, |
5152 | 0 | ((char *) dst->data + i1*nb1 + i02*nb2 + i03*nb3), nc); |
5153 | 0 | } |
5154 | | } else { |
5155 | | GGML_ABORT("src0->type = %d (%s) not supported", src0->type, ggml_type_name(src0->type)); |
5156 | | } |
5157 | 0 | } |
5158 | 0 | } |
5159 | 0 | } |
5160 | 0 | } Unexecuted instantiation: ops.cpp:void ggml_compute_forward_set_rows_impl<float, long>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_set_rows_impl<float, int>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_set_rows_impl<unsigned short, long>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_set_rows_impl<unsigned short, int>(ggml_compute_params const*, ggml_tensor*) |
5161 | | |
5162 | | void ggml_compute_forward_set_rows( |
5163 | | const ggml_compute_params * params, |
5164 | 0 | ggml_tensor * dst) { |
5165 | |
|
5166 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5167 | 0 | const ggml_tensor * src1 = dst->src[1]; |
5168 | |
|
5169 | 0 | switch (src0->type) { |
5170 | 0 | case GGML_TYPE_F32: |
5171 | 0 | { |
5172 | 0 | if (src1->type == GGML_TYPE_I64) { |
5173 | 0 | ggml_compute_forward_set_rows_impl<float, int64_t>(params, dst); |
5174 | 0 | } else if (src1->type == GGML_TYPE_I32) { |
5175 | 0 | ggml_compute_forward_set_rows_impl<float, int32_t>(params, dst); |
5176 | 0 | } else { |
5177 | 0 | GGML_ABORT("src1->type = %d (%s) not supported", src1->type, ggml_type_name(src1->type)); |
5178 | 0 | } |
5179 | 0 | } break; |
5180 | 0 | case GGML_TYPE_F16: |
5181 | 0 | { |
5182 | 0 | if (src1->type == GGML_TYPE_I64) { |
5183 | 0 | ggml_compute_forward_set_rows_impl<ggml_fp16_t, int64_t>(params, dst); |
5184 | 0 | } else if (src1->type == GGML_TYPE_I32) { |
5185 | 0 | ggml_compute_forward_set_rows_impl<ggml_fp16_t, int32_t>(params, dst); |
5186 | 0 | } else { |
5187 | 0 | GGML_ABORT("src1->type = %d (%s) not supported", src1->type, ggml_type_name(src1->type)); |
5188 | 0 | } |
5189 | 0 | } break; |
5190 | 0 | default: |
5191 | 0 | { |
5192 | 0 | GGML_ABORT("src0->type = %d (%s) not supported", src0->type, ggml_type_name(src0->type)); |
5193 | 0 | } |
5194 | 0 | } |
5195 | 0 | } |
5196 | | |
5197 | | // ggml_compute_forward_get_rows_back |
5198 | | |
5199 | | static void ggml_compute_forward_get_rows_back_f32_f16( |
5200 | | const ggml_compute_params * params, |
5201 | 0 | ggml_tensor * dst) { |
5202 | |
|
5203 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5204 | 0 | const ggml_tensor * src1 = dst->src[1]; |
5205 | |
|
5206 | 0 | if (params->ith != 0) { |
5207 | 0 | return; |
5208 | 0 | } |
5209 | | |
5210 | 0 | GGML_ASSERT(ggml_is_contiguous(dst)); |
5211 | | |
5212 | | // ggml_compute_forward_dup_same_cont(params, opt0, dst); |
5213 | |
|
5214 | 0 | memset(dst->data, 0, ggml_nbytes(dst)); |
5215 | |
|
5216 | 0 | const int nc = src0->ne[0]; |
5217 | 0 | const int nr = ggml_nelements(src1); |
5218 | |
|
5219 | 0 | GGML_ASSERT( dst->ne[0] == nc); |
5220 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(ggml_fp16_t)); |
5221 | |
|
5222 | 0 | for (int i = 0; i < nr; ++i) { |
5223 | 0 | const int r = ((int32_t *) src1->data)[i]; |
5224 | |
|
5225 | 0 | for (int j = 0; j < nc; ++j) { |
5226 | 0 | ggml_fp16_t v = ((ggml_fp16_t *) ((char *) src0->data + i*src0->nb[1]))[j]; |
5227 | 0 | ((float *) ((char *) dst->data + r*dst->nb[1]))[j] += GGML_CPU_FP16_TO_FP32(v); |
5228 | 0 | } |
5229 | 0 | } |
5230 | 0 | } |
5231 | | |
5232 | | static void ggml_compute_forward_get_rows_back_f32( |
5233 | | const ggml_compute_params * params, |
5234 | 0 | ggml_tensor * dst) { |
5235 | |
|
5236 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5237 | 0 | const ggml_tensor * src1 = dst->src[1]; |
5238 | |
|
5239 | 0 | if (params->ith != 0) { |
5240 | 0 | return; |
5241 | 0 | } |
5242 | | |
5243 | 0 | GGML_ASSERT(ggml_is_contiguous(dst)); |
5244 | | |
5245 | | // ggml_compute_forward_dup_same_cont(params, opt0, dst); |
5246 | |
|
5247 | 0 | memset(dst->data, 0, ggml_nbytes(dst)); |
5248 | |
|
5249 | 0 | const int nc = src0->ne[0]; |
5250 | 0 | const int nr = ggml_nelements(src1); |
5251 | |
|
5252 | 0 | GGML_ASSERT( dst->ne[0] == nc); |
5253 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
5254 | |
|
5255 | 0 | for (int i = 0; i < nr; ++i) { |
5256 | 0 | const int r = ((int32_t *) src1->data)[i]; |
5257 | |
|
5258 | 0 | ggml_vec_add_f32(nc, |
5259 | 0 | (float *) ((char *) dst->data + r*dst->nb[1]), |
5260 | 0 | (float *) ((char *) dst->data + r*dst->nb[1]), |
5261 | 0 | (float *) ((char *) src0->data + i*src0->nb[1])); |
5262 | 0 | } |
5263 | 0 | } |
5264 | | |
5265 | | void ggml_compute_forward_get_rows_back( |
5266 | | const ggml_compute_params * params, |
5267 | 0 | ggml_tensor * dst) { |
5268 | |
|
5269 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5270 | |
|
5271 | 0 | switch (src0->type) { |
5272 | 0 | case GGML_TYPE_F16: |
5273 | 0 | { |
5274 | 0 | ggml_compute_forward_get_rows_back_f32_f16(params, dst); |
5275 | 0 | } break; |
5276 | 0 | case GGML_TYPE_F32: |
5277 | 0 | { |
5278 | 0 | ggml_compute_forward_get_rows_back_f32(params, dst); |
5279 | 0 | } break; |
5280 | 0 | default: |
5281 | 0 | { |
5282 | 0 | GGML_ABORT("fatal error"); |
5283 | 0 | } |
5284 | 0 | } |
5285 | | |
5286 | | //static bool first = true; |
5287 | | //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]); |
5288 | | //if (first) { |
5289 | | // first = false; |
5290 | | //} else { |
5291 | | // for (int k = 0; k < dst->ne[1]; ++k) { |
5292 | | // for (int j = 0; j < dst->ne[0]/16; ++j) { |
5293 | | // for (int i = 0; i < 16; ++i) { |
5294 | | // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]); |
5295 | | // } |
5296 | | // printf("\n"); |
5297 | | // } |
5298 | | // printf("\n"); |
5299 | | // } |
5300 | | // printf("\n"); |
5301 | | // exit(0); |
5302 | | //} |
5303 | 0 | } |
5304 | | |
5305 | | // ggml_compute_forward_diag |
5306 | | |
5307 | | static void ggml_compute_forward_diag_f32( |
5308 | | const ggml_compute_params * params, |
5309 | 0 | ggml_tensor * dst) { |
5310 | |
|
5311 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5312 | |
|
5313 | 0 | if (params->ith != 0) { |
5314 | 0 | return; |
5315 | 0 | } |
5316 | | |
5317 | | // TODO: handle transposed/permuted matrices |
5318 | | |
5319 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
5320 | |
|
5321 | 0 | GGML_ASSERT(ne00 == ne0); |
5322 | 0 | GGML_ASSERT(ne00 == ne1); |
5323 | 0 | GGML_ASSERT(ne01 == 1); |
5324 | 0 | GGML_ASSERT(ne02 == ne2); |
5325 | 0 | GGML_ASSERT(ne03 == ne3); |
5326 | |
|
5327 | 0 | GGML_ASSERT(nb00 == sizeof(float)); |
5328 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
5329 | |
|
5330 | 0 | for (int i3 = 0; i3 < ne3; i3++) { |
5331 | 0 | for (int i2 = 0; i2 < ne2; i2++) { |
5332 | 0 | for (int i1 = 0; i1 < ne1; i1++) { |
5333 | 0 | float * d = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1); |
5334 | 0 | float * s = (float *)((char *) src0->data + i3*nb03 + i2*nb02); |
5335 | 0 | for (int i0 = 0; i0 < i1; i0++) { |
5336 | 0 | d[i0] = 0; |
5337 | 0 | } |
5338 | 0 | d[i1] = s[i1]; |
5339 | 0 | for (int i0 = i1+1; i0 < ne0; i0++) { |
5340 | 0 | d[i0] = 0; |
5341 | 0 | } |
5342 | 0 | } |
5343 | 0 | } |
5344 | 0 | } |
5345 | 0 | } |
5346 | | |
5347 | | void ggml_compute_forward_diag( |
5348 | | const ggml_compute_params * params, |
5349 | 0 | ggml_tensor * dst) { |
5350 | |
|
5351 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5352 | |
|
5353 | 0 | switch (src0->type) { |
5354 | 0 | case GGML_TYPE_F32: |
5355 | 0 | { |
5356 | 0 | ggml_compute_forward_diag_f32(params, dst); |
5357 | 0 | } break; |
5358 | 0 | default: |
5359 | 0 | { |
5360 | 0 | GGML_ABORT("fatal error"); |
5361 | 0 | } |
5362 | 0 | } |
5363 | 0 | } |
5364 | | |
5365 | | // ggml_compute_forward_diag_mask_inf |
5366 | | |
5367 | | static void ggml_compute_forward_diag_mask_f32( |
5368 | | const ggml_compute_params * params, |
5369 | | ggml_tensor * dst, |
5370 | 0 | const float value) { |
5371 | |
|
5372 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5373 | |
|
5374 | 0 | const int ith = params->ith; |
5375 | 0 | const int nth = params->nth; |
5376 | |
|
5377 | 0 | const int n_past = ((int32_t *) dst->op_params)[0]; |
5378 | 0 | const bool inplace = src0->data == dst->data; |
5379 | |
|
5380 | 0 | GGML_ASSERT(n_past >= 0); |
5381 | |
|
5382 | 0 | if (!inplace) { |
5383 | 0 | if (ith == 0) { |
5384 | | // memcpy needs to be synchronized across threads to avoid race conditions. |
5385 | | // => do it in INIT phase |
5386 | 0 | GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
5387 | 0 | GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
5388 | 0 | memcpy( |
5389 | 0 | ((char *) dst->data), |
5390 | 0 | ((char *) src0->data), |
5391 | 0 | ggml_nbytes(dst)); |
5392 | 0 | } |
5393 | 0 | ggml_barrier(params->threadpool); |
5394 | 0 | } |
5395 | | |
5396 | | // TODO: handle transposed/permuted matrices |
5397 | |
|
5398 | 0 | const int n = ggml_nrows(src0); |
5399 | 0 | const int nc = src0->ne[0]; |
5400 | 0 | const int nr = src0->ne[1]; |
5401 | 0 | const int nz = n/nr; |
5402 | |
|
5403 | 0 | GGML_ASSERT( dst->nb[0] == sizeof(float)); |
5404 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
5405 | |
|
5406 | 0 | for (int k = 0; k < nz; k++) { |
5407 | 0 | for (int j = ith; j < nr; j += nth) { |
5408 | 0 | for (int i = n_past; i < nc; i++) { |
5409 | 0 | if (i > n_past + j) { |
5410 | 0 | *(float *)((char *) dst->data + k*dst->nb[2] + j*dst->nb[1] + i*dst->nb[0]) = value; |
5411 | 0 | } |
5412 | 0 | } |
5413 | 0 | } |
5414 | 0 | } |
5415 | 0 | } |
5416 | | |
5417 | | void ggml_compute_forward_diag_mask_inf( |
5418 | | const ggml_compute_params * params, |
5419 | 0 | ggml_tensor * dst) { |
5420 | |
|
5421 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5422 | |
|
5423 | 0 | switch (src0->type) { |
5424 | 0 | case GGML_TYPE_F32: |
5425 | 0 | { |
5426 | 0 | ggml_compute_forward_diag_mask_f32(params, dst, -INFINITY); |
5427 | 0 | } break; |
5428 | 0 | default: |
5429 | 0 | { |
5430 | 0 | GGML_ABORT("fatal error"); |
5431 | 0 | } |
5432 | 0 | } |
5433 | 0 | } |
5434 | | |
5435 | | void ggml_compute_forward_diag_mask_zero( |
5436 | | const ggml_compute_params * params, |
5437 | 0 | ggml_tensor * dst) { |
5438 | |
|
5439 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5440 | |
|
5441 | 0 | switch (src0->type) { |
5442 | 0 | case GGML_TYPE_F32: |
5443 | 0 | { |
5444 | 0 | ggml_compute_forward_diag_mask_f32(params, dst, 0); |
5445 | 0 | } break; |
5446 | 0 | default: |
5447 | 0 | { |
5448 | 0 | GGML_ABORT("fatal error"); |
5449 | 0 | } |
5450 | 0 | } |
5451 | 0 | } |
5452 | | |
5453 | | // ggml_compute_forward_soft_max |
5454 | | |
5455 | | static void ggml_compute_forward_soft_max_f32( |
5456 | | const ggml_compute_params * params, |
5457 | 0 | ggml_tensor * dst) { |
5458 | |
|
5459 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5460 | 0 | const ggml_tensor * src1 = dst->src[1]; |
5461 | 0 | const ggml_tensor * src2 = dst->src[2]; |
5462 | |
|
5463 | 0 | assert(ggml_is_contiguous(dst)); |
5464 | 0 | assert(ggml_are_same_shape(src0, dst)); |
5465 | |
|
5466 | 0 | float scale = 1.0f; |
5467 | 0 | float max_bias = 0.0f; |
5468 | |
|
5469 | 0 | memcpy(&scale, (float *) dst->op_params + 0, sizeof(float)); |
5470 | 0 | memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float)); |
5471 | |
|
5472 | 0 | const int ith = params->ith; |
5473 | 0 | const int nth = params->nth; |
5474 | |
|
5475 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
5476 | |
|
5477 | 0 | const int64_t nb11 = src1 ? src1->nb[1] : 1; |
5478 | 0 | const int64_t nb12 = src1 ? src1->nb[2] : 1; |
5479 | 0 | const int64_t nb13 = src1 ? src1->nb[3] : 1; |
5480 | |
|
5481 | 0 | const int64_t ne12 = src1 ? src1->ne[2] : 1; |
5482 | 0 | const int64_t ne13 = src1 ? src1->ne[3] : 1; |
5483 | | |
5484 | | // TODO: is this supposed to be ceil instead of floor? |
5485 | | // https://huggingface.co/mosaicml/mpt-7b/blob/main/attention.py#L370 |
5486 | 0 | const uint32_t n_head = ne02; |
5487 | 0 | const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); |
5488 | |
|
5489 | 0 | const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); |
5490 | 0 | const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); |
5491 | |
|
5492 | 0 | float * wp = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
5493 | |
|
5494 | 0 | const bool use_f16 = (src1 && src1->type == GGML_TYPE_F16); |
5495 | | |
5496 | | // sinks |
5497 | 0 | const float * sk = src2 ? (float *)((char *) src2->data) : nullptr; |
5498 | |
|
5499 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
5500 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
5501 | 0 | for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
5502 | 0 | const int64_t i11 = i01; |
5503 | 0 | const int64_t i12 = i02%ne12; |
5504 | 0 | const int64_t i13 = i03%ne13; |
5505 | | |
5506 | | // ALiBi |
5507 | 0 | const uint32_t h = i02; // head |
5508 | 0 | const float slope = (max_bias > 0.0f) ? h < n_head_log2 ? powf(m0, h + 1) : powf(m1, 2*(h - n_head_log2) + 1) : 1.0f; |
5509 | |
|
5510 | 0 | float * sp = (float *)((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
5511 | 0 | float * dp = (float *)((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
5512 | | |
5513 | | // broadcast the mask across rows |
5514 | 0 | ggml_fp16_t * mp_f16 = src1 ? (ggml_fp16_t *)((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13) : NULL; |
5515 | 0 | float * mp_f32 = src1 ? (float *)((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13) : NULL; |
5516 | |
|
5517 | 0 | ggml_vec_cpy_f32 (ne00, wp, sp); |
5518 | 0 | ggml_vec_scale_f32(ne00, wp, scale); |
5519 | 0 | if (mp_f32) { |
5520 | 0 | if (use_f16) { |
5521 | 0 | for (int i = 0; i < ne00; ++i) { |
5522 | 0 | wp[i] += slope*GGML_CPU_FP16_TO_FP32(mp_f16[i]); |
5523 | 0 | } |
5524 | 0 | } else { |
5525 | 0 | for (int i = 0; i < ne00; ++i) { |
5526 | 0 | wp[i] += slope*mp_f32[i]; |
5527 | 0 | } |
5528 | 0 | } |
5529 | 0 | } |
5530 | |
|
5531 | | #ifndef NDEBUG |
5532 | | for (int i = 0; i < ne00; ++i) { |
5533 | | //printf("p[%d] = %f\n", i, p[i]); |
5534 | | assert(!isnan(wp[i])); |
5535 | | } |
5536 | | #endif // NDEBUG |
5537 | |
|
5538 | 0 | float max = -INFINITY; |
5539 | 0 | ggml_vec_max_f32(ne00, &max, wp); |
5540 | | |
5541 | | // if we have sinks, make a correction as if they were included in the softmax |
5542 | 0 | if (sk) { |
5543 | 0 | max = MAX(max, sk[i02]); |
5544 | 0 | } |
5545 | |
|
5546 | 0 | ggml_float sum = ggml_vec_soft_max_f32(ne00, dp, wp, max); |
5547 | 0 | assert(sum > 0.0); |
5548 | |
|
5549 | 0 | if (sk) { |
5550 | 0 | sum += (ggml_float) expf(sk[i02] - max); |
5551 | 0 | } |
5552 | |
|
5553 | 0 | sum = 1.0/sum; |
5554 | 0 | ggml_vec_scale_f32(ne00, dp, sum); |
5555 | |
|
5556 | | #ifndef NDEBUG |
5557 | | for (int i = 0; i < ne00; ++i) { |
5558 | | assert(!isnan(dp[i])); |
5559 | | assert(!isinf(dp[i])); |
5560 | | } |
5561 | | #endif // NDEBUG |
5562 | 0 | } |
5563 | 0 | } |
5564 | 0 | } |
5565 | 0 | } |
5566 | | |
5567 | | void ggml_compute_forward_soft_max( |
5568 | | const ggml_compute_params * params, |
5569 | 0 | ggml_tensor * dst) { |
5570 | |
|
5571 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5572 | |
|
5573 | 0 | switch (src0->type) { |
5574 | 0 | case GGML_TYPE_F32: |
5575 | 0 | { |
5576 | 0 | ggml_compute_forward_soft_max_f32(params, dst); |
5577 | 0 | } break; |
5578 | 0 | default: |
5579 | 0 | { |
5580 | 0 | GGML_ABORT("fatal error"); |
5581 | 0 | } |
5582 | 0 | } |
5583 | 0 | } |
5584 | | |
5585 | | |
5586 | | // ggml_compute_forward_soft_max_ext_back |
5587 | | |
5588 | | static void ggml_compute_forward_soft_max_ext_back_f32( |
5589 | | const ggml_compute_params * params, |
5590 | 0 | ggml_tensor * dst) { |
5591 | |
|
5592 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5593 | 0 | const ggml_tensor * src1 = dst->src[1]; |
5594 | |
|
5595 | 0 | GGML_ASSERT(ggml_is_contiguous(src0)); |
5596 | 0 | GGML_ASSERT(ggml_is_contiguous(src1)); |
5597 | 0 | GGML_ASSERT(ggml_is_contiguous(dst)); |
5598 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
5599 | 0 | GGML_ASSERT(ggml_are_same_shape(src1, dst)); |
5600 | |
|
5601 | 0 | float scale = 1.0f; |
5602 | 0 | float max_bias = 0.0f; |
5603 | |
|
5604 | 0 | memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float)); |
5605 | 0 | memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float)); |
5606 | |
|
5607 | 0 | GGML_ASSERT(max_bias == 0.0f); |
5608 | | |
5609 | | // TODO: handle transposed/permuted matrices |
5610 | |
|
5611 | 0 | const int ith = params->ith; |
5612 | 0 | const int nth = params->nth; |
5613 | |
|
5614 | 0 | const int nc = src0->ne[0]; |
5615 | 0 | const int nr = ggml_nrows(src0); |
5616 | | |
5617 | | // rows per thread |
5618 | 0 | const int dr = (nr + nth - 1)/nth; |
5619 | | |
5620 | | // row range for this thread |
5621 | 0 | const int ir0 = dr*ith; |
5622 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
5623 | |
|
5624 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
5625 | 0 | float *dy = (float *)((char *) src0->data + i1*src0->nb[1]); |
5626 | 0 | float *y = (float *)((char *) src1->data + i1*src1->nb[1]); |
5627 | 0 | float *dx = (float *)((char *) dst->data + i1*dst->nb[1]); |
5628 | |
|
5629 | | #ifndef NDEBUG |
5630 | | for (int i = 0; i < nc; ++i) { |
5631 | | //printf("p[%d] = %f\n", i, p[i]); |
5632 | | assert(!isnan(dy[i])); |
5633 | | assert(!isnan(y[i])); |
5634 | | } |
5635 | | #endif // NDEBUG |
5636 | | // Jii = yi - yi*yi |
5637 | | // Jij = -yi*yj |
5638 | | // J = diag(y)-y.T*y |
5639 | | // dx = J * dy |
5640 | | // dxk = sum_i(Jki * dyi) |
5641 | | // dxk = sum_i(-yk*yi * dyi) - (-yk*yk)*dyk + (yk - yk*yk)*dyk |
5642 | | // dxk = sum_i(-yk*yi * dyi) + yk*yk*dyk + yk*dyk - yk*yk*dyk |
5643 | | // dxk = sum_i(-yk*yi * dyi) + yk*dyk |
5644 | | // dxk = -yk * sum_i(yi * dyi) + yk*dyk |
5645 | | // dxk = -yk * dot(y, dy) + yk*dyk |
5646 | | // dxk = yk * (- dot(y, dy) + dyk) |
5647 | | // dxk = yk * (dyk - dot(y, dy)) |
5648 | | // |
5649 | | // post-order: |
5650 | | // dot_y_dy := dot(y, dy) |
5651 | | // dx := dy |
5652 | | // dx := dx - dot_y_dy |
5653 | | // dx := dx * y |
5654 | | |
5655 | | // linear runtime, no additional memory |
5656 | 0 | float dot_y_dy = 0; |
5657 | 0 | ggml_vec_dot_f32 (nc, &dot_y_dy, 0, y, 0, dy, 0, 1); |
5658 | 0 | ggml_vec_cpy_f32 (nc, dx, dy); |
5659 | 0 | ggml_vec_acc1_f32 (nc, dx, -dot_y_dy); |
5660 | 0 | ggml_vec_mul_f32 (nc, dx, dx, y); |
5661 | 0 | ggml_vec_scale_f32(nc, dx, scale); |
5662 | |
|
5663 | | #ifndef NDEBUG |
5664 | | for (int i = 0; i < nc; ++i) { |
5665 | | assert(!isnan(dx[i])); |
5666 | | assert(!isinf(dx[i])); |
5667 | | } |
5668 | | #endif // NDEBUG |
5669 | 0 | } |
5670 | 0 | } |
5671 | | |
5672 | | void ggml_compute_forward_soft_max_ext_back( |
5673 | | const ggml_compute_params * params, |
5674 | 0 | ggml_tensor * dst) { |
5675 | |
|
5676 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5677 | |
|
5678 | 0 | switch (src0->type) { |
5679 | 0 | case GGML_TYPE_F32: |
5680 | 0 | { |
5681 | 0 | ggml_compute_forward_soft_max_ext_back_f32(params, dst); |
5682 | 0 | } break; |
5683 | 0 | default: |
5684 | 0 | { |
5685 | 0 | GGML_ABORT("fatal error"); |
5686 | 0 | } |
5687 | 0 | } |
5688 | 0 | } |
5689 | | |
5690 | | // ggml_compute_forward_clamp |
5691 | | |
5692 | | static void ggml_compute_forward_clamp_f32( |
5693 | | const ggml_compute_params * params, |
5694 | 0 | ggml_tensor * dst) { |
5695 | |
|
5696 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5697 | |
|
5698 | 0 | float min; |
5699 | 0 | float max; |
5700 | 0 | memcpy(&min, (float *) dst->op_params + 0, sizeof(float)); |
5701 | 0 | memcpy(&max, (float *) dst->op_params + 1, sizeof(float)); |
5702 | |
|
5703 | 0 | const int ith = params->ith; |
5704 | 0 | const int nth = params->nth; |
5705 | |
|
5706 | 0 | const int n = ggml_nrows(src0); |
5707 | 0 | const int nc = src0->ne[0]; |
5708 | |
|
5709 | 0 | const size_t nb00 = src0->nb[0]; |
5710 | 0 | const size_t nb01 = src0->nb[1]; |
5711 | |
|
5712 | 0 | const size_t nb0 = dst->nb[0]; |
5713 | 0 | const size_t nb1 = dst->nb[1]; |
5714 | |
|
5715 | 0 | GGML_ASSERT( nb0 == sizeof(float)); |
5716 | 0 | GGML_ASSERT(nb00 == sizeof(float)); |
5717 | |
|
5718 | 0 | for (int j = ith; j < n; j += nth) { |
5719 | 0 | float * dst_ptr = (float *) ((char *) dst->data + j*nb1); |
5720 | 0 | float * src0_ptr = (float *) ((char *) src0->data + j*nb01); |
5721 | |
|
5722 | 0 | for (int i = 0; i < nc; i++) { |
5723 | 0 | dst_ptr[i] = MAX(MIN(src0_ptr[i], max), min); |
5724 | 0 | } |
5725 | 0 | } |
5726 | 0 | } |
5727 | | |
5728 | | static void ggml_compute_forward_clamp_f16( |
5729 | | const ggml_compute_params * params, |
5730 | 0 | ggml_tensor * dst) { |
5731 | |
|
5732 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5733 | |
|
5734 | 0 | float min; |
5735 | 0 | float max; |
5736 | 0 | memcpy(&min, (float *) dst->op_params + 0, sizeof(float)); |
5737 | 0 | memcpy(&max, (float *) dst->op_params + 1, sizeof(float)); |
5738 | |
|
5739 | 0 | const int ith = params->ith; |
5740 | 0 | const int nth = params->nth; |
5741 | |
|
5742 | 0 | const int n = ggml_nrows(src0); |
5743 | 0 | const int nc = src0->ne[0]; |
5744 | |
|
5745 | 0 | const size_t nb00 = src0->nb[0]; |
5746 | 0 | const size_t nb01 = src0->nb[1]; |
5747 | |
|
5748 | 0 | const size_t nb0 = dst->nb[0]; |
5749 | 0 | const size_t nb1 = dst->nb[1]; |
5750 | |
|
5751 | 0 | GGML_ASSERT( nb0 == sizeof(ggml_fp16_t)); |
5752 | 0 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
5753 | |
|
5754 | 0 | for (int j = ith; j < n; j += nth) { |
5755 | 0 | ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + j*nb1); |
5756 | 0 | ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + j*nb01); |
5757 | |
|
5758 | 0 | for (int i = 0; i < nc; i++) { |
5759 | 0 | float v = GGML_CPU_FP16_TO_FP32(src0_ptr[i]); |
5760 | 0 | dst_ptr[i] = GGML_CPU_FP32_TO_FP16(MAX(MIN(v, max), min)); |
5761 | 0 | } |
5762 | 0 | } |
5763 | 0 | } |
5764 | | |
5765 | | void ggml_compute_forward_clamp( |
5766 | | const ggml_compute_params * params, |
5767 | 0 | ggml_tensor * dst) { |
5768 | |
|
5769 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5770 | |
|
5771 | 0 | switch (src0->type) { |
5772 | 0 | case GGML_TYPE_F32: |
5773 | 0 | { |
5774 | 0 | ggml_compute_forward_clamp_f32(params, dst); |
5775 | 0 | } break; |
5776 | 0 | case GGML_TYPE_F16: |
5777 | 0 | { |
5778 | 0 | ggml_compute_forward_clamp_f16(params, dst); |
5779 | 0 | } break; |
5780 | 0 | case GGML_TYPE_BF16: |
5781 | 0 | case GGML_TYPE_Q1_0: |
5782 | 0 | case GGML_TYPE_Q2_0: |
5783 | 0 | case GGML_TYPE_Q4_0: |
5784 | 0 | case GGML_TYPE_Q4_1: |
5785 | 0 | case GGML_TYPE_Q5_0: |
5786 | 0 | case GGML_TYPE_Q5_1: |
5787 | 0 | case GGML_TYPE_Q8_0: |
5788 | 0 | case GGML_TYPE_Q8_1: |
5789 | 0 | case GGML_TYPE_MXFP4: |
5790 | 0 | case GGML_TYPE_NVFP4: |
5791 | 0 | case GGML_TYPE_Q2_K: |
5792 | 0 | case GGML_TYPE_Q3_K: |
5793 | 0 | case GGML_TYPE_Q4_K: |
5794 | 0 | case GGML_TYPE_Q5_K: |
5795 | 0 | case GGML_TYPE_Q6_K: |
5796 | 0 | case GGML_TYPE_TQ1_0: |
5797 | 0 | case GGML_TYPE_TQ2_0: |
5798 | 0 | case GGML_TYPE_IQ2_XXS: |
5799 | 0 | case GGML_TYPE_IQ2_XS: |
5800 | 0 | case GGML_TYPE_IQ3_XXS: |
5801 | 0 | case GGML_TYPE_IQ1_S: |
5802 | 0 | case GGML_TYPE_IQ1_M: |
5803 | 0 | case GGML_TYPE_IQ4_NL: |
5804 | 0 | case GGML_TYPE_IQ4_XS: |
5805 | 0 | case GGML_TYPE_IQ3_S: |
5806 | 0 | case GGML_TYPE_IQ2_S: |
5807 | 0 | case GGML_TYPE_Q8_K: |
5808 | 0 | case GGML_TYPE_I8: |
5809 | 0 | case GGML_TYPE_I16: |
5810 | 0 | case GGML_TYPE_I32: |
5811 | 0 | case GGML_TYPE_I64: |
5812 | 0 | case GGML_TYPE_F64: |
5813 | 0 | case GGML_TYPE_COUNT: |
5814 | 0 | { |
5815 | 0 | GGML_ABORT("fatal error"); |
5816 | 0 | } |
5817 | 0 | } |
5818 | 0 | } |
5819 | | |
5820 | | // ggml_compute_forward_rope |
5821 | | |
5822 | 0 | static float rope_yarn_ramp(const float low, const float high, const int i0) { |
5823 | 0 | const float y = (i0 / 2 - low) / MAX(0.001f, high - low); |
5824 | 0 | return 1 - MIN(1, MAX(0, y)); |
5825 | 0 | } |
5826 | | |
5827 | | // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn |
5828 | | // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng. |
5829 | | static void rope_yarn( |
5830 | | float theta_extrap, float freq_scale, float corr_dims[2], int64_t i0, float ext_factor, float mscale, |
5831 | 0 | float * cos_theta, float * sin_theta) { |
5832 | | // Get n-d rotational scaling corrected for extrapolation |
5833 | 0 | float theta_interp = freq_scale * theta_extrap; |
5834 | 0 | float theta = theta_interp; |
5835 | 0 | if (ext_factor != 0.0f) { |
5836 | 0 | float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor; |
5837 | 0 | theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix; |
5838 | | |
5839 | | // Get n-d magnitude scaling corrected for interpolation |
5840 | 0 | mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale); |
5841 | 0 | } |
5842 | 0 | *cos_theta = cosf(theta) * mscale; |
5843 | 0 | *sin_theta = sinf(theta) * mscale; |
5844 | 0 | } |
5845 | | |
5846 | | static void ggml_rope_cache_init( |
5847 | | float theta_base, float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale, |
5848 | 0 | float * cache, float sin_sign, float theta_scale) { |
5849 | | // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py |
5850 | 0 | float theta = theta_base; |
5851 | 0 | for (int64_t i0 = 0; i0 < ne0; i0 += 2) { |
5852 | 0 | const float ff = freq_factors ? freq_factors[i0/2] : 1.0f; |
5853 | 0 | rope_yarn( |
5854 | 0 | theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1] |
5855 | 0 | ); |
5856 | 0 | cache[i0 + 1] *= sin_sign; |
5857 | |
|
5858 | 0 | theta *= theta_scale; |
5859 | 0 | } |
5860 | 0 | } |
5861 | | |
5862 | | static void ggml_mrope_cache_init( |
5863 | | float theta_base_t, float theta_base_h, float theta_base_w, float theta_base_e, int sections[4], bool is_imrope, bool indep_sects, |
5864 | | float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale, |
5865 | 0 | float * cache, float sin_sign, float theta_scale) { |
5866 | | // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py |
5867 | 0 | float theta_t = theta_base_t; |
5868 | 0 | float theta_h = theta_base_h; |
5869 | 0 | float theta_w = theta_base_w; |
5870 | 0 | float theta_e = theta_base_e; // extra position id for vision encoder |
5871 | 0 | int sect_dims = sections[0] + sections[1] + sections[2] + sections[3]; |
5872 | 0 | int sec_w = sections[1] + sections[0]; |
5873 | 0 | int sec_e = sections[2] + sec_w; |
5874 | 0 | GGML_ASSERT(sect_dims <= ne0); |
5875 | |
|
5876 | 0 | for (int64_t i0 = 0; i0 < ne0; i0 += 2) { |
5877 | 0 | const float ff = freq_factors ? freq_factors[i0/2] : 1.0f; |
5878 | |
|
5879 | 0 | int sector = (i0 / 2) % sect_dims; |
5880 | 0 | if (indep_sects) { |
5881 | | // compute theta independently for each dim sections |
5882 | | // (i.e. reset corresponding theta when `i0` go from one section to another) |
5883 | 0 | if (sector == 0) { |
5884 | 0 | theta_t = theta_base_t; |
5885 | 0 | } |
5886 | 0 | else if (sector == sections[0]) { |
5887 | 0 | theta_h = theta_base_h;; |
5888 | 0 | } |
5889 | 0 | else if (sector == sec_w) { |
5890 | 0 | theta_w = theta_base_w; |
5891 | 0 | } |
5892 | 0 | else if (sector == sec_e) { |
5893 | 0 | theta_e = theta_base_e; |
5894 | 0 | } |
5895 | 0 | } |
5896 | |
|
5897 | 0 | float theta = theta_t; |
5898 | 0 | if (is_imrope) { // qwen3vl apply interleaved mrope |
5899 | 0 | if (sector % 3 == 1 && sector < 3 * sections[1]) { |
5900 | 0 | theta = theta_h; |
5901 | 0 | } else if (sector % 3 == 2 && sector < 3 * sections[2]) { |
5902 | 0 | theta = theta_w; |
5903 | 0 | } else if (sector % 3 == 0 && sector < 3 * sections[0]) { |
5904 | 0 | theta = theta_t; |
5905 | 0 | } else { |
5906 | 0 | theta = theta_e; |
5907 | 0 | } |
5908 | 0 | } else { |
5909 | 0 | if (sector >= sections[0] && sector < sec_w) { |
5910 | 0 | theta = theta_h; |
5911 | 0 | } |
5912 | 0 | else if (sector >= sec_w && sector < sec_w + sections[2]) { |
5913 | 0 | theta = theta_w; |
5914 | 0 | } |
5915 | 0 | else if (sector >= sec_w + sections[2]) { |
5916 | 0 | theta = theta_e; |
5917 | 0 | } |
5918 | 0 | } |
5919 | |
|
5920 | 0 | rope_yarn( |
5921 | 0 | theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1] |
5922 | 0 | ); |
5923 | 0 | cache[i0 + 1] *= sin_sign; |
5924 | |
|
5925 | 0 | theta_t *= theta_scale; |
5926 | 0 | theta_w *= theta_scale; |
5927 | 0 | theta_h *= theta_scale; |
5928 | 0 | theta_e *= theta_scale; |
5929 | 0 | } |
5930 | 0 | } |
5931 | | |
5932 | | |
5933 | | template<typename T> |
5934 | 0 | static void rotate_pairs(const int64_t n, const int64_t n_offset, const float * cache, const T * src_data, T * dst_data, const int scale = 2) { |
5935 | 0 | for (int64_t i0 = 0; i0 < n; i0 += 2) { |
5936 | 0 | const int64_t ic = i0/scale; // hack for GGML_ROPE_TYPE_NORMAL, where we need ic = i0; for all other cases, ic = i0/2 |
5937 | |
|
5938 | 0 | const float cos_theta = cache[i0 + 0]; |
5939 | 0 | const float sin_theta = cache[i0 + 1]; |
5940 | |
|
5941 | 0 | const T * const src = src_data + ic; |
5942 | 0 | T * dst = dst_data + ic; |
5943 | |
|
5944 | 0 | const float x0 = type_conversion_table<T>::to_f32(src[0]); |
5945 | 0 | const float x1 = type_conversion_table<T>::to_f32(src[n_offset]); |
5946 | |
|
5947 | 0 | dst[0] = type_conversion_table<T>::from_f32(x0*cos_theta - x1*sin_theta); |
5948 | 0 | dst[n_offset] = type_conversion_table<T>::from_f32(x0*sin_theta + x1*cos_theta); |
5949 | 0 | } |
5950 | 0 | } Unexecuted instantiation: ops.cpp:void rotate_pairs<unsigned short>(long, long, float const*, unsigned short const*, unsigned short*, int) Unexecuted instantiation: ops.cpp:void rotate_pairs<float>(long, long, float const*, float const*, float*, int) |
5951 | | |
5952 | | template<typename T> //float or ggml_fp16_t |
5953 | | static void ggml_compute_forward_rope_flt( |
5954 | | const ggml_compute_params * params, |
5955 | | ggml_tensor * dst, |
5956 | 0 | const bool forward) { |
5957 | |
|
5958 | 0 | const ggml_tensor * src0 = dst->src[0]; |
5959 | 0 | const ggml_tensor * src1 = dst->src[1]; |
5960 | 0 | const ggml_tensor * src2 = dst->src[2]; |
5961 | |
|
5962 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); |
5963 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_I32); |
5964 | |
|
5965 | 0 | float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow; |
5966 | 0 | int sections[4]; |
5967 | | |
5968 | | //const int n_past = ((int32_t *) dst->op_params)[0]; |
5969 | 0 | const int n_dims = ((int32_t *) dst->op_params)[1]; |
5970 | 0 | const int mode = ((int32_t *) dst->op_params)[2]; |
5971 | | //const int n_ctx = ((int32_t *) dst->op_params)[3]; |
5972 | 0 | const int n_ctx_orig = ((int32_t *) dst->op_params)[4]; |
5973 | |
|
5974 | 0 | memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float)); |
5975 | 0 | memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float)); |
5976 | 0 | memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float)); |
5977 | 0 | memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float)); |
5978 | 0 | memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float)); |
5979 | 0 | memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float)); |
5980 | 0 | memcpy(§ions, (int32_t *) dst->op_params + 11, sizeof(int)*4); |
5981 | |
|
5982 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
5983 | | |
5984 | | //printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3); |
5985 | | //printf("n_past = %d, ne2 = %d\n", n_past, ne2); |
5986 | |
|
5987 | 0 | GGML_ASSERT(nb0 == nb00); |
5988 | 0 | GGML_ASSERT(nb0 == sizeof(T)); |
5989 | |
|
5990 | 0 | const int ith = params->ith; |
5991 | 0 | const int nth = params->nth; |
5992 | |
|
5993 | 0 | const int nr = ggml_nrows(dst); |
5994 | |
|
5995 | 0 | GGML_ASSERT(n_dims <= ne0); |
5996 | 0 | GGML_ASSERT(n_dims % 2 == 0); |
5997 | | |
5998 | | // rows per thread |
5999 | 0 | const int dr = (nr + nth - 1)/nth; |
6000 | | |
6001 | | // row range for this thread |
6002 | 0 | const int ir0 = dr*ith; |
6003 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
6004 | | |
6005 | | // row index used to determine which thread to use |
6006 | 0 | int ir = 0; |
6007 | |
|
6008 | 0 | const float theta_scale = powf(freq_base, -2.0f/n_dims); |
6009 | |
|
6010 | 0 | float corr_dims[2]; |
6011 | 0 | ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims); |
6012 | |
|
6013 | 0 | const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE; // qwen3vl apply interleaved mrope |
6014 | 0 | const bool mrope_used = mode & GGML_ROPE_TYPE_MROPE; // ggml_rope_multi, note: also true for vision (24 & 8 == true) and for imrope |
6015 | 0 | const bool is_vision = mode == GGML_ROPE_TYPE_VISION; |
6016 | |
|
6017 | 0 | if (mrope_used) { |
6018 | 0 | GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0); |
6019 | 0 | } |
6020 | |
|
6021 | 0 | if (is_vision) { |
6022 | 0 | GGML_ASSERT(n_dims == ne0/2); |
6023 | 0 | } |
6024 | |
|
6025 | 0 | const float * freq_factors = NULL; |
6026 | 0 | if (src2 != NULL) { |
6027 | 0 | GGML_ASSERT(src2->type == GGML_TYPE_F32); |
6028 | 0 | GGML_ASSERT(src2->ne[0] >= n_dims / 2); |
6029 | 0 | freq_factors = (const float *) src2->data; |
6030 | 0 | } |
6031 | | |
6032 | | // backward process uses inverse rotation by cos and sin. |
6033 | | // cos and sin build a rotation matrix, where the inverse is the transpose. |
6034 | | // this essentially just switches the sign of sin. |
6035 | 0 | const float sin_sign = forward ? 1.0f : -1.0f; |
6036 | |
|
6037 | 0 | const int32_t * pos = (const int32_t *) src1->data; |
6038 | |
|
6039 | 0 | int64_t last_i2 = -1; |
6040 | |
|
6041 | 0 | for (int64_t i3 = 0; i3 < ne3; i3++) { // batch |
6042 | 0 | for (int64_t i2 = 0; i2 < ne2; i2++) { // seq-len |
6043 | 0 | for (int64_t i1 = 0; i1 < ne1; i1++) { // attn-heads |
6044 | 0 | if (ir++ < ir0) continue; // skip rows mapped to other threads |
6045 | 0 | if (ir > ir1) break; |
6046 | | |
6047 | 0 | float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith; |
6048 | 0 | if (last_i2 != i2) { |
6049 | 0 | if (!mrope_used) { |
6050 | 0 | const int64_t p = pos[i2]; |
6051 | 0 | ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale); |
6052 | 0 | } |
6053 | 0 | else { |
6054 | 0 | const int64_t p_t = pos[i2]; |
6055 | 0 | const int64_t p_h = pos[i2 + ne2]; |
6056 | 0 | const int64_t p_w = pos[i2 + ne2 * 2]; |
6057 | 0 | const int64_t p_e = pos[i2 + ne2 * 3]; |
6058 | 0 | ggml_mrope_cache_init( |
6059 | 0 | p_t, p_h, p_w, p_e, sections, is_imrope, is_vision, |
6060 | 0 | freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale); |
6061 | 0 | } |
6062 | |
|
6063 | 0 | last_i2 = i2; |
6064 | 0 | } |
6065 | |
|
6066 | 0 | T * src = (T *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
6067 | 0 | T * dst_data = (T *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1); |
6068 | |
|
6069 | 0 | switch (mode) { |
6070 | 0 | case GGML_ROPE_TYPE_NORMAL: |
6071 | 0 | rotate_pairs<T>(n_dims, 1, cache, src, dst_data, 1); |
6072 | 0 | break; |
6073 | 0 | case GGML_ROPE_TYPE_NEOX: |
6074 | 0 | case GGML_ROPE_TYPE_MROPE: |
6075 | 0 | case GGML_ROPE_TYPE_IMROPE: |
6076 | 0 | rotate_pairs<T>(n_dims, n_dims/2, cache, src, dst_data); |
6077 | 0 | break; |
6078 | 0 | case GGML_ROPE_TYPE_VISION: |
6079 | 0 | rotate_pairs<T>(ne0, n_dims, cache, src, dst_data); |
6080 | 0 | break; |
6081 | 0 | default: |
6082 | 0 | GGML_ABORT("rope type not supported"); |
6083 | 0 | } |
6084 | | |
6085 | 0 | if (!is_vision) { |
6086 | | // fill the remain channels with data from src tensor |
6087 | 0 | for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) { |
6088 | 0 | const T * const src = (T *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00); |
6089 | 0 | T * dst_data = (T *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0); |
6090 | |
|
6091 | 0 | dst_data[0] = src[0]; |
6092 | 0 | dst_data[1] = src[1]; |
6093 | 0 | } |
6094 | 0 | } |
6095 | 0 | } //attn-heads |
6096 | 0 | } |
6097 | 0 | } |
6098 | 0 | } Unexecuted instantiation: ops.cpp:void ggml_compute_forward_rope_flt<unsigned short>(ggml_compute_params const*, ggml_tensor*, bool) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_rope_flt<float>(ggml_compute_params const*, ggml_tensor*, bool) |
6099 | | |
6100 | | void ggml_compute_forward_rope( |
6101 | | const ggml_compute_params * params, |
6102 | 0 | ggml_tensor * dst) { |
6103 | |
|
6104 | 0 | const ggml_tensor * src0 = dst->src[0]; |
6105 | |
|
6106 | 0 | switch (src0->type) { |
6107 | 0 | case GGML_TYPE_F16: |
6108 | 0 | { |
6109 | 0 | ggml_compute_forward_rope_flt<ggml_fp16_t>(params, dst, true); |
6110 | 0 | } break; |
6111 | 0 | case GGML_TYPE_F32: |
6112 | 0 | { |
6113 | 0 | ggml_compute_forward_rope_flt<float>(params, dst, true); |
6114 | 0 | } break; |
6115 | 0 | default: |
6116 | 0 | { |
6117 | 0 | GGML_ABORT("fatal error"); |
6118 | 0 | } |
6119 | 0 | } |
6120 | 0 | } |
6121 | | |
6122 | | // ggml_compute_forward_rope_back |
6123 | | |
6124 | | void ggml_compute_forward_rope_back( |
6125 | | const ggml_compute_params * params, |
6126 | 0 | ggml_tensor * dst) { |
6127 | |
|
6128 | 0 | const ggml_tensor * src0 = dst->src[0]; |
6129 | |
|
6130 | 0 | switch (src0->type) { |
6131 | 0 | case GGML_TYPE_F16: |
6132 | 0 | { |
6133 | 0 | ggml_compute_forward_rope_flt<ggml_fp16_t>(params, dst, false); |
6134 | 0 | } break; |
6135 | 0 | case GGML_TYPE_F32: |
6136 | 0 | { |
6137 | 0 | ggml_compute_forward_rope_flt<float>(params, dst, false); |
6138 | 0 | } break; |
6139 | 0 | default: |
6140 | 0 | { |
6141 | 0 | GGML_ABORT("fatal error"); |
6142 | 0 | } |
6143 | 0 | } |
6144 | 0 | } |
6145 | | |
6146 | | // ggml_compute_forward_conv_transpose_1d |
6147 | | |
6148 | | static void ggml_compute_forward_conv_transpose_1d_f16_f32( |
6149 | | const ggml_compute_params * params, |
6150 | 0 | ggml_tensor * dst) { |
6151 | |
|
6152 | 0 | const ggml_tensor * src0 = dst->src[0]; |
6153 | 0 | const ggml_tensor * src1 = dst->src[1]; |
6154 | |
|
6155 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F16); |
6156 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
6157 | 0 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
6158 | |
|
6159 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
6160 | |
|
6161 | 0 | const int ith = params->ith; |
6162 | 0 | const int nth = params->nth; |
6163 | |
|
6164 | 0 | const int nk = ne00*ne01*ne02; |
6165 | |
|
6166 | 0 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
6167 | 0 | GGML_ASSERT(nb10 == sizeof(float)); |
6168 | |
|
6169 | 0 | if (ith == 0) { |
6170 | 0 | memset(params->wdata, 0, params->wsize); |
6171 | | |
6172 | | // permute kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout) |
6173 | 0 | { |
6174 | 0 | ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0; |
6175 | |
|
6176 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
6177 | 0 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
6178 | 0 | const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i02*nb02 + i01*nb01); |
6179 | 0 | ggml_fp16_t * dst_data = wdata + i01*ne00*ne02; |
6180 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
6181 | 0 | dst_data[i00*ne02 + i02] = src[i00]; |
6182 | 0 | } |
6183 | 0 | } |
6184 | 0 | } |
6185 | 0 | } |
6186 | | |
6187 | | // permute source data (src1) from (L x Cin) to (Cin x L) |
6188 | 0 | { |
6189 | 0 | ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk; |
6190 | 0 | ggml_fp16_t * dst_data = wdata; |
6191 | |
|
6192 | 0 | for (int64_t i11 = 0; i11 < ne11; i11++) { |
6193 | 0 | const float * const src = (float *)((char *) src1->data + i11*nb11); |
6194 | 0 | for (int64_t i10 = 0; i10 < ne10; i10++) { |
6195 | 0 | dst_data[i10*ne11 + i11] = GGML_CPU_FP32_TO_FP16(src[i10]); |
6196 | 0 | } |
6197 | 0 | } |
6198 | 0 | } |
6199 | | |
6200 | | // need to zero dst since we are accumulating into it |
6201 | 0 | memset(dst->data, 0, ggml_nbytes(dst)); |
6202 | 0 | } |
6203 | 0 | ggml_barrier(params->threadpool); |
6204 | |
|
6205 | 0 | const int32_t s0 = ((const int32_t*)(dst->op_params))[0]; |
6206 | | |
6207 | | // total rows in dst |
6208 | 0 | const int nr = ne1; |
6209 | | |
6210 | | // rows per thread |
6211 | 0 | const int dr = (nr + nth - 1)/nth; |
6212 | | |
6213 | | // row range for this thread |
6214 | 0 | const int ir0 = dr*ith; |
6215 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
6216 | |
|
6217 | 0 | ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0; |
6218 | 0 | ggml_fp16_t * const wdata_src = wdata + nk; |
6219 | |
|
6220 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
6221 | 0 | float * dst_data = (float *)((char *) dst->data + i1*nb1); |
6222 | 0 | ggml_fp16_t * wdata_kernel = wdata + i1*ne02*ne00; |
6223 | 0 | for (int i10 = 0; i10 < ne10; i10++) { |
6224 | 0 | const int i1n = i10*ne11; |
6225 | 0 | for (int i00 = 0; i00 < ne00; i00++) { |
6226 | 0 | float v = 0; |
6227 | 0 | ggml_vec_dot_f16(ne02, &v, 0, |
6228 | 0 | (ggml_fp16_t *) wdata_src + i1n, 0, |
6229 | 0 | (ggml_fp16_t *) wdata_kernel + i00*ne02, 0, 1); |
6230 | 0 | dst_data[i10*s0 + i00] += v; |
6231 | 0 | } |
6232 | 0 | } |
6233 | 0 | } |
6234 | 0 | } |
6235 | | |
6236 | | static void ggml_compute_forward_conv_transpose_1d_f32( |
6237 | | const ggml_compute_params * params, |
6238 | 0 | ggml_tensor * dst) { |
6239 | |
|
6240 | 0 | const ggml_tensor * src0 = dst->src[0]; |
6241 | 0 | const ggml_tensor * src1 = dst->src[1]; |
6242 | |
|
6243 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
6244 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
6245 | 0 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
6246 | |
|
6247 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
6248 | |
|
6249 | 0 | const int ith = params->ith; |
6250 | 0 | const int nth = params->nth; |
6251 | |
|
6252 | 0 | const int nk = ne00*ne01*ne02; |
6253 | |
|
6254 | 0 | GGML_ASSERT(nb00 == sizeof(float)); |
6255 | 0 | GGML_ASSERT(nb10 == sizeof(float)); |
6256 | |
|
6257 | 0 | if (ith == 0) { |
6258 | 0 | memset(params->wdata, 0, params->wsize); |
6259 | | |
6260 | | // prepare kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout) |
6261 | 0 | { |
6262 | 0 | float * const wdata = (float *) params->wdata + 0; |
6263 | |
|
6264 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
6265 | 0 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
6266 | 0 | const float * const src = (float *)((char *) src0->data + i02*nb02 + i01*nb01); |
6267 | 0 | float * dst_data = wdata + i01*ne00*ne02; |
6268 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
6269 | 0 | dst_data[i00*ne02 + i02] = src[i00]; |
6270 | 0 | } |
6271 | 0 | } |
6272 | 0 | } |
6273 | 0 | } |
6274 | | |
6275 | | // prepare source data (src1) |
6276 | 0 | { |
6277 | 0 | float * const wdata = (float *) params->wdata + nk; |
6278 | 0 | float * dst_data = wdata; |
6279 | |
|
6280 | 0 | for (int64_t i11 = 0; i11 < ne11; i11++) { |
6281 | 0 | const float * const src = (float *)((char *) src1->data + i11*nb11); |
6282 | 0 | for (int64_t i10 = 0; i10 < ne10; i10++) { |
6283 | 0 | dst_data[i10*ne11 + i11] = src[i10]; |
6284 | 0 | } |
6285 | 0 | } |
6286 | 0 | } |
6287 | | |
6288 | | // need to zero dst since we are accumulating into it |
6289 | 0 | memset(dst->data, 0, ggml_nbytes(dst)); |
6290 | 0 | } |
6291 | 0 | ggml_barrier(params->threadpool); |
6292 | |
|
6293 | 0 | const int32_t s0 = ((const int32_t*)(dst->op_params))[0]; |
6294 | | |
6295 | | // total rows in dst |
6296 | 0 | const int nr = ne1; |
6297 | | |
6298 | | // rows per thread |
6299 | 0 | const int dr = (nr + nth - 1)/nth; |
6300 | | |
6301 | | // row range for this thread |
6302 | 0 | const int ir0 = dr*ith; |
6303 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
6304 | |
|
6305 | 0 | float * const wdata = (float *) params->wdata + 0; |
6306 | 0 | float * const wdata_src = wdata + nk; |
6307 | |
|
6308 | 0 | for (int i1 = ir0; i1 < ir1; i1++) { |
6309 | 0 | float * dst_data = (float *)((char *) dst->data + i1*nb1); |
6310 | 0 | float * wdata_kernel = wdata + i1*ne02*ne00; |
6311 | 0 | for (int i10 = 0; i10 < ne10; i10++) { |
6312 | 0 | const int i1n = i10*ne11; |
6313 | 0 | for (int i00 = 0; i00 < ne00; i00++) { |
6314 | 0 | float v = 0; |
6315 | 0 | ggml_vec_dot_f32(ne02, &v, 0, |
6316 | 0 | wdata_src + i1n, 0, |
6317 | 0 | wdata_kernel + i00*ne02, 0, 1); |
6318 | 0 | dst_data[i10*s0 + i00] += v; |
6319 | 0 | } |
6320 | 0 | } |
6321 | 0 | } |
6322 | 0 | } |
6323 | | |
6324 | | void ggml_compute_forward_conv_transpose_1d( |
6325 | | const ggml_compute_params * params, |
6326 | 0 | ggml_tensor * dst) { |
6327 | |
|
6328 | 0 | const ggml_tensor * src0 = dst->src[0]; |
6329 | |
|
6330 | 0 | switch (src0->type) { |
6331 | 0 | case GGML_TYPE_F16: |
6332 | 0 | { |
6333 | 0 | ggml_compute_forward_conv_transpose_1d_f16_f32(params, dst); |
6334 | 0 | } break; |
6335 | 0 | case GGML_TYPE_F32: |
6336 | 0 | { |
6337 | 0 | ggml_compute_forward_conv_transpose_1d_f32(params, dst); |
6338 | 0 | } break; |
6339 | 0 | default: |
6340 | 0 | { |
6341 | 0 | GGML_ABORT("fatal error"); |
6342 | 0 | } |
6343 | 0 | } |
6344 | 0 | } |
6345 | | |
6346 | | // ggml_compute_forward_im2col_f32 |
6347 | | // src0: kernel [OC, IC, KH, KW] |
6348 | | // src1: image [N, IC, IH, IW] |
6349 | | // dst: result [N, OH, OW, IC*KH*KW] |
6350 | | static void ggml_compute_forward_im2col_f32( |
6351 | | const ggml_compute_params * params, |
6352 | 0 | ggml_tensor * dst) { |
6353 | |
|
6354 | 0 | const ggml_tensor * src0 = dst->src[0]; |
6355 | 0 | const ggml_tensor * src1 = dst->src[1]; |
6356 | |
|
6357 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
6358 | 0 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
6359 | |
|
6360 | 0 | GGML_TENSOR_BINARY_OP_LOCALS; |
6361 | |
|
6362 | 0 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
6363 | 0 | const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
6364 | 0 | const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
6365 | 0 | const int32_t p1 = ((const int32_t *)(dst->op_params))[3]; |
6366 | 0 | const int32_t d0 = ((const int32_t *)(dst->op_params))[4]; |
6367 | 0 | const int32_t d1 = ((const int32_t *)(dst->op_params))[5]; |
6368 | 0 | const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1; |
6369 | |
|
6370 | 0 | const int ith = params->ith; |
6371 | 0 | const int nth = params->nth; |
6372 | |
|
6373 | 0 | const int64_t N = is_2D ? ne13 : ne12; |
6374 | 0 | const int64_t IC = is_2D ? ne12 : ne11; |
6375 | 0 | const int64_t IH = is_2D ? ne11 : 1; |
6376 | 0 | const int64_t IW = ne10; |
6377 | |
|
6378 | 0 | const int64_t KH = is_2D ? ne01 : 1; |
6379 | 0 | const int64_t KW = ne00; |
6380 | |
|
6381 | 0 | const int64_t OH = is_2D ? ne2 : 1; |
6382 | 0 | const int64_t OW = ne1; |
6383 | |
|
6384 | 0 | int ofs0 = is_2D ? nb13 : nb12; |
6385 | 0 | int ofs1 = is_2D ? nb12 : nb11; |
6386 | |
|
6387 | 0 | GGML_ASSERT(nb10 == sizeof(float)); |
6388 | | |
6389 | | // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] |
6390 | 0 | { |
6391 | 0 | float * const wdata = (float *) dst->data; |
6392 | |
|
6393 | 0 | for (int64_t in = 0; in < N; in++) { |
6394 | 0 | for (int64_t ioh = 0; ioh < OH; ioh++) { // 1 |
6395 | 0 | for (int64_t iow = 0; iow < OW; iow++) { |
6396 | 0 | for (int64_t iic = ith; iic < IC; iic += nth) { |
6397 | | |
6398 | | // micro kernel |
6399 | 0 | float * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW] |
6400 | 0 | const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW] |
6401 | |
|
6402 | 0 | for (int64_t ikh = 0; ikh < KH; ikh++) { // 1 |
6403 | 0 | for (int64_t ikw = 0; ikw < KW; ikw++) { |
6404 | 0 | const int64_t iiw = iow*s0 + ikw*d0 - p0; |
6405 | 0 | const int64_t iih = ioh*s1 + ikh*d1 - p1; |
6406 | |
|
6407 | 0 | if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) { |
6408 | 0 | dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0; |
6409 | 0 | } else { |
6410 | 0 | dst_data[iic*(KH*KW) + ikh*KW + ikw] = (src_data[iih*IW + iiw]); |
6411 | 0 | } |
6412 | 0 | } |
6413 | 0 | } |
6414 | 0 | } |
6415 | 0 | } |
6416 | 0 | } |
6417 | 0 | } |
6418 | 0 | } |
6419 | 0 | } |
6420 | | |
6421 | | |
6422 | | // ggml_compute_forward_im2col_f16 |
6423 | | // src0: kernel [OC, IC, KH, KW] |
6424 | | // src1: image [N, IC, IH, IW] |
6425 | | // dst: result [N, OH, OW, IC*KH*KW] |
6426 | | static void ggml_compute_forward_im2col_f16( |
6427 | | const ggml_compute_params * params, |
6428 | 0 | ggml_tensor * dst) { |
6429 | |
|
6430 | 0 | const ggml_tensor * src0 = dst->src[0]; |
6431 | 0 | const ggml_tensor * src1 = dst->src[1]; |
6432 | |
|
6433 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F16 || src1->type == GGML_TYPE_F32); |
6434 | 0 | GGML_ASSERT( dst->type == GGML_TYPE_F16); |
6435 | |
|
6436 | 0 | GGML_TENSOR_BINARY_OP_LOCALS; |
6437 | |
|
6438 | 0 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
6439 | 0 | const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
6440 | 0 | const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
6441 | 0 | const int32_t p1 = ((const int32_t *)(dst->op_params))[3]; |
6442 | 0 | const int32_t d0 = ((const int32_t *)(dst->op_params))[4]; |
6443 | 0 | const int32_t d1 = ((const int32_t *)(dst->op_params))[5]; |
6444 | 0 | const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1; |
6445 | |
|
6446 | 0 | const int ith = params->ith; |
6447 | 0 | const int nth = params->nth; |
6448 | |
|
6449 | 0 | const int64_t N = is_2D ? ne13 : ne12; |
6450 | 0 | const int64_t IC = is_2D ? ne12 : ne11; |
6451 | 0 | const int64_t IH = is_2D ? ne11 : 1; |
6452 | 0 | const int64_t IW = ne10; |
6453 | |
|
6454 | 0 | const int64_t KH = is_2D ? ne01 : 1; |
6455 | 0 | const int64_t KW = ne00; |
6456 | |
|
6457 | 0 | const int64_t OH = is_2D ? ne2 : 1; |
6458 | 0 | const int64_t OW = ne1; |
6459 | |
|
6460 | 0 | int ofs0 = is_2D ? nb13 : nb12; |
6461 | 0 | int ofs1 = is_2D ? nb12 : nb11; |
6462 | |
|
6463 | 0 | GGML_ASSERT(nb10 == ggml_type_size(src1->type)); |
6464 | | |
6465 | | // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] |
6466 | 0 | { |
6467 | 0 | ggml_fp16_t * const wdata = (ggml_fp16_t *) dst->data; |
6468 | |
|
6469 | 0 | for (int64_t in = 0; in < N; in++) { |
6470 | 0 | for (int64_t ioh = 0; ioh < OH; ioh++) { // 1 |
6471 | 0 | for (int64_t iow = 0; iow < OW; iow++) { |
6472 | 0 | for (int64_t iic = ith; iic < IC; iic += nth) { |
6473 | | |
6474 | | // micro kernel |
6475 | 0 | ggml_fp16_t * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW] |
6476 | 0 | const float * const src_data_f32 = src1->type == GGML_TYPE_F32 |
6477 | 0 | ? (const float *)((const char *) src1->data + in*ofs0 + iic*ofs1) |
6478 | 0 | : nullptr; // [IH, IW] |
6479 | 0 | const ggml_fp16_t * const src_data_f16 = src1->type == GGML_TYPE_F16 |
6480 | 0 | ? (const ggml_fp16_t *)((const char *) src1->data + in*ofs0 + iic*ofs1) |
6481 | 0 | : nullptr; // [IH, IW] |
6482 | |
|
6483 | 0 | for (int64_t ikh = 0; ikh < KH; ikh++) { // 1 |
6484 | 0 | for (int64_t ikw = 0; ikw < KW; ikw++) { |
6485 | 0 | const int64_t iiw = iow*s0 + ikw*d0 - p0; |
6486 | 0 | const int64_t iih = ioh*s1 + ikh*d1 - p1; |
6487 | |
|
6488 | 0 | if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) { |
6489 | 0 | dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0; |
6490 | 0 | } else { |
6491 | 0 | if (src_data_f32 != nullptr) { |
6492 | 0 | dst_data[iic*(KH*KW) + ikh*KW + ikw] = GGML_CPU_FP32_TO_FP16(src_data_f32[iih*IW + iiw]); |
6493 | 0 | } else { |
6494 | 0 | dst_data[iic*(KH*KW) + ikh*KW + ikw] = src_data_f16[iih*IW + iiw]; |
6495 | 0 | } |
6496 | 0 | } |
6497 | 0 | } |
6498 | 0 | } |
6499 | 0 | } |
6500 | 0 | } |
6501 | 0 | } |
6502 | 0 | } |
6503 | 0 | } |
6504 | 0 | } |
6505 | | |
6506 | | void ggml_compute_forward_im2col( |
6507 | | const ggml_compute_params * params, |
6508 | 0 | ggml_tensor * dst) { |
6509 | 0 | switch (dst->type) { |
6510 | 0 | case GGML_TYPE_F16: |
6511 | 0 | { |
6512 | 0 | ggml_compute_forward_im2col_f16(params, dst); |
6513 | 0 | } break; |
6514 | 0 | case GGML_TYPE_F32: |
6515 | 0 | { |
6516 | 0 | ggml_compute_forward_im2col_f32(params, dst); |
6517 | 0 | } break; |
6518 | 0 | default: |
6519 | 0 | { |
6520 | 0 | GGML_ABORT("fatal error"); |
6521 | 0 | } |
6522 | 0 | } |
6523 | 0 | } |
6524 | | |
6525 | | // ggml_compute_forward_im2col_back_f32 |
6526 | | |
6527 | | void ggml_compute_forward_im2col_back_f32( |
6528 | | const ggml_compute_params * params, |
6529 | 0 | ggml_tensor * dst) { |
6530 | |
|
6531 | 0 | const ggml_tensor * src0 = dst->src[0]; // gradients of forward pass output |
6532 | 0 | const ggml_tensor * src1 = dst->src[1]; // convolution kernel |
6533 | |
|
6534 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
6535 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); |
6536 | 0 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
6537 | |
|
6538 | 0 | GGML_TENSOR_BINARY_OP_LOCALS; |
6539 | |
|
6540 | 0 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
6541 | 0 | const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
6542 | 0 | const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
6543 | 0 | const int32_t p1 = ((const int32_t *)(dst->op_params))[3]; |
6544 | 0 | const int32_t d0 = ((const int32_t *)(dst->op_params))[4]; |
6545 | 0 | const int32_t d1 = ((const int32_t *)(dst->op_params))[5]; |
6546 | 0 | const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1; |
6547 | |
|
6548 | 0 | const int ith = params->ith; |
6549 | 0 | const int nth = params->nth; |
6550 | |
|
6551 | 0 | const int64_t N = is_2D ? ne3 : ne2; |
6552 | 0 | const int64_t IC = is_2D ? ne2 : ne1; |
6553 | 0 | const int64_t IH = is_2D ? ne1 : 1; |
6554 | 0 | const int64_t IW = ne0; |
6555 | |
|
6556 | 0 | const int64_t KH = is_2D ? ne11 : 1; |
6557 | 0 | const int64_t KW = ne10; |
6558 | |
|
6559 | 0 | const int64_t OH = is_2D ? ne02 : 1; |
6560 | 0 | const int64_t OW = ne01; |
6561 | |
|
6562 | 0 | int ofs0 = is_2D ? nb3 : nb2; |
6563 | 0 | int ofs1 = is_2D ? nb2 : nb1; |
6564 | |
|
6565 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
6566 | | |
6567 | | // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] |
6568 | 0 | { |
6569 | 0 | float * const wdata = (float *) dst->data; |
6570 | |
|
6571 | 0 | for (int64_t in = 0; in < N; in++) { |
6572 | 0 | for (int64_t iic = ith; iic < IC; iic += nth) { |
6573 | 0 | for (int64_t iih = 0; iih < IH; iih++) { |
6574 | 0 | for (int64_t iiw = 0; iiw < IW; iiw++) { |
6575 | | |
6576 | | // micro kernel |
6577 | 0 | float grad = 0.0f; |
6578 | 0 | for (int64_t ikh = 0; ikh < KH; ikh++) { |
6579 | 0 | for (int64_t ikw = 0; ikw < KW; ikw++) { |
6580 | | // For s0 > 1 some values were skipped over in the forward pass. |
6581 | | // These values have tmpw % s0 != 0 and need to be skipped in the backwards pass as well. |
6582 | 0 | const int64_t tmpw = (iiw + p0 - ikw*d0); |
6583 | 0 | if (tmpw % s0 != 0) { |
6584 | 0 | continue; |
6585 | 0 | } |
6586 | 0 | const int64_t iow = tmpw / s0; |
6587 | | |
6588 | | // Equivalent logic as above except for s1. |
6589 | 0 | int64_t ioh; |
6590 | 0 | if (is_2D) { |
6591 | 0 | const int64_t tmph = iih + p1 - ikh*d1; |
6592 | |
|
6593 | 0 | if (tmph % s1 != 0) { |
6594 | 0 | continue; |
6595 | 0 | } |
6596 | | |
6597 | 0 | ioh = tmph / s1; |
6598 | 0 | } else { |
6599 | 0 | ioh = 0; |
6600 | 0 | } |
6601 | | |
6602 | 0 | if (iow < 0 || iow >= OW || ioh < 0 || ioh >= OH) { |
6603 | 0 | continue; |
6604 | 0 | } |
6605 | | |
6606 | 0 | const float * const grad_in = (const float *) src0->data |
6607 | 0 | + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW] |
6608 | 0 | grad += grad_in[iic*(KH*KW) + ikh*KW + ikw]; |
6609 | 0 | } |
6610 | 0 | } |
6611 | 0 | float * dst_data = (float *)((char *) wdata + (in*ofs0 + iic*ofs1)); // [IH, IW] |
6612 | 0 | dst_data[iih*IW + iiw] = grad; |
6613 | 0 | } |
6614 | 0 | } |
6615 | 0 | } |
6616 | 0 | } |
6617 | 0 | } |
6618 | 0 | } |
6619 | | |
6620 | | |
6621 | | // ggml_compute_forward_im2col_3d_f16 |
6622 | | // src0: kernel [OC*IC, KD, KH, KW] |
6623 | | // src1: image [N*IC, ID, IH, IW] |
6624 | | // dst: result [N*OD, OH, OW, IC * KD * KH * KW] |
6625 | | static void ggml_compute_forward_im2col_3d_f16( |
6626 | | const ggml_compute_params * params, |
6627 | 0 | ggml_tensor * dst) { |
6628 | |
|
6629 | 0 | const ggml_tensor * src0 = dst->src[0]; |
6630 | 0 | const ggml_tensor * src1 = dst->src[1]; |
6631 | |
|
6632 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
6633 | 0 | GGML_ASSERT( dst->type == GGML_TYPE_F16); |
6634 | |
|
6635 | 0 | GGML_TENSOR_BINARY_OP_LOCALS; |
6636 | |
|
6637 | 0 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
6638 | 0 | const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
6639 | 0 | const int32_t s2 = ((const int32_t *)(dst->op_params))[2]; |
6640 | 0 | const int32_t p0 = ((const int32_t *)(dst->op_params))[3]; |
6641 | 0 | const int32_t p1 = ((const int32_t *)(dst->op_params))[4]; |
6642 | 0 | const int32_t p2 = ((const int32_t *)(dst->op_params))[5]; |
6643 | 0 | const int32_t d0 = ((const int32_t *)(dst->op_params))[6]; |
6644 | 0 | const int32_t d1 = ((const int32_t *)(dst->op_params))[7]; |
6645 | 0 | const int32_t d2 = ((const int32_t *)(dst->op_params))[8]; |
6646 | 0 | const int32_t IC = ((const int32_t *)(dst->op_params))[9]; |
6647 | | |
6648 | |
|
6649 | 0 | const int ith = params->ith; |
6650 | 0 | const int nth = params->nth; |
6651 | |
|
6652 | 0 | const int64_t N = ne13 / IC; |
6653 | 0 | const int64_t ID = ne12; |
6654 | 0 | const int64_t IH = ne11; |
6655 | 0 | const int64_t IW = ne10; |
6656 | |
|
6657 | 0 | const int64_t OC = ne03 / IC; |
6658 | 0 | GGML_UNUSED(OC); |
6659 | 0 | const int64_t KD = ne02; |
6660 | 0 | const int64_t KH = ne01; |
6661 | 0 | const int64_t KW = ne00; |
6662 | |
|
6663 | 0 | const int64_t OD = ne3 / N; |
6664 | 0 | const int64_t OH = ne2; |
6665 | 0 | const int64_t OW = ne1; |
6666 | 0 | const int64_t OH_OW = OH*OW; |
6667 | 0 | const int64_t KD_KH_KW = KD*KH*KW; |
6668 | 0 | const int64_t KH_KW = KH*KW; |
6669 | 0 | const int64_t IC_KD_KH_KW = IC*KD*KH*KW; |
6670 | |
|
6671 | 0 | GGML_ASSERT(nb10 == sizeof(float)); |
6672 | | |
6673 | | // im2col: [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW] |
6674 | 0 | { |
6675 | 0 | ggml_fp16_t * const wdata = (ggml_fp16_t *) dst->data; |
6676 | |
|
6677 | 0 | for (int64_t in = 0; in < N; in++) { |
6678 | 0 | for (int64_t iod = 0; iod < OD; iod++) { |
6679 | 0 | for (int64_t ioh = 0; ioh < OH; ioh++) { |
6680 | 0 | for (int64_t iow = 0; iow < OW; iow++) { |
6681 | 0 | for (int64_t iic = ith; iic < IC; iic += nth) { |
6682 | | |
6683 | | // micro kernel |
6684 | 0 | ggml_fp16_t * dst_data = wdata + (in*OD*OH_OW + iod*OH_OW + ioh*OW + iow)*IC_KD_KH_KW; // [IC, KD, KH, KW] |
6685 | 0 | const float * const src_data = (const float *) ((const char *)src1->data + (in*IC + iic)*nb13); // [ID, IH, IW] |
6686 | |
|
6687 | 0 | for (int64_t ikd = 0; ikd < KD; ikd++) { |
6688 | 0 | for (int64_t ikh = 0; ikh < KH; ikh++) { |
6689 | 0 | for (int64_t ikw = 0; ikw < KW; ikw++) { |
6690 | 0 | const int64_t iiw = iow*s0 + ikw*d0 - p0; |
6691 | 0 | const int64_t iih = ioh*s1 + ikh*d1 - p1; |
6692 | 0 | const int64_t iid = iod*s2 + ikd*d2 - p2; |
6693 | |
|
6694 | 0 | if (iid < 0 || iid >= ID || iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) { |
6695 | 0 | dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = 0; |
6696 | 0 | } else { |
6697 | 0 | const float * const s = (const float *) ((const char *)src_data + iid*nb12 + iih*nb11 + iiw*nb10); // [ID, IH, IW] |
6698 | 0 | dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = GGML_CPU_FP32_TO_FP16(*s); |
6699 | 0 | } |
6700 | 0 | } |
6701 | 0 | } |
6702 | 0 | } |
6703 | 0 | } |
6704 | 0 | } |
6705 | 0 | } |
6706 | 0 | } |
6707 | 0 | } |
6708 | 0 | } |
6709 | 0 | } |
6710 | | |
6711 | | // ggml_compute_forward_im2col_3d_f32 |
6712 | | // src0: kernel [OC*IC, KD, KH, KW] |
6713 | | // src1: image [N*IC, ID, IH, IW] |
6714 | | // dst: result [N*OD, OH, OW, IC * KD * KH * KW] |
6715 | | static void ggml_compute_forward_im2col_3d_f32( |
6716 | | const ggml_compute_params * params, |
6717 | 0 | ggml_tensor * dst) { |
6718 | |
|
6719 | 0 | const ggml_tensor * src0 = dst->src[0]; |
6720 | 0 | const ggml_tensor * src1 = dst->src[1]; |
6721 | |
|
6722 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
6723 | 0 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
6724 | |
|
6725 | 0 | GGML_TENSOR_BINARY_OP_LOCALS; |
6726 | |
|
6727 | 0 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
6728 | 0 | const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
6729 | 0 | const int32_t s2 = ((const int32_t *)(dst->op_params))[2]; |
6730 | 0 | const int32_t p0 = ((const int32_t *)(dst->op_params))[3]; |
6731 | 0 | const int32_t p1 = ((const int32_t *)(dst->op_params))[4]; |
6732 | 0 | const int32_t p2 = ((const int32_t *)(dst->op_params))[5]; |
6733 | 0 | const int32_t d0 = ((const int32_t *)(dst->op_params))[6]; |
6734 | 0 | const int32_t d1 = ((const int32_t *)(dst->op_params))[7]; |
6735 | 0 | const int32_t d2 = ((const int32_t *)(dst->op_params))[8]; |
6736 | 0 | const int32_t IC = ((const int32_t *)(dst->op_params))[9]; |
6737 | | |
6738 | |
|
6739 | 0 | const int ith = params->ith; |
6740 | 0 | const int nth = params->nth; |
6741 | |
|
6742 | 0 | const int64_t N = ne13 / IC; |
6743 | 0 | const int64_t ID = ne12; |
6744 | 0 | const int64_t IH = ne11; |
6745 | 0 | const int64_t IW = ne10; |
6746 | |
|
6747 | 0 | const int64_t OC = ne03 / IC; |
6748 | 0 | GGML_UNUSED(OC); |
6749 | 0 | const int64_t KD = ne02; |
6750 | 0 | const int64_t KH = ne01; |
6751 | 0 | const int64_t KW = ne00; |
6752 | |
|
6753 | 0 | const int64_t OD = ne3 / N; |
6754 | 0 | const int64_t OH = ne2; |
6755 | 0 | const int64_t OW = ne1; |
6756 | |
|
6757 | 0 | const int64_t OH_OW = OH*OW; |
6758 | 0 | const int64_t KD_KH_KW = KD*KH*KW; |
6759 | 0 | const int64_t KH_KW = KH*KW; |
6760 | 0 | const int64_t IC_KD_KH_KW = IC*KD*KH*KW; |
6761 | |
|
6762 | 0 | GGML_ASSERT(nb10 == sizeof(float)); |
6763 | | |
6764 | | // im2col: [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW] |
6765 | 0 | { |
6766 | 0 | float * const wdata = (float *) dst->data; |
6767 | |
|
6768 | 0 | for (int64_t in = 0; in < N; in++) { |
6769 | 0 | for (int64_t iod = 0; iod < OD; iod++) { |
6770 | 0 | for (int64_t ioh = 0; ioh < OH; ioh++) { |
6771 | 0 | for (int64_t iow = 0; iow < OW; iow++) { |
6772 | 0 | for (int64_t iic = ith; iic < IC; iic += nth) { |
6773 | | |
6774 | | // micro kernel |
6775 | 0 | float * dst_data = wdata + (in*OD*OH_OW + iod*OH_OW + ioh*OW + iow)*IC_KD_KH_KW; // [IC, KD, KH, KW] |
6776 | 0 | const float * const src_data = (const float *) ((const char *)src1->data + (in*IC + iic)*nb13); // [ID, IH, IW] |
6777 | |
|
6778 | 0 | for (int64_t ikd = 0; ikd < KD; ikd++) { |
6779 | 0 | for (int64_t ikh = 0; ikh < KH; ikh++) { |
6780 | 0 | for (int64_t ikw = 0; ikw < KW; ikw++) { |
6781 | 0 | const int64_t iiw = iow*s0 + ikw*d0 - p0; |
6782 | 0 | const int64_t iih = ioh*s1 + ikh*d1 - p1; |
6783 | 0 | const int64_t iid = iod*s2 + ikd*d2 - p2; |
6784 | |
|
6785 | 0 | if (iid < 0 || iid >= ID || iih < 0 || iih >= IH || iiw < 0 || iiw >= IW || iid < 0 || iid >= ID) { |
6786 | 0 | dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = 0; |
6787 | 0 | } else { |
6788 | 0 | const float * const s = (const float *) ((const char *)src_data + iid*nb12 + iih*nb11 + iiw*nb10); // [ID, IH, IW] |
6789 | 0 | dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = *s; |
6790 | 0 | } |
6791 | 0 | } |
6792 | 0 | } |
6793 | 0 | } |
6794 | 0 | } |
6795 | 0 | } |
6796 | 0 | } |
6797 | 0 | } |
6798 | 0 | } |
6799 | 0 | } |
6800 | 0 | } |
6801 | | |
6802 | | |
6803 | | void ggml_compute_forward_im2col_3d( |
6804 | | const ggml_compute_params * params, |
6805 | 0 | ggml_tensor * dst) { |
6806 | 0 | switch (dst->type) { |
6807 | 0 | case GGML_TYPE_F16: |
6808 | 0 | { |
6809 | 0 | ggml_compute_forward_im2col_3d_f16(params, dst); |
6810 | 0 | } break; |
6811 | 0 | case GGML_TYPE_F32: |
6812 | 0 | { |
6813 | 0 | ggml_compute_forward_im2col_3d_f32(params, dst); |
6814 | 0 | } break; |
6815 | 0 | default: |
6816 | 0 | { |
6817 | 0 | GGML_ABORT("fatal error"); |
6818 | 0 | } |
6819 | 0 | } |
6820 | 0 | } |
6821 | | |
6822 | | static void ggml_call_mul_mat(ggml_type type, const ggml_compute_params * params, int64_t m, int64_t n, int64_t k, |
6823 | 0 | void * a, void * b, float * c) { |
6824 | 0 | const ggml_type_traits * traits = ggml_get_type_traits(type); |
6825 | 0 | struct ggml_tensor src1 = {}; |
6826 | 0 | src1.type = type; |
6827 | 0 | src1.ne[0] = k; |
6828 | 0 | src1.ne[1] = m; |
6829 | 0 | src1.ne[2] = 1; |
6830 | 0 | src1.ne[3] = 1; |
6831 | 0 | src1.nb[0] = traits->type_size; |
6832 | 0 | src1.nb[1] = k * traits->type_size; |
6833 | 0 | src1.nb[2] = src1.nb[1]; |
6834 | 0 | src1.nb[3] = src1.nb[2]; |
6835 | 0 | src1.data = a; |
6836 | |
|
6837 | 0 | struct ggml_tensor src0 = {}; |
6838 | 0 | src0.type = type; |
6839 | 0 | src0.ne[0] = k; |
6840 | 0 | src0.ne[1] = n; |
6841 | 0 | src0.ne[2] = 1; |
6842 | 0 | src0.ne[3] = 1; |
6843 | 0 | src0.nb[0] = traits->type_size; |
6844 | 0 | src0.nb[1] = k * traits->type_size; |
6845 | 0 | src0.nb[2] = src0.nb[1]; |
6846 | 0 | src0.nb[3] = src0.nb[2]; |
6847 | 0 | src0.data = b; |
6848 | |
|
6849 | 0 | struct ggml_tensor dst = {}; |
6850 | 0 | dst.ne[0] = n; |
6851 | 0 | dst.ne[1] = m; |
6852 | 0 | dst.ne[2] = 1; |
6853 | 0 | dst.ne[3] = 1; |
6854 | 0 | dst.nb[0] = sizeof(float); |
6855 | 0 | dst.nb[1] = n * sizeof(float); |
6856 | 0 | dst.nb[2] = dst.nb[1]; |
6857 | 0 | dst.nb[3] = dst.nb[2]; |
6858 | 0 | dst.data = c; |
6859 | 0 | dst.src[0] = &src0; |
6860 | 0 | dst.src[1] = &src1; |
6861 | |
|
6862 | 0 | ggml_compute_forward_mul_mat(params, &dst); |
6863 | 0 | } |
6864 | | |
6865 | 0 | static inline int64_t ggml_wrap_around(int64_t coord, int64_t size) { |
6866 | 0 | return (coord + size) % size; // adding size avoids negative number weirdness |
6867 | 0 | } |
6868 | | |
6869 | | // ggml_compute_forward_col2im_1d |
6870 | | // |
6871 | | // Scatter-add columns [K*OC, T_in] -> signal [T_out, OC] |
6872 | | // where T_out = (T_in - 1)*s + K - 2*p. Gather approach: each output reads ceil(K/s) inputs. |
6873 | | // Parallelized over the time axis so the split stays balanced whatever OC is. |
6874 | | // Supports F32, F16, BF16 input/output (same type), F32 accumulator. |
6875 | | |
6876 | | template <typename elem_t> |
6877 | | static void ggml_compute_forward_col2im_1d_impl( |
6878 | | const ggml_compute_params * params, |
6879 | 0 | ggml_tensor * dst) { |
6880 | |
|
6881 | 0 | const ggml_tensor * src = dst->src[0]; // [K*OC, T_in] |
6882 | |
|
6883 | 0 | GGML_ASSERT(ggml_is_contiguous(src)); |
6884 | 0 | GGML_ASSERT(ggml_is_contiguous(dst)); |
6885 | |
|
6886 | 0 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
6887 | 0 | const int32_t OC = ((const int32_t *)(dst->op_params))[1]; |
6888 | 0 | const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
6889 | |
|
6890 | 0 | const int64_t K_OC = src->ne[0]; |
6891 | 0 | const int64_t T_in = src->ne[1]; |
6892 | 0 | const int64_t K = K_OC / OC; |
6893 | 0 | const int64_t T_out = dst->ne[0]; |
6894 | |
|
6895 | 0 | const elem_t * col_data = (const elem_t *) src->data; |
6896 | 0 | elem_t * dst_data = (elem_t *) dst->data; |
6897 | |
|
6898 | 0 | const int ith = params->ith; |
6899 | 0 | const int nth = params->nth; |
6900 | | |
6901 | | // Parallelize over the time axis: the split stays balanced whatever OC is, |
6902 | | // down to OC = 1 for mono audio, and threads read disjoint column bands |
6903 | 0 | const int64_t dr = (T_out + nth - 1) / nth; |
6904 | 0 | const int64_t it0 = dr * ith; |
6905 | 0 | const int64_t it1 = it0 + dr < T_out ? it0 + dr : T_out; |
6906 | |
|
6907 | 0 | for (int64_t oc = 0; oc < OC; oc++) { |
6908 | 0 | for (int64_t t_out = it0; t_out < it1; t_out++) { |
6909 | 0 | const int64_t t_abs = t_out + p0; // absolute position in uncropped signal |
6910 | | // Gather: find all (t_in, k) where t_in * s + k == t_abs, 0 <= k < K |
6911 | 0 | int64_t t_in_min = (t_abs - K + 1 + s0 - 1) / s0; // ceil((t_abs-K+1)/s) |
6912 | 0 | if (t_in_min < 0) t_in_min = 0; |
6913 | 0 | int64_t t_in_max = t_abs / s0; |
6914 | 0 | if (t_in_max >= T_in) t_in_max = T_in - 1; |
6915 | |
|
6916 | 0 | float sum = 0.0f; |
6917 | 0 | for (int64_t t_in = t_in_min; t_in <= t_in_max; t_in++) { |
6918 | 0 | int64_t k = t_abs - t_in * s0; |
6919 | 0 | if (k >= 0 && k < K) { |
6920 | | // col layout: [K*OC, T_in], element (oc*K+k, t_in) |
6921 | 0 | sum += type_conversion_table<elem_t>::to_f32(col_data[(oc * K + k) + t_in * K_OC]); |
6922 | 0 | } |
6923 | 0 | } |
6924 | | // dst layout: [T_out, OC], element (t_out, oc) |
6925 | 0 | dst_data[t_out + oc * T_out] = type_conversion_table<elem_t>::from_f32(sum); |
6926 | 0 | } |
6927 | 0 | } |
6928 | 0 | } Unexecuted instantiation: ops.cpp:void ggml_compute_forward_col2im_1d_impl<float>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_col2im_1d_impl<unsigned short>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_col2im_1d_impl<ggml_bf16_t>(ggml_compute_params const*, ggml_tensor*) |
6929 | | |
6930 | | void ggml_compute_forward_col2im_1d( |
6931 | | const ggml_compute_params * params, |
6932 | 0 | ggml_tensor * dst) { |
6933 | 0 | switch (dst->src[0]->type) { |
6934 | 0 | case GGML_TYPE_F32: ggml_compute_forward_col2im_1d_impl<float> (params, dst); break; |
6935 | 0 | case GGML_TYPE_F16: ggml_compute_forward_col2im_1d_impl<ggml_fp16_t>(params, dst); break; |
6936 | 0 | case GGML_TYPE_BF16: ggml_compute_forward_col2im_1d_impl<ggml_bf16_t>(params, dst); break; |
6937 | 0 | default: GGML_ABORT("col2im_1d: unsupported type %d", dst->src[0]->type); |
6938 | 0 | } |
6939 | 0 | } |
6940 | | |
6941 | | // ggml_compute_forward_conv_2d |
6942 | | |
6943 | | |
6944 | | static void ggml_compute_forward_conv_2d_impl(const ggml_compute_params * params, |
6945 | | const ggml_tensor * kernel, // [KW, KH, IC, OC] |
6946 | | const ggml_tensor * src, // [W, H, C, N] |
6947 | | ggml_tensor * dst, // [OW, OH, OC, N] |
6948 | 0 | ggml_type kernel_type) { |
6949 | |
|
6950 | 0 | GGML_ASSERT(ggml_is_contiguous(kernel)); |
6951 | 0 | GGML_ASSERT(kernel_type == GGML_TYPE_F16 || kernel_type == GGML_TYPE_F32); |
6952 | 0 | GGML_ASSERT(kernel->type == kernel_type); |
6953 | |
|
6954 | 0 | const ggml_type_traits * traits = ggml_get_type_traits(kernel_type); |
6955 | |
|
6956 | 0 | const int32_t stride_x = dst->op_params[0]; |
6957 | 0 | const int32_t stride_y = dst->op_params[1]; |
6958 | 0 | const int32_t pad_x = dst->op_params[2]; |
6959 | 0 | const int32_t pad_y = dst->op_params[3]; |
6960 | 0 | const int32_t dilation_x = dst->op_params[4]; |
6961 | 0 | const int32_t dilation_y = dst->op_params[5]; |
6962 | |
|
6963 | 0 | const int64_t c_in = src->ne[2]; |
6964 | 0 | const int64_t c_out = kernel->ne[3]; |
6965 | 0 | GGML_ASSERT(c_in == kernel->ne[2]); |
6966 | |
|
6967 | 0 | const int64_t src_w = src->ne[0]; |
6968 | 0 | const int64_t src_h = src->ne[1]; |
6969 | 0 | const int64_t knl_w = kernel->ne[0]; |
6970 | 0 | const int64_t knl_h = kernel->ne[1]; |
6971 | 0 | const int64_t dst_w = dst->ne[0]; |
6972 | 0 | const int64_t dst_h = dst->ne[1]; |
6973 | |
|
6974 | 0 | const float * src_data = (float *) src->data; |
6975 | 0 | void * knl_data = kernel->data; |
6976 | 0 | float * dst_data = (float *) dst->data; |
6977 | |
|
6978 | 0 | const int64_t knl_n = knl_w * knl_h * c_in; |
6979 | 0 | const int64_t patch_total = dst->ne[3] * dst_w * dst_h; |
6980 | |
|
6981 | 0 | const int64_t space_per_patch = knl_n * traits->type_size + c_out * sizeof(float); |
6982 | 0 | const int64_t batch_size = params->wsize / space_per_patch; |
6983 | 0 | const int64_t patches_per_batch = batch_size > 8 ? (batch_size / 8) * 8 : batch_size; |
6984 | 0 | const int64_t batch_n = (patch_total + patches_per_batch - 1) / patches_per_batch; |
6985 | |
|
6986 | 0 | GGML_ASSERT(patches_per_batch > 0 && batch_size >= 1); |
6987 | |
|
6988 | 0 | void * tmp = params->wdata; |
6989 | |
|
6990 | 0 | for (int64_t batch_i = 0; batch_i < batch_n; ++batch_i) { |
6991 | |
|
6992 | 0 | const int64_t patch_start_batch = batch_i * patches_per_batch; |
6993 | 0 | const int64_t patch_end_batch = std::min(patch_start_batch + patches_per_batch, |
6994 | 0 | patch_total); |
6995 | 0 | const int64_t patch_n = patch_end_batch - patch_start_batch; |
6996 | |
|
6997 | 0 | const int64_t patch_per_thread = (patch_n + params->nth - 1) / params->nth; |
6998 | 0 | const int64_t patch_start = patch_start_batch + params->ith * patch_per_thread; |
6999 | 0 | const int64_t patch_end = std::min(patch_start + patch_per_thread, patch_end_batch); |
7000 | | |
7001 | | //im2col for a patch |
7002 | 0 | for (int64_t p = patch_start; p < patch_end; ++p) { |
7003 | 0 | const int64_t batch_n = p / (dst_w * dst_h); |
7004 | 0 | const int64_t src_x = (p / dst_w) % dst_h; |
7005 | 0 | const int64_t src_y = p % dst_w; |
7006 | |
|
7007 | 0 | const float * src_base = (const float *)((const char *)src_data + batch_n * src->nb[3]); |
7008 | 0 | char * dst_row = (char *) tmp + (p % patches_per_batch) * knl_n * traits->type_size; |
7009 | |
|
7010 | 0 | for (int64_t ic = 0; ic < c_in; ++ic) { |
7011 | 0 | for (int64_t ky = 0; ky < knl_h; ++ky) { |
7012 | 0 | for (int64_t kx = 0; kx < knl_w; ++kx) { |
7013 | 0 | const int64_t sy = src_x * stride_y + ky * dilation_y - pad_y; |
7014 | 0 | const int64_t sx = src_y * stride_x + kx * dilation_x - pad_x; |
7015 | |
|
7016 | 0 | int64_t dst_idx = ic * (knl_h * knl_w) + ky * knl_w + kx; |
7017 | |
|
7018 | 0 | float src_val; |
7019 | 0 | if (sy < 0 || sy >= src_h || sx < 0 || sx >= src_w) { |
7020 | 0 | src_val = 0.0f; |
7021 | 0 | } else { |
7022 | 0 | const float * src_ptr = (const float *)((const char *)src_base + sx * src->nb[0] + sy * src->nb[1] + ic * src->nb[2]); |
7023 | 0 | src_val = *src_ptr; |
7024 | 0 | } |
7025 | |
|
7026 | 0 | char * element_ptr = dst_row + dst_idx * traits->type_size; |
7027 | 0 | if (kernel_type == GGML_TYPE_F32) { |
7028 | 0 | *(float *) element_ptr = src_val; |
7029 | 0 | } else if (kernel_type == GGML_TYPE_F16) { |
7030 | 0 | *(ggml_fp16_t *) element_ptr = GGML_CPU_FP32_TO_FP16(src_val); |
7031 | 0 | } |
7032 | 0 | } |
7033 | 0 | } |
7034 | 0 | } |
7035 | 0 | } // patches handled by this thread |
7036 | |
|
7037 | 0 | ggml_barrier(params->threadpool); |
7038 | |
|
7039 | 0 | float * gemm_output = (float *) ((char *) tmp + patches_per_batch * knl_n * traits->type_size); |
7040 | |
|
7041 | 0 | GGML_ASSERT(gemm_output + patch_n * c_out <= (float*)tmp + params->wsize); |
7042 | | |
7043 | | // GEMM: patches[patch_n, knl_n] × kernel[knl_n, c_out] = output[patch_n, c_out] |
7044 | 0 | ggml_call_mul_mat(kernel_type, params, patch_n, c_out, knl_n, tmp, knl_data, gemm_output); |
7045 | |
|
7046 | 0 | ggml_barrier(params->threadpool); |
7047 | | |
7048 | | |
7049 | | //permute back [OC, N, OH, OW] to [N, OC, OH, OW] |
7050 | 0 | const int64_t permute_per_thread = (patch_n + params->nth - 1) / params->nth; |
7051 | 0 | const int64_t permute_start = params->ith * permute_per_thread; |
7052 | 0 | const int64_t permute_end = std::min(permute_start + permute_per_thread, patch_n); |
7053 | |
|
7054 | 0 | for (int64_t i = permute_start; i < permute_end; ++i) { |
7055 | 0 | const int64_t p = patch_start_batch + i; |
7056 | 0 | const int64_t batch_n = p / (dst_w * dst_h); |
7057 | 0 | const int64_t dst_y = (p / dst_w) % dst_h; |
7058 | 0 | const int64_t dst_x = p % dst_w; |
7059 | |
|
7060 | 0 | for (int64_t oc = 0; oc < c_out; ++oc) { |
7061 | 0 | const float value = gemm_output[i * c_out + oc]; |
7062 | 0 | float * dst_ptr = (float *)((char *)dst_data + dst_x * dst->nb[0] + dst_y * dst->nb[1] + oc * dst->nb[2] + batch_n * dst->nb[3]); |
7063 | 0 | *dst_ptr = value; |
7064 | 0 | } |
7065 | 0 | } |
7066 | 0 | } |
7067 | 0 | } |
7068 | | |
7069 | | void ggml_compute_forward_conv_2d( |
7070 | | const ggml_compute_params * params, |
7071 | 0 | ggml_tensor * dst) { |
7072 | |
|
7073 | 0 | const ggml_tensor * src0 = dst->src[0]; |
7074 | 0 | const ggml_tensor * src1 = dst->src[1]; |
7075 | |
|
7076 | 0 | ggml_compute_forward_conv_2d_impl(params, src0, src1, dst, src0->type); |
7077 | 0 | } |
7078 | | |
7079 | | // ggml_compute_forward_conv_3d |
7080 | | |
7081 | | static void ggml_compute_forward_conv_3d_impl(const ggml_compute_params * params, |
7082 | | const ggml_tensor * kernel, |
7083 | | const ggml_tensor * src, |
7084 | | ggml_tensor * dst, |
7085 | 0 | ggml_type kernel_type) { |
7086 | |
|
7087 | 0 | GGML_ASSERT(ggml_is_contiguous(kernel)); |
7088 | 0 | GGML_ASSERT(kernel_type == GGML_TYPE_F16 || kernel_type == GGML_TYPE_F32); |
7089 | 0 | GGML_ASSERT(kernel->type == kernel_type); |
7090 | |
|
7091 | 0 | const ggml_type_traits * traits = ggml_get_type_traits(kernel_type); |
7092 | |
|
7093 | 0 | const int32_t s0 = dst->op_params[0]; |
7094 | 0 | const int32_t s1 = dst->op_params[1]; |
7095 | 0 | const int32_t s2 = dst->op_params[2]; |
7096 | 0 | const int32_t p0 = dst->op_params[3]; |
7097 | 0 | const int32_t p1 = dst->op_params[4]; |
7098 | 0 | const int32_t p2 = dst->op_params[5]; |
7099 | 0 | const int32_t d0 = dst->op_params[6]; |
7100 | 0 | const int32_t d1 = dst->op_params[7]; |
7101 | 0 | const int32_t d2 = dst->op_params[8]; |
7102 | 0 | const int32_t c = dst->op_params[9]; |
7103 | 0 | const int32_t n = dst->op_params[10]; |
7104 | 0 | const int32_t oc = dst->op_params[11]; |
7105 | |
|
7106 | 0 | const int64_t src_w = src->ne[0]; |
7107 | 0 | const int64_t src_h = src->ne[1]; |
7108 | 0 | const int64_t src_d = src->ne[2]; |
7109 | 0 | const int64_t knl_w = kernel->ne[0]; |
7110 | 0 | const int64_t knl_h = kernel->ne[1]; |
7111 | 0 | const int64_t knl_d = kernel->ne[2]; |
7112 | 0 | const int64_t dst_w = dst->ne[0]; |
7113 | 0 | const int64_t dst_h = dst->ne[1]; |
7114 | 0 | const int64_t dst_d = dst->ne[2]; |
7115 | |
|
7116 | 0 | const float * src_data = (float *) src->data; |
7117 | 0 | void * knl_data = kernel->data; |
7118 | 0 | float * dst_data = (float *) dst->data; |
7119 | |
|
7120 | 0 | const int64_t knl_n_per_channel = knl_w * knl_h * knl_d; |
7121 | 0 | const int64_t knl_n_total = knl_n_per_channel * c; |
7122 | 0 | const int64_t patch_total = n * dst_w * dst_h * dst_d; |
7123 | |
|
7124 | 0 | const int64_t space_per_patch = knl_n_total * traits->type_size + oc * sizeof(float); |
7125 | 0 | const int64_t batch_size = params->wsize / space_per_patch; |
7126 | 0 | const int64_t patches_per_batch = batch_size > 8 ? (batch_size / 8) * 8 : batch_size; |
7127 | 0 | const int64_t batch_n = (patch_total + patches_per_batch - 1) / patches_per_batch; |
7128 | |
|
7129 | 0 | GGML_ASSERT(patches_per_batch > 0 && batch_size >= 1); |
7130 | |
|
7131 | 0 | void * tmp = params->wdata; |
7132 | |
|
7133 | 0 | for (int64_t batch_i = 0; batch_i < batch_n; ++batch_i) { |
7134 | 0 | const int64_t patch_start_batch = batch_i * patches_per_batch; |
7135 | 0 | const int64_t patch_end_batch = std::min(patch_start_batch + patches_per_batch, patch_total); |
7136 | 0 | const int64_t patch_n_in_batch = patch_end_batch - patch_start_batch; |
7137 | |
|
7138 | 0 | const int64_t patch_per_thread = (patch_n_in_batch + params->nth - 1) / params->nth; |
7139 | 0 | const int64_t patch_start = patch_start_batch + params->ith * patch_per_thread; |
7140 | 0 | const int64_t patch_end = std::min(patch_start + patch_per_thread, patch_end_batch); |
7141 | |
|
7142 | 0 | for (int64_t p = patch_start; p < patch_end; ++p) { |
7143 | 0 | const int64_t p_in_batch = p % (dst_w * dst_h * dst_d); |
7144 | 0 | const int64_t p_in_depth = p_in_batch % (dst_w * dst_h); |
7145 | 0 | const int64_t batch_idx = p / (dst_w * dst_h * dst_d); |
7146 | 0 | const int64_t dst_z = p_in_batch / (dst_w * dst_h); |
7147 | 0 | const int64_t dst_y = p_in_depth / dst_w; |
7148 | 0 | const int64_t dst_x = p_in_depth % dst_w; |
7149 | |
|
7150 | 0 | char * dst_row = (char *) tmp + (p % patches_per_batch) * knl_n_total * traits->type_size; |
7151 | |
|
7152 | 0 | for (int64_t ic = 0; ic < c; ++ic) { |
7153 | 0 | for (int64_t kz = 0; kz < knl_d; ++kz) { |
7154 | 0 | for (int64_t ky = 0; ky < knl_h; ++ky) { |
7155 | 0 | for (int64_t kx = 0; kx < knl_w; ++kx) { |
7156 | 0 | const int64_t sz = dst_z * s2 + kz * d2 - p2; |
7157 | 0 | const int64_t sy = dst_y * s1 + ky * d1 - p1; |
7158 | 0 | const int64_t sx = dst_x * s0 + kx * d0 - p0; |
7159 | |
|
7160 | 0 | int64_t dst_idx = ic * knl_n_per_channel + kz * (knl_h * knl_w) + ky * knl_w + kx; |
7161 | |
|
7162 | 0 | float src_val; |
7163 | 0 | if (sz < 0 || sz >= src_d || sy < 0 || sy >= src_h || sx < 0 || sx >= src_w) { |
7164 | 0 | src_val = 0.0f; |
7165 | 0 | } else { |
7166 | 0 | const int64_t cn_idx = batch_idx * c + ic; |
7167 | 0 | const float * src_ptr = (const float *)((const char *)src_data + sx*src->nb[0] + sy*src->nb[1] + sz*src->nb[2] + cn_idx*src->nb[3]); |
7168 | 0 | src_val = *src_ptr; |
7169 | 0 | } |
7170 | |
|
7171 | 0 | char * element_ptr = dst_row + dst_idx * traits->type_size; |
7172 | 0 | if (kernel_type == GGML_TYPE_F32) { |
7173 | 0 | *(float *)element_ptr = src_val; |
7174 | 0 | } else if (kernel_type == GGML_TYPE_F16) { |
7175 | 0 | *(ggml_fp16_t *)element_ptr = GGML_CPU_FP32_TO_FP16(src_val); |
7176 | 0 | } |
7177 | 0 | } |
7178 | 0 | } |
7179 | 0 | } |
7180 | 0 | } |
7181 | 0 | } |
7182 | |
|
7183 | 0 | ggml_barrier(params->threadpool); |
7184 | |
|
7185 | 0 | float * gemm_output = (float *) ((char *) tmp + patches_per_batch * knl_n_total * traits->type_size); |
7186 | 0 | ggml_call_mul_mat(kernel_type, params, patch_n_in_batch, oc, knl_n_total, tmp, knl_data, gemm_output); |
7187 | |
|
7188 | 0 | ggml_barrier(params->threadpool); |
7189 | |
|
7190 | 0 | const int64_t permute_per_thread = (patch_n_in_batch + params->nth - 1) / params->nth; |
7191 | 0 | const int64_t permute_start = params->ith * permute_per_thread; |
7192 | 0 | const int64_t permute_end = std::min(permute_start + permute_per_thread, patch_n_in_batch); |
7193 | |
|
7194 | 0 | for (int64_t i = permute_start; i < permute_end; ++i) { |
7195 | 0 | const int64_t p = patch_start_batch + i; |
7196 | 0 | const int64_t p_in_batch = p % (dst_w * dst_h * dst_d); |
7197 | 0 | const int64_t p_in_depth = p_in_batch % (dst_w * dst_h); |
7198 | 0 | const int64_t batch_idx = p / (dst_w * dst_h * dst_d); |
7199 | 0 | const int64_t dst_z = p_in_batch / (dst_w * dst_h); |
7200 | 0 | const int64_t dst_y = p_in_depth / dst_w; |
7201 | 0 | const int64_t dst_x = p_in_depth % dst_w; |
7202 | |
|
7203 | 0 | for (int64_t ioc = 0; ioc < oc; ++ioc) { |
7204 | 0 | const float value = gemm_output[i * oc + ioc]; |
7205 | 0 | const int64_t ocn_idx = batch_idx * oc + ioc; |
7206 | 0 | float * dst_ptr = (float *)((char *)dst_data + dst_x*dst->nb[0] + dst_y*dst->nb[1] + dst_z*dst->nb[2] + ocn_idx*dst->nb[3]); |
7207 | 0 | *dst_ptr = value; |
7208 | 0 | } |
7209 | 0 | } |
7210 | 0 | } |
7211 | 0 | } |
7212 | | |
7213 | | void ggml_compute_forward_conv_3d( |
7214 | | const ggml_compute_params * params, |
7215 | 0 | ggml_tensor * dst) { |
7216 | 0 | const ggml_tensor * src0 = dst->src[0]; |
7217 | 0 | const ggml_tensor * src1 = dst->src[1]; |
7218 | 0 | ggml_compute_forward_conv_3d_impl(params, src0, src1, dst, src0->type); |
7219 | 0 | } |
7220 | | |
7221 | | template <typename kernel_t> |
7222 | | static void ggml_compute_forward_conv_transpose_2d_impl( |
7223 | | const ggml_compute_params * params, |
7224 | 0 | ggml_tensor * dst) { |
7225 | |
|
7226 | 0 | const ggml_tensor * src0 = dst->src[0]; |
7227 | 0 | const ggml_tensor * src1 = dst->src[1]; |
7228 | |
|
7229 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_F32); |
7230 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
7231 | 0 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
7232 | |
|
7233 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
7234 | |
|
7235 | 0 | const int ith = params->ith; |
7236 | 0 | const int nth = params->nth; |
7237 | |
|
7238 | 0 | const int nk = ne00*ne01*ne02*ne03; |
7239 | |
|
7240 | 0 | GGML_ASSERT(nb00 == ggml_type_size(src0->type)); |
7241 | 0 | GGML_ASSERT(nb10 == sizeof(float)); |
7242 | |
|
7243 | 0 | if (ith == 0) { |
7244 | 0 | memset(params->wdata, 0, params->wsize); |
7245 | | |
7246 | | // permute kernel data (src0) from (Kw x Kh x Cout x Cin) to (Cin x Kw x Kh x Cout) |
7247 | 0 | { |
7248 | 0 | kernel_t * const wdata = (kernel_t *) params->wdata + 0; |
7249 | |
|
7250 | 0 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
7251 | 0 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
7252 | 0 | const kernel_t * const src = (kernel_t *)((char *) src0->data + i03*nb03 + i02*nb02); |
7253 | 0 | kernel_t * dst_data = wdata + i02*ne01*ne00*ne03; |
7254 | 0 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
7255 | 0 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
7256 | 0 | dst_data[i01*ne00*ne03 + i00*ne03 + i03] = src[i01 * ne00 + i00]; |
7257 | 0 | } |
7258 | 0 | } |
7259 | 0 | } |
7260 | 0 | } |
7261 | 0 | } |
7262 | | |
7263 | | // permute source data (src1) from (Sw x Sh x Cin) to (Cin x Sw x Sh) |
7264 | 0 | { |
7265 | 0 | kernel_t * const wdata = (kernel_t *) params->wdata + nk; |
7266 | 0 | for (int i12 = 0; i12 < ne12; i12++) { |
7267 | 0 | for (int i11 = 0; i11 < ne11; i11++) { |
7268 | 0 | const float * const src = (float *)((char *) src1->data + i12*nb12 + i11*nb11); |
7269 | 0 | kernel_t * dst_data = wdata + i11*ne10*ne12; |
7270 | 0 | for (int i10 = 0; i10 < ne10; i10++) { |
7271 | 0 | if constexpr (std::is_same_v<kernel_t, ggml_fp16_t>) { |
7272 | 0 | dst_data[i10*ne12 + i12] = GGML_CPU_FP32_TO_FP16(src[i10]); |
7273 | 0 | } else { |
7274 | 0 | dst_data[i10*ne12 + i12] = src[i10]; |
7275 | 0 | } |
7276 | 0 | } |
7277 | 0 | } |
7278 | 0 | } |
7279 | 0 | } |
7280 | |
|
7281 | 0 | memset(dst->data, 0, ggml_nbytes(dst)); |
7282 | 0 | } |
7283 | 0 | ggml_barrier(params->threadpool); |
7284 | |
|
7285 | 0 | const int32_t stride = ggml_get_op_params_i32(dst, 0); |
7286 | | |
7287 | | // total patches in dst |
7288 | 0 | const int np = ne2; |
7289 | | |
7290 | | // patches per thread |
7291 | 0 | const int dp = (np + nth - 1)/nth; |
7292 | | |
7293 | | // patch range for this thread |
7294 | 0 | const int ip0 = dp*ith; |
7295 | 0 | const int ip1 = MIN(ip0 + dp, np); |
7296 | |
|
7297 | 0 | kernel_t * const wdata = (kernel_t *) params->wdata + 0; |
7298 | 0 | kernel_t * const wdata_src = wdata + nk; |
7299 | |
|
7300 | 0 | for (int i2 = ip0; i2 < ip1; i2++) { // Cout |
7301 | 0 | float * dst_data = (float *)((char *) dst->data + i2*nb2); |
7302 | 0 | kernel_t * wdata_kernel = wdata + i2*ne01*ne00*ne03; |
7303 | 0 | for (int i11 = 0; i11 < ne11; i11++) { |
7304 | 0 | for (int i10 = 0; i10 < ne10; i10++) { |
7305 | 0 | const int i1n = i11*ne10*ne12 + i10*ne12; |
7306 | 0 | for (int i01 = 0; i01 < ne01; i01++) { |
7307 | 0 | for (int i00 = 0; i00 < ne00; i00++) { |
7308 | 0 | float v = 0; |
7309 | 0 | if constexpr (std::is_same_v<kernel_t, ggml_fp16_t>) { |
7310 | 0 | ggml_vec_dot_f16(ne03, &v, 0, |
7311 | 0 | wdata_src + i1n, 0, |
7312 | 0 | wdata_kernel + i01*ne00*ne03 + i00*ne03, 0, 1); |
7313 | 0 | } else { |
7314 | 0 | ggml_vec_dot_f32(ne03, &v, 0, |
7315 | 0 | wdata_src + i1n, 0, |
7316 | 0 | wdata_kernel + i01*ne00*ne03 + i00*ne03, 0, 1); |
7317 | 0 | } |
7318 | 0 | dst_data[(i11*stride + i01)*ne0 + i10*stride + i00] += v; |
7319 | 0 | } |
7320 | 0 | } |
7321 | 0 | } |
7322 | 0 | } |
7323 | 0 | } |
7324 | 0 | } Unexecuted instantiation: ops.cpp:void ggml_compute_forward_conv_transpose_2d_impl<unsigned short>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_conv_transpose_2d_impl<float>(ggml_compute_params const*, ggml_tensor*) |
7325 | | |
7326 | | void ggml_compute_forward_conv_transpose_2d( |
7327 | | const ggml_compute_params * params, |
7328 | 0 | ggml_tensor * dst) { |
7329 | |
|
7330 | 0 | const ggml_tensor * src0 = dst->src[0]; |
7331 | |
|
7332 | 0 | switch (src0->type) { |
7333 | 0 | case GGML_TYPE_F16: |
7334 | 0 | { |
7335 | 0 | ggml_compute_forward_conv_transpose_2d_impl<ggml_fp16_t>(params, dst); |
7336 | 0 | } break; |
7337 | 0 | case GGML_TYPE_F32: |
7338 | 0 | { |
7339 | 0 | ggml_compute_forward_conv_transpose_2d_impl<float>(params, dst); |
7340 | 0 | } break; |
7341 | 0 | default: |
7342 | 0 | { |
7343 | 0 | GGML_ABORT("fatal error"); |
7344 | 0 | } |
7345 | 0 | } |
7346 | 0 | } |
7347 | | |
7348 | | // ggml_compute_forward_conv_2d_dw |
7349 | | |
7350 | | struct ggml_conv_2d_dw_params { |
7351 | | int64_t channels; |
7352 | | int64_t batch; |
7353 | | int64_t src_w; |
7354 | | int64_t src_h; |
7355 | | int64_t dst_w; |
7356 | | int64_t dst_h; |
7357 | | int64_t knl_w; |
7358 | | int64_t knl_h; |
7359 | | int stride_x; |
7360 | | int stride_y; |
7361 | | int pad_x; |
7362 | | int pad_y; |
7363 | | int dilation_x; |
7364 | | int dilation_y; |
7365 | | }; |
7366 | | |
7367 | 0 | static inline float ggml_conv_2d_dw_knl_f32(const char * data, int64_t i, ggml_type type) { |
7368 | 0 | if (type == GGML_TYPE_F16) { |
7369 | 0 | return GGML_FP16_TO_FP32(((const ggml_fp16_t *)data)[i]); |
7370 | 0 | } |
7371 | 0 | return ((const float *)data)[i]; |
7372 | 0 | } |
7373 | | |
7374 | | static void ggml_compute_forward_conv_2d_dw_cwhn( |
7375 | | const ggml_compute_params * params, |
7376 | | const ggml_tensor * src, |
7377 | | const ggml_tensor * kernel, |
7378 | | ggml_tensor * dst, |
7379 | 0 | const ggml_conv_2d_dw_params & p) { |
7380 | |
|
7381 | 0 | const int64_t c = p.channels; |
7382 | 0 | const char * knl_data = (const char *)kernel->data; |
7383 | 0 | const ggml_type knl_type = kernel->type; |
7384 | |
|
7385 | 0 | const int64_t rows_total = p.dst_h * p.batch; |
7386 | 0 | const int64_t rows_per_thread = (rows_total + params->nth - 1) / params->nth; |
7387 | 0 | const int64_t row_start = params->ith * rows_per_thread; |
7388 | 0 | const int64_t row_end = MIN(row_start + rows_per_thread, rows_total); |
7389 | |
|
7390 | 0 | #ifdef GGML_SIMD |
7391 | 0 | int64_t c_pkg_end = 0; |
7392 | 0 | int64_t pkg_size = GGML_F32_EPR; |
7393 | 0 | if (knl_type == GGML_TYPE_F32) { |
7394 | | #if defined(__ARM_FEATURE_SVE) |
7395 | | pkg_size = svcntw(); |
7396 | | #else |
7397 | 0 | pkg_size = GGML_F32_EPR; |
7398 | 0 | #endif |
7399 | 0 | c_pkg_end = (c / pkg_size) * pkg_size; |
7400 | 0 | } |
7401 | | #else |
7402 | | const int64_t c_pkg_end = 0; |
7403 | | #endif |
7404 | |
|
7405 | 0 | for (int64_t row = row_start; row < row_end; ++row) { |
7406 | 0 | const int64_t dst_y = row % p.dst_h; |
7407 | 0 | const float * src_data = (const float *)src->data + (row / p.dst_h) * p.src_w * p.src_h * c; |
7408 | 0 | for (int64_t dst_x = 0; dst_x < p.dst_w; ++dst_x) { |
7409 | 0 | float * dst_data = (float *)dst->data + (row * p.dst_w + dst_x) * c; |
7410 | 0 | const int64_t src_y_base = dst_y * p.stride_y - p.pad_y; |
7411 | 0 | const int64_t src_x_base = dst_x * p.stride_x - p.pad_x; |
7412 | |
|
7413 | 0 | #ifdef GGML_SIMD |
7414 | 0 | for (int64_t c_i = 0; c_i < c_pkg_end; c_i += pkg_size) { |
7415 | 0 | GGML_F32_VEC sum = GGML_F32_VEC_ZERO; |
7416 | 0 | for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) { |
7417 | 0 | const int64_t src_y = src_y_base + knl_y * p.dilation_y; |
7418 | 0 | if (src_y < 0 || src_y >= p.src_h) { |
7419 | 0 | continue; |
7420 | 0 | } |
7421 | 0 | for (int64_t knl_x = 0; knl_x < p.knl_w; ++knl_x) { |
7422 | 0 | const int64_t src_x = src_x_base + knl_x * p.dilation_x; |
7423 | 0 | if (src_x < 0 || src_x >= p.src_w) { |
7424 | 0 | continue; |
7425 | 0 | } |
7426 | 0 | const float * kp = (const float *)knl_data + (knl_y * p.knl_w + knl_x) * c + c_i; |
7427 | 0 | GGML_F32_VEC k = GGML_F32_VEC_LOAD(kp); |
7428 | 0 | GGML_F32_VEC s = GGML_F32_VEC_LOAD(src_data + (src_y * p.src_w + src_x) * c + c_i); |
7429 | 0 | sum = GGML_F32_VEC_FMA(sum, k, s); |
7430 | 0 | } |
7431 | 0 | } |
7432 | 0 | GGML_F32_VEC_STORE(dst_data + c_i, sum); |
7433 | 0 | } |
7434 | 0 | #endif |
7435 | 0 | for (int64_t c_i = c_pkg_end; c_i < c; ++c_i) { |
7436 | 0 | float sum = 0.0f; |
7437 | 0 | for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) { |
7438 | 0 | const int64_t src_y = src_y_base + knl_y * p.dilation_y; |
7439 | 0 | if (src_y < 0 || src_y >= p.src_h) { |
7440 | 0 | continue; |
7441 | 0 | } |
7442 | 0 | for (int64_t knl_x = 0; knl_x < p.knl_w; ++knl_x) { |
7443 | 0 | const int64_t src_x = src_x_base + knl_x * p.dilation_x; |
7444 | 0 | if (src_x < 0 || src_x >= p.src_w) { |
7445 | 0 | continue; |
7446 | 0 | } |
7447 | 0 | sum += ggml_conv_2d_dw_knl_f32(knl_data, (knl_y * p.knl_w + knl_x) * c + c_i, knl_type) |
7448 | 0 | * src_data[(src_y * p.src_w + src_x) * c + c_i]; |
7449 | 0 | } |
7450 | 0 | } |
7451 | 0 | dst_data[c_i] = sum; |
7452 | 0 | } |
7453 | 0 | } |
7454 | 0 | } |
7455 | 0 | } |
7456 | | |
7457 | | static void ggml_compute_forward_conv_2d_dw_whcn( |
7458 | | const ggml_compute_params * params, |
7459 | | const ggml_tensor * src, |
7460 | | const ggml_tensor * kernel, |
7461 | | ggml_tensor * dst, |
7462 | 0 | const ggml_conv_2d_dw_params & p) { |
7463 | |
|
7464 | 0 | const int64_t n = p.channels * p.batch; |
7465 | 0 | const int64_t per_thread = (n + params->nth - 1) / params->nth; |
7466 | 0 | const int64_t start = params->ith * per_thread; |
7467 | 0 | const int64_t end = MIN(start + per_thread, n); |
7468 | 0 | const char * knl_base = (const char *)kernel->data; |
7469 | 0 | const ggml_type knl_type = kernel->type; |
7470 | |
|
7471 | 0 | for (int64_t i = start; i < end; ++i) { |
7472 | 0 | const int64_t knl_offset = (i % p.channels) * p.knl_w * p.knl_h; |
7473 | 0 | const float * src_data = (const float *)src->data + i * p.src_w * p.src_h; |
7474 | 0 | float * dst_data = (float *)dst->data + i * p.dst_w * p.dst_h; |
7475 | |
|
7476 | 0 | for (int64_t dst_y = 0; dst_y < p.dst_h; ++dst_y) { |
7477 | 0 | for (int64_t dst_x = 0; dst_x < p.dst_w; ++dst_x) { |
7478 | |
|
7479 | 0 | float sum = 0.0f; |
7480 | 0 | for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) { |
7481 | 0 | const int64_t src_y = dst_y * p.stride_y + knl_y * p.dilation_y - p.pad_y; |
7482 | 0 | if (src_y < 0 || src_y >= p.src_h) { |
7483 | 0 | continue; |
7484 | 0 | } |
7485 | 0 | for (int64_t knl_x = 0; knl_x < p.knl_w; ++knl_x) { |
7486 | 0 | const int64_t src_x = dst_x * p.stride_x + knl_x * p.dilation_x - p.pad_x; |
7487 | 0 | if (src_x < 0 || src_x >= p.src_w) { |
7488 | 0 | continue; |
7489 | 0 | } |
7490 | 0 | sum += ggml_conv_2d_dw_knl_f32(knl_base, knl_offset + knl_y * p.knl_w + knl_x, knl_type) |
7491 | 0 | * src_data[src_y * p.src_w + src_x]; |
7492 | 0 | } |
7493 | 0 | } |
7494 | 0 | dst_data[dst_y * p.dst_w + dst_x] = sum; |
7495 | 0 | } |
7496 | 0 | } |
7497 | 0 | } |
7498 | 0 | } |
7499 | | |
7500 | | void ggml_compute_forward_conv_2d_dw( |
7501 | | const ggml_compute_params * params, |
7502 | 0 | ggml_tensor * dst) { |
7503 | |
|
7504 | 0 | const ggml_tensor * kernel = dst->src[0]; |
7505 | 0 | const ggml_tensor * src = dst->src[1]; |
7506 | 0 | ggml_conv_2d_dw_params p; |
7507 | 0 | p.channels = src->ne[2]; |
7508 | 0 | p.batch = src->ne[3]; |
7509 | 0 | p.src_w = src->ne[0]; |
7510 | 0 | p.src_h = src->ne[1]; |
7511 | 0 | p.dst_w = dst->ne[0]; |
7512 | 0 | p.dst_h = dst->ne[1]; |
7513 | 0 | p.knl_w = kernel->ne[0]; |
7514 | 0 | p.knl_h = kernel->ne[1]; |
7515 | 0 | p.stride_x = dst->op_params[0]; |
7516 | 0 | p.stride_y = dst->op_params[1]; |
7517 | 0 | p.pad_x = dst->op_params[2]; |
7518 | 0 | p.pad_y = dst->op_params[3]; |
7519 | 0 | p.dilation_x = dst->op_params[4]; |
7520 | 0 | p.dilation_y = dst->op_params[5]; |
7521 | |
|
7522 | 0 | GGML_ASSERT(kernel->type == GGML_TYPE_F32 || kernel->type == GGML_TYPE_F16); |
7523 | 0 | GGML_ASSERT(kernel->ne[3] == p.channels); |
7524 | 0 | GGML_ASSERT(dst->ne[3] == p.batch); |
7525 | |
|
7526 | 0 | if (ggml_is_contiguous(src)) { |
7527 | 0 | ggml_compute_forward_conv_2d_dw_whcn(params, src, kernel, dst, p); |
7528 | 0 | } else if (ggml_is_contiguous_channels(src)) { |
7529 | 0 | GGML_ASSERT(kernel->nb[0] >= kernel->nb[2] && kernel->nb[1] >= kernel->nb[0]); |
7530 | 0 | ggml_compute_forward_conv_2d_dw_cwhn(params, src, kernel, dst, p); |
7531 | 0 | } else { |
7532 | 0 | GGML_ABORT("non-contiguous memory layout not supported"); |
7533 | 0 | } |
7534 | 0 | } |
7535 | | |
7536 | | // ggml_compute_forward_pool_1d_ksp |
7537 | | static void ggml_compute_forward_pool_1d_ksp( |
7538 | | const ggml_compute_params * params, |
7539 | | const ggml_op_pool op, |
7540 | | const int k, |
7541 | | const int s, |
7542 | | const int p, |
7543 | 0 | ggml_tensor * dst) { |
7544 | |
|
7545 | 0 | const ggml_tensor * src = dst->src[0]; |
7546 | |
|
7547 | 0 | assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16); |
7548 | |
|
7549 | 0 | if (params->ith != 0) { |
7550 | 0 | return; |
7551 | 0 | } |
7552 | | |
7553 | 0 | const int64_t IW = src->ne[0]; |
7554 | 0 | const int64_t OW = dst->ne[0]; |
7555 | |
|
7556 | 0 | const int64_t nr = ggml_nrows(src); |
7557 | |
|
7558 | 0 | for (int64_t ir = 0; ir < nr; ++ir) { |
7559 | 0 | const char * srow_bytes = (const char *) src->data + ir * src->nb[1]; |
7560 | 0 | float * drow = (float *) (( char *) dst->data + ir * dst->nb[1]); |
7561 | |
|
7562 | 0 | for (int64_t ow = 0; ow < OW; ++ow) { |
7563 | 0 | float res = 0; |
7564 | 0 | switch (op) { |
7565 | 0 | case GGML_OP_POOL_AVG: res = 0.0f; break; |
7566 | 0 | case GGML_OP_POOL_MAX: res = -FLT_MAX; break; |
7567 | 0 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
7568 | 0 | } |
7569 | | |
7570 | 0 | int count = 0; |
7571 | 0 | const int base = (int) ow * s - p; |
7572 | |
|
7573 | 0 | for (int ki = 0; ki < k; ++ki) { |
7574 | 0 | const int j = base + ki; |
7575 | 0 | if (j < 0 || j >= (int) IW) { |
7576 | 0 | continue; |
7577 | 0 | } |
7578 | | |
7579 | 0 | float v; |
7580 | 0 | if (src->type == GGML_TYPE_F32) { |
7581 | 0 | v = ((const float *) srow_bytes)[j]; |
7582 | 0 | } else { |
7583 | 0 | v = GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t *) srow_bytes)[j]); |
7584 | 0 | } |
7585 | |
|
7586 | 0 | switch (op) { |
7587 | 0 | case GGML_OP_POOL_AVG: res += v; break; |
7588 | 0 | case GGML_OP_POOL_MAX: res = std::max(v, res); break; |
7589 | 0 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
7590 | 0 | } |
7591 | | |
7592 | 0 | ++count; |
7593 | 0 | } |
7594 | | |
7595 | 0 | switch (op) { |
7596 | 0 | case GGML_OP_POOL_AVG: res = (count > 0) ? (res / count) : 0.0f; break; |
7597 | 0 | case GGML_OP_POOL_MAX: break; |
7598 | 0 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
7599 | 0 | } |
7600 | | |
7601 | 0 | drow[ow] = res; |
7602 | 0 | } |
7603 | 0 | } |
7604 | 0 | } |
7605 | | |
7606 | | // ggml_compute_forward_pool_1d |
7607 | | |
7608 | | void ggml_compute_forward_pool_1d( |
7609 | | const ggml_compute_params * params, |
7610 | 0 | ggml_tensor * dst) { |
7611 | |
|
7612 | 0 | const int32_t * opts = (const int32_t *)dst->op_params; |
7613 | 0 | ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]); |
7614 | 0 | const int k0 = opts[1]; |
7615 | 0 | const int s0 = opts[2]; |
7616 | 0 | const int p0 = opts[3]; |
7617 | |
|
7618 | 0 | ggml_compute_forward_pool_1d_ksp(params, op, k0, s0, p0, dst); |
7619 | 0 | } |
7620 | | |
7621 | | // ggml_compute_forward_pool_2d |
7622 | | |
7623 | | void ggml_compute_forward_pool_2d( |
7624 | | const ggml_compute_params * params, |
7625 | 0 | ggml_tensor * dst) { |
7626 | |
|
7627 | 0 | const ggml_tensor * src = dst->src[0]; |
7628 | |
|
7629 | 0 | assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16); |
7630 | |
|
7631 | 0 | if (params->ith != 0) { |
7632 | 0 | return; |
7633 | 0 | } |
7634 | | |
7635 | 0 | const int32_t * opts = (const int32_t *)dst->op_params; |
7636 | |
|
7637 | 0 | ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]); |
7638 | 0 | const int k0 = opts[1]; |
7639 | 0 | const int k1 = opts[2]; |
7640 | 0 | const int s0 = opts[3]; |
7641 | 0 | const int s1 = opts[4]; |
7642 | 0 | const int p0 = opts[5]; |
7643 | 0 | const int p1 = opts[6]; |
7644 | 0 | const char * cdata = (const char*)src->data; |
7645 | 0 | const char * const data_end = cdata + ggml_nbytes(src); |
7646 | |
|
7647 | 0 | const int64_t px = dst->ne[0]; |
7648 | 0 | const int64_t py = dst->ne[1]; |
7649 | 0 | const int64_t pa = px * py; |
7650 | |
|
7651 | 0 | float * dplane = (float *)dst->data; |
7652 | |
|
7653 | 0 | const int ka = k0 * k1; |
7654 | 0 | const int offset0 = -p0; |
7655 | 0 | const int offset1 = -p1; |
7656 | |
|
7657 | 0 | while (cdata < data_end) { |
7658 | 0 | for (int oy = 0; oy < py; ++oy) { |
7659 | 0 | float * const drow = dplane + oy * px; |
7660 | 0 | float * const out = drow; |
7661 | |
|
7662 | 0 | for (int ox = 0; ox < px; ++ox) { |
7663 | 0 | float res = 0; |
7664 | 0 | switch (op) { |
7665 | 0 | case GGML_OP_POOL_AVG: res = 0; break; |
7666 | 0 | case GGML_OP_POOL_MAX: res = -FLT_MAX; break; |
7667 | 0 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
7668 | 0 | } |
7669 | | |
7670 | 0 | const int ix = offset0 + ox * s0; |
7671 | 0 | const int iy = offset1 + oy * s1; |
7672 | |
|
7673 | 0 | for (int ky = 0; ky < k1; ++ky) { |
7674 | 0 | if (iy + ky < 0 || iy + ky >= src->ne[1]) { |
7675 | 0 | continue; |
7676 | 0 | } |
7677 | | |
7678 | 0 | const void * srow = (const void *)(cdata + src->nb[1] * (iy + ky)); |
7679 | 0 | for (int kx = 0; kx < k0; ++kx) { |
7680 | 0 | int j = ix + kx; |
7681 | 0 | if (j < 0 || j >= src->ne[0]) { |
7682 | 0 | continue; |
7683 | 0 | } |
7684 | | |
7685 | 0 | const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]); |
7686 | 0 | switch (op) { |
7687 | 0 | case GGML_OP_POOL_AVG: res += srow_j; break; |
7688 | 0 | case GGML_OP_POOL_MAX: res = std::max(srow_j, res); break; |
7689 | 0 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
7690 | 0 | } |
7691 | 0 | } |
7692 | 0 | } |
7693 | 0 | switch (op) { |
7694 | 0 | case GGML_OP_POOL_AVG: res /= ka; break; |
7695 | 0 | case GGML_OP_POOL_MAX: break; |
7696 | 0 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
7697 | 0 | } |
7698 | | |
7699 | 0 | out[ox] = res; |
7700 | 0 | } |
7701 | 0 | } |
7702 | | |
7703 | 0 | cdata += src->nb[2]; |
7704 | 0 | dplane += pa; |
7705 | 0 | } |
7706 | 0 | } |
7707 | | |
7708 | | // ggml_compute_forward_pool_2d_back |
7709 | | |
7710 | | void ggml_compute_forward_pool_2d_back( |
7711 | | const ggml_compute_params * params, |
7712 | 0 | ggml_tensor * dst) { |
7713 | |
|
7714 | 0 | const ggml_tensor * src = dst->src[0]; |
7715 | 0 | const ggml_tensor * dstf = dst->src[1]; // forward tensor of dst |
7716 | |
|
7717 | 0 | assert(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16); |
7718 | |
|
7719 | 0 | if (params->ith != 0) { |
7720 | 0 | return; |
7721 | 0 | } |
7722 | | |
7723 | 0 | const int32_t * opts = (const int32_t *)dst->op_params; |
7724 | 0 | ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]); |
7725 | 0 | const int k0 = opts[1]; |
7726 | 0 | const int k1 = opts[2]; |
7727 | 0 | const int s0 = opts[3]; |
7728 | 0 | const int s1 = opts[4]; |
7729 | 0 | const int p0 = opts[5]; |
7730 | 0 | const int p1 = opts[6]; |
7731 | |
|
7732 | 0 | char * cdata = (char *) dst->data; |
7733 | 0 | const char * cdataf = (const char *) dstf->data; |
7734 | 0 | const char * const data_end = cdata + ggml_nbytes(dst); |
7735 | |
|
7736 | 0 | GGML_ASSERT(params->ith == 0); |
7737 | 0 | memset(cdata, 0, ggml_nbytes(dst)); |
7738 | |
|
7739 | 0 | const int64_t px = src->ne[0]; |
7740 | 0 | const int64_t py = src->ne[1]; |
7741 | 0 | const int64_t pa = px * py; |
7742 | |
|
7743 | 0 | const float * splane = (const float *) src->data; |
7744 | |
|
7745 | 0 | const int ka = k0 * k1; |
7746 | 0 | const int offset0 = -p0; |
7747 | 0 | const int offset1 = -p1; |
7748 | |
|
7749 | 0 | while (cdata < data_end) { |
7750 | 0 | for (int oy = 0; oy < py; ++oy) { |
7751 | 0 | const float * const srow = splane + oy * px; |
7752 | 0 | for (int ox = 0; ox < px; ++ox) { |
7753 | 0 | const float grad0 = srow[ox]; |
7754 | |
|
7755 | 0 | const int ix = offset0 + ox * s0; |
7756 | 0 | const int iy = offset1 + oy * s1; |
7757 | |
|
7758 | 0 | if (op == GGML_OP_POOL_MAX) { |
7759 | 0 | float maxval = -FLT_MAX; |
7760 | 0 | int kxmax = -1; |
7761 | 0 | int kymax = -1; |
7762 | |
|
7763 | 0 | for (int ky = 0; ky < k1; ++ky) { |
7764 | 0 | if (iy + ky < 0 || iy + ky >= dst->ne[1]) { |
7765 | 0 | continue; |
7766 | 0 | } |
7767 | 0 | const void * drowf = (const void *)(cdataf + dst->nb[1] * (iy + ky)); |
7768 | 0 | for (int kx = 0; kx < k0; ++kx) { |
7769 | 0 | int j = ix + kx; |
7770 | 0 | if (j < 0 || j >= dst->ne[0]) { |
7771 | 0 | continue; |
7772 | 0 | } |
7773 | | |
7774 | 0 | const float val = dst->type == GGML_TYPE_F32 ? |
7775 | 0 | ((const float *) drowf)[j] : GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t *) drowf)[j]); |
7776 | 0 | if (val <= maxval) { |
7777 | 0 | continue; |
7778 | 0 | } |
7779 | | |
7780 | 0 | maxval = val; |
7781 | 0 | kxmax = kx; |
7782 | 0 | kymax = ky; |
7783 | 0 | } |
7784 | 0 | } |
7785 | |
|
7786 | 0 | if (kxmax == -1 || kymax == -1) { |
7787 | 0 | continue; |
7788 | 0 | } |
7789 | | |
7790 | 0 | void * drow = (void *)(cdata + dst->nb[1] * (iy + kymax)); |
7791 | 0 | const int j = ix + kxmax; |
7792 | 0 | if (dst->type == GGML_TYPE_F32) { |
7793 | 0 | ((float *) drow)[j] += grad0; |
7794 | 0 | } else { |
7795 | 0 | ((ggml_fp16_t *) drow)[j] = GGML_CPU_FP32_TO_FP16(grad0 + GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t *) drow)[j])); |
7796 | 0 | } |
7797 | 0 | } else if (op == GGML_OP_POOL_AVG) { |
7798 | 0 | const float grad = grad0 / ka; |
7799 | |
|
7800 | 0 | for (int ky = 0; ky < k1; ++ky) { |
7801 | 0 | if (iy + ky < 0 || iy + ky >= dst->ne[1]) { |
7802 | 0 | continue; |
7803 | 0 | } |
7804 | 0 | void * drow = (void *)(cdata + dst->nb[1] * (iy + ky)); |
7805 | 0 | for (int kx = 0; kx < k0; ++kx) { |
7806 | 0 | int j = ix + kx; |
7807 | 0 | if (j < 0 || j >= dst->ne[0]) { |
7808 | 0 | continue; |
7809 | 0 | } |
7810 | | |
7811 | 0 | if (dst->type == GGML_TYPE_F32) { |
7812 | 0 | ((float *) drow)[j] += grad; |
7813 | 0 | } else { |
7814 | 0 | ((ggml_fp16_t *) drow)[j] += GGML_CPU_FP32_TO_FP16(grad); |
7815 | 0 | } |
7816 | 0 | } |
7817 | 0 | } |
7818 | 0 | } else { |
7819 | 0 | GGML_ASSERT(false); |
7820 | 0 | } |
7821 | 0 | } |
7822 | 0 | } |
7823 | |
|
7824 | 0 | cdata += dst->nb[2]; |
7825 | 0 | cdataf += dst->nb[2]; |
7826 | 0 | splane += pa; |
7827 | 0 | } |
7828 | 0 | } |
7829 | | |
7830 | | // ggml_compute_forward_upscale |
7831 | | |
7832 | | static void ggml_compute_forward_upscale_f32( |
7833 | | const ggml_compute_params * params, |
7834 | 0 | ggml_tensor * dst) { |
7835 | |
|
7836 | 0 | const ggml_tensor * src0 = dst->src[0]; |
7837 | |
|
7838 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
7839 | |
|
7840 | 0 | const int ith = params->ith; |
7841 | 0 | const int nth = params->nth; |
7842 | |
|
7843 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
7844 | |
|
7845 | 0 | float sf0 = (float)ne0/src0->ne[0]; |
7846 | 0 | float sf1 = (float)ne1/src0->ne[1]; |
7847 | 0 | float sf2 = (float)ne2/src0->ne[2]; |
7848 | 0 | float sf3 = (float)ne3/src0->ne[3]; |
7849 | 0 | float pixel_offset = 0.5f; |
7850 | |
|
7851 | 0 | const int32_t mode_flags = ggml_get_op_params_i32(dst, 0); |
7852 | 0 | const ggml_scale_mode mode = (ggml_scale_mode) (mode_flags & 0xFF); |
7853 | |
|
7854 | 0 | if (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS) { |
7855 | 0 | pixel_offset = 0.0f; |
7856 | 0 | sf0 = ne0 > 1 && ne00 > 1 ? (float)(ne0 - 1) / (ne00 - 1) : sf0; |
7857 | 0 | sf1 = ne1 > 1 && ne01 > 1 ? (float)(ne1 - 1) / (ne01 - 1) : sf1; |
7858 | 0 | } |
7859 | |
|
7860 | 0 | if (mode == GGML_SCALE_MODE_NEAREST) { |
7861 | 0 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
7862 | 0 | const int64_t i03 = i3 / sf3; |
7863 | 0 | for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
7864 | 0 | const int64_t i02 = i2 / sf2; |
7865 | 0 | for (int64_t i1 = 0; i1 < ne1; i1++) { |
7866 | 0 | const int64_t i01 = i1 / sf1; |
7867 | 0 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
7868 | 0 | const int64_t i00 = i0 / sf0; |
7869 | |
|
7870 | 0 | const float * x = (float *)((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
7871 | 0 | float * y = (float *)((char *) dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
7872 | |
|
7873 | 0 | *y = *x; |
7874 | 0 | } |
7875 | 0 | } |
7876 | 0 | } |
7877 | 0 | } |
7878 | 0 | } else if (mode == GGML_SCALE_MODE_BILINEAR && (mode_flags & GGML_SCALE_FLAG_ANTIALIAS)) { |
7879 | | // Similar to F.interpolate(..., mode="bilinear", align_corners=False, antialias=True) |
7880 | | // https://github.com/pytorch/pytorch/blob/8871ff29b743948d1225389d5b7068f37b22750b/aten/src/ATen/native/cpu/UpSampleKernel.cpp |
7881 | 0 | auto triangle_filter = [](float x) -> float { |
7882 | 0 | return std::max(1.0f - fabsf(x), 0.0f); |
7883 | 0 | }; |
7884 | | |
7885 | | // support and invscale, minimum 1 pixel for bilinear |
7886 | 0 | const float support1 = std::max(1.0f, 1.0f / sf1); |
7887 | 0 | const float invscale1 = 1.0f / support1; |
7888 | 0 | const float support0 = std::max(1.0f, 1.0f / sf0); |
7889 | 0 | const float invscale0 = 1.0f / support0; |
7890 | |
|
7891 | 0 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
7892 | 0 | const int64_t i03 = i3 / sf3; |
7893 | 0 | for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
7894 | 0 | const int64_t i02 = i2 / sf2; |
7895 | 0 | for (int64_t i1 = 0; i1 < ne1; i1++) { |
7896 | 0 | const float y = ((float) i1 + pixel_offset) / sf1; |
7897 | 0 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
7898 | 0 | const float x = ((float) i0 + pixel_offset) / sf0; |
7899 | | |
7900 | | // the range of source pixels that contribute |
7901 | 0 | const int64_t x_min = std::max<int64_t>(x - support0 + pixel_offset, 0); |
7902 | 0 | const int64_t x_max = std::min<int64_t>(x + support0 + pixel_offset, ne00); |
7903 | 0 | const int64_t y_min = std::max<int64_t>(y - support1 + pixel_offset, 0); |
7904 | 0 | const int64_t y_max = std::min<int64_t>(y + support1 + pixel_offset, ne01); |
7905 | | |
7906 | | // bilinear filter with antialiasing |
7907 | 0 | float val = 0.0f; |
7908 | 0 | float total_weight = 0.0f; |
7909 | |
|
7910 | 0 | for (int64_t sy = y_min; sy < y_max; sy++) { |
7911 | 0 | const float weight_y = triangle_filter((sy - y + pixel_offset) * invscale1); |
7912 | |
|
7913 | 0 | for (int64_t sx = x_min; sx < x_max; sx++) { |
7914 | 0 | const float weight_x = triangle_filter((sx - x + pixel_offset) * invscale0); |
7915 | 0 | const float weight = weight_x * weight_y; |
7916 | |
|
7917 | 0 | if (weight <= 0.0f) { |
7918 | 0 | continue; |
7919 | 0 | } |
7920 | | |
7921 | 0 | const float pixel = *(const float *)((const char *)src0->data + sx*nb00 + sy*nb01 + i02*nb02 + i03*nb03); |
7922 | 0 | val += pixel * weight; |
7923 | 0 | total_weight += weight; |
7924 | 0 | } |
7925 | 0 | } |
7926 | |
|
7927 | 0 | if (total_weight > 0.0f) { |
7928 | 0 | val /= total_weight; |
7929 | 0 | } |
7930 | |
|
7931 | 0 | float * dst_ptr = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
7932 | 0 | *dst_ptr = val; |
7933 | 0 | } |
7934 | 0 | } |
7935 | 0 | } |
7936 | 0 | } |
7937 | 0 | } else if (mode == GGML_SCALE_MODE_BILINEAR) { |
7938 | 0 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
7939 | 0 | const int64_t i03 = i3 / sf3; |
7940 | 0 | for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
7941 | 0 | const int64_t i02 = i2 / sf2; |
7942 | 0 | for (int64_t i1 = 0; i1 < ne1; i1++) { |
7943 | 0 | const float y = ((float)i1 + pixel_offset) / sf1 - pixel_offset; |
7944 | 0 | int64_t y0 = (int64_t)floorf(y); |
7945 | 0 | int64_t y1 = y0 + 1; |
7946 | |
|
7947 | 0 | y0 = std::max(int64_t(0), std::min(y0, ne01 - 1)); |
7948 | 0 | y1 = std::max(int64_t(0), std::min(y1, ne01 - 1)); |
7949 | |
|
7950 | 0 | float dy = y - (float)y0; |
7951 | 0 | dy = std::max(0.0f, std::min(dy, 1.0f)); |
7952 | |
|
7953 | 0 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
7954 | 0 | const float x = ((float)i0 + pixel_offset) / sf0 - pixel_offset; |
7955 | 0 | int64_t x0 = (int64_t)floorf(x); |
7956 | 0 | int64_t x1 = x0 + 1; |
7957 | |
|
7958 | 0 | x0 = std::max(int64_t(0), std::min(x0, ne00 - 1)); |
7959 | 0 | x1 = std::max(int64_t(0), std::min(x1, ne00 - 1)); |
7960 | |
|
7961 | 0 | float dx = x - (float)x0; |
7962 | 0 | dx = std::max(0.0f, std::min(dx, 1.0f)); |
7963 | | |
7964 | | // fetch the four surrounding pixel values and interpolate |
7965 | 0 | const float a = *(const float *)((const char *)src0->data + x0*nb00 + y0*nb01 + i02*nb02 + i03*nb03); |
7966 | 0 | const float b = *(const float *)((const char *)src0->data + x1*nb00 + y0*nb01 + i02*nb02 + i03*nb03); |
7967 | 0 | const float c = *(const float *)((const char *)src0->data + x0*nb00 + y1*nb01 + i02*nb02 + i03*nb03); |
7968 | 0 | const float d = *(const float *)((const char *)src0->data + x1*nb00 + y1*nb01 + i02*nb02 + i03*nb03); |
7969 | |
|
7970 | 0 | const float val = a*(1 - dx)*(1 - dy) + b*dx*(1 - dy) + c*(1 - dx)*dy + d*dx*dy; |
7971 | |
|
7972 | 0 | float * y_dst = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
7973 | 0 | *y_dst = val; |
7974 | 0 | } |
7975 | 0 | } |
7976 | 0 | } |
7977 | 0 | } |
7978 | 0 | } else if (mode == GGML_SCALE_MODE_BICUBIC) { |
7979 | | // https://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm |
7980 | 0 | const float a = -0.75f; // use alpha = -0.75 (same as PyTorch) |
7981 | 0 | auto weight1 = [a](float x) { return ((a + 2) * x - (a + 3)) * x * x + 1; }; |
7982 | 0 | auto weight2 = [a](float x) { return ((a * x - 5 * a) * x + 8 * a) * x - 4 * a; }; |
7983 | 0 | auto bicubic = [=](float p0, float p1, float p2, float p3, float x) { |
7984 | 0 | const float w0 = weight2(x + 1); |
7985 | 0 | const float w1 = weight1(x + 0); |
7986 | 0 | const float w2 = weight1(1 - x); |
7987 | 0 | const float w3 = weight2(2 - x); |
7988 | 0 | return p0*w0 + p1*w1 + p2*w2 + p3*w3; |
7989 | 0 | }; |
7990 | |
|
7991 | 0 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
7992 | 0 | const int64_t i03 = i3 / sf3; |
7993 | 0 | for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
7994 | 0 | const int64_t i02 = i2 / sf2; |
7995 | 0 | for (int64_t i1 = 0; i1 < ne1; i1++) { |
7996 | 0 | const float y = ((float)i1 + pixel_offset) / sf1 - pixel_offset; |
7997 | 0 | const int64_t y0 = (int64_t)floorf(y); |
7998 | 0 | const float dy = y - (float)y0; |
7999 | |
|
8000 | 0 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
8001 | 0 | const float x = ((float)i0 + pixel_offset) / sf0 - pixel_offset; |
8002 | 0 | const int64_t x0 = (int64_t)floorf(x); |
8003 | 0 | const float dx = x - (float)x0; |
8004 | |
|
8005 | 0 | auto p = [=](int64_t x_off, int64_t y_off) -> float { |
8006 | 0 | int64_t i00 = std::max(int64_t(0), std::min(x0 + x_off, ne00 - 1)); |
8007 | 0 | int64_t i01 = std::max(int64_t(0), std::min(y0 + y_off, ne01 - 1)); |
8008 | 0 | return *(const float *)((const char *)src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
8009 | 0 | }; |
8010 | |
|
8011 | 0 | const float val = bicubic( |
8012 | 0 | bicubic(p(-1,-1), p(0,-1), p(1,-1), p(2,-1), dx), |
8013 | 0 | bicubic(p(-1, 0), p(0, 0), p(1, 0), p(2, 0), dx), |
8014 | 0 | bicubic(p(-1, 1), p(0, 1), p(1, 1), p(2, 1), dx), |
8015 | 0 | bicubic(p(-1, 2), p(0, 2), p(1, 2), p(2, 2), dx), dy); |
8016 | |
|
8017 | 0 | float * y_dst = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
8018 | 0 | *y_dst = val; |
8019 | 0 | } |
8020 | 0 | } |
8021 | 0 | } |
8022 | 0 | } |
8023 | 0 | } else { |
8024 | 0 | GGML_ABORT("unsupported upscale mode"); |
8025 | 0 | } |
8026 | 0 | } |
8027 | | |
8028 | | void ggml_compute_forward_upscale( |
8029 | | const ggml_compute_params * params, |
8030 | 0 | ggml_tensor * dst) { |
8031 | |
|
8032 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8033 | |
|
8034 | 0 | switch (src0->type) { |
8035 | 0 | case GGML_TYPE_F32: |
8036 | 0 | { |
8037 | 0 | ggml_compute_forward_upscale_f32(params, dst); |
8038 | 0 | } break; |
8039 | 0 | default: |
8040 | 0 | { |
8041 | 0 | GGML_ABORT("fatal error"); |
8042 | 0 | } |
8043 | 0 | } |
8044 | 0 | } |
8045 | | |
8046 | | |
8047 | | // ggml_compute_forward_pad |
8048 | | |
8049 | | template<bool circular_t> |
8050 | | static void ggml_compute_forward_pad_f32( |
8051 | | const ggml_compute_params * params, |
8052 | 0 | ggml_tensor * dst) { |
8053 | |
|
8054 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8055 | |
|
8056 | 0 | assert(dst->nb[0] == sizeof(float)); |
8057 | |
|
8058 | 0 | const int ith = params->ith; |
8059 | 0 | const int nth = params->nth; |
8060 | |
|
8061 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
8062 | |
|
8063 | 0 | float * dst_ptr = (float *) dst->data; |
8064 | 0 | const int32_t lp0 = ggml_get_op_params_i32(dst, 0); |
8065 | 0 | const int32_t rp0 = ggml_get_op_params_i32(dst, 1); |
8066 | 0 | const int32_t lp1 = ggml_get_op_params_i32(dst, 2); |
8067 | 0 | const int32_t rp1 = ggml_get_op_params_i32(dst, 3); |
8068 | 0 | const int32_t lp2 = ggml_get_op_params_i32(dst, 4); |
8069 | 0 | const int32_t rp2 = ggml_get_op_params_i32(dst, 5); |
8070 | 0 | const int32_t lp3 = ggml_get_op_params_i32(dst, 6); |
8071 | 0 | const int32_t rp3 = ggml_get_op_params_i32(dst, 7); |
8072 | | |
8073 | | // TODO: optimize |
8074 | |
|
8075 | 0 | for (int64_t i2 = 0; i2 < ne2; ++i2) { |
8076 | 0 | for (int64_t i1 = ith; i1 < ne1; i1 += nth) { |
8077 | 0 | for (int64_t i0 = 0; i0 < ne0; ++i0) { |
8078 | 0 | for (int64_t i3 = 0; i3 < ne3; ++i3) { |
8079 | | // circular means wrap around on a torus, so x and y loop around |
8080 | 0 | if constexpr (circular_t) { |
8081 | 0 | const int64_t dst_idx = i3*(ne0*ne1*ne2) + i2*(ne0*ne1) + i1*ne0 + i0; |
8082 | 0 | const int64_t src_i0 = ggml_wrap_around(i0 - lp0, ne00); |
8083 | 0 | const int64_t src_i1 = ggml_wrap_around(i1 - lp1, ne01); |
8084 | 0 | const int64_t src_i2 = ggml_wrap_around(i2 - lp2, ne02); |
8085 | 0 | const int64_t src_i3 = ggml_wrap_around(i3 - lp3, ne03); |
8086 | |
|
8087 | 0 | const int64_t src_idx = |
8088 | 0 | src_i3*nb03 + |
8089 | 0 | src_i2*nb02 + |
8090 | 0 | src_i1*nb01 + |
8091 | 0 | src_i0*nb00; |
8092 | |
|
8093 | 0 | const float * src_ptr = (const float *)((char *) src0->data + src_idx); |
8094 | 0 | dst_ptr[dst_idx] = *src_ptr; |
8095 | 0 | } else { |
8096 | 0 | const int64_t dst_idx = i3*(ne0*ne1*ne2) + i2*(ne0*ne1) + i1*ne0 + i0; |
8097 | 0 | if ((i0 >= lp0 && i0 < ne0 - rp0) \ |
8098 | 0 | && (i1 >= lp1 && i1 < ne1 - rp1) \ |
8099 | 0 | && (i2 >= lp2 && i2 < ne2 - rp2) \ |
8100 | 0 | && (i3 >= lp3 && i3 < ne3 - rp3)) { |
8101 | 0 | const int64_t src_idx = (i3 - lp3)*nb03 + (i2 - lp2)*nb02 + (i1 - lp1)*nb01 + (i0 - lp0)*nb00; |
8102 | 0 | const float * src_ptr = (const float *)((char *) src0->data + src_idx); |
8103 | 0 | dst_ptr[dst_idx] = *src_ptr; |
8104 | 0 | } else { |
8105 | 0 | dst_ptr[dst_idx] = 0; |
8106 | 0 | } |
8107 | 0 | } |
8108 | 0 | } |
8109 | 0 | } |
8110 | 0 | } |
8111 | 0 | } |
8112 | 0 | } Unexecuted instantiation: ops.cpp:void ggml_compute_forward_pad_f32<true>(ggml_compute_params const*, ggml_tensor*) Unexecuted instantiation: ops.cpp:void ggml_compute_forward_pad_f32<false>(ggml_compute_params const*, ggml_tensor*) |
8113 | | |
8114 | | |
8115 | | void ggml_compute_forward_pad( |
8116 | | const ggml_compute_params * params, |
8117 | 0 | ggml_tensor * dst) { |
8118 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8119 | 0 | const bool circular = (bool) ggml_get_op_params_i32(dst, 8); |
8120 | 0 | switch (src0->type) { |
8121 | 0 | case GGML_TYPE_F32: |
8122 | 0 | { |
8123 | 0 | if (circular) { |
8124 | 0 | ggml_compute_forward_pad_f32<true>(params, dst); |
8125 | 0 | } else { |
8126 | 0 | ggml_compute_forward_pad_f32<false>(params, dst); |
8127 | 0 | } |
8128 | 0 | } break; |
8129 | 0 | default: |
8130 | 0 | { |
8131 | 0 | GGML_ABORT("fatal error"); |
8132 | 0 | } |
8133 | 0 | } |
8134 | 0 | } |
8135 | | |
8136 | | // ggml_compute_forward_pad_reflect_1d |
8137 | | |
8138 | | void ggml_compute_forward_pad_reflect_1d( |
8139 | | const ggml_compute_params * params, |
8140 | 0 | ggml_tensor * dst) { |
8141 | |
|
8142 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8143 | |
|
8144 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
8145 | 0 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
8146 | |
|
8147 | 0 | const int ith = params->ith; |
8148 | 0 | const int nth = params->nth; |
8149 | |
|
8150 | 0 | const int32_t * opts = (const int32_t *) dst->op_params; |
8151 | 0 | const int p0 = opts[0]; |
8152 | 0 | const int p1 = opts[1]; |
8153 | |
|
8154 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
8155 | |
|
8156 | 0 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
8157 | 0 | for (int64_t i2 = 0; i2 < ne2; i2++) { |
8158 | 0 | for (int64_t i1 = ith; i1 < ne1; i1 += nth) { |
8159 | 0 | float * left = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + p0*nb0); |
8160 | 0 | float * right = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + (ne0-p1-1)*nb0); |
8161 | |
|
8162 | 0 | ggml_vec_cpy_f32(ne00, left, (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
8163 | |
|
8164 | 0 | for (int i0 = 1; i0 <= p0; i0++) { left[-i0] = left[i0]; } |
8165 | 0 | for (int i0 = 1; i0 <= p1; i0++) { right[i0] = right[-i0]; } |
8166 | 0 | } |
8167 | 0 | } |
8168 | 0 | } |
8169 | 0 | } |
8170 | | |
8171 | | // ggml_compute_forward_roll |
8172 | | |
8173 | 0 | static int64_t ggml_wrap_index(int64_t i, int64_t ne) { |
8174 | 0 | if (i < 0) { |
8175 | 0 | return i + ne; |
8176 | 0 | } else if (i >= ne) { |
8177 | 0 | return i - ne; |
8178 | 0 | } |
8179 | 0 | return i; |
8180 | 0 | } |
8181 | | |
8182 | | static void ggml_compute_forward_roll_f32( |
8183 | | const ggml_compute_params * params, |
8184 | 0 | ggml_tensor * dst) { |
8185 | |
|
8186 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8187 | 0 | const float * src_data = (const float *) src0->data; |
8188 | 0 | float * dst_data = (float *) dst->data; |
8189 | |
|
8190 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
8191 | |
|
8192 | 0 | const int s0 = ggml_get_op_params_i32(dst, 0); |
8193 | 0 | const int s1 = ggml_get_op_params_i32(dst, 1); |
8194 | 0 | const int s2 = ggml_get_op_params_i32(dst, 2); |
8195 | 0 | const int s3 = ggml_get_op_params_i32(dst, 3); |
8196 | |
|
8197 | 0 | const int64_t total = ne1 * ne2 * ne3; |
8198 | 0 | const int64_t per_thread = (total + params->nth) / params->nth; |
8199 | 0 | const int64_t start = params->ith * per_thread; |
8200 | 0 | const int64_t end = std::min(start + per_thread, total); |
8201 | |
|
8202 | 0 | for (int64_t i = start; i < end; ++i) { |
8203 | 0 | const int64_t i1 = i % ne1; |
8204 | 0 | const int64_t i2 = (i / ne1) % ne2; |
8205 | 0 | const int64_t i3 = i / (ne2 * ne1); |
8206 | 0 | float * dst_row = dst_data + (i3*nb3 + i2*nb2 + i1*nb1) / sizeof(float); |
8207 | |
|
8208 | 0 | const int64_t i01 = ggml_wrap_index(i1 - s1, ne01); |
8209 | 0 | const int64_t i02 = ggml_wrap_index(i2 - s2, ne02); |
8210 | 0 | const int64_t i03 = ggml_wrap_index(i3 - s3, ne03); |
8211 | 0 | const float * src_row = src_data + (i03*nb03 + i02*nb02 + i01*nb01) / sizeof(float); |
8212 | |
|
8213 | 0 | const int64_t s = ggml_wrap_index(-s0, ne00); |
8214 | 0 | const int64_t n = ne00 - s; |
8215 | 0 | ggml_vec_cpy_f32(n, dst_row, src_row + s); |
8216 | 0 | ggml_vec_cpy_f32(s, dst_row + n, src_row); |
8217 | 0 | } |
8218 | 0 | } |
8219 | | |
8220 | | void ggml_compute_forward_roll( |
8221 | | const ggml_compute_params * params, |
8222 | 0 | ggml_tensor * dst) { |
8223 | |
|
8224 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8225 | |
|
8226 | 0 | switch (src0->type) { |
8227 | 0 | case GGML_TYPE_F32: |
8228 | 0 | { |
8229 | 0 | ggml_compute_forward_roll_f32(params, dst); |
8230 | 0 | } break; |
8231 | 0 | default: |
8232 | 0 | { |
8233 | 0 | GGML_ABORT("fatal error"); |
8234 | 0 | } |
8235 | 0 | } |
8236 | 0 | } |
8237 | | |
8238 | | // ggml_compute_forward_arange |
8239 | | |
8240 | | static void ggml_compute_forward_arange_f32( |
8241 | | const ggml_compute_params * params, |
8242 | 0 | ggml_tensor * dst) { |
8243 | |
|
8244 | 0 | GGML_ASSERT(dst->nb[0] == sizeof(float)); |
8245 | |
|
8246 | 0 | const int ith = params->ith; |
8247 | 0 | const int nth = params->nth; |
8248 | |
|
8249 | 0 | const float start = ggml_get_op_params_f32(dst, 0); |
8250 | 0 | const float stop = ggml_get_op_params_f32(dst, 1); |
8251 | 0 | const float step = ggml_get_op_params_f32(dst, 2); |
8252 | |
|
8253 | 0 | const int64_t steps = (int64_t) ceilf((stop - start) / step); |
8254 | |
|
8255 | 0 | GGML_ASSERT(ggml_nelements(dst) == steps); |
8256 | |
|
8257 | 0 | for (int64_t i = ith; i < steps; i+= nth) { |
8258 | 0 | float value = start + step * i; |
8259 | 0 | ((float *)dst->data)[i] = value; |
8260 | 0 | } |
8261 | 0 | } |
8262 | | |
8263 | | void ggml_compute_forward_arange( |
8264 | | const ggml_compute_params * params, |
8265 | 0 | ggml_tensor * dst) { |
8266 | 0 | switch (dst->type) { |
8267 | 0 | case GGML_TYPE_F32: |
8268 | 0 | { |
8269 | 0 | ggml_compute_forward_arange_f32(params, dst); |
8270 | 0 | } break; |
8271 | 0 | default: |
8272 | 0 | { |
8273 | 0 | GGML_ABORT("fatal error"); |
8274 | 0 | } |
8275 | 0 | } |
8276 | 0 | } |
8277 | | |
8278 | | static void ggml_compute_forward_timestep_embedding_f32( |
8279 | | const ggml_compute_params * params, |
8280 | 0 | ggml_tensor * dst) { |
8281 | |
|
8282 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8283 | |
|
8284 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
8285 | |
|
8286 | 0 | const int ith = params->ith; |
8287 | 0 | const int nth = params->nth; |
8288 | |
|
8289 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
8290 | |
|
8291 | 0 | const int dim = ggml_get_op_params_i32(dst, 0); |
8292 | 0 | const int max_period = ggml_get_op_params_i32(dst, 1); |
8293 | |
|
8294 | 0 | int half = dim / 2; |
8295 | |
|
8296 | 0 | for (int64_t i = 0; i < ne00; i++) { |
8297 | 0 | float * embed_data = (float *)((char *) dst->data + i*nb1); |
8298 | 0 | for (int64_t j = ith; j < half; j += nth) { |
8299 | 0 | float timestep = ((float *)src0->data)[i]; |
8300 | 0 | float freq = (float)expf(-logf(max_period) * j / half); |
8301 | 0 | float arg = timestep * freq; |
8302 | 0 | embed_data[j] = cosf(arg); |
8303 | 0 | embed_data[j + half] = sinf(arg); |
8304 | 0 | } |
8305 | 0 | if (dim % 2 != 0 && ith == 0) { |
8306 | 0 | embed_data[2 * half] = 0.f; |
8307 | 0 | } |
8308 | 0 | } |
8309 | 0 | } |
8310 | | |
8311 | | void ggml_compute_forward_timestep_embedding( |
8312 | | const ggml_compute_params * params, |
8313 | 0 | ggml_tensor * dst) { |
8314 | |
|
8315 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8316 | |
|
8317 | 0 | switch (src0->type) { |
8318 | 0 | case GGML_TYPE_F32: |
8319 | 0 | { |
8320 | 0 | ggml_compute_forward_timestep_embedding_f32(params, dst); |
8321 | 0 | } break; |
8322 | 0 | default: |
8323 | 0 | { |
8324 | 0 | GGML_ABORT("fatal error"); |
8325 | 0 | } |
8326 | 0 | } |
8327 | 0 | } |
8328 | | |
8329 | | // ggml_compute_forward_argsort |
8330 | | |
8331 | | template<enum ggml_sort_order order> |
8332 | | struct cmp_argsort { |
8333 | | const float * data; |
8334 | 0 | bool operator()(int32_t a, int32_t b) const { |
8335 | 0 | if constexpr (order == GGML_SORT_ORDER_ASC) { |
8336 | 0 | return data[a] < data[b]; |
8337 | 0 | } else { |
8338 | 0 | return data[a] > data[b]; |
8339 | 0 | } |
8340 | 0 | } Unexecuted instantiation: cmp_argsort<(ggml_sort_order)0>::operator()(int, int) const Unexecuted instantiation: cmp_argsort<(ggml_sort_order)1>::operator()(int, int) const |
8341 | | }; |
8342 | | |
8343 | | static void ggml_compute_forward_argsort_f32( |
8344 | | const ggml_compute_params * params, |
8345 | 0 | ggml_tensor * dst) { |
8346 | |
|
8347 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8348 | |
|
8349 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
8350 | |
|
8351 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
8352 | |
|
8353 | 0 | const int ith = params->ith; |
8354 | 0 | const int nth = params->nth; |
8355 | |
|
8356 | 0 | const int64_t nr = ggml_nrows(src0); |
8357 | |
|
8358 | 0 | ggml_sort_order order = (ggml_sort_order) ggml_get_op_params_i32(dst, 0); |
8359 | |
|
8360 | 0 | for (int64_t i = ith; i < nr; i += nth) { |
8361 | 0 | const float * src_data = (float *)((char *) src0->data + i*nb01); |
8362 | |
|
8363 | 0 | int32_t * dst_data = (int32_t *)((char *) dst->data + i*nb1); |
8364 | |
|
8365 | 0 | for (int64_t j = 0; j < ne0; j++) { |
8366 | 0 | dst_data[j] = j; |
8367 | 0 | } |
8368 | |
|
8369 | 0 | switch (order) { |
8370 | 0 | case GGML_SORT_ORDER_ASC: |
8371 | 0 | std::sort(dst_data, dst_data + ne0, cmp_argsort<GGML_SORT_ORDER_ASC>{src_data}); |
8372 | 0 | break; |
8373 | | |
8374 | 0 | case GGML_SORT_ORDER_DESC: |
8375 | 0 | std::sort(dst_data, dst_data + ne0, cmp_argsort<GGML_SORT_ORDER_DESC>{src_data}); |
8376 | 0 | break; |
8377 | | |
8378 | 0 | default: |
8379 | 0 | GGML_ABORT("invalid sort order"); |
8380 | 0 | } |
8381 | 0 | } |
8382 | 0 | } |
8383 | | |
8384 | | void ggml_compute_forward_argsort( |
8385 | | const ggml_compute_params * params, |
8386 | 0 | ggml_tensor * dst) { |
8387 | |
|
8388 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8389 | |
|
8390 | 0 | switch (src0->type) { |
8391 | 0 | case GGML_TYPE_F32: |
8392 | 0 | { |
8393 | 0 | ggml_compute_forward_argsort_f32(params, dst); |
8394 | 0 | } break; |
8395 | 0 | default: |
8396 | 0 | { |
8397 | 0 | GGML_ABORT("fatal error"); |
8398 | 0 | } |
8399 | 0 | } |
8400 | 0 | } |
8401 | | |
8402 | | // ggml_compute_forward_top_k |
8403 | | |
8404 | | struct cmp_top_k { |
8405 | | const float * data; |
8406 | 0 | bool operator()(int32_t a, int32_t b) const { |
8407 | 0 | return data[a] > data[b]; |
8408 | 0 | } |
8409 | | }; |
8410 | | |
8411 | | static void ggml_compute_forward_top_k_f32( |
8412 | | const ggml_compute_params * params, |
8413 | 0 | ggml_tensor * dst) { |
8414 | |
|
8415 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8416 | |
|
8417 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
8418 | |
|
8419 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
8420 | |
|
8421 | 0 | const int ith = params->ith; |
8422 | 0 | const int nth = params->nth; |
8423 | |
|
8424 | 0 | const int64_t nr = ggml_nrows(src0); |
8425 | |
|
8426 | 0 | const int top_k = ne0; |
8427 | |
|
8428 | 0 | int32_t * tmp = (int32_t *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
8429 | |
|
8430 | 0 | for (int64_t i = ith; i < nr; i += nth) { |
8431 | 0 | const float * src_data = (float *)((char *) src0->data + i*nb01); |
8432 | |
|
8433 | 0 | for (int64_t j = 0; j < ne00; j++) { |
8434 | 0 | tmp[j] = j; |
8435 | 0 | } |
8436 | |
|
8437 | 0 | std::partial_sort(tmp, tmp + top_k, tmp + ne00, cmp_top_k{src_data}); |
8438 | |
|
8439 | 0 | int32_t * dst_data = (int32_t *)((char *) dst->data + i*nb1); |
8440 | |
|
8441 | 0 | std::copy(tmp, tmp + top_k, dst_data); |
8442 | | |
8443 | | // emphasize that the order is not important |
8444 | 0 | if (top_k > 1) { |
8445 | 0 | std::swap(dst_data[0], dst_data[1]); |
8446 | 0 | } |
8447 | 0 | } |
8448 | 0 | } |
8449 | | |
8450 | | void ggml_compute_forward_top_k( |
8451 | | const ggml_compute_params * params, |
8452 | 0 | ggml_tensor * dst) { |
8453 | |
|
8454 | 0 | const ggml_tensor * src0 = dst->src[0]; |
8455 | |
|
8456 | 0 | switch (src0->type) { |
8457 | 0 | case GGML_TYPE_F32: |
8458 | 0 | { |
8459 | 0 | ggml_compute_forward_top_k_f32(params, dst); |
8460 | 0 | } break; |
8461 | 0 | default: |
8462 | 0 | { |
8463 | 0 | GGML_ABORT("fatal error"); |
8464 | 0 | } |
8465 | 0 | } |
8466 | 0 | } |
8467 | | |
8468 | | static void ggml_compute_forward_flash_attn_ext_f16_one_chunk( |
8469 | | const ggml_compute_params * params, |
8470 | | ggml_tensor * dst, |
8471 | | int ir0, int ir1, |
8472 | | int64_t ic_start, int64_t ic_end, |
8473 | 0 | float * partials, int64_t partial_stride) { |
8474 | |
|
8475 | 0 | const bool write_partials = (partials != nullptr); |
8476 | 0 | const ggml_tensor * q = dst->src[0]; |
8477 | 0 | const ggml_tensor * k = dst->src[1]; |
8478 | 0 | const ggml_tensor * v = dst->src[2]; |
8479 | 0 | const ggml_tensor * mask = dst->src[3]; |
8480 | 0 | const ggml_tensor * sinks = dst->src[4]; |
8481 | |
|
8482 | 0 | GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
8483 | 0 | GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
8484 | 0 | GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
8485 | 0 | GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
8486 | 0 | GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
8487 | 0 | GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
8488 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
8489 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
8490 | |
|
8491 | 0 | const int64_t DK = nek0; |
8492 | 0 | const int64_t DV = nev0; |
8493 | 0 | const int64_t N = neq1; |
8494 | |
|
8495 | 0 | GGML_ASSERT(ne0 == DV); |
8496 | 0 | GGML_ASSERT(ne2 == N); |
8497 | | |
8498 | | // input tensor rows must be contiguous |
8499 | 0 | GGML_ASSERT(nbq0 == ggml_type_size(q->type)); |
8500 | 0 | GGML_ASSERT(nbk0 == ggml_type_size(k->type)); |
8501 | 0 | GGML_ASSERT(nbv0 == ggml_type_size(v->type)); |
8502 | |
|
8503 | 0 | GGML_ASSERT(neq0 == DK); |
8504 | 0 | GGML_ASSERT(nek0 == DK); |
8505 | 0 | GGML_ASSERT(nev0 == DV); |
8506 | |
|
8507 | 0 | GGML_ASSERT(neq1 == N); |
8508 | | |
8509 | | // dst cannot be transposed or permuted |
8510 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
8511 | 0 | GGML_ASSERT(nb0 <= nb1); |
8512 | 0 | GGML_ASSERT(nb1 <= nb2); |
8513 | 0 | GGML_ASSERT(nb2 <= nb3); |
8514 | | |
8515 | | // broadcast factors |
8516 | 0 | const int64_t rk2 = neq2/nek2; |
8517 | 0 | const int64_t rk3 = neq3/nek3; |
8518 | |
|
8519 | 0 | const int64_t rv2 = neq2/nev2; |
8520 | 0 | const int64_t rv3 = neq3/nev3; |
8521 | | |
8522 | | // parallelize by q rows using ggml_vec_dot_f32 |
8523 | |
|
8524 | 0 | float scale = 1.0f; |
8525 | 0 | float max_bias = 0.0f; |
8526 | 0 | float logit_softcap = 0.0f; |
8527 | |
|
8528 | 0 | memcpy(&scale, (float *) dst->op_params + 0, sizeof(float)); |
8529 | 0 | memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float)); |
8530 | 0 | memcpy(&logit_softcap, (float *) dst->op_params + 2, sizeof(float)); |
8531 | |
|
8532 | 0 | if (logit_softcap != 0) { |
8533 | 0 | scale /= logit_softcap; |
8534 | 0 | } |
8535 | |
|
8536 | 0 | const uint32_t n_head = neq2; |
8537 | 0 | const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); |
8538 | |
|
8539 | 0 | const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); |
8540 | 0 | const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); |
8541 | |
|
8542 | 0 | ggml_type const k_vec_dot_type = ggml_get_type_traits_cpu(k->type)->vec_dot_type; |
8543 | 0 | ggml_from_float_t const q_to_vec_dot = ggml_get_type_traits_cpu(k_vec_dot_type)->from_float; |
8544 | 0 | ggml_vec_dot_t const kq_vec_dot = ggml_get_type_traits_cpu(k->type)->vec_dot; |
8545 | 0 | ggml_to_float_t const v_to_float = ggml_get_type_traits(v->type)->to_float; |
8546 | |
|
8547 | 0 | GGML_ASSERT(( q_to_vec_dot) && "fattn: unsupported K-type"); |
8548 | 0 | GGML_ASSERT((v->type == GGML_TYPE_F32 || v_to_float ) && "fattn: unsupported V-type"); |
8549 | |
|
8550 | 0 | int ith = params->ith; |
8551 | |
|
8552 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
8553 | | // q indices |
8554 | 0 | const int iq3 = ir/(neq2*neq1); |
8555 | 0 | const int iq2 = (ir - iq3*neq2*neq1)/neq1; |
8556 | 0 | const int iq1 = (ir - iq3*neq2*neq1 - iq2*neq1); |
8557 | |
|
8558 | 0 | const uint32_t h = iq2; // head index |
8559 | 0 | const float slope = (max_bias > 0.0f) ? h < n_head_log2 ? powf(m0, h + 1) : powf(m1, 2*(h - n_head_log2) + 1) : 1.0f; |
8560 | |
|
8561 | 0 | float S = 0.0f; // sum |
8562 | 0 | float M = -INFINITY; // maximum KQ value |
8563 | |
|
8564 | 0 | float * VKQ32 = (float *) params->wdata + ith*(1*DK + 2*DV + CACHE_LINE_SIZE_F32); // FP32 VKQ accumulator |
8565 | 0 | float * V32 = (VKQ32 + 1*DV); // (temporary) FP32 V buffer |
8566 | 0 | ggml_fp16_t * VKQ16 = (ggml_fp16_t *) (VKQ32 + 1*DV); // (temporary) FP16 VKQ accumulator |
8567 | 0 | ggml_fp16_t * Q_q = (ggml_fp16_t *) (VKQ32 + 2*DV); // (temporary) buffer for Q converted to quantized/FP16 |
8568 | |
|
8569 | 0 | if (v->type == GGML_TYPE_F16) { |
8570 | 0 | memset(VKQ16, 0, DV*sizeof(ggml_fp16_t)); |
8571 | 0 | } else { |
8572 | 0 | memset(VKQ32, 0, DV*sizeof(float)); |
8573 | 0 | } |
8574 | |
|
8575 | 0 | const ggml_fp16_t * mp = mask ? (ggml_fp16_t *)((char *) mask->data + iq1*mask->nb[1] + (iq2%mask->ne[2])*mask->nb[2] + (iq3%mask->ne[3])*mask->nb[3]) : NULL; |
8576 | | |
8577 | | // k indices |
8578 | 0 | const int ik3 = iq3 / rk3; |
8579 | 0 | const int ik2 = iq2 / rk2; |
8580 | | |
8581 | | // v indices |
8582 | 0 | const int iv3 = iq3 / rv3; |
8583 | 0 | const int iv2 = iq2 / rv2; |
8584 | |
|
8585 | 0 | const float * pq = (const float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)); |
8586 | 0 | q_to_vec_dot(pq, Q_q, DK); |
8587 | | |
8588 | | // online softmax / attention |
8589 | | // loop over n_kv and n_head_kv |
8590 | | // ref: https://arxiv.org/pdf/2112.05682.pdf |
8591 | |
|
8592 | 0 | for (int64_t ic = ic_start; ic < ic_end; ++ic) { |
8593 | 0 | const float mv = mp ? slope*GGML_CPU_FP16_TO_FP32(mp[ic]) : 0.0f; |
8594 | 0 | if (mv == -INFINITY) { |
8595 | 0 | continue; |
8596 | 0 | } |
8597 | | |
8598 | 0 | float s; // KQ value |
8599 | |
|
8600 | 0 | const char * k_data = (const char *) k->data + ( ic*nbk1 + ik2*nbk2 + ik3*nbk3); |
8601 | 0 | kq_vec_dot(DK, &s, 0, k_data, 0, Q_q, 0, 1); |
8602 | |
|
8603 | 0 | s = s*scale; // scale KQ value |
8604 | |
|
8605 | 0 | if (logit_softcap != 0.0f) { |
8606 | 0 | s = logit_softcap*tanhf(s); |
8607 | 0 | } |
8608 | |
|
8609 | 0 | s += mv; // apply mask |
8610 | |
|
8611 | 0 | const float Mold = M; |
8612 | |
|
8613 | 0 | float ms = 1.0f; // upon new higher max val, scale VKQ and KQ sum with this value |
8614 | 0 | float vs = 1.0f; // post-softmax KQ value, expf(s - M) |
8615 | |
|
8616 | 0 | const char * v_data = ((const char *) v->data + (ic*nbv1 + iv2*nbv2 + iv3*nbv3)); |
8617 | |
|
8618 | 0 | if (v->type == GGML_TYPE_F16) { |
8619 | 0 | if (s > M) { |
8620 | | // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f |
8621 | 0 | M = s; |
8622 | 0 | ms = expf(Mold - M); |
8623 | | |
8624 | | // V = V*expf(Mold - M) |
8625 | 0 | ggml_vec_scale_f16(DV, VKQ16, ms); |
8626 | 0 | } else { |
8627 | | // no new maximum, ms == 1.0f, vs != 1.0f |
8628 | 0 | vs = expf(s - M); |
8629 | 0 | } |
8630 | | |
8631 | | // V += v*expf(s - M) |
8632 | 0 | ggml_vec_mad_f16(DV, VKQ16, (const ggml_fp16_t *) v_data, vs); |
8633 | 0 | } else { |
8634 | 0 | if (s > M) { |
8635 | | // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f |
8636 | 0 | M = s; |
8637 | 0 | ms = expf(Mold - M); |
8638 | | |
8639 | | // V = V*expf(Mold - M) |
8640 | 0 | ggml_vec_scale_f32(DV, VKQ32, ms); |
8641 | 0 | } else { |
8642 | | // no new maximum, ms == 1.0f, vs != 1.0f |
8643 | 0 | vs = expf(s - M); |
8644 | 0 | } |
8645 | | |
8646 | | // V += v*expf(s - M) |
8647 | 0 | if (v_to_float) { |
8648 | 0 | v_to_float(v_data, V32, DV); |
8649 | 0 | ggml_vec_mad_f32(DV, VKQ32, V32, vs); |
8650 | 0 | } else { |
8651 | | // V is F32 |
8652 | 0 | ggml_vec_mad_f32(DV, VKQ32, (const float *) v_data, vs); |
8653 | 0 | } |
8654 | 0 | } |
8655 | |
|
8656 | 0 | S = S*ms + vs; // scale and increment sum with partial sum |
8657 | 0 | } |
8658 | |
|
8659 | 0 | if (v->type == GGML_TYPE_F16) { |
8660 | 0 | for (int64_t d = 0; d < DV; ++d) { |
8661 | 0 | VKQ32[d] = GGML_CPU_FP16_TO_FP32(VKQ16[d]); |
8662 | 0 | } |
8663 | 0 | } |
8664 | | |
8665 | | // sinks - apply only on the first kv-chunk |
8666 | 0 | if (sinks && ic_start == 0) { |
8667 | 0 | const float s = ((float *)((char *) sinks->data))[h]; |
8668 | |
|
8669 | 0 | float ms = 1.0f; |
8670 | 0 | float vs = 1.0f; |
8671 | |
|
8672 | 0 | if (s > M) { |
8673 | 0 | ms = expf(M - s); |
8674 | 0 | M = s; |
8675 | 0 | ggml_vec_scale_f32(DV, VKQ32, ms); |
8676 | 0 | } else { |
8677 | 0 | vs = expf(s - M); |
8678 | 0 | } |
8679 | |
|
8680 | 0 | S = S*ms + vs; |
8681 | 0 | } |
8682 | |
|
8683 | 0 | if (write_partials) { |
8684 | | // Write M, S, VKQ to partials for later reduction |
8685 | | // partials layout: [M, S, VKQ[DV]] per query head |
8686 | 0 | float * partial = partials + ir * partial_stride; |
8687 | 0 | partial[0] = M; |
8688 | 0 | partial[1] = S; |
8689 | 0 | memcpy(partial + 2, VKQ32, DV * sizeof(float)); |
8690 | 0 | } else { |
8691 | | // V /= S |
8692 | 0 | const float S_inv = S == 0.0f ? 0.0f : 1.0f/S; |
8693 | 0 | ggml_vec_scale_f32(DV, VKQ32, S_inv); |
8694 | | |
8695 | | // dst indices |
8696 | 0 | const int i1 = iq1; |
8697 | 0 | const int i2 = iq2; |
8698 | 0 | const int i3 = iq3; |
8699 | | |
8700 | | // permute(0, 2, 1, 3) |
8701 | 0 | memcpy((char *) dst->data + (i3*ne2*ne1 + i2 + i1*ne1)*nb1, VKQ32, nb1); |
8702 | 0 | } |
8703 | 0 | } |
8704 | 0 | } |
8705 | | |
8706 | | static void ggml_compute_forward_flash_attn_ext_tiled( |
8707 | | const ggml_compute_params * params, |
8708 | | ggml_tensor * dst, |
8709 | 0 | int ir0, int ir1) { |
8710 | 0 | const ggml_tensor * q = dst->src[0]; |
8711 | 0 | const ggml_tensor * k = dst->src[1]; |
8712 | 0 | const ggml_tensor * v = dst->src[2]; |
8713 | 0 | const ggml_tensor * mask = dst->src[3]; |
8714 | 0 | const ggml_tensor * sinks = dst->src[4]; |
8715 | |
|
8716 | 0 | GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
8717 | 0 | GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
8718 | 0 | GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
8719 | 0 | GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
8720 | 0 | GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
8721 | 0 | GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
8722 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
8723 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
8724 | |
|
8725 | 0 | const int64_t DK = nek0; |
8726 | 0 | const int64_t DV = nev0; |
8727 | 0 | const int64_t N = neq1; |
8728 | |
|
8729 | 0 | GGML_ASSERT(ne0 == DV); |
8730 | 0 | GGML_ASSERT(ne2 == N); |
8731 | | |
8732 | | // input tensor rows must be contiguous |
8733 | 0 | GGML_ASSERT(nbq0 == ggml_type_size(q->type)); |
8734 | 0 | GGML_ASSERT(nbk0 == ggml_type_size(k->type)); |
8735 | 0 | GGML_ASSERT(nbv0 == ggml_type_size(v->type)); |
8736 | |
|
8737 | 0 | GGML_ASSERT(neq0 == DK); |
8738 | 0 | GGML_ASSERT(nek0 == DK); |
8739 | 0 | GGML_ASSERT(nev0 == DV); |
8740 | |
|
8741 | 0 | GGML_ASSERT(neq1 == N); |
8742 | | |
8743 | | // dst cannot be transposed or permuted |
8744 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
8745 | 0 | GGML_ASSERT(nb0 <= nb1); |
8746 | 0 | GGML_ASSERT(nb1 <= nb2); |
8747 | 0 | GGML_ASSERT(nb2 <= nb3); |
8748 | |
|
8749 | 0 | GGML_ASSERT(k->type == v->type); |
8750 | 0 | const ggml_type kv_type = k->type; |
8751 | | |
8752 | | |
8753 | | // broadcast factors |
8754 | 0 | const int64_t rk2 = neq2/nek2; |
8755 | 0 | const int64_t rk3 = neq3/nek3; |
8756 | |
|
8757 | 0 | const int64_t rv2 = neq2/nev2; |
8758 | 0 | const int64_t rv3 = neq3/nev3; |
8759 | |
|
8760 | 0 | float scale = 1.0f; |
8761 | 0 | float max_bias = 0.0f; |
8762 | 0 | float logit_softcap = 0.0f; |
8763 | |
|
8764 | 0 | memcpy(&scale, (float *) dst->op_params + 0, sizeof(float)); |
8765 | 0 | memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float)); |
8766 | 0 | memcpy(&logit_softcap, (float *) dst->op_params + 2, sizeof(float)); |
8767 | |
|
8768 | 0 | if (logit_softcap != 0) { |
8769 | 0 | scale /= logit_softcap; |
8770 | 0 | } |
8771 | |
|
8772 | 0 | const uint32_t n_head = neq2; |
8773 | 0 | const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); |
8774 | |
|
8775 | 0 | const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); |
8776 | 0 | const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); |
8777 | |
|
8778 | 0 | int ith = params->ith; |
8779 | |
|
8780 | 0 | static constexpr int Q_TILE_SZ = ggml_fa_tile_config::Q; |
8781 | 0 | static constexpr int KV_TILE_SZ = ggml_fa_tile_config::KV; |
8782 | |
|
8783 | 0 | int ir = ir0; |
8784 | 0 | while (ir < ir1) { |
8785 | | // q indices for the start of this tile |
8786 | 0 | const int iq3 = ir/(neq2*neq1); |
8787 | 0 | const int iq2 = (ir - iq3*neq2*neq1)/neq1; |
8788 | 0 | const int iq1 = (ir - iq3*neq2*neq1 - iq2*neq1); |
8789 | | |
8790 | | // Number of valid rows in this tile: |
8791 | | // - limited by tile size (Q_TILE_SZ) |
8792 | | // - limited by chunk boundary (ir1 - ir) |
8793 | | // - limited by head boundary (neq1 - iq1) to avoid crossing into next head |
8794 | 0 | const int tile_rows = MIN(Q_TILE_SZ, MIN((int)(ir1 - ir), (int)(neq1 - iq1))); |
8795 | 0 | GGML_ASSERT(tile_rows > 0); |
8796 | |
|
8797 | 0 | const uint32_t h = iq2; // head index |
8798 | 0 | const float slope = (max_bias > 0.0f) ? h < n_head_log2 ? powf(m0, h + 1) : powf(m1, 2*(h - n_head_log2) + 1) : 1.0f; |
8799 | |
|
8800 | 0 | float S[Q_TILE_SZ]; |
8801 | 0 | float M[Q_TILE_SZ]; |
8802 | |
|
8803 | 0 | for (int i = 0 ; i < Q_TILE_SZ; ++i) { |
8804 | 0 | S[i] = 0.; |
8805 | 0 | M[i] = -INFINITY; |
8806 | 0 | } |
8807 | | |
8808 | | // Per-thread scratch layout: |
8809 | | // Q_q: Q_TILE_SZ * DK (converted Q tile — F32 for GEMM, KV type for scalar) |
8810 | | // KQ: Q_TILE_SZ * KV_TILE_SZ (attention scores in float) |
8811 | | // mask: Q_TILE_SZ * KV_TILE_SZ (mask in float) |
8812 | | // VKQ32: Q_TILE_SZ * DV (FP32 output accumulator) |
8813 | | // V32: KV_TILE_SZ * DV (F32 buffer for V tile) |
8814 | | // K_f32: KV_TILE_SZ * DK (F32 buffer for K tile — GEMM path) |
8815 | 0 | float * base = (float *) params->wdata + ith*(Q_TILE_SZ*DK + 2*Q_TILE_SZ*KV_TILE_SZ + Q_TILE_SZ*DV + KV_TILE_SZ*DV + KV_TILE_SZ*DK + CACHE_LINE_SIZE_F32); |
8816 | |
|
8817 | 0 | void * Q_q = base; |
8818 | 0 | float * KQ = (float *)((char *)base + Q_TILE_SZ * DK * sizeof(float)); |
8819 | 0 | float * mask32 = KQ + Q_TILE_SZ * KV_TILE_SZ; |
8820 | 0 | float * VKQ32 = mask32 + Q_TILE_SZ * KV_TILE_SZ; |
8821 | 0 | float * V32 = VKQ32 + Q_TILE_SZ * DV; |
8822 | 0 | float * K_f32 = V32 + KV_TILE_SZ * DV; |
8823 | |
|
8824 | 0 | memset(VKQ32, 0, Q_TILE_SZ * DV * sizeof(float)); |
8825 | 0 | memset(mask32, 0, Q_TILE_SZ * KV_TILE_SZ * sizeof(float)); |
8826 | | |
8827 | | // k indices |
8828 | 0 | const int ik3 = iq3 / rk3; |
8829 | 0 | const int ik2 = iq2 / rk2; |
8830 | | |
8831 | | // v indices |
8832 | 0 | const int iv3 = iq3 / rv3; |
8833 | 0 | const int iv2 = iq2 / rv2; |
8834 | |
|
8835 | 0 | { |
8836 | 0 | float * Q_f32 = (float *)Q_q; |
8837 | 0 | for (int tq = 0; tq < tile_rows; tq++) { |
8838 | 0 | const float * pq = (const float *) ((char *) q->data + ((iq1 + tq)*nbq1 + iq2*nbq2 + iq3*nbq3)); |
8839 | 0 | memcpy(Q_f32 + tq * DK, pq, DK * sizeof(float)); |
8840 | 0 | } |
8841 | 0 | for (int tq = tile_rows; tq < Q_TILE_SZ; tq++) { |
8842 | 0 | memset(Q_f32 + tq * DK, 0, DK * sizeof(float)); |
8843 | 0 | } |
8844 | 0 | } |
8845 | |
|
8846 | 0 | memset(K_f32, 0, DK * KV_TILE_SZ * sizeof(float)); |
8847 | 0 | memset(V32, 0, KV_TILE_SZ * DV * sizeof(float)); |
8848 | |
|
8849 | 0 | for (int64_t ic = 0; ic < nek1; ic += KV_TILE_SZ) { |
8850 | 0 | const int kv_tile = (int)std::min((int64_t)KV_TILE_SZ, nek1 - ic); |
8851 | | |
8852 | | // skip the tile entirely if all the masks are -inf |
8853 | 0 | if (mask) { |
8854 | 0 | bool can_skip = true; |
8855 | 0 | for (int tq = 0; tq < tile_rows; tq++) { |
8856 | 0 | const ggml_fp16_t * mp_row = (const ggml_fp16_t *)((const char *) mask->data + (iq1 + tq)*mask->nb[1] + (iq2%mask->ne[2])*mask->nb[2] + (iq3%mask->ne[3])*mask->nb[3]); |
8857 | 0 | for (int tk = 0; tk < kv_tile; tk++) { |
8858 | 0 | mask32[tq * KV_TILE_SZ + tk] = slope * GGML_CPU_FP16_TO_FP32(mp_row[ic + tk]); |
8859 | 0 | if (mask32[tq * KV_TILE_SZ + tk] != -INFINITY) { |
8860 | 0 | can_skip = false; |
8861 | 0 | } |
8862 | 0 | } |
8863 | | // Pad remaining mask entries with -inf |
8864 | 0 | for (int tk = kv_tile; tk < KV_TILE_SZ; tk++) { |
8865 | 0 | mask32[tq * KV_TILE_SZ + tk] = -INFINITY; |
8866 | 0 | } |
8867 | 0 | } |
8868 | |
|
8869 | 0 | if (can_skip) { |
8870 | 0 | continue; |
8871 | 0 | } |
8872 | 0 | } |
8873 | | |
8874 | | // Pack K tile transposed: K_f32[dk][kv] so KV_TILE is contiguous (SIMD dim) |
8875 | | // Zero-pad the last tile so the GEMM always operates on KV_TILE_SZ columns |
8876 | 0 | for (int tk = 0; tk < kv_tile; tk++) { |
8877 | 0 | const char * k_data = (const char *)k->data + (ic + tk)*nbk1 + ik2*nbk2 + ik3*nbk3; |
8878 | 0 | if (kv_type == GGML_TYPE_F16) { |
8879 | 0 | const ggml_fp16_t * k_f16 = (const ggml_fp16_t *)k_data; |
8880 | 0 | for (int64_t dk = 0; dk < DK; dk++) { |
8881 | 0 | K_f32[dk * KV_TILE_SZ + tk] = GGML_CPU_FP16_TO_FP32(k_f16[dk]); |
8882 | 0 | } |
8883 | 0 | } else { |
8884 | 0 | const float * k_f32_src = (const float *)k_data; |
8885 | 0 | for (int64_t dk = 0; dk < DK; dk++) { |
8886 | 0 | K_f32[dk * KV_TILE_SZ + tk] = k_f32_src[dk]; |
8887 | 0 | } |
8888 | 0 | } |
8889 | 0 | } |
8890 | 0 | memset(KQ, 0, Q_TILE_SZ * KV_TILE_SZ * sizeof(float)); |
8891 | 0 | simd_gemm(KQ, (const float *)Q_q, K_f32, Q_TILE_SZ, DK, KV_TILE_SZ); |
8892 | 0 | ggml_vec_scale_f32(Q_TILE_SZ * KV_TILE_SZ, KQ, scale); |
8893 | | |
8894 | | // Set padded KQ entries to -inf so softmax gives them zero weight |
8895 | 0 | if (kv_tile < KV_TILE_SZ) { |
8896 | 0 | for (int tq = 0; tq < Q_TILE_SZ; tq++) { |
8897 | 0 | for (int tk = kv_tile; tk < KV_TILE_SZ; tk++) { |
8898 | 0 | KQ[tq * KV_TILE_SZ + tk] = -INFINITY; |
8899 | 0 | } |
8900 | 0 | } |
8901 | 0 | } |
8902 | |
|
8903 | 0 | if (logit_softcap != 0.0f) { |
8904 | 0 | ggml_vec_tanh_f32(Q_TILE_SZ * KV_TILE_SZ, KQ, KQ); |
8905 | 0 | ggml_vec_scale_f32(Q_TILE_SZ * KV_TILE_SZ, KQ, logit_softcap); |
8906 | 0 | } |
8907 | |
|
8908 | 0 | if (mask) { |
8909 | 0 | ggml_vec_add_f32(tile_rows * KV_TILE_SZ, KQ, KQ, mask32); |
8910 | 0 | } |
8911 | |
|
8912 | 0 | bool skip[Q_TILE_SZ] = {}; |
8913 | |
|
8914 | 0 | for (int tq = 0; tq < Q_TILE_SZ; tq++) { |
8915 | 0 | float * kq_row = KQ + tq * KV_TILE_SZ; |
8916 | |
|
8917 | 0 | float tile_max; |
8918 | 0 | ggml_vec_max_f32(KV_TILE_SZ, &tile_max, kq_row); |
8919 | |
|
8920 | 0 | if (tile_max == -INFINITY) { |
8921 | 0 | skip[tq] = true; |
8922 | 0 | continue; |
8923 | 0 | } |
8924 | | |
8925 | 0 | const float Mold = M[tq]; |
8926 | 0 | const float Mnew = fmaxf(Mold, tile_max); |
8927 | |
|
8928 | 0 | if (Mnew > Mold) { |
8929 | 0 | const float ms = expf(Mold - Mnew); |
8930 | 0 | ggml_vec_scale_f32(DV, VKQ32 + tq * DV, ms); |
8931 | 0 | S[tq] *= ms; |
8932 | 0 | } |
8933 | 0 | M[tq] = Mnew; |
8934 | | |
8935 | |
|
8936 | 0 | S[tq] += ggml_vec_soft_max_f32(KV_TILE_SZ, kq_row, kq_row, Mnew); |
8937 | 0 | } |
8938 | | |
8939 | | // V accumulation: VKQ32 += softmax(KQ) * V |
8940 | | // Pack V tile to contiguous F32, zero-padded |
8941 | 0 | for (int tk = 0; tk < kv_tile; tk++) { |
8942 | 0 | const char * v_data = (const char *)v->data + (ic + tk)*nbv1 + iv2*nbv2 + iv3*nbv3; |
8943 | 0 | if (kv_type == GGML_TYPE_F16) { |
8944 | 0 | ggml_fp16_to_fp32_row((const ggml_fp16_t *)v_data, V32 + tk * DV, DV); |
8945 | 0 | } else { |
8946 | 0 | memcpy(V32 + tk * DV, v_data, DV * sizeof(float)); |
8947 | 0 | } |
8948 | 0 | } |
8949 | 0 | for (int tq = 0; tq < Q_TILE_SZ; tq++) { |
8950 | 0 | if (skip[tq]) { |
8951 | 0 | memset(KQ + tq * KV_TILE_SZ, 0, KV_TILE_SZ * sizeof(float)); |
8952 | 0 | } |
8953 | 0 | } |
8954 | 0 | simd_gemm(VKQ32, KQ, V32, Q_TILE_SZ, KV_TILE_SZ, DV); |
8955 | 0 | } |
8956 | | |
8957 | | // sinks (apply only to valid rows in the tile) |
8958 | 0 | if (sinks) { |
8959 | 0 | const float s = ((float *)((char *) sinks->data))[h]; |
8960 | |
|
8961 | 0 | for (int tq = 0; tq < tile_rows; tq++) { |
8962 | 0 | float ms = 1.0f; |
8963 | 0 | float vs = 1.0f; |
8964 | |
|
8965 | 0 | if (s > M[tq]) { |
8966 | 0 | ms = expf(M[tq] - s); |
8967 | 0 | ggml_vec_scale_f32(DV, VKQ32 + tq * DV, ms); |
8968 | 0 | } else { |
8969 | 0 | vs = expf(s - M[tq]); |
8970 | 0 | } |
8971 | |
|
8972 | 0 | S[tq] = S[tq] * ms + vs; |
8973 | 0 | } |
8974 | 0 | } |
8975 | |
|
8976 | 0 | for (int tq = 0; tq < tile_rows; tq++) { |
8977 | | // V /= S |
8978 | 0 | const float S_inv = S[tq] == 0.0f ? 0.0f : 1.0f / S[tq]; |
8979 | 0 | ggml_vec_scale_f32(DV, VKQ32 + tq * DV, S_inv); |
8980 | | |
8981 | | // dst indices |
8982 | 0 | const int i1 = iq1 + tq; |
8983 | 0 | const int i2 = iq2; |
8984 | 0 | const int i3 = iq3; |
8985 | | |
8986 | | // permute(0, 2, 1, 3) |
8987 | 0 | memcpy((char *) dst->data + (i3*ne2*ne1 + i2 + i1*ne1)*nb1, VKQ32 + tq * DV, nb1); |
8988 | 0 | } |
8989 | |
|
8990 | 0 | ir += tile_rows; |
8991 | 0 | } |
8992 | 0 | } |
8993 | | |
8994 | | // Reduction function: combines partial results across KV chunks |
8995 | | // Partials layout in wdata: [n_q_heads][n_chunks][2 + DV] |
8996 | | static void ggml_flash_attn_ext_reduce_partials( |
8997 | | const ggml_compute_params * params, |
8998 | | ggml_tensor * dst, |
8999 | | const int64_t n_chunks, |
9000 | 0 | const int64_t chunk_size) { |
9001 | |
|
9002 | 0 | const ggml_tensor * q = dst->src[0]; |
9003 | 0 | const ggml_tensor * k = dst->src[1]; |
9004 | 0 | const ggml_tensor * v = dst->src[2]; |
9005 | |
|
9006 | 0 | const int64_t DK = k->ne[0]; |
9007 | 0 | const int64_t DV = v->ne[0]; |
9008 | 0 | const int64_t nek1 = k->ne[1]; |
9009 | 0 | const int64_t n_q_heads = q->ne[2]; |
9010 | |
|
9011 | 0 | const int ith = params->ith; |
9012 | 0 | const int nth = params->nth; |
9013 | |
|
9014 | 0 | const int64_t wdata_per_thread = DK + 2*DV + CACHE_LINE_SIZE_F32; |
9015 | 0 | float * thread_wdata = (float *) params->wdata + ith * wdata_per_thread; |
9016 | |
|
9017 | 0 | const int64_t partials_offset = nth * (DK + 2*DV + CACHE_LINE_SIZE_F32); |
9018 | 0 | const int64_t partial_size = 2 + DV; |
9019 | 0 | const float * partials_base = (const float *) params->wdata + partials_offset; |
9020 | | |
9021 | | // Output layout |
9022 | 0 | const int64_t ne1 = dst->ne[1]; |
9023 | 0 | const int64_t ne2 = dst->ne[2]; |
9024 | 0 | const size_t nb1 = dst->nb[1]; |
9025 | | |
9026 | | // Each thread reduces a subset of query heads |
9027 | 0 | for (int64_t q_head = ith; q_head < n_q_heads; q_head += nth) { |
9028 | 0 | float M_final = -INFINITY; |
9029 | 0 | float S_final = 0.0f; |
9030 | 0 | float * VKQ_final = thread_wdata; |
9031 | 0 | memset(VKQ_final, 0, DV * sizeof(float)); |
9032 | | |
9033 | | // Combine partials from all chunks |
9034 | 0 | for (int64_t chunk_idx = 0; chunk_idx < n_chunks; ++chunk_idx) { |
9035 | 0 | const int64_t ic_start = chunk_idx * chunk_size; |
9036 | 0 | if (ic_start >= nek1) continue; |
9037 | | |
9038 | 0 | const float * partial = partials_base + (q_head * n_chunks + chunk_idx) * partial_size; |
9039 | 0 | const float M_chunk = partial[0]; |
9040 | 0 | const float S_chunk = partial[1]; |
9041 | 0 | const float * VKQ_chunk = partial + 2; |
9042 | |
|
9043 | 0 | if (S_chunk == 0.0f) continue; |
9044 | | |
9045 | 0 | const float M_new = fmaxf(M_final, M_chunk); |
9046 | 0 | const float scale_old = expf(M_final - M_new); |
9047 | 0 | const float scale_new = expf(M_chunk - M_new); |
9048 | |
|
9049 | 0 | for (int64_t d = 0; d < DV; ++d) { |
9050 | 0 | VKQ_final[d] = VKQ_final[d] * scale_old + VKQ_chunk[d] * scale_new; |
9051 | 0 | } |
9052 | 0 | S_final = S_final * scale_old + S_chunk * scale_new; |
9053 | 0 | M_final = M_new; |
9054 | 0 | } |
9055 | | |
9056 | | // Normalize and write to output |
9057 | 0 | if (S_final != 0.0f) { |
9058 | 0 | const float S_inv = 1.0f / S_final; |
9059 | 0 | ggml_vec_scale_f32(DV, VKQ_final, S_inv); |
9060 | 0 | } |
9061 | | // iq1=0, iq3=0 for decode |
9062 | 0 | memcpy((char *) dst->data + (0*ne2*ne1 + q_head + 0*ne1)*nb1, VKQ_final, nb1); |
9063 | 0 | } |
9064 | 0 | } |
9065 | | |
9066 | | static void ggml_compute_forward_flash_attn_ext_f16( |
9067 | | const ggml_compute_params * params, |
9068 | 0 | ggml_tensor * dst) { |
9069 | |
|
9070 | 0 | const ggml_tensor * q = dst->src[0]; |
9071 | 0 | const ggml_tensor * k = dst->src[1]; |
9072 | 0 | const ggml_tensor * v = dst->src[2]; |
9073 | |
|
9074 | 0 | GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
9075 | 0 | GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
9076 | 0 | GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
9077 | 0 | GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
9078 | 0 | GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
9079 | 0 | GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
9080 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
9081 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
9082 | |
|
9083 | 0 | const int64_t DK = nek0; |
9084 | 0 | const int64_t DV = nev0; |
9085 | 0 | const int64_t N = neq1; |
9086 | | |
9087 | |
|
9088 | 0 | GGML_ASSERT(ne0 == DV); |
9089 | 0 | GGML_ASSERT(ne2 == N); |
9090 | | |
9091 | | // input tensor rows must be contiguous |
9092 | 0 | GGML_ASSERT(nbq0 == ggml_type_size(q->type)); |
9093 | 0 | GGML_ASSERT(nbk0 == ggml_type_size(k->type)); |
9094 | 0 | GGML_ASSERT(nbv0 == ggml_type_size(v->type)); |
9095 | |
|
9096 | 0 | GGML_ASSERT(neq0 == DK); |
9097 | 0 | GGML_ASSERT(nek0 == DK); |
9098 | 0 | GGML_ASSERT(nev0 == DV); |
9099 | |
|
9100 | 0 | GGML_ASSERT(neq1 == N); |
9101 | | |
9102 | | // dst cannot be transposed or permuted |
9103 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
9104 | 0 | GGML_ASSERT(nb0 <= nb1); |
9105 | 0 | GGML_ASSERT(nb1 <= nb2); |
9106 | 0 | GGML_ASSERT(nb2 <= nb3); |
9107 | |
|
9108 | 0 | const int ith = params->ith; |
9109 | 0 | const int nth = params->nth; |
9110 | | |
9111 | | // When use_ref is set, force the vec-only reference implementation (no tiling, no KV-chunking) |
9112 | 0 | const bool use_ref = params->use_ref; |
9113 | |
|
9114 | 0 | const bool kv_is_f32_or_f16 = (k->type == GGML_TYPE_F32 || k->type == GGML_TYPE_F16); |
9115 | 0 | const bool use_split_kv_path = !use_ref && (neq1 == 1 && neq3 == 1) && kv_is_f32_or_f16 && (k->type == v->type) && q->type == GGML_TYPE_F32 && nek1 >= 512; |
9116 | |
|
9117 | 0 | if (use_split_kv_path) { |
9118 | 0 | const int64_t chunk_size = (nek1 + nth - 1) / nth; |
9119 | | |
9120 | | // Partials buffer layout: [q_head][kv_chunk][M, S, VKQ] |
9121 | 0 | const int64_t partial_size = 2 + DV; |
9122 | 0 | float * partials_base = (float *) params->wdata + nth * (DK + 2*DV + CACHE_LINE_SIZE_F32); |
9123 | |
|
9124 | 0 | const int64_t ic_start = ith * chunk_size; |
9125 | 0 | const int64_t ic_end = std::min(ic_start + chunk_size, nek1); |
9126 | |
|
9127 | 0 | const int64_t partial_stride = nth * partial_size; |
9128 | 0 | float * chunk_partials = partials_base + ith * partial_size; |
9129 | |
|
9130 | 0 | if (ic_start < nek1) { |
9131 | 0 | for (int64_t q_head = 0; q_head < neq2; q_head++) { |
9132 | 0 | ggml_compute_forward_flash_attn_ext_f16_one_chunk( |
9133 | 0 | params, dst, q_head, q_head + 1, ic_start, ic_end, |
9134 | 0 | chunk_partials, partial_stride); |
9135 | 0 | } |
9136 | 0 | } else { |
9137 | 0 | for (int64_t q_head = 0; q_head < neq2; q_head++) { |
9138 | 0 | float * q_partials = chunk_partials + q_head * partial_stride; |
9139 | 0 | q_partials[0] = -INFINITY; // M |
9140 | 0 | q_partials[1] = 0.0f; // S |
9141 | 0 | } |
9142 | 0 | } |
9143 | |
|
9144 | 0 | ggml_barrier(params->threadpool); |
9145 | 0 | ggml_flash_attn_ext_reduce_partials(params, dst, nth, chunk_size); |
9146 | 0 | } else { |
9147 | | |
9148 | | // total rows in q |
9149 | 0 | const int64_t nr = neq1*neq2*neq3; |
9150 | | |
9151 | | // disable for NUMA |
9152 | 0 | const bool disable_chunking = ggml_is_numa(); |
9153 | | |
9154 | | // 4x chunks per thread |
9155 | 0 | int nth_scaled = nth * 4; |
9156 | 0 | int64_t chunk_size = (nr + nth_scaled - 1) / nth_scaled; |
9157 | 0 | int64_t nchunk = (nr + chunk_size - 1) / chunk_size; |
9158 | |
|
9159 | 0 | if (nth == 1 || nchunk < nth || disable_chunking) { |
9160 | 0 | nchunk = nth; |
9161 | 0 | } |
9162 | |
|
9163 | 0 | if (ith == 0) { |
9164 | 0 | ggml_threadpool_chunk_set(params->threadpool, nth); |
9165 | 0 | } |
9166 | |
|
9167 | 0 | ggml_barrier(params->threadpool); |
9168 | |
|
9169 | 0 | const int64_t dr = (nr + nchunk - 1) / nchunk; |
9170 | |
|
9171 | 0 | static constexpr int64_t Q_TILE_SZ = ggml_fa_tile_config::Q; |
9172 | 0 | bool use_tiled = !use_ref && |
9173 | 0 | (q->type == GGML_TYPE_F32 && |
9174 | 0 | kv_is_f32_or_f16 && |
9175 | 0 | k->type == v->type && |
9176 | 0 | neq1 >= Q_TILE_SZ); |
9177 | 0 | #ifdef GGML_SIMD |
9178 | | #if defined(__ARM_FEATURE_SVE) |
9179 | | const int64_t f32_epr = svcntw(); |
9180 | | #else |
9181 | 0 | const int64_t f32_epr = GGML_F32_EPR; |
9182 | 0 | #endif |
9183 | 0 | use_tiled &= (DV % f32_epr == 0); |
9184 | 0 | #endif |
9185 | 0 | int current_chunk = ith; |
9186 | |
|
9187 | 0 | while (current_chunk < nchunk) { |
9188 | 0 | const int64_t ir0 = dr * current_chunk; |
9189 | 0 | const int64_t ir1 = MIN(ir0 + dr, nr); |
9190 | |
|
9191 | 0 | if (use_tiled) { |
9192 | 0 | ggml_compute_forward_flash_attn_ext_tiled(params, dst, ir0, ir1); |
9193 | 0 | } else { |
9194 | 0 | ggml_compute_forward_flash_attn_ext_f16_one_chunk(params, dst, ir0, ir1, 0, nek1, nullptr, 0); |
9195 | 0 | } |
9196 | |
|
9197 | 0 | current_chunk = ggml_threadpool_chunk_add(params->threadpool, 1); |
9198 | 0 | } |
9199 | 0 | } |
9200 | 0 | } |
9201 | | |
9202 | | void ggml_compute_forward_flash_attn_ext( |
9203 | | const ggml_compute_params * params, |
9204 | 0 | ggml_tensor * dst) { |
9205 | 0 | switch (dst->op_params[3]) { |
9206 | 0 | case GGML_PREC_DEFAULT: |
9207 | 0 | case GGML_PREC_F32: |
9208 | 0 | { |
9209 | | // uses F32 accumulators |
9210 | 0 | ggml_compute_forward_flash_attn_ext_f16(params, dst); |
9211 | 0 | } break; |
9212 | 0 | default: |
9213 | 0 | { |
9214 | 0 | GGML_ABORT("fatal error"); |
9215 | 0 | } |
9216 | 0 | } |
9217 | 0 | } |
9218 | | |
9219 | | // ggml_compute_forward_flash_attn_back |
9220 | | |
9221 | | static void ggml_compute_forward_flash_attn_back_f32( |
9222 | | const ggml_compute_params * params, |
9223 | | const bool masked, |
9224 | 0 | ggml_tensor * dst) { |
9225 | |
|
9226 | 0 | const ggml_tensor * q = dst->src[0]; |
9227 | 0 | const ggml_tensor * k = dst->src[1]; |
9228 | 0 | const ggml_tensor * v = dst->src[2]; |
9229 | 0 | const ggml_tensor * d = dst->src[3]; |
9230 | |
|
9231 | 0 | GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
9232 | 0 | GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
9233 | 0 | GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
9234 | 0 | GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
9235 | 0 | GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
9236 | 0 | GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
9237 | 0 | GGML_TENSOR_LOCALS(int64_t, ned, d, ne) |
9238 | 0 | GGML_TENSOR_LOCALS(size_t, nbd, d, nb) |
9239 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
9240 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
9241 | |
|
9242 | 0 | const int ith = params->ith; |
9243 | 0 | const int nth = params->nth; |
9244 | |
|
9245 | 0 | const int64_t D = neq0; |
9246 | 0 | const int64_t N = neq1; |
9247 | 0 | const int64_t P = nek1 - N; |
9248 | 0 | const int64_t M = P + N; |
9249 | |
|
9250 | 0 | const int Mup = ggml_up(M, GGML_SOFT_MAX_UNROLL); |
9251 | 0 | const int mxDM = MAX(D, Mup); |
9252 | | |
9253 | | // GGML_ASSERT(ne0 == D); |
9254 | | // GGML_ASSERT(ne1 == N); |
9255 | 0 | GGML_ASSERT(P >= 0); |
9256 | |
|
9257 | 0 | GGML_ASSERT(nbq0 == sizeof(float)); |
9258 | 0 | GGML_ASSERT(nbk0 == sizeof(float)); |
9259 | 0 | GGML_ASSERT(nbv0 == sizeof(float)); |
9260 | |
|
9261 | 0 | GGML_ASSERT(neq0 == D); |
9262 | 0 | GGML_ASSERT(nek0 == D); |
9263 | 0 | GGML_ASSERT(nev1 == D); |
9264 | 0 | GGML_ASSERT(ned0 == D); |
9265 | |
|
9266 | 0 | GGML_ASSERT(neq1 == N); |
9267 | 0 | GGML_ASSERT(nek1 == N + P); |
9268 | 0 | GGML_ASSERT(nev1 == D); |
9269 | 0 | GGML_ASSERT(ned1 == N); |
9270 | | |
9271 | | // dst cannot be transposed or permuted |
9272 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
9273 | 0 | GGML_ASSERT(nb0 <= nb1); |
9274 | 0 | GGML_ASSERT(nb1 <= nb2); |
9275 | 0 | GGML_ASSERT(nb2 <= nb3); |
9276 | |
|
9277 | 0 | if (ith == 0) { |
9278 | 0 | memset(dst->data, 0, nb0*ne0*ne1*ne2*ne3); |
9279 | 0 | } |
9280 | 0 | ggml_barrier(params->threadpool); |
9281 | |
|
9282 | 0 | const int64_t elem_q = ggml_nelements(q); |
9283 | 0 | const int64_t elem_k = ggml_nelements(k); |
9284 | |
|
9285 | 0 | ggml_type result_type = dst->type; |
9286 | 0 | GGML_ASSERT(ggml_blck_size(result_type) == 1); |
9287 | 0 | const size_t tsize = ggml_type_size(result_type); |
9288 | |
|
9289 | 0 | const size_t offs_q = 0; |
9290 | 0 | const size_t offs_k = offs_q + GGML_PAD(elem_q * tsize, GGML_MEM_ALIGN); |
9291 | 0 | const size_t offs_v = offs_k + GGML_PAD(elem_k * tsize, GGML_MEM_ALIGN); |
9292 | |
|
9293 | 0 | void * grad_q = (char *) dst->data; |
9294 | 0 | void * grad_k = (char *) dst->data + offs_k; |
9295 | 0 | void * grad_v = (char *) dst->data + offs_v; |
9296 | |
|
9297 | 0 | const size_t nbgq1 = nb0*neq0; |
9298 | 0 | const size_t nbgq2 = nb0*neq0*neq1; |
9299 | 0 | const size_t nbgq3 = nb0*neq0*neq1*neq2; |
9300 | |
|
9301 | 0 | const size_t nbgk1 = nb0*nek0; |
9302 | 0 | const size_t nbgk2 = nb0*nek0*nek1; |
9303 | 0 | const size_t nbgk3 = nb0*nek0*nek1*neq2; |
9304 | |
|
9305 | 0 | const size_t nbgv1 = nb0*nev0; |
9306 | 0 | const size_t nbgv2 = nb0*nev0*nev1; |
9307 | 0 | const size_t nbgv3 = nb0*nev0*nev1*neq2; |
9308 | | |
9309 | | // parallelize by k rows using ggml_vec_dot_f32 |
9310 | | |
9311 | | // total rows in k |
9312 | 0 | const int nr = nek2*nek3; |
9313 | | |
9314 | | // rows per thread |
9315 | 0 | const int dr = (nr + nth - 1)/nth; |
9316 | | |
9317 | | // row range for this thread |
9318 | 0 | const int ir0 = dr*ith; |
9319 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
9320 | |
|
9321 | 0 | const float scale = 1.0f/sqrtf(D); |
9322 | | |
9323 | | //printf("P=%d N=%d D=%d ir0=%d ir1=%d scale = %f\n", P, N, D, ir0, ir1, scale); |
9324 | | |
9325 | | // how often k2 (and v2) is repeated in q2 |
9326 | 0 | int nrep = neq2/nek2; |
9327 | |
|
9328 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
9329 | | // q indices |
9330 | 0 | const int ik3 = ir/(nek2); |
9331 | 0 | const int ik2 = ir - ik3*nek2; |
9332 | |
|
9333 | 0 | const int iq3 = ik3; |
9334 | 0 | const int id3 = ik3; |
9335 | 0 | const int iv3 = ik3; |
9336 | 0 | const int iv2 = ik2; |
9337 | |
|
9338 | 0 | for (int irep = 0; irep < nrep; ++irep) { |
9339 | 0 | const int iq2 = ik2 + irep*nek2; |
9340 | 0 | const int id2 = iq2; |
9341 | | |
9342 | | // (ik2 + irep*nek2) % nek2 == ik2 |
9343 | 0 | for (int iq1 = 0; iq1 < neq1; ++iq1) { |
9344 | 0 | const int id1 = iq1; |
9345 | | |
9346 | | // not sure about CACHE_LINE_SIZE_F32.. |
9347 | | // - maybe it must not be multiplied by 2 and excluded from .. in SM 1*(..) offset? |
9348 | 0 | float * S = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 0*(mxDM+CACHE_LINE_SIZE_F32); |
9349 | 0 | float * SM = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 1*(mxDM+CACHE_LINE_SIZE_F32); |
9350 | |
|
9351 | 0 | for (int i = M; i < Mup; ++i) { |
9352 | 0 | S[i] = -INFINITY; |
9353 | 0 | } |
9354 | |
|
9355 | 0 | const int64_t masked_begin = masked ? (P + iq1 + 1) : M; |
9356 | 0 | for (int64_t ic = 0; ic < masked_begin; ++ic) { |
9357 | | // k indices |
9358 | 0 | const int ik1 = ic; |
9359 | | |
9360 | | // S indices |
9361 | 0 | const int i1 = ik1; |
9362 | |
|
9363 | 0 | ggml_vec_dot_f32(neq0, |
9364 | 0 | S + i1, 0, |
9365 | 0 | (float *) ((char *) k->data + (ik1*nbk1 + ik2*nbk2 + ik3*nbk3)), 0, |
9366 | 0 | (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)), 0, 1); |
9367 | 0 | } |
9368 | | |
9369 | | // scale |
9370 | 0 | ggml_vec_scale_f32(masked_begin, S, scale); |
9371 | |
|
9372 | 0 | for (int64_t i = masked_begin; i < M; i++) { |
9373 | 0 | S[i] = -INFINITY; |
9374 | 0 | } |
9375 | | |
9376 | | // softmax |
9377 | | // exclude known -INF S[..] values from max and loop |
9378 | | // dont forget to set their SM values to zero |
9379 | 0 | { |
9380 | 0 | float max = -INFINITY; |
9381 | 0 | ggml_vec_max_f32(masked_begin, &max, S); |
9382 | |
|
9383 | 0 | ggml_float sum = 0.0; |
9384 | 0 | { |
9385 | | #ifdef GGML_SOFT_MAX_ACCELERATE |
9386 | | max = -max; |
9387 | | vDSP_vsadd(SM, 1, &max, SM, 1, Mup); |
9388 | | vvexpf(SM, SM, &Mup); |
9389 | | ggml_vec_sum_f32(Mup, &sum, SM); |
9390 | | #else |
9391 | 0 | sum = ggml_vec_soft_max_f32(Mup, SM, S, max); |
9392 | 0 | #endif |
9393 | 0 | } |
9394 | |
|
9395 | 0 | assert(sum > 0.0); |
9396 | |
|
9397 | 0 | sum = 1.0/sum; |
9398 | 0 | ggml_vec_scale_f32(masked_begin, SM, sum); |
9399 | |
|
9400 | 0 | } |
9401 | | |
9402 | | // step-by-step explanation |
9403 | 0 | { |
9404 | | // forward-process shape grads from backward process |
9405 | | // parallel_for ik2,ik3: |
9406 | | // for irep: |
9407 | | // iq2 = ik2 + irep*nek2 |
9408 | | // k[:D,:M,:,:] [D,M,:,:] grad[k][:D,:M,ik2,ik3] += grad[kcur] |
9409 | | // q[:D,:N,:,:] [D,N,:,:] grad[q][:D,iq1,iq2,iq3] += grad[qcur] |
9410 | | // v[:M,:D,:,:] [M,D,:,:] grad[v][:M,:D,iv2,iv3] += grad[vcur] |
9411 | | // for iq1: |
9412 | | // kcur = k[:D,:M,ik2,ik3] [D,M,1,1] grad[kcur] = grad[S1].T @ qcur |
9413 | | // qcur = q[:D,iq1,iq2,iq3] [D,1,1,1] grad[qcur] = grad[S1] @ kcur |
9414 | | // vcur = v[:M,:D,iv2,iv3] [M,D,1,1] grad[vcur] = grad[S5].T @ S4 |
9415 | | // S0 = -Inf [D,1,1,1] |
9416 | | // ~S1[i] = dot(kcur[:D,i], qcur) |
9417 | | // S1 = qcur @ kcur.T [M,1,1,1] grad[S1] = grad[S2] * scale |
9418 | | // S2 = S1 * scale [M,1,1,1] grad[S2] = diag_mask_zero(grad[S3], P) |
9419 | | // S3 = diag_mask_inf(S2, P) [M,1,1,1] grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) |
9420 | | // S4 = softmax(S3) [M,1,1,1] grad[S4] = grad[S5] @ vcur |
9421 | | // ~S5[i] = dot(vcur[:,i], S4) |
9422 | | // S5 = S4 @ vcur.T [D,1,1,1] grad[S5] = d[:D,id1,id2,id3] |
9423 | | // ~dst[i,iq1,iq2,iq3] = S5[i] ^ |
9424 | | // dst[:D,iq1,iq2,iq3] = S5 | grad[dst[:D,iq1,iq2,iq3]] = d[:D,id1,id2,id3] |
9425 | | // dst backward-/ grad[dst] = d |
9426 | | // |
9427 | | // output gradients with their dependencies: |
9428 | | // |
9429 | | // grad[kcur] = grad[S1].T @ qcur |
9430 | | // grad[S1] = diag_mask_zero(grad[S3], P) * scale |
9431 | | // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) |
9432 | | // grad[S4] = grad[S5] @ vcur |
9433 | | // grad[S4] = d[:D,id1,id2,id3] @ vcur |
9434 | | // grad[qcur] = grad[S1] @ kcur |
9435 | | // grad[vcur] = grad[S5].T @ S4 |
9436 | | // grad[vcur] = d[:D,id1,id2,id3].T @ S4 |
9437 | | // |
9438 | | // in post-order: |
9439 | | // |
9440 | | // S1 = qcur @ kcur.T |
9441 | | // S2 = S1 * scale |
9442 | | // S3 = diag_mask_inf(S2, P) |
9443 | | // S4 = softmax(S3) |
9444 | | // grad[S4] = d[:D,id1,id2,id3] @ vcur |
9445 | | // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) |
9446 | | // grad[S1] = diag_mask_zero(grad[S3], P) * scale |
9447 | | // grad[qcur] = grad[S1] @ kcur |
9448 | | // grad[kcur] = grad[S1].T @ qcur |
9449 | | // grad[vcur] = d[:D,id1,id2,id3].T @ S4 |
9450 | | // |
9451 | | // using less variables (SM=S4): |
9452 | | // |
9453 | | // S = diag_mask_inf(qcur @ kcur.T * scale, P) |
9454 | | // SM = softmax(S) |
9455 | | // S = d[:D,iq1,iq2,iq3] @ vcur |
9456 | | // dot_SM_gradSM = dot(SM, S) |
9457 | | // S = SM * (S - dot(SM, S)) |
9458 | | // S = diag_mask_zero(S, P) * scale |
9459 | | // |
9460 | | // grad[q][:D,iq1,iq2,iq3] += S @ kcur |
9461 | | // grad[k][:D,:M,ik2,ik3] += S.T @ qcur |
9462 | | // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM |
9463 | 0 | } |
9464 | | |
9465 | | // S = gradSM = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3] |
9466 | | // S = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3] |
9467 | | // for ic: |
9468 | | // S[:M] += vcur[:M,ic,iv2,iv3] * d[ic,id1,id2,id3] |
9469 | | // exclude known future zero S[..] values from operation |
9470 | 0 | ggml_vec_set_f32(masked_begin, S, 0); |
9471 | 0 | for (int64_t ic = 0; ic < D; ++ic) { |
9472 | 0 | ggml_vec_mad_f32(masked_begin, |
9473 | 0 | S, |
9474 | 0 | (float *) ((char *) v->data + ( ic*nbv1 + iv2*nbv2 + iv3*nbv3)), |
9475 | 0 | *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3))); |
9476 | 0 | } |
9477 | | |
9478 | | // S = SM * (S - dot(SM, S)) |
9479 | 0 | float dot_SM_gradSM = 0; |
9480 | 0 | ggml_vec_dot_f32 (masked_begin, &dot_SM_gradSM, 0, SM, 0, S, 0, 1); |
9481 | 0 | ggml_vec_acc1_f32(M, S, -dot_SM_gradSM); |
9482 | 0 | ggml_vec_mul_f32 (masked_begin, S, S, SM); |
9483 | | |
9484 | | // S = diag_mask_zero(S, P) * scale |
9485 | | // already done by above ggml_vec_set_f32 |
9486 | | |
9487 | | // exclude known zero S[..] values from operation |
9488 | 0 | ggml_vec_scale_f32(masked_begin, S, scale); |
9489 | | |
9490 | | // S shape [M,1] |
9491 | | // SM shape [M,1] |
9492 | | // kcur shape [D,M] |
9493 | | // qcur shape [D,1] |
9494 | | // vcur shape [M,D] |
9495 | | |
9496 | | // grad[q][:D,iq1,iq2,iq3] += S @ kcur |
9497 | | // grad[q][:D,iq1,iq2,iq3] += shape[M,1] @ shape[D,M] |
9498 | | // for ic: |
9499 | | // grad[q][:D,iq1,iq2,iq3] += S[ic] * kcur[:D,ic,ik2,ik3] |
9500 | | // exclude known zero S[..] values from loop |
9501 | 0 | for (int64_t ic = 0; ic < masked_begin; ++ic) { |
9502 | 0 | ggml_vec_mad_f32(D, |
9503 | 0 | (float *) ((char *) grad_q + (iq1*nbgq1 + iq2*nbgq2 + iq3*nbgq3)), |
9504 | 0 | (float *) ((char *) k->data + (ic*nbk1 + ik2*nbk2 + ik3*nbk3)), |
9505 | 0 | S[ic]); |
9506 | 0 | } |
9507 | | |
9508 | | // grad[k][:D,:M,iq2,iq3] += S.T @ qcur |
9509 | | // for ic: |
9510 | | // grad[k][:D,ic,iq2,iq3] += S.T[0,ic] * qcur[:D,0] |
9511 | | // grad[k][:D,ic,iq2,iq3] += S[ic] * qcur[:D,0] |
9512 | | // exclude known zero S[..] values from loop |
9513 | 0 | for (int64_t ic = 0; ic < masked_begin; ++ic) { |
9514 | 0 | ggml_vec_mad_f32(D, |
9515 | 0 | (float *) ((char *) grad_k + (ic*nbgk1 + ik2*nbgk2 + ik3*nbgk3)), |
9516 | 0 | (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)), |
9517 | 0 | S[ic]); |
9518 | 0 | } |
9519 | | |
9520 | | // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM |
9521 | | // for ic: |
9522 | | // grad[v][:M,ic,iv2,iv3] += d[:D,id1,id2,id3].T[0,ic] * SM[:M] |
9523 | | // grad[v][:M,ic,iv2,iv3] += d[ic,id1,id2,id3] * SM[:M] |
9524 | | // exclude known zero SM[..] values from mad |
9525 | 0 | for (int64_t ic = 0; ic < D; ++ic) { |
9526 | 0 | ggml_vec_mad_f32(masked_begin, |
9527 | 0 | (float *) ((char *) grad_v + ( ic*nbgv1 + iv2*nbgv2 + iv3*nbgv3)), |
9528 | 0 | SM, |
9529 | 0 | *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3))); |
9530 | 0 | } |
9531 | 0 | } |
9532 | 0 | } |
9533 | 0 | } |
9534 | 0 | } |
9535 | | |
9536 | | void ggml_compute_forward_flash_attn_back( |
9537 | | const ggml_compute_params * params, |
9538 | | const bool masked, |
9539 | 0 | ggml_tensor * dst) { |
9540 | |
|
9541 | 0 | const ggml_tensor * q = dst->src[0]; |
9542 | |
|
9543 | 0 | switch (q->type) { |
9544 | 0 | case GGML_TYPE_F32: |
9545 | 0 | { |
9546 | 0 | ggml_compute_forward_flash_attn_back_f32(params, masked, dst); |
9547 | 0 | } break; |
9548 | 0 | default: |
9549 | 0 | { |
9550 | 0 | GGML_ABORT("fatal error"); |
9551 | 0 | } |
9552 | 0 | } |
9553 | 0 | } |
9554 | | |
9555 | | // ggml_compute_forward_ssm_conv |
9556 | | |
9557 | | static void ggml_compute_forward_ssm_conv_f32( |
9558 | | const ggml_compute_params * params, |
9559 | 0 | ggml_tensor * dst) { |
9560 | 0 | const ggml_tensor * src0 = dst->src[0]; // conv_x |
9561 | 0 | const ggml_tensor * src1 = dst->src[1]; // conv1d.weight |
9562 | |
|
9563 | 0 | const int ith = params->ith; |
9564 | 0 | const int nth = params->nth; |
9565 | |
|
9566 | 0 | const int nc = src1->ne[0]; // d_conv |
9567 | 0 | const int ncs = src0->ne[0]; // d_conv - 1 + n_t |
9568 | 0 | const int nr = src0->ne[1]; // d_inner |
9569 | 0 | const int n_t = dst->ne[1]; // tokens per sequence |
9570 | 0 | const int n_s = dst->ne[2]; // number of sequences in the batch |
9571 | |
|
9572 | 0 | GGML_ASSERT( dst->ne[0] == nr); |
9573 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
9574 | 0 | GGML_ASSERT(src1->nb[0] == sizeof(float)); |
9575 | 0 | GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float)); |
9576 | | |
9577 | | // rows per thread |
9578 | 0 | const int dr = (nr + nth - 1)/nth; |
9579 | | |
9580 | | // row range for this thread |
9581 | 0 | const int ir0 = dr*ith; |
9582 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
9583 | 0 | const int ir = ir1 - ir0; |
9584 | |
|
9585 | 0 | for (int i3 = 0; i3 < n_s; ++i3) { |
9586 | 0 | for (int i2 = 0; i2 < n_t; ++i2) { |
9587 | | // {d_conv - 1 + n_t, d_inner, n_seqs} |
9588 | | // sliding window |
9589 | 0 | const float * s = (const float *) ((const char *) src0->data + ir0*(src0->nb[1]) + i2*(src0->nb[0]) + i3*(src0->nb[2])); // {d_conv, d_inner, n_s} |
9590 | 0 | const float * c = (const float *) ((const char *) src1->data + ir0*(src1->nb[1])); // {d_conv, d_inner} |
9591 | 0 | float * x = (float *) ((char *) dst->data + ir0*(dst->nb[0]) + i2*(dst->nb[1]) + i3*(dst->nb[2])); // {d_inner, n_t, n_s} |
9592 | | |
9593 | | // TODO: transpose the output for smaller strides for big batches? |
9594 | | // d_inner |
9595 | 0 | for (int i1 = 0; i1 < ir; ++i1) { |
9596 | | // rowwise dot product |
9597 | | // NOTE: not using ggml_vec_dot_f32, because its sum is in double precision |
9598 | 0 | float sumf = 0.0f; |
9599 | | |
9600 | | // d_conv |
9601 | 0 | for (int i0 = 0; i0 < nc; ++i0) { |
9602 | 0 | sumf += s[i0 + i1*ncs] * c[i0 + i1*nc]; |
9603 | 0 | } |
9604 | 0 | x[i1] = sumf; |
9605 | 0 | } |
9606 | 0 | } |
9607 | 0 | } |
9608 | 0 | } |
9609 | | |
9610 | | void ggml_compute_forward_ssm_conv( |
9611 | | const ggml_compute_params * params, |
9612 | 0 | ggml_tensor * dst) { |
9613 | 0 | switch (dst->src[0]->type) { |
9614 | 0 | case GGML_TYPE_F32: |
9615 | 0 | { |
9616 | 0 | ggml_compute_forward_ssm_conv_f32(params, dst); |
9617 | 0 | } break; |
9618 | 0 | default: |
9619 | 0 | { |
9620 | 0 | GGML_ABORT("fatal error"); |
9621 | 0 | } |
9622 | 0 | } |
9623 | 0 | } |
9624 | | |
9625 | | // ggml_compute_forward_ssm_scan |
9626 | | |
9627 | | static void ggml_compute_forward_ssm_scan_f32( |
9628 | | const ggml_compute_params * params, |
9629 | 0 | ggml_tensor * dst) { |
9630 | 0 | const ggml_tensor * src0 = dst->src[0]; // s {d_state, dim, n_head, n_seqs+} |
9631 | 0 | const ggml_tensor * src1 = dst->src[1]; // x {dim, n_head, n_seq_tokens, n_seqs} |
9632 | 0 | const ggml_tensor * src2 = dst->src[2]; // dt {n_head, n_seq_tokens, n_seqs} |
9633 | 0 | const ggml_tensor * src3 = dst->src[3]; // A {d_state, n_head} or {1, n_head} |
9634 | 0 | const ggml_tensor * src4 = dst->src[4]; // B {d_state, n_group, n_seq_tokens, n_seqs} |
9635 | 0 | const ggml_tensor * src5 = dst->src[5]; // C {d_state, n_group, n_seq_tokens, n_seqs} |
9636 | 0 | const ggml_tensor * src6 = dst->src[6]; // ids {n_seqs} |
9637 | |
|
9638 | 0 | const int ith = params->ith; |
9639 | 0 | const int nth = params->nth; |
9640 | |
|
9641 | 0 | const int64_t nc = src0->ne[0]; // d_state |
9642 | 0 | const int64_t nr = src0->ne[1]; // dim |
9643 | 0 | const int64_t nh = src1->ne[1]; // n_head |
9644 | 0 | const int64_t ng = src4->ne[1]; |
9645 | 0 | const int64_t nt = src1->ne[2]; // number of tokens per sequence |
9646 | 0 | const int64_t ns = src1->ne[3]; // number of sequences in the batch |
9647 | | |
9648 | | // can't use ggml_nbytes because src1 is not necessarily contiguous |
9649 | 0 | const int64_t s_off = ggml_nelements(src1) * ggml_element_size(src1); |
9650 | |
|
9651 | 0 | GGML_ASSERT(ggml_nelements(src1) + nc*nr*nh*ns == ggml_nelements(dst)); |
9652 | 0 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
9653 | 0 | GGML_ASSERT(src1->nb[0] == sizeof(float)); |
9654 | 0 | GGML_ASSERT(src2->nb[0] == sizeof(float)); |
9655 | 0 | GGML_ASSERT(src3->nb[0] == sizeof(float)); |
9656 | 0 | GGML_ASSERT(src4->nb[0] == sizeof(float)); |
9657 | 0 | GGML_ASSERT(src5->nb[0] == sizeof(float)); |
9658 | 0 | GGML_ASSERT(src6->nb[0] == sizeof(int32_t)); |
9659 | 0 | GGML_ASSERT(nh % ng == 0); |
9660 | | |
9661 | | // heads per thread |
9662 | 0 | const int dh = (nh + nth - 1)/nth; |
9663 | | |
9664 | | // head range for this thread |
9665 | 0 | const int ih0 = dh*ith; |
9666 | 0 | const int ih1 = MIN(ih0 + dh, nh); |
9667 | |
|
9668 | 0 | const int32_t * ids = (const int32_t *) src6->data; |
9669 | |
|
9670 | 0 | for (int i3 = 0; i3 < ns; ++i3) { |
9671 | 0 | const float * s0 = (const float *) ((const char *) src0->data + ids[i3]*(src0->nb[3])); // {d_state, dim, nh, ns} |
9672 | 0 | float * s = ( float *) (( char *) dst->data + i3*(src0->nb[3]) + s_off); // {d_state, dim, nh, ns} |
9673 | |
|
9674 | 0 | for (int i2 = 0; i2 < nt; ++i2) { |
9675 | 0 | const float * x = (const float *) ((const char *) src1->data + i2*(src1->nb[2]) + i3*(src1->nb[3])); // {dim, nh, nt, ns} |
9676 | 0 | const float * dt = (const float *) ((const char *) src2->data + i2*(src2->nb[1]) + i3*(src2->nb[2])); // {nh, nt, ns} |
9677 | 0 | const float * A = (const float *) ((const char *) src3->data); // {d_state, nh} or {1, nh} |
9678 | 0 | const float * B = (const float *) ((const char *) src4->data + i2*(src4->nb[2]) + i3*(src4->nb[3])); // {d_state, ng, nt, ns} |
9679 | 0 | const float * C = (const float *) ((const char *) src5->data + i2*(src5->nb[2]) + i3*(src5->nb[3])); // {d_state, ng, nt, ns} |
9680 | 0 | float * y = ( float *) (( char *) dst->data + i2*(nh*nr*sizeof(float)) + i3*(nt*nh*nr*sizeof(float))); // {dim, nh, nt, ns} |
9681 | |
|
9682 | 0 | if (src3->ne[0] == 1) { |
9683 | | // Mamba-2 has a scalar decay factor per head; dA can be outside the state-wise loop |
9684 | | |
9685 | | // n_head |
9686 | 0 | for (int h = ih0; h < ih1; ++h) { |
9687 | | // ref: https://github.com/state-spaces/mamba/blob/62db608da60f6fc790b8ed9f4b3225e95ca15fde/mamba_ssm/ops/triton/softplus.py#L16 |
9688 | 0 | const float dt_soft_plus = ggml_compute_softplus_f32(dt[h]); |
9689 | 0 | const float dA = expf(dt_soft_plus * A[h]); |
9690 | 0 | const int g = h / (nh / ng); // repeat_interleave |
9691 | | |
9692 | | // dim |
9693 | 0 | for (int i1 = 0; i1 < nr; ++i1) { |
9694 | 0 | const int ii = i1 + h*nr; |
9695 | 0 | const float x_dt = x[ii] * dt_soft_plus; |
9696 | 0 | float sumf = 0.0f; |
9697 | 0 | #if defined(GGML_SIMD) |
9698 | | #if defined(__ARM_FEATURE_SVE) |
9699 | | const int ggml_f32_epr = svcntw(); |
9700 | | const int ggml_f32_step = 1 * ggml_f32_epr; |
9701 | | |
9702 | | const int np = (nc & ~(ggml_f32_step - 1)); |
9703 | | |
9704 | | GGML_F32_VEC sum = GGML_F32_VEC_ZERO; |
9705 | | |
9706 | | GGML_F32_VEC adA = GGML_F32_VEC_SET1(dA); |
9707 | | GGML_F32_VEC axdt = GGML_F32_VEC_SET1(x_dt); |
9708 | | |
9709 | | for (int i = 0; i < np; i += ggml_f32_step) { |
9710 | | // TODO: maybe unroll more? |
9711 | | for (int j = 0; j < 1; j++) { |
9712 | | GGML_F32_VEC t0 = GGML_F32_VEC_LOAD(s0 + i + j*ggml_f32_epr + ii*nc); |
9713 | | GGML_F32_VEC t1 = GGML_F32_VEC_LOAD(B + i + j*ggml_f32_epr + g*nc); |
9714 | | GGML_F32_VEC t2 = GGML_F32_VEC_LOAD(C + i + j*ggml_f32_epr + g*nc); |
9715 | | |
9716 | | t0 = GGML_F32_VEC_MUL(t0, adA); |
9717 | | t1 = GGML_F32_VEC_MUL(t1, axdt); |
9718 | | |
9719 | | t0 = GGML_F32_VEC_ADD(t0, t1); |
9720 | | |
9721 | | sum = GGML_F32_VEC_FMA(sum, t0, t2); |
9722 | | |
9723 | | GGML_F32_VEC_STORE(s + i + j*ggml_f32_epr + ii*nc, t0); |
9724 | | } |
9725 | | } |
9726 | | |
9727 | | sumf = GGML_F32xt_REDUCE_ONE(sum); |
9728 | | #elif defined(__riscv_v_intrinsic) |
9729 | | // todo: RVV implementation |
9730 | | const int np = 0; |
9731 | | #else |
9732 | 0 | const int np = (nc & ~(GGML_F32_STEP - 1)); |
9733 | |
|
9734 | 0 | GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO }; |
9735 | |
|
9736 | 0 | GGML_F32_VEC adA = GGML_F32_VEC_SET1(dA); |
9737 | 0 | GGML_F32_VEC axdt = GGML_F32_VEC_SET1(x_dt); |
9738 | |
|
9739 | 0 | GGML_F32_VEC ax[GGML_F32_ARR]; |
9740 | 0 | GGML_F32_VEC ay[GGML_F32_ARR]; |
9741 | 0 | GGML_F32_VEC az[GGML_F32_ARR]; |
9742 | |
|
9743 | 0 | for (int i = 0; i < np; i += GGML_F32_STEP) { |
9744 | 0 | for (int j = 0; j < GGML_F32_ARR; j++) { |
9745 | 0 | ax[j] = GGML_F32_VEC_LOAD(s0 + i + j*GGML_F32_EPR + ii*nc); |
9746 | 0 | ay[j] = GGML_F32_VEC_LOAD(B + i + j*GGML_F32_EPR + g*nc); |
9747 | 0 | az[j] = GGML_F32_VEC_LOAD(C + i + j*GGML_F32_EPR + g*nc); |
9748 | |
|
9749 | 0 | ax[j] = GGML_F32_VEC_MUL(ax[j], adA); |
9750 | 0 | ay[j] = GGML_F32_VEC_MUL(ay[j], axdt); |
9751 | |
|
9752 | 0 | ax[j] = GGML_F32_VEC_ADD(ax[j], ay[j]); |
9753 | |
|
9754 | 0 | sum[j] = GGML_F32_VEC_FMA(sum[j], ax[j], az[j]); |
9755 | |
|
9756 | 0 | GGML_F32_VEC_STORE(s + i + j*GGML_F32_EPR + ii*nc, ax[j]); |
9757 | 0 | } |
9758 | 0 | } |
9759 | | |
9760 | | // reduce sum0..sum3 to sum0 |
9761 | 0 | GGML_F32_VEC_REDUCE(sumf, sum); |
9762 | 0 | #endif |
9763 | | #else |
9764 | | const int np = 0; |
9765 | | #endif |
9766 | | // d_state |
9767 | 0 | for (int i0 = np; i0 < nc; ++i0) { |
9768 | 0 | const int i = i0 + ii*nc; |
9769 | 0 | const int ig = i0 + g*nc; |
9770 | | // state = prev_state * dA + dB * x |
9771 | 0 | const float state = (s0[i] * dA) + (B[ig] * x_dt); |
9772 | | // y = rowwise_dotprod(state, C) |
9773 | 0 | sumf += state * C[ig]; |
9774 | 0 | s[i] = state; |
9775 | 0 | } |
9776 | 0 | y[ii] = sumf; |
9777 | 0 | } |
9778 | 0 | } |
9779 | 0 | } else { |
9780 | | // Mamba-1 has an element-wise decay factor for the states |
9781 | | |
9782 | | // n_head |
9783 | 0 | for (int h = ih0; h < ih1; ++h) { |
9784 | | // ref: https://github.com/state-spaces/mamba/blob/62db608da60f6fc790b8ed9f4b3225e95ca15fde/mamba_ssm/ops/triton/softplus.py#L16 |
9785 | 0 | const float dt_soft_plus = ggml_compute_softplus_f32(dt[h]); |
9786 | 0 | const int g = h / (nh / ng); // repeat_interleave |
9787 | | |
9788 | | // dim |
9789 | 0 | for (int i1 = 0; i1 < nr; ++i1) { |
9790 | 0 | const int ii = i1 + h*nr; |
9791 | 0 | const float x_dt = x[ii] * dt_soft_plus; |
9792 | | #if defined(__ARM_FEATURE_SVE) |
9793 | | svfloat32_t vx_dt = GGML_F32_VEC_SET1(x_dt); |
9794 | | svfloat32_t vdt_soft_plus = GGML_F32_VEC_SET1(dt_soft_plus); |
9795 | | svfloat32_t r1_vector = GGML_F32_VEC_ZERO; |
9796 | | |
9797 | | // d_state |
9798 | | // TODO: what happens when (d_state % svcntw()) != 0? |
9799 | | for (int64_t k = 0; k < nc; k += svcntw()) { |
9800 | | svfloat32_t vA = GGML_F32_VEC_LOAD(&A[h*nc + k]); |
9801 | | svfloat32_t vB = GGML_F32_VEC_LOAD(&B[k + g*nc]); |
9802 | | svfloat32_t vC = GGML_F32_VEC_LOAD(&C[k + g*nc]); |
9803 | | svfloat32_t vs0 = GGML_F32_VEC_LOAD(&s0[ii*nc + k]); |
9804 | | |
9805 | | svfloat32_t t1 = GGML_F32_VEC_MUL(vdt_soft_plus, vA); |
9806 | | t1 = exp_ps_sve(svptrue_b32(), t1); |
9807 | | svfloat32_t t2 = GGML_F32_VEC_MUL(vx_dt, vB); |
9808 | | |
9809 | | vs0 = GGML_F32_VEC_FMA(t2, vs0, t1); |
9810 | | r1_vector = GGML_F32_VEC_ADD(GGML_F32_VEC_MUL(vs0, vC), r1_vector); |
9811 | | |
9812 | | GGML_F32_VEC_STORE(&s[ii*nc + k], vs0); |
9813 | | } |
9814 | | y[ii] = GGML_F32xt_REDUCE_ONE(r1_vector); |
9815 | | #else |
9816 | 0 | float sumf = 0.0f; |
9817 | | // NOTE: can't really use GGML_SIMD here because d_state is usually 16 |
9818 | | // and also because expf is used within the loop. |
9819 | | // d_state |
9820 | 0 | for (int i0 = 0; i0 < nc; ++i0) { |
9821 | 0 | const int i = i0 + ii*nc; |
9822 | 0 | const int ig = i0 + g*nc; |
9823 | | // state = prev_state * dA + dB * x |
9824 | 0 | const float state = (s0[i] * expf(dt_soft_plus * A[i0 + h*nc])) + (B[ig] * x_dt); |
9825 | | // y = rowwise_dotprod(state, C) |
9826 | 0 | sumf += state * C[ig]; |
9827 | 0 | s[i] = state; |
9828 | 0 | } |
9829 | 0 | y[ii] = sumf; |
9830 | 0 | #endif |
9831 | 0 | } |
9832 | 0 | } |
9833 | 0 | } |
9834 | | // use the output as the source when it's not the first token-wise iteration |
9835 | 0 | s0 = s; |
9836 | 0 | } |
9837 | 0 | } |
9838 | 0 | } |
9839 | | |
9840 | | void ggml_compute_forward_ssm_scan( |
9841 | | const ggml_compute_params * params, |
9842 | 0 | ggml_tensor * dst) { |
9843 | 0 | switch (dst->src[0]->type) { |
9844 | 0 | case GGML_TYPE_F32: |
9845 | 0 | { |
9846 | 0 | ggml_compute_forward_ssm_scan_f32(params, dst); |
9847 | 0 | } break; |
9848 | 0 | default: |
9849 | 0 | { |
9850 | 0 | GGML_ABORT("fatal error"); |
9851 | 0 | } |
9852 | 0 | } |
9853 | 0 | } |
9854 | | |
9855 | | // ggml_compute_forward_win_part |
9856 | | |
9857 | | static void ggml_compute_forward_win_part_f32( |
9858 | | const ggml_compute_params * params, |
9859 | 0 | ggml_tensor * dst) { |
9860 | 0 | GGML_UNUSED(params); |
9861 | |
|
9862 | 0 | const ggml_tensor * src0 = dst->src[0]; |
9863 | |
|
9864 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
9865 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
9866 | |
|
9867 | 0 | const int32_t nep0 = ((const int32_t *)(dst->op_params))[0]; |
9868 | 0 | const int32_t nep1 = ((const int32_t *)(dst->op_params))[1]; |
9869 | 0 | const int32_t w = ((const int32_t *)(dst->op_params))[2]; |
9870 | |
|
9871 | 0 | assert(ne00 == ne0); |
9872 | 0 | assert(ne3 == nep0*nep1); |
9873 | | |
9874 | | // TODO: optimize / multi-thread |
9875 | 0 | for (int py = 0; py < nep1; ++py) { |
9876 | 0 | for (int px = 0; px < nep0; ++px) { |
9877 | 0 | const int64_t i3 = py*nep0 + px; |
9878 | 0 | for (int64_t i2 = 0; i2 < ne2; ++i2) { |
9879 | 0 | for (int64_t i1 = 0; i1 < ne1; ++i1) { |
9880 | 0 | for (int64_t i0 = 0; i0 < ne0; ++i0) { |
9881 | 0 | const int64_t i02 = py*w + i2; |
9882 | 0 | const int64_t i01 = px*w + i1; |
9883 | 0 | const int64_t i00 = i0; |
9884 | |
|
9885 | 0 | const int64_t i = i3*ne2*ne1*ne0 + i2*ne1*ne0 + i1*ne0 + i0; |
9886 | 0 | const int64_t j = i02*ne01*ne00 + i01*ne00 + i00; |
9887 | |
|
9888 | 0 | if (py*w + i2 >= ne02 || px*w + i1 >= ne01) { |
9889 | 0 | ((float *) dst->data)[i] = 0.0f; |
9890 | 0 | } else { |
9891 | 0 | ((float *) dst->data)[i] = ((float *) src0->data)[j]; |
9892 | 0 | } |
9893 | 0 | } |
9894 | 0 | } |
9895 | 0 | } |
9896 | 0 | } |
9897 | 0 | } |
9898 | 0 | } |
9899 | | |
9900 | | void ggml_compute_forward_win_part( |
9901 | | const ggml_compute_params * params, |
9902 | 0 | ggml_tensor * dst) { |
9903 | |
|
9904 | 0 | const ggml_tensor * src0 = dst->src[0]; |
9905 | |
|
9906 | 0 | switch (src0->type) { |
9907 | 0 | case GGML_TYPE_F32: |
9908 | 0 | { |
9909 | 0 | ggml_compute_forward_win_part_f32(params, dst); |
9910 | 0 | } break; |
9911 | 0 | default: |
9912 | 0 | { |
9913 | 0 | GGML_ABORT("fatal error"); |
9914 | 0 | } |
9915 | 0 | } |
9916 | 0 | } |
9917 | | |
9918 | | // ggml_compute_forward_win_unpart |
9919 | | |
9920 | | static void ggml_compute_forward_win_unpart_f32( |
9921 | | const ggml_compute_params * params, |
9922 | 0 | ggml_tensor * dst) { |
9923 | 0 | GGML_UNUSED(params); |
9924 | |
|
9925 | 0 | const ggml_tensor * src0 = dst->src[0]; |
9926 | |
|
9927 | 0 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
9928 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
9929 | |
|
9930 | 0 | const int32_t w = ((const int32_t *)(dst->op_params))[0]; |
9931 | | |
9932 | | // padding |
9933 | 0 | const int px = (w - ne1%w)%w; |
9934 | | //const int py = (w - ne2%w)%w; |
9935 | |
|
9936 | 0 | const int npx = (px + ne1)/w; |
9937 | | //const int npy = (py + ne2)/w; |
9938 | |
|
9939 | 0 | assert(ne0 == ne00); |
9940 | | |
9941 | | // TODO: optimize / multi-thread |
9942 | 0 | for (int64_t i2 = 0; i2 < ne2; ++i2) { |
9943 | 0 | for (int64_t i1 = 0; i1 < ne1; ++i1) { |
9944 | 0 | for (int64_t i0 = 0; i0 < ne0; ++i0) { |
9945 | 0 | const int ip2 = i2/w; |
9946 | 0 | const int ip1 = i1/w; |
9947 | |
|
9948 | 0 | const int64_t i02 = i2%w; |
9949 | 0 | const int64_t i01 = i1%w; |
9950 | 0 | const int64_t i00 = i0; |
9951 | |
|
9952 | 0 | const int64_t i = (ip2*npx + ip1)*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00 + i00; |
9953 | 0 | const int64_t j = i2*ne1*ne0 + i1*ne0 + i0; |
9954 | |
|
9955 | 0 | ((float *) dst->data)[j] = ((float *) src0->data)[i]; |
9956 | 0 | } |
9957 | 0 | } |
9958 | 0 | } |
9959 | 0 | } |
9960 | | |
9961 | | void ggml_compute_forward_win_unpart( |
9962 | | const ggml_compute_params * params, |
9963 | 0 | ggml_tensor * dst) { |
9964 | |
|
9965 | 0 | const ggml_tensor * src0 = dst->src[0]; |
9966 | |
|
9967 | 0 | switch (src0->type) { |
9968 | 0 | case GGML_TYPE_F32: |
9969 | 0 | { |
9970 | 0 | ggml_compute_forward_win_unpart_f32(params, dst); |
9971 | 0 | } break; |
9972 | 0 | default: |
9973 | 0 | { |
9974 | 0 | GGML_ABORT("fatal error"); |
9975 | 0 | } |
9976 | 0 | } |
9977 | 0 | } |
9978 | | |
9979 | | //ggml_compute_forward_unary |
9980 | | |
9981 | | void ggml_compute_forward_unary( |
9982 | | const ggml_compute_params * params, |
9983 | 0 | ggml_tensor * dst) { |
9984 | |
|
9985 | 0 | const ggml_unary_op op = ggml_get_unary_op(dst); |
9986 | |
|
9987 | 0 | switch (op) { |
9988 | 0 | case GGML_UNARY_OP_ABS: |
9989 | 0 | { |
9990 | 0 | ggml_compute_forward_abs(params, dst); |
9991 | 0 | } break; |
9992 | 0 | case GGML_UNARY_OP_SGN: |
9993 | 0 | { |
9994 | 0 | ggml_compute_forward_sgn(params, dst); |
9995 | 0 | } break; |
9996 | 0 | case GGML_UNARY_OP_NEG: |
9997 | 0 | { |
9998 | 0 | ggml_compute_forward_neg(params, dst); |
9999 | 0 | } break; |
10000 | 0 | case GGML_UNARY_OP_STEP: |
10001 | 0 | { |
10002 | 0 | ggml_compute_forward_step(params, dst); |
10003 | 0 | } break; |
10004 | 0 | case GGML_UNARY_OP_TANH: |
10005 | 0 | { |
10006 | 0 | ggml_compute_forward_tanh(params, dst); |
10007 | 0 | } break; |
10008 | 0 | case GGML_UNARY_OP_ELU: |
10009 | 0 | { |
10010 | 0 | ggml_compute_forward_elu(params, dst); |
10011 | 0 | } break; |
10012 | 0 | case GGML_UNARY_OP_RELU: |
10013 | 0 | { |
10014 | 0 | ggml_compute_forward_relu(params, dst); |
10015 | 0 | } break; |
10016 | 0 | case GGML_UNARY_OP_SIGMOID: |
10017 | 0 | { |
10018 | 0 | ggml_compute_forward_sigmoid(params, dst); |
10019 | 0 | } break; |
10020 | 0 | case GGML_UNARY_OP_GELU: |
10021 | 0 | { |
10022 | 0 | ggml_compute_forward_gelu(params, dst); |
10023 | 0 | } break; |
10024 | 0 | case GGML_UNARY_OP_GELU_ERF: |
10025 | 0 | { |
10026 | 0 | ggml_compute_forward_gelu_erf(params, dst); |
10027 | 0 | } break; |
10028 | 0 | case GGML_UNARY_OP_GELU_QUICK: |
10029 | 0 | { |
10030 | 0 | ggml_compute_forward_gelu_quick(params, dst); |
10031 | 0 | } break; |
10032 | 0 | case GGML_UNARY_OP_SILU: |
10033 | 0 | { |
10034 | 0 | ggml_compute_forward_silu(params, dst); |
10035 | 0 | } break; |
10036 | 0 | case GGML_UNARY_OP_HARDSWISH: |
10037 | 0 | { |
10038 | 0 | ggml_compute_forward_hardswish(params, dst); |
10039 | 0 | } break; |
10040 | 0 | case GGML_UNARY_OP_HARDSIGMOID: |
10041 | 0 | { |
10042 | 0 | ggml_compute_forward_hardsigmoid(params, dst); |
10043 | 0 | } break; |
10044 | 0 | case GGML_UNARY_OP_EXP: |
10045 | 0 | { |
10046 | 0 | ggml_compute_forward_exp(params, dst); |
10047 | 0 | } break; |
10048 | 0 | case GGML_UNARY_OP_FLOOR: |
10049 | 0 | { |
10050 | 0 | ggml_compute_forward_floor(params, dst); |
10051 | 0 | } break; |
10052 | 0 | case GGML_UNARY_OP_CEIL: |
10053 | 0 | { |
10054 | 0 | ggml_compute_forward_ceil(params, dst); |
10055 | 0 | } break; |
10056 | 0 | case GGML_UNARY_OP_ROUND: |
10057 | 0 | { |
10058 | 0 | ggml_compute_forward_round(params, dst); |
10059 | 0 | } break; |
10060 | 0 | case GGML_UNARY_OP_TRUNC: |
10061 | 0 | { |
10062 | 0 | ggml_compute_forward_trunc(params, dst); |
10063 | 0 | } break; |
10064 | 0 | case GGML_UNARY_OP_XIELU: |
10065 | 0 | { |
10066 | 0 | ggml_compute_forward_xielu(params, dst); |
10067 | 0 | } break; |
10068 | 0 | case GGML_UNARY_OP_EXPM1: |
10069 | 0 | { |
10070 | 0 | ggml_compute_forward_expm1(params, dst); |
10071 | 0 | } break; |
10072 | 0 | case GGML_UNARY_OP_SOFTPLUS: |
10073 | 0 | { |
10074 | 0 | ggml_compute_forward_softplus(params, dst); |
10075 | 0 | } break; |
10076 | 0 | default: |
10077 | 0 | { |
10078 | 0 | GGML_ABORT("fatal error"); |
10079 | 0 | } |
10080 | 0 | } |
10081 | 0 | } |
10082 | | |
10083 | | //ggml_compute_forward_glu |
10084 | | |
10085 | | void ggml_compute_forward_glu( |
10086 | | const ggml_compute_params * params, |
10087 | 0 | ggml_tensor * dst) { |
10088 | |
|
10089 | 0 | const ggml_glu_op op = ggml_get_glu_op(dst); |
10090 | |
|
10091 | 0 | switch (op) { |
10092 | 0 | case GGML_GLU_OP_REGLU: |
10093 | 0 | { |
10094 | 0 | ggml_compute_forward_reglu(params, dst); |
10095 | 0 | } break; |
10096 | 0 | case GGML_GLU_OP_GEGLU: |
10097 | 0 | { |
10098 | 0 | ggml_compute_forward_geglu(params, dst); |
10099 | 0 | } break; |
10100 | 0 | case GGML_GLU_OP_SWIGLU: |
10101 | 0 | { |
10102 | 0 | ggml_compute_forward_swiglu(params, dst); |
10103 | 0 | } break; |
10104 | 0 | case GGML_GLU_OP_SWIGLU_OAI: |
10105 | 0 | { |
10106 | 0 | ggml_compute_forward_swiglu_oai(params, dst); |
10107 | 0 | } break; |
10108 | 0 | case GGML_GLU_OP_GEGLU_ERF: |
10109 | 0 | { |
10110 | 0 | ggml_compute_forward_geglu_erf(params, dst); |
10111 | 0 | } break; |
10112 | 0 | case GGML_GLU_OP_GEGLU_QUICK: |
10113 | 0 | { |
10114 | 0 | ggml_compute_forward_geglu_quick(params, dst); |
10115 | 0 | } break; |
10116 | 0 | default: |
10117 | 0 | { |
10118 | 0 | GGML_ABORT("fatal error"); |
10119 | 0 | } |
10120 | 0 | } |
10121 | 0 | } |
10122 | | |
10123 | | // ggml_compute_forward_get_rel_pos |
10124 | | |
10125 | | static void ggml_compute_forward_get_rel_pos_f16( |
10126 | | const ggml_compute_params * params, |
10127 | 0 | ggml_tensor * dst) { |
10128 | 0 | GGML_UNUSED(params); |
10129 | |
|
10130 | 0 | const ggml_tensor * src0 = dst->src[0]; |
10131 | | |
10132 | | // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L292-L322 |
10133 | |
|
10134 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
10135 | |
|
10136 | 0 | const int64_t w = ne1; |
10137 | |
|
10138 | 0 | ggml_fp16_t * src0_data = (ggml_fp16_t *) src0->data; |
10139 | 0 | ggml_fp16_t * dst_data = (ggml_fp16_t *) dst->data; |
10140 | |
|
10141 | 0 | for (int64_t i2 = 0; i2 < ne2; ++i2) { |
10142 | 0 | for (int64_t i1 = 0; i1 < ne1; ++i1) { |
10143 | 0 | const int64_t pos = (w - i1 - 1) + i2; |
10144 | 0 | for (int64_t i0 = 0; i0 < ne0; ++i0) { |
10145 | 0 | dst_data[i2*ne1*ne0 + i1*ne0 + i0] = src0_data[pos*ne00 + i0]; |
10146 | 0 | } |
10147 | 0 | } |
10148 | 0 | } |
10149 | 0 | } |
10150 | | |
10151 | | void ggml_compute_forward_get_rel_pos( |
10152 | | const ggml_compute_params * params, |
10153 | 0 | ggml_tensor * dst) { |
10154 | |
|
10155 | 0 | const ggml_tensor * src0 = dst->src[0]; |
10156 | |
|
10157 | 0 | switch (src0->type) { |
10158 | 0 | case GGML_TYPE_F16: |
10159 | 0 | case GGML_TYPE_BF16: |
10160 | 0 | { |
10161 | 0 | ggml_compute_forward_get_rel_pos_f16(params, dst); |
10162 | 0 | } break; |
10163 | 0 | default: |
10164 | 0 | { |
10165 | 0 | GGML_ABORT("fatal error"); |
10166 | 0 | } |
10167 | 0 | } |
10168 | 0 | } |
10169 | | |
10170 | | // ggml_compute_forward_add_rel_pos |
10171 | | |
10172 | | static void ggml_compute_forward_add_rel_pos_f32( |
10173 | | const ggml_compute_params * params, |
10174 | 0 | ggml_tensor * dst) { |
10175 | |
|
10176 | 0 | const ggml_tensor * src0 = dst->src[0]; |
10177 | 0 | const ggml_tensor * src1 = dst->src[1]; |
10178 | 0 | const ggml_tensor * src2 = dst->src[2]; |
10179 | |
|
10180 | 0 | const bool inplace = (bool) ((int32_t *) dst->op_params)[0]; |
10181 | 0 | if (!inplace) { |
10182 | 0 | if (params->ith == 0) { |
10183 | 0 | memcpy((char *) dst->data, (char *) src0->data, ggml_nbytes(dst)); |
10184 | 0 | } |
10185 | 0 | ggml_barrier(params->threadpool); |
10186 | 0 | } |
10187 | | // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L357-L359 |
10188 | |
|
10189 | 0 | float * src1_data = (float *) src1->data; |
10190 | 0 | float * src2_data = (float *) src2->data; |
10191 | 0 | float * dst_data = (float *) dst->data; |
10192 | |
|
10193 | 0 | const int64_t ne10 = src1->ne[0]; |
10194 | 0 | const int64_t ne11 = src1->ne[1]; |
10195 | 0 | const int64_t ne12 = src1->ne[2]; |
10196 | 0 | const int64_t ne13 = src1->ne[3]; |
10197 | |
|
10198 | 0 | const int ith = params->ith; |
10199 | 0 | const int nth = params->nth; |
10200 | | |
10201 | | // total patches in dst |
10202 | 0 | const int np = ne13; |
10203 | | |
10204 | | // patches per thread |
10205 | 0 | const int dp = (np + nth - 1)/nth; |
10206 | | |
10207 | | // patch range for this thread |
10208 | 0 | const int ip0 = dp*ith; |
10209 | 0 | const int ip1 = MIN(ip0 + dp, np); |
10210 | |
|
10211 | 0 | for (int64_t i13 = ip0; i13 < ip1; ++i13) { |
10212 | 0 | for (int64_t i12 = 0; i12 < ne12; ++i12) { |
10213 | 0 | for (int64_t i11 = 0; i11 < ne11; ++i11) { |
10214 | 0 | const int64_t jp1 = i13*ne12*ne11*ne10 + i12*ne11*ne10 + i11*ne10; |
10215 | 0 | for (int64_t i10 = 0; i10 < ne10; ++i10) { |
10216 | 0 | const int64_t jp0 = jp1 + i10; |
10217 | 0 | const float src1_e = src1_data[jp0]; |
10218 | 0 | const float src2_e = src2_data[jp0]; |
10219 | |
|
10220 | 0 | const int64_t jdh = jp0 * ne10; |
10221 | 0 | const int64_t jdw = jdh - (ne10 - 1) * i10; |
10222 | |
|
10223 | 0 | for (int64_t j = 0; j < ne10; ++j) { |
10224 | 0 | dst_data[jdh + j ] += src2_e; |
10225 | 0 | dst_data[jdw + j*ne10] += src1_e; |
10226 | 0 | } |
10227 | 0 | } |
10228 | 0 | } |
10229 | 0 | } |
10230 | 0 | } |
10231 | 0 | } |
10232 | | |
10233 | | void ggml_compute_forward_add_rel_pos( |
10234 | | const ggml_compute_params * params, |
10235 | 0 | ggml_tensor * dst) { |
10236 | |
|
10237 | 0 | const ggml_tensor * src0 = dst->src[0]; |
10238 | |
|
10239 | 0 | switch (src0->type) { |
10240 | 0 | case GGML_TYPE_F32: |
10241 | 0 | { |
10242 | 0 | ggml_compute_forward_add_rel_pos_f32(params, dst); |
10243 | 0 | } break; |
10244 | 0 | default: |
10245 | 0 | { |
10246 | 0 | GGML_ABORT("fatal error"); |
10247 | 0 | } |
10248 | 0 | } |
10249 | 0 | } |
10250 | | |
10251 | | // ggml_compute_forward_rwkv_wkv6 |
10252 | | |
10253 | | static void ggml_compute_forward_rwkv_wkv6_f32( |
10254 | | const ggml_compute_params * params, |
10255 | 0 | ggml_tensor * dst) { |
10256 | 0 | const int64_t T = dst->src[1]->ne[2]; |
10257 | 0 | const int64_t C = dst->ne[0]; |
10258 | 0 | const int64_t HEADS = dst->src[1]->ne[1]; |
10259 | 0 | const int64_t n_seqs = dst->src[5]->ne[1]; |
10260 | 0 | const int64_t head_size = C / HEADS; |
10261 | |
|
10262 | 0 | float * dst_data = (float *) dst->data; |
10263 | 0 | float * state = ((float *) dst->data) + C * T; |
10264 | |
|
10265 | 0 | const int ith = params->ith; |
10266 | 0 | const int nth = params->nth; |
10267 | |
|
10268 | 0 | const int h_start = (HEADS * (ith )) / nth; |
10269 | 0 | const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ? |
10270 | 0 | (HEADS * (ith + 1)) / nth : HEADS; |
10271 | |
|
10272 | 0 | float * k = (float *) dst->src[0]->data; |
10273 | 0 | float * v = (float *) dst->src[1]->data; |
10274 | 0 | float * r = (float *) dst->src[2]->data; |
10275 | 0 | float * time_faaaa = (float *) dst->src[3]->data; |
10276 | 0 | float * time_decay = (float *) dst->src[4]->data; |
10277 | |
|
10278 | 0 | size_t t_stride = HEADS * head_size; // Same to C |
10279 | |
|
10280 | 0 | size_t h_stride = C / HEADS; |
10281 | 0 | GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS |
10282 | 0 | size_t h_stride_2d = head_size * head_size; |
10283 | |
|
10284 | 0 | if (ith == 0) { |
10285 | 0 | memset(dst_data, 0, T * C * sizeof(float)); |
10286 | 0 | } |
10287 | 0 | ggml_barrier(params->threadpool); |
10288 | | |
10289 | |
|
10290 | 0 | #if defined(__AVX__) && !defined(__AVX512F__) |
10291 | 0 | #define GGML_F32X GGML_F32x8 |
10292 | 0 | #define GGML_F32X_SET1 GGML_F32x8_SET1 |
10293 | 0 | #define GGML_F32X_LOAD GGML_F32x8_LOAD |
10294 | 0 | #define GGML_F32X_STORE GGML_F32x8_STORE |
10295 | 0 | #define GGML_F32X_MUL GGML_F32x8_MUL |
10296 | 0 | #define GGML_F32X_FMA GGML_F32x8_FMA |
10297 | 0 | #define WKV_VECTOR_SIZE 8 |
10298 | | #elif defined(__AVX512F__) |
10299 | | #define GGML_F32X GGML_F32x16 |
10300 | | #define GGML_F32X_SET1 GGML_F32x16_SET1 |
10301 | | #define GGML_F32X_LOAD GGML_F32x16_LOAD |
10302 | | #define GGML_F32X_STORE GGML_F32x16_STORE |
10303 | | #define GGML_F32X_MUL GGML_F32x16_MUL |
10304 | | #define GGML_F32X_FMA GGML_F32x16_FMA |
10305 | | #define WKV_VECTOR_SIZE 16 |
10306 | | #elif defined(__ARM_FEATURE_SVE) && defined(__aarch64__) |
10307 | | #define GGML_F32X GGML_F32xt |
10308 | | #define GGML_F32X_SET1 GGML_F32xt_SET1 |
10309 | | #define GGML_F32X_LOAD GGML_F32xt_LOAD |
10310 | | #define GGML_F32X_STORE GGML_F32xt_STORE |
10311 | | #define GGML_F32X_MUL GGML_F32xt_MUL |
10312 | | #define GGML_F32X_FMA GGML_F32xt_FMA |
10313 | | #define WKV_VECTOR_SIZE 8 |
10314 | | #elif defined(__ARM_NEON) && defined(__aarch64__) |
10315 | | #define GGML_F32X GGML_F32x4 |
10316 | | #define GGML_F32X_SET1 GGML_F32x4_SET1 |
10317 | | #define GGML_F32X_LOAD GGML_F32x4_LOAD |
10318 | | #define GGML_F32X_STORE GGML_F32x4_STORE |
10319 | | #define GGML_F32X_MUL GGML_F32x4_MUL |
10320 | | #define GGML_F32X_FMA GGML_F32x4_FMA |
10321 | | #define WKV_VECTOR_SIZE 4 |
10322 | | #endif |
10323 | |
|
10324 | 0 | #ifdef WKV_VECTOR_SIZE |
10325 | 0 | int wkv_vector_size; |
10326 | | #if defined(__ARM_FEATURE_SVE) |
10327 | | wkv_vector_size = svcntw(); |
10328 | | #else |
10329 | 0 | wkv_vector_size = WKV_VECTOR_SIZE; |
10330 | 0 | #endif |
10331 | 0 | const int64_t vec_count = head_size / wkv_vector_size; |
10332 | |
|
10333 | 0 | for (int64_t t = 0; t < T; t++) { |
10334 | 0 | size_t t_offset = t * t_stride; |
10335 | 0 | size_t state_offset = head_size * C * (t / (T / n_seqs)); |
10336 | 0 | float * state_cur = state + state_offset; |
10337 | 0 | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset; |
10338 | |
|
10339 | 0 | for (int64_t h = h_start; h < h_end; h++) { |
10340 | 0 | size_t h_offset = h * h_stride; |
10341 | 0 | size_t t_h_offset = t_offset + h_offset; |
10342 | 0 | size_t h_2d_offset = h * h_stride_2d; |
10343 | |
|
10344 | 0 | for (int64_t i = 0; i < head_size; i++) { |
10345 | 0 | size_t t_h_i_offset = t_h_offset + i; |
10346 | 0 | size_t h_i_offset = h_offset + i; |
10347 | 0 | size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
10348 | |
|
10349 | 0 | float k_val = k[t_h_i_offset]; |
10350 | 0 | float r_val = r[t_h_i_offset]; |
10351 | 0 | float time_faaaa_val = time_faaaa[h_i_offset]; |
10352 | 0 | float time_decay_val = time_decay[t_h_i_offset]; |
10353 | | |
10354 | | // Broadcast scalar values to vectors |
10355 | 0 | GGML_F32X k_vec = GGML_F32X_SET1(k_val); |
10356 | 0 | GGML_F32X r_vec = GGML_F32X_SET1(r_val); |
10357 | 0 | GGML_F32X time_faaaa_vec = GGML_F32X_SET1(time_faaaa_val); |
10358 | 0 | GGML_F32X time_decay_vec = GGML_F32X_SET1(time_decay_val); |
10359 | |
|
10360 | 0 | for (int64_t j = 0; j < vec_count; j++) { |
10361 | 0 | size_t base_j = j * wkv_vector_size; |
10362 | 0 | size_t t_h_j_offset = t_h_offset + base_j; |
10363 | 0 | size_t h_2d_i_j_offset = h_2d_i_offset + base_j; |
10364 | | |
10365 | | // Load x elements at once |
10366 | 0 | GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]); |
10367 | 0 | GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]); |
10368 | 0 | GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]); |
10369 | | |
10370 | | // Compute kv = v * k |
10371 | 0 | GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec); |
10372 | | |
10373 | | // Compute temp = kv * time_faaaa + prev_state |
10374 | 0 | GGML_F32X temp_vec = GGML_F32X_FMA(prev_state_vec, kv_vec, time_faaaa_vec); |
10375 | | |
10376 | | // Update dst: dst += temp * r |
10377 | 0 | dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, r_vec); |
10378 | 0 | GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec); |
10379 | | |
10380 | | // Update state: state = prev_state * time_decay + kv |
10381 | 0 | GGML_F32X new_state_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, time_decay_vec); |
10382 | 0 | GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], new_state_vec); |
10383 | 0 | } |
10384 | | |
10385 | | // Handle remaining elements, this will not be used. |
10386 | 0 | for (int64_t j = vec_count * wkv_vector_size; j < head_size; j++) { |
10387 | 0 | size_t t_h_j_offset = t_h_offset + j; |
10388 | 0 | size_t h_2d_i_j_offset = h_2d_i_offset + j; |
10389 | 0 | float v_val = v[t_h_j_offset]; |
10390 | 0 | float kv_val = v_val * k_val; |
10391 | 0 | float prev_state_val = state_prev[h_2d_i_j_offset]; |
10392 | 0 | float temp_val = kv_val * time_faaaa_val + prev_state_val; |
10393 | 0 | dst_data[t_h_j_offset] += temp_val * r_val; |
10394 | 0 | state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val; |
10395 | 0 | } |
10396 | 0 | } |
10397 | 0 | } |
10398 | 0 | } |
10399 | |
|
10400 | | #else |
10401 | | // basically fused operations: |
10402 | | // dst = r @ (time_faaaa * (k @ v) + state), |
10403 | | // state = time_decay * state + (k @ v), |
10404 | | // recursive through each token |
10405 | | for (int64_t t = 0; t < T; t++) { |
10406 | | size_t t_offset = t * t_stride; |
10407 | | size_t state_offset = head_size * C * (t / (T / n_seqs)); |
10408 | | float * state_cur = state + state_offset; |
10409 | | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset; |
10410 | | |
10411 | | for (int64_t h = h_start; h < h_end; h++) { |
10412 | | size_t h_offset = h * h_stride; |
10413 | | size_t t_h_offset = t_offset + h_offset; |
10414 | | size_t h_2d_offset = h * h_stride_2d; |
10415 | | |
10416 | | for (int64_t i = 0; i < head_size; i++) { |
10417 | | size_t t_h_i_offset = t_h_offset + i; |
10418 | | size_t h_i_offset = h_offset + i; |
10419 | | size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
10420 | | |
10421 | | float k_val = k[t_h_i_offset]; |
10422 | | float r_val = r[t_h_i_offset]; |
10423 | | float time_faaaa_val = time_faaaa[h_i_offset]; |
10424 | | // RWKV v6: different time_decay for each token. |
10425 | | float time_decay_val = time_decay[t_h_i_offset]; |
10426 | | |
10427 | | for (int64_t j = 0; j < head_size; j++) { |
10428 | | size_t t_h_j_offset = t_h_offset + j; |
10429 | | size_t h_2d_i_j_offset = h_2d_i_offset + j; |
10430 | | |
10431 | | float v_val = v[t_h_j_offset]; |
10432 | | float kv_val = v_val * k_val; |
10433 | | float prev_state_val = state_prev[h_2d_i_j_offset]; |
10434 | | float temp_val = kv_val * time_faaaa_val + prev_state_val; |
10435 | | dst_data[t_h_j_offset] += temp_val * r_val; |
10436 | | state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val; |
10437 | | } |
10438 | | } |
10439 | | } |
10440 | | } |
10441 | | #endif |
10442 | 0 | } |
10443 | | |
10444 | | |
10445 | | void ggml_compute_forward_rwkv_wkv6( |
10446 | | const ggml_compute_params * params, |
10447 | 0 | ggml_tensor * dst) { |
10448 | |
|
10449 | 0 | const ggml_tensor * src0 = dst->src[0]; |
10450 | |
|
10451 | 0 | switch (src0->type) { |
10452 | 0 | case GGML_TYPE_F32: |
10453 | 0 | { |
10454 | 0 | ggml_compute_forward_rwkv_wkv6_f32(params, dst); |
10455 | 0 | } break; |
10456 | 0 | default: |
10457 | 0 | { |
10458 | 0 | GGML_ABORT("fatal error"); |
10459 | 0 | } |
10460 | 0 | } |
10461 | 0 | } |
10462 | | |
10463 | | // ggml_compute_forward_gla |
10464 | | |
10465 | | static void ggml_compute_forward_gla_f32( |
10466 | | const ggml_compute_params * params, |
10467 | 0 | ggml_tensor * dst) { |
10468 | 0 | const int64_t T = dst->src[1]->ne[2]; |
10469 | 0 | const int64_t C = dst->ne[0]; |
10470 | 0 | const int64_t HEADS = dst->src[1]->ne[1]; |
10471 | 0 | const int64_t n_seqs = dst->src[4]->ne[1]; |
10472 | 0 | const int64_t head_size = C / HEADS; |
10473 | 0 | const float scale = ggml_get_op_params_f32(dst, 0); |
10474 | |
|
10475 | 0 | float * dst_data = (float *) dst->data; |
10476 | 0 | float * state = ((float *) dst->data) + C * T; |
10477 | |
|
10478 | 0 | const int ith = params->ith; |
10479 | 0 | const int nth = params->nth; |
10480 | |
|
10481 | 0 | const int h_start = (HEADS * (ith )) / nth; |
10482 | 0 | const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ? |
10483 | 0 | (HEADS * (ith + 1)) / nth : HEADS; |
10484 | |
|
10485 | 0 | float * k = (float *) dst->src[0]->data; |
10486 | 0 | float * v = (float *) dst->src[1]->data; |
10487 | 0 | float * q = (float *) dst->src[2]->data; |
10488 | 0 | float * g = (float *) dst->src[3]->data; |
10489 | |
|
10490 | 0 | size_t t_stride = HEADS * head_size; // Same to C |
10491 | |
|
10492 | 0 | size_t h_stride = C / HEADS; |
10493 | 0 | GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS |
10494 | 0 | size_t h_stride_2d = head_size * head_size; |
10495 | |
|
10496 | 0 | if (ith == 0) { |
10497 | 0 | memset(dst_data, 0, T * C * sizeof(float)); |
10498 | 0 | } |
10499 | 0 | ggml_barrier(params->threadpool); |
10500 | | |
10501 | |
|
10502 | 0 | #if defined(__AVX__) && !defined(__AVX512F__) |
10503 | 0 | #define GGML_F32X GGML_F32x8 |
10504 | 0 | #define GGML_F32X_SET1 GGML_F32x8_SET1 |
10505 | 0 | #define GGML_F32X_LOAD GGML_F32x8_LOAD |
10506 | 0 | #define GGML_F32X_STORE GGML_F32x8_STORE |
10507 | 0 | #define GGML_F32X_MUL GGML_F32x8_MUL |
10508 | 0 | #define GGML_F32X_FMA GGML_F32x8_FMA |
10509 | 0 | #define GLA_VECTOR_SIZE 8 |
10510 | | #elif defined(__AVX512F__) |
10511 | | #define GGML_F32X GGML_F32x16 |
10512 | | #define GGML_F32X_SET1 GGML_F32x16_SET1 |
10513 | | #define GGML_F32X_LOAD GGML_F32x16_LOAD |
10514 | | #define GGML_F32X_STORE GGML_F32x16_STORE |
10515 | | #define GGML_F32X_MUL GGML_F32x16_MUL |
10516 | | #define GGML_F32X_FMA GGML_F32x16_FMA |
10517 | | #define GLA_VECTOR_SIZE 16 |
10518 | | #elif defined(__ARM_FEATURE_SVE) && defined(__aarch64__) |
10519 | | #define GGML_F32X GGML_F32xt |
10520 | | #define GGML_F32X_SET1 GGML_F32xt_SET1 |
10521 | | #define GGML_F32X_LOAD GGML_F32xt_LOAD |
10522 | | #define GGML_F32X_STORE GGML_F32xt_STORE |
10523 | | #define GGML_F32X_MUL GGML_F32xt_MUL |
10524 | | #define GGML_F32X_FMA GGML_F32xt_FMA |
10525 | | #define GLA_VECTOR_SIZE 8 |
10526 | | #elif defined(__ARM_NEON) && defined(__aarch64__) |
10527 | | #define GGML_F32X GGML_F32x4 |
10528 | | #define GGML_F32X_SET1 GGML_F32x4_SET1 |
10529 | | #define GGML_F32X_LOAD GGML_F32x4_LOAD |
10530 | | #define GGML_F32X_STORE GGML_F32x4_STORE |
10531 | | #define GGML_F32X_MUL GGML_F32x4_MUL |
10532 | | #define GGML_F32X_FMA GGML_F32x4_FMA |
10533 | | #define GLA_VECTOR_SIZE 4 |
10534 | | #endif |
10535 | |
|
10536 | 0 | #ifdef GLA_VECTOR_SIZE |
10537 | 0 | int gla_vector_size; |
10538 | | #if defined(__ARM_FEATURE_SVE) |
10539 | | gla_vector_size = svcntw(); |
10540 | | #else |
10541 | 0 | gla_vector_size = GLA_VECTOR_SIZE; |
10542 | 0 | #endif |
10543 | 0 | const int64_t vec_count = head_size / gla_vector_size; |
10544 | |
|
10545 | 0 | for (int64_t t = 0; t < T; t++) { |
10546 | 0 | size_t t_offset = t * t_stride; |
10547 | 0 | size_t state_offset = head_size * C * (t / (T / n_seqs)); |
10548 | 0 | float * state_cur = state + state_offset; |
10549 | 0 | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[4]->data + state_offset; |
10550 | |
|
10551 | 0 | for (int64_t h = h_start; h < h_end; h++) { |
10552 | 0 | size_t h_offset = h * h_stride; |
10553 | 0 | size_t t_h_offset = t_offset + h_offset; |
10554 | 0 | size_t h_2d_offset = h * h_stride_2d; |
10555 | |
|
10556 | 0 | for (int64_t i = 0; i < head_size; i++) { |
10557 | 0 | size_t t_h_i_offset = t_h_offset + i; |
10558 | 0 | size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
10559 | |
|
10560 | 0 | float k_val = k[t_h_i_offset]; |
10561 | 0 | float q_val = q[t_h_i_offset] * scale; |
10562 | 0 | float g_val = g[t_h_i_offset]; |
10563 | | |
10564 | | // Broadcast scalar values to vectors |
10565 | 0 | GGML_F32X k_vec = GGML_F32X_SET1(k_val); |
10566 | 0 | GGML_F32X q_vec = GGML_F32X_SET1(q_val); |
10567 | 0 | GGML_F32X g_vec = GGML_F32X_SET1(g_val); |
10568 | |
|
10569 | 0 | for (int64_t j = 0; j < vec_count; j++) { |
10570 | 0 | size_t base_j = j * gla_vector_size; |
10571 | 0 | size_t t_h_j_offset = t_h_offset + base_j; |
10572 | 0 | size_t h_2d_i_j_offset = h_2d_i_offset + base_j; |
10573 | | |
10574 | | // Load x elements at once |
10575 | 0 | GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]); |
10576 | 0 | GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]); |
10577 | 0 | GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]); |
10578 | | |
10579 | | // Compute kv = v * k |
10580 | 0 | GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec); |
10581 | | |
10582 | | // Compute temp = prev_state * g + kv |
10583 | 0 | GGML_F32X temp_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, g_vec); |
10584 | | |
10585 | | // Update dst: dst += temp * q |
10586 | 0 | dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, q_vec); |
10587 | 0 | GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec); |
10588 | | |
10589 | | // Update state |
10590 | 0 | GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], temp_vec); |
10591 | 0 | } |
10592 | | |
10593 | | // Handle remaining elements, this will not be used. |
10594 | 0 | for (int64_t j = vec_count * gla_vector_size; j < head_size; j++) { |
10595 | 0 | size_t t_h_j_offset = t_h_offset + j; |
10596 | 0 | size_t h_2d_i_j_offset = h_2d_i_offset + j; |
10597 | 0 | float v_val = v[t_h_j_offset]; |
10598 | 0 | float kv_val = v_val * k_val; |
10599 | 0 | float prev_state_val = state_prev[h_2d_i_j_offset]; |
10600 | 0 | float temp_val = kv_val + prev_state_val * g_val; |
10601 | 0 | dst_data[t_h_j_offset] += temp_val * q_val; |
10602 | 0 | state_cur[h_2d_i_j_offset] = temp_val; |
10603 | 0 | } |
10604 | 0 | } |
10605 | 0 | } |
10606 | 0 | } |
10607 | |
|
10608 | | #else |
10609 | | for (int64_t t = 0; t < T; t++) { |
10610 | | size_t t_offset = t * t_stride; |
10611 | | size_t state_offset = head_size * C * (t / (T / n_seqs)); |
10612 | | float * state_cur = state + state_offset; |
10613 | | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[4]->data + state_offset; |
10614 | | |
10615 | | for (int64_t h = h_start; h < h_end; h++) { |
10616 | | size_t h_offset = h * h_stride; |
10617 | | size_t t_h_offset = t_offset + h_offset; |
10618 | | size_t h_2d_offset = h * h_stride_2d; |
10619 | | |
10620 | | for (int64_t i = 0; i < head_size; i++) { |
10621 | | size_t t_h_i_offset = t_h_offset + i; |
10622 | | size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
10623 | | |
10624 | | float k_val = k[t_h_i_offset]; |
10625 | | float q_val = q[t_h_i_offset] * scale; |
10626 | | float g_val = g[t_h_i_offset]; |
10627 | | |
10628 | | for (int64_t j = 0; j < head_size; j++) { |
10629 | | size_t t_h_j_offset = t_h_offset + j; |
10630 | | size_t h_2d_i_j_offset = h_2d_i_offset + j; |
10631 | | |
10632 | | float v_val = v[t_h_j_offset]; |
10633 | | float kv_val = v_val * k_val; |
10634 | | float prev_state_val = state_prev[h_2d_i_j_offset]; |
10635 | | float temp_val = prev_state_val * g_val + kv_val; |
10636 | | dst_data[t_h_j_offset] += temp_val * q_val; |
10637 | | state_cur[h_2d_i_j_offset] = temp_val; |
10638 | | } |
10639 | | } |
10640 | | } |
10641 | | } |
10642 | | #endif |
10643 | 0 | } |
10644 | | |
10645 | | |
10646 | | void ggml_compute_forward_gla( |
10647 | | const ggml_compute_params * params, |
10648 | 0 | ggml_tensor * dst) { |
10649 | |
|
10650 | 0 | const ggml_tensor * src0 = dst->src[0]; |
10651 | |
|
10652 | 0 | switch (src0->type) { |
10653 | 0 | case GGML_TYPE_F32: |
10654 | 0 | { |
10655 | 0 | ggml_compute_forward_gla_f32(params, dst); |
10656 | 0 | } break; |
10657 | 0 | default: |
10658 | 0 | { |
10659 | 0 | GGML_ABORT("fatal error"); |
10660 | 0 | } |
10661 | 0 | } |
10662 | 0 | } |
10663 | | |
10664 | 0 | static void ggml_compute_forward_solve_tri_f32(const struct ggml_compute_params * params, struct ggml_tensor * dst) { |
10665 | 0 | const struct ggml_tensor * src0 = dst->src[0]; // A (lower triangular) |
10666 | 0 | const struct ggml_tensor * src1 = dst->src[1]; // B (RHS) |
10667 | |
|
10668 | 0 | GGML_TENSOR_BINARY_OP_LOCALS; |
10669 | |
|
10670 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
10671 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
10672 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
10673 | |
|
10674 | 0 | GGML_ASSERT(ne00 == ne01); // A must be square |
10675 | 0 | GGML_ASSERT(ne0 == ne10); // solution cols == B cols |
10676 | 0 | GGML_ASSERT(ne1 == ne11); // solution rows == B rows |
10677 | |
|
10678 | 0 | GGML_ASSERT(ne02 == ne12 && ne12 == ne2); |
10679 | 0 | GGML_ASSERT(ne03 == ne13 && ne13 == ne3); |
10680 | |
|
10681 | 0 | const int ith = params->ith; |
10682 | 0 | const int nth = params->nth; |
10683 | |
|
10684 | 0 | const int64_t k = ne10; // number of RHS columns |
10685 | 0 | const int64_t n = ne11; // A is n×n |
10686 | 0 | const int64_t nr = ne02 * ne03 * k; // we're parallelizing on columns here, so seq x token x column will be the unit |
10687 | | |
10688 | | // chunks per thread |
10689 | 0 | const int64_t dr = (nr + nth - 1)/nth; |
10690 | | |
10691 | | // chunk range for this thread |
10692 | 0 | const int64_t ir0 = dr*ith; |
10693 | 0 | const int64_t ir1 = MIN(ir0 + dr, nr); |
10694 | |
|
10695 | 0 | const float * A = (const float *) src0->data; // [n, n, B1, B2] |
10696 | 0 | const float * B = (const float *) src1->data; // [n, k, B1, B2] |
10697 | 0 | float * X = ( float *) dst->data; // [n, k, B1, B2] |
10698 | |
|
10699 | 0 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
10700 | 0 | const int64_t i03 = ir/(ne02*k); |
10701 | 0 | const int64_t i02 = (ir - i03*ne02*k)/k; |
10702 | 0 | const int64_t i01 = (ir - i03*ne02*k - i02*k); |
10703 | |
|
10704 | 0 | const float * A_batch = A + i02 * nb02 / sizeof(float) + i03 * nb03 / sizeof(float); |
10705 | 0 | const float * B_batch = B + i02 * nb12 / sizeof(float) + i03 * nb13 / sizeof(float); |
10706 | |
|
10707 | 0 | float * X_batch = X + i02 * nb2 / sizeof(float) + i03 * nb3 / sizeof(float); |
10708 | |
|
10709 | 0 | for (int64_t i00 = 0; i00 < n; ++i00) { |
10710 | 0 | float sum = 0.0f; |
10711 | 0 | for (int64_t t = 0; t < i00; ++t) { |
10712 | 0 | sum += A_batch[i00 * n + t] * X_batch[t * k + i01]; |
10713 | 0 | } |
10714 | |
|
10715 | 0 | const float diag = A_batch[i00 * n + i00]; |
10716 | 0 | assert(diag != 0.0f && "Zero diagonal in triangular matrix"); |
10717 | |
|
10718 | 0 | X_batch[i00 * k + i01] = (B_batch[i00 * k + i01] - sum) / diag; |
10719 | 0 | } |
10720 | 0 | } |
10721 | 0 | } |
10722 | | |
10723 | 0 | void ggml_compute_forward_solve_tri(const struct ggml_compute_params * params, struct ggml_tensor * dst) { |
10724 | 0 | const ggml_tensor * src0 = dst->src[0]; |
10725 | 0 | const ggml_tensor * src1 = dst->src[1]; |
10726 | |
|
10727 | 0 | if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) { |
10728 | 0 | ggml_compute_forward_solve_tri_f32(params, dst); |
10729 | 0 | } else { |
10730 | 0 | GGML_ABORT("fatal error"); |
10731 | 0 | } |
10732 | 0 | } |
10733 | | |
10734 | | // ggml_compute_forward_gated_delta_net |
10735 | | static void ggml_compute_forward_gated_delta_net_one_chunk( |
10736 | | const ggml_compute_params * params, |
10737 | | ggml_tensor * dst, |
10738 | | int64_t ir0, |
10739 | 0 | int64_t ir1) { |
10740 | |
|
10741 | 0 | ggml_tensor * src_q = dst->src[0]; |
10742 | 0 | ggml_tensor * src_k = dst->src[1]; |
10743 | 0 | ggml_tensor * src_v = dst->src[2]; |
10744 | 0 | ggml_tensor * src_g = dst->src[3]; |
10745 | 0 | ggml_tensor * src_beta = dst->src[4]; |
10746 | 0 | ggml_tensor * src_state = dst->src[5]; |
10747 | |
|
10748 | 0 | const int64_t S_v = src_v->ne[0]; |
10749 | 0 | const int64_t H = src_v->ne[1]; |
10750 | 0 | const int64_t n_tokens = src_v->ne[2]; |
10751 | 0 | const int64_t n_seqs = src_v->ne[3]; |
10752 | |
|
10753 | 0 | GGML_ASSERT(ggml_is_contiguous_rows(src_q)); |
10754 | 0 | GGML_ASSERT(ggml_is_contiguous_rows(src_k)); |
10755 | 0 | GGML_ASSERT(ggml_is_contiguous_rows(src_v)); |
10756 | 0 | GGML_ASSERT(ggml_is_contiguous(src_g)); |
10757 | 0 | GGML_ASSERT(ggml_is_contiguous(src_beta)); |
10758 | 0 | GGML_ASSERT(ggml_is_contiguous(src_state)); |
10759 | |
|
10760 | 0 | GGML_ASSERT(src_g->ne[0] == 1 || src_g->ne[0] == S_v); |
10761 | 0 | GGML_ASSERT(src_beta->ne[0] == 1); |
10762 | |
|
10763 | 0 | GGML_TENSOR_LOCALS(int64_t, neq, src_q, ne); |
10764 | 0 | GGML_TENSOR_LOCALS(size_t, nbq, src_q, nb); |
10765 | 0 | GGML_TENSOR_LOCALS(int64_t, nek, src_k, ne); |
10766 | 0 | GGML_TENSOR_LOCALS(size_t, nbk, src_k, nb); |
10767 | 0 | GGML_TENSOR_LOCALS(int64_t, nev, src_v, ne); |
10768 | 0 | GGML_TENSOR_LOCALS(size_t, nbv, src_v, nb); |
10769 | 0 | GGML_TENSOR_LOCALS(int64_t, neg, src_g, ne); |
10770 | 0 | GGML_TENSOR_LOCALS(size_t, nbg, src_g, nb); |
10771 | 0 | GGML_TENSOR_LOCALS(size_t, nbb, src_beta, nb); |
10772 | |
|
10773 | 0 | const bool kda = (neg0 == S_v); |
10774 | | |
10775 | | // K (snapshot slot count) is an op param; state holds s0 only [S_v, S_v, H, n_seqs]. |
10776 | 0 | const int64_t K = ggml_get_op_params_i32(dst, 0); |
10777 | 0 | GGML_ASSERT(K >= 1); |
10778 | | // per-seq stride in floats (seq s starts at state + s * seq_stride) |
10779 | 0 | const int64_t state_seq_stride = src_state->nb[3] / sizeof(float); |
10780 | |
|
10781 | 0 | const int64_t per_thread = S_v + (K > 1 ? S_v * S_v : 0); |
10782 | 0 | const int ith = params->ith; |
10783 | |
|
10784 | 0 | float * delta = (float *)params->wdata + ith * per_thread + CACHE_LINE_SIZE_F32; |
10785 | 0 | float * state_work = K > 1 ? (delta + S_v) : nullptr; |
10786 | | |
10787 | | // output layout: [attn_scores | new_states] |
10788 | | // attn_scores: S_v * H * n_tokens * n_seqs floats |
10789 | | // new_states: S_v * S_v * H * n_seqs * K floats (K snapshot slots; last min(n_tokens, K)) |
10790 | 0 | const int64_t attn_score_elems = S_v * H * n_tokens * n_seqs; |
10791 | 0 | const int64_t state_size_per_snap = S_v * S_v * H * n_seqs; |
10792 | 0 | float * attn_out_base = (float *)dst->data; |
10793 | 0 | float * state_out_base = (float *)dst->data + attn_score_elems; |
10794 | | |
10795 | | // snapshot slot mapping: slot 0 = most recent state, slot s = s tokens back. |
10796 | | // When n_tokens < K only slots 0..n_tokens-1 are written; older slots are caller-owned. |
10797 | |
|
10798 | 0 | const float * state_in_base = (const float *)src_state->data; |
10799 | | |
10800 | | //const int64_t rq1 = nev1 / neq1; |
10801 | | //const int64_t rk1 = nev1 / nek1; |
10802 | 0 | const int64_t rq3 = nev3 / neq3; |
10803 | 0 | const int64_t rk3 = nev3 / nek3; |
10804 | |
|
10805 | 0 | const float scale = 1.0f / sqrtf((float) S_v); |
10806 | |
|
10807 | 0 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
10808 | 0 | const int64_t iv1 = ir % H; // head_index |
10809 | 0 | const int64_t iv3 = ir / H; // sequence |
10810 | |
|
10811 | 0 | const int64_t iq1 = iv1 % neq1; |
10812 | 0 | const int64_t ik1 = iv1 % nek1; |
10813 | |
|
10814 | 0 | const int64_t iq3 = iv3 / rq3; |
10815 | 0 | const int64_t ik3 = iv3 / rk3; |
10816 | | |
10817 | | // For K=1, write directly to the single output slot to avoid an extra memcpy at the end. |
10818 | | // For K>1, work in scratch and copy out per-token when the slot is in range. |
10819 | 0 | float * s_out = (K > 1) |
10820 | 0 | ? state_work |
10821 | 0 | : state_out_base + (iv3 * H + iv1) * S_v * S_v; |
10822 | | |
10823 | | // copy input state into the working buffer and operate in-place |
10824 | | // state layout [S_v, S_v, H, n_seqs]: seq iv3 starts at iv3 * state_seq_stride. |
10825 | 0 | const float * s_in = state_in_base + iv3 * state_seq_stride + iv1 * S_v * S_v; |
10826 | 0 | memcpy(s_out, s_in, S_v * S_v * sizeof(float)); |
10827 | | |
10828 | | // attn output pointer for first token of this (head, seq) |
10829 | 0 | float * attn_data = attn_out_base + (iv3 * n_tokens * H + iv1) * S_v; |
10830 | |
|
10831 | 0 | for (int64_t t = 0; t < n_tokens; t++) { |
10832 | 0 | const float * q_d = (const float *)((const char *)src_q->data + iq3 * nbq3 + t * nbq2 + iq1 * nbq1); |
10833 | 0 | const float * k_d = (const float *)((const char *)src_k->data + ik3 * nbk3 + t * nbk2 + ik1 * nbk1); |
10834 | 0 | const float * v_d = (const float *)((const char *)src_v->data + iv3 * nbv3 + t * nbv2 + iv1 * nbv1); |
10835 | |
|
10836 | 0 | const float beta_val = *(const float *)((const char *)src_beta->data + iv3 * nbb3 + t * nbb2 + iv1 * nbb1); |
10837 | 0 | const float * g_d = (const float *)((const char *)src_g->data + iv3 * nbg3 + t * nbg2 + iv1 * nbg1); |
10838 | | |
10839 | | // state is stored transposed: s_out[j*S_v + i] = S[i][j] |
10840 | | // so row j of s_out = column j of S (contiguous access) |
10841 | |
|
10842 | 0 | if (kda) { |
10843 | | // precompute exp(g) into delta scratch (reused below) |
10844 | 0 | for (int64_t i = 0; i < S_v; ++i) { |
10845 | 0 | delta[i] = expf(g_d[i]); |
10846 | 0 | } |
10847 | | // S[i][:] *= exp(g[i]) => for each row j of M: M[j][i] *= exp(g[i]) |
10848 | 0 | for (int64_t j = 0; j < S_v; ++j) { |
10849 | 0 | ggml_vec_mul_f32(S_v, &s_out[j * S_v], &s_out[j * S_v], delta); |
10850 | 0 | } |
10851 | 0 | } else { |
10852 | 0 | ggml_vec_scale_f32(S_v * S_v, s_out, expf(g_d[0])); |
10853 | 0 | } |
10854 | | |
10855 | | // delta[j] = sum_i S[i][j] * k[i] = dot(row j of M, k) |
10856 | 0 | for (int64_t j = 0; j < S_v; ++j) { |
10857 | 0 | float sum = 0.0f; |
10858 | 0 | ggml_vec_dot_f32(S_v, &sum, 0, &s_out[j * S_v], 0, k_d, 0, 1); |
10859 | 0 | delta[j] = (v_d[j] - sum) * beta_val; |
10860 | 0 | } |
10861 | | |
10862 | | // outer product: S[i][j] += k[i] * delta[j] => M[j][i] += delta[j] * k[i] |
10863 | 0 | for (int64_t j = 0; j < S_v; ++j) { |
10864 | 0 | ggml_vec_mad_f32(S_v, &s_out[j * S_v], k_d, delta[j]); |
10865 | 0 | } |
10866 | | |
10867 | | // attn_out[j] = sum_i S[i][j] * q[i] = dot(row j of M, q) |
10868 | 0 | for (int64_t j = 0; j < S_v; ++j) { |
10869 | 0 | float sum = 0.0f; |
10870 | 0 | ggml_vec_dot_f32(S_v, &sum, 0, &s_out[j * S_v], 0, q_d, 0, 1); |
10871 | 0 | attn_data[j] = sum * scale; |
10872 | 0 | } |
10873 | |
|
10874 | 0 | attn_data += S_v * H; // advance to next token |
10875 | |
|
10876 | 0 | if (K > 1) { |
10877 | 0 | const int64_t target_slot = n_tokens - 1 - t; |
10878 | 0 | if (target_slot >= 0 && target_slot < K) { |
10879 | 0 | float * curr_state_o = state_out_base + target_slot * state_size_per_snap + |
10880 | 0 | (iv3 * H + iv1) * S_v * S_v; |
10881 | 0 | memcpy(curr_state_o, s_out, S_v * S_v * sizeof(float)); |
10882 | 0 | } |
10883 | 0 | } |
10884 | 0 | } |
10885 | 0 | } |
10886 | 0 | } |
10887 | | |
10888 | | |
10889 | | static void ggml_compute_forward_gated_delta_net_f32( |
10890 | | const ggml_compute_params * params, |
10891 | 0 | ggml_tensor * dst) { |
10892 | |
|
10893 | 0 | ggml_tensor * V = dst->src[2]; |
10894 | 0 | int64_t nr = V->ne[1] * V->ne[3]; |
10895 | | |
10896 | | // disable for NUMA |
10897 | 0 | const bool disable_chunking = ggml_is_numa(); |
10898 | |
|
10899 | 0 | int nth = params->nth; |
10900 | 0 | int ith = params->ith; |
10901 | | |
10902 | | // 4x chunks per thread |
10903 | 0 | int nth_scaled = nth * 4; |
10904 | 0 | int64_t chunk_size = (nr + nth_scaled - 1) / nth_scaled; |
10905 | 0 | int64_t nchunk = (nr + chunk_size - 1) / chunk_size; |
10906 | |
|
10907 | 0 | if (nth == 1 || nchunk < nth || disable_chunking) { |
10908 | 0 | nchunk = nth; |
10909 | 0 | } |
10910 | |
|
10911 | 0 | if (ith == 0) { |
10912 | 0 | ggml_threadpool_chunk_set(params->threadpool, nth); |
10913 | 0 | } |
10914 | |
|
10915 | 0 | ggml_barrier(params->threadpool); |
10916 | |
|
10917 | 0 | const int64_t dr = (nr + nchunk - 1) / nchunk; |
10918 | |
|
10919 | 0 | int current_chunk = ith; |
10920 | |
|
10921 | 0 | while (current_chunk < nchunk) { |
10922 | 0 | const int64_t ir0 = dr * current_chunk; |
10923 | 0 | const int64_t ir1 = MIN(ir0 + dr, nr); |
10924 | |
|
10925 | 0 | ggml_compute_forward_gated_delta_net_one_chunk(params, dst, ir0, ir1); |
10926 | 0 | current_chunk = ggml_threadpool_chunk_add(params->threadpool, 1); |
10927 | 0 | } |
10928 | 0 | } |
10929 | | |
10930 | | void ggml_compute_forward_gated_delta_net( |
10931 | | const ggml_compute_params * params, |
10932 | 0 | ggml_tensor * dst) { |
10933 | 0 | const ggml_tensor * src0 = dst->src[0]; |
10934 | |
|
10935 | 0 | switch (src0->type) { |
10936 | 0 | case GGML_TYPE_F32: |
10937 | 0 | { |
10938 | 0 | ggml_compute_forward_gated_delta_net_f32(params, dst); |
10939 | 0 | } break; |
10940 | 0 | default: |
10941 | 0 | { |
10942 | 0 | GGML_ABORT("fatal error"); |
10943 | 0 | } |
10944 | 0 | } |
10945 | 0 | } |
10946 | | |
10947 | | // ggml_compute_forward_rwkv_wkv7 |
10948 | | |
10949 | | static void ggml_compute_forward_rwkv_wkv7_f32( |
10950 | | const ggml_compute_params * params, |
10951 | 0 | ggml_tensor * dst) { |
10952 | 0 | const int64_t T = dst->src[1]->ne[2]; |
10953 | 0 | const int64_t C = dst->ne[0]; |
10954 | 0 | const int64_t HEADS = dst->src[1]->ne[1]; |
10955 | 0 | const int64_t n_seqs = dst->src[6]->ne[1]; |
10956 | 0 | const int64_t head_size = C / HEADS; |
10957 | |
|
10958 | 0 | float * dst_data = (float *) dst->data; |
10959 | 0 | float * state = ((float *) dst->data) + C * T; |
10960 | |
|
10961 | 0 | const int ith = params->ith; |
10962 | 0 | const int nth = params->nth; |
10963 | |
|
10964 | 0 | const int h_start = (HEADS * (ith )) / nth; |
10965 | 0 | const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ? |
10966 | 0 | (HEADS * (ith + 1)) / nth : HEADS; |
10967 | |
|
10968 | 0 | float * r = (float *) dst->src[0]->data; |
10969 | 0 | float * w = (float *) dst->src[1]->data; |
10970 | 0 | float * k = (float *) dst->src[2]->data; |
10971 | 0 | float * v = (float *) dst->src[3]->data; |
10972 | 0 | float * a = (float *) dst->src[4]->data; |
10973 | 0 | float * b = (float *) dst->src[5]->data; |
10974 | |
|
10975 | 0 | int64_t t_stride = HEADS * head_size; // Same to C |
10976 | |
|
10977 | 0 | int64_t h_stride = C / HEADS; |
10978 | 0 | GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS |
10979 | 0 | int64_t h_stride_2d = head_size * head_size; |
10980 | |
|
10981 | 0 | #if defined(GGML_SIMD) |
10982 | | #if defined(__ARM_FEATURE_SVE) || defined(__riscv_v_intrinsic) |
10983 | | // scalar Route to scalar implementation //TODO: Write SVE code and RVV code |
10984 | | for (int64_t t = 0; t < T; t++) { |
10985 | | int64_t t_offset = t * t_stride; |
10986 | | int64_t state_offset = head_size * C * (t / (T / n_seqs)); |
10987 | | float * state_cur = state + state_offset; |
10988 | | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[6]->data + state_offset; |
10989 | | |
10990 | | for (int64_t h = h_start; h < h_end; h++) { |
10991 | | int64_t h_offset = h * h_stride; |
10992 | | int64_t t_h_offset = t_offset + h_offset; |
10993 | | int64_t h_2d_offset = h * h_stride_2d; |
10994 | | |
10995 | | for (int64_t i = 0; i < head_size; i++) { |
10996 | | int64_t t_h_i_offset = t_h_offset + i; |
10997 | | int64_t h_2d_i_offset = h_2d_offset + i * h_stride; |
10998 | | |
10999 | | float v_val = v[t_h_i_offset]; |
11000 | | |
11001 | | float sa = 0, result = 0; |
11002 | | for (int64_t j = 0; j < head_size; j++) { |
11003 | | sa += a[t_h_offset + j] * state_prev[h_2d_i_offset + j]; |
11004 | | } |
11005 | | |
11006 | | for (int64_t j = 0; j < head_size; j++) { |
11007 | | int64_t t_h_j_offset = t_h_offset + j; |
11008 | | int64_t h_2d_i_j_offset = h_2d_i_offset + j; |
11009 | | |
11010 | | float r_val = r[t_h_j_offset]; |
11011 | | float w_val = w[t_h_j_offset]; |
11012 | | float k_val = k[t_h_j_offset]; |
11013 | | float b_val = b[t_h_j_offset]; |
11014 | | float kv_val = v_val * k_val; |
11015 | | float prev_state_val = state_prev[h_2d_i_j_offset]; |
11016 | | state_cur[h_2d_i_j_offset] = prev_state_val * w_val + kv_val + sa * b_val; |
11017 | | result += state_cur[h_2d_i_j_offset] * r_val; |
11018 | | } |
11019 | | dst_data[t_h_i_offset] = result; |
11020 | | } |
11021 | | } |
11022 | | } |
11023 | | #else |
11024 | 0 | for (int64_t t = 0; t < T; t++) { |
11025 | 0 | int64_t t_offset = t * t_stride; |
11026 | 0 | int64_t state_offset = head_size * C * (t / (T / n_seqs)); |
11027 | 0 | float * state_cur = state + state_offset; |
11028 | 0 | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[6]->data + state_offset; |
11029 | |
|
11030 | 0 | for (int64_t h = h_start; h < h_end; h++) { |
11031 | 0 | int64_t h_offset = h * h_stride; |
11032 | 0 | int64_t t_h_offset = t_offset + h_offset; |
11033 | 0 | int64_t h_2d_offset = h * h_stride_2d; |
11034 | |
|
11035 | 0 | for (int64_t ii = 0; ii < head_size; ii++) { |
11036 | 0 | int64_t t_h_i_offset = t_h_offset + ii; |
11037 | 0 | int64_t h_2d_i_offset = h_2d_offset + ii * h_stride; |
11038 | |
|
11039 | 0 | GGML_F32_VEC v_vec = GGML_F32_VEC_SET1(v[t_h_i_offset]); |
11040 | |
|
11041 | 0 | float sa = 0; |
11042 | 0 | { |
11043 | 0 | GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO }; |
11044 | 0 | GGML_F32_VEC ax[GGML_F32_ARR]; |
11045 | 0 | GGML_F32_VEC ay[GGML_F32_ARR]; |
11046 | 0 | for (int64_t j = 0; j < head_size; j += GGML_F32_STEP) { |
11047 | 0 | for (int64_t kk = 0; kk < GGML_F32_ARR; kk++) { |
11048 | 0 | ax[kk] = GGML_F32_VEC_LOAD(&a[t_h_offset + j + kk * GGML_F32_EPR]); |
11049 | 0 | ay[kk] = GGML_F32_VEC_LOAD(&state_prev[h_2d_i_offset + j + kk * GGML_F32_EPR]); |
11050 | 0 | sum[kk] = GGML_F32_VEC_FMA(sum[kk], ax[kk], ay[kk]); |
11051 | 0 | } |
11052 | 0 | } |
11053 | 0 | GGML_F32_VEC_REDUCE(sa, sum); |
11054 | 0 | } |
11055 | |
|
11056 | 0 | GGML_F32_VEC sa_vec = GGML_F32_VEC_SET1(sa); |
11057 | |
|
11058 | 0 | int64_t j = 0; |
11059 | 0 | GGML_F32_VEC result_vec[GGML_F32_ARR] = { GGML_F32_VEC_ZERO }; |
11060 | 0 | for (; j < head_size; j += GGML_F32_STEP) { |
11061 | 0 | for (int64_t kk = 0; kk < GGML_F32_ARR; kk++) { |
11062 | 0 | int64_t t_h_j_offset = t_h_offset + j + kk * GGML_F32_EPR; |
11063 | 0 | int64_t h_2d_i_j_offset = h_2d_i_offset + j + kk * GGML_F32_EPR; |
11064 | |
|
11065 | 0 | GGML_F32_VEC r_vec = GGML_F32_VEC_LOAD(&r[t_h_j_offset]); |
11066 | 0 | GGML_F32_VEC w_vec = GGML_F32_VEC_LOAD(&w[t_h_j_offset]); |
11067 | 0 | GGML_F32_VEC k_vec = GGML_F32_VEC_LOAD(&k[t_h_j_offset]); |
11068 | 0 | GGML_F32_VEC b_vec = GGML_F32_VEC_LOAD(&b[t_h_j_offset]); |
11069 | |
|
11070 | 0 | k_vec = GGML_F32_VEC_MUL(v_vec, k_vec); |
11071 | |
|
11072 | 0 | GGML_F32_VEC state_vec = GGML_F32_VEC_LOAD(&state_prev[h_2d_i_j_offset]); |
11073 | | // kv + s * decay + sa * b |
11074 | 0 | state_vec = GGML_F32_VEC_FMA(k_vec, state_vec, w_vec); |
11075 | 0 | state_vec = GGML_F32_VEC_FMA(state_vec, sa_vec, b_vec); |
11076 | 0 | GGML_F32_VEC_STORE(&state_cur[h_2d_i_j_offset], state_vec); |
11077 | |
|
11078 | 0 | result_vec[kk] = GGML_F32_VEC_FMA(result_vec[kk], state_vec, r_vec); |
11079 | 0 | } |
11080 | 0 | } |
11081 | 0 | GGML_F32_VEC_REDUCE(dst_data[t_h_i_offset], result_vec); |
11082 | | |
11083 | | // There shouldn't be left-overs though. |
11084 | 0 | for (; j < head_size; j++) { |
11085 | 0 | int64_t t_h_j_offset = t_h_offset + j; |
11086 | 0 | int64_t h_2d_i_j_offset = h_2d_i_offset + j; |
11087 | |
|
11088 | 0 | float r_val = r[t_h_j_offset]; |
11089 | 0 | float w_val = w[t_h_j_offset]; |
11090 | 0 | float k_val = k[t_h_j_offset]; |
11091 | 0 | float b_val = b[t_h_j_offset]; |
11092 | 0 | float kv_val = v[t_h_i_offset] * k_val; |
11093 | |
|
11094 | 0 | float prev_state_val = state_prev[h_2d_i_j_offset]; |
11095 | 0 | state_cur[h_2d_i_j_offset] = prev_state_val * w_val + kv_val + sa * b_val; |
11096 | 0 | dst_data[t_h_i_offset] += state_cur[h_2d_i_j_offset] * r_val; |
11097 | 0 | } |
11098 | 0 | } |
11099 | 0 | } |
11100 | 0 | } |
11101 | 0 | #endif |
11102 | | #else |
11103 | | for (int64_t t = 0; t < T; t++) { |
11104 | | int64_t t_offset = t * t_stride; |
11105 | | int64_t state_offset = head_size * C * (t / (T / n_seqs)); |
11106 | | float * state_cur = state + state_offset; |
11107 | | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[6]->data + state_offset; |
11108 | | |
11109 | | for (int64_t h = h_start; h < h_end; h++) { |
11110 | | int64_t h_offset = h * h_stride; |
11111 | | int64_t t_h_offset = t_offset + h_offset; |
11112 | | int64_t h_2d_offset = h * h_stride_2d; |
11113 | | |
11114 | | for (int64_t i = 0; i < head_size; i++) { |
11115 | | int64_t t_h_i_offset = t_h_offset + i; |
11116 | | int64_t h_2d_i_offset = h_2d_offset + i * h_stride; |
11117 | | |
11118 | | float v_val = v[t_h_i_offset]; |
11119 | | |
11120 | | float sa = 0, result = 0; |
11121 | | for (int64_t j = 0; j < head_size; j++) { |
11122 | | sa += a[t_h_offset + j] * state_prev[h_2d_i_offset + j]; |
11123 | | } |
11124 | | |
11125 | | for (int64_t j = 0; j < head_size; j++) { |
11126 | | int64_t t_h_j_offset = t_h_offset + j; |
11127 | | int64_t h_2d_i_j_offset = h_2d_i_offset + j; |
11128 | | |
11129 | | float r_val = r[t_h_j_offset]; |
11130 | | float w_val = w[t_h_j_offset]; |
11131 | | float k_val = k[t_h_j_offset]; |
11132 | | float b_val = b[t_h_j_offset]; |
11133 | | float kv_val = v_val * k_val; |
11134 | | float prev_state_val = state_prev[h_2d_i_j_offset]; |
11135 | | state_cur[h_2d_i_j_offset] = prev_state_val * w_val + kv_val + sa * b_val; |
11136 | | result += state_cur[h_2d_i_j_offset] * r_val; |
11137 | | } |
11138 | | dst_data[t_h_i_offset] = result; |
11139 | | } |
11140 | | } |
11141 | | } |
11142 | | #endif |
11143 | 0 | } |
11144 | | |
11145 | | |
11146 | | void ggml_compute_forward_rwkv_wkv7( |
11147 | | const ggml_compute_params * params, |
11148 | 0 | ggml_tensor * dst) { |
11149 | |
|
11150 | 0 | const ggml_tensor * src0 = dst->src[0]; |
11151 | |
|
11152 | 0 | switch (src0->type) { |
11153 | 0 | case GGML_TYPE_F32: |
11154 | 0 | { |
11155 | 0 | ggml_compute_forward_rwkv_wkv7_f32(params, dst); |
11156 | 0 | } break; |
11157 | 0 | default: |
11158 | 0 | { |
11159 | 0 | GGML_ABORT("fatal error"); |
11160 | 0 | } |
11161 | 0 | } |
11162 | 0 | } |
11163 | | |
11164 | | // ggml_compute_forward_map_custom1 |
11165 | | |
11166 | | void ggml_compute_forward_map_custom1( |
11167 | | const ggml_compute_params * params, |
11168 | 0 | ggml_tensor * dst) { |
11169 | |
|
11170 | 0 | const ggml_tensor * a = dst->src[0]; |
11171 | |
|
11172 | 0 | struct ggml_map_custom1_op_params p; |
11173 | 0 | memcpy(&p, dst->op_params, sizeof(p)); |
11174 | |
|
11175 | 0 | p.fun(dst, a, params->ith, params->nth, p.userdata); |
11176 | 0 | } |
11177 | | |
11178 | | // ggml_compute_forward_map_custom2 |
11179 | | |
11180 | | void ggml_compute_forward_map_custom2( |
11181 | | const ggml_compute_params * params, |
11182 | 0 | ggml_tensor * dst) { |
11183 | |
|
11184 | 0 | const ggml_tensor * a = dst->src[0]; |
11185 | 0 | const ggml_tensor * b = dst->src[1]; |
11186 | |
|
11187 | 0 | struct ggml_map_custom2_op_params p; |
11188 | 0 | memcpy(&p, dst->op_params, sizeof(p)); |
11189 | |
|
11190 | 0 | p.fun(dst, a, b, params->ith, params->nth, p.userdata); |
11191 | 0 | } |
11192 | | |
11193 | | // ggml_compute_forward_map_custom3 |
11194 | | |
11195 | | void ggml_compute_forward_map_custom3( |
11196 | | const ggml_compute_params * params, |
11197 | 0 | ggml_tensor * dst) { |
11198 | |
|
11199 | 0 | const ggml_tensor * a = dst->src[0]; |
11200 | 0 | const ggml_tensor * b = dst->src[1]; |
11201 | 0 | const ggml_tensor * c = dst->src[2]; |
11202 | |
|
11203 | 0 | struct ggml_map_custom3_op_params p; |
11204 | 0 | memcpy(&p, dst->op_params, sizeof(p)); |
11205 | |
|
11206 | 0 | p.fun(dst, a, b, c, params->ith, params->nth, p.userdata); |
11207 | 0 | } |
11208 | | |
11209 | | // ggml_compute_forward_custom |
11210 | | |
11211 | | void ggml_compute_forward_custom( |
11212 | | const struct ggml_compute_params * params, |
11213 | 0 | struct ggml_tensor * dst) { |
11214 | |
|
11215 | 0 | struct ggml_custom_op_params p; |
11216 | 0 | memcpy(&p, dst->op_params, sizeof(p)); |
11217 | |
|
11218 | 0 | p.fun(dst, params->ith, params->nth, p.userdata); |
11219 | 0 | } |
11220 | | |
11221 | | // ggml_compute_forward_cross_entropy_loss |
11222 | | |
11223 | | static void ggml_compute_forward_cross_entropy_loss_f32( |
11224 | | const ggml_compute_params * params, |
11225 | 0 | ggml_tensor * dst) { |
11226 | |
|
11227 | 0 | const ggml_tensor * src0 = dst->src[0]; |
11228 | 0 | const ggml_tensor * src1 = dst->src[1]; |
11229 | |
|
11230 | 0 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
11231 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
11232 | 0 | GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type)); |
11233 | 0 | GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type)); |
11234 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, src1)); |
11235 | 0 | GGML_ASSERT(ggml_is_scalar(dst)); |
11236 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
11237 | | |
11238 | | // TODO: handle transposed/permuted matrices |
11239 | 0 | const int64_t nc = src0->ne[0]; |
11240 | 0 | const int64_t nr = ggml_nrows(src0); |
11241 | |
|
11242 | 0 | const int ith = params->ith; |
11243 | 0 | const int nth = params->nth; |
11244 | |
|
11245 | 0 | float * sums = (float *) params->wdata; |
11246 | 0 | float * st = ((float *) params->wdata) + nth + ith*nc; |
11247 | 0 | float sum_thread = 0.0f; |
11248 | |
|
11249 | 0 | GGML_ASSERT(params->wsize >= sizeof(float) * (nth + nth * nc)); |
11250 | | |
11251 | | // rows per thread |
11252 | 0 | const int64_t dr = (nr + nth - 1)/nth; |
11253 | | |
11254 | | // row range for this thread |
11255 | 0 | const int64_t ir0 = dr*ith; |
11256 | 0 | const int64_t ir1 = MIN(ir0 + dr, nr); |
11257 | |
|
11258 | 0 | for (int64_t i1 = ir0; i1 < ir1; ++i1) { |
11259 | 0 | const float * s0 = (const float *)((const char *) src0->data + i1*src0->nb[1]); |
11260 | 0 | const float * s1 = (const float *)((const char *) src1->data + i1*src1->nb[1]); |
11261 | |
|
11262 | | #ifndef NDEBUG |
11263 | | for (int64_t i = 0; i < nc; ++i) { |
11264 | | //printf("p[%d] = %f\n", i, p[i]); |
11265 | | assert(!isnan(s0[i])); |
11266 | | assert(!isnan(s1[i])); |
11267 | | } |
11268 | | #endif // NDEBUG |
11269 | |
|
11270 | 0 | float max = -INFINITY; |
11271 | 0 | ggml_vec_max_f32(nc, &max, s0); |
11272 | 0 | const ggml_float sum_softmax = ggml_vec_log_soft_max_f32(nc, st, s0, max); |
11273 | 0 | assert(sum_softmax >= 0.0); |
11274 | |
|
11275 | 0 | ggml_vec_add1_f32(nc, st, st, -sum_softmax); |
11276 | 0 | ggml_vec_mul_f32(nc, st, st, s1); |
11277 | |
|
11278 | 0 | float sum_st = 0.0f; |
11279 | 0 | ggml_vec_sum_f32(nc, &sum_st, st); |
11280 | 0 | sum_thread += sum_st; |
11281 | |
|
11282 | | #ifndef NDEBUG |
11283 | | for (int64_t i = 0; i < nc; ++i) { |
11284 | | assert(!isnan(st[i])); |
11285 | | assert(!isinf(st[i])); |
11286 | | } |
11287 | | #endif // NDEBUG |
11288 | 0 | } |
11289 | 0 | sums[ith] = sum_thread; |
11290 | 0 | ggml_barrier(params->threadpool); |
11291 | |
|
11292 | 0 | if (ith == 0) { |
11293 | 0 | float * dp = (float *) dst->data; |
11294 | 0 | ggml_vec_sum_f32(nth, dp, sums); |
11295 | 0 | dp[0] *= -1.0f / (float) nr; |
11296 | 0 | } |
11297 | 0 | } |
11298 | | |
11299 | | void ggml_compute_forward_cross_entropy_loss( |
11300 | | const ggml_compute_params * params, |
11301 | 0 | ggml_tensor * dst) { |
11302 | |
|
11303 | 0 | const ggml_tensor * src0 = dst->src[0]; |
11304 | |
|
11305 | 0 | switch (src0->type) { |
11306 | 0 | case GGML_TYPE_F32: |
11307 | 0 | { |
11308 | 0 | ggml_compute_forward_cross_entropy_loss_f32(params, dst); |
11309 | 0 | } break; |
11310 | 0 | default: |
11311 | 0 | { |
11312 | 0 | GGML_ABORT("fatal error"); |
11313 | 0 | } |
11314 | 0 | } |
11315 | 0 | } |
11316 | | |
11317 | | // ggml_compute_forward_cross_entropy_loss_back |
11318 | | |
11319 | | static void ggml_compute_forward_cross_entropy_loss_back_f32( |
11320 | | const ggml_compute_params * params, |
11321 | 0 | ggml_tensor * dst) { |
11322 | |
|
11323 | 0 | const ggml_tensor * grad = dst->src[0]; // gradient of forward pass output |
11324 | 0 | const ggml_tensor * src0f = dst->src[1]; // src0 of forward pass |
11325 | 0 | const ggml_tensor * src1f = dst->src[2]; // src1 of forward pass |
11326 | |
|
11327 | 0 | GGML_ASSERT(ggml_is_contiguous(dst)); |
11328 | 0 | GGML_ASSERT(ggml_is_contiguous(src0f)); |
11329 | 0 | GGML_ASSERT(ggml_is_contiguous(src1f)); |
11330 | 0 | GGML_ASSERT(ggml_is_contiguous(grad)); |
11331 | 0 | GGML_ASSERT(ggml_are_same_shape(src0f, src1f) && ggml_are_same_shape(src0f, dst)); |
11332 | |
|
11333 | 0 | const int64_t ith = params->ith; |
11334 | 0 | const int64_t nth = params->nth; |
11335 | | |
11336 | | // TODO: handle transposed/permuted matrices |
11337 | 0 | const int64_t nc = src0f->ne[0]; |
11338 | 0 | const int64_t nr = ggml_nrows(src0f); |
11339 | | |
11340 | | // rows per thread |
11341 | 0 | const int64_t dr = (nr + nth - 1)/nth; |
11342 | | |
11343 | | // row range for this thread |
11344 | 0 | const int64_t ir0 = dr*ith; |
11345 | 0 | const int64_t ir1 = MIN(ir0 + dr, nr); |
11346 | |
|
11347 | 0 | const float d_by_nr = ((const float *) grad->data)[0] / (float) nr; |
11348 | |
|
11349 | 0 | for (int64_t i1 = ir0; i1 < ir1; i1++) { |
11350 | 0 | float * ds0 = (float *)((char *) dst->data + i1*dst->nb[1]); |
11351 | 0 | const float * s0 = (const float *)((const char *) src0f->data + i1*src0f->nb[1]); |
11352 | 0 | const float * s1 = (const float *)((const char *) src1f->data + i1*src1f->nb[1]); |
11353 | |
|
11354 | | #ifndef NDEBUG |
11355 | | for (int64_t i = 0; i < nc; ++i) { |
11356 | | //printf("p[%d] = %f\n", i, p[i]); |
11357 | | assert(!isnan(s0[i])); |
11358 | | assert(!isnan(s1[i])); |
11359 | | } |
11360 | | #endif // NDEBUG |
11361 | | |
11362 | | // soft_max |
11363 | 0 | float max = -INFINITY; |
11364 | 0 | ggml_vec_max_f32(nc, &max, s0); |
11365 | 0 | const ggml_float sum = ggml_vec_soft_max_f32(nc, ds0, s0, max); |
11366 | 0 | assert(sum > 0.0); |
11367 | 0 | ggml_vec_scale_f32(nc, ds0, 1.0/sum); |
11368 | | |
11369 | | // grad(src0f) = (softmax(src0f) - src1f) * grad(cross_entropy_loss(src0f, src1f)) / nr |
11370 | 0 | ggml_vec_sub_f32(nc, ds0, ds0, s1); |
11371 | 0 | ggml_vec_scale_f32(nc, ds0, d_by_nr); |
11372 | |
|
11373 | | #ifndef NDEBUG |
11374 | | for (int64_t i = 0; i < nc; ++i) { |
11375 | | assert(!isnan(ds0[i])); |
11376 | | assert(!isinf(ds0[i])); |
11377 | | } |
11378 | | #endif // NDEBUG |
11379 | 0 | } |
11380 | 0 | } |
11381 | | |
11382 | | void ggml_compute_forward_cross_entropy_loss_back( |
11383 | | const ggml_compute_params * params, |
11384 | 0 | ggml_tensor * dst) { |
11385 | |
|
11386 | 0 | const ggml_tensor * src0 = dst->src[0]; |
11387 | |
|
11388 | 0 | switch (src0->type) { |
11389 | 0 | case GGML_TYPE_F32: |
11390 | 0 | { |
11391 | 0 | ggml_compute_forward_cross_entropy_loss_back_f32(params, dst); |
11392 | 0 | } break; |
11393 | 0 | default: |
11394 | 0 | { |
11395 | 0 | GGML_ABORT("fatal error"); |
11396 | 0 | } |
11397 | 0 | } |
11398 | 0 | } |
11399 | | |
11400 | | static void ggml_compute_forward_opt_step_adamw_f32( |
11401 | | const ggml_compute_params * params, |
11402 | 0 | ggml_tensor * dst) { |
11403 | |
|
11404 | 0 | const ggml_tensor * src0 = dst->src[0]; |
11405 | 0 | const ggml_tensor * src0_grad = dst->src[1]; |
11406 | 0 | const ggml_tensor * src0_grad_m = dst->src[2]; |
11407 | 0 | const ggml_tensor * src0_grad_v = dst->src[3]; |
11408 | 0 | const ggml_tensor * adamw_params = dst->src[4]; |
11409 | |
|
11410 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, src0_grad)); |
11411 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_m)); |
11412 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_v)); |
11413 | 0 | GGML_ASSERT(ggml_nelements(adamw_params) == 7); |
11414 | |
|
11415 | 0 | const int ith = params->ith; |
11416 | 0 | const int nth = params->nth; |
11417 | |
|
11418 | 0 | const int nr = ggml_nrows(src0); |
11419 | |
|
11420 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
11421 | 0 | GGML_ASSERT(nb00 == sizeof(float)); |
11422 | | |
11423 | | // rows per thread |
11424 | 0 | const int dr = (nr + nth - 1)/nth; |
11425 | | |
11426 | | // row range for this thread |
11427 | 0 | const int ir0 = dr*ith; |
11428 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
11429 | |
|
11430 | 0 | const float * adamw_params_ptr = ggml_get_data_f32(adamw_params); |
11431 | |
|
11432 | 0 | const float alpha = adamw_params_ptr[0]; |
11433 | 0 | const float beta1 = adamw_params_ptr[1]; |
11434 | 0 | const float beta2 = adamw_params_ptr[2]; |
11435 | 0 | const float eps = adamw_params_ptr[3]; |
11436 | 0 | const float wd = adamw_params_ptr[4]; |
11437 | 0 | const float beta1h = adamw_params_ptr[5]; |
11438 | 0 | const float beta2h = adamw_params_ptr[6]; |
11439 | 0 | const float keep = 1.f - alpha * wd; |
11440 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
11441 | 0 | const int64_t i03 = ir/(ne02*ne01); |
11442 | 0 | const int64_t i02 = (ir - i03*ne02*ne01)/ne01; |
11443 | 0 | const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01); |
11444 | |
|
11445 | 0 | const size_t offset = i03*nb03 + i02*nb02 + i01*nb01; |
11446 | |
|
11447 | 0 | float * w = (float *) ((char *) src0->data + offset); // weight |
11448 | 0 | const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad |
11449 | 0 | float * m = (float *) ((char *) src0_grad_m->data + offset); |
11450 | 0 | float * v = (float *) ((char *) src0_grad_v->data + offset); |
11451 | |
|
11452 | 0 | for (int i00 = 0; i00 < ne00; ++i00) { |
11453 | 0 | m[i00] = m[i00]*beta1 + g[i00]*(1.0f - beta1); |
11454 | 0 | v[i00] = v[i00]*beta2 + g[i00]*g[i00]*(1.0f - beta2); |
11455 | |
|
11456 | 0 | const float mh = m[i00]*beta1h; |
11457 | 0 | const float vh = sqrtf(v[i00]*beta2h) + eps; |
11458 | | |
11459 | | // The weight decay is applied independently of the Adam momenta m and v. |
11460 | | // This is NOT equivalent to l2 regularization that adds w[i00]*w[i00] to the loss. |
11461 | | // See: https://arxiv.org/pdf/1711.05101v3.pdf |
11462 | 0 | w[i00] = w[i00] * keep - alpha * mh / vh; |
11463 | 0 | } |
11464 | 0 | } |
11465 | 0 | } |
11466 | | |
11467 | | void ggml_compute_forward_opt_step_adamw( |
11468 | | const ggml_compute_params * params, |
11469 | 0 | ggml_tensor * dst) { |
11470 | |
|
11471 | 0 | const ggml_tensor * src0 = dst->src[0]; |
11472 | |
|
11473 | 0 | switch (src0->type) { |
11474 | 0 | case GGML_TYPE_F32: |
11475 | 0 | { |
11476 | 0 | ggml_compute_forward_opt_step_adamw_f32(params, dst); |
11477 | 0 | } break; |
11478 | 0 | default: |
11479 | 0 | { |
11480 | 0 | GGML_ABORT("fatal error"); |
11481 | 0 | } |
11482 | 0 | } |
11483 | 0 | } |
11484 | | |
11485 | 0 | static void ggml_compute_forward_opt_step_sgd_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
11486 | 0 | const ggml_tensor * src0 = dst->src[0]; |
11487 | 0 | const ggml_tensor * src0_grad = dst->src[1]; |
11488 | 0 | const ggml_tensor * sgd_params = dst->src[2]; |
11489 | |
|
11490 | 0 | GGML_ASSERT(ggml_are_same_shape(src0, src0_grad)); |
11491 | 0 | GGML_ASSERT(ggml_nelements(sgd_params) == 2); |
11492 | |
|
11493 | 0 | const int ith = params->ith; |
11494 | 0 | const int nth = params->nth; |
11495 | |
|
11496 | 0 | const int nr = ggml_nrows(src0); |
11497 | |
|
11498 | 0 | GGML_TENSOR_UNARY_OP_LOCALS |
11499 | 0 | GGML_ASSERT(nb00 == sizeof(float)); |
11500 | | |
11501 | | // rows per thread |
11502 | 0 | const int dr = (nr + nth - 1) / nth; |
11503 | | |
11504 | | // row range for this thread |
11505 | 0 | const int ir0 = dr * ith; |
11506 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
11507 | | |
11508 | | // using adamw param subset we care about - alpha, wd - could have a separate struct |
11509 | 0 | const float * sgd_params_ptr = ggml_get_data_f32(sgd_params); |
11510 | 0 | const float alpha = sgd_params_ptr[0]; |
11511 | 0 | const float keep = 1.f - alpha * sgd_params_ptr[1]; |
11512 | |
|
11513 | 0 | for (int ir = ir0; ir < ir1; ++ir) { |
11514 | 0 | const int64_t i03 = ir / (ne02 * ne01); |
11515 | 0 | const int64_t i02 = (ir - i03 * ne02 * ne01) / ne01; |
11516 | 0 | const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01); |
11517 | |
|
11518 | 0 | const size_t offset = i03 * nb03 + i02 * nb02 + i01 * nb01; |
11519 | |
|
11520 | 0 | float * w = (float *) ((char *) src0->data + offset); // weight |
11521 | 0 | const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad |
11522 | |
|
11523 | 0 | for (int i00 = 0; i00 < ne00; ++i00) { |
11524 | 0 | w[i00] = w[i00] * keep - alpha * g[i00]; |
11525 | 0 | } |
11526 | 0 | } |
11527 | 0 | } |
11528 | | |
11529 | 0 | void ggml_compute_forward_opt_step_sgd(const ggml_compute_params * params, ggml_tensor * dst) { |
11530 | 0 | const ggml_tensor * src0 = dst->src[0]; |
11531 | |
|
11532 | 0 | switch (src0->type) { |
11533 | 0 | case GGML_TYPE_F32: |
11534 | 0 | { |
11535 | 0 | ggml_compute_forward_opt_step_sgd_f32(params, dst); |
11536 | 0 | } |
11537 | 0 | break; |
11538 | 0 | default: |
11539 | 0 | { |
11540 | 0 | GGML_ABORT("fatal error - sgd is F32 only"); |
11541 | 0 | } |
11542 | 0 | } |
11543 | 0 | } |
11544 | | |
11545 | 0 | static void ggml_compute_forward_fwht_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
11546 | 0 | const ggml_tensor * src0 = dst->src[0]; |
11547 | 0 | const ggml_tensor * src1 = dst->src[1]; |
11548 | |
|
11549 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
11550 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
11551 | |
|
11552 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
11553 | |
|
11554 | 0 | const int ith = params->ith; |
11555 | 0 | const int nth = params->nth; |
11556 | |
|
11557 | 0 | const int64_t n = ne10; |
11558 | 0 | GGML_ASSERT((n & (n - 1)) == 0); // must be power of 2 |
11559 | |
|
11560 | 0 | const int64_t nr = ne11 * ne12 * ne13; |
11561 | 0 | const int64_t rows_per_thread = (nr + nth - 1) / nth; |
11562 | 0 | const int64_t start_row = ith * rows_per_thread; |
11563 | 0 | const int64_t end_row = MIN(start_row + rows_per_thread, nr); |
11564 | |
|
11565 | 0 | const float scale = 1.0f / sqrtf((float)n); |
11566 | |
|
11567 | 0 | #if defined(GGML_SIMD) |
11568 | 0 | const GGML_F32_VEC v_minus_one = GGML_F32_VEC_SET1(-1.0f); |
11569 | 0 | #endif |
11570 | |
|
11571 | 0 | for (int64_t r = start_row; r < end_row; r++) { |
11572 | 0 | const int64_t i13 = r / (ne11 * ne12); |
11573 | 0 | const int64_t i12 = (r - i13 * ne11 * ne12) / ne11; |
11574 | 0 | const int64_t i11 = r - i13 * ne11 * ne12 - i12 * ne11; |
11575 | |
|
11576 | 0 | const float * src_row = (const float *) ((const char *) src1->data + i11 * nb11 + i12 * nb12 + i13 * nb13); |
11577 | 0 | float * dst_row = (float *) ((char *) dst->data + i11 * nb1 + i12 * nb2 + i13 * nb3); |
11578 | |
|
11579 | 0 | for (int64_t j = 0; j < n; j++) { |
11580 | 0 | dst_row[j] = src_row[j] * scale; |
11581 | 0 | } |
11582 | | |
11583 | | // Scalar passes |
11584 | 0 | #if defined(GGML_SIMD) |
11585 | | #if defined(__ARM_FEATURE_SVE) |
11586 | | const int step = svcntw(); |
11587 | | #else |
11588 | 0 | const int step = GGML_F32_EPR; |
11589 | 0 | #endif |
11590 | | #else |
11591 | | const int step = n; |
11592 | | #endif |
11593 | 0 | for (int64_t len = 1; len < step && len < n; len <<= 1) { |
11594 | 0 | for (int64_t i = 0; i < n; i += 2 * len) { |
11595 | 0 | for (int64_t j = 0; j < len; j++) { |
11596 | 0 | float u = dst_row[i + j]; |
11597 | 0 | float v = dst_row[i + len + j]; |
11598 | 0 | dst_row[i + j] = u + v; |
11599 | 0 | dst_row[i + len + j] = u - v; |
11600 | 0 | } |
11601 | 0 | } |
11602 | 0 | } |
11603 | | |
11604 | | // SIMD passes using GGML_F32_VEC_* macros for multi-architecture support |
11605 | 0 | #if defined(GGML_SIMD) |
11606 | 0 | for (int64_t len = step; len < n; len <<= 1) { |
11607 | 0 | for (int64_t i = 0; i < n; i += 2 * len) { |
11608 | 0 | for (int64_t j = 0; j < len; j += step) { |
11609 | 0 | GGML_F32_VEC u = GGML_F32_VEC_LOAD(dst_row + i + j); |
11610 | 0 | GGML_F32_VEC v = GGML_F32_VEC_LOAD(dst_row + i + len + j); |
11611 | |
|
11612 | 0 | GGML_F32_VEC_STORE(dst_row + i + j, GGML_F32_VEC_ADD(u, v)); |
11613 | 0 | GGML_F32_VEC_STORE(dst_row + i + len + j, GGML_F32_VEC_FMA(u, v, v_minus_one)); |
11614 | 0 | } |
11615 | 0 | } |
11616 | 0 | } |
11617 | 0 | #endif |
11618 | 0 | } |
11619 | 0 | } |
11620 | | |
11621 | 0 | void ggml_compute_forward_fwht(const ggml_compute_params * params, ggml_tensor * dst) { |
11622 | 0 | const ggml_tensor * src1 = dst->src[1]; |
11623 | |
|
11624 | 0 | switch (src1->type) { |
11625 | 0 | case GGML_TYPE_F32: |
11626 | 0 | { |
11627 | 0 | ggml_compute_forward_fwht_f32(params, dst); |
11628 | 0 | } |
11629 | 0 | break; |
11630 | 0 | default: |
11631 | 0 | { |
11632 | 0 | GGML_ABORT("fatal error - fwht is F32 only"); |
11633 | 0 | } |
11634 | 0 | } |
11635 | 0 | } |
11636 | | |
11637 | | // ggml_compute_forward_lightning_indexer |
11638 | | |
11639 | | void ggml_compute_forward_lightning_indexer( |
11640 | | const ggml_compute_params * params, |
11641 | 0 | ggml_tensor * dst) { |
11642 | |
|
11643 | 0 | const ggml_tensor * q = dst->src[0]; |
11644 | 0 | const ggml_tensor * k = dst->src[1]; |
11645 | 0 | const ggml_tensor * w = dst->src[2]; // weights |
11646 | 0 | const ggml_tensor * m = dst->src[3]; // mask |
11647 | |
|
11648 | 0 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
11649 | 0 | GGML_ASSERT( q->type == GGML_TYPE_F32); |
11650 | 0 | GGML_ASSERT( w->type == GGML_TYPE_F32); |
11651 | 0 | GGML_ASSERT( m->type == GGML_TYPE_F16); |
11652 | |
|
11653 | 0 | GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
11654 | 0 | GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
11655 | 0 | GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
11656 | 0 | GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
11657 | 0 | GGML_TENSOR_LOCALS(int64_t, new, w, ne) |
11658 | 0 | GGML_TENSOR_LOCALS(size_t, nbw, w, nb) |
11659 | 0 | GGML_TENSOR_LOCALS(int64_t, nem, m, ne) |
11660 | 0 | GGML_TENSOR_LOCALS(size_t, nbm, m, nb) |
11661 | 0 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
11662 | 0 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
11663 | |
|
11664 | 0 | GGML_ASSERT( nb0 == ggml_type_size(dst->type)); |
11665 | 0 | GGML_ASSERT(nbq0 == ggml_type_size( q->type)); |
11666 | 0 | GGML_ASSERT(nbk0 == ggml_type_size( k->type)); |
11667 | 0 | GGML_ASSERT(nbw0 == ggml_type_size( w->type)); |
11668 | 0 | GGML_ASSERT(nbm0 == ggml_type_size( m->type)); |
11669 | |
|
11670 | 0 | const int n_embd = q->ne[0]; |
11671 | 0 | const int n_head = q->ne[1]; |
11672 | 0 | const int n_tokens = q->ne[2]; |
11673 | 0 | const int n_stream = q->ne[3]; |
11674 | 0 | const int n_kv = k->ne[2]; |
11675 | |
|
11676 | 0 | ggml_to_float_t const k_to_float = ggml_get_type_traits(k->type)->to_float; |
11677 | 0 | GGML_ASSERT((k->type == GGML_TYPE_F32 || k_to_float) && "lightning indexer: unsupported K-type"); |
11678 | |
|
11679 | 0 | const int nr = n_kv; |
11680 | 0 | const int ith = params->ith; |
11681 | 0 | const int nth = params->nth; |
11682 | | |
11683 | | // (temporary) buffer for K converted to float |
11684 | 0 | float * k_row_f32 = (float *) params->wdata + ith*(1*n_embd + CACHE_LINE_SIZE_F32); |
11685 | | |
11686 | | // rows per thread |
11687 | 0 | const int dr = (nr + nth - 1)/nth; |
11688 | | |
11689 | | // row range for this thread |
11690 | 0 | const int ir0 = dr*ith; |
11691 | 0 | const int ir1 = MIN(ir0 + dr, nr); |
11692 | |
|
11693 | 0 | for (int s = 0; s < n_stream; ++s) { |
11694 | 0 | for (int t = 0; t < n_tokens; ++t) { |
11695 | 0 | const float * w_row = (float *) ((char *) w->data + t*nbw1 + s*nbw3); |
11696 | 0 | const ggml_fp16_t * m_row = (ggml_fp16_t *) ((char *) m->data + t*nbm1 + (s%nem3)*nbm3); |
11697 | 0 | float * dst_row = (float *) ((char *) dst->data + t*nb1 + s*nb3 ); |
11698 | 0 | for (int ik = ir0; ik < ir1; ++ik) { |
11699 | 0 | char * k_row = (char *) k->data + ik*nbk2 + s*nbk3; |
11700 | 0 | if (k_to_float) { |
11701 | 0 | k_to_float(k_row, k_row_f32, n_embd); |
11702 | 0 | } else { |
11703 | 0 | k_row_f32 = (float *) k_row; |
11704 | 0 | } |
11705 | 0 | float score = 0.0f; |
11706 | 0 | for (int h = 0; h < n_head; ++h) { |
11707 | | // dot product of q and k for head h |
11708 | 0 | float qk = 0.0f; |
11709 | 0 | const float * q_row = (float *) ((char *) q->data + h*nbq1 + t*nbq2 + s*nbq3); |
11710 | 0 | ggml_vec_dot_f32(n_embd, &qk, 0, q_row, 0, k_row_f32, 0, 1); |
11711 | | // ReLU and weights (prescaled) |
11712 | 0 | score += MAX(qk, 0.0f) * w_row[h]; |
11713 | 0 | } |
11714 | | // apply mask |
11715 | 0 | dst_row[ik] = score + GGML_CPU_FP16_TO_FP32(m_row[ik]); |
11716 | 0 | } |
11717 | 0 | } |
11718 | 0 | } |
11719 | 0 | } |