/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c
Line | Count | Source |
1 | | #define _CRT_SECURE_NO_DEPRECATE // Disables "unsafe" warnings on Windows |
2 | | #define _USE_MATH_DEFINES // For M_PI on MSVC |
3 | | |
4 | | #include "ggml-backend-impl.h" |
5 | | #include "ggml-backend.h" |
6 | | #include "traits.h" |
7 | | #include "ggml-cpu-impl.h" |
8 | | #include "ggml-impl.h" |
9 | | #include "quants.h" |
10 | | #include "ggml-threading.h" |
11 | | #include "unary-ops.h" |
12 | | #include "binary-ops.h" |
13 | | #include "vec.h" |
14 | | #include "ops.h" |
15 | | #include "ggml.h" |
16 | | #include "common.h" |
17 | | |
18 | | #if defined(_MSC_VER) || defined(__MINGW32__) |
19 | | #include <malloc.h> // using malloc.h with MSC/MINGW |
20 | | #elif !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) |
21 | | #include <alloca.h> |
22 | | #endif |
23 | | |
24 | | #include <assert.h> |
25 | | #include <errno.h> |
26 | | #include <time.h> |
27 | | #include <math.h> |
28 | | #include <stdlib.h> |
29 | | #include <string.h> |
30 | | #include <stdint.h> |
31 | | #include <inttypes.h> |
32 | | #include <stdio.h> |
33 | | #include <float.h> |
34 | | #include <limits.h> |
35 | | #include <stdarg.h> |
36 | | #include <signal.h> |
37 | | #if defined(__gnu_linux__) |
38 | | #include <syscall.h> |
39 | | #endif |
40 | | |
41 | | #ifdef GGML_USE_OPENMP |
42 | | #include <omp.h> |
43 | | #endif |
44 | | |
45 | | #if defined(__ARM_FEATURE_SVE) || defined(__ARM_FEATURE_MATMUL_INT8) |
46 | | #undef GGML_USE_LLAMAFILE |
47 | | #endif |
48 | | |
49 | | #ifdef GGML_USE_LLAMAFILE |
50 | | #include "llamafile/sgemm.h" |
51 | | #endif |
52 | | |
53 | | #ifdef GGML_USE_CPU_RISCV64_SPACEMIT |
54 | | # include "spacemit/ime.h" |
55 | | #endif |
56 | | |
57 | | // Note: once we move threading into a separate C++ file |
58 | | // will use std::hardware_destructive_interference_size instead of hardcoding it here |
59 | | // and we'll use C++ attribute syntax. |
60 | | #define GGML_CACHE_LINE 64 |
61 | | |
62 | | #if defined(__clang__) || defined(__GNUC__) |
63 | | #define GGML_CACHE_ALIGN __attribute__((aligned(GGML_CACHE_LINE))) |
64 | | #endif |
65 | | |
66 | | #if defined(__has_feature) |
67 | | #if __has_feature(thread_sanitizer) |
68 | | #define GGML_TSAN_ENABLED 1 |
69 | | #endif |
70 | | #else // __has_feature |
71 | | #if defined(__SANITIZE_THREAD__) |
72 | | #define GGML_TSAN_ENABLED 1 |
73 | | #endif |
74 | | #endif // __has_feature |
75 | | |
76 | 6 | #define UNUSED GGML_UNUSED |
77 | | #define SWAP(x, y, T) do { T SWAP = x; (x) = y; (y) = SWAP; } while (0) |
78 | | |
79 | | // precomputed f32 table for f16 (256 KB) (simd-mappings.h) |
80 | | float ggml_table_f32_f16[1 << 16]; |
81 | | |
82 | | // precomputed f32 table for e8m0 half (1 KB) (simd-mappings.h) |
83 | | float ggml_table_f32_e8m0_half[1 << 8]; |
84 | | |
85 | | #if defined(__ARM_ARCH) |
86 | | struct ggml_arm_arch_features_type { |
87 | | int sve_cnt; |
88 | | } ggml_arm_arch_features = { 0 }; |
89 | | #endif |
90 | | |
91 | | #if defined(__riscv) |
92 | | struct ggml_riscv_arch_features_type { |
93 | | int rvv_vlen; |
94 | | } ggml_riscv_arch_features = { 0 }; |
95 | | #endif |
96 | | |
97 | | #if defined(_WIN32) |
98 | | |
99 | | #define WIN32_LEAN_AND_MEAN |
100 | | #ifndef NOMINMAX |
101 | | #define NOMINMAX |
102 | | #endif |
103 | | #include <windows.h> |
104 | | |
105 | | #if defined(_MSC_VER) && !defined(__clang__) |
106 | | #define GGML_CACHE_ALIGN __declspec(align(GGML_CACHE_LINE)) |
107 | | |
108 | | typedef volatile LONG atomic_int; |
109 | | typedef atomic_int atomic_bool; |
110 | | typedef atomic_int atomic_flag; |
111 | | |
112 | | #define ATOMIC_FLAG_INIT 0 |
113 | | |
114 | | typedef enum { |
115 | | memory_order_relaxed, |
116 | | memory_order_consume, |
117 | | memory_order_acquire, |
118 | | memory_order_release, |
119 | | memory_order_acq_rel, |
120 | | memory_order_seq_cst |
121 | | } memory_order; |
122 | | |
123 | | static void atomic_store(atomic_int * ptr, LONG val) { |
124 | | InterlockedExchange(ptr, val); |
125 | | } |
126 | | static void atomic_store_explicit(atomic_int * ptr, LONG val, memory_order mo) { |
127 | | // TODO: add support for explicit memory order |
128 | | InterlockedExchange(ptr, val); |
129 | | } |
130 | | static LONG atomic_load(atomic_int * ptr) { |
131 | | return InterlockedCompareExchange(ptr, 0, 0); |
132 | | } |
133 | | static LONG atomic_load_explicit(atomic_int * ptr, memory_order mo) { |
134 | | // TODO: add support for explicit memory order |
135 | | return InterlockedCompareExchange(ptr, 0, 0); |
136 | | } |
137 | | static LONG atomic_fetch_add(atomic_int * ptr, LONG inc) { |
138 | | return InterlockedExchangeAdd(ptr, inc); |
139 | | } |
140 | | static LONG atomic_fetch_add_explicit(atomic_int * ptr, LONG inc, memory_order mo) { |
141 | | // TODO: add support for explicit memory order |
142 | | return InterlockedExchangeAdd(ptr, inc); |
143 | | } |
144 | | static atomic_bool atomic_flag_test_and_set(atomic_flag * ptr) { |
145 | | return InterlockedExchange(ptr, 1); |
146 | | } |
147 | | static void atomic_flag_clear(atomic_flag * ptr) { |
148 | | InterlockedExchange(ptr, 0); |
149 | | } |
150 | | static void atomic_thread_fence(memory_order mo) { |
151 | | MemoryBarrier(); |
152 | | } |
153 | | #else // clang |
154 | | #include <stdatomic.h> |
155 | | #endif |
156 | | |
157 | | typedef HANDLE pthread_t; |
158 | | |
159 | | typedef DWORD thread_ret_t; |
160 | | static int pthread_create(pthread_t * out, void * unused, thread_ret_t(*func)(void *), void * arg) { |
161 | | (void) unused; |
162 | | HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL); |
163 | | if (handle == NULL) |
164 | | { |
165 | | return EAGAIN; |
166 | | } |
167 | | |
168 | | *out = handle; |
169 | | return 0; |
170 | | } |
171 | | |
172 | | static int pthread_join(pthread_t thread, void * unused) { |
173 | | (void) unused; |
174 | | int ret = (int) WaitForSingleObject(thread, INFINITE); |
175 | | CloseHandle(thread); |
176 | | return ret; |
177 | | } |
178 | | |
179 | | static int sched_yield (void) { |
180 | | Sleep (0); |
181 | | return 0; |
182 | | } |
183 | | #else |
184 | | |
185 | | #include <pthread.h> |
186 | | #include <stdatomic.h> |
187 | | #include <sched.h> |
188 | | #if defined(__FreeBSD__) |
189 | | #include <pthread_np.h> |
190 | | #endif |
191 | | |
192 | | typedef void * thread_ret_t; |
193 | | |
194 | | #include <sys/types.h> |
195 | | #include <sys/stat.h> |
196 | | #include <unistd.h> |
197 | | |
198 | | #endif |
199 | | |
200 | | typedef pthread_t ggml_thread_t; |
201 | | |
202 | 0 | #define GGML_THREADPOOL_N_THREADS_MASK (0xffffU) |
203 | 0 | #define GGML_THREADPOOL_N_THREADS_BITS (16) |
204 | | |
205 | | #if defined(__APPLE__) |
206 | | #include <unistd.h> |
207 | | #include <mach/mach.h> |
208 | | #include <TargetConditionals.h> |
209 | | #endif |
210 | | |
211 | | static const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT] = { |
212 | | [GGML_TYPE_F32] = { |
213 | | .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_fp32, |
214 | | .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f32, |
215 | | .vec_dot_type = GGML_TYPE_F32, |
216 | | .nrows = 1, |
217 | | }, |
218 | | [GGML_TYPE_F16] = { |
219 | | .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_fp16, |
220 | | .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f16, |
221 | | .vec_dot_type = GGML_TYPE_F16, |
222 | | .nrows = 1, |
223 | | }, |
224 | | [GGML_TYPE_Q1_0] = { |
225 | | .from_float = quantize_row_q1_0, |
226 | | .vec_dot = ggml_vec_dot_q1_0_q8_0, |
227 | | .vec_dot_type = GGML_TYPE_Q8_0, |
228 | | .nrows = 1, |
229 | | }, |
230 | | [GGML_TYPE_Q4_0] = { |
231 | | .from_float = quantize_row_q4_0, |
232 | | .vec_dot = ggml_vec_dot_q4_0_q8_0, |
233 | | .vec_dot_type = GGML_TYPE_Q8_0, |
234 | | #if defined (__ARM_FEATURE_MATMUL_INT8) |
235 | | .nrows = 2, |
236 | | #else |
237 | | .nrows = 1, |
238 | | #endif |
239 | | }, |
240 | | [GGML_TYPE_Q4_1] = { |
241 | | .from_float = quantize_row_q4_1, |
242 | | .vec_dot = ggml_vec_dot_q4_1_q8_1, |
243 | | .vec_dot_type = GGML_TYPE_Q8_1, |
244 | | #if defined (__ARM_FEATURE_MATMUL_INT8) |
245 | | .nrows = 2, |
246 | | #else |
247 | | .nrows = 1, |
248 | | #endif |
249 | | }, |
250 | | [GGML_TYPE_Q5_0] = { |
251 | | .from_float = quantize_row_q5_0, |
252 | | .vec_dot = ggml_vec_dot_q5_0_q8_0, |
253 | | .vec_dot_type = GGML_TYPE_Q8_0, |
254 | | .nrows = 1, |
255 | | }, |
256 | | [GGML_TYPE_Q5_1] = { |
257 | | .from_float = quantize_row_q5_1, |
258 | | .vec_dot = ggml_vec_dot_q5_1_q8_1, |
259 | | .vec_dot_type = GGML_TYPE_Q8_1, |
260 | | .nrows = 1, |
261 | | }, |
262 | | [GGML_TYPE_Q8_0] = { |
263 | | .from_float = quantize_row_q8_0, |
264 | | .vec_dot = ggml_vec_dot_q8_0_q8_0, |
265 | | .vec_dot_type = GGML_TYPE_Q8_0, |
266 | | #if defined (__ARM_FEATURE_MATMUL_INT8) |
267 | | .nrows = 2, |
268 | | #else |
269 | | .nrows = 1, |
270 | | #endif |
271 | | }, |
272 | | [GGML_TYPE_Q8_1] = { |
273 | | .from_float = quantize_row_q8_1, |
274 | | .vec_dot_type = GGML_TYPE_Q8_1, |
275 | | .nrows = 1, |
276 | | }, |
277 | | [GGML_TYPE_MXFP4] = { |
278 | | .from_float = quantize_row_mxfp4, |
279 | | .vec_dot = ggml_vec_dot_mxfp4_q8_0, |
280 | | .vec_dot_type = GGML_TYPE_Q8_0, |
281 | | .nrows = 1, |
282 | | }, |
283 | | [GGML_TYPE_NVFP4] = { |
284 | | .from_float = quantize_row_nvfp4, |
285 | | .vec_dot = ggml_vec_dot_nvfp4_q8_0, |
286 | | .vec_dot_type = GGML_TYPE_Q8_0, |
287 | | .nrows = 1, |
288 | | }, |
289 | | [GGML_TYPE_Q2_K] = { |
290 | | .from_float = quantize_row_q2_K, |
291 | | .vec_dot = ggml_vec_dot_q2_K_q8_K, |
292 | | .vec_dot_type = GGML_TYPE_Q8_K, |
293 | | .nrows = 1, |
294 | | }, |
295 | | [GGML_TYPE_Q3_K] = { |
296 | | .from_float = quantize_row_q3_K, |
297 | | .vec_dot = ggml_vec_dot_q3_K_q8_K, |
298 | | .vec_dot_type = GGML_TYPE_Q8_K, |
299 | | .nrows = 1, |
300 | | }, |
301 | | [GGML_TYPE_Q4_K] = { |
302 | | .from_float = quantize_row_q4_K, |
303 | | .vec_dot = ggml_vec_dot_q4_K_q8_K, |
304 | | .vec_dot_type = GGML_TYPE_Q8_K, |
305 | | #if defined (__ARM_FEATURE_MATMUL_INT8) |
306 | | .nrows = 2, |
307 | | #else |
308 | | .nrows = 1, |
309 | | #endif |
310 | | }, |
311 | | [GGML_TYPE_Q5_K] = { |
312 | | .from_float = quantize_row_q5_K, |
313 | | .vec_dot = ggml_vec_dot_q5_K_q8_K, |
314 | | .vec_dot_type = GGML_TYPE_Q8_K, |
315 | | .nrows = 1, |
316 | | }, |
317 | | [GGML_TYPE_Q6_K] = { |
318 | | .from_float = quantize_row_q6_K, |
319 | | .vec_dot = ggml_vec_dot_q6_K_q8_K, |
320 | | .vec_dot_type = GGML_TYPE_Q8_K, |
321 | | #if defined (__ARM_FEATURE_MATMUL_INT8) |
322 | | .nrows = 2, |
323 | | #else |
324 | | .nrows = 1, |
325 | | #endif |
326 | | }, |
327 | | [GGML_TYPE_IQ2_XXS] = { |
328 | | .from_float = NULL, |
329 | | .vec_dot = ggml_vec_dot_iq2_xxs_q8_K, |
330 | | .vec_dot_type = GGML_TYPE_Q8_K, |
331 | | .nrows = 1, |
332 | | }, |
333 | | [GGML_TYPE_IQ2_XS] = { |
334 | | .from_float = NULL, |
335 | | .vec_dot = ggml_vec_dot_iq2_xs_q8_K, |
336 | | .vec_dot_type = GGML_TYPE_Q8_K, |
337 | | .nrows = 1, |
338 | | }, |
339 | | [GGML_TYPE_IQ3_XXS] = { |
340 | | // NOTE: from_float for iq3 and iq2_s was removed because these quants require initialization in ggml_quantize_init |
341 | | //.from_float = quantize_row_iq3_xxs, |
342 | | .vec_dot = ggml_vec_dot_iq3_xxs_q8_K, |
343 | | .vec_dot_type = GGML_TYPE_Q8_K, |
344 | | .nrows = 1, |
345 | | }, |
346 | | [GGML_TYPE_IQ3_S] = { |
347 | | //.from_float = quantize_row_iq3_s, |
348 | | .vec_dot = ggml_vec_dot_iq3_s_q8_K, |
349 | | .vec_dot_type = GGML_TYPE_Q8_K, |
350 | | .nrows = 1, |
351 | | }, |
352 | | [GGML_TYPE_IQ2_S] = { |
353 | | //.from_float = quantize_row_iq2_s, |
354 | | .vec_dot = ggml_vec_dot_iq2_s_q8_K, |
355 | | .vec_dot_type = GGML_TYPE_Q8_K, |
356 | | .nrows = 1, |
357 | | }, |
358 | | [GGML_TYPE_IQ1_S] = { |
359 | | .from_float = NULL, |
360 | | .vec_dot = ggml_vec_dot_iq1_s_q8_K, |
361 | | .vec_dot_type = GGML_TYPE_Q8_K, |
362 | | .nrows = 1, |
363 | | }, |
364 | | [GGML_TYPE_IQ1_M] = { |
365 | | .from_float = NULL, |
366 | | .vec_dot = ggml_vec_dot_iq1_m_q8_K, |
367 | | .vec_dot_type = GGML_TYPE_Q8_K, |
368 | | .nrows = 1, |
369 | | }, |
370 | | [GGML_TYPE_IQ4_NL] = { |
371 | | .from_float = quantize_row_iq4_nl, |
372 | | .vec_dot = ggml_vec_dot_iq4_nl_q8_0, |
373 | | .vec_dot_type = GGML_TYPE_Q8_0, |
374 | | .nrows = 1, |
375 | | }, |
376 | | [GGML_TYPE_IQ4_XS] = { |
377 | | .from_float = quantize_row_iq4_xs, |
378 | | .vec_dot = ggml_vec_dot_iq4_xs_q8_K, |
379 | | .vec_dot_type = GGML_TYPE_Q8_K, |
380 | | .nrows = 1, |
381 | | }, |
382 | | [GGML_TYPE_Q8_K] = { |
383 | | .from_float = quantize_row_q8_K, |
384 | | }, |
385 | | [GGML_TYPE_BF16] = { |
386 | | .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_bf16, |
387 | | .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_bf16, |
388 | | .vec_dot_type = GGML_TYPE_BF16, |
389 | | .nrows = 1, |
390 | | }, |
391 | | [GGML_TYPE_TQ1_0] = { |
392 | | .from_float = quantize_row_tq1_0, |
393 | | .vec_dot = ggml_vec_dot_tq1_0_q8_K, |
394 | | .vec_dot_type = GGML_TYPE_Q8_K, |
395 | | .nrows = 1, |
396 | | }, |
397 | | [GGML_TYPE_TQ2_0] = { |
398 | | .from_float = quantize_row_tq2_0, |
399 | | .vec_dot = ggml_vec_dot_tq2_0_q8_K, |
400 | | .vec_dot_type = GGML_TYPE_Q8_K, |
401 | | .nrows = 1, |
402 | | }, |
403 | | [GGML_TYPE_I32] = { |
404 | | .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_i32, |
405 | | }, |
406 | | }; |
407 | | |
408 | 0 | const struct ggml_type_traits_cpu * ggml_get_type_traits_cpu(enum ggml_type type) { |
409 | 0 | return &type_traits_cpu[type]; |
410 | 0 | } |
411 | | |
412 | | // |
413 | | // Threading defs |
414 | | // |
415 | | |
416 | | typedef pthread_t ggml_thread_t; |
417 | | |
418 | | #if defined(_WIN32) |
419 | | |
420 | | typedef CONDITION_VARIABLE ggml_cond_t; |
421 | | typedef SRWLOCK ggml_mutex_t; |
422 | | |
423 | | #define ggml_mutex_init(m) InitializeSRWLock(m) |
424 | | #define ggml_mutex_destroy(m) |
425 | | #define ggml_mutex_lock(m) AcquireSRWLockExclusive(m) |
426 | | #define ggml_mutex_unlock(m) ReleaseSRWLockExclusive(m) |
427 | | #define ggml_mutex_lock_shared(m) AcquireSRWLockShared(m) |
428 | | #define ggml_mutex_unlock_shared(m) ReleaseSRWLockShared(m) |
429 | | |
430 | | #define ggml_cond_init(c) InitializeConditionVariable(c) |
431 | | #define ggml_cond_destroy(c) |
432 | | #define ggml_cond_wait(c, m) SleepConditionVariableSRW(c, m, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED) |
433 | | #define ggml_cond_broadcast(c) WakeAllConditionVariable(c) |
434 | | |
435 | | #define ggml_thread_create pthread_create |
436 | | #define ggml_thread_join pthread_join |
437 | | |
438 | | #else |
439 | | |
440 | | typedef pthread_cond_t ggml_cond_t; |
441 | | typedef pthread_mutex_t ggml_mutex_t; |
442 | | |
443 | 0 | #define ggml_mutex_init(m) pthread_mutex_init(m, NULL) |
444 | 0 | #define ggml_mutex_destroy(m) pthread_mutex_destroy(m) |
445 | 0 | #define ggml_mutex_lock(m) pthread_mutex_lock(m) |
446 | 0 | #define ggml_mutex_unlock(m) pthread_mutex_unlock(m) |
447 | 0 | #define ggml_mutex_lock_shared(m) pthread_mutex_lock(m) |
448 | 0 | #define ggml_mutex_unlock_shared(m) pthread_mutex_unlock(m) |
449 | | |
450 | | #define ggml_lock_init(x) UNUSED(x) |
451 | | #define ggml_lock_destroy(x) UNUSED(x) |
452 | | #if defined(__x86_64__) || (defined(_MSC_VER) && defined(_M_AMD64)) |
453 | | #define ggml_lock_lock(x) _mm_pause() |
454 | | #else |
455 | | #define ggml_lock_lock(x) UNUSED(x) |
456 | | #endif |
457 | | #define ggml_lock_unlock(x) UNUSED(x) |
458 | | |
459 | | #define GGML_LOCK_INITIALIZER 0 |
460 | 0 | #define ggml_cond_init(c) pthread_cond_init(c, NULL) |
461 | 0 | #define ggml_cond_destroy(c) pthread_cond_destroy(c) |
462 | 0 | #define ggml_cond_wait(c, m) pthread_cond_wait(c, m) |
463 | 0 | #define ggml_cond_broadcast(c) pthread_cond_broadcast(c) |
464 | | |
465 | 0 | #define ggml_thread_create pthread_create |
466 | 0 | #define ggml_thread_join pthread_join |
467 | | |
468 | | #endif |
469 | | |
470 | | // Threadpool def |
471 | | struct ggml_threadpool { |
472 | | ggml_mutex_t mutex; // mutex for cond.var |
473 | | ggml_cond_t cond; // cond.var for waiting for new work |
474 | | |
475 | | struct ggml_cgraph * cgraph; |
476 | | struct ggml_cplan * cplan; |
477 | | |
478 | | // synchronization primitives |
479 | | atomic_int n_graph; // updated when there is work to be done (i.e each graph) holds graph and active thread counts. |
480 | | atomic_int GGML_CACHE_ALIGN n_barrier; |
481 | | atomic_int GGML_CACHE_ALIGN n_barrier_passed; |
482 | | atomic_int GGML_CACHE_ALIGN current_chunk; // currently processing chunk during Mat_Mul, shared between all the threads. |
483 | | |
484 | | // these are atomic as an annotation for thread-sanitizer |
485 | | atomic_bool stop; // Used for stopping the threadpool altogether |
486 | | atomic_bool pause; // Used for pausing the threadpool or individual threads |
487 | | atomic_int abort; // Used for aborting processing of a graph |
488 | | |
489 | | struct ggml_compute_state * workers; // per thread state |
490 | | int n_threads; // Number of threads in the pool |
491 | | int32_t prio; // Scheduling priority |
492 | | uint32_t poll; // Polling level (0 - no polling) |
493 | | |
494 | | enum ggml_status ec; |
495 | | }; |
496 | | |
497 | | // Per-thread state |
498 | | struct ggml_compute_state { |
499 | | #ifndef GGML_USE_OPENMP |
500 | | ggml_thread_t thrd; |
501 | | int last_graph; |
502 | | bool pending; |
503 | | #endif |
504 | | bool cpumask[GGML_MAX_N_THREADS]; |
505 | | struct ggml_threadpool * threadpool; |
506 | | int ith; |
507 | | }; |
508 | | |
509 | | // Helpers for polling loops |
510 | | #if defined(__aarch64__) && ( defined(__clang__) || defined(__GNUC__) ) |
511 | | static inline void ggml_thread_cpu_relax(void) { |
512 | | __asm__ volatile("yield" ::: "memory"); |
513 | | } |
514 | | #elif defined(__x86_64__) |
515 | 0 | static inline void ggml_thread_cpu_relax(void) { |
516 | 0 | _mm_pause(); |
517 | 0 | } |
518 | | #elif defined(__riscv) |
519 | | static inline void ggml_thread_cpu_relax(void) { |
520 | | #ifdef __riscv_zihintpause |
521 | | __asm__ __volatile__ ("pause"); |
522 | | #else |
523 | | /* Encoding of the pause instruction */ |
524 | | __asm__ __volatile__ (".4byte 0x100000F"); |
525 | | #endif |
526 | | } |
527 | | #else |
528 | | static inline void ggml_thread_cpu_relax(void) {;} |
529 | | #endif |
530 | | |
531 | | // |
532 | | // NUMA support |
533 | | // |
534 | | |
535 | 0 | #define GGML_NUMA_MAX_NODES 8 |
536 | 0 | #define GGML_NUMA_MAX_CPUS 512 |
537 | | |
538 | | struct ggml_numa_node { |
539 | | uint32_t cpus[GGML_NUMA_MAX_CPUS]; // hardware threads on this node |
540 | | uint32_t n_cpus; |
541 | | }; |
542 | | |
543 | | struct ggml_numa_nodes { |
544 | | enum ggml_numa_strategy numa_strategy; |
545 | | struct ggml_numa_node nodes[GGML_NUMA_MAX_NODES]; |
546 | | uint32_t n_nodes; |
547 | | uint32_t total_cpus; // hardware threads on system |
548 | | uint32_t current_node; // node on which main process is execting |
549 | | #if defined(__gnu_linux__) |
550 | | cpu_set_t cpuset; // cpuset from numactl |
551 | | #else |
552 | | uint32_t cpuset; // no NUMA support outside of Linux at this time. Use a portable datatype |
553 | | #endif |
554 | | }; |
555 | | |
556 | | // |
557 | | // ggml state |
558 | | // |
559 | | |
560 | | struct ggml_state { |
561 | | struct ggml_numa_nodes numa; |
562 | | }; |
563 | | |
564 | | static struct ggml_state g_state = {0}; |
565 | | |
566 | 0 | void ggml_barrier(struct ggml_threadpool * tp) { |
567 | 0 | int n_threads = atomic_load_explicit(&tp->n_graph, memory_order_relaxed) & GGML_THREADPOOL_N_THREADS_MASK; |
568 | 0 | if (n_threads == 1) { |
569 | 0 | return; |
570 | 0 | } |
571 | | |
572 | | #ifdef GGML_USE_OPENMP |
573 | | #pragma omp barrier |
574 | | #else |
575 | 0 | int n_passed = atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed); |
576 | | |
577 | | // enter barrier (full seq-cst fence) |
578 | 0 | int n_barrier = atomic_fetch_add_explicit(&tp->n_barrier, 1, memory_order_seq_cst); |
579 | |
|
580 | 0 | if (n_barrier == (n_threads - 1)) { |
581 | | // last thread |
582 | 0 | atomic_store_explicit(&tp->n_barrier, 0, memory_order_relaxed); |
583 | | |
584 | | // exit barrier (full seq-cst fence) |
585 | 0 | atomic_fetch_add_explicit(&tp->n_barrier_passed, 1, memory_order_seq_cst); |
586 | 0 | return; |
587 | 0 | } |
588 | | |
589 | | // wait for other threads |
590 | 0 | while (atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed) == n_passed) { |
591 | 0 | ggml_thread_cpu_relax(); |
592 | 0 | } |
593 | | |
594 | | // exit barrier (full seq-cst fence) |
595 | | // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead |
596 | | #ifdef GGML_TSAN_ENABLED |
597 | | atomic_fetch_add_explicit(&tp->n_barrier_passed, 0, memory_order_seq_cst); |
598 | | #else |
599 | 0 | atomic_thread_fence(memory_order_seq_cst); |
600 | 0 | #endif |
601 | 0 | #endif |
602 | 0 | } |
603 | | |
604 | 0 | void ggml_threadpool_chunk_set(struct ggml_threadpool * tp, int value) { |
605 | 0 | atomic_store_explicit(&tp->current_chunk, value, memory_order_relaxed); |
606 | 0 | } |
607 | | |
608 | 0 | int ggml_threadpool_chunk_add(struct ggml_threadpool * tp, int value) { |
609 | 0 | return atomic_fetch_add_explicit(&tp->current_chunk, value, memory_order_relaxed); |
610 | 0 | } |
611 | | |
612 | | #if defined(__gnu_linux__) |
613 | 0 | static cpu_set_t ggml_get_numa_affinity(void) { |
614 | 0 | cpu_set_t cpuset; |
615 | 0 | pthread_t thread; |
616 | 0 | thread = pthread_self(); |
617 | 0 | CPU_ZERO(&cpuset); |
618 | 0 | pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); |
619 | 0 | return cpuset; |
620 | 0 | } |
621 | | #else |
622 | | static uint32_t ggml_get_numa_affinity(void) { |
623 | | return 0; // no NUMA support |
624 | | } |
625 | | #endif |
626 | | |
627 | 0 | void ggml_numa_init(enum ggml_numa_strategy numa_flag) { |
628 | 0 | if (g_state.numa.n_nodes > 0) { |
629 | 0 | fprintf(stderr, "ggml_numa_init: NUMA already initialized\n"); |
630 | |
|
631 | 0 | return; |
632 | 0 | } |
633 | | |
634 | 0 | #if defined(__gnu_linux__) |
635 | 0 | struct stat st; |
636 | 0 | char path[256]; |
637 | 0 | int rv; |
638 | | |
639 | | // set numa scheme |
640 | 0 | g_state.numa.numa_strategy = numa_flag; |
641 | |
|
642 | 0 | GGML_PRINT_DEBUG("numa strategy %u\n",g_state.numa.numa_strategy); |
643 | |
|
644 | 0 | g_state.numa.cpuset = ggml_get_numa_affinity(); |
645 | | |
646 | | // enumerate nodes |
647 | 0 | while (g_state.numa.n_nodes < GGML_NUMA_MAX_NODES) { |
648 | 0 | rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u", g_state.numa.n_nodes); |
649 | 0 | GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path)); |
650 | 0 | if (stat(path, &st) != 0) { break; } |
651 | 0 | ++g_state.numa.n_nodes; |
652 | 0 | } |
653 | | |
654 | | // enumerate CPUs |
655 | 0 | while (g_state.numa.total_cpus < GGML_NUMA_MAX_CPUS) { |
656 | 0 | rv = snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u", g_state.numa.total_cpus); |
657 | 0 | GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path)); |
658 | 0 | if (stat(path, &st) != 0) { break; } |
659 | 0 | ++g_state.numa.total_cpus; |
660 | 0 | } |
661 | |
|
662 | 0 | GGML_PRINT_DEBUG("found %u numa nodes, %u CPUs\n", g_state.numa.n_nodes, g_state.numa.total_cpus); |
663 | | |
664 | | // figure out which node we're on |
665 | 0 | uint current_cpu; |
666 | 0 | int getcpu_ret = 0; |
667 | | #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 33) || defined(__COSMOPOLITAN__) |
668 | | getcpu_ret = getcpu(¤t_cpu, &g_state.numa.current_node); |
669 | | #else |
670 | | // old glibc doesn't have a wrapper for this call. Fall back on direct syscall |
671 | | # if !defined(SYS_getcpu) && defined(SYS_get_cpu) |
672 | | # define SYS_getcpu SYS_get_cpu // some older glibc versions use this name |
673 | | # endif |
674 | 0 | getcpu_ret = syscall(SYS_getcpu, ¤t_cpu, &g_state.numa.current_node); |
675 | 0 | #endif |
676 | |
|
677 | 0 | if (g_state.numa.n_nodes < 1 || g_state.numa.total_cpus < 1 || getcpu_ret != 0) { |
678 | 0 | g_state.numa.n_nodes = 0; |
679 | 0 | return; |
680 | 0 | } |
681 | | |
682 | 0 | GGML_PRINT_DEBUG("found our process on numa node %u, CPU %u\n", g_state.numa.current_node, current_cpu); |
683 | |
|
684 | 0 | for (uint32_t n = 0; n < g_state.numa.n_nodes; ++n) { |
685 | 0 | struct ggml_numa_node * node = &g_state.numa.nodes[n]; |
686 | 0 | GGML_PRINT_DEBUG("CPUs on node %u:", n); |
687 | 0 | node->n_cpus = 0; |
688 | 0 | for (uint32_t c = 0; c < g_state.numa.total_cpus; ++c) { |
689 | 0 | rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u/cpu%u", n, c); |
690 | 0 | GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path)); |
691 | 0 | if (stat(path, &st) == 0) { |
692 | 0 | node->cpus[node->n_cpus++] = c; |
693 | 0 | GGML_PRINT_DEBUG(" %u", c); |
694 | 0 | } |
695 | 0 | } |
696 | 0 | GGML_PRINT_DEBUG("\n"); |
697 | 0 | } |
698 | |
|
699 | 0 | if (ggml_is_numa()) { |
700 | 0 | FILE *fptr = fopen("/proc/sys/kernel/numa_balancing", "r"); |
701 | 0 | if (fptr != NULL) { |
702 | 0 | char buf[42]; |
703 | 0 | if (fgets(buf, sizeof(buf), fptr) && strncmp(buf, "0\n", sizeof(buf)) != 0) { |
704 | 0 | GGML_LOG_WARN("/proc/sys/kernel/numa_balancing is enabled, this has been observed to impair performance\n"); |
705 | 0 | } |
706 | 0 | fclose(fptr); |
707 | 0 | } |
708 | 0 | } |
709 | | #else |
710 | | UNUSED(numa_flag); |
711 | | // TODO |
712 | | #endif |
713 | 0 | } |
714 | | |
715 | 0 | bool ggml_is_numa(void) { |
716 | 0 | return g_state.numa.n_nodes > 1; |
717 | 0 | } |
718 | | |
719 | | #if defined(__ARM_ARCH) |
720 | | #if defined(__aarch64__) && defined(__ARM_FEATURE_SVE) |
721 | | #include <arm_sve.h> |
722 | | static void ggml_init_arm_arch_features(void) { |
723 | | ggml_arm_arch_features.sve_cnt = svcntb(); |
724 | | } |
725 | | #else |
726 | | static void ggml_init_arm_arch_features(void) {} |
727 | | #endif |
728 | | #endif // __ARM_ARCH |
729 | | |
730 | | #if defined(__riscv) && defined(__riscv_v_intrinsic) |
731 | | #include <riscv_vector.h> |
732 | | static void ggml_init_riscv_arch_features(void) { |
733 | | ggml_riscv_arch_features.rvv_vlen = __riscv_vlenb(); |
734 | | } |
735 | | #else |
736 | 0 | static void ggml_init_riscv_arch_features(void) {} |
737 | | #endif |
738 | | |
739 | 0 | struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value) { |
740 | 0 | GGML_ASSERT(!ggml_get_no_alloc(ctx)); |
741 | |
|
742 | 0 | struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 1); |
743 | |
|
744 | 0 | ggml_set_i32(result, value); |
745 | |
|
746 | 0 | return result; |
747 | 0 | } |
748 | | |
749 | 0 | struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value) { |
750 | 0 | GGML_ASSERT(!ggml_get_no_alloc(ctx)); |
751 | |
|
752 | 0 | struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1); |
753 | |
|
754 | 0 | ggml_set_f32(result, value); |
755 | |
|
756 | 0 | return result; |
757 | 0 | } |
758 | | |
759 | 0 | struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value) { |
760 | 0 | const int n = ggml_nrows(tensor); |
761 | 0 | const int nc = tensor->ne[0]; |
762 | 0 | const size_t n1 = tensor->nb[1]; |
763 | |
|
764 | 0 | char * const data = tensor->data; |
765 | |
|
766 | 0 | switch (tensor->type) { |
767 | 0 | case GGML_TYPE_I8: |
768 | 0 | { |
769 | 0 | assert(tensor->nb[0] == sizeof(int8_t)); |
770 | 0 | for (int i = 0; i < n; i++) { |
771 | 0 | ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value); |
772 | 0 | } |
773 | 0 | } break; |
774 | 0 | case GGML_TYPE_I16: |
775 | 0 | { |
776 | 0 | assert(tensor->nb[0] == sizeof(int16_t)); |
777 | 0 | for (int i = 0; i < n; i++) { |
778 | 0 | ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value); |
779 | 0 | } |
780 | 0 | } break; |
781 | 0 | case GGML_TYPE_I32: |
782 | 0 | { |
783 | 0 | assert(tensor->nb[0] == sizeof(int32_t)); |
784 | 0 | for (int i = 0; i < n; i++) { |
785 | 0 | ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value); |
786 | 0 | } |
787 | 0 | } break; |
788 | 0 | case GGML_TYPE_F16: |
789 | 0 | { |
790 | 0 | assert(tensor->nb[0] == sizeof(ggml_fp16_t)); |
791 | 0 | for (int i = 0; i < n; i++) { |
792 | 0 | ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_CPU_FP32_TO_FP16(value)); |
793 | 0 | } |
794 | 0 | } break; |
795 | 0 | case GGML_TYPE_BF16: |
796 | 0 | { |
797 | 0 | assert(tensor->nb[0] == sizeof(ggml_fp16_t)); |
798 | 0 | for (int i = 0; i < n; i++) { |
799 | 0 | ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value)); |
800 | 0 | } |
801 | 0 | } break; |
802 | 0 | case GGML_TYPE_F32: |
803 | 0 | { |
804 | 0 | assert(tensor->nb[0] == sizeof(float)); |
805 | 0 | for (int i = 0; i < n; i++) { |
806 | 0 | ggml_vec_set_f32(nc, (float *)(data + i*n1), value); |
807 | 0 | } |
808 | 0 | } break; |
809 | 0 | default: |
810 | 0 | { |
811 | 0 | GGML_ABORT("fatal error"); |
812 | 0 | } |
813 | 0 | } |
814 | | |
815 | 0 | return tensor; |
816 | 0 | } |
817 | | |
818 | 0 | struct ggml_tensor * ggml_set_f32(struct ggml_tensor * tensor, float value) { |
819 | 0 | const int n = ggml_nrows(tensor); |
820 | 0 | const int nc = tensor->ne[0]; |
821 | 0 | const size_t n1 = tensor->nb[1]; |
822 | |
|
823 | 0 | char * const data = tensor->data; |
824 | |
|
825 | 0 | switch (tensor->type) { |
826 | 0 | case GGML_TYPE_I8: |
827 | 0 | { |
828 | 0 | assert(tensor->nb[0] == sizeof(int8_t)); |
829 | 0 | for (int i = 0; i < n; i++) { |
830 | 0 | ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value); |
831 | 0 | } |
832 | 0 | } break; |
833 | 0 | case GGML_TYPE_I16: |
834 | 0 | { |
835 | 0 | assert(tensor->nb[0] == sizeof(int16_t)); |
836 | 0 | for (int i = 0; i < n; i++) { |
837 | 0 | ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value); |
838 | 0 | } |
839 | 0 | } break; |
840 | 0 | case GGML_TYPE_I32: |
841 | 0 | { |
842 | 0 | assert(tensor->nb[0] == sizeof(int32_t)); |
843 | 0 | for (int i = 0; i < n; i++) { |
844 | 0 | ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value); |
845 | 0 | } |
846 | 0 | } break; |
847 | 0 | case GGML_TYPE_F16: |
848 | 0 | { |
849 | 0 | assert(tensor->nb[0] == sizeof(ggml_fp16_t)); |
850 | 0 | for (int i = 0; i < n; i++) { |
851 | 0 | ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_CPU_FP32_TO_FP16(value)); |
852 | 0 | } |
853 | 0 | } break; |
854 | 0 | case GGML_TYPE_BF16: |
855 | 0 | { |
856 | 0 | assert(tensor->nb[0] == sizeof(ggml_bf16_t)); |
857 | 0 | for (int i = 0; i < n; i++) { |
858 | 0 | ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value)); |
859 | 0 | } |
860 | 0 | } break; |
861 | 0 | case GGML_TYPE_F32: |
862 | 0 | { |
863 | 0 | assert(tensor->nb[0] == sizeof(float)); |
864 | 0 | for (int i = 0; i < n; i++) { |
865 | 0 | ggml_vec_set_f32(nc, (float *)(data + i*n1), value); |
866 | 0 | } |
867 | 0 | } break; |
868 | 0 | default: |
869 | 0 | { |
870 | 0 | GGML_ABORT("fatal error"); |
871 | 0 | } |
872 | 0 | } |
873 | | |
874 | 0 | return tensor; |
875 | 0 | } |
876 | | |
877 | 0 | int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i) { |
878 | 0 | if (!ggml_is_contiguous(tensor)) { |
879 | 0 | int64_t id[4] = { 0, 0, 0, 0 }; |
880 | 0 | ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]); |
881 | 0 | return ggml_get_i32_nd(tensor, id[0], id[1], id[2], id[3]); |
882 | 0 | } |
883 | 0 | switch (tensor->type) { |
884 | 0 | case GGML_TYPE_I8: |
885 | 0 | { |
886 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(int8_t)); |
887 | 0 | return ((int8_t *)(tensor->data))[i]; |
888 | 0 | } |
889 | 0 | case GGML_TYPE_I16: |
890 | 0 | { |
891 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(int16_t)); |
892 | 0 | return ((int16_t *)(tensor->data))[i]; |
893 | 0 | } |
894 | 0 | case GGML_TYPE_I32: |
895 | 0 | { |
896 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(int32_t)); |
897 | 0 | return ((int32_t *)(tensor->data))[i]; |
898 | 0 | } |
899 | 0 | case GGML_TYPE_F16: |
900 | 0 | { |
901 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t)); |
902 | 0 | return GGML_CPU_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]); |
903 | 0 | } |
904 | 0 | case GGML_TYPE_BF16: |
905 | 0 | { |
906 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t)); |
907 | 0 | return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]); |
908 | 0 | } |
909 | 0 | case GGML_TYPE_F32: |
910 | 0 | { |
911 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(float)); |
912 | 0 | return ((float *)(tensor->data))[i]; |
913 | 0 | } |
914 | 0 | default: |
915 | 0 | { |
916 | 0 | GGML_ABORT("fatal error"); |
917 | 0 | } |
918 | 0 | } |
919 | 0 | } |
920 | | |
921 | 0 | void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value) { |
922 | 0 | if (!ggml_is_contiguous(tensor)) { |
923 | 0 | int64_t id[4] = { 0, 0, 0, 0 }; |
924 | 0 | ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]); |
925 | 0 | ggml_set_i32_nd(tensor, id[0], id[1], id[2], id[3], value); |
926 | 0 | return; |
927 | 0 | } |
928 | 0 | switch (tensor->type) { |
929 | 0 | case GGML_TYPE_I8: |
930 | 0 | { |
931 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(int8_t)); |
932 | 0 | ((int8_t *)(tensor->data))[i] = value; |
933 | 0 | } break; |
934 | 0 | case GGML_TYPE_I16: |
935 | 0 | { |
936 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(int16_t)); |
937 | 0 | ((int16_t *)(tensor->data))[i] = value; |
938 | 0 | } break; |
939 | 0 | case GGML_TYPE_I32: |
940 | 0 | { |
941 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(int32_t)); |
942 | 0 | ((int32_t *)(tensor->data))[i] = value; |
943 | 0 | } break; |
944 | 0 | case GGML_TYPE_F16: |
945 | 0 | { |
946 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t)); |
947 | 0 | ((ggml_fp16_t *)(tensor->data))[i] = GGML_CPU_FP32_TO_FP16(value); |
948 | 0 | } break; |
949 | 0 | case GGML_TYPE_BF16: |
950 | 0 | { |
951 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t)); |
952 | 0 | ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value); |
953 | 0 | } break; |
954 | 0 | case GGML_TYPE_F32: |
955 | 0 | { |
956 | 0 | GGML_ASSERT(tensor->nb[0] == sizeof(float)); |
957 | 0 | ((float *)(tensor->data))[i] = value; |
958 | 0 | } break; |
959 | 0 | default: |
960 | 0 | { |
961 | 0 | GGML_ABORT("fatal error"); |
962 | 0 | } |
963 | 0 | } |
964 | 0 | } |
965 | | |
966 | 0 | int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) { |
967 | 0 | void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3]; |
968 | 0 | switch (tensor->type) { |
969 | 0 | case GGML_TYPE_I8: |
970 | 0 | return ((int8_t *) data)[0]; |
971 | 0 | case GGML_TYPE_I16: |
972 | 0 | return ((int16_t *) data)[0]; |
973 | 0 | case GGML_TYPE_I32: |
974 | 0 | return ((int32_t *) data)[0]; |
975 | 0 | case GGML_TYPE_F16: |
976 | 0 | return GGML_CPU_FP16_TO_FP32(((ggml_fp16_t *) data)[0]); |
977 | 0 | case GGML_TYPE_BF16: |
978 | 0 | return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]); |
979 | 0 | case GGML_TYPE_F32: |
980 | 0 | return ((float *) data)[0]; |
981 | 0 | default: |
982 | 0 | GGML_ABORT("fatal error"); |
983 | 0 | } |
984 | 0 | } |
985 | | |
986 | 0 | void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value) { |
987 | 0 | void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3]; |
988 | 0 | switch (tensor->type) { |
989 | 0 | case GGML_TYPE_I8: |
990 | 0 | { |
991 | 0 | ((int8_t *)(data))[0] = value; |
992 | 0 | } break; |
993 | 0 | case GGML_TYPE_I16: |
994 | 0 | { |
995 | 0 | ((int16_t *)(data))[0] = value; |
996 | 0 | } break; |
997 | 0 | case GGML_TYPE_I32: |
998 | 0 | { |
999 | 0 | ((int32_t *)(data))[0] = value; |
1000 | 0 | } break; |
1001 | 0 | case GGML_TYPE_F16: |
1002 | 0 | { |
1003 | 0 | ((ggml_fp16_t *)(data))[0] = GGML_CPU_FP32_TO_FP16(value); |
1004 | 0 | } break; |
1005 | 0 | case GGML_TYPE_BF16: |
1006 | 0 | { |
1007 | 0 | ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value); |
1008 | 0 | } break; |
1009 | 0 | case GGML_TYPE_F32: |
1010 | 0 | { |
1011 | 0 | ((float *)(data))[0] = value; |
1012 | 0 | } break; |
1013 | 0 | default: |
1014 | 0 | { |
1015 | 0 | GGML_ABORT("fatal error"); |
1016 | 0 | } |
1017 | 0 | } |
1018 | 0 | } |
1019 | | |
1020 | 0 | float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i) { |
1021 | 0 | if (!ggml_is_contiguous(tensor)) { |
1022 | 0 | int64_t id[4] = { 0, 0, 0, 0 }; |
1023 | 0 | ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]); |
1024 | 0 | return ggml_get_f32_nd(tensor, id[0], id[1], id[2], id[3]); |
1025 | 0 | } |
1026 | 0 | switch (tensor->type) { |
1027 | 0 | case GGML_TYPE_I8: |
1028 | 0 | { |
1029 | 0 | return ((int8_t *)(tensor->data))[i]; |
1030 | 0 | } |
1031 | 0 | case GGML_TYPE_I16: |
1032 | 0 | { |
1033 | 0 | return ((int16_t *)(tensor->data))[i]; |
1034 | 0 | } |
1035 | 0 | case GGML_TYPE_I32: |
1036 | 0 | { |
1037 | 0 | return ((int32_t *)(tensor->data))[i]; |
1038 | 0 | } |
1039 | 0 | case GGML_TYPE_F16: |
1040 | 0 | { |
1041 | 0 | return GGML_CPU_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]); |
1042 | 0 | } |
1043 | 0 | case GGML_TYPE_BF16: |
1044 | 0 | { |
1045 | 0 | return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]); |
1046 | 0 | } |
1047 | 0 | case GGML_TYPE_F32: |
1048 | 0 | { |
1049 | 0 | return ((float *)(tensor->data))[i]; |
1050 | 0 | } |
1051 | 0 | default: |
1052 | 0 | { |
1053 | 0 | GGML_ABORT("fatal error"); |
1054 | 0 | } |
1055 | 0 | } |
1056 | 0 | } |
1057 | | |
1058 | 0 | void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value) { |
1059 | 0 | if (!ggml_is_contiguous(tensor)) { |
1060 | 0 | int64_t id[4] = { 0, 0, 0, 0 }; |
1061 | 0 | ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]); |
1062 | 0 | ggml_set_f32_nd(tensor, id[0], id[1], id[2], id[3], value); |
1063 | 0 | return; |
1064 | 0 | } |
1065 | 0 | switch (tensor->type) { |
1066 | 0 | case GGML_TYPE_I8: |
1067 | 0 | { |
1068 | 0 | ((int8_t *)(tensor->data))[i] = value; |
1069 | 0 | } break; |
1070 | 0 | case GGML_TYPE_I16: |
1071 | 0 | { |
1072 | 0 | ((int16_t *)(tensor->data))[i] = value; |
1073 | 0 | } break; |
1074 | 0 | case GGML_TYPE_I32: |
1075 | 0 | { |
1076 | 0 | ((int32_t *)(tensor->data))[i] = value; |
1077 | 0 | } break; |
1078 | 0 | case GGML_TYPE_F16: |
1079 | 0 | { |
1080 | 0 | ((ggml_fp16_t *)(tensor->data))[i] = GGML_CPU_FP32_TO_FP16(value); |
1081 | 0 | } break; |
1082 | 0 | case GGML_TYPE_BF16: |
1083 | 0 | { |
1084 | 0 | ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value); |
1085 | 0 | } break; |
1086 | 0 | case GGML_TYPE_F32: |
1087 | 0 | { |
1088 | 0 | ((float *)(tensor->data))[i] = value; |
1089 | 0 | } break; |
1090 | 0 | default: |
1091 | 0 | { |
1092 | 0 | GGML_ABORT("fatal error"); |
1093 | 0 | } |
1094 | 0 | } |
1095 | 0 | } |
1096 | | |
1097 | 0 | float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) { |
1098 | 0 | void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3]; |
1099 | 0 | switch (tensor->type) { |
1100 | 0 | case GGML_TYPE_I8: |
1101 | 0 | return ((int8_t *) data)[0]; |
1102 | 0 | case GGML_TYPE_I16: |
1103 | 0 | return ((int16_t *) data)[0]; |
1104 | 0 | case GGML_TYPE_I32: |
1105 | 0 | return ((int32_t *) data)[0]; |
1106 | 0 | case GGML_TYPE_F16: |
1107 | 0 | return GGML_CPU_FP16_TO_FP32(((ggml_fp16_t *) data)[0]); |
1108 | 0 | case GGML_TYPE_BF16: |
1109 | 0 | return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]); |
1110 | 0 | case GGML_TYPE_F32: |
1111 | 0 | return ((float *) data)[0]; |
1112 | 0 | default: |
1113 | 0 | GGML_ABORT("fatal error"); |
1114 | 0 | } |
1115 | 0 | } |
1116 | | |
1117 | 0 | void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value) { |
1118 | 0 | void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3]; |
1119 | 0 | switch (tensor->type) { |
1120 | 0 | case GGML_TYPE_I8: |
1121 | 0 | { |
1122 | 0 | ((int8_t *)(data))[0] = value; |
1123 | 0 | } break; |
1124 | 0 | case GGML_TYPE_I16: |
1125 | 0 | { |
1126 | 0 | ((int16_t *)(data))[0] = value; |
1127 | 0 | } break; |
1128 | 0 | case GGML_TYPE_I32: |
1129 | 0 | { |
1130 | 0 | ((int32_t *)(data))[0] = value; |
1131 | 0 | } break; |
1132 | 0 | case GGML_TYPE_F16: |
1133 | 0 | { |
1134 | 0 | ((ggml_fp16_t *)(data))[0] = GGML_CPU_FP32_TO_FP16(value); |
1135 | 0 | } break; |
1136 | 0 | case GGML_TYPE_BF16: |
1137 | 0 | { |
1138 | 0 | ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value); |
1139 | 0 | } break; |
1140 | 0 | case GGML_TYPE_F32: |
1141 | 0 | { |
1142 | 0 | ((float *)(data))[0] = value; |
1143 | 0 | } break; |
1144 | 0 | default: |
1145 | 0 | { |
1146 | 0 | GGML_ABORT("fatal error"); |
1147 | 0 | } |
1148 | 0 | } |
1149 | 0 | } |
1150 | | |
1151 | | //////////////////////////////////////////////////////////////////////////////// |
1152 | | |
1153 | | // ggml_compute_forward_mul_mat |
1154 | | |
1155 | | static void ggml_compute_forward_mul_mat_one_chunk( |
1156 | | const struct ggml_compute_params * params, |
1157 | | struct ggml_tensor * dst, |
1158 | | const enum ggml_type type, |
1159 | | const int64_t num_rows_per_vec_dot, |
1160 | | const int64_t ir0_start, |
1161 | | const int64_t ir0_end, |
1162 | | const int64_t ir1_start, |
1163 | 0 | const int64_t ir1_end) { |
1164 | |
|
1165 | 0 | const struct ggml_tensor * src0 = dst->src[0]; |
1166 | 0 | const struct ggml_tensor * src1 = dst->src[1]; |
1167 | |
|
1168 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
1169 | |
|
1170 | 0 | const bool src1_cont = ggml_is_contiguous(src1); |
1171 | |
|
1172 | 0 | ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot; |
1173 | 0 | enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type; |
1174 | | |
1175 | | // broadcast factors |
1176 | 0 | const int64_t r2 = ne12 / ne02; |
1177 | 0 | const int64_t r3 = ne13 / ne03; |
1178 | | |
1179 | | //printf("ir0_start = %6lld, ir0_end = %6lld, ir1_start = %6lld, ir1_end = %6lld\n", ir0_start, ir0_end, ir1_start, ir1_end); |
1180 | | |
1181 | | // threads with no work simply yield (not sure if it helps) |
1182 | 0 | if (ir0_start >= ir0_end || ir1_start >= ir1_end) { |
1183 | 0 | return; |
1184 | 0 | } |
1185 | | |
1186 | 0 | const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata; |
1187 | 0 | const size_t row_size = ggml_row_size(vec_dot_type, ne10); |
1188 | |
|
1189 | 0 | assert(ne12 % ne02 == 0); |
1190 | 0 | assert(ne13 % ne03 == 0); |
1191 | | |
1192 | | // block-tiling attempt |
1193 | 0 | const int64_t blck_0 = 16; |
1194 | 0 | const int64_t blck_1 = 16; |
1195 | |
|
1196 | 0 | const size_t src1_col_stride = src1_cont || src1->type != vec_dot_type ? row_size : nb11; |
1197 | | |
1198 | | // attempt to reduce false-sharing (does not seem to make a difference) |
1199 | | // 16 * 2, accounting for mmla kernels |
1200 | 0 | float tmp[32]; |
1201 | |
|
1202 | 0 | for (int64_t iir1 = ir1_start; iir1 < ir1_end; iir1 += blck_1) { |
1203 | 0 | for (int64_t iir0 = ir0_start; iir0 < ir0_end; iir0 += blck_0) { |
1204 | 0 | for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir1_end; ir1 += num_rows_per_vec_dot) { |
1205 | 0 | const int64_t i13 = (ir1 / (ne12 * ne1)); |
1206 | 0 | const int64_t i12 = (ir1 - i13 * ne12 * ne1) / ne1; |
1207 | 0 | const int64_t i11 = (ir1 - i13 * ne12 * ne1 - i12 * ne1); |
1208 | | |
1209 | | // broadcast src0 into src1 |
1210 | 0 | const int64_t i03 = i13 / r3; |
1211 | 0 | const int64_t i02 = i12 / r2; |
1212 | |
|
1213 | 0 | const int64_t i1 = i11; |
1214 | 0 | const int64_t i2 = i12; |
1215 | 0 | const int64_t i3 = i13; |
1216 | |
|
1217 | 0 | const char * src0_row = (const char*)src0->data + (0 + i02 * nb02 + i03 * nb03); |
1218 | | |
1219 | | // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides |
1220 | | // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using |
1221 | | // the original src1 data pointer, so we should index using the indices directly |
1222 | | // TODO: this is a bit of a hack, we should probably have a better way to handle this |
1223 | 0 | const char * src1_col = (const char*)wdata + |
1224 | 0 | (src1_cont || src1->type != vec_dot_type |
1225 | 0 | ? (i11 + i12 * ne11 + i13 * ne12 * ne11) * row_size |
1226 | 0 | : (i11 * nb11 + i12 * nb12 + i13 * nb13)); |
1227 | 0 | float * dst_col = (float*)((char*)dst->data + (i1 * nb1 + i2 * nb2 + i3 * nb3)); |
1228 | | |
1229 | | //for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ++ir0) { |
1230 | | // vec_dot(ne00, &dst_col[ir0], src0_row + ir0*nb01, src1_col); |
1231 | | //} |
1232 | |
|
1233 | 0 | for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ir0 += num_rows_per_vec_dot) { |
1234 | 0 | vec_dot(ne00, &tmp[ir0 - iir0], (num_rows_per_vec_dot > 1 ? 16 : 0), src0_row + ir0 * nb01, (num_rows_per_vec_dot > 1 ? nb01 : 0), src1_col, (num_rows_per_vec_dot > 1 ? src1_col_stride : 0), num_rows_per_vec_dot); |
1235 | 0 | } |
1236 | |
|
1237 | 0 | for (int cn = 0; cn < num_rows_per_vec_dot; ++cn) { |
1238 | 0 | memcpy(&dst_col[iir0 + cn * nb1 / nb0], tmp + (cn * 16), (MIN(iir0 + blck_0, ir0_end) - iir0) * sizeof(float)); |
1239 | 0 | } |
1240 | 0 | } |
1241 | 0 | } |
1242 | 0 | } |
1243 | 0 | } |
1244 | | |
1245 | | void ggml_compute_forward_mul_mat( |
1246 | | const struct ggml_compute_params * params, |
1247 | 0 | struct ggml_tensor * dst) { |
1248 | |
|
1249 | 0 | const struct ggml_tensor * src0 = dst->src[0]; |
1250 | 0 | const struct ggml_tensor * src1 = dst->src[1]; |
1251 | |
|
1252 | 0 | const int32_t hint = ggml_get_op_params_i32(dst, 1); |
1253 | 0 | if (hint == GGML_HINT_SRC0_IS_HADAMARD && !params->use_ref) { |
1254 | 0 | ggml_compute_forward_fwht(params, dst); |
1255 | 0 | return; |
1256 | 0 | } |
1257 | | |
1258 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
1259 | |
|
1260 | 0 | const int ith = params->ith; |
1261 | 0 | const int nth = params->nth; |
1262 | |
|
1263 | 0 | enum ggml_type const vec_dot_type = type_traits_cpu[src0->type].vec_dot_type; |
1264 | 0 | ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float; |
1265 | 0 | int64_t const vec_dot_num_rows = type_traits_cpu[src0->type].nrows; |
1266 | |
|
1267 | 0 | GGML_ASSERT(ne0 == ne01); |
1268 | 0 | GGML_ASSERT(ne1 == ne11); |
1269 | 0 | GGML_ASSERT(ne2 == ne12); |
1270 | 0 | GGML_ASSERT(ne3 == ne13); |
1271 | | |
1272 | | // we don't support permuted src0 or src1 |
1273 | 0 | GGML_ASSERT(nb00 == ggml_type_size(src0->type)); |
1274 | 0 | GGML_ASSERT(nb10 == ggml_type_size(src1->type)); |
1275 | | |
1276 | | // dst cannot be transposed or permuted |
1277 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
1278 | 0 | GGML_ASSERT(nb0 <= nb1); |
1279 | 0 | GGML_ASSERT(nb1 <= nb2); |
1280 | 0 | GGML_ASSERT(nb2 <= nb3); |
1281 | | |
1282 | | // nb01 >= nb00 - src0 is not transposed |
1283 | | // compute by src0 rows |
1284 | | |
1285 | | // TODO: extract to "extra_op" |
1286 | 0 | #if GGML_USE_LLAMAFILE |
1287 | | // broadcast factors |
1288 | 0 | const int64_t r2 = ne12 / ne02; |
1289 | 0 | const int64_t r3 = ne13 / ne03; |
1290 | |
|
1291 | 0 | const bool src1_cont = ggml_is_contiguous(src1); |
1292 | |
|
1293 | 0 | if (src1_cont) { |
1294 | 0 | for (int64_t i13 = 0; i13 < ne13; i13++) |
1295 | 0 | for (int64_t i12 = 0; i12 < ne12; i12++) |
1296 | 0 | if (!llamafile_sgemm(params, |
1297 | 0 | ne01, ne11, ne00/ggml_blck_size(src0->type), |
1298 | 0 | (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03, |
1299 | 0 | nb01/ggml_type_size(src0->type), |
1300 | 0 | (const char *)src1->data + i12*nb12 + i13*nb13, |
1301 | 0 | nb11/ggml_type_size(src1->type), |
1302 | 0 | (char *)dst->data + i12*nb2 + i13*nb3, |
1303 | 0 | nb1/ggml_type_size(dst->type), |
1304 | 0 | src0->type, |
1305 | 0 | src1->type, |
1306 | 0 | dst->type)) |
1307 | 0 | goto UseGgmlGemm1; |
1308 | 0 | return; |
1309 | 0 | } |
1310 | 0 | UseGgmlGemm1:; |
1311 | 0 | #endif |
1312 | |
|
1313 | 0 | if (src1->type != vec_dot_type) { |
1314 | 0 | char * wdata = params->wdata; |
1315 | |
|
1316 | 0 | const size_t nbw0 = ggml_type_size(vec_dot_type); |
1317 | 0 | const size_t nbw1 = ggml_row_size(vec_dot_type, ne10); |
1318 | 0 | const size_t nbw2 = nbw1*ne11; |
1319 | 0 | const size_t nbw3 = nbw2*ne12; |
1320 | |
|
1321 | 0 | assert(params->wsize >= ne13*nbw3); |
1322 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
1323 | |
|
1324 | | #if 0 |
1325 | | for (int64_t i13 = 0; i13 < ne13; ++i13) { |
1326 | | for (int64_t i12 = 0; i12 < ne12; ++i12) { |
1327 | | for (int64_t i11 = ith; i11 < ne11; i11 += nth) { |
1328 | | from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11), |
1329 | | (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1), |
1330 | | ne10); |
1331 | | } |
1332 | | } |
1333 | | } |
1334 | | #else |
1335 | 0 | for (int64_t i13 = 0; i13 < ne13; ++i13) { |
1336 | 0 | for (int64_t i12 = 0; i12 < ne12; ++i12) { |
1337 | 0 | for (int64_t i11 = 0; i11 < ne11; ++i11) { |
1338 | 0 | size_t bs = ggml_blck_size(vec_dot_type); |
1339 | 0 | int64_t ne10_block_start = (ith * ne10/bs) / nth; |
1340 | 0 | int64_t ne10_block_end = ((ith + 1) * ne10/bs) / nth; |
1341 | 0 | from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + ne10_block_start*bs*nb10), |
1342 | 0 | (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1 + ne10_block_start*nbw0), |
1343 | 0 | (ne10_block_end - ne10_block_start) * bs); |
1344 | 0 | } |
1345 | 0 | } |
1346 | 0 | } |
1347 | 0 | #endif |
1348 | 0 | } |
1349 | |
|
1350 | 0 | if (ith == 0) { |
1351 | | // Every thread starts at ith, so the first unprocessed chunk is nth. This save a bit of coordination right at the start. |
1352 | 0 | atomic_store_explicit(¶ms->threadpool->current_chunk, nth, memory_order_relaxed); |
1353 | 0 | } |
1354 | |
|
1355 | 0 | ggml_barrier(params->threadpool); |
1356 | |
|
1357 | 0 | #if GGML_USE_LLAMAFILE |
1358 | 0 | if (src1->type != vec_dot_type) { |
1359 | 0 | const void* wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata; |
1360 | 0 | const size_t row_size = ggml_row_size(vec_dot_type, ne10); |
1361 | |
|
1362 | 0 | for (int64_t i13 = 0; i13 < ne13; i13++) |
1363 | 0 | for (int64_t i12 = 0; i12 < ne12; i12++) |
1364 | 0 | if (!llamafile_sgemm(params, |
1365 | 0 | ne01, ne11, ne00/ggml_blck_size(src0->type), |
1366 | 0 | (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03, |
1367 | 0 | nb01/ggml_type_size(src0->type), |
1368 | 0 | (const char *)wdata + (i12*ne11 + i13*ne12*ne11)*row_size, |
1369 | 0 | row_size/ggml_type_size(vec_dot_type), |
1370 | 0 | (char *)dst->data + i12*nb2 + i13*nb3, |
1371 | 0 | nb1/ggml_type_size(dst->type), |
1372 | 0 | src0->type, |
1373 | 0 | vec_dot_type, |
1374 | 0 | dst->type)) |
1375 | 0 | goto UseGgmlGemm2; |
1376 | 0 | return; |
1377 | 0 | } |
1378 | 0 | UseGgmlGemm2:; |
1379 | 0 | #endif |
1380 | | |
1381 | | // This is the size of the first dimension of the result, so we can iterate that way. (see the ASSERT above, these are the same numbers) |
1382 | 0 | const int64_t nr0 = ne0; |
1383 | | |
1384 | | // This is the size of the rest of the dimensions of the result |
1385 | 0 | const int64_t nr1 = ne1 * ne2 * ne3; |
1386 | | |
1387 | | // Now select a reasonable chunk size. |
1388 | 0 | int chunk_size = 16; |
1389 | | |
1390 | | // We need to step up the size if it's small |
1391 | 0 | if (nr0 == 1 || nr1 == 1) { |
1392 | 0 | chunk_size = 64; |
1393 | 0 | } |
1394 | | |
1395 | | // distribute the work across the inner or outer loop based on which one is larger |
1396 | | // The number of chunks in the 0/1 dim. |
1397 | | // CEIL(nr0/chunk_size) |
1398 | 0 | int64_t nchunk0 = (nr0 + chunk_size - 1) / chunk_size; |
1399 | 0 | int64_t nchunk1 = (nr1 + chunk_size - 1) / chunk_size; |
1400 | | |
1401 | | // If the chunking is poor for the number of threads on this setup, scrap the whole plan. Re-chunk it by thread. |
1402 | | // Also, chunking by thread was measured to have perform better on NUMA systems. See https://github.com/ggml-org/llama.cpp/pull/6915 |
1403 | | // In theory, chunking should be just as useful on NUMA and non NUMA systems, but testing disagreed with that. |
1404 | 0 | if (nchunk0 * nchunk1 < nth * 4 || ggml_is_numa()) { |
1405 | | // distribute the thread work across the inner or outer loop based on which one is larger |
1406 | 0 | nchunk0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows |
1407 | 0 | nchunk1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows |
1408 | 0 | } |
1409 | | |
1410 | | // The number of elements in each chunk |
1411 | 0 | const int64_t dr0 = (nr0 + nchunk0 - 1) / nchunk0; |
1412 | 0 | const int64_t dr1 = (nr1 + nchunk1 - 1) / nchunk1; |
1413 | | |
1414 | | // The first chunk comes from our thread_id, the rest will get auto-assigned. |
1415 | 0 | int current_chunk = ith; |
1416 | |
|
1417 | 0 | while (current_chunk < nchunk0 * nchunk1) { |
1418 | 0 | const int64_t ith0 = current_chunk % nchunk0; |
1419 | 0 | const int64_t ith1 = current_chunk / nchunk0; |
1420 | |
|
1421 | 0 | const int64_t ir0_start = dr0 * ith0; |
1422 | 0 | const int64_t ir0_end = MIN(ir0_start + dr0, nr0); |
1423 | |
|
1424 | 0 | const int64_t ir1_start = dr1 * ith1; |
1425 | 0 | const int64_t ir1_end = MIN(ir1_start + dr1, nr1); |
1426 | | |
1427 | | // dot kernels can handle 1 row and col at a time, but mmla kernels can process 2 rows and cols |
1428 | 0 | int64_t num_rows_per_vec_dot = vec_dot_num_rows; |
1429 | | |
1430 | | // these checks are needed to avoid crossing dim1 boundaries |
1431 | | // can be optimized, but the logic would become more complicated, so keeping it like this for simplicity |
1432 | 0 | if ((nr0 % 2 != 0) || (ne11 % 2 != 0) || ((ir0_end - ir0_start) % 2 != 0) || ((ir1_end - ir1_start) % 2 != 0)) { |
1433 | 0 | num_rows_per_vec_dot = 1; |
1434 | 0 | } |
1435 | 0 | ggml_compute_forward_mul_mat_one_chunk(params, dst, src0->type, num_rows_per_vec_dot, ir0_start, ir0_end, ir1_start, ir1_end); |
1436 | |
|
1437 | 0 | if (nth >= nchunk0 * nchunk1) { |
1438 | 0 | break; |
1439 | 0 | } |
1440 | | |
1441 | 0 | current_chunk = atomic_fetch_add_explicit(¶ms->threadpool->current_chunk, 1, memory_order_relaxed); |
1442 | 0 | } |
1443 | 0 | } |
1444 | | |
1445 | | // ggml_compute_forward_mul_mat_id |
1446 | | |
1447 | 0 | #define MMID_MATRIX_ROW(row_id, i1) matrix_rows[(row_id)*ids->ne[0]*ids->ne[1] + (i1)] |
1448 | | |
1449 | | struct mmid_row_mapping { |
1450 | | int32_t i1; |
1451 | | int32_t i2; |
1452 | | }; |
1453 | | |
1454 | | static void ggml_compute_forward_mul_mat_id_one_chunk( |
1455 | | struct ggml_tensor * dst, |
1456 | | const struct ggml_tensor * src0, |
1457 | | const struct ggml_tensor * src1, |
1458 | | const struct ggml_tensor * ids, |
1459 | | const int64_t cur_a, |
1460 | | const int64_t ir0_start, |
1461 | | const int64_t ir0_end, |
1462 | | const int64_t ir1_start, |
1463 | | const int64_t ir1_end, |
1464 | | const char * src0_cur, |
1465 | | const struct mmid_row_mapping * matrix_rows, |
1466 | | const size_t row_size, |
1467 | | const bool src1_cont, |
1468 | 0 | const void * wdata) { |
1469 | |
|
1470 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
1471 | |
|
1472 | 0 | const enum ggml_type type = src0->type; |
1473 | |
|
1474 | 0 | ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot; |
1475 | 0 | enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type; |
1476 | |
|
1477 | 0 | const int64_t blck_0 = 16; |
1478 | 0 | const int64_t blck_1 = 16; |
1479 | |
|
1480 | 0 | float tmp[16]; |
1481 | |
|
1482 | 0 | for (int64_t iir1 = ir1_start; iir1 < ir1_end; iir1 += blck_1) { |
1483 | 0 | for (int64_t iir0 = ir0_start; iir0 < ir0_end; iir0 += blck_0) { |
1484 | 0 | for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir1_end; ++ir1) { |
1485 | 0 | const int64_t _i12 = ir1; // logical row index for this expert |
1486 | |
|
1487 | 0 | struct mmid_row_mapping row_mapping = MMID_MATRIX_ROW(cur_a, _i12); |
1488 | 0 | const int id = row_mapping.i1; // selected expert index |
1489 | |
|
1490 | 0 | const int64_t i11 = id % ne11; |
1491 | 0 | const int64_t i12 = row_mapping.i2; // row index in src1 |
1492 | |
|
1493 | 0 | const int64_t i1 = id; // selected expert index |
1494 | 0 | const int64_t i2 = i12; // row |
1495 | | |
1496 | | // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides |
1497 | | // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using |
1498 | | // the original src1 data pointer, so we should index using the indices directly |
1499 | | // TODO: this is a bit of a hack, we should probably have a better way to handle this |
1500 | 0 | const char * src1_col = (const char *) wdata + |
1501 | 0 | (src1_cont || src1->type != vec_dot_type |
1502 | 0 | ? (i11 + i12*ne11)*row_size |
1503 | 0 | : (i11*nb11 + i12*nb12)); |
1504 | |
|
1505 | 0 | float * dst_col = (float *) ((char *) dst->data + (i1*nb1 + i2*nb2)); |
1506 | |
|
1507 | 0 | for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ++ir0) { |
1508 | 0 | vec_dot(ne00, &tmp[ir0 - iir0], 0, src0_cur + ir0*nb01, 0, src1_col, 0, 1); |
1509 | 0 | } |
1510 | |
|
1511 | 0 | memcpy(&dst_col[iir0], tmp, (MIN(iir0 + blck_0, ir0_end) - iir0)*sizeof(float)); |
1512 | 0 | } |
1513 | 0 | } |
1514 | 0 | } |
1515 | 0 | } |
1516 | | |
1517 | 0 | static void * incr_ptr_aligned(void ** p, size_t size, size_t align) { |
1518 | |
|
1519 | 0 | void * ptr = *p; |
1520 | 0 | ptr = (void *) GGML_PAD((uintptr_t) ptr, align); |
1521 | 0 | *p = (void *) ((char *) ptr + size); |
1522 | 0 | return ptr; |
1523 | 0 | } |
1524 | | |
1525 | | static void ggml_compute_forward_mul_mat_id( |
1526 | | const struct ggml_compute_params * params, |
1527 | 0 | struct ggml_tensor * dst) { |
1528 | |
|
1529 | 0 | const struct ggml_tensor * src0 = dst->src[0]; |
1530 | 0 | const struct ggml_tensor * src1 = dst->src[1]; |
1531 | 0 | const struct ggml_tensor * ids = dst->src[2]; |
1532 | |
|
1533 | 0 | GGML_TENSOR_BINARY_OP_LOCALS |
1534 | |
|
1535 | 0 | const int ith = params->ith; |
1536 | 0 | const int nth = params->nth; |
1537 | |
|
1538 | 0 | const enum ggml_type type = src0->type; |
1539 | |
|
1540 | 0 | const bool src1_cont = ggml_is_contiguous(src1); |
1541 | |
|
1542 | 0 | enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type; |
1543 | 0 | ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float; |
1544 | | |
1545 | | // we don't support permuted src0 or src1 |
1546 | 0 | GGML_ASSERT(nb00 == ggml_type_size(type)); |
1547 | 0 | GGML_ASSERT(nb10 == ggml_type_size(src1->type)); |
1548 | | |
1549 | | // dst cannot be transposed or permuted |
1550 | 0 | GGML_ASSERT(nb0 == sizeof(float)); |
1551 | 0 | GGML_ASSERT(nb0 <= nb1); |
1552 | 0 | GGML_ASSERT(nb1 <= nb2); |
1553 | 0 | GGML_ASSERT(nb2 <= nb3); |
1554 | | |
1555 | | // row groups |
1556 | 0 | const int n_ids = ids->ne[0]; // n_expert_used |
1557 | 0 | const int n_as = ne02; // n_expert |
1558 | |
|
1559 | 0 | void * wdata_cur = params->wdata; |
1560 | |
|
1561 | 0 | if (src1->type != vec_dot_type) { |
1562 | 0 | incr_ptr_aligned(&wdata_cur, ggml_row_size(vec_dot_type, ggml_nelements(src1)), sizeof(int64_t)); |
1563 | 0 | } |
1564 | |
|
1565 | 0 | int64_t * matrix_row_counts = // [n_as] |
1566 | 0 | incr_ptr_aligned(&wdata_cur, n_as*sizeof(int64_t), sizeof(int64_t)); |
1567 | |
|
1568 | 0 | struct mmid_row_mapping * matrix_rows = // [n_as][ids->ne[0]*ids->ne[1]] |
1569 | 0 | incr_ptr_aligned(&wdata_cur, n_as*ids->ne[0]*ids->ne[1]*sizeof(struct mmid_row_mapping), sizeof(int64_t)); |
1570 | |
|
1571 | 0 | char (*atomic_current_chunk)[CACHE_LINE_SIZE] = // [n_as] |
1572 | 0 | incr_ptr_aligned(&wdata_cur, CACHE_LINE_SIZE * n_as, CACHE_LINE_SIZE); |
1573 | |
|
1574 | 0 | GGML_ASSERT(params->wsize >= (size_t)((char *) wdata_cur - (char *) params->wdata)); |
1575 | |
|
1576 | 0 | if (src1->type != vec_dot_type) { |
1577 | 0 | char * wdata = params->wdata; |
1578 | |
|
1579 | 0 | const size_t nbw0 = ggml_type_size(vec_dot_type); |
1580 | 0 | const size_t nbw1 = ggml_row_size(vec_dot_type, ne10); |
1581 | 0 | const size_t nbw2 = nbw1*ne11; |
1582 | 0 | const size_t nbw3 = nbw2*ne12; |
1583 | |
|
1584 | 0 | assert(params->wsize >= ne13*nbw3); |
1585 | 0 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
1586 | |
|
1587 | | #if 0 |
1588 | | for (int64_t i13 = 0; i13 < ne13; ++i13) { |
1589 | | for (int64_t i12 = ith; i12 < ne12; i12 += nth) { |
1590 | | for (int64_t i11 = 0; i11 < ne11; ++i11) { |
1591 | | from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11), |
1592 | | (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1), |
1593 | | ne10); |
1594 | | } |
1595 | | } |
1596 | | } |
1597 | | #else |
1598 | 0 | for (int64_t i13 = 0; i13 < ne13; ++i13) { |
1599 | 0 | for (int64_t i12 = 0; i12 < ne12; ++i12) { |
1600 | 0 | for (int64_t i11 = 0; i11 < ne11; ++i11) { |
1601 | 0 | size_t bs = ggml_blck_size(vec_dot_type); |
1602 | 0 | int64_t ne10_block_start = (ith * ne10/bs) / nth; |
1603 | 0 | int64_t ne10_block_end = ((ith + 1) * ne10/bs) / nth; |
1604 | 0 | from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + ne10_block_start*bs*nb10), |
1605 | 0 | (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1 + ne10_block_start*nbw0), |
1606 | 0 | (ne10_block_end - ne10_block_start) * bs); |
1607 | 0 | } |
1608 | 0 | } |
1609 | 0 | } |
1610 | 0 | #endif |
1611 | 0 | } |
1612 | |
|
1613 | 0 | if (ith == 0) { |
1614 | | // initialize matrix_row_counts |
1615 | 0 | memset(matrix_row_counts, 0, n_as*sizeof(int64_t)); |
1616 | | |
1617 | | // group rows by src0 matrix |
1618 | 0 | for (int64_t iid1 = 0; iid1 < ids->ne[1]; ++iid1) { |
1619 | 0 | for (int id = 0; id < n_ids; ++id) { |
1620 | 0 | const int32_t i02 = *(const int32_t *) ((const char *) ids->data + iid1*ids->nb[1] + id*ids->nb[0]); |
1621 | |
|
1622 | 0 | assert(i02 >= 0 && i02 < n_as); |
1623 | |
|
1624 | 0 | MMID_MATRIX_ROW(i02, matrix_row_counts[i02]) = (struct mmid_row_mapping) {id, iid1}; |
1625 | 0 | matrix_row_counts[i02] += 1; |
1626 | 0 | } |
1627 | 0 | } |
1628 | 0 | } |
1629 | | |
1630 | | // reset current_chunk |
1631 | 0 | for (int cur_a = ith; cur_a < n_as; cur_a += nth) { |
1632 | 0 | atomic_int * current_chunk_ctr = (atomic_int *)(atomic_current_chunk + cur_a); |
1633 | 0 | *current_chunk_ctr = nth; |
1634 | 0 | } |
1635 | |
|
1636 | 0 | ggml_barrier(params->threadpool); |
1637 | |
|
1638 | 0 | for (int cur_a = 0; cur_a < n_as; ++cur_a) { |
1639 | 0 | const int64_t cne1 = matrix_row_counts[cur_a]; |
1640 | |
|
1641 | 0 | if (cne1 == 0) { |
1642 | 0 | continue; |
1643 | 0 | } |
1644 | | |
1645 | 0 | const char * src0_cur = (const char *) src0->data + cur_a * nb02; |
1646 | 0 | const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata; |
1647 | 0 | const size_t row_size = ggml_row_size(vec_dot_type, ne10); |
1648 | |
|
1649 | 0 | const int64_t nr0 = ne01; |
1650 | 0 | const int64_t nr1 = cne1; |
1651 | |
|
1652 | 0 | int chunk_size = 16; |
1653 | 0 | if (nr0 == 1 || nr1 == 1) { |
1654 | 0 | chunk_size = 64; |
1655 | 0 | } |
1656 | | |
1657 | | // disable for NUMA |
1658 | 0 | const bool disable_chunking = ggml_is_numa(); |
1659 | |
|
1660 | 0 | int64_t nchunk0 = (nr0 + chunk_size - 1) / chunk_size; |
1661 | 0 | int64_t nchunk1 = (nr1 + chunk_size - 1) / chunk_size; |
1662 | |
|
1663 | 0 | if (nchunk0 * nchunk1 < nth * 4 || disable_chunking) { |
1664 | 0 | nchunk0 = nr0 > nr1 ? nth : 1; |
1665 | 0 | nchunk1 = nr0 > nr1 ? 1 : nth; |
1666 | 0 | } |
1667 | |
|
1668 | 0 | const int64_t dr0 = (nr0 + nchunk0 - 1) / nchunk0; |
1669 | 0 | const int64_t dr1 = (nr1 + nchunk1 - 1) / nchunk1; |
1670 | |
|
1671 | 0 | int current_chunk = ith; |
1672 | |
|
1673 | 0 | atomic_int * current_chunk_ctr = (atomic_int *)(atomic_current_chunk + cur_a); |
1674 | |
|
1675 | 0 | while (current_chunk < nchunk0 * nchunk1) { |
1676 | 0 | const int64_t ith0 = current_chunk % nchunk0; |
1677 | 0 | const int64_t ith1 = current_chunk / nchunk0; |
1678 | |
|
1679 | 0 | const int64_t ir0_start = dr0 * ith0; |
1680 | 0 | const int64_t ir0_end = MIN(ir0_start + dr0, nr0); |
1681 | |
|
1682 | 0 | const int64_t ir1_start = dr1 * ith1; |
1683 | 0 | const int64_t ir1_end = MIN(ir1_start + dr1, nr1); |
1684 | |
|
1685 | 0 | ggml_compute_forward_mul_mat_id_one_chunk( |
1686 | 0 | dst, src0, src1, ids, cur_a, |
1687 | 0 | ir0_start, ir0_end, ir1_start, ir1_end, |
1688 | 0 | src0_cur, matrix_rows, row_size, src1_cont, wdata |
1689 | 0 | ); |
1690 | |
|
1691 | 0 | if (nth >= nchunk0 * nchunk1) { |
1692 | 0 | break; |
1693 | 0 | } |
1694 | | |
1695 | 0 | current_chunk = atomic_fetch_add_explicit(current_chunk_ctr, 1, memory_order_relaxed); |
1696 | 0 | } |
1697 | 0 | } |
1698 | 0 | } |
1699 | | |
1700 | | ///////////////////////////////// |
1701 | | |
1702 | 0 | static void ggml_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) { |
1703 | 0 | GGML_ASSERT(params); |
1704 | |
|
1705 | 0 | if (tensor->op == GGML_OP_NONE || ggml_is_empty(tensor)) { |
1706 | 0 | return; |
1707 | 0 | } |
1708 | | |
1709 | | // extra_buffer op? |
1710 | 0 | if (ggml_cpu_extra_compute_forward(params, tensor)) { |
1711 | 0 | return; |
1712 | 0 | } |
1713 | | |
1714 | 0 | switch (tensor->op) { |
1715 | 0 | case GGML_OP_DUP: |
1716 | 0 | { |
1717 | 0 | ggml_compute_forward_dup(params, tensor); |
1718 | 0 | } break; |
1719 | 0 | case GGML_OP_ADD: |
1720 | 0 | { |
1721 | 0 | ggml_compute_forward_add(params, tensor); |
1722 | 0 | } break; |
1723 | 0 | case GGML_OP_ADD_ID: |
1724 | 0 | { |
1725 | 0 | ggml_compute_forward_add_id(params, tensor); |
1726 | 0 | } break; |
1727 | 0 | case GGML_OP_ADD1: |
1728 | 0 | { |
1729 | 0 | ggml_compute_forward_add1(params, tensor); |
1730 | 0 | } break; |
1731 | 0 | case GGML_OP_ACC: |
1732 | 0 | { |
1733 | 0 | ggml_compute_forward_acc(params, tensor); |
1734 | 0 | } break; |
1735 | 0 | case GGML_OP_SUB: |
1736 | 0 | { |
1737 | 0 | ggml_compute_forward_sub(params, tensor); |
1738 | 0 | } break; |
1739 | 0 | case GGML_OP_MUL: |
1740 | 0 | { |
1741 | 0 | ggml_compute_forward_mul(params, tensor); |
1742 | 0 | } break; |
1743 | 0 | case GGML_OP_DIV: |
1744 | 0 | { |
1745 | 0 | ggml_compute_forward_div(params, tensor); |
1746 | 0 | } break; |
1747 | 0 | case GGML_OP_SQR: |
1748 | 0 | { |
1749 | 0 | ggml_compute_forward_sqr(params, tensor); |
1750 | 0 | } break; |
1751 | 0 | case GGML_OP_SQRT: |
1752 | 0 | { |
1753 | 0 | ggml_compute_forward_sqrt(params, tensor); |
1754 | 0 | } break; |
1755 | 0 | case GGML_OP_LOG: |
1756 | 0 | { |
1757 | 0 | ggml_compute_forward_log(params, tensor); |
1758 | 0 | } break; |
1759 | 0 | case GGML_OP_SIN: |
1760 | 0 | { |
1761 | 0 | ggml_compute_forward_sin(params, tensor); |
1762 | 0 | } break; |
1763 | 0 | case GGML_OP_COS: |
1764 | 0 | { |
1765 | 0 | ggml_compute_forward_cos(params, tensor); |
1766 | 0 | } break; |
1767 | 0 | case GGML_OP_SUM: |
1768 | 0 | { |
1769 | 0 | ggml_compute_forward_sum(params, tensor); |
1770 | 0 | } break; |
1771 | 0 | case GGML_OP_SUM_ROWS: |
1772 | 0 | { |
1773 | 0 | ggml_compute_forward_sum_rows(params, tensor); |
1774 | 0 | } break; |
1775 | 0 | case GGML_OP_CUMSUM: |
1776 | 0 | { |
1777 | 0 | ggml_compute_forward_cumsum(params, tensor); |
1778 | 0 | } break; |
1779 | 0 | case GGML_OP_MEAN: |
1780 | 0 | { |
1781 | 0 | ggml_compute_forward_mean(params, tensor); |
1782 | 0 | } break; |
1783 | 0 | case GGML_OP_ARGMAX: |
1784 | 0 | { |
1785 | 0 | ggml_compute_forward_argmax(params, tensor); |
1786 | 0 | } break; |
1787 | 0 | case GGML_OP_COUNT_EQUAL: |
1788 | 0 | { |
1789 | 0 | ggml_compute_forward_count_equal(params, tensor); |
1790 | 0 | } break; |
1791 | 0 | case GGML_OP_REPEAT: |
1792 | 0 | { |
1793 | 0 | ggml_compute_forward_repeat(params, tensor); |
1794 | 0 | } break; |
1795 | 0 | case GGML_OP_REPEAT_BACK: |
1796 | 0 | { |
1797 | 0 | ggml_compute_forward_repeat_back(params, tensor); |
1798 | 0 | } break; |
1799 | 0 | case GGML_OP_CONCAT: |
1800 | 0 | { |
1801 | 0 | ggml_compute_forward_concat(params, tensor); |
1802 | 0 | } break; |
1803 | 0 | case GGML_OP_SILU_BACK: |
1804 | 0 | { |
1805 | 0 | ggml_compute_forward_silu_back(params, tensor); |
1806 | 0 | } break; |
1807 | 0 | case GGML_OP_NORM: |
1808 | 0 | { |
1809 | 0 | ggml_compute_forward_norm(params, tensor); |
1810 | 0 | } break; |
1811 | 0 | case GGML_OP_RMS_NORM: |
1812 | 0 | { |
1813 | 0 | ggml_compute_forward_rms_norm(params, tensor); |
1814 | 0 | } break; |
1815 | 0 | case GGML_OP_RMS_NORM_BACK: |
1816 | 0 | { |
1817 | 0 | ggml_compute_forward_rms_norm_back(params, tensor); |
1818 | 0 | } break; |
1819 | 0 | case GGML_OP_GROUP_NORM: |
1820 | 0 | { |
1821 | 0 | ggml_compute_forward_group_norm(params, tensor); |
1822 | 0 | } break; |
1823 | 0 | case GGML_OP_L2_NORM: |
1824 | 0 | { |
1825 | 0 | ggml_compute_forward_l2_norm(params, tensor); |
1826 | 0 | } break; |
1827 | 0 | case GGML_OP_MUL_MAT: |
1828 | 0 | { |
1829 | 0 | ggml_compute_forward_mul_mat(params, tensor); |
1830 | 0 | } break; |
1831 | 0 | case GGML_OP_MUL_MAT_ID: |
1832 | 0 | { |
1833 | 0 | ggml_compute_forward_mul_mat_id(params, tensor); |
1834 | 0 | } break; |
1835 | 0 | case GGML_OP_OUT_PROD: |
1836 | 0 | { |
1837 | 0 | ggml_compute_forward_out_prod(params, tensor); |
1838 | 0 | } break; |
1839 | 0 | case GGML_OP_SCALE: |
1840 | 0 | { |
1841 | 0 | ggml_compute_forward_scale(params, tensor); |
1842 | 0 | } break; |
1843 | 0 | case GGML_OP_SET: |
1844 | 0 | { |
1845 | 0 | ggml_compute_forward_set(params, tensor); |
1846 | 0 | } break; |
1847 | 0 | case GGML_OP_CPY: |
1848 | 0 | { |
1849 | 0 | ggml_compute_forward_cpy(params, tensor); |
1850 | 0 | } break; |
1851 | 0 | case GGML_OP_CONT: |
1852 | 0 | { |
1853 | 0 | ggml_compute_forward_cont(params, tensor); |
1854 | 0 | } break; |
1855 | 0 | case GGML_OP_GET_ROWS: |
1856 | 0 | { |
1857 | 0 | ggml_compute_forward_get_rows(params, tensor); |
1858 | 0 | } break; |
1859 | 0 | case GGML_OP_GET_ROWS_BACK: |
1860 | 0 | { |
1861 | 0 | ggml_compute_forward_get_rows_back(params, tensor); |
1862 | 0 | } break; |
1863 | 0 | case GGML_OP_SET_ROWS: |
1864 | 0 | { |
1865 | 0 | ggml_compute_forward_set_rows(params, tensor); |
1866 | 0 | } break; |
1867 | 0 | case GGML_OP_DIAG: |
1868 | 0 | { |
1869 | 0 | ggml_compute_forward_diag(params, tensor); |
1870 | 0 | } break; |
1871 | 0 | case GGML_OP_DIAG_MASK_INF: |
1872 | 0 | { |
1873 | 0 | ggml_compute_forward_diag_mask_inf(params, tensor); |
1874 | 0 | } break; |
1875 | 0 | case GGML_OP_DIAG_MASK_ZERO: |
1876 | 0 | { |
1877 | 0 | ggml_compute_forward_diag_mask_zero(params, tensor); |
1878 | 0 | } break; |
1879 | 0 | case GGML_OP_SOFT_MAX: |
1880 | 0 | { |
1881 | 0 | ggml_compute_forward_soft_max(params, tensor); |
1882 | 0 | } break; |
1883 | 0 | case GGML_OP_SOFT_MAX_BACK: |
1884 | 0 | { |
1885 | 0 | ggml_compute_forward_soft_max_ext_back(params, tensor); |
1886 | 0 | } break; |
1887 | 0 | case GGML_OP_ROPE: |
1888 | 0 | { |
1889 | 0 | ggml_compute_forward_rope(params, tensor); |
1890 | 0 | } break; |
1891 | 0 | case GGML_OP_ROPE_BACK: |
1892 | 0 | { |
1893 | 0 | ggml_compute_forward_rope_back(params, tensor); |
1894 | 0 | } break; |
1895 | 0 | case GGML_OP_CLAMP: |
1896 | 0 | { |
1897 | 0 | ggml_compute_forward_clamp(params, tensor); |
1898 | 0 | } break; |
1899 | 0 | case GGML_OP_CONV_TRANSPOSE_1D: |
1900 | 0 | { |
1901 | 0 | ggml_compute_forward_conv_transpose_1d(params, tensor); |
1902 | 0 | } break; |
1903 | 0 | case GGML_OP_IM2COL: |
1904 | 0 | { |
1905 | 0 | ggml_compute_forward_im2col(params, tensor); |
1906 | 0 | } break; |
1907 | 0 | case GGML_OP_IM2COL_BACK: |
1908 | 0 | { |
1909 | 0 | ggml_compute_forward_im2col_back_f32(params, tensor); |
1910 | 0 | } break; |
1911 | 0 | case GGML_OP_IM2COL_3D: |
1912 | 0 | { |
1913 | 0 | ggml_compute_forward_im2col_3d(params, tensor); |
1914 | 0 | } break; |
1915 | 0 | case GGML_OP_COL2IM_1D: |
1916 | 0 | { |
1917 | 0 | ggml_compute_forward_col2im_1d(params, tensor); |
1918 | 0 | } break; |
1919 | 0 | case GGML_OP_CONV_2D: |
1920 | 0 | { |
1921 | 0 | ggml_compute_forward_conv_2d(params, tensor); |
1922 | 0 | } break; |
1923 | 0 | case GGML_OP_CONV_3D: |
1924 | 0 | { |
1925 | 0 | ggml_compute_forward_conv_3d(params, tensor); |
1926 | 0 | } break; |
1927 | 0 | case GGML_OP_CONV_2D_DW: |
1928 | 0 | { |
1929 | 0 | ggml_compute_forward_conv_2d_dw(params, tensor); |
1930 | 0 | } break; |
1931 | 0 | case GGML_OP_CONV_TRANSPOSE_2D: |
1932 | 0 | { |
1933 | 0 | ggml_compute_forward_conv_transpose_2d(params, tensor); |
1934 | 0 | } break; |
1935 | 0 | case GGML_OP_POOL_1D: |
1936 | 0 | { |
1937 | 0 | ggml_compute_forward_pool_1d(params, tensor); |
1938 | 0 | } break; |
1939 | 0 | case GGML_OP_POOL_2D: |
1940 | 0 | { |
1941 | 0 | ggml_compute_forward_pool_2d(params, tensor); |
1942 | 0 | } break; |
1943 | 0 | case GGML_OP_POOL_2D_BACK: |
1944 | 0 | { |
1945 | 0 | ggml_compute_forward_pool_2d_back(params, tensor); |
1946 | 0 | } break; |
1947 | 0 | case GGML_OP_UPSCALE: |
1948 | 0 | { |
1949 | 0 | ggml_compute_forward_upscale(params, tensor); |
1950 | 0 | } break; |
1951 | 0 | case GGML_OP_PAD: |
1952 | 0 | { |
1953 | 0 | ggml_compute_forward_pad(params, tensor); |
1954 | 0 | } break; |
1955 | 0 | case GGML_OP_PAD_REFLECT_1D: |
1956 | 0 | { |
1957 | 0 | ggml_compute_forward_pad_reflect_1d(params, tensor); |
1958 | 0 | } break; |
1959 | 0 | case GGML_OP_ROLL: |
1960 | 0 | { |
1961 | 0 | ggml_compute_forward_roll(params, tensor); |
1962 | 0 | } break; |
1963 | 0 | case GGML_OP_ARANGE: |
1964 | 0 | { |
1965 | 0 | ggml_compute_forward_arange(params, tensor); |
1966 | 0 | } break; |
1967 | 0 | case GGML_OP_TIMESTEP_EMBEDDING: |
1968 | 0 | { |
1969 | 0 | ggml_compute_forward_timestep_embedding(params, tensor); |
1970 | 0 | } break; |
1971 | 0 | case GGML_OP_ARGSORT: |
1972 | 0 | { |
1973 | 0 | ggml_compute_forward_argsort(params, tensor); |
1974 | 0 | } break; |
1975 | 0 | case GGML_OP_TOP_K: |
1976 | 0 | { |
1977 | 0 | ggml_compute_forward_top_k(params, tensor); |
1978 | 0 | } break; |
1979 | 0 | case GGML_OP_LEAKY_RELU: |
1980 | 0 | { |
1981 | 0 | ggml_compute_forward_leaky_relu(params, tensor); |
1982 | 0 | } break; |
1983 | 0 | case GGML_OP_TRI: |
1984 | 0 | { |
1985 | 0 | ggml_compute_forward_tri(params, tensor); |
1986 | 0 | } break; |
1987 | 0 | case GGML_OP_FILL: |
1988 | 0 | { |
1989 | 0 | ggml_compute_forward_fill(params, tensor); |
1990 | 0 | } break; |
1991 | 0 | case GGML_OP_FLASH_ATTN_EXT: |
1992 | 0 | { |
1993 | 0 | ggml_compute_forward_flash_attn_ext(params, tensor); |
1994 | 0 | } break; |
1995 | 0 | case GGML_OP_FLASH_ATTN_BACK: |
1996 | 0 | { |
1997 | 0 | int32_t t = ggml_get_op_params_i32(tensor, 0); |
1998 | 0 | GGML_ASSERT(t == 0 || t == 1); |
1999 | 0 | bool masked = t != 0; |
2000 | 0 | ggml_compute_forward_flash_attn_back(params, masked, tensor); |
2001 | 0 | } break; |
2002 | 0 | case GGML_OP_SSM_CONV: |
2003 | 0 | { |
2004 | 0 | ggml_compute_forward_ssm_conv(params, tensor); |
2005 | 0 | } break; |
2006 | 0 | case GGML_OP_SSM_SCAN: |
2007 | 0 | { |
2008 | 0 | ggml_compute_forward_ssm_scan(params, tensor); |
2009 | 0 | } break; |
2010 | 0 | case GGML_OP_WIN_PART: |
2011 | 0 | { |
2012 | 0 | ggml_compute_forward_win_part(params, tensor); |
2013 | 0 | } break; |
2014 | 0 | case GGML_OP_WIN_UNPART: |
2015 | 0 | { |
2016 | 0 | ggml_compute_forward_win_unpart(params, tensor); |
2017 | 0 | } break; |
2018 | 0 | case GGML_OP_UNARY: |
2019 | 0 | { |
2020 | 0 | ggml_compute_forward_unary(params, tensor); |
2021 | 0 | } break; |
2022 | 0 | case GGML_OP_GLU: |
2023 | 0 | { |
2024 | 0 | ggml_compute_forward_glu(params, tensor); |
2025 | 0 | } break; |
2026 | 0 | case GGML_OP_GET_REL_POS: |
2027 | 0 | { |
2028 | 0 | ggml_compute_forward_get_rel_pos(params, tensor); |
2029 | 0 | } break; |
2030 | 0 | case GGML_OP_ADD_REL_POS: |
2031 | 0 | { |
2032 | 0 | ggml_compute_forward_add_rel_pos(params, tensor); |
2033 | 0 | } break; |
2034 | 0 | case GGML_OP_RWKV_WKV6: |
2035 | 0 | { |
2036 | 0 | ggml_compute_forward_rwkv_wkv6(params, tensor); |
2037 | 0 | } break; |
2038 | 0 | case GGML_OP_GATED_LINEAR_ATTN: |
2039 | 0 | { |
2040 | 0 | ggml_compute_forward_gla(params, tensor); |
2041 | 0 | } break; |
2042 | 0 | case GGML_OP_RWKV_WKV7: |
2043 | 0 | { |
2044 | 0 | ggml_compute_forward_rwkv_wkv7(params, tensor); |
2045 | 0 | } break; |
2046 | 0 | case GGML_OP_SOLVE_TRI: |
2047 | 0 | { |
2048 | 0 | ggml_compute_forward_solve_tri(params, tensor); |
2049 | 0 | } break; |
2050 | 0 | case GGML_OP_GATED_DELTA_NET: |
2051 | 0 | { |
2052 | 0 | ggml_compute_forward_gated_delta_net(params, tensor); |
2053 | 0 | } break; |
2054 | 0 | case GGML_OP_MAP_CUSTOM1: |
2055 | 0 | { |
2056 | 0 | ggml_compute_forward_map_custom1(params, tensor); |
2057 | 0 | } |
2058 | 0 | break; |
2059 | 0 | case GGML_OP_MAP_CUSTOM2: |
2060 | 0 | { |
2061 | 0 | ggml_compute_forward_map_custom2(params, tensor); |
2062 | 0 | } |
2063 | 0 | break; |
2064 | 0 | case GGML_OP_MAP_CUSTOM3: |
2065 | 0 | { |
2066 | 0 | ggml_compute_forward_map_custom3(params, tensor); |
2067 | 0 | } |
2068 | 0 | break; |
2069 | 0 | case GGML_OP_CUSTOM: |
2070 | 0 | { |
2071 | 0 | ggml_compute_forward_custom(params, tensor); |
2072 | 0 | } |
2073 | 0 | break; |
2074 | 0 | case GGML_OP_CROSS_ENTROPY_LOSS: |
2075 | 0 | { |
2076 | 0 | ggml_compute_forward_cross_entropy_loss(params, tensor); |
2077 | 0 | } |
2078 | 0 | break; |
2079 | 0 | case GGML_OP_CROSS_ENTROPY_LOSS_BACK: |
2080 | 0 | { |
2081 | 0 | ggml_compute_forward_cross_entropy_loss_back(params, tensor); |
2082 | 0 | } |
2083 | 0 | break; |
2084 | 0 | case GGML_OP_OPT_STEP_ADAMW: |
2085 | 0 | { |
2086 | 0 | ggml_compute_forward_opt_step_adamw(params, tensor); |
2087 | 0 | } |
2088 | 0 | break; |
2089 | 0 | case GGML_OP_OPT_STEP_SGD: |
2090 | 0 | { |
2091 | 0 | ggml_compute_forward_opt_step_sgd(params, tensor); |
2092 | 0 | } |
2093 | 0 | break; |
2094 | 0 | case GGML_OP_NONE: |
2095 | 0 | { |
2096 | | // nop |
2097 | 0 | } break; |
2098 | 0 | case GGML_OP_RESHAPE: |
2099 | 0 | { |
2100 | | // nop |
2101 | 0 | } break; |
2102 | 0 | case GGML_OP_PERMUTE: |
2103 | 0 | { |
2104 | | // nop |
2105 | 0 | } break; |
2106 | 0 | case GGML_OP_VIEW: |
2107 | 0 | { |
2108 | | // nop |
2109 | 0 | } break; |
2110 | 0 | case GGML_OP_TRANSPOSE: |
2111 | 0 | { |
2112 | | // nop |
2113 | 0 | } break; |
2114 | 0 | case GGML_OP_COUNT: |
2115 | 0 | { |
2116 | 0 | GGML_ABORT("fatal error"); |
2117 | 0 | } |
2118 | 0 | } |
2119 | 0 | } |
2120 | | |
2121 | | // Android's libc implementation "bionic" does not support setting affinity |
2122 | | #if defined(__gnu_linux__) |
2123 | 0 | static void set_numa_thread_affinity(int thread_n) { |
2124 | 0 | if (!ggml_is_numa()) { |
2125 | 0 | return; |
2126 | 0 | } |
2127 | | |
2128 | 0 | int node_num; |
2129 | 0 | int rv; |
2130 | 0 | size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus); |
2131 | |
|
2132 | 0 | switch(g_state.numa.numa_strategy) { |
2133 | 0 | case GGML_NUMA_STRATEGY_DISTRIBUTE: |
2134 | | // run thread on node_num thread_n / (threads per node) |
2135 | 0 | node_num = thread_n % g_state.numa.n_nodes; |
2136 | 0 | break; |
2137 | 0 | case GGML_NUMA_STRATEGY_ISOLATE: |
2138 | | // run thread on current_node |
2139 | 0 | node_num = g_state.numa.current_node; |
2140 | 0 | break; |
2141 | 0 | case GGML_NUMA_STRATEGY_NUMACTL: |
2142 | | // use the cpuset that numactl gave us |
2143 | 0 | rv = pthread_setaffinity_np(pthread_self(), setsize, &g_state.numa.cpuset); |
2144 | 0 | if (rv) { |
2145 | 0 | fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n",strerror(rv)); |
2146 | 0 | } |
2147 | 0 | return; |
2148 | 0 | default: |
2149 | 0 | return; |
2150 | 0 | } |
2151 | | |
2152 | 0 | struct ggml_numa_node * node = &g_state.numa.nodes[node_num]; |
2153 | |
|
2154 | 0 | cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus); |
2155 | 0 | CPU_ZERO_S(setsize, cpus); |
2156 | 0 | for (size_t i = 0; i < node->n_cpus; ++i) { |
2157 | 0 | CPU_SET_S(node->cpus[i], setsize, cpus); |
2158 | 0 | } |
2159 | |
|
2160 | 0 | rv = pthread_setaffinity_np(pthread_self(), setsize, cpus); |
2161 | 0 | if (rv) { |
2162 | 0 | fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv)); |
2163 | 0 | } |
2164 | |
|
2165 | 0 | CPU_FREE(cpus); |
2166 | 0 | } |
2167 | | |
2168 | 0 | static void clear_numa_thread_affinity(void) { |
2169 | 0 | if (!ggml_is_numa()) { |
2170 | 0 | return; |
2171 | 0 | } |
2172 | | |
2173 | 0 | size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus); |
2174 | |
|
2175 | 0 | cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus); |
2176 | 0 | CPU_ZERO_S(setsize, cpus); |
2177 | 0 | for (unsigned i = 0; i < g_state.numa.total_cpus; ++i) { |
2178 | 0 | CPU_SET_S(i, setsize, cpus); |
2179 | 0 | } |
2180 | |
|
2181 | 0 | int rv = pthread_setaffinity_np(pthread_self(), setsize, cpus); |
2182 | 0 | if (rv) { |
2183 | 0 | fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv)); |
2184 | 0 | } |
2185 | |
|
2186 | 0 | CPU_FREE(cpus); |
2187 | 0 | } |
2188 | | #else |
2189 | | // TODO: Windows etc. |
2190 | | // (the linux implementation may also work on BSD, someone should test) |
2191 | | static void set_numa_thread_affinity(int thread_n) { UNUSED(thread_n); } |
2192 | | static void clear_numa_thread_affinity(void) {} |
2193 | | #endif |
2194 | | |
2195 | 0 | static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) { |
2196 | 0 | int n_tasks = 0; |
2197 | |
|
2198 | 0 | if (ggml_is_empty(node)) { |
2199 | | // no need to multi-thread a no-op |
2200 | 0 | n_tasks = 1; |
2201 | 0 | return n_tasks; |
2202 | 0 | } |
2203 | | |
2204 | 0 | switch (node->op) { |
2205 | 0 | case GGML_OP_CPY: |
2206 | 0 | case GGML_OP_DUP: |
2207 | 0 | case GGML_OP_CONT: |
2208 | 0 | case GGML_OP_ADD: |
2209 | 0 | case GGML_OP_ADD_ID: |
2210 | 0 | case GGML_OP_ADD1: |
2211 | 0 | case GGML_OP_ACC: |
2212 | 0 | case GGML_OP_CUMSUM: |
2213 | 0 | case GGML_OP_TRI: |
2214 | 0 | case GGML_OP_FILL: |
2215 | 0 | { |
2216 | 0 | n_tasks = n_threads; |
2217 | 0 | } break; |
2218 | 0 | case GGML_OP_SUB: |
2219 | 0 | case GGML_OP_SQR: |
2220 | 0 | case GGML_OP_SQRT: |
2221 | 0 | case GGML_OP_LOG: |
2222 | 0 | case GGML_OP_SIN: |
2223 | 0 | case GGML_OP_COS: |
2224 | 0 | case GGML_OP_SUM: |
2225 | 0 | case GGML_OP_SUM_ROWS: |
2226 | 0 | case GGML_OP_MEAN: |
2227 | 0 | case GGML_OP_ARGMAX: |
2228 | 0 | { |
2229 | 0 | n_tasks = 1; |
2230 | 0 | } break; |
2231 | 0 | case GGML_OP_COUNT_EQUAL: |
2232 | 0 | case GGML_OP_SOLVE_TRI: |
2233 | 0 | case GGML_OP_GATED_DELTA_NET: |
2234 | 0 | { |
2235 | 0 | n_tasks = n_threads; |
2236 | 0 | } break; |
2237 | 0 | case GGML_OP_REPEAT: |
2238 | 0 | case GGML_OP_REPEAT_BACK: |
2239 | 0 | case GGML_OP_LEAKY_RELU: |
2240 | 0 | { |
2241 | 0 | n_tasks = 1; |
2242 | 0 | } break; |
2243 | 0 | case GGML_OP_UNARY: |
2244 | 0 | switch (ggml_get_unary_op(node)) { |
2245 | 0 | case GGML_UNARY_OP_ABS: |
2246 | 0 | case GGML_UNARY_OP_SGN: |
2247 | 0 | case GGML_UNARY_OP_NEG: |
2248 | 0 | case GGML_UNARY_OP_STEP: |
2249 | 0 | case GGML_UNARY_OP_TANH: |
2250 | 0 | case GGML_UNARY_OP_ELU: |
2251 | 0 | case GGML_UNARY_OP_RELU: |
2252 | 0 | case GGML_UNARY_OP_SIGMOID: |
2253 | 0 | case GGML_UNARY_OP_HARDSWISH: |
2254 | 0 | case GGML_UNARY_OP_HARDSIGMOID: |
2255 | 0 | case GGML_UNARY_OP_EXP: |
2256 | 0 | case GGML_UNARY_OP_SOFTPLUS: |
2257 | 0 | case GGML_UNARY_OP_EXPM1: |
2258 | 0 | case GGML_UNARY_OP_FLOOR: |
2259 | 0 | case GGML_UNARY_OP_CEIL: |
2260 | 0 | case GGML_UNARY_OP_ROUND: |
2261 | 0 | case GGML_UNARY_OP_TRUNC: |
2262 | 0 | { |
2263 | 0 | n_tasks = 1; |
2264 | 0 | } break; |
2265 | | |
2266 | 0 | case GGML_UNARY_OP_GELU: |
2267 | 0 | case GGML_UNARY_OP_GELU_ERF: |
2268 | 0 | case GGML_UNARY_OP_GELU_QUICK: |
2269 | 0 | case GGML_UNARY_OP_SILU: |
2270 | 0 | case GGML_UNARY_OP_XIELU: |
2271 | 0 | { |
2272 | 0 | n_tasks = n_threads; |
2273 | 0 | } break; |
2274 | 0 | default: |
2275 | 0 | GGML_ABORT("fatal error"); |
2276 | 0 | } |
2277 | 0 | break; |
2278 | 0 | case GGML_OP_GLU: |
2279 | 0 | switch (ggml_get_glu_op(node)) { |
2280 | 0 | case GGML_GLU_OP_REGLU: |
2281 | 0 | case GGML_GLU_OP_GEGLU: |
2282 | 0 | case GGML_GLU_OP_SWIGLU: |
2283 | 0 | case GGML_GLU_OP_SWIGLU_OAI: |
2284 | 0 | case GGML_GLU_OP_GEGLU_ERF: |
2285 | 0 | case GGML_GLU_OP_GEGLU_QUICK: |
2286 | 0 | { |
2287 | 0 | n_tasks = n_threads; |
2288 | 0 | } break; |
2289 | 0 | default: |
2290 | 0 | GGML_ABORT("fatal error"); |
2291 | 0 | } |
2292 | 0 | break; |
2293 | 0 | case GGML_OP_SILU_BACK: |
2294 | 0 | case GGML_OP_MUL: |
2295 | 0 | case GGML_OP_DIV: |
2296 | 0 | case GGML_OP_NORM: |
2297 | 0 | case GGML_OP_RMS_NORM: |
2298 | 0 | case GGML_OP_RMS_NORM_BACK: |
2299 | 0 | case GGML_OP_L2_NORM: |
2300 | 0 | case GGML_OP_GROUP_NORM: |
2301 | 0 | case GGML_OP_CONCAT: |
2302 | 0 | case GGML_OP_MUL_MAT: |
2303 | 0 | case GGML_OP_MUL_MAT_ID: |
2304 | 0 | case GGML_OP_OUT_PROD: |
2305 | 0 | { |
2306 | 0 | n_tasks = n_threads; |
2307 | 0 | } break; |
2308 | 0 | case GGML_OP_GET_ROWS: |
2309 | 0 | case GGML_OP_SET_ROWS: |
2310 | 0 | { |
2311 | | // FIXME: get_rows can use additional threads, but the cost of launching additional threads |
2312 | | // decreases performance with GPU offloading |
2313 | | //n_tasks = n_threads; |
2314 | 0 | n_tasks = 1; |
2315 | 0 | } break; |
2316 | 0 | case GGML_OP_SCALE: |
2317 | 0 | case GGML_OP_SET: |
2318 | 0 | case GGML_OP_RESHAPE: |
2319 | 0 | case GGML_OP_VIEW: |
2320 | 0 | case GGML_OP_PERMUTE: |
2321 | 0 | case GGML_OP_TRANSPOSE: |
2322 | 0 | case GGML_OP_GET_ROWS_BACK: |
2323 | 0 | case GGML_OP_DIAG: |
2324 | 0 | { |
2325 | 0 | n_tasks = 1; |
2326 | 0 | } break; |
2327 | 0 | case GGML_OP_DIAG_MASK_ZERO: |
2328 | 0 | case GGML_OP_DIAG_MASK_INF: |
2329 | 0 | case GGML_OP_SOFT_MAX_BACK: |
2330 | 0 | case GGML_OP_ROPE: |
2331 | 0 | case GGML_OP_ROPE_BACK: |
2332 | 0 | case GGML_OP_ADD_REL_POS: |
2333 | 0 | { |
2334 | 0 | n_tasks = n_threads; |
2335 | 0 | } break; |
2336 | 0 | case GGML_OP_CLAMP: |
2337 | 0 | { |
2338 | 0 | n_tasks = 1; //TODO |
2339 | 0 | } break; |
2340 | 0 | case GGML_OP_SOFT_MAX: |
2341 | 0 | { |
2342 | 0 | n_tasks = MIN(n_threads, ggml_nrows(node->src[0])); |
2343 | 0 | } break; |
2344 | 0 | case GGML_OP_IM2COL: |
2345 | 0 | case GGML_OP_IM2COL_BACK: |
2346 | 0 | case GGML_OP_IM2COL_3D: |
2347 | 0 | case GGML_OP_CONV_2D: |
2348 | 0 | case GGML_OP_CONV_3D: |
2349 | 0 | case GGML_OP_CONV_2D_DW: |
2350 | 0 | case GGML_OP_COL2IM_1D: |
2351 | 0 | case GGML_OP_CONV_TRANSPOSE_1D: |
2352 | 0 | case GGML_OP_CONV_TRANSPOSE_2D: |
2353 | 0 | { |
2354 | 0 | n_tasks = n_threads; |
2355 | 0 | } break; |
2356 | 0 | case GGML_OP_POOL_1D: |
2357 | 0 | case GGML_OP_POOL_2D: |
2358 | 0 | case GGML_OP_POOL_2D_BACK: |
2359 | 0 | { |
2360 | 0 | n_tasks = 1; |
2361 | 0 | } break; |
2362 | 0 | case GGML_OP_UPSCALE: |
2363 | 0 | case GGML_OP_PAD: |
2364 | 0 | case GGML_OP_PAD_REFLECT_1D: |
2365 | 0 | case GGML_OP_ROLL: |
2366 | 0 | case GGML_OP_ARANGE: |
2367 | 0 | case GGML_OP_TIMESTEP_EMBEDDING: |
2368 | 0 | case GGML_OP_ARGSORT: |
2369 | 0 | case GGML_OP_TOP_K: |
2370 | 0 | case GGML_OP_FLASH_ATTN_EXT: |
2371 | 0 | case GGML_OP_FLASH_ATTN_BACK: |
2372 | 0 | case GGML_OP_SSM_CONV: |
2373 | 0 | case GGML_OP_SSM_SCAN: |
2374 | 0 | { |
2375 | 0 | n_tasks = n_threads; |
2376 | 0 | } break; |
2377 | 0 | case GGML_OP_RWKV_WKV6: |
2378 | 0 | case GGML_OP_GATED_LINEAR_ATTN: |
2379 | 0 | case GGML_OP_RWKV_WKV7: |
2380 | 0 | { |
2381 | 0 | const int64_t n_heads = node->src[1]->ne[1]; |
2382 | 0 | n_tasks = MIN(n_threads, n_heads); |
2383 | 0 | } break; |
2384 | 0 | case GGML_OP_WIN_PART: |
2385 | 0 | case GGML_OP_WIN_UNPART: |
2386 | 0 | case GGML_OP_GET_REL_POS: |
2387 | 0 | { |
2388 | 0 | n_tasks = 1; |
2389 | 0 | } break; |
2390 | 0 | case GGML_OP_MAP_CUSTOM1: |
2391 | 0 | { |
2392 | 0 | struct ggml_map_custom1_op_params p; |
2393 | 0 | memcpy(&p, node->op_params, sizeof(p)); |
2394 | 0 | if (p.n_tasks == GGML_N_TASKS_MAX) { |
2395 | 0 | n_tasks = n_threads; |
2396 | 0 | } else { |
2397 | 0 | n_tasks = MIN(p.n_tasks, n_threads); |
2398 | 0 | } |
2399 | 0 | } break; |
2400 | 0 | case GGML_OP_MAP_CUSTOM2: |
2401 | 0 | { |
2402 | 0 | struct ggml_map_custom2_op_params p; |
2403 | 0 | memcpy(&p, node->op_params, sizeof(p)); |
2404 | 0 | if (p.n_tasks == GGML_N_TASKS_MAX) { |
2405 | 0 | n_tasks = n_threads; |
2406 | 0 | } else { |
2407 | 0 | n_tasks = MIN(p.n_tasks, n_threads); |
2408 | 0 | } |
2409 | 0 | } break; |
2410 | 0 | case GGML_OP_MAP_CUSTOM3: |
2411 | 0 | { |
2412 | 0 | struct ggml_map_custom3_op_params p; |
2413 | 0 | memcpy(&p, node->op_params, sizeof(p)); |
2414 | 0 | if (p.n_tasks == GGML_N_TASKS_MAX) { |
2415 | 0 | n_tasks = n_threads; |
2416 | 0 | } else { |
2417 | 0 | n_tasks = MIN(p.n_tasks, n_threads); |
2418 | 0 | } |
2419 | 0 | } break; |
2420 | 0 | case GGML_OP_CUSTOM: |
2421 | 0 | { |
2422 | 0 | struct ggml_custom_op_params p; |
2423 | 0 | memcpy(&p, node->op_params, sizeof(p)); |
2424 | 0 | if (p.n_tasks == GGML_N_TASKS_MAX) { |
2425 | 0 | n_tasks = n_threads; |
2426 | 0 | } else { |
2427 | 0 | n_tasks = MIN(p.n_tasks, n_threads); |
2428 | 0 | } |
2429 | 0 | } break; |
2430 | 0 | case GGML_OP_CROSS_ENTROPY_LOSS: |
2431 | 0 | case GGML_OP_CROSS_ENTROPY_LOSS_BACK: |
2432 | 0 | case GGML_OP_OPT_STEP_ADAMW: |
2433 | 0 | case GGML_OP_OPT_STEP_SGD: |
2434 | 0 | { |
2435 | 0 | n_tasks = n_threads; |
2436 | 0 | } break; |
2437 | 0 | case GGML_OP_NONE: |
2438 | 0 | { |
2439 | 0 | n_tasks = 1; |
2440 | 0 | } break; |
2441 | 0 | case GGML_OP_COUNT: |
2442 | 0 | { |
2443 | 0 | GGML_ABORT("fatal error"); |
2444 | 0 | } |
2445 | 0 | default: |
2446 | 0 | { |
2447 | 0 | fprintf(stderr, "%s: op not implemented: ", __func__); |
2448 | 0 | if (node->op < GGML_OP_COUNT) { |
2449 | 0 | fprintf(stderr, "%s\n", ggml_op_name(node->op)); |
2450 | 0 | } else { |
2451 | 0 | fprintf(stderr, "%d\n", node->op); |
2452 | 0 | } |
2453 | 0 | GGML_ABORT("fatal error"); |
2454 | 0 | } |
2455 | 0 | } |
2456 | | |
2457 | 0 | assert(n_tasks > 0); |
2458 | |
|
2459 | 0 | return n_tasks; |
2460 | 0 | } |
2461 | | |
2462 | | static thread_ret_t ggml_graph_compute_secondary_thread(void* data); |
2463 | | |
2464 | | #if defined(_WIN32) |
2465 | | #include "windows.h" |
2466 | | |
2467 | | // TODO: support > 64 CPUs |
2468 | | static bool ggml_thread_apply_affinity(bool * mask) { |
2469 | | HANDLE h = GetCurrentThread(); |
2470 | | uint64_t bitmask = 0ULL; |
2471 | | |
2472 | | assert(GGML_MAX_N_THREADS >= 64); |
2473 | | |
2474 | | for (int32_t i = 0; i < 8; i++) { |
2475 | | int32_t idx = i * 8; |
2476 | | uint8_t val = 0; |
2477 | | val |= mask[idx + 0] << 0; |
2478 | | val |= mask[idx + 1] << 1; |
2479 | | val |= mask[idx + 2] << 2; |
2480 | | val |= mask[idx + 3] << 3; |
2481 | | val |= mask[idx + 4] << 4; |
2482 | | val |= mask[idx + 5] << 5; |
2483 | | val |= mask[idx + 6] << 6; |
2484 | | val |= mask[idx + 7] << 7; |
2485 | | bitmask |= (uint64_t)val << idx; |
2486 | | } |
2487 | | |
2488 | | for (int32_t i = 64; i < GGML_MAX_N_THREADS; i++) { |
2489 | | if (mask[i]) { |
2490 | | fprintf(stderr, "warn: setting thread-affinity for > 64 CPUs isn't supported on windows!\n"); |
2491 | | break; |
2492 | | } |
2493 | | } |
2494 | | |
2495 | | DWORD_PTR m = (DWORD_PTR)bitmask; |
2496 | | |
2497 | | m = SetThreadAffinityMask(h, m); |
2498 | | |
2499 | | return m != 0; |
2500 | | } |
2501 | | |
2502 | | static bool ggml_thread_apply_priority(int32_t prio) { |
2503 | | // Note that on Windows the Process Priority Class must be updated in order to set Thread priority. |
2504 | | // This is up to the applications. |
2505 | | DWORD p = THREAD_PRIORITY_NORMAL; |
2506 | | switch (prio) { |
2507 | | case GGML_SCHED_PRIO_LOW: p = THREAD_PRIORITY_BELOW_NORMAL; break; |
2508 | | case GGML_SCHED_PRIO_NORMAL: p = THREAD_PRIORITY_NORMAL; break; |
2509 | | case GGML_SCHED_PRIO_MEDIUM: p = THREAD_PRIORITY_ABOVE_NORMAL; break; |
2510 | | case GGML_SCHED_PRIO_HIGH: p = THREAD_PRIORITY_HIGHEST; break; |
2511 | | case GGML_SCHED_PRIO_REALTIME: p = THREAD_PRIORITY_TIME_CRITICAL; break; |
2512 | | } |
2513 | | |
2514 | | if (prio != GGML_SCHED_PRIO_LOW) { |
2515 | | // Tell Windows that this thread should not be throttled (needs its own CPU core). |
2516 | | // Newer Windows 11 versions aggressively park (offline) CPU cores and often place |
2517 | | // all our threads onto the first 4 cores which results in terrible performance with |
2518 | | // n_threads > 4 |
2519 | | #if _WIN32_WINNT >= 0x0602 |
2520 | | THREAD_POWER_THROTTLING_STATE t; |
2521 | | ZeroMemory(&t, sizeof(t)); |
2522 | | t.Version = THREAD_POWER_THROTTLING_CURRENT_VERSION; |
2523 | | t.ControlMask = THREAD_POWER_THROTTLING_EXECUTION_SPEED; |
2524 | | t.StateMask = 0; |
2525 | | |
2526 | | if (!SetThreadInformation(GetCurrentThread(), ThreadPowerThrottling, &t, sizeof(t))) { |
2527 | | GGML_LOG_DEBUG("failed to disable thread power throttling %d : (%d)\n", prio, (int) GetLastError()); |
2528 | | return false; |
2529 | | } |
2530 | | #endif |
2531 | | } |
2532 | | |
2533 | | if (prio == GGML_SCHED_PRIO_NORMAL) { |
2534 | | // Keep inherited policy/priority |
2535 | | return true; |
2536 | | } |
2537 | | |
2538 | | if (!SetThreadPriority(GetCurrentThread(), p)) { |
2539 | | fprintf(stderr, "warn: failed to set thread priority %d : (%d)\n", prio, (int) GetLastError()); |
2540 | | return false; |
2541 | | } |
2542 | | |
2543 | | return true; |
2544 | | } |
2545 | | |
2546 | | #elif defined(__APPLE__) |
2547 | | #include <sys/types.h> |
2548 | | #include <sys/resource.h> |
2549 | | |
2550 | | static bool ggml_thread_apply_affinity(const bool * mask) { |
2551 | | // Not supported on Apple platforms |
2552 | | UNUSED(mask); |
2553 | | return true; |
2554 | | } |
2555 | | |
2556 | | static bool ggml_thread_apply_priority(int32_t prio) { |
2557 | | struct sched_param p; |
2558 | | int32_t policy = SCHED_OTHER; |
2559 | | switch (prio) { |
2560 | | // TODO: there seems to be no way to set lower prio on Apple platforms |
2561 | | case GGML_SCHED_PRIO_LOW: policy = SCHED_OTHER; p.sched_priority = 0; break; |
2562 | | case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break; |
2563 | | case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break; |
2564 | | case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break; |
2565 | | case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break; |
2566 | | } |
2567 | | |
2568 | | if (prio == GGML_SCHED_PRIO_NORMAL) { |
2569 | | // Keep inherited policy/priority |
2570 | | return true; |
2571 | | } |
2572 | | |
2573 | | int32_t err = pthread_setschedparam(pthread_self(), policy, &p); |
2574 | | if (err != 0) { |
2575 | | fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err); |
2576 | | return false; |
2577 | | } |
2578 | | |
2579 | | return true; |
2580 | | } |
2581 | | |
2582 | | #elif defined(__gnu_linux__) |
2583 | | // TODO: this may not work on BSD, to be verified |
2584 | | |
2585 | 0 | static bool ggml_thread_apply_affinity(const bool * mask) { |
2586 | 0 | cpu_set_t cpuset; |
2587 | 0 | int err; |
2588 | |
|
2589 | 0 | CPU_ZERO(&cpuset); |
2590 | |
|
2591 | 0 | for (uint32_t i = 0; i < GGML_MAX_N_THREADS; i++) { |
2592 | 0 | if (mask[i]) { |
2593 | 0 | GGML_PRINT_DEBUG("Thread %lx: adding %d to cpuset\n", pthread_self(), i); |
2594 | 0 | CPU_SET(i, &cpuset); |
2595 | 0 | } |
2596 | 0 | } |
2597 | |
|
2598 | | #ifdef __ANDROID__ |
2599 | | err = sched_setaffinity(0, sizeof(cpuset), &cpuset); |
2600 | | if (err < 0) { |
2601 | | err = errno; |
2602 | | } |
2603 | | #else |
2604 | 0 | err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset); |
2605 | 0 | #endif |
2606 | 0 | if (err != 0) { |
2607 | 0 | fprintf(stderr, "warn: failed to set affinity mask 0x%llx : %s (%d)\n", (unsigned long long)mask, strerror(err), err); |
2608 | 0 | return false; |
2609 | 0 | } |
2610 | | |
2611 | 0 | return true; |
2612 | 0 | } |
2613 | | |
2614 | 0 | static bool ggml_thread_apply_priority(int32_t prio) { |
2615 | 0 | struct sched_param p; |
2616 | 0 | int32_t policy = SCHED_OTHER; |
2617 | 0 | switch (prio) { |
2618 | 0 | case GGML_SCHED_PRIO_LOW: policy = SCHED_BATCH; p.sched_priority = 0; break; |
2619 | 0 | case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break; |
2620 | 0 | case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break; |
2621 | 0 | case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break; |
2622 | 0 | case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break; |
2623 | 0 | } |
2624 | | |
2625 | 0 | if (prio == GGML_SCHED_PRIO_NORMAL) { |
2626 | | // Keep inherited policy/priority |
2627 | 0 | return true; |
2628 | 0 | } |
2629 | | |
2630 | 0 | int32_t err = pthread_setschedparam(pthread_self(), policy, &p); |
2631 | 0 | if (err != 0) { |
2632 | 0 | fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err); |
2633 | 0 | return false; |
2634 | 0 | } |
2635 | | |
2636 | 0 | return true; |
2637 | 0 | } |
2638 | | |
2639 | | #else // unsupported platforms |
2640 | | |
2641 | | static bool ggml_thread_apply_affinity(const bool * mask) { |
2642 | | UNUSED(mask); |
2643 | | return true; |
2644 | | } |
2645 | | |
2646 | | static bool ggml_thread_apply_priority(int32_t prio) { |
2647 | | UNUSED(prio); |
2648 | | return true; |
2649 | | } |
2650 | | |
2651 | | #endif |
2652 | | |
2653 | 0 | static bool ggml_thread_cpumask_is_valid(const bool * mask) { |
2654 | 0 | for (int i = 0; i < GGML_MAX_N_THREADS; i++) { |
2655 | 0 | if (mask[i]) { return true; } |
2656 | 0 | } |
2657 | 0 | return false; |
2658 | 0 | } |
2659 | | |
2660 | 0 | static void ggml_thread_cpumask_next(const bool * global_mask, bool * local_mask, bool strict, int32_t* iter) { |
2661 | 0 | if (!strict) { |
2662 | 0 | memcpy(local_mask, global_mask, GGML_MAX_N_THREADS); |
2663 | 0 | return; |
2664 | 0 | } else { |
2665 | 0 | memset(local_mask, 0, GGML_MAX_N_THREADS); |
2666 | 0 | int32_t base_idx = *iter; |
2667 | 0 | for (int32_t i = 0; i < GGML_MAX_N_THREADS; i++) { |
2668 | 0 | int32_t idx = base_idx + i; |
2669 | 0 | if (idx >= GGML_MAX_N_THREADS) { |
2670 | | // Just a cheaper modulo |
2671 | 0 | idx -= GGML_MAX_N_THREADS; |
2672 | 0 | } |
2673 | 0 | if (global_mask[idx]) { |
2674 | 0 | local_mask[idx] = 1; |
2675 | 0 | *iter = idx + 1; |
2676 | 0 | return; |
2677 | 0 | } |
2678 | 0 | } |
2679 | 0 | } |
2680 | 0 | } |
2681 | | |
2682 | 0 | void ggml_threadpool_free(struct ggml_threadpool* threadpool) { |
2683 | 0 | if (!threadpool) return; |
2684 | | |
2685 | 0 | const int n_threads = threadpool->n_threads; |
2686 | |
|
2687 | 0 | #ifndef GGML_USE_OPENMP |
2688 | 0 | struct ggml_compute_state* workers = threadpool->workers; |
2689 | |
|
2690 | 0 | ggml_mutex_lock(&threadpool->mutex); |
2691 | |
|
2692 | 0 | threadpool->stop = true; |
2693 | 0 | threadpool->pause = false; |
2694 | |
|
2695 | 0 | ggml_cond_broadcast(&threadpool->cond); |
2696 | 0 | ggml_mutex_unlock(&threadpool->mutex); |
2697 | |
|
2698 | 0 | for (int j = 1; j < n_threads; j++) { |
2699 | 0 | int32_t rc = ggml_thread_join(workers[j].thrd, NULL); |
2700 | 0 | GGML_ASSERT(rc == GGML_EXIT_SUCCESS || rc == GGML_EXIT_ABORTED); |
2701 | 0 | UNUSED(rc); |
2702 | 0 | } |
2703 | |
|
2704 | 0 | ggml_mutex_destroy(&threadpool->mutex); |
2705 | 0 | ggml_cond_destroy(&threadpool->cond); |
2706 | 0 | #endif // GGML_USE_OPENMP |
2707 | |
|
2708 | 0 | const size_t workers_size = sizeof(struct ggml_compute_state) * n_threads; |
2709 | 0 | ggml_aligned_free(threadpool->workers, workers_size); |
2710 | 0 | ggml_aligned_free(threadpool, sizeof(struct ggml_threadpool)); |
2711 | 0 | } |
2712 | | |
2713 | | #ifndef GGML_USE_OPENMP |
2714 | | // pause/resume must be called under mutex |
2715 | 0 | static void ggml_threadpool_pause_locked(struct ggml_threadpool * threadpool) { |
2716 | 0 | GGML_PRINT_DEBUG("Pausing threadpool\n"); |
2717 | 0 | threadpool->pause = true; |
2718 | 0 | ggml_cond_broadcast(&threadpool->cond); |
2719 | 0 | } |
2720 | | |
2721 | 0 | static void ggml_threadpool_resume_locked(struct ggml_threadpool * threadpool) { |
2722 | 0 | GGML_PRINT_DEBUG("Resuming threadpool\n"); |
2723 | 0 | threadpool->pause = false; |
2724 | 0 | ggml_cond_broadcast(&threadpool->cond); |
2725 | 0 | } |
2726 | | #endif |
2727 | | |
2728 | 0 | void ggml_threadpool_pause(struct ggml_threadpool * threadpool) { |
2729 | 0 | #ifndef GGML_USE_OPENMP |
2730 | 0 | ggml_mutex_lock(&threadpool->mutex); |
2731 | 0 | if (!threadpool->pause) { |
2732 | 0 | ggml_threadpool_pause_locked(threadpool); |
2733 | 0 | } |
2734 | 0 | ggml_mutex_unlock(&threadpool->mutex); |
2735 | | #else |
2736 | | UNUSED(threadpool); |
2737 | | #endif |
2738 | 0 | } |
2739 | | |
2740 | 0 | void ggml_threadpool_resume(struct ggml_threadpool * threadpool) { |
2741 | 0 | #ifndef GGML_USE_OPENMP |
2742 | 0 | ggml_mutex_lock(&threadpool->mutex); |
2743 | 0 | if (threadpool->pause) { |
2744 | 0 | ggml_threadpool_resume_locked(threadpool); |
2745 | 0 | } |
2746 | 0 | ggml_mutex_unlock(&threadpool->mutex); |
2747 | | #else |
2748 | | UNUSED(threadpool); |
2749 | | #endif |
2750 | 0 | } |
2751 | | |
2752 | | struct ggml_cplan ggml_graph_plan( |
2753 | | const struct ggml_cgraph * cgraph, |
2754 | | int n_threads, |
2755 | 0 | struct ggml_threadpool * threadpool) { |
2756 | |
|
2757 | 0 | if (threadpool == NULL) { |
2758 | | //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads); |
2759 | 0 | } |
2760 | 0 | if (n_threads <= 0) { |
2761 | 0 | n_threads = threadpool ? threadpool->n_threads : GGML_DEFAULT_N_THREADS; |
2762 | 0 | } |
2763 | |
|
2764 | | #if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__) |
2765 | | // Emscripten without pthreads support can only use a single thread |
2766 | | n_threads = 1; |
2767 | | #endif |
2768 | |
|
2769 | 0 | size_t work_size = 0; |
2770 | |
|
2771 | 0 | struct ggml_cplan cplan; |
2772 | 0 | memset(&cplan, 0, sizeof(struct ggml_cplan)); |
2773 | |
|
2774 | 0 | int max_tasks = 1; |
2775 | | |
2776 | | // thread scheduling for the different operations + work buffer size estimation |
2777 | 0 | for (int i = 0; i < cgraph->n_nodes; i++) { |
2778 | 0 | struct ggml_tensor * node = cgraph->nodes[i]; |
2779 | |
|
2780 | 0 | const int n_tasks = ggml_get_n_tasks(node, n_threads); |
2781 | |
|
2782 | 0 | max_tasks = MAX(max_tasks, n_tasks); |
2783 | |
|
2784 | 0 | size_t cur = 0; |
2785 | |
|
2786 | 0 | if (!ggml_cpu_extra_work_size(n_threads, node, &cur)) { |
2787 | 0 | switch (node->op) { |
2788 | 0 | case GGML_OP_CPY: |
2789 | 0 | case GGML_OP_DUP: |
2790 | 0 | { |
2791 | 0 | if (ggml_is_quantized(node->type) || |
2792 | | // F16 -> BF16 and BF16 -> F16 copies go through intermediate F32 |
2793 | 0 | (node->src[0]->type == GGML_TYPE_F16 && node->src[1] && node->src[1]->type == GGML_TYPE_BF16) || |
2794 | 0 | (node->src[0]->type == GGML_TYPE_BF16 && node->src[1] && node->src[1]->type == GGML_TYPE_F16) || |
2795 | | // conversion between F32 and I32 |
2796 | 0 | (node->src[0]->type == GGML_TYPE_F32 && node->src[1] && node->src[1]->type == GGML_TYPE_I32) || |
2797 | 0 | (node->src[0]->type == GGML_TYPE_I32 && node->src[1] && node->src[1]->type == GGML_TYPE_F32)) { |
2798 | 0 | cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks; |
2799 | 0 | } |
2800 | 0 | } break; |
2801 | 0 | case GGML_OP_ADD: |
2802 | 0 | case GGML_OP_ADD_ID: |
2803 | 0 | case GGML_OP_ADD1: |
2804 | 0 | { |
2805 | 0 | if (ggml_is_quantized(node->src[0]->type)) { |
2806 | 0 | cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks; |
2807 | 0 | } |
2808 | 0 | } break; |
2809 | 0 | case GGML_OP_ACC: |
2810 | 0 | { |
2811 | 0 | if (ggml_is_quantized(node->src[0]->type)) { |
2812 | 0 | cur = ggml_type_size(GGML_TYPE_F32) * node->src[1]->ne[0] * n_tasks; |
2813 | 0 | } |
2814 | 0 | } break; |
2815 | 0 | case GGML_OP_COUNT_EQUAL: |
2816 | 0 | { |
2817 | 0 | cur = ggml_type_size(node->type)*n_tasks; |
2818 | 0 | } break; |
2819 | 0 | case GGML_OP_MUL_MAT: |
2820 | 0 | { |
2821 | 0 | const enum ggml_type vec_dot_type = type_traits_cpu[node->src[0]->type].vec_dot_type; |
2822 | |
|
2823 | 0 | if (node->src[1]->type != vec_dot_type) { |
2824 | 0 | cur = ggml_row_size(vec_dot_type, ggml_nelements(node->src[1])); |
2825 | 0 | } |
2826 | 0 | } break; |
2827 | 0 | case GGML_OP_MUL_MAT_ID: |
2828 | 0 | { |
2829 | 0 | cur = 0; |
2830 | 0 | const struct ggml_tensor * src0 = node->src[0]; |
2831 | 0 | const struct ggml_tensor * src1 = node->src[1]; |
2832 | 0 | const struct ggml_tensor * ids = node->src[2]; |
2833 | 0 | const enum ggml_type vec_dot_type = type_traits_cpu[src0->type].vec_dot_type; |
2834 | 0 | const int n_as = src0->ne[2]; |
2835 | | // src1 |
2836 | 0 | if (src1->type != vec_dot_type) { |
2837 | 0 | cur += ggml_row_size(vec_dot_type, ggml_nelements(src1)) + sizeof(int64_t); |
2838 | 0 | } |
2839 | | // matrix_row_counts |
2840 | 0 | cur += n_as * sizeof(int64_t) + sizeof(int64_t); |
2841 | | // matrix_rows |
2842 | 0 | cur += n_as*ids->ne[0]*ids->ne[1]*sizeof(struct mmid_row_mapping) + sizeof(int64_t); |
2843 | | // atomic_current_chunk |
2844 | 0 | cur += CACHE_LINE_SIZE*n_as + CACHE_LINE_SIZE; |
2845 | 0 | } break; |
2846 | 0 | case GGML_OP_OUT_PROD: |
2847 | 0 | { |
2848 | 0 | if (ggml_is_quantized(node->src[0]->type)) { |
2849 | 0 | cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks; |
2850 | 0 | } |
2851 | 0 | } break; |
2852 | 0 | case GGML_OP_SOFT_MAX: |
2853 | 0 | case GGML_OP_ROPE: |
2854 | 0 | case GGML_OP_ROPE_BACK: |
2855 | 0 | { |
2856 | 0 | cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks; |
2857 | 0 | } break; |
2858 | 0 | case GGML_OP_CONV_TRANSPOSE_1D: |
2859 | 0 | { |
2860 | 0 | GGML_ASSERT(node->src[0]->ne[3] == 1); |
2861 | 0 | GGML_ASSERT(node->src[1]->ne[2] == 1); |
2862 | 0 | GGML_ASSERT(node->src[1]->ne[3] == 1); |
2863 | |
|
2864 | 0 | const int64_t ne00 = node->src[0]->ne[0]; // K |
2865 | 0 | const int64_t ne01 = node->src[0]->ne[1]; // Cout |
2866 | 0 | const int64_t ne02 = node->src[0]->ne[2]; // Cin |
2867 | 0 | const int64_t ne10 = node->src[1]->ne[0]; // L |
2868 | 0 | const int64_t ne11 = node->src[1]->ne[1]; // Cin |
2869 | |
|
2870 | 0 | if ((node->src[0]->type == GGML_TYPE_F16 || |
2871 | 0 | node->src[0]->type == GGML_TYPE_BF16) && |
2872 | 0 | node->src[1]->type == GGML_TYPE_F32) { |
2873 | 0 | cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02; |
2874 | 0 | cur += sizeof(ggml_fp16_t)*ne10*ne11; |
2875 | 0 | } else if (node->src[0]->type == GGML_TYPE_F32 && |
2876 | 0 | node->src[1]->type == GGML_TYPE_F32) { |
2877 | 0 | cur += sizeof(float)*ne00*ne01*ne02; |
2878 | 0 | cur += sizeof(float)*ne10*ne11; |
2879 | 0 | } else { |
2880 | 0 | GGML_ABORT("fatal error"); |
2881 | 0 | } |
2882 | 0 | } break; |
2883 | 0 | case GGML_OP_CONV_2D: |
2884 | 0 | case GGML_OP_CONV_3D: |
2885 | 0 | { |
2886 | 0 | cur = GGML_IM2COL_WORK_SIZE; |
2887 | 0 | } break; |
2888 | 0 | case GGML_OP_CONV_TRANSPOSE_2D: |
2889 | 0 | { |
2890 | 0 | const int64_t ne00 = node->src[0]->ne[0]; // W |
2891 | 0 | const int64_t ne01 = node->src[0]->ne[1]; // H |
2892 | 0 | const int64_t ne02 = node->src[0]->ne[2]; // Channels Out |
2893 | 0 | const int64_t ne03 = node->src[0]->ne[3]; // Channels In |
2894 | |
|
2895 | 0 | const int64_t ne10 = node->src[1]->ne[0]; // W |
2896 | 0 | const int64_t ne11 = node->src[1]->ne[1]; // H |
2897 | 0 | const int64_t ne12 = node->src[1]->ne[2]; // Channels In |
2898 | |
|
2899 | 0 | GGML_ASSERT(node->src[0]->type == GGML_TYPE_F16 || node->src[0]->type == GGML_TYPE_F32); |
2900 | 0 | GGML_ASSERT(node->src[1]->type == GGML_TYPE_F32); |
2901 | |
|
2902 | 0 | cur += ggml_type_size(node->src[0]->type) * ne00 * ne01 * ne02 * ne03; |
2903 | 0 | cur += ggml_type_size(node->src[0]->type) * ne10 * ne11 * ne12; |
2904 | |
|
2905 | 0 | } break; |
2906 | 0 | case GGML_OP_TOP_K: |
2907 | 0 | { |
2908 | 0 | cur += sizeof(int32_t)*node->src[0]->ne[0]*n_tasks; |
2909 | 0 | } break; |
2910 | 0 | case GGML_OP_FLASH_ATTN_EXT: |
2911 | 0 | { |
2912 | 0 | const int64_t neq2 = node->src[0]->ne[2]; // number of query heads |
2913 | 0 | const int64_t DK = node->src[1]->ne[0]; |
2914 | 0 | const int64_t DV = node->src[2]->ne[0]; |
2915 | | |
2916 | | // Tiled flash attention scratch (tile sizes defined in common.h) |
2917 | | // Per-thread: Q_q + KQ + mask + VKQ32 + V32 + K_f32 + padding |
2918 | 0 | size_t prefill = sizeof(float)*(GGML_FA_TILE_Q*DK + 2*GGML_FA_TILE_Q*GGML_FA_TILE_KV + GGML_FA_TILE_Q*DV + GGML_FA_TILE_KV*DV + GGML_FA_TILE_KV*DK)*n_tasks; |
2919 | | |
2920 | | // Decode path: n_kv_chunks = n_tasks (one chunk per thread) |
2921 | | // Per-thread: VKQ accmulator (DV), partial M, partial S + intra-thread scratch for V, Q and VKQ |
2922 | 0 | size_t n_chunks = n_tasks; |
2923 | 0 | size_t decode = sizeof(float)*(neq2*n_chunks*(2+DV) + n_tasks*(DK + 2*DV)); |
2924 | |
|
2925 | 0 | cur += MAX(prefill, decode); |
2926 | 0 | } break; |
2927 | 0 | case GGML_OP_FLASH_ATTN_BACK: |
2928 | 0 | { |
2929 | 0 | const int64_t D = node->src[0]->ne[0]; |
2930 | 0 | const int64_t ne11 = ggml_up(node->src[1]->ne[1], GGML_SOFT_MAX_UNROLL); |
2931 | 0 | const int64_t mxDn = MAX(D, ne11) * 2; // *2 because of S and SM in ggml_compute_forward_flash_attn_back |
2932 | 0 | if (node->src[1]->type == GGML_TYPE_F32) { |
2933 | 0 | cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1) |
2934 | 0 | cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2 |
2935 | 0 | } else if (node->src[1]->type == GGML_TYPE_F16) { |
2936 | 0 | cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1) |
2937 | 0 | cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2 |
2938 | 0 | } else if (node->src[1]->type == GGML_TYPE_BF16) { |
2939 | 0 | cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1) |
2940 | 0 | cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2 |
2941 | 0 | } |
2942 | 0 | } break; |
2943 | | |
2944 | 0 | case GGML_OP_CROSS_ENTROPY_LOSS: |
2945 | 0 | { |
2946 | 0 | cur = ggml_type_size(node->type)*(n_tasks + node->src[0]->ne[0]*n_tasks); |
2947 | 0 | } break; |
2948 | 0 | case GGML_OP_GATED_DELTA_NET: |
2949 | 0 | { |
2950 | 0 | const int64_t S_v = node->src[2]->ne[0]; |
2951 | 0 | const int64_t K = ggml_get_op_params_i32(node, 0); |
2952 | 0 | const int64_t per_thread = S_v + (K > 1 ? S_v * S_v : 0); |
2953 | 0 | cur = per_thread * sizeof(float) * n_tasks; |
2954 | 0 | } break; |
2955 | 0 | case GGML_OP_COUNT: |
2956 | 0 | { |
2957 | 0 | GGML_ABORT("fatal error"); |
2958 | 0 | } |
2959 | 0 | default: |
2960 | 0 | break; |
2961 | 0 | } |
2962 | 0 | } |
2963 | | |
2964 | 0 | work_size = MAX(work_size, cur); |
2965 | 0 | } |
2966 | | |
2967 | 0 | if (work_size > 0) { |
2968 | 0 | work_size += CACHE_LINE_SIZE*(n_threads); |
2969 | 0 | } |
2970 | |
|
2971 | 0 | cplan.threadpool = threadpool; |
2972 | 0 | cplan.n_threads = MIN(max_tasks, n_threads); |
2973 | 0 | cplan.work_size = work_size; |
2974 | 0 | cplan.work_data = NULL; |
2975 | |
|
2976 | 0 | return cplan; |
2977 | 0 | } |
2978 | | |
2979 | | |
2980 | | // Try to fuse the current node with subsequent nodes for better performance. |
2981 | | // Returns the number of nodes skipped by fusion (>=1), or 0 if no fusion was applied. |
2982 | | static bool ggml_cpu_disable_fusion = false; // initialized once in ggml_cpu_init(), read-only afterwards |
2983 | | |
2984 | | static int ggml_cpu_try_fuse_ops( |
2985 | | const struct ggml_cgraph * cgraph, |
2986 | | const int node_n, |
2987 | | const struct ggml_compute_params * params, |
2988 | 0 | const struct ggml_cplan * cplan) { |
2989 | |
|
2990 | 0 | if (ggml_cpu_disable_fusion || cplan->use_ref) { |
2991 | 0 | return 0; |
2992 | 0 | } |
2993 | | |
2994 | 0 | struct ggml_tensor * node = cgraph->nodes[node_n]; |
2995 | |
|
2996 | 0 | if (node->op == GGML_OP_RMS_NORM) { |
2997 | | // RMS_NORM + MUL fusion |
2998 | 0 | const enum ggml_op fuse_ops[] = { GGML_OP_RMS_NORM, GGML_OP_MUL }; |
2999 | 0 | if (ggml_can_fuse(cgraph, node_n, fuse_ops, 2)) { |
3000 | 0 | struct ggml_tensor * mul_node = cgraph->nodes[node_n + 1]; |
3001 | 0 | const struct ggml_tensor * mul_w = (mul_node->src[0] == node) |
3002 | 0 | ? mul_node->src[1] : mul_node->src[0]; |
3003 | 0 | if (node->src[0]->type == GGML_TYPE_F32 && |
3004 | 0 | mul_node->type == GGML_TYPE_F32 && |
3005 | 0 | mul_w->type == GGML_TYPE_F32 && |
3006 | 0 | mul_w->ne[0] == node->ne[0] && |
3007 | 0 | mul_w->nb[0] == sizeof(float)) { |
3008 | |
|
3009 | 0 | ggml_compute_forward_rms_norm_mul_fused(params, node, mul_node); |
3010 | 0 | return 1; |
3011 | 0 | } |
3012 | 0 | } |
3013 | 0 | } |
3014 | | |
3015 | 0 | return 0; |
3016 | 0 | } |
3017 | | |
3018 | 0 | static thread_ret_t ggml_graph_compute_thread(void * data) { |
3019 | 0 | struct ggml_compute_state * state = (struct ggml_compute_state *) data; |
3020 | 0 | struct ggml_threadpool * tp = state->threadpool; |
3021 | |
|
3022 | 0 | const struct ggml_cgraph * cgraph = tp->cgraph; |
3023 | 0 | const struct ggml_cplan * cplan = tp->cplan; |
3024 | |
|
3025 | | #ifdef GGML_USE_CPU_RISCV64_SPACEMIT |
3026 | | ggml_backend_cpu_riscv64_spacemit_set_numa_thread_affinity(state->ith); |
3027 | | #else |
3028 | 0 | set_numa_thread_affinity(state->ith); |
3029 | 0 | #endif |
3030 | |
|
3031 | 0 | struct ggml_compute_params params = { |
3032 | 0 | /*.ith =*/ state->ith, |
3033 | 0 | /*.nth =*/ atomic_load_explicit(&tp->n_graph, memory_order_relaxed) & GGML_THREADPOOL_N_THREADS_MASK, |
3034 | 0 | /*.wsize =*/ cplan->work_size, |
3035 | 0 | /*.wdata =*/ cplan->work_data, |
3036 | 0 | /*.threadpool =*/ tp, |
3037 | 0 | /*.use_ref =*/ cplan->use_ref, |
3038 | 0 | }; |
3039 | |
|
3040 | | #ifdef GGML_USE_OPENMP |
3041 | | GGML_PRINT_DEBUG("thread #%d compute-start cplan %p\n", state->ith, (const void *)cplan); |
3042 | | #else |
3043 | 0 | GGML_PRINT_DEBUG("thread #%d compute-start cplan %p last-graph %d\n", state->ith, (const void *)cplan, state->last_graph); |
3044 | 0 | #endif |
3045 | |
|
3046 | 0 | for (int node_n = 0; node_n < cgraph->n_nodes && atomic_load_explicit(&tp->abort, memory_order_relaxed) != node_n; node_n++) { |
3047 | 0 | struct ggml_tensor * node = cgraph->nodes[node_n]; |
3048 | |
|
3049 | 0 | if (ggml_op_is_empty(node->op)) { |
3050 | | // skip NOPs |
3051 | 0 | continue; |
3052 | 0 | } |
3053 | | |
3054 | 0 | if ((node->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) { |
3055 | 0 | continue; |
3056 | 0 | } |
3057 | | |
3058 | | // TODO: move fused-op detection into ggml_graph_plan so fusion decisions are made once at planning time |
3059 | | // Try fused ops, fall back to normal compute |
3060 | 0 | const int n_fused = ggml_cpu_try_fuse_ops(cgraph, node_n, ¶ms, cplan); |
3061 | 0 | if (n_fused > 0) { |
3062 | 0 | node_n += n_fused; |
3063 | 0 | } else { |
3064 | 0 | ggml_compute_forward(¶ms, node); |
3065 | 0 | } |
3066 | |
|
3067 | 0 | if (state->ith == 0 && cplan->abort_callback && |
3068 | 0 | cplan->abort_callback(cplan->abort_callback_data)) { |
3069 | 0 | atomic_store_explicit(&tp->abort, node_n + 1, memory_order_relaxed); |
3070 | 0 | tp->ec = GGML_STATUS_ABORTED; |
3071 | 0 | } |
3072 | |
|
3073 | 0 | if (node_n + 1 < cgraph->n_nodes) { |
3074 | 0 | ggml_barrier(state->threadpool); |
3075 | 0 | } |
3076 | 0 | } |
3077 | |
|
3078 | | #ifdef GGML_USE_OPENMP |
3079 | | GGML_PRINT_DEBUG("thread #%d compute-done cplan %p\n", state->ith, (const void *)cplan); |
3080 | | #else |
3081 | 0 | GGML_PRINT_DEBUG("thread #%d compute-done cplan %p last-graph %d\n", state->ith, (const void *)cplan, state->last_graph); |
3082 | 0 | #endif |
3083 | |
|
3084 | 0 | ggml_barrier(state->threadpool); |
3085 | |
|
3086 | | #ifdef GGML_USE_CPU_RISCV64_SPACEMIT |
3087 | | ggml_backend_cpu_riscv64_spacemit_clear_numa_thread_affinity_threaded(state->ith); |
3088 | | #endif |
3089 | |
|
3090 | 0 | return 0; |
3091 | 0 | } |
3092 | | |
3093 | | #ifndef GGML_USE_OPENMP |
3094 | | |
3095 | | // check if thread is ready to proceed (exit from polling or sleeping) |
3096 | | // returns true if loops should exit, sets state->pending to indicate new work |
3097 | 0 | static inline bool ggml_graph_compute_thread_ready(struct ggml_compute_state * state) { |
3098 | 0 | struct ggml_threadpool * threadpool = state->threadpool; |
3099 | |
|
3100 | 0 | if (state->pending || threadpool->stop || threadpool->pause) { return true; } |
3101 | | |
3102 | | // check for new graph/work |
3103 | 0 | int n_graph = atomic_load_explicit(&threadpool->n_graph, memory_order_relaxed); |
3104 | 0 | int n_threads = n_graph & GGML_THREADPOOL_N_THREADS_MASK; |
3105 | 0 | if (n_graph != state->last_graph) { |
3106 | 0 | state->pending = (state->ith < n_threads); |
3107 | 0 | state->last_graph = n_graph; |
3108 | 0 | return true; |
3109 | 0 | } |
3110 | | |
3111 | 0 | return false; |
3112 | 0 | } |
3113 | | |
3114 | | // sync thread state after polling |
3115 | 0 | static inline void ggml_graph_compute_thread_sync(struct ggml_compute_state * state) { |
3116 | | // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead |
3117 | | #ifdef GGML_TSAN_ENABLED |
3118 | | atomic_fetch_add_explicit(&state->threadpool->n_graph, 0, memory_order_seq_cst); |
3119 | | #else |
3120 | 0 | atomic_thread_fence(memory_order_seq_cst); |
3121 | 0 | #endif |
3122 | 0 | UNUSED(state); |
3123 | 0 | } |
3124 | | |
3125 | 0 | static inline bool ggml_graph_compute_poll_for_work(struct ggml_compute_state * state) { |
3126 | 0 | struct ggml_threadpool * threadpool = state->threadpool; |
3127 | | |
3128 | | // This seems to make 0 ... 100 a decent range for polling level across modern processors. |
3129 | | // Perhaps, we can adjust it dynamically based on load and things. |
3130 | 0 | const uint64_t n_rounds = 1024UL * 128 * threadpool->poll; |
3131 | |
|
3132 | 0 | for (uint64_t i=0; !ggml_graph_compute_thread_ready(state) && i < n_rounds; i++) { |
3133 | | // No new work. Keep polling. |
3134 | 0 | ggml_thread_cpu_relax(); |
3135 | 0 | } |
3136 | |
|
3137 | 0 | return state->pending; |
3138 | 0 | } |
3139 | | |
3140 | 0 | static inline bool ggml_graph_compute_check_for_work(struct ggml_compute_state * state) { |
3141 | 0 | struct ggml_threadpool * threadpool = state->threadpool; |
3142 | |
|
3143 | 0 | if (ggml_graph_compute_poll_for_work(state)) { |
3144 | 0 | ggml_graph_compute_thread_sync(state); |
3145 | 0 | return state->pending; |
3146 | 0 | } |
3147 | | |
3148 | 0 | ggml_mutex_lock_shared(&threadpool->mutex); |
3149 | 0 | while (!ggml_graph_compute_thread_ready(state)) { |
3150 | | // No new work. Wait for the signal. |
3151 | 0 | GGML_PRINT_DEBUG("thread #%d waiting for work (sleeping)\n", state->ith); |
3152 | 0 | ggml_cond_wait(&threadpool->cond, &threadpool->mutex); |
3153 | 0 | } |
3154 | 0 | ggml_mutex_unlock_shared(&threadpool->mutex); |
3155 | |
|
3156 | 0 | return state->pending; |
3157 | 0 | } |
3158 | | |
3159 | 0 | static thread_ret_t ggml_graph_compute_secondary_thread(void* data) { |
3160 | 0 | struct ggml_compute_state * state = (struct ggml_compute_state *) data; |
3161 | 0 | struct ggml_threadpool * threadpool = state->threadpool; |
3162 | |
|
3163 | 0 | ggml_thread_apply_priority(threadpool->prio); |
3164 | 0 | if (ggml_thread_cpumask_is_valid(state->cpumask)) { |
3165 | 0 | ggml_thread_apply_affinity(state->cpumask); |
3166 | 0 | } |
3167 | |
|
3168 | 0 | while (true) { |
3169 | | // Check if we need to sleep |
3170 | 0 | while (threadpool->pause) { |
3171 | 0 | GGML_PRINT_DEBUG("thread #%d inside pause loop\n", state->ith); |
3172 | 0 | ggml_mutex_lock_shared(&threadpool->mutex); |
3173 | 0 | if (threadpool->pause) { |
3174 | 0 | ggml_cond_wait(&threadpool->cond, &threadpool->mutex); |
3175 | 0 | } |
3176 | 0 | GGML_PRINT_DEBUG("thread #%d resuming after wait\n", state->ith); |
3177 | 0 | ggml_mutex_unlock_shared(&threadpool->mutex); |
3178 | 0 | } |
3179 | | |
3180 | | // This needs to be checked for after the cond_wait |
3181 | 0 | if (threadpool->stop) break; |
3182 | | |
3183 | | // Check if there is new work |
3184 | | // The main thread is the only one that can dispatch new work |
3185 | | |
3186 | 0 | ggml_graph_compute_check_for_work(state); |
3187 | 0 | if (state->pending) { |
3188 | 0 | state->pending = false; |
3189 | 0 | ggml_graph_compute_thread(state); |
3190 | 0 | } |
3191 | 0 | } |
3192 | |
|
3193 | 0 | return (thread_ret_t) 0; |
3194 | 0 | } |
3195 | | |
3196 | | // Start processing new graph |
3197 | | static void ggml_graph_compute_kickoff(struct ggml_threadpool * threadpool, int n_threads) |
3198 | 0 | { |
3199 | | // Always take the mutex here because the worker threads are doing hybrid poll/wait |
3200 | |
|
3201 | 0 | ggml_mutex_lock(&threadpool->mutex); |
3202 | | |
3203 | | // Update the number of active threads and the graph count |
3204 | 0 | int n_graph = atomic_load_explicit(&threadpool->n_graph, memory_order_relaxed) >> GGML_THREADPOOL_N_THREADS_BITS; |
3205 | 0 | n_graph = ((n_graph + 1) << GGML_THREADPOOL_N_THREADS_BITS) | (n_threads & GGML_THREADPOOL_N_THREADS_MASK); |
3206 | |
|
3207 | 0 | GGML_PRINT_DEBUG("compute-kickoff: n_threads %d n_graph %d\n", n_threads, n_graph); |
3208 | | |
3209 | | // Indicate the graph is ready to be processed |
3210 | | // We need the full seq-cst fence here because of the polling threads (used in thread_sync) |
3211 | 0 | atomic_store_explicit(&threadpool->n_graph, n_graph, memory_order_seq_cst); |
3212 | |
|
3213 | 0 | if (threadpool->pause) { |
3214 | | // Update main thread prio and affinity to match the threadpool settings |
3215 | 0 | ggml_thread_apply_priority(threadpool->prio); |
3216 | 0 | if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) { |
3217 | 0 | ggml_thread_apply_affinity(threadpool->workers[0].cpumask); |
3218 | 0 | } |
3219 | | |
3220 | | // resume does cond broadcast |
3221 | 0 | ggml_threadpool_resume_locked(threadpool); |
3222 | 0 | } else { |
3223 | 0 | ggml_cond_broadcast(&threadpool->cond); |
3224 | 0 | } |
3225 | |
|
3226 | 0 | ggml_mutex_unlock(&threadpool->mutex); |
3227 | 0 | } |
3228 | | |
3229 | | #endif // GGML_USE_OPENMP |
3230 | | |
3231 | | static struct ggml_threadpool * ggml_threadpool_new_impl( |
3232 | | struct ggml_threadpool_params * tpp, |
3233 | | struct ggml_cgraph * cgraph, |
3234 | 0 | struct ggml_cplan * cplan) { |
3235 | |
|
3236 | 0 | struct ggml_threadpool * threadpool = |
3237 | 0 | ggml_aligned_malloc(sizeof(struct ggml_threadpool)); |
3238 | 0 | { |
3239 | 0 | threadpool->cgraph = cgraph; |
3240 | 0 | threadpool->cplan = cplan; |
3241 | 0 | threadpool->n_graph = 0; |
3242 | 0 | threadpool->n_barrier = 0; |
3243 | 0 | threadpool->n_barrier_passed = 0; |
3244 | 0 | threadpool->current_chunk = 0; |
3245 | 0 | threadpool->stop = false; |
3246 | 0 | threadpool->pause = tpp->paused; |
3247 | 0 | threadpool->abort = -1; |
3248 | 0 | threadpool->workers = NULL; |
3249 | 0 | threadpool->n_threads = tpp->n_threads; |
3250 | 0 | threadpool->poll = tpp->poll; |
3251 | 0 | threadpool->prio = tpp->prio; |
3252 | 0 | threadpool->ec = GGML_STATUS_SUCCESS; |
3253 | 0 | } |
3254 | | |
3255 | | // Allocate and init workers state |
3256 | 0 | const size_t workers_size = sizeof(struct ggml_compute_state) * tpp->n_threads; |
3257 | 0 | struct ggml_compute_state * workers = ggml_aligned_malloc(workers_size); |
3258 | |
|
3259 | 0 | memset(workers, 0, workers_size); |
3260 | 0 | for (int j = 0; j < tpp->n_threads; j++) { |
3261 | 0 | workers[j].threadpool = threadpool; |
3262 | 0 | workers[j].ith = j; |
3263 | 0 | } |
3264 | |
|
3265 | 0 | threadpool->workers = workers; |
3266 | |
|
3267 | | #ifdef GGML_USE_OPENMP |
3268 | | int32_t cpumask_iter = 0; |
3269 | | |
3270 | | // Compute CPU masks for each thread |
3271 | | for (int j = 0; j < tpp->n_threads; j++) { |
3272 | | ggml_thread_cpumask_next(tpp->cpumask, workers[j].cpumask, tpp->strict_cpu, &cpumask_iter); |
3273 | | } |
3274 | | #else // GGML_USE_OPENMP |
3275 | 0 | ggml_mutex_init(&threadpool->mutex); |
3276 | 0 | ggml_cond_init(&threadpool->cond); |
3277 | | |
3278 | | // Spin the threads for all workers, and update CPU placements. |
3279 | | // Place the main thread last (towards the higher numbered CPU cores). |
3280 | |
|
3281 | 0 | int32_t cpumask_iter = 0; |
3282 | |
|
3283 | 0 | for (int j = 1; j < tpp->n_threads; j++) { |
3284 | 0 | ggml_thread_cpumask_next(tpp->cpumask, workers[j].cpumask, tpp->strict_cpu, &cpumask_iter); |
3285 | |
|
3286 | 0 | int32_t rc = ggml_thread_create(&workers[j].thrd, NULL, ggml_graph_compute_secondary_thread, &workers[j]); |
3287 | 0 | GGML_ASSERT(rc == 0); |
3288 | 0 | } |
3289 | |
|
3290 | 0 | ggml_thread_cpumask_next(tpp->cpumask, workers[0].cpumask, tpp->strict_cpu, &cpumask_iter); |
3291 | |
|
3292 | 0 | if (!threadpool->pause) { |
3293 | | // Update main thread prio and affinity at the start, otherwise we'll do it in resume |
3294 | 0 | ggml_thread_apply_priority(threadpool->prio); |
3295 | 0 | if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) { |
3296 | 0 | ggml_thread_apply_affinity(threadpool->workers[0].cpumask); |
3297 | 0 | } |
3298 | 0 | } |
3299 | 0 | #endif // GGML_USE_OPENMP |
3300 | |
|
3301 | 0 | return threadpool; |
3302 | 0 | } |
3303 | | |
3304 | 0 | struct ggml_threadpool * ggml_threadpool_new(struct ggml_threadpool_params * tpp) { |
3305 | 0 | return ggml_threadpool_new_impl(tpp, NULL, NULL); |
3306 | 0 | } |
3307 | | |
3308 | 0 | enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan) { |
3309 | 0 | ggml_cpu_init(); |
3310 | |
|
3311 | 0 | GGML_ASSERT(cplan); |
3312 | 0 | GGML_ASSERT(cplan->n_threads > 0); |
3313 | 0 | GGML_ASSERT(cplan->work_size == 0 || cplan->work_data != NULL); |
3314 | |
|
3315 | 0 | int n_threads = cplan->n_threads; |
3316 | 0 | struct ggml_threadpool * threadpool = cplan->threadpool; |
3317 | |
|
3318 | 0 | bool disposable_threadpool = false; |
3319 | |
|
3320 | 0 | if (threadpool == NULL) { |
3321 | | //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads); |
3322 | 0 | disposable_threadpool = true; |
3323 | |
|
3324 | 0 | struct ggml_threadpool_params ttp = ggml_threadpool_params_default(n_threads); |
3325 | 0 | threadpool = ggml_threadpool_new_impl(&ttp, cgraph, cplan); |
3326 | 0 | } else { |
3327 | | // Reset some of the parameters that need resetting |
3328 | | // No worker threads should be accessing the parameters below at this stage |
3329 | 0 | threadpool->cgraph = cgraph; |
3330 | 0 | threadpool->cplan = cplan; |
3331 | 0 | threadpool->current_chunk = 0; |
3332 | 0 | threadpool->abort = -1; |
3333 | 0 | threadpool->ec = GGML_STATUS_SUCCESS; |
3334 | 0 | } |
3335 | |
|
3336 | | #ifdef GGML_USE_OPENMP |
3337 | | if (n_threads > 1) { |
3338 | | #pragma omp parallel num_threads(n_threads) |
3339 | | { |
3340 | | #pragma omp single |
3341 | | { |
3342 | | // update the number of threads from the actual number of threads that we got from OpenMP |
3343 | | n_threads = omp_get_num_threads(); |
3344 | | atomic_store_explicit(&threadpool->n_graph, n_threads, memory_order_relaxed); |
3345 | | } |
3346 | | |
3347 | | // Apply thread CPU mask and priority |
3348 | | int ith = omp_get_thread_num(); |
3349 | | |
3350 | | ggml_thread_apply_priority(threadpool->prio); |
3351 | | if (ggml_thread_cpumask_is_valid(threadpool->workers[ith].cpumask)) { |
3352 | | ggml_thread_apply_affinity(threadpool->workers[ith].cpumask); |
3353 | | } |
3354 | | ggml_graph_compute_thread(&threadpool->workers[ith]); |
3355 | | } |
3356 | | } else { |
3357 | | atomic_store_explicit(&threadpool->n_graph, 1, memory_order_relaxed); |
3358 | | ggml_graph_compute_thread(&threadpool->workers[0]); |
3359 | | } |
3360 | | #else |
3361 | 0 | if (n_threads > threadpool->n_threads) { |
3362 | 0 | GGML_LOG_WARN("cplan requested more threads (%d) than available (%d)\n", n_threads, threadpool->n_threads); |
3363 | 0 | n_threads = threadpool->n_threads; |
3364 | 0 | } |
3365 | | |
3366 | | // Kick all threads to start the new graph |
3367 | 0 | ggml_graph_compute_kickoff(threadpool, n_threads); |
3368 | | |
3369 | | // This is a work thread too |
3370 | 0 | ggml_graph_compute_thread(&threadpool->workers[0]); |
3371 | 0 | #endif |
3372 | | |
3373 | | // don't leave affinity set on the main thread |
3374 | 0 | clear_numa_thread_affinity(); |
3375 | |
|
3376 | 0 | enum ggml_status ret = threadpool->ec; |
3377 | |
|
3378 | 0 | if (disposable_threadpool) { |
3379 | 0 | ggml_threadpool_free(threadpool); |
3380 | 0 | } |
3381 | |
|
3382 | 0 | return ret; |
3383 | 0 | } |
3384 | | |
3385 | 0 | enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads) { |
3386 | 0 | struct ggml_cplan cplan = ggml_graph_plan(cgraph, n_threads, NULL); |
3387 | |
|
3388 | 0 | cplan.work_data = (uint8_t *)ggml_new_buffer(ctx, cplan.work_size); |
3389 | |
|
3390 | 0 | return ggml_graph_compute(cgraph, &cplan); |
3391 | 0 | } |
3392 | | |
3393 | 0 | void ggml_cpu_fp32_to_fp32(const float * x, float * y, int64_t n) { |
3394 | 0 | memcpy(y, x, n * sizeof(float)); |
3395 | 0 | } |
3396 | | |
3397 | 0 | void ggml_cpu_fp32_to_fp16(const float * x, ggml_fp16_t * y, int64_t n) { |
3398 | 0 | int64_t i = 0; |
3399 | 0 | #if defined(__F16C__) |
3400 | | #if defined(__AVX512F__) |
3401 | | for (; i + 15 < n; i += 16) { |
3402 | | __m512 x_vec = _mm512_loadu_ps(x + i); |
3403 | | __m256i y_vec = _mm512_cvtps_ph(x_vec, _MM_FROUND_TO_NEAREST_INT); |
3404 | | _mm256_storeu_si256((__m256i *)(y + i), y_vec); |
3405 | | } |
3406 | | #endif |
3407 | 0 | for (; i + 7 < n; i += 8) { |
3408 | 0 | __m256 x_vec = _mm256_loadu_ps(x + i); |
3409 | 0 | __m128i y_vec = _mm256_cvtps_ph(x_vec, _MM_FROUND_TO_NEAREST_INT); |
3410 | 0 | _mm_storeu_si128((__m128i *)(y + i), y_vec); |
3411 | 0 | } |
3412 | 0 | for (; i + 3 < n; i += 4) { |
3413 | 0 | __m128 x_vec = _mm_loadu_ps(x + i); |
3414 | 0 | __m128i y_vec = _mm_cvtps_ph(x_vec, _MM_FROUND_TO_NEAREST_INT); |
3415 | 0 | _mm_storel_epi64((__m128i *)(y + i), y_vec); |
3416 | 0 | } |
3417 | | #elif defined(__riscv_zvfh) |
3418 | | for (int vl; i < n; i += vl) { |
3419 | | vl = __riscv_vsetvl_e32m2(n - i); |
3420 | | vfloat32m2_t vx = __riscv_vle32_v_f32m2(&x[i], vl); |
3421 | | vfloat16m1_t vy = __riscv_vfncvt_f_f_w_f16m1(vx, vl); |
3422 | | __riscv_vse16_v_f16m1((_Float16 *)&y[i], vy, vl); |
3423 | | } |
3424 | | #endif |
3425 | 0 | for (; i < n; ++i) { |
3426 | 0 | y[i] = GGML_CPU_FP32_TO_FP16(x[i]); |
3427 | 0 | } |
3428 | 0 | } |
3429 | | |
3430 | 0 | void ggml_cpu_fp16_to_fp32(const ggml_fp16_t * x, float * y, int64_t n) { |
3431 | 0 | int64_t i = 0; |
3432 | 0 | #if defined(__F16C__) |
3433 | | #if defined(__AVX512F__) |
3434 | | for (; i + 15 < n; i += 16) { |
3435 | | __m256i x_vec = _mm256_loadu_si256((const __m256i *)(x + i)); |
3436 | | __m512 y_vec = _mm512_cvtph_ps(x_vec); |
3437 | | _mm512_storeu_ps(y + i, y_vec); |
3438 | | } |
3439 | | #endif |
3440 | 0 | for (; i + 7 < n; i += 8) { |
3441 | 0 | __m128i x_vec = _mm_loadu_si128((const __m128i *)(x + i)); |
3442 | 0 | __m256 y_vec = _mm256_cvtph_ps(x_vec); |
3443 | 0 | _mm256_storeu_ps(y + i, y_vec); |
3444 | 0 | } |
3445 | 0 | for (; i + 3 < n; i += 4) { |
3446 | 0 | __m128i x_vec = _mm_loadl_epi64((const __m128i *)(x + i)); |
3447 | 0 | __m128 y_vec = _mm_cvtph_ps(x_vec); |
3448 | 0 | _mm_storeu_ps(y + i, y_vec); |
3449 | 0 | } |
3450 | |
|
3451 | | #elif defined(__riscv_v_intrinsic) && defined(__riscv_zvfhmin) |
3452 | | // calculate step size |
3453 | | const int epr = __riscv_vsetvlmax_e16m2(); |
3454 | | const int step = epr * 2; |
3455 | | const int np = (n & ~(step - 1)); |
3456 | | |
3457 | | // unroll by 2 |
3458 | | for (; i < np; i += step) { |
3459 | | vfloat16m2_t ax0 = __riscv_vle16_v_f16m2((const _Float16*)x + i, epr); |
3460 | | vfloat32m4_t ay0 = __riscv_vfwcvt_f_f_v_f32m4(ax0, epr); |
3461 | | __riscv_vse32_v_f32m4(y + i, ay0, epr); |
3462 | | |
3463 | | vfloat16m2_t ax1 = __riscv_vle16_v_f16m2((const _Float16*)x + i + epr, epr); |
3464 | | vfloat32m4_t ay1 = __riscv_vfwcvt_f_f_v_f32m4(ax1, epr); |
3465 | | __riscv_vse32_v_f32m4(y + i + epr, ay1, epr); |
3466 | | } |
3467 | | |
3468 | | // leftovers |
3469 | | int vl; |
3470 | | for (i = np; i < n; i += vl) { |
3471 | | vl = __riscv_vsetvl_e16m2(n - i); |
3472 | | vfloat16m2_t ax0 = __riscv_vle16_v_f16m2((const _Float16*)x + i, vl); |
3473 | | vfloat32m4_t ay0 = __riscv_vfwcvt_f_f_v_f32m4(ax0, vl); |
3474 | | __riscv_vse32_v_f32m4(y + i, ay0, vl); |
3475 | | } |
3476 | | |
3477 | | #endif |
3478 | |
|
3479 | 0 | for (; i < n; ++i) { |
3480 | 0 | y[i] = GGML_CPU_FP16_TO_FP32(x[i]); |
3481 | 0 | } |
3482 | 0 | } |
3483 | | |
3484 | 0 | void ggml_cpu_fp32_to_bf16(const float * x, ggml_bf16_t * y, int64_t n) { |
3485 | 0 | int64_t i = 0; |
3486 | 0 | for (; i < n; ++i) { |
3487 | 0 | y[i] = GGML_FP32_TO_BF16(x[i]); |
3488 | 0 | } |
3489 | 0 | } |
3490 | | |
3491 | 0 | void ggml_cpu_fp32_to_i32(const float * x, int32_t * y, int64_t n) { |
3492 | 0 | int64_t i = 0; |
3493 | 0 | for (; i < n; ++i) { |
3494 | 0 | y[i] = x[i]; |
3495 | 0 | } |
3496 | 0 | } |
3497 | | |
3498 | 0 | void ggml_cpu_bf16_to_fp32(const ggml_bf16_t * x, float * y, int64_t n) { |
3499 | 0 | int64_t i = 0; |
3500 | 0 | #if defined(__AVX2__) |
3501 | | #if defined(__AVX512F__) |
3502 | | for (; i + 15 < n; i += 16) { |
3503 | | _mm512_storeu_ps(y + i, |
3504 | | _mm512_castsi512_ps( |
3505 | | _mm512_slli_epi32( |
3506 | | _mm512_cvtepu16_epi32( |
3507 | | _mm256_loadu_si256( |
3508 | | (const __m256i *)(x + i))), |
3509 | | 16))); |
3510 | | } |
3511 | | #endif |
3512 | 0 | for (; i + 7 < n; i += 8) { |
3513 | 0 | _mm256_storeu_ps(y + i, |
3514 | 0 | _mm256_castsi256_ps( |
3515 | 0 | _mm256_slli_epi32( |
3516 | 0 | _mm256_cvtepu16_epi32( |
3517 | 0 | _mm_loadu_si128( |
3518 | 0 | (const __m128i *)(x + i))), |
3519 | 0 | 16))); |
3520 | 0 | } |
3521 | | #elif defined(__riscv_v_intrinsic) && defined(__riscv_zvfbfmin) |
3522 | | // calculate step size |
3523 | | const int epr = __riscv_vsetvlmax_e16m2(); |
3524 | | const int step = epr * 2; |
3525 | | const int np = (n & ~(step - 1)); |
3526 | | |
3527 | | // unroll by 2 |
3528 | | for (; i < np; i += step) { |
3529 | | vbfloat16m2_t ax0 = __riscv_vle16_v_bf16m2((const __bf16*)x + i, epr); |
3530 | | vfloat32m4_t ay0 = __riscv_vfwcvtbf16_f_f_v_f32m4(ax0, epr); |
3531 | | __riscv_vse32_v_f32m4(y + i, ay0, epr); |
3532 | | |
3533 | | vbfloat16m2_t ax1 = __riscv_vle16_v_bf16m2((const __bf16*)x + i + epr, epr); |
3534 | | vfloat32m4_t ay1 = __riscv_vfwcvtbf16_f_f_v_f32m4(ax1, epr); |
3535 | | __riscv_vse32_v_f32m4(y + i + epr, ay1, epr); |
3536 | | } |
3537 | | |
3538 | | // leftovers |
3539 | | int vl; |
3540 | | for (i = np; i < n; i += vl) { |
3541 | | vl = __riscv_vsetvl_e16m2(n - i); |
3542 | | vbfloat16m2_t ax0 = __riscv_vle16_v_bf16m2((const __bf16*)x + i, vl); |
3543 | | vfloat32m4_t ay0 = __riscv_vfwcvtbf16_f_f_v_f32m4(ax0, vl); |
3544 | | __riscv_vse32_v_f32m4(y + i, ay0, vl); |
3545 | | } |
3546 | | #endif |
3547 | 0 | for (; i < n; i++) { |
3548 | 0 | y[i] = GGML_BF16_TO_FP32(x[i]); |
3549 | 0 | } |
3550 | 0 | } |
3551 | | |
3552 | 0 | int ggml_cpu_has_avx(void) { |
3553 | 0 | #if defined(__AVX__) |
3554 | 0 | return 1; |
3555 | | #else |
3556 | | return 0; |
3557 | | #endif |
3558 | 0 | } |
3559 | | |
3560 | 0 | int ggml_cpu_has_avx_vnni(void) { |
3561 | | #if defined(__AVXVNNI__) |
3562 | | return 1; |
3563 | | #else |
3564 | 0 | return 0; |
3565 | 0 | #endif |
3566 | 0 | } |
3567 | | |
3568 | 0 | int ggml_cpu_has_avx2(void) { |
3569 | 0 | #if defined(__AVX2__) |
3570 | 0 | return 1; |
3571 | | #else |
3572 | | return 0; |
3573 | | #endif |
3574 | 0 | } |
3575 | | |
3576 | 0 | int ggml_cpu_has_avx512(void) { |
3577 | | #if defined(__AVX512F__) |
3578 | | return 1; |
3579 | | #else |
3580 | 0 | return 0; |
3581 | 0 | #endif |
3582 | 0 | } |
3583 | | |
3584 | 0 | int ggml_cpu_has_avx512_vbmi(void) { |
3585 | | #if defined(__AVX512VBMI__) |
3586 | | return 1; |
3587 | | #else |
3588 | 0 | return 0; |
3589 | 0 | #endif |
3590 | 0 | } |
3591 | | |
3592 | 0 | int ggml_cpu_has_avx512_vnni(void) { |
3593 | | #if defined(__AVX512VNNI__) |
3594 | | return 1; |
3595 | | #else |
3596 | 0 | return 0; |
3597 | 0 | #endif |
3598 | 0 | } |
3599 | | |
3600 | 0 | int ggml_cpu_has_avx512_bf16(void) { |
3601 | | #if defined(__AVX512BF16__) |
3602 | | return 1; |
3603 | | #else |
3604 | 0 | return 0; |
3605 | 0 | #endif |
3606 | 0 | } |
3607 | | |
3608 | 0 | int ggml_cpu_has_amx_int8(void) { |
3609 | | #if defined(__AMX_INT8__) |
3610 | | return 1; |
3611 | | #else |
3612 | 0 | return 0; |
3613 | 0 | #endif |
3614 | 0 | } |
3615 | | |
3616 | 0 | int ggml_cpu_has_bmi2(void) { |
3617 | 0 | #if defined(__BMI2__) |
3618 | 0 | return 1; |
3619 | | #else |
3620 | | return 0; |
3621 | | #endif |
3622 | 0 | } |
3623 | | |
3624 | 0 | int ggml_cpu_has_fma(void) { |
3625 | 0 | #if defined(__FMA__) |
3626 | 0 | return 1; |
3627 | | #else |
3628 | | return 0; |
3629 | | #endif |
3630 | 0 | } |
3631 | | |
3632 | 0 | int ggml_cpu_has_arm_fma(void) { |
3633 | | #if defined(__ARM_FEATURE_FMA) |
3634 | | return 1; |
3635 | | #else |
3636 | 0 | return 0; |
3637 | 0 | #endif |
3638 | 0 | } |
3639 | | |
3640 | 0 | int ggml_cpu_has_riscv_v(void) { |
3641 | | #if defined(__riscv_v_intrinsic) |
3642 | | return 1; |
3643 | | #else |
3644 | 0 | return 0; |
3645 | 0 | #endif |
3646 | 0 | } |
3647 | | |
3648 | 0 | int ggml_cpu_get_rvv_vlen(void) { |
3649 | | #if defined(__riscv) && defined(__riscv_v_intrinsic) |
3650 | | return ggml_riscv_arch_features.rvv_vlen; |
3651 | | #else |
3652 | 0 | return 0; |
3653 | 0 | #endif |
3654 | 0 | } |
3655 | | |
3656 | 0 | int ggml_cpu_has_f16c(void) { |
3657 | 0 | #if defined(__F16C__) |
3658 | 0 | return 1; |
3659 | | #else |
3660 | | return 0; |
3661 | | #endif |
3662 | 0 | } |
3663 | | |
3664 | 0 | int ggml_cpu_has_fp16_va(void) { |
3665 | | #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
3666 | | return 1; |
3667 | | #else |
3668 | 0 | return 0; |
3669 | 0 | #endif |
3670 | 0 | } |
3671 | | |
3672 | 0 | int ggml_cpu_has_wasm_simd(void) { |
3673 | | #if defined(__wasm_simd128__) |
3674 | | return 1; |
3675 | | #else |
3676 | 0 | return 0; |
3677 | 0 | #endif |
3678 | 0 | } |
3679 | | |
3680 | 0 | int ggml_cpu_has_llamafile(void) { |
3681 | 0 | #if defined(GGML_USE_LLAMAFILE) |
3682 | 0 | return 1; |
3683 | | #else |
3684 | | return 0; |
3685 | | #endif |
3686 | 0 | } |
3687 | | |
3688 | 0 | int ggml_cpu_has_sse3(void) { |
3689 | 0 | #if defined(__SSE3__) |
3690 | 0 | return 1; |
3691 | | #else |
3692 | | return 0; |
3693 | | #endif |
3694 | 0 | } |
3695 | | |
3696 | 0 | int ggml_cpu_has_ssse3(void) { |
3697 | 0 | #if defined(__SSSE3__) |
3698 | 0 | return 1; |
3699 | | #else |
3700 | | return 0; |
3701 | | #endif |
3702 | 0 | } |
3703 | | |
3704 | 0 | int ggml_cpu_has_vsx(void) { |
3705 | | #if defined(__POWER9_VECTOR__) |
3706 | | return 1; |
3707 | | #else |
3708 | 0 | return 0; |
3709 | 0 | #endif |
3710 | 0 | } |
3711 | | |
3712 | 0 | int ggml_cpu_has_vxe(void) { |
3713 | | #if defined(__VXE__) || defined(__VXE2__) |
3714 | | return 1; |
3715 | | #else |
3716 | 0 | return 0; |
3717 | 0 | #endif |
3718 | 0 | } |
3719 | | |
3720 | 0 | int ggml_cpu_has_neon(void) { |
3721 | | #if defined(__ARM_ARCH) && defined(__ARM_NEON) |
3722 | | return 1; |
3723 | | #else |
3724 | 0 | return 0; |
3725 | 0 | #endif |
3726 | 0 | } |
3727 | | |
3728 | 0 | int ggml_cpu_has_dotprod(void) { |
3729 | | #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_DOTPROD) |
3730 | | return 1; |
3731 | | #else |
3732 | 0 | return 0; |
3733 | 0 | #endif |
3734 | 0 | } |
3735 | | |
3736 | 0 | int ggml_cpu_has_sve(void) { |
3737 | | #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE) |
3738 | | return 1; |
3739 | | #else |
3740 | 0 | return 0; |
3741 | 0 | #endif |
3742 | 0 | } |
3743 | | |
3744 | 0 | int ggml_cpu_has_matmul_int8(void) { |
3745 | | #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_MATMUL_INT8) |
3746 | | return 1; |
3747 | | #else |
3748 | 0 | return 0; |
3749 | 0 | #endif |
3750 | 0 | } |
3751 | | |
3752 | 0 | int ggml_cpu_get_sve_cnt(void) { |
3753 | | #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE) |
3754 | | return ggml_arm_arch_features.sve_cnt; |
3755 | | #else |
3756 | 0 | return 0; |
3757 | 0 | #endif |
3758 | 0 | } |
3759 | | |
3760 | 0 | int ggml_cpu_has_sme(void) { |
3761 | | #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SME) |
3762 | | return 1; |
3763 | | #else |
3764 | 0 | return 0; |
3765 | 0 | #endif |
3766 | 0 | } |
3767 | | |
3768 | 3 | void ggml_cpu_init(void) { |
3769 | | // needed to initialize ggml_time |
3770 | 3 | { |
3771 | 3 | struct ggml_init_params params = { 0, NULL, false }; |
3772 | 3 | struct ggml_context * ctx = ggml_init(params); |
3773 | 3 | ggml_free(ctx); |
3774 | 3 | } |
3775 | | |
3776 | 3 | ggml_critical_section_start(); |
3777 | | |
3778 | 3 | static bool is_first_call = true; |
3779 | | |
3780 | 3 | if (is_first_call) { |
3781 | | // initialize GELU, Quick GELU, SILU and EXP F32 tables |
3782 | 3 | { |
3783 | 3 | const uint64_t t_start = ggml_time_us(); UNUSED(t_start); |
3784 | | |
3785 | 196k | for (int i = 0; i < (1 << 16); ++i) { |
3786 | 196k | union { |
3787 | 196k | uint16_t u16; |
3788 | 196k | ggml_fp16_t fp16; |
3789 | 196k | } u = {i}; |
3790 | 196k | float f = GGML_COMPUTE_FP16_TO_FP32(u.fp16); |
3791 | 196k | ggml_table_f32_f16[i] = f; |
3792 | 196k | ggml_table_gelu_f16[i] = GGML_CPU_FP32_TO_FP16(ggml_gelu_f32(f)); |
3793 | 196k | ggml_table_gelu_quick_f16[i] = GGML_CPU_FP32_TO_FP16(ggml_gelu_quick_f32(f)); |
3794 | 196k | } |
3795 | | |
3796 | | // initialize E8M0 half table (256 entries) |
3797 | 771 | for (int i = 0; i < (1 << 8); ++i) { |
3798 | 768 | ggml_table_f32_e8m0_half[i] = GGML_E8M0_TO_FP32_HALF(i); |
3799 | 768 | } |
3800 | | |
3801 | 3 | const uint64_t t_end = ggml_time_us(); UNUSED(t_end); |
3802 | | |
3803 | 3 | GGML_PRINT_DEBUG("%s: GELU, Quick GELU, SILU and EXP tables initialized in %f ms\n", __func__, (t_end - t_start)/1000.0); |
3804 | | |
3805 | | #ifdef GGML_USE_OPENMP |
3806 | | //if (!getenv("OMP_WAIT_POLICY")) { |
3807 | | // // set the wait policy to active, so that OpenMP threads don't sleep |
3808 | | // setenv("OMP_WAIT_POLICY", "active", 0) |
3809 | | //} |
3810 | | |
3811 | | if (!getenv("KMP_BLOCKTIME")) { |
3812 | | // set the time to wait before sleeping a thread |
3813 | | // this is less aggressive than setting the wait policy to active, but should achieve similar results in most cases |
3814 | | #ifdef _WIN32 |
3815 | | _putenv_s("KMP_BLOCKTIME", "200"); // 200ms |
3816 | | #else |
3817 | | setenv("KMP_BLOCKTIME", "200", 0); // 200ms |
3818 | | #endif |
3819 | | } |
3820 | | #endif |
3821 | 3 | } |
3822 | | |
3823 | | #if defined(__ARM_ARCH) |
3824 | | ggml_init_arm_arch_features(); |
3825 | | #endif |
3826 | | |
3827 | | #if defined(__riscv) |
3828 | | ggml_init_riscv_arch_features(); |
3829 | | #endif |
3830 | | |
3831 | 3 | { |
3832 | 3 | const char * env = getenv("GGML_CPU_DISABLE_FUSION"); |
3833 | 3 | ggml_cpu_disable_fusion = (env != NULL && atoi(env) == 1); |
3834 | 3 | } |
3835 | | |
3836 | 3 | is_first_call = false; |
3837 | 3 | } |
3838 | | |
3839 | 3 | ggml_critical_section_end(); |
3840 | 3 | } |