Debian Codegraph @

external/whispercpp/ggml.h

raw
1 #pragma once
2
3 /*
4 Added by Paul Boersma on behalf of inclusion into Praat
5 */
6 #define WHISPER_VERSION "1.8.3"
7 #define GGML_VERSION "0.9.7"
8 #define GGML_COMMIT "unknown"
9 #define GGML_USE_CPU
10 #define GGML_CPU_GENERIC
11 #if ! defined (_GNU_SOURCE)
12 #define _GNU_SOURCE
13 #endif
14
15 //
16 // GGML Tensor Library
17 //
18 // This documentation is still a work in progress.
19 // If you wish some specific topics to be covered, feel free to drop a comment:
20 //
21 // https://github.com/ggml-org/whisper.cpp/issues/40
22 //
23 // ## Overview
24 //
25 // This library implements:
26 //
27 // - a set of tensor operations
28 // - automatic differentiation
29 // - basic optimization algorithms
30 //
31 // The aim of this library is to provide a minimalistic approach for various machine learning tasks. This includes,
32 // but is not limited to, the following:
33 //
34 // - linear regression
35 // - support vector machines
36 // - neural networks
37 //
38 // The library allows the user to define a certain function using the available tensor operations. This function
39 // definition is represented internally via a computation graph. Each tensor operation in the function definition
40 // corresponds to a node in the graph. Having the computation graph defined, the user can choose to compute the
41 // function's value and/or its gradient with respect to the input variables. Optionally, the function can be optimized
42 // using one of the available optimization algorithms.
43 //
44 // For example, here we define the function: f(x) = a*x^2 + b
45 //
46 // {
47 // struct ggml_init_params params = {
48 // .mem_size = 16*1024*1024,
49 // .mem_buffer = NULL,
50 // };
51 //
52 // // memory allocation happens here
53 // struct ggml_context * ctx = ggml_init(params);
54 //
55 // struct ggml_tensor * x = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
56 //
57 // ggml_set_param(ctx, x); // x is an input variable
58 //
59 // struct ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
60 // struct ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
61 // struct ggml_tensor * x2 = ggml_mul(ctx, x, x);
62 // struct ggml_tensor * f = ggml_add(ctx, ggml_mul(ctx, a, x2), b);
63 //
64 // ...
65 // }
66 //
67 // Notice that the function definition above does not involve any actual computation. The computation is performed only
68 // when the user explicitly requests it. For example, to compute the function's value at x = 2.0:
69 //
70 // {
71 // ...
72 //
73 // struct ggml_cgraph * gf = ggml_new_graph(ctx);
74 // ggml_build_forward_expand(gf, f);
75 //
76 // // set the input variable and parameter values
77 // ggml_set_f32(x, 2.0f);
78 // ggml_set_f32(a, 3.0f);
79 // ggml_set_f32(b, 4.0f);
80 //
81 // ggml_graph_compute_with_ctx(ctx, &gf, n_threads);
82 //
83 // printf("f = %f\n", ggml_get_f32_1d(f, 0));
84 //
85 // ...
86 // }
87 //
88 // The actual computation is performed in the ggml_graph_compute() function.
89 //
90 // The ggml_new_tensor_...() functions create new tensors. They are allocated in the memory buffer provided to the
91 // ggml_init() function. You have to be careful not to exceed the memory buffer size. Therefore, you have to know
92 // in advance how much memory you need for your computation. Alternatively, you can allocate a large enough memory
93 // and after defining the computation graph, call the ggml_used_mem() function to find out how much memory was
94 // actually needed.
95 //
96 // The ggml_set_param() function marks a tensor as an input variable. This is used by the automatic
97 // differentiation and optimization algorithms.
98 //
99 // The described approach allows to define the function graph once and then compute its forward or backward graphs
100 // multiple times. All computations will use the same memory buffer allocated in the ggml_init() function. This way
101 // the user can avoid the memory allocation overhead at runtime.
102 //
103 // The library supports multi-dimensional tensors - up to 4 dimensions. The FP16 and FP32 data types are first class
104 // citizens, but in theory the library can be extended to support FP8 and integer data types.
105 //
106 // Each tensor operation produces a new tensor. Initially the library was envisioned to support only the use of unary
107 // and binary operations. Most of the available operations fall into one of these two categories. With time, it became
108 // clear that the library needs to support more complex operations. The way to support these operations is not clear
109 // yet, but a few examples are demonstrated in the following operations:
110 //
111 // - ggml_permute()
112 // - ggml_conv_1d_1s()
113 // - ggml_conv_1d_2s()
114 //
115 // For each tensor operator, the library implements a forward and backward computation function. The forward function
116 // computes the output tensor value given the input tensor values. The backward function computes the adjoint of the
117 // input tensors given the adjoint of the output tensor. For a detailed explanation of what this means, take a
118 // calculus class, or watch the following video:
119 //
120 // What is Automatic Differentiation?
121 // https://www.youtube.com/watch?v=wG_nF1awSSY
122 //
123 //
124 // ## Tensor data (struct ggml_tensor)
125 //
126 // The tensors are stored in memory via the ggml_tensor struct. The structure provides information about the size of
127 // the tensor, the data type, and the memory buffer where the tensor data is stored. Additionally, it contains
128 // pointers to the "source" tensors - i.e. the tensors that were used to compute the current tensor. For example:
129 //
130 // {
131 // struct ggml_tensor * c = ggml_add(ctx, a, b);
132 //
133 // assert(c->src[0] == a);
134 // assert(c->src[1] == b);
135 // }
136 //
137 // The multi-dimensional tensors are stored in row-major order. The ggml_tensor struct contains fields for the
138 // number of elements in each dimension ("ne") as well as the number of bytes ("nb", a.k.a. stride). This allows
139 // to store tensors that are not contiguous in memory, which is useful for operations such as transposition and
140 // permutation. All tensor operations have to take the stride into account and not assume that the tensor is
141 // contiguous in memory.
142 //
143 // The data of the tensor is accessed via the "data" pointer. For example:
144 //
145 // {
146 // const int nx = 2;
147 // const int ny = 3;
148 //
149 // struct ggml_tensor * a = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, nx, ny);
150 //
151 // for (int y = 0; y < ny; y++) {
152 // for (int x = 0; x < nx; x++) {
153 // *(float *) ((char *) a->data + y*a->nb[1] + x*a->nb[0]) = x + y;
154 // }
155 // }
156 //
157 // ...
158 // }
159 //
160 // Alternatively, there are helper functions, such as ggml_get_f32_1d() and ggml_set_f32_1d() that can be used.
161 //
162 // ## The matrix multiplication operator (ggml_mul_mat)
163 //
164 // TODO
165 //
166 //
167 // ## Multi-threading
168 //
169 // TODO
170 //
171 //
172 // ## Overview of ggml.c
173 //
174 // TODO
175 //
176 //
177 // ## SIMD optimizations
178 //
179 // TODO
180 //
181 //
182 // ## Debugging ggml
183 //
184 // TODO
185 //
186 //
187
188 #ifdef GGML_SHARED
189 # if defined(_WIN32) && !defined(__MINGW32__)
190 # ifdef GGML_BUILD
191 # define GGML_API __declspec(dllexport) extern
192 # else
193 # define GGML_API __declspec(dllimport) extern
194 # endif
195 # else
196 # define GGML_API __attribute__ ((visibility ("default"))) extern
197 # endif
198 #else
199 # define GGML_API extern
200 #endif
201
202 // TODO: support for clang
203 #ifdef __GNUC__
204 # define GGML_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
205 #elif defined(_MSC_VER)
206 # define GGML_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
207 #else
208 # define GGML_DEPRECATED(func, hint) func
209 #endif
210
211 #ifndef __GNUC__
212 # define GGML_ATTRIBUTE_FORMAT(...)
213 #elif defined(__MINGW32__) && !defined(__clang__)
214 # define GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
215 #else
216 # define GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
217 #endif
218
219 #if defined(_WIN32) && !defined(_WIN32_WINNT)
220 # define _WIN32_WINNT 0x0A00
221 #endif
222
223 #include <stdbool.h>
224 #include <stddef.h>
225 #include <stdint.h>
226 #include <stdio.h>
227
228 #define GGML_FILE_MAGIC 0x67676d6c // "ggml"
229 #define GGML_FILE_VERSION 2
230
231 #define GGML_QNT_VERSION 2 // bump this on quantization format changes
232 #define GGML_QNT_VERSION_FACTOR 1000 // do not change this
233
234 #define GGML_MAX_DIMS 4
235 #define GGML_MAX_PARAMS 2048
236 #define GGML_MAX_SRC 10
237 #define GGML_MAX_N_THREADS 512
238 #define GGML_MAX_OP_PARAMS 64
239
240 #ifndef GGML_MAX_NAME
241 # define GGML_MAX_NAME 64
242 #endif
243
244 #define GGML_DEFAULT_N_THREADS 4
245 #define GGML_DEFAULT_GRAPH_SIZE 2048
246
247 #if UINTPTR_MAX == 0xFFFFFFFF
248 #define GGML_MEM_ALIGN 4
249 #elif defined(__EMSCRIPTEN__)
250 // emscripten uses max_align_t == 8, so we need GGML_MEM_ALIGN == 8 for 64-bit wasm.
251 // (for 32-bit wasm, the first conditional is true and GGML_MEM_ALIGN stays 4.)
252 // ref: https://github.com/ggml-org/llama.cpp/pull/18628
253 #define GGML_MEM_ALIGN 8
254 #else
255 #define GGML_MEM_ALIGN 16
256 #endif
257
258 #define GGML_EXIT_SUCCESS 0
259 #define GGML_EXIT_ABORTED 1
260
261 // TODO: convert to enum https://github.com/ggml-org/llama.cpp/pull/16187#discussion_r2388538726
262 #define GGML_ROPE_TYPE_NORMAL 0
263 #define GGML_ROPE_TYPE_NEOX 2
264 #define GGML_ROPE_TYPE_MROPE 8
265 #define GGML_ROPE_TYPE_VISION 24
266 #define GGML_ROPE_TYPE_IMROPE 40 // binary: 101000
267
268 #define GGML_MROPE_SECTIONS 4
269
270 #define GGML_UNUSED(x) (void)(x)
271 #ifdef __CUDACC__
272 template<typename... Args>
273 __host__ __device__ constexpr inline void ggml_unused_vars_impl(Args&&...) noexcept {}
274 #define GGML_UNUSED_VARS(...) ggml_unused_vars_impl(__VA_ARGS__)
275 #else
276 #define GGML_UNUSED_VARS(...) do { (void)sizeof((__VA_ARGS__, 0)); } while(0)
277 #endif // __CUDACC__
278
279 #define GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
280
281 #ifndef NDEBUG
282 # define GGML_UNREACHABLE() do { fprintf(stderr, "statement should be unreachable\n"); abort(); } while(0)
283 #elif defined(__GNUC__)
284 # define GGML_UNREACHABLE() __builtin_unreachable()
285 #elif defined(_MSC_VER)
286 # define GGML_UNREACHABLE() __assume(0)
287 #else
288 # define GGML_UNREACHABLE() ((void) 0)
289 #endif
290
291 #ifdef __cplusplus
292 # define GGML_NORETURN [[noreturn]]
293 #elif defined(_MSC_VER)
294 # define GGML_NORETURN __declspec(noreturn)
295 #else
296 # define GGML_NORETURN _Noreturn
297 #endif
298
299 #define GGML_ABORT(...) ggml_abort(__FILE__, __LINE__, __VA_ARGS__)
300 #define GGML_ASSERT(x) if (!(x)) GGML_ABORT("GGML_ASSERT(%s) failed", #x)
301
302 // used to copy the number of elements and stride in bytes of tensors into local variables.
303 // main purpose is to reduce code duplication and improve readability.
304 //
305 // example:
306 //
307 // GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
308 // GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
309 //
310 #define GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
311 const type prefix##0 = (pointer) ? (pointer)->array[0] : 0; \
312 GGML_UNUSED(prefix##0);
313 #define GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
314 GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
315 const type prefix##1 = (pointer) ? (pointer)->array[1] : 0; \
316 GGML_UNUSED(prefix##1);
317 #define GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
318 GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
319 const type prefix##2 = (pointer) ? (pointer)->array[2] : 0; \
320 GGML_UNUSED(prefix##2);
321 #define GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
322 GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
323 const type prefix##3 = (pointer) ? (pointer)->array[3] : 0; \
324 GGML_UNUSED(prefix##3);
325
326 #define GGML_TENSOR_UNARY_OP_LOCALS \
327 GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
328 GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
329 GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
330 GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
331
332 #define GGML_TENSOR_BINARY_OP_LOCALS \
333 GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
334 GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
335 GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
336 GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
337 GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
338 GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
339
340 #define GGML_TENSOR_TERNARY_OP_LOCALS \
341 GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
342 GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
343 GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
344 GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
345 GGML_TENSOR_LOCALS(int64_t, ne2, src2, ne) \
346 GGML_TENSOR_LOCALS(size_t, nb2, src2, nb) \
347 GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
348 GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
349
350 #define GGML_TENSOR_BINARY_OP_LOCALS01 \
351 GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
352 GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
353 GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
354 GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
355
356 #ifdef __cplusplus
357 extern "C" {
358 #endif
359
360 // Function type used in fatal error callbacks
361 typedef void (*ggml_abort_callback_t)(const char * error_message);
362
363 // Set the abort callback (passing null will restore original abort functionality: printing a message to stdout)
364 // Returns the old callback for chaining
365 GGML_API ggml_abort_callback_t ggml_set_abort_callback(ggml_abort_callback_t callback);
366
367 GGML_NORETURN GGML_ATTRIBUTE_FORMAT(3, 4)
368 GGML_API void ggml_abort(const char * file, int line, const char * fmt, ...);
369
370 GGML_API void * ggml_malloc(size_t size);
371 GGML_API void * ggml_calloc(size_t num, size_t size);
372 GGML_API void * ggml_realloc(void * ptr, size_t size);
373 #ifdef __cplusplus
374 GGML_API void ggml_raw_free(void * ptr, bool toRemoveFromPool = true);
375 #else
376 GGML_API void ggml_raw_free(void * ptr, bool toRemoveFromPool);
377 #endif
378
379 enum ggml_status {
380 GGML_STATUS_ALLOC_FAILED = -2,
381 GGML_STATUS_FAILED = -1,
382 GGML_STATUS_SUCCESS = 0,
383 GGML_STATUS_ABORTED = 1,
384 };
385
386 // get ggml_status name string
387 GGML_API const char * ggml_status_to_string(enum ggml_status status);
388
389 // ieee 754-2008 half-precision float16
390 // todo: make this not an integral type
391 typedef uint16_t ggml_fp16_t;
392 GGML_API float ggml_fp16_to_fp32(ggml_fp16_t);
393 GGML_API ggml_fp16_t ggml_fp32_to_fp16(float);
394 GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t *, float *, int64_t);
395 GGML_API void ggml_fp32_to_fp16_row(const float *, ggml_fp16_t *, int64_t);
396
397 // google brain half-precision bfloat16
398 typedef struct { uint16_t bits; } ggml_bf16_t;
399 GGML_API ggml_bf16_t ggml_fp32_to_bf16(float);
400 GGML_API float ggml_bf16_to_fp32(ggml_bf16_t); // consider just doing << 16
401 GGML_API void ggml_bf16_to_fp32_row(const ggml_bf16_t *, float *, int64_t);
402 GGML_API void ggml_fp32_to_bf16_row_ref(const float *, ggml_bf16_t *, int64_t);
403 GGML_API void ggml_fp32_to_bf16_row(const float *, ggml_bf16_t *, int64_t);
404
405 struct ggml_object;
406 struct ggml_context;
407 struct ggml_cgraph;
408
409 // NOTE: always add types at the end of the enum to keep backward compatibility
410 enum ggml_type {
411 GGML_TYPE_F32 = 0,
412 GGML_TYPE_F16 = 1,
413 GGML_TYPE_Q4_0 = 2,
414 GGML_TYPE_Q4_1 = 3,
415 // GGML_TYPE_Q4_2 = 4, support has been removed
416 // GGML_TYPE_Q4_3 = 5, support has been removed
417 GGML_TYPE_Q5_0 = 6,
418 GGML_TYPE_Q5_1 = 7,
419 GGML_TYPE_Q8_0 = 8,
420 GGML_TYPE_Q8_1 = 9,
421 GGML_TYPE_Q2_K = 10,
422 GGML_TYPE_Q3_K = 11,
423 GGML_TYPE_Q4_K = 12,
424 GGML_TYPE_Q5_K = 13,
425 GGML_TYPE_Q6_K = 14,
426 GGML_TYPE_Q8_K = 15,
427 GGML_TYPE_IQ2_XXS = 16,
428 GGML_TYPE_IQ2_XS = 17,
429 GGML_TYPE_IQ3_XXS = 18,
430 GGML_TYPE_IQ1_S = 19,
431 GGML_TYPE_IQ4_NL = 20,
432 GGML_TYPE_IQ3_S = 21,
433 GGML_TYPE_IQ2_S = 22,
434 GGML_TYPE_IQ4_XS = 23,
435 GGML_TYPE_I8 = 24,
436 GGML_TYPE_I16 = 25,
437 GGML_TYPE_I32 = 26,
438 GGML_TYPE_I64 = 27,
439 GGML_TYPE_F64 = 28,
440 GGML_TYPE_IQ1_M = 29,
441 GGML_TYPE_BF16 = 30,
442 // GGML_TYPE_Q4_0_4_4 = 31, support has been removed from gguf files
443 // GGML_TYPE_Q4_0_4_8 = 32,
444 // GGML_TYPE_Q4_0_8_8 = 33,
445 GGML_TYPE_TQ1_0 = 34,
446 GGML_TYPE_TQ2_0 = 35,
447 // GGML_TYPE_IQ4_NL_4_4 = 36,
448 // GGML_TYPE_IQ4_NL_4_8 = 37,
449 // GGML_TYPE_IQ4_NL_8_8 = 38,
450 GGML_TYPE_MXFP4 = 39, // MXFP4 (1 block)
451 GGML_TYPE_COUNT = 40,
452 };
453
454 // precision
455 enum ggml_prec {
456 GGML_PREC_DEFAULT = 0, // stored as ggml_tensor.op_params, 0 by default
457 GGML_PREC_F32 = 10,
458 };
459
460 // model file types
461 enum ggml_ftype {
462 GGML_FTYPE_UNKNOWN = -1,
463 GGML_FTYPE_ALL_F32 = 0,
464 GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
465 GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
466 GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
467 GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
468 GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
469 GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
470 GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
471 GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
472 GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
473 GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
474 GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
475 GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
476 GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors
477 GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors
478 GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors
479 GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors
480 GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors
481 GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors
482 GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors
483 GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors
484 GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors
485 GGML_FTYPE_MOSTLY_BF16 = 24, // except 1d tensors
486 GGML_FTYPE_MOSTLY_MXFP4 = 25, // except 1d tensors
487 };
488
489 // available tensor operations:
490 enum ggml_op {
491 GGML_OP_NONE = 0,
492
493 GGML_OP_DUP,
494 GGML_OP_ADD,
495 GGML_OP_ADD_ID,
496 GGML_OP_ADD1,
497 GGML_OP_ACC,
498 GGML_OP_SUB,
499 GGML_OP_MUL,
500 GGML_OP_DIV,
501 GGML_OP_SQR,
502 GGML_OP_SQRT,
503 GGML_OP_LOG,
504 GGML_OP_SIN,
505 GGML_OP_COS,
506 GGML_OP_SUM,
507 GGML_OP_SUM_ROWS,
508 GGML_OP_CUMSUM,
509 GGML_OP_MEAN,
510 GGML_OP_ARGMAX,
511 GGML_OP_COUNT_EQUAL,
512 GGML_OP_REPEAT,
513 GGML_OP_REPEAT_BACK,
514 GGML_OP_CONCAT,
515 GGML_OP_SILU_BACK,
516 GGML_OP_NORM, // normalize
517 GGML_OP_RMS_NORM,
518 GGML_OP_RMS_NORM_BACK,
519 GGML_OP_GROUP_NORM,
520 GGML_OP_L2_NORM,
521
522 GGML_OP_MUL_MAT,
523 GGML_OP_MUL_MAT_ID,
524 GGML_OP_OUT_PROD,
525
526 GGML_OP_SCALE,
527 GGML_OP_SET,
528 GGML_OP_CPY,
529 GGML_OP_CONT,
530 GGML_OP_RESHAPE,
531 GGML_OP_VIEW,
532 GGML_OP_PERMUTE,
533 GGML_OP_TRANSPOSE,
534 GGML_OP_GET_ROWS,
535 GGML_OP_GET_ROWS_BACK,
536 GGML_OP_SET_ROWS,
537 GGML_OP_DIAG,
538 GGML_OP_DIAG_MASK_INF,
539 GGML_OP_DIAG_MASK_ZERO,
540 GGML_OP_SOFT_MAX,
541 GGML_OP_SOFT_MAX_BACK,
542 GGML_OP_ROPE,
543 GGML_OP_ROPE_BACK,
544 GGML_OP_CLAMP,
545 GGML_OP_CONV_TRANSPOSE_1D,
546 GGML_OP_IM2COL,
547 GGML_OP_IM2COL_BACK,
548 GGML_OP_IM2COL_3D,
549 GGML_OP_CONV_2D,
550 GGML_OP_CONV_3D,
551 GGML_OP_CONV_2D_DW,
552 GGML_OP_CONV_TRANSPOSE_2D,
553 GGML_OP_POOL_1D,
554 GGML_OP_POOL_2D,
555 GGML_OP_POOL_2D_BACK,
556 GGML_OP_UPSCALE,
557 GGML_OP_PAD,
558 GGML_OP_PAD_REFLECT_1D,
559 GGML_OP_ROLL,
560 GGML_OP_ARANGE,
561 GGML_OP_TIMESTEP_EMBEDDING,
562 GGML_OP_ARGSORT,
563 GGML_OP_TOP_K,
564 GGML_OP_LEAKY_RELU,
565 GGML_OP_TRI,
566 GGML_OP_FILL,
567
568 GGML_OP_FLASH_ATTN_EXT,
569 GGML_OP_FLASH_ATTN_BACK,
570 GGML_OP_SSM_CONV,
571 GGML_OP_SSM_SCAN,
572 GGML_OP_WIN_PART,
573 GGML_OP_WIN_UNPART,
574 GGML_OP_GET_REL_POS,
575 GGML_OP_ADD_REL_POS,
576 GGML_OP_RWKV_WKV6,
577 GGML_OP_GATED_LINEAR_ATTN,
578 GGML_OP_RWKV_WKV7,
579 GGML_OP_SOLVE_TRI,
580
581 GGML_OP_UNARY,
582
583 GGML_OP_MAP_CUSTOM1,
584 GGML_OP_MAP_CUSTOM2,
585 GGML_OP_MAP_CUSTOM3,
586
587 GGML_OP_CUSTOM,
588
589 GGML_OP_CROSS_ENTROPY_LOSS,
590 GGML_OP_CROSS_ENTROPY_LOSS_BACK,
591 GGML_OP_OPT_STEP_ADAMW,
592 GGML_OP_OPT_STEP_SGD,
593
594 GGML_OP_GLU,
595
596 GGML_OP_COUNT,
597 };
598
599 enum ggml_unary_op {
600 GGML_UNARY_OP_ABS,
601 GGML_UNARY_OP_SGN,
602 GGML_UNARY_OP_NEG,
603 GGML_UNARY_OP_STEP,
604 GGML_UNARY_OP_TANH,
605 GGML_UNARY_OP_ELU,
606 GGML_UNARY_OP_RELU,
607 GGML_UNARY_OP_SIGMOID,
608 GGML_UNARY_OP_GELU,
609 GGML_UNARY_OP_GELU_QUICK,
610 GGML_UNARY_OP_SILU,
611 GGML_UNARY_OP_HARDSWISH,
612 GGML_UNARY_OP_HARDSIGMOID,
613 GGML_UNARY_OP_EXP,
614 GGML_UNARY_OP_EXPM1,
615 GGML_UNARY_OP_SOFTPLUS,
616 GGML_UNARY_OP_GELU_ERF,
617 GGML_UNARY_OP_XIELU,
618 GGML_UNARY_OP_FLOOR,
619 GGML_UNARY_OP_CEIL,
620 GGML_UNARY_OP_ROUND,
621 GGML_UNARY_OP_TRUNC,
622
623 GGML_UNARY_OP_COUNT,
624 };
625
626 enum ggml_glu_op {
627 GGML_GLU_OP_REGLU,
628 GGML_GLU_OP_GEGLU,
629 GGML_GLU_OP_SWIGLU,
630 GGML_GLU_OP_SWIGLU_OAI,
631 GGML_GLU_OP_GEGLU_ERF,
632 GGML_GLU_OP_GEGLU_QUICK,
633
634 GGML_GLU_OP_COUNT,
635 };
636
637 enum ggml_object_type {
638 GGML_OBJECT_TYPE_TENSOR,
639 GGML_OBJECT_TYPE_GRAPH,
640 GGML_OBJECT_TYPE_WORK_BUFFER
641 };
642
643 enum ggml_log_level {
644 GGML_LOG_LEVEL_NONE = 0,
645 GGML_LOG_LEVEL_DEBUG = 1,
646 GGML_LOG_LEVEL_INFO = 2,
647 GGML_LOG_LEVEL_WARN = 3,
648 GGML_LOG_LEVEL_ERROR = 4,
649 GGML_LOG_LEVEL_CONT = 5, // continue previous log
650 };
651
652 // this tensor...
653 enum ggml_tensor_flag {
654 GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
655 GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
656 GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
657 GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up)
658 GGML_TENSOR_FLAG_COMPUTE = 16, // ...must be computed
659 };
660
661 enum ggml_tri_type {
662 GGML_TRI_TYPE_UPPER_DIAG = 0,
663 GGML_TRI_TYPE_UPPER = 1,
664 GGML_TRI_TYPE_LOWER_DIAG = 2,
665 GGML_TRI_TYPE_LOWER = 3
666 };
667
668 struct ggml_init_params {
669 // memory pool
670 size_t mem_size; // bytes
671 void * mem_buffer; // if NULL, memory will be allocated internally
672 bool no_alloc; // don't allocate memory for the tensor data
673 };
674
675 // n-dimensional tensor
676 struct ggml_tensor {
677 enum ggml_type type;
678
679 struct ggml_backend_buffer * buffer;
680
681 int64_t ne[GGML_MAX_DIMS]; // number of elements
682 size_t nb[GGML_MAX_DIMS]; // stride in bytes:
683 // nb[0] = ggml_type_size(type)
684 // nb[1] = nb[0] * (ne[0] / ggml_blck_size(type)) + padding
685 // nb[i] = nb[i-1] * ne[i-1]
686
687 // compute data
688 enum ggml_op op;
689
690 // op params - allocated as int32_t for alignment
691 int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
692
693 int32_t flags;
694
695 struct ggml_tensor * src[GGML_MAX_SRC];
696
697 // source tensor and offset for views
698 struct ggml_tensor * view_src;
699 size_t view_offs;
700
701 void * data;
702
703 char name[GGML_MAX_NAME];
704
705 void * extra; // extra things e.g. for ggml-cuda.cu
706
707 char padding[8];
708 };
709
710 static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor);
711
712 // Abort callback
713 // If not NULL, called before ggml computation
714 // If it returns true, the computation is aborted
715 typedef bool (*ggml_abort_callback)(void * data);
716
717
718 //
719 // GUID
720 //
721
722 // GUID types
723 typedef uint8_t ggml_guid[16];
724 typedef ggml_guid * ggml_guid_t;
725
726 GGML_API bool ggml_guid_matches(ggml_guid_t guid_a, ggml_guid_t guid_b);
727
728 // misc
729
730 GGML_API const char * ggml_version(void);
731 GGML_API const char * ggml_commit(void);
732
733 GGML_API void ggml_time_init(void); // call this once at the beginning of the program
734 GGML_API int64_t ggml_time_ms(void);
735 GGML_API int64_t ggml_time_us(void);
736 GGML_API int64_t ggml_cycles(void);
737 GGML_API int64_t ggml_cycles_per_ms(void);
738
739 // accepts a UTF-8 path, even on Windows
740 GGML_API FILE * ggml_fopen(const char * fname, const char * mode);
741
742 GGML_API void ggml_print_object (const struct ggml_object * obj);
743 GGML_API void ggml_print_objects(const struct ggml_context * ctx);
744
745 GGML_API int64_t ggml_nelements (const struct ggml_tensor * tensor);
746 GGML_API int64_t ggml_nrows (const struct ggml_tensor * tensor);
747 GGML_API size_t ggml_nbytes (const struct ggml_tensor * tensor);
748 GGML_API size_t ggml_nbytes_pad(const struct ggml_tensor * tensor); // same as ggml_nbytes() but padded to GGML_MEM_ALIGN
749
750 GGML_API int64_t ggml_blck_size(enum ggml_type type);
751 GGML_API size_t ggml_type_size(enum ggml_type type); // size in bytes for all elements in a block
752 GGML_API size_t ggml_row_size (enum ggml_type type, int64_t ne); // size in bytes for all elements in a row
753
754 GGML_API const char * ggml_type_name(enum ggml_type type);
755 GGML_API const char * ggml_op_name (enum ggml_op op);
756 GGML_API const char * ggml_op_symbol(enum ggml_op op);
757
758 GGML_API const char * ggml_unary_op_name(enum ggml_unary_op op);
759 GGML_API const char * ggml_glu_op_name(enum ggml_glu_op op);
760 GGML_API const char * ggml_op_desc(const struct ggml_tensor * t); // unary or op name
761
762 GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor);
763
764 GGML_API bool ggml_is_quantized(enum ggml_type type);
765
766 // TODO: temporary until model loading of ggml examples is refactored
767 GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
768
769 GGML_API bool ggml_is_transposed(const struct ggml_tensor * tensor);
770 GGML_API bool ggml_is_permuted (const struct ggml_tensor * tensor);
771 GGML_API bool ggml_is_empty (const struct ggml_tensor * tensor);
772 GGML_API bool ggml_is_view (const struct ggml_tensor * tensor);
773 GGML_API bool ggml_is_scalar (const struct ggml_tensor * tensor);
774 GGML_API bool ggml_is_vector (const struct ggml_tensor * tensor);
775 GGML_API bool ggml_is_matrix (const struct ggml_tensor * tensor);
776 GGML_API bool ggml_is_3d (const struct ggml_tensor * tensor);
777 GGML_API int ggml_n_dims (const struct ggml_tensor * tensor); // returns 1 for scalars
778
779 // returns whether the tensor elements can be iterated over with a flattened index (no gaps, no permutation)
780 GGML_API bool ggml_is_contiguous (const struct ggml_tensor * tensor);
781 GGML_API bool ggml_is_contiguous_0(const struct ggml_tensor * tensor); // same as ggml_is_contiguous()
782 GGML_API bool ggml_is_contiguous_1(const struct ggml_tensor * tensor); // contiguous for dims >= 1
783 GGML_API bool ggml_is_contiguous_2(const struct ggml_tensor * tensor); // contiguous for dims >= 2
784
785 // returns whether the tensor elements are allocated as one contiguous block of memory (no gaps, but permutation ok)
786 GGML_API bool ggml_is_contiguously_allocated(const struct ggml_tensor * tensor);
787
788 // true for tensor that is stored in memory as CxWxHxN and has been permuted to WxHxCxN
789 GGML_API bool ggml_is_contiguous_channels(const struct ggml_tensor * tensor);
790
791 // true if the elements in dimension 0 are contiguous, or there is just 1 block of elements
792 GGML_API bool ggml_is_contiguous_rows(const struct ggml_tensor * tensor);
793
794 GGML_API bool ggml_are_same_shape (const struct ggml_tensor * t0, const struct ggml_tensor * t1);
795 GGML_API bool ggml_are_same_stride(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
796
797 GGML_API bool ggml_can_repeat(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
798
799 // use this to compute the memory overhead of a tensor
800 GGML_API size_t ggml_tensor_overhead(void);
801
802 GGML_API bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbytes);
803
804 // main
805
806 GGML_API struct ggml_context * ggml_init (struct ggml_init_params params);
807 GGML_API void ggml_reset(struct ggml_context * ctx);
808 GGML_API void ggml_free (struct ggml_context * ctx);
809
810 GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
811
812 GGML_API bool ggml_get_no_alloc(struct ggml_context * ctx);
813 GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
814
815 GGML_API void * ggml_get_mem_buffer (const struct ggml_context * ctx);
816 GGML_API size_t ggml_get_mem_size (const struct ggml_context * ctx);
817 GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx);
818
819 GGML_API struct ggml_tensor * ggml_new_tensor(
820 struct ggml_context * ctx,
821 enum ggml_type type,
822 int n_dims,
823 const int64_t *ne);
824
825 GGML_API struct ggml_tensor * ggml_new_tensor_1d(
826 struct ggml_context * ctx,
827 enum ggml_type type,
828 int64_t ne0);
829
830 GGML_API struct ggml_tensor * ggml_new_tensor_2d(
831 struct ggml_context * ctx,
832 enum ggml_type type,
833 int64_t ne0,
834 int64_t ne1);
835
836 GGML_API struct ggml_tensor * ggml_new_tensor_3d(
837 struct ggml_context * ctx,
838 enum ggml_type type,
839 int64_t ne0,
840 int64_t ne1,
841 int64_t ne2);
842
843 GGML_API struct ggml_tensor * ggml_new_tensor_4d(
844 struct ggml_context * ctx,
845 enum ggml_type type,
846 int64_t ne0,
847 int64_t ne1,
848 int64_t ne2,
849 int64_t ne3);
850
851 GGML_API void * ggml_new_buffer(struct ggml_context * ctx, size_t nbytes);
852
853 GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src);
854 GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, struct ggml_tensor * src);
855
856 // Context tensor enumeration and lookup
857 GGML_API struct ggml_tensor * ggml_get_first_tensor(const struct ggml_context * ctx);
858 GGML_API struct ggml_tensor * ggml_get_next_tensor (const struct ggml_context * ctx, struct ggml_tensor * tensor);
859 GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name);
860
861 // Converts a flat index into coordinates
862 GGML_API void ggml_unravel_index(const struct ggml_tensor * tensor, int64_t i, int64_t * i0, int64_t * i1, int64_t * i2, int64_t * i3);
863
864 GGML_API enum ggml_unary_op ggml_get_unary_op(const struct ggml_tensor * tensor);
865 GGML_API enum ggml_glu_op ggml_get_glu_op(const struct ggml_tensor * tensor);
866
867 GGML_API void * ggml_get_data (const struct ggml_tensor * tensor);
868 GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor);
869
870 GGML_API const char * ggml_get_name (const struct ggml_tensor * tensor);
871 GGML_API struct ggml_tensor * ggml_set_name ( struct ggml_tensor * tensor, const char * name);
872 GGML_ATTRIBUTE_FORMAT(2, 3)
873 GGML_API struct ggml_tensor * ggml_format_name( struct ggml_tensor * tensor, const char * fmt, ...);
874
875 // Tensor flags
876 GGML_API void ggml_set_input(struct ggml_tensor * tensor);
877 GGML_API void ggml_set_output(struct ggml_tensor * tensor);
878 GGML_API void ggml_set_param(struct ggml_tensor * tensor);
879 GGML_API void ggml_set_loss(struct ggml_tensor * tensor);
880
881 //
882 // operations on tensors with backpropagation
883 //
884
885 GGML_API struct ggml_tensor * ggml_dup(
886 struct ggml_context * ctx,
887 struct ggml_tensor * a);
888
889 // in-place, returns view(a)
890 GGML_API struct ggml_tensor * ggml_dup_inplace(
891 struct ggml_context * ctx,
892 struct ggml_tensor * a);
893
894 GGML_API struct ggml_tensor * ggml_add(
895 struct ggml_context * ctx,
896 struct ggml_tensor * a,
897 struct ggml_tensor * b);
898
899 GGML_API struct ggml_tensor * ggml_add_inplace(
900 struct ggml_context * ctx,
901 struct ggml_tensor * a,
902 struct ggml_tensor * b);
903
904 GGML_API struct ggml_tensor * ggml_add_cast(
905 struct ggml_context * ctx,
906 struct ggml_tensor * a,
907 struct ggml_tensor * b,
908 enum ggml_type type);
909
910 // dst[i0, i1, i2] = a[i0, i1, i2] + b[i0, ids[i1, i2]]
911 GGML_API struct ggml_tensor * ggml_add_id(
912 struct ggml_context * ctx,
913 struct ggml_tensor * a,
914 struct ggml_tensor * b,
915 struct ggml_tensor * ids);
916
917 GGML_API struct ggml_tensor * ggml_add1(
918 struct ggml_context * ctx,
919 struct ggml_tensor * a,
920 struct ggml_tensor * b);
921
922 GGML_API struct ggml_tensor * ggml_add1_inplace(
923 struct ggml_context * ctx,
924 struct ggml_tensor * a,
925 struct ggml_tensor * b);
926
927 // dst = a
928 // view(dst, nb1, nb2, nb3, offset) += b
929 // return dst
930 GGML_API struct ggml_tensor * ggml_acc(
931 struct ggml_context * ctx,
932 struct ggml_tensor * a,
933 struct ggml_tensor * b,
934 size_t nb1,
935 size_t nb2,
936 size_t nb3,
937 size_t offset);
938
939 GGML_API struct ggml_tensor * ggml_acc_inplace(
940 struct ggml_context * ctx,
941 struct ggml_tensor * a,
942 struct ggml_tensor * b,
943 size_t nb1,
944 size_t nb2,
945 size_t nb3,
946 size_t offset);
947
948 GGML_API struct ggml_tensor * ggml_sub(
949 struct ggml_context * ctx,
950 struct ggml_tensor * a,
951 struct ggml_tensor * b);
952
953 GGML_API struct ggml_tensor * ggml_sub_inplace(
954 struct ggml_context * ctx,
955 struct ggml_tensor * a,
956 struct ggml_tensor * b);
957
958 GGML_API struct ggml_tensor * ggml_mul(
959 struct ggml_context * ctx,
960 struct ggml_tensor * a,
961 struct ggml_tensor * b);
962
963 GGML_API struct ggml_tensor * ggml_mul_inplace(
964 struct ggml_context * ctx,
965 struct ggml_tensor * a,
966 struct ggml_tensor * b);
967
968 GGML_API struct ggml_tensor * ggml_div(
969 struct ggml_context * ctx,
970 struct ggml_tensor * a,
971 struct ggml_tensor * b);
972
973 GGML_API struct ggml_tensor * ggml_div_inplace(
974 struct ggml_context * ctx,
975 struct ggml_tensor * a,
976 struct ggml_tensor * b);
977
978 GGML_API struct ggml_tensor * ggml_sqr(
979 struct ggml_context * ctx,
980 struct ggml_tensor * a);
981
982 GGML_API struct ggml_tensor * ggml_sqr_inplace(
983 struct ggml_context * ctx,
984 struct ggml_tensor * a);
985
986 GGML_API struct ggml_tensor * ggml_sqrt(
987 struct ggml_context * ctx,
988 struct ggml_tensor * a);
989
990 GGML_API struct ggml_tensor * ggml_sqrt_inplace(
991 struct ggml_context * ctx,
992 struct ggml_tensor * a);
993
994 GGML_API struct ggml_tensor * ggml_log(
995 struct ggml_context * ctx,
996 struct ggml_tensor * a);
997
998 GGML_API struct ggml_tensor * ggml_log_inplace(
999 struct ggml_context * ctx,
1000 struct ggml_tensor * a);
1001
1002 GGML_API struct ggml_tensor * ggml_expm1(
1003 struct ggml_context * ctx,
1004 struct ggml_tensor * a);
1005
1006 GGML_API struct ggml_tensor * ggml_expm1_inplace(
1007 struct ggml_context * ctx,
1008 struct ggml_tensor * a);
1009
1010 GGML_API struct ggml_tensor * ggml_softplus(
1011 struct ggml_context * ctx,
1012 struct ggml_tensor * a);
1013
1014 GGML_API struct ggml_tensor * ggml_softplus_inplace(
1015 struct ggml_context * ctx,
1016 struct ggml_tensor * a);
1017
1018 GGML_API struct ggml_tensor * ggml_sin(
1019 struct ggml_context * ctx,
1020 struct ggml_tensor * a);
1021
1022 GGML_API struct ggml_tensor * ggml_sin_inplace(
1023 struct ggml_context * ctx,
1024 struct ggml_tensor * a);
1025
1026 GGML_API struct ggml_tensor * ggml_cos(
1027 struct ggml_context * ctx,
1028 struct ggml_tensor * a);
1029
1030 GGML_API struct ggml_tensor * ggml_cos_inplace(
1031 struct ggml_context * ctx,
1032 struct ggml_tensor * a);
1033
1034 // return scalar
1035 GGML_API struct ggml_tensor * ggml_sum(
1036 struct ggml_context * ctx,
1037 struct ggml_tensor * a);
1038
1039 // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
1040 GGML_API struct ggml_tensor * ggml_sum_rows(
1041 struct ggml_context * ctx,
1042 struct ggml_tensor * a);
1043
1044 GGML_API struct ggml_tensor * ggml_cumsum(
1045 struct ggml_context * ctx,
1046 struct ggml_tensor * a);
1047
1048 // mean along rows
1049 GGML_API struct ggml_tensor * ggml_mean(
1050 struct ggml_context * ctx,
1051 struct ggml_tensor * a);
1052
1053 // argmax along rows
1054 GGML_API struct ggml_tensor * ggml_argmax(
1055 struct ggml_context * ctx,
1056 struct ggml_tensor * a);
1057
1058 // count number of equal elements in a and b
1059 GGML_API struct ggml_tensor * ggml_count_equal(
1060 struct ggml_context * ctx,
1061 struct ggml_tensor * a,
1062 struct ggml_tensor * b);
1063
1064 // if a is the same shape as b, and a is not parameter, return a
1065 // otherwise, return a new tensor: repeat(a) to fit in b
1066 GGML_API struct ggml_tensor * ggml_repeat(
1067 struct ggml_context * ctx,
1068 struct ggml_tensor * a,
1069 struct ggml_tensor * b);
1070
1071 // repeat a to the specified shape
1072 GGML_API struct ggml_tensor * ggml_repeat_4d(
1073 struct ggml_context * ctx,
1074 struct ggml_tensor * a,
1075 int64_t ne0,
1076 int64_t ne1,
1077 int64_t ne2,
1078 int64_t ne3);
1079
1080 // sums repetitions in a into shape of b
1081 GGML_API struct ggml_tensor * ggml_repeat_back(
1082 struct ggml_context * ctx,
1083 struct ggml_tensor * a,
1084 struct ggml_tensor * b); // sum up values that are adjacent in dims > 0 instead of repeated with same stride
1085
1086 // concat a and b along dim
1087 // used in stable-diffusion
1088 GGML_API struct ggml_tensor * ggml_concat(
1089 struct ggml_context * ctx,
1090 struct ggml_tensor * a,
1091 struct ggml_tensor * b,
1092 int dim);
1093
1094 GGML_API struct ggml_tensor * ggml_abs(
1095 struct ggml_context * ctx,
1096 struct ggml_tensor * a);
1097
1098 GGML_API struct ggml_tensor * ggml_abs_inplace(
1099 struct ggml_context * ctx,
1100 struct ggml_tensor * a);
1101
1102 GGML_API struct ggml_tensor * ggml_sgn(
1103 struct ggml_context * ctx,
1104 struct ggml_tensor * a);
1105
1106 GGML_API struct ggml_tensor * ggml_sgn_inplace(
1107 struct ggml_context * ctx,
1108 struct ggml_tensor * a);
1109
1110 GGML_API struct ggml_tensor * ggml_neg(
1111 struct ggml_context * ctx,
1112 struct ggml_tensor * a);
1113
1114 GGML_API struct ggml_tensor * ggml_neg_inplace(
1115 struct ggml_context * ctx,
1116 struct ggml_tensor * a);
1117
1118 GGML_API struct ggml_tensor * ggml_step(
1119 struct ggml_context * ctx,
1120 struct ggml_tensor * a);
1121
1122 GGML_API struct ggml_tensor * ggml_step_inplace(
1123 struct ggml_context * ctx,
1124 struct ggml_tensor * a);
1125
1126 GGML_API struct ggml_tensor * ggml_tanh(
1127 struct ggml_context * ctx,
1128 struct ggml_tensor * a);
1129
1130 GGML_API struct ggml_tensor * ggml_tanh_inplace(
1131 struct ggml_context * ctx,
1132 struct ggml_tensor * a);
1133
1134 GGML_API struct ggml_tensor * ggml_elu(
1135 struct ggml_context * ctx,
1136 struct ggml_tensor * a);
1137
1138 GGML_API struct ggml_tensor * ggml_elu_inplace(
1139 struct ggml_context * ctx,
1140 struct ggml_tensor * a);
1141
1142 GGML_API struct ggml_tensor * ggml_relu(
1143 struct ggml_context * ctx,
1144 struct ggml_tensor * a);
1145
1146 GGML_API struct ggml_tensor * ggml_leaky_relu(
1147 struct ggml_context * ctx,
1148 struct ggml_tensor * a, float negative_slope, bool inplace);
1149
1150 GGML_API struct ggml_tensor * ggml_relu_inplace(
1151 struct ggml_context * ctx,
1152 struct ggml_tensor * a);
1153
1154 GGML_API struct ggml_tensor * ggml_sigmoid(
1155 struct ggml_context * ctx,
1156 struct ggml_tensor * a);
1157
1158 GGML_API struct ggml_tensor * ggml_sigmoid_inplace(
1159 struct ggml_context * ctx,
1160 struct ggml_tensor * a);
1161
1162 GGML_API struct ggml_tensor * ggml_gelu(
1163 struct ggml_context * ctx,
1164 struct ggml_tensor * a);
1165
1166 GGML_API struct ggml_tensor * ggml_gelu_inplace(
1167 struct ggml_context * ctx,
1168 struct ggml_tensor * a);
1169
1170 // GELU using erf (error function) when possible
1171 // some backends may fallback to approximation based on Abramowitz and Stegun formula
1172 GGML_API struct ggml_tensor * ggml_gelu_erf(
1173 struct ggml_context * ctx,
1174 struct ggml_tensor * a);
1175
1176 GGML_API struct ggml_tensor * ggml_gelu_erf_inplace(
1177 struct ggml_context * ctx,
1178 struct ggml_tensor * a);
1179
1180 GGML_API struct ggml_tensor * ggml_gelu_quick(
1181 struct ggml_context * ctx,
1182 struct ggml_tensor * a);
1183
1184 GGML_API struct ggml_tensor * ggml_gelu_quick_inplace(
1185 struct ggml_context * ctx,
1186 struct ggml_tensor * a);
1187
1188 GGML_API struct ggml_tensor * ggml_silu(
1189 struct ggml_context * ctx,
1190 struct ggml_tensor * a);
1191
1192 GGML_API struct ggml_tensor * ggml_silu_inplace(
1193 struct ggml_context * ctx,
1194 struct ggml_tensor * a);
1195
1196 // a - x
1197 // b - dy
1198 GGML_API struct ggml_tensor * ggml_silu_back(
1199 struct ggml_context * ctx,
1200 struct ggml_tensor * a,
1201 struct ggml_tensor * b);
1202
1203 // hardswish(x) = x * relu6(x + 3) / 6
1204 GGML_API struct ggml_tensor * ggml_hardswish(
1205 struct ggml_context * ctx,
1206 struct ggml_tensor * a);
1207
1208 // hardsigmoid(x) = relu6(x + 3) / 6
1209 GGML_API struct ggml_tensor * ggml_hardsigmoid(
1210 struct ggml_context * ctx,
1211 struct ggml_tensor * a);
1212
1213 GGML_API struct ggml_tensor * ggml_exp(
1214 struct ggml_context * ctx,
1215 struct ggml_tensor * a);
1216
1217 GGML_API struct ggml_tensor * ggml_exp_inplace(
1218 struct ggml_context * ctx,
1219 struct ggml_tensor * a);
1220
1221 GGML_API struct ggml_tensor * ggml_floor(
1222 struct ggml_context * ctx,
1223 struct ggml_tensor * a);
1224
1225 GGML_API struct ggml_tensor * ggml_floor_inplace(
1226 struct ggml_context * ctx,
1227 struct ggml_tensor * a);
1228
1229 GGML_API struct ggml_tensor * ggml_ceil(
1230 struct ggml_context * ctx,
1231 struct ggml_tensor * a);
1232
1233 GGML_API struct ggml_tensor * ggml_ceil_inplace(
1234 struct ggml_context * ctx,
1235 struct ggml_tensor * a);
1236
1237 GGML_API struct ggml_tensor * ggml_round(
1238 struct ggml_context * ctx,
1239 struct ggml_tensor * a);
1240
1241 GGML_API struct ggml_tensor * ggml_round_inplace(
1242 struct ggml_context * ctx,
1243 struct ggml_tensor * a);
1244
1245 /**
1246 * Truncates the fractional part of each element in the tensor (towards zero).
1247 * For example: trunc(3.7) = 3.0, trunc(-2.9) = -2.0
1248 * Similar to std::trunc in C/C++.
1249 */
1250
1251 GGML_API struct ggml_tensor * ggml_trunc(
1252 struct ggml_context * ctx,
1253 struct ggml_tensor * a);
1254
1255 GGML_API struct ggml_tensor * ggml_trunc_inplace(
1256 struct ggml_context * ctx,
1257 struct ggml_tensor * a);
1258
1259
1260
1261 // xIELU activation function
1262 // x = x * (c_a(alpha_n) + c_b(alpha_p, beta) * sigmoid(beta * x)) + eps * (x > 0)
1263 // where c_a = softplus and c_b(a, b) = softplus(a) + b are constraining functions
1264 // that constrain the positive and negative source alpha values respectively
1265 GGML_API struct ggml_tensor * ggml_xielu(
1266 struct ggml_context * ctx,
1267 struct ggml_tensor * a,
1268 float alpha_n,
1269 float alpha_p,
1270 float beta,
1271 float eps);
1272
1273 // gated linear unit ops
1274 // A: n columns, r rows,
1275 // result is n / 2 columns, r rows,
1276 // expects gate in second half of row, unless swapped is true
1277 GGML_API struct ggml_tensor * ggml_glu(
1278 struct ggml_context * ctx,
1279 struct ggml_tensor * a,
1280 enum ggml_glu_op op,
1281 bool swapped);
1282
1283 GGML_API struct ggml_tensor * ggml_reglu(
1284 struct ggml_context * ctx,
1285 struct ggml_tensor * a);
1286
1287 GGML_API struct ggml_tensor * ggml_reglu_swapped(
1288 struct ggml_context * ctx,
1289 struct ggml_tensor * a);
1290
1291 GGML_API struct ggml_tensor * ggml_geglu(
1292 struct ggml_context * ctx,
1293 struct ggml_tensor * a);
1294
1295 GGML_API struct ggml_tensor * ggml_geglu_swapped(
1296 struct ggml_context * ctx,
1297 struct ggml_tensor * a);
1298
1299 GGML_API struct ggml_tensor * ggml_swiglu(
1300 struct ggml_context * ctx,
1301 struct ggml_tensor * a);
1302
1303 GGML_API struct ggml_tensor * ggml_swiglu_swapped(
1304 struct ggml_context * ctx,
1305 struct ggml_tensor * a);
1306
1307 GGML_API struct ggml_tensor * ggml_geglu_erf(
1308 struct ggml_context * ctx,
1309 struct ggml_tensor * a);
1310
1311 GGML_API struct ggml_tensor * ggml_geglu_erf_swapped(
1312 struct ggml_context * ctx,
1313 struct ggml_tensor * a);
1314
1315 GGML_API struct ggml_tensor * ggml_geglu_quick(
1316 struct ggml_context * ctx,
1317 struct ggml_tensor * a);
1318
1319 GGML_API struct ggml_tensor * ggml_geglu_quick_swapped(
1320 struct ggml_context * ctx,
1321 struct ggml_tensor * a);
1322
1323 // A: n columns, r rows,
1324 // B: n columns, r rows,
1325 GGML_API struct ggml_tensor * ggml_glu_split(
1326 struct ggml_context * ctx,
1327 struct ggml_tensor * a,
1328 struct ggml_tensor * b,
1329 enum ggml_glu_op op);
1330
1331 GGML_API struct ggml_tensor * ggml_reglu_split(
1332 struct ggml_context * ctx,
1333 struct ggml_tensor * a,
1334 struct ggml_tensor * b);
1335
1336 GGML_API struct ggml_tensor * ggml_geglu_split(
1337 struct ggml_context * ctx,
1338 struct ggml_tensor * a,
1339 struct ggml_tensor * b);
1340
1341 GGML_API struct ggml_tensor * ggml_swiglu_split(
1342 struct ggml_context * ctx,
1343 struct ggml_tensor * a,
1344 struct ggml_tensor * b);
1345
1346 GGML_API struct ggml_tensor * ggml_geglu_erf_split(
1347 struct ggml_context * ctx,
1348 struct ggml_tensor * a,
1349 struct ggml_tensor * b);
1350
1351 GGML_API struct ggml_tensor * ggml_geglu_quick_split(
1352 struct ggml_context * ctx,
1353 struct ggml_tensor * a,
1354 struct ggml_tensor * b);
1355
1356 GGML_API struct ggml_tensor * ggml_swiglu_oai(
1357 struct ggml_context * ctx,
1358 struct ggml_tensor * a,
1359 struct ggml_tensor * b,
1360 float alpha,
1361 float limit);
1362
1363 // normalize along rows
1364 GGML_API struct ggml_tensor * ggml_norm(
1365 struct ggml_context * ctx,
1366 struct ggml_tensor * a,
1367 float eps);
1368
1369 GGML_API struct ggml_tensor * ggml_norm_inplace(
1370 struct ggml_context * ctx,
1371 struct ggml_tensor * a,
1372 float eps);
1373
1374 GGML_API struct ggml_tensor * ggml_rms_norm(
1375 struct ggml_context * ctx,
1376 struct ggml_tensor * a,
1377 float eps);
1378
1379 GGML_API struct ggml_tensor * ggml_rms_norm_inplace(
1380 struct ggml_context * ctx,
1381 struct ggml_tensor * a,
1382 float eps);
1383
1384 // group normalize along ne0*ne1*n_groups
1385 // used in stable-diffusion
1386 GGML_API struct ggml_tensor * ggml_group_norm(
1387 struct ggml_context * ctx,
1388 struct ggml_tensor * a,
1389 int n_groups,
1390 float eps);
1391
1392 GGML_API struct ggml_tensor * ggml_group_norm_inplace(
1393 struct ggml_context * ctx,
1394 struct ggml_tensor * a,
1395 int n_groups,
1396 float eps);
1397
1398 // l2 normalize along rows
1399 // used in rwkv v7
1400 GGML_API struct ggml_tensor * ggml_l2_norm(
1401 struct ggml_context * ctx,
1402 struct ggml_tensor * a,
1403 float eps);
1404
1405 GGML_API struct ggml_tensor * ggml_l2_norm_inplace(
1406 struct ggml_context * ctx,
1407 struct ggml_tensor * a,
1408 float eps);
1409
1410 // a - x
1411 // b - dy
1412 GGML_API struct ggml_tensor * ggml_rms_norm_back(
1413 struct ggml_context * ctx,
1414 struct ggml_tensor * a,
1415 struct ggml_tensor * b,
1416 float eps);
1417
1418 // A: k columns, n rows => [ne03, ne02, n, k]
1419 // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
1420 // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
1421 GGML_API struct ggml_tensor * ggml_mul_mat(
1422 struct ggml_context * ctx,
1423 struct ggml_tensor * a,
1424 struct ggml_tensor * b);
1425
1426 // change the precision of a matrix multiplication
1427 // set to GGML_PREC_F32 for higher precision (useful for phi-2)
1428 GGML_API void ggml_mul_mat_set_prec(
1429 struct ggml_tensor * a,
1430 enum ggml_prec prec);
1431
1432 // indirect matrix multiplication
1433 GGML_API struct ggml_tensor * ggml_mul_mat_id(
1434 struct ggml_context * ctx,
1435 struct ggml_tensor * as,
1436 struct ggml_tensor * b,
1437 struct ggml_tensor * ids);
1438
1439 // A: m columns, n rows,
1440 // B: p columns, n rows,
1441 // result is m columns, p rows
1442 GGML_API struct ggml_tensor * ggml_out_prod(
1443 struct ggml_context * ctx,
1444 struct ggml_tensor * a,
1445 struct ggml_tensor * b);
1446
1447 //
1448 // operations on tensors without backpropagation
1449 //
1450
1451 GGML_API struct ggml_tensor * ggml_scale(
1452 struct ggml_context * ctx,
1453 struct ggml_tensor * a,
1454 float s);
1455
1456 // in-place, returns view(a)
1457 GGML_API struct ggml_tensor * ggml_scale_inplace(
1458 struct ggml_context * ctx,
1459 struct ggml_tensor * a,
1460 float s);
1461
1462 // x = s * a + b
1463 GGML_API struct ggml_tensor * ggml_scale_bias(
1464 struct ggml_context * ctx,
1465 struct ggml_tensor * a,
1466 float s,
1467 float b);
1468
1469 GGML_API struct ggml_tensor * ggml_scale_bias_inplace(
1470 struct ggml_context * ctx,
1471 struct ggml_tensor * a,
1472 float s,
1473 float b);
1474
1475 // b -> view(a,offset,nb1,nb2,3), return modified a
1476 GGML_API struct ggml_tensor * ggml_set(
1477 struct ggml_context * ctx,
1478 struct ggml_tensor * a,
1479 struct ggml_tensor * b,
1480 size_t nb1,
1481 size_t nb2,
1482 size_t nb3,
1483 size_t offset); // in bytes
1484
1485 // b -> view(a,offset,nb1,nb2,3), return view(a)
1486 GGML_API struct ggml_tensor * ggml_set_inplace(
1487 struct ggml_context * ctx,
1488 struct ggml_tensor * a,
1489 struct ggml_tensor * b,
1490 size_t nb1,
1491 size_t nb2,
1492 size_t nb3,
1493 size_t offset); // in bytes
1494
1495 GGML_API struct ggml_tensor * ggml_set_1d(
1496 struct ggml_context * ctx,
1497 struct ggml_tensor * a,
1498 struct ggml_tensor * b,
1499 size_t offset); // in bytes
1500
1501 GGML_API struct ggml_tensor * ggml_set_1d_inplace(
1502 struct ggml_context * ctx,
1503 struct ggml_tensor * a,
1504 struct ggml_tensor * b,
1505 size_t offset); // in bytes
1506
1507 // b -> view(a,offset,nb1,nb2,3), return modified a
1508 GGML_API struct ggml_tensor * ggml_set_2d(
1509 struct ggml_context * ctx,
1510 struct ggml_tensor * a,
1511 struct ggml_tensor * b,
1512 size_t nb1,
1513 size_t offset); // in bytes
1514
1515 // b -> view(a,offset,nb1,nb2,3), return view(a)
1516 GGML_API struct ggml_tensor * ggml_set_2d_inplace(
1517 struct ggml_context * ctx,
1518 struct ggml_tensor * a,
1519 struct ggml_tensor * b,
1520 size_t nb1,
1521 size_t offset); // in bytes
1522
1523 // a -> b, return view(b)
1524 GGML_API struct ggml_tensor * ggml_cpy(
1525 struct ggml_context * ctx,
1526 struct ggml_tensor * a,
1527 struct ggml_tensor * b);
1528
1529 // note: casting from f32 to i32 will discard the fractional part
1530 GGML_API struct ggml_tensor * ggml_cast(
1531 struct ggml_context * ctx,
1532 struct ggml_tensor * a,
1533 enum ggml_type type);
1534
1535 // make contiguous
1536 GGML_API struct ggml_tensor * ggml_cont(
1537 struct ggml_context * ctx,
1538 struct ggml_tensor * a);
1539
1540 // make contiguous, with new shape
1541 GGML_API struct ggml_tensor * ggml_cont_1d(
1542 struct ggml_context * ctx,
1543 struct ggml_tensor * a,
1544 int64_t ne0);
1545
1546 GGML_API struct ggml_tensor * ggml_cont_2d(
1547 struct ggml_context * ctx,
1548 struct ggml_tensor * a,
1549 int64_t ne0,
1550 int64_t ne1);
1551
1552 GGML_API struct ggml_tensor * ggml_cont_3d(
1553 struct ggml_context * ctx,
1554 struct ggml_tensor * a,
1555 int64_t ne0,
1556 int64_t ne1,
1557 int64_t ne2);
1558
1559 GGML_API struct ggml_tensor * ggml_cont_4d(
1560 struct ggml_context * ctx,
1561 struct ggml_tensor * a,
1562 int64_t ne0,
1563 int64_t ne1,
1564 int64_t ne2,
1565 int64_t ne3);
1566
1567 // return view(a), b specifies the new shape
1568 // TODO: when we start computing gradient, make a copy instead of view
1569 GGML_API struct ggml_tensor * ggml_reshape(
1570 struct ggml_context * ctx,
1571 struct ggml_tensor * a,
1572 struct ggml_tensor * b);
1573
1574 // return view(a)
1575 // TODO: when we start computing gradient, make a copy instead of view
1576 GGML_API struct ggml_tensor * ggml_reshape_1d(
1577 struct ggml_context * ctx,
1578 struct ggml_tensor * a,
1579 int64_t ne0);
1580
1581 GGML_API struct ggml_tensor * ggml_reshape_2d(
1582 struct ggml_context * ctx,
1583 struct ggml_tensor * a,
1584 int64_t ne0,
1585 int64_t ne1);
1586
1587 // return view(a)
1588 // TODO: when we start computing gradient, make a copy instead of view
1589 GGML_API struct ggml_tensor * ggml_reshape_3d(
1590 struct ggml_context * ctx,
1591 struct ggml_tensor * a,
1592 int64_t ne0,
1593 int64_t ne1,
1594 int64_t ne2);
1595
1596 GGML_API struct ggml_tensor * ggml_reshape_4d(
1597 struct ggml_context * ctx,
1598 struct ggml_tensor * a,
1599 int64_t ne0,
1600 int64_t ne1,
1601 int64_t ne2,
1602 int64_t ne3);
1603
1604 // offset in bytes
1605 GGML_API struct ggml_tensor * ggml_view_1d(
1606 struct ggml_context * ctx,
1607 struct ggml_tensor * a,
1608 int64_t ne0,
1609 size_t offset);
1610
1611 GGML_API struct ggml_tensor * ggml_view_2d(
1612 struct ggml_context * ctx,
1613 struct ggml_tensor * a,
1614 int64_t ne0,
1615 int64_t ne1,
1616 size_t nb1, // row stride in bytes
1617 size_t offset);
1618
1619 GGML_API struct ggml_tensor * ggml_view_3d(
1620 struct ggml_context * ctx,
1621 struct ggml_tensor * a,
1622 int64_t ne0,
1623 int64_t ne1,
1624 int64_t ne2,
1625 size_t nb1, // row stride in bytes
1626 size_t nb2, // slice stride in bytes
1627 size_t offset);
1628
1629 GGML_API struct ggml_tensor * ggml_view_4d(
1630 struct ggml_context * ctx,
1631 struct ggml_tensor * a,
1632 int64_t ne0,
1633 int64_t ne1,
1634 int64_t ne2,
1635 int64_t ne3,
1636 size_t nb1, // row stride in bytes
1637 size_t nb2, // slice stride in bytes
1638 size_t nb3,
1639 size_t offset);
1640
1641 GGML_API struct ggml_tensor * ggml_permute(
1642 struct ggml_context * ctx,
1643 struct ggml_tensor * a,
1644 int axis0,
1645 int axis1,
1646 int axis2,
1647 int axis3);
1648
1649 // alias for ggml_permute(ctx, a, 1, 0, 2, 3)
1650 GGML_API struct ggml_tensor * ggml_transpose(
1651 struct ggml_context * ctx,
1652 struct ggml_tensor * a);
1653
1654 // supports 4D a:
1655 // a [n_embd, ne1, ne2, ne3]
1656 // b I32 [n_rows, ne2, ne3, 1]
1657 //
1658 // return [n_embd, n_rows, ne2, ne3]
1659 GGML_API struct ggml_tensor * ggml_get_rows(
1660 struct ggml_context * ctx,
1661 struct ggml_tensor * a, // data
1662 struct ggml_tensor * b); // row indices
1663
1664 GGML_API struct ggml_tensor * ggml_get_rows_back(
1665 struct ggml_context * ctx,
1666 struct ggml_tensor * a, // gradients of ggml_get_rows result
1667 struct ggml_tensor * b, // row indices
1668 struct ggml_tensor * c); // data for ggml_get_rows, only used for its shape
1669
1670 // a TD [n_embd, ne1, ne2, ne3]
1671 // b TS [n_embd, n_rows, ne02, ne03] | ne02 == ne2, ne03 == ne3
1672 // c I64 [n_rows, ne11, ne12, 1] | c[i] in [0, ne1)
1673 //
1674 // undefined behavior if destination rows overlap
1675 //
1676 // broadcast:
1677 // ne2 % ne11 == 0
1678 // ne3 % ne12 == 0
1679 //
1680 // return view(a)
1681 GGML_API struct ggml_tensor * ggml_set_rows(
1682 struct ggml_context * ctx,
1683 struct ggml_tensor * a, // destination
1684 struct ggml_tensor * b, // source
1685 struct ggml_tensor * c); // row indices
1686
1687 GGML_API struct ggml_tensor * ggml_diag(
1688 struct ggml_context * ctx,
1689 struct ggml_tensor * a);
1690
1691 // set elements above the diagonal to -INF
1692 GGML_API struct ggml_tensor * ggml_diag_mask_inf(
1693 struct ggml_context * ctx,
1694 struct ggml_tensor * a,
1695 int n_past);
1696
1697 // in-place, returns view(a)
1698 GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace(
1699 struct ggml_context * ctx,
1700 struct ggml_tensor * a,
1701 int n_past);
1702
1703 // set elements above the diagonal to 0
1704 GGML_API struct ggml_tensor * ggml_diag_mask_zero(
1705 struct ggml_context * ctx,
1706 struct ggml_tensor * a,
1707 int n_past);
1708
1709 // in-place, returns view(a)
1710 GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace(
1711 struct ggml_context * ctx,
1712 struct ggml_tensor * a,
1713 int n_past);
1714
1715 GGML_API struct ggml_tensor * ggml_soft_max(
1716 struct ggml_context * ctx,
1717 struct ggml_tensor * a);
1718
1719 // in-place, returns view(a)
1720 GGML_API struct ggml_tensor * ggml_soft_max_inplace(
1721 struct ggml_context * ctx,
1722 struct ggml_tensor * a);
1723
1724 // a [ne0, ne01, ne02, ne03]
1725 // mask [ne0, ne11, ne12, ne13] | ne11 >= ne01, F16 or F32, optional
1726 //
1727 // broadcast:
1728 // ne02 % ne12 == 0
1729 // ne03 % ne13 == 0
1730 //
1731 // fused soft_max(a*scale + mask*(ALiBi slope))
1732 // max_bias = 0.0f for no ALiBi
1733 GGML_API struct ggml_tensor * ggml_soft_max_ext(
1734 struct ggml_context * ctx,
1735 struct ggml_tensor * a,
1736 struct ggml_tensor * mask,
1737 float scale,
1738 float max_bias);
1739
1740 GGML_API struct ggml_tensor * ggml_soft_max_ext_inplace(
1741 struct ggml_context * ctx,
1742 struct ggml_tensor * a,
1743 struct ggml_tensor * mask,
1744 float scale,
1745 float max_bias);
1746
1747 GGML_API void ggml_soft_max_add_sinks(
1748 struct ggml_tensor * a,
1749 struct ggml_tensor * sinks);
1750
1751 GGML_API struct ggml_tensor * ggml_soft_max_ext_back(
1752 struct ggml_context * ctx,
1753 struct ggml_tensor * a,
1754 struct ggml_tensor * b,
1755 float scale,
1756 float max_bias);
1757
1758 // in-place, returns view(a)
1759 GGML_API struct ggml_tensor * ggml_soft_max_ext_back_inplace(
1760 struct ggml_context * ctx,
1761 struct ggml_tensor * a,
1762 struct ggml_tensor * b,
1763 float scale,
1764 float max_bias);
1765
1766 // rotary position embedding
1767 // if (mode & 1) - skip n_past elements (NOT SUPPORTED)
1768 // if (mode & GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
1769 //
1770 // b is an int32 vector with size a->ne[2], it contains the positions
1771 GGML_API struct ggml_tensor * ggml_rope(
1772 struct ggml_context * ctx,
1773 struct ggml_tensor * a,
1774 struct ggml_tensor * b,
1775 int n_dims,
1776 int mode);
1777
1778 // in-place, returns view(a)
1779 GGML_API struct ggml_tensor * ggml_rope_inplace(
1780 struct ggml_context * ctx,
1781 struct ggml_tensor * a,
1782 struct ggml_tensor * b,
1783 int n_dims,
1784 int mode);
1785
1786 // custom RoPE
1787 // c is freq factors (e.g. phi3-128k), (optional)
1788 GGML_API struct ggml_tensor * ggml_rope_ext(
1789 struct ggml_context * ctx,
1790 struct ggml_tensor * a,
1791 struct ggml_tensor * b,
1792 struct ggml_tensor * c,
1793 int n_dims,
1794 int mode,
1795 int n_ctx_orig,
1796 float freq_base,
1797 float freq_scale,
1798 float ext_factor,
1799 float attn_factor,
1800 float beta_fast,
1801 float beta_slow);
1802
1803 GGML_API struct ggml_tensor * ggml_rope_multi(
1804 struct ggml_context * ctx,
1805 struct ggml_tensor * a,
1806 struct ggml_tensor * b,
1807 struct ggml_tensor * c,
1808 int n_dims,
1809 int sections[GGML_MROPE_SECTIONS],
1810 int mode,
1811 int n_ctx_orig,
1812 float freq_base,
1813 float freq_scale,
1814 float ext_factor,
1815 float attn_factor,
1816 float beta_fast,
1817 float beta_slow);
1818
1819 // in-place, returns view(a)
1820 GGML_API struct ggml_tensor * ggml_rope_ext_inplace(
1821 struct ggml_context * ctx,
1822 struct ggml_tensor * a,
1823 struct ggml_tensor * b,
1824 struct ggml_tensor * c,
1825 int n_dims,
1826 int mode,
1827 int n_ctx_orig,
1828 float freq_base,
1829 float freq_scale,
1830 float ext_factor,
1831 float attn_factor,
1832 float beta_fast,
1833 float beta_slow);
1834
1835 GGML_API struct ggml_tensor * ggml_rope_multi_inplace(
1836 struct ggml_context * ctx,
1837 struct ggml_tensor * a,
1838 struct ggml_tensor * b,
1839 struct ggml_tensor * c,
1840 int n_dims,
1841 int sections[GGML_MROPE_SECTIONS],
1842 int mode,
1843 int n_ctx_orig,
1844 float freq_base,
1845 float freq_scale,
1846 float ext_factor,
1847 float attn_factor,
1848 float beta_fast,
1849 float beta_slow);
1850
1851 GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom(
1852 struct ggml_context * ctx,
1853 struct ggml_tensor * a,
1854 struct ggml_tensor * b,
1855 int n_dims,
1856 int mode,
1857 int n_ctx_orig,
1858 float freq_base,
1859 float freq_scale,
1860 float ext_factor,
1861 float attn_factor,
1862 float beta_fast,
1863 float beta_slow),
1864 "use ggml_rope_ext instead");
1865
1866 GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom_inplace(
1867 struct ggml_context * ctx,
1868 struct ggml_tensor * a,
1869 struct ggml_tensor * b,
1870 int n_dims,
1871 int mode,
1872 int n_ctx_orig,
1873 float freq_base,
1874 float freq_scale,
1875 float ext_factor,
1876 float attn_factor,
1877 float beta_fast,
1878 float beta_slow),
1879 "use ggml_rope_ext_inplace instead");
1880
1881 // compute correction dims for YaRN RoPE scaling
1882 GGML_API void ggml_rope_yarn_corr_dims(
1883 int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]);
1884
1885 // rotary position embedding backward, i.e compute dx from dy
1886 // a - dy
1887 GGML_API struct ggml_tensor * ggml_rope_ext_back(
1888 struct ggml_context * ctx,
1889 struct ggml_tensor * a, // gradients of ggml_rope result
1890 struct ggml_tensor * b, // positions
1891 struct ggml_tensor * c, // freq factors
1892 int n_dims,
1893 int mode,
1894 int n_ctx_orig,
1895 float freq_base,
1896 float freq_scale,
1897 float ext_factor,
1898 float attn_factor,
1899 float beta_fast,
1900 float beta_slow);
1901
1902 GGML_API struct ggml_tensor * ggml_rope_multi_back(
1903 struct ggml_context * ctx,
1904 struct ggml_tensor * a,
1905 struct ggml_tensor * b,
1906 struct ggml_tensor * c,
1907 int n_dims,
1908 int sections[4],
1909 int mode,
1910 int n_ctx_orig,
1911 float freq_base,
1912 float freq_scale,
1913 float ext_factor,
1914 float attn_factor,
1915 float beta_fast,
1916 float beta_slow);
1917
1918
1919 // clamp
1920 // in-place, returns view(a)
1921 GGML_API struct ggml_tensor * ggml_clamp(
1922 struct ggml_context * ctx,
1923 struct ggml_tensor * a,
1924 float min,
1925 float max);
1926
1927 // im2col
1928 // converts data into a format that effectively results in a convolution when combined with matrix multiplication
1929 GGML_API struct ggml_tensor * ggml_im2col(
1930 struct ggml_context * ctx,
1931 struct ggml_tensor * a, // convolution kernel
1932 struct ggml_tensor * b, // data
1933 int s0, // stride dimension 0
1934 int s1, // stride dimension 1
1935 int p0, // padding dimension 0
1936 int p1, // padding dimension 1
1937 int d0, // dilation dimension 0
1938 int d1, // dilation dimension 1
1939 bool is_2D,
1940 enum ggml_type dst_type);
1941
1942 GGML_API struct ggml_tensor * ggml_im2col_back(
1943 struct ggml_context * ctx,
1944 struct ggml_tensor * a, // convolution kernel
1945 struct ggml_tensor * b, // gradient of im2col output
1946 int64_t * ne, // shape of im2col input
1947 int s0, // stride dimension 0
1948 int s1, // stride dimension 1
1949 int p0, // padding dimension 0
1950 int p1, // padding dimension 1
1951 int d0, // dilation dimension 0
1952 int d1, // dilation dimension 1
1953 bool is_2D);
1954
1955 GGML_API struct ggml_tensor * ggml_conv_1d(
1956 struct ggml_context * ctx,
1957 struct ggml_tensor * a, // convolution kernel
1958 struct ggml_tensor * b, // data
1959 int s0, // stride
1960 int p0, // padding
1961 int d0); // dilation
1962
1963 // conv_1d with padding = half
1964 // alias for ggml_conv_1d(a, b, s, a->ne[0]/2, d)
1965 GGML_API struct ggml_tensor* ggml_conv_1d_ph(
1966 struct ggml_context * ctx,
1967 struct ggml_tensor * a, // convolution kernel
1968 struct ggml_tensor * b, // data
1969 int s, // stride
1970 int d); // dilation
1971
1972 // depthwise
1973 // TODO: this is very likely wrong for some cases! - needs more testing
1974 GGML_API struct ggml_tensor * ggml_conv_1d_dw(
1975 struct ggml_context * ctx,
1976 struct ggml_tensor * a, // convolution kernel
1977 struct ggml_tensor * b, // data
1978 int s0, // stride
1979 int p0, // padding
1980 int d0); // dilation
1981
1982 GGML_API struct ggml_tensor * ggml_conv_1d_dw_ph(
1983 struct ggml_context * ctx,
1984 struct ggml_tensor * a, // convolution kernel
1985 struct ggml_tensor * b, // data
1986 int s0, // stride
1987 int d0); // dilation
1988
1989 GGML_API struct ggml_tensor * ggml_conv_transpose_1d(
1990 struct ggml_context * ctx,
1991 struct ggml_tensor * a, // convolution kernel
1992 struct ggml_tensor * b, // data
1993 int s0, // stride
1994 int p0, // padding
1995 int d0); // dilation
1996
1997 GGML_API struct ggml_tensor * ggml_conv_2d(
1998 struct ggml_context * ctx,
1999 struct ggml_tensor * a, // convolution kernel
2000 struct ggml_tensor * b, // data
2001 int s0, // stride dimension 0
2002 int s1, // stride dimension 1
2003 int p0, // padding dimension 0
2004 int p1, // padding dimension 1
2005 int d0, // dilation dimension 0
2006 int d1); // dilation dimension 1
2007
2008 GGML_API struct ggml_tensor * ggml_im2col_3d(
2009 struct ggml_context * ctx,
2010 struct ggml_tensor * a,
2011 struct ggml_tensor * b,
2012 int64_t IC,
2013 int s0, // stride width
2014 int s1, // stride height
2015 int s2, // stride depth
2016 int p0, // padding width
2017 int p1, // padding height
2018 int p2, // padding depth
2019 int d0, // dilation width
2020 int d1, // dilation height
2021 int d2, // dilation depth
2022 enum ggml_type dst_type);
2023
2024 // a: [OC*IC, KD, KH, KW]
2025 // b: [N*IC, ID, IH, IW]
2026 // result: [N*OC, OD, OH, OW]
2027 GGML_API struct ggml_tensor * ggml_conv_3d(
2028 struct ggml_context * ctx,
2029 struct ggml_tensor * a,
2030 struct ggml_tensor * b,
2031 int64_t IC,
2032 int s0, // stride width
2033 int s1, // stride height
2034 int s2, // stride depth
2035 int p0, // padding width
2036 int p1, // padding height
2037 int p2, // padding depth
2038 int d0, // dilation width
2039 int d1, // dilation height
2040 int d2 // dilation depth
2041 );
2042
2043 // kernel size is a->ne[0] x a->ne[1]
2044 // stride is equal to kernel size
2045 // padding is zero
2046 // example:
2047 // a: 16 16 3 768
2048 // b: 1024 1024 3 1
2049 // res: 64 64 768 1
2050 // used in sam
2051 GGML_API struct ggml_tensor * ggml_conv_2d_sk_p0(
2052 struct ggml_context * ctx,
2053 struct ggml_tensor * a,
2054 struct ggml_tensor * b);
2055
2056 // kernel size is a->ne[0] x a->ne[1]
2057 // stride is 1
2058 // padding is half
2059 // example:
2060 // a: 3 3 256 256
2061 // b: 64 64 256 1
2062 // res: 64 64 256 1
2063 // used in sam
2064 GGML_API struct ggml_tensor * ggml_conv_2d_s1_ph(
2065 struct ggml_context * ctx,
2066 struct ggml_tensor * a,
2067 struct ggml_tensor * b);
2068
2069 // depthwise (via im2col and mul_mat)
2070 GGML_API struct ggml_tensor * ggml_conv_2d_dw(
2071 struct ggml_context * ctx,
2072 struct ggml_tensor * a, // convolution kernel
2073 struct ggml_tensor * b, // data
2074 int s0, // stride dimension 0
2075 int s1, // stride dimension 1
2076 int p0, // padding dimension 0
2077 int p1, // padding dimension 1
2078 int d0, // dilation dimension 0
2079 int d1); // dilation dimension 1
2080
2081 // Depthwise 2D convolution
2082 // may be faster than ggml_conv_2d_dw, but not available in all backends
2083 // a: KW KH 1 C convolution kernel
2084 // b: W H C N input data
2085 // res: W_out H_out C N
2086 GGML_API struct ggml_tensor * ggml_conv_2d_dw_direct(
2087 struct ggml_context * ctx,
2088 struct ggml_tensor * a,
2089 struct ggml_tensor * b,
2090 int stride0,
2091 int stride1,
2092 int pad0,
2093 int pad1,
2094 int dilation0,
2095 int dilation1);
2096
2097 GGML_API struct ggml_tensor * ggml_conv_transpose_2d_p0(
2098 struct ggml_context * ctx,
2099 struct ggml_tensor * a,
2100 struct ggml_tensor * b,
2101 int stride);
2102
2103 GGML_API struct ggml_tensor * ggml_conv_2d_direct(
2104 struct ggml_context * ctx,
2105 struct ggml_tensor * a, // convolution kernel [KW, KH, IC, OC]
2106 struct ggml_tensor * b, // input data [W, H, C, N]
2107 int s0, // stride dimension 0
2108 int s1, // stride dimension 1
2109 int p0, // padding dimension 0
2110 int p1, // padding dimension 1
2111 int d0, // dilation dimension 0
2112 int d1); // dilation dimension 1
2113
2114 GGML_API struct ggml_tensor * ggml_conv_3d_direct(
2115 struct ggml_context * ctx,
2116 struct ggml_tensor * a, // kernel [KW, KH, KD, IC * OC]
2117 struct ggml_tensor * b, // input [W, H, D, C * N]
2118 int s0, // stride
2119 int s1,
2120 int s2,
2121 int p0, // padding
2122 int p1,
2123 int p2,
2124 int d0, // dilation
2125 int d1,
2126 int d2,
2127 int n_channels,
2128 int n_batch,
2129 int n_channels_out);
2130
2131 enum ggml_op_pool {
2132 GGML_OP_POOL_MAX,
2133 GGML_OP_POOL_AVG,
2134 GGML_OP_POOL_COUNT,
2135 };
2136
2137 GGML_API struct ggml_tensor * ggml_pool_1d(
2138 struct ggml_context * ctx,
2139 struct ggml_tensor * a,
2140 enum ggml_op_pool op,
2141 int k0, // kernel size
2142 int s0, // stride
2143 int p0); // padding
2144
2145 // the result will have 2*p0 padding for the first dimension
2146 // and 2*p1 padding for the second dimension
2147 GGML_API struct ggml_tensor * ggml_pool_2d(
2148 struct ggml_context * ctx,
2149 struct ggml_tensor * a,
2150 enum ggml_op_pool op,
2151 int k0,
2152 int k1,
2153 int s0,
2154 int s1,
2155 float p0,
2156 float p1);
2157
2158 GGML_API struct ggml_tensor * ggml_pool_2d_back(
2159 struct ggml_context * ctx,
2160 struct ggml_tensor * a,
2161 struct ggml_tensor * af, // "a"/input used in forward pass
2162 enum ggml_op_pool op,
2163 int k0,
2164 int k1,
2165 int s0,
2166 int s1,
2167 float p0,
2168 float p1);
2169
2170 enum ggml_scale_mode {
2171 GGML_SCALE_MODE_NEAREST = 0,
2172 GGML_SCALE_MODE_BILINEAR = 1,
2173 GGML_SCALE_MODE_BICUBIC = 2,
2174
2175 GGML_SCALE_MODE_COUNT
2176 };
2177
2178 enum ggml_scale_flag {
2179 GGML_SCALE_FLAG_ALIGN_CORNERS = (1 << 8),
2180 GGML_SCALE_FLAG_ANTIALIAS = (1 << 9),
2181 };
2182
2183 // interpolate
2184 // multiplies ne0 and ne1 by scale factor
2185 GGML_API struct ggml_tensor * ggml_upscale(
2186 struct ggml_context * ctx,
2187 struct ggml_tensor * a,
2188 int scale_factor,
2189 enum ggml_scale_mode mode);
2190
2191 // interpolate
2192 // interpolate scale to specified dimensions
2193 GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_upscale_ext(
2194 struct ggml_context * ctx,
2195 struct ggml_tensor * a,
2196 int ne0,
2197 int ne1,
2198 int ne2,
2199 int ne3,
2200 enum ggml_scale_mode mode),
2201 "use ggml_interpolate instead");
2202
2203 // Up- or downsamples the input to the specified size.
2204 // 2D scale modes (eg. bilinear) are applied to the first two dimensions.
2205 GGML_API struct ggml_tensor * ggml_interpolate(
2206 struct ggml_context * ctx,
2207 struct ggml_tensor * a,
2208 int64_t ne0,
2209 int64_t ne1,
2210 int64_t ne2,
2211 int64_t ne3,
2212 uint32_t mode); // ggml_scale_mode [ | ggml_scale_flag...]
2213
2214 // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
2215 GGML_API struct ggml_tensor * ggml_pad(
2216 struct ggml_context * ctx,
2217 struct ggml_tensor * a,
2218 int p0,
2219 int p1,
2220 int p2,
2221 int p3);
2222
2223 // pad each dimension with values on the other side of the torus (looping around)
2224 GGML_API struct ggml_tensor * ggml_pad_circular(
2225 struct ggml_context * ctx,
2226 struct ggml_tensor * a,
2227 int p0,
2228 int p1,
2229 int p2,
2230 int p3);
2231
2232 GGML_API struct ggml_tensor * ggml_pad_ext(
2233 struct ggml_context * ctx,
2234 struct ggml_tensor * a,
2235 int lp0,
2236 int rp0,
2237 int lp1,
2238 int rp1,
2239 int lp2,
2240 int rp2,
2241 int lp3,
2242 int rp3
2243 );
2244
2245 // pad each dimension with values on the other side of the torus (looping around)
2246 GGML_API struct ggml_tensor * ggml_pad_ext_circular(
2247 struct ggml_context * ctx,
2248 struct ggml_tensor * a,
2249 int lp0,
2250 int rp0,
2251 int lp1,
2252 int rp1,
2253 int lp2,
2254 int rp2,
2255 int lp3,
2256 int rp3);
2257
2258 // pad each dimension with reflection: [a, b, c, d] -> [b, a, b, c, d, c]
2259 GGML_API struct ggml_tensor * ggml_pad_reflect_1d(
2260 struct ggml_context * ctx,
2261 struct ggml_tensor * a,
2262 int p0,
2263 int p1);
2264
2265 // Move tensor elements by an offset given for each dimension. Elements that
2266 // are shifted beyond the last position are wrapped around to the beginning.
2267 GGML_API struct ggml_tensor * ggml_roll(
2268 struct ggml_context * ctx,
2269 struct ggml_tensor * a,
2270 int shift0,
2271 int shift1,
2272 int shift2,
2273 int shift3);
2274
2275 // Convert matrix into a triangular one (upper, strict upper, lower or strict lower) by writing
2276 // zeroes everywhere outside the masked area
2277 GGML_API struct ggml_tensor * ggml_tri(
2278 struct ggml_context * ctx,
2279 struct ggml_tensor * a,
2280 enum ggml_tri_type type);
2281
2282 // Fill tensor a with constant c
2283 GGML_API struct ggml_tensor * ggml_fill(
2284 struct ggml_context * ctx,
2285 struct ggml_tensor * a,
2286 float c);
2287
2288 GGML_API struct ggml_tensor * ggml_fill_inplace(
2289 struct ggml_context * ctx,
2290 struct ggml_tensor * a,
2291 float c);
2292
2293 // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
2294 // timesteps: [N,]
2295 // return: [N, dim]
2296 GGML_API struct ggml_tensor * ggml_timestep_embedding(
2297 struct ggml_context * ctx,
2298 struct ggml_tensor * timesteps,
2299 int dim,
2300 int max_period);
2301
2302 // sort rows
2303 enum ggml_sort_order {
2304 GGML_SORT_ORDER_ASC,
2305 GGML_SORT_ORDER_DESC,
2306 };
2307
2308 GGML_API struct ggml_tensor * ggml_argsort(
2309 struct ggml_context * ctx,
2310 struct ggml_tensor * a,
2311 enum ggml_sort_order order);
2312
2313 // similar to ggml_top_k but implemented as `argsort` + `view`
2314 GGML_API struct ggml_tensor * ggml_argsort_top_k(
2315 struct ggml_context * ctx,
2316 struct ggml_tensor * a,
2317 int k);
2318
2319 // top k elements per row
2320 // note: the resulting top k indices are in no particular order
2321 GGML_API struct ggml_tensor * ggml_top_k(
2322 struct ggml_context * ctx,
2323 struct ggml_tensor * a,
2324 int k);
2325
2326 GGML_API struct ggml_tensor * ggml_arange(
2327 struct ggml_context * ctx,
2328 float start,
2329 float stop,
2330 float step);
2331
2332 // q: [n_embd_k, n_batch, n_head, ne3 ]
2333 // k: [n_embd_k, n_kv, n_head_kv, ne3 ]
2334 // v: [n_embd_v, n_kv, n_head_kv, ne3 ] !! not transposed !!
2335 // mask: [n_kv, n_batch, ne32, ne33]
2336 // res: [n_embd_v, n_head, n_batch, ne3 ] !! permuted !!
2337 //
2338 // broadcast:
2339 // n_head % n_head_kv == 0
2340 // n_head % ne32 == 0
2341 // ne3 % ne33 == 0
2342 //
2343 GGML_API struct ggml_tensor * ggml_flash_attn_ext(
2344 struct ggml_context * ctx,
2345 struct ggml_tensor * q,
2346 struct ggml_tensor * k,
2347 struct ggml_tensor * v,
2348 struct ggml_tensor * mask,
2349 float scale,
2350 float max_bias,
2351 float logit_softcap);
2352
2353 GGML_API void ggml_flash_attn_ext_set_prec(
2354 struct ggml_tensor * a,
2355 enum ggml_prec prec);
2356
2357 GGML_API enum ggml_prec ggml_flash_attn_ext_get_prec(
2358 const struct ggml_tensor * a);
2359
2360 GGML_API void ggml_flash_attn_ext_add_sinks(
2361 struct ggml_tensor * a,
2362 struct ggml_tensor * sinks);
2363
2364 // TODO: needs to be adapted to ggml_flash_attn_ext
2365 GGML_API struct ggml_tensor * ggml_flash_attn_back(
2366 struct ggml_context * ctx,
2367 struct ggml_tensor * q,
2368 struct ggml_tensor * k,
2369 struct ggml_tensor * v,
2370 struct ggml_tensor * d,
2371 bool masked);
2372
2373 GGML_API struct ggml_tensor * ggml_ssm_conv(
2374 struct ggml_context * ctx,
2375 struct ggml_tensor * sx,
2376 struct ggml_tensor * c);
2377
2378 GGML_API struct ggml_tensor * ggml_ssm_scan(
2379 struct ggml_context * ctx,
2380 struct ggml_tensor * s,
2381 struct ggml_tensor * x,
2382 struct ggml_tensor * dt,
2383 struct ggml_tensor * A,
2384 struct ggml_tensor * B,
2385 struct ggml_tensor * C,
2386 struct ggml_tensor * ids);
2387
2388 // partition into non-overlapping windows with padding if needed
2389 // example:
2390 // a: 768 64 64 1
2391 // w: 14
2392 // res: 768 14 14 25
2393 // used in sam
2394 GGML_API struct ggml_tensor * ggml_win_part(
2395 struct ggml_context * ctx,
2396 struct ggml_tensor * a,
2397 int w);
2398
2399 // reverse of ggml_win_part
2400 // used in sam
2401 GGML_API struct ggml_tensor * ggml_win_unpart(
2402 struct ggml_context * ctx,
2403 struct ggml_tensor * a,
2404 int w0,
2405 int h0,
2406 int w);
2407
2408 GGML_API struct ggml_tensor * ggml_unary(
2409 struct ggml_context * ctx,
2410 struct ggml_tensor * a,
2411 enum ggml_unary_op op);
2412
2413 GGML_API struct ggml_tensor * ggml_unary_inplace(
2414 struct ggml_context * ctx,
2415 struct ggml_tensor * a,
2416 enum ggml_unary_op op);
2417
2418 // used in sam
2419 GGML_API struct ggml_tensor * ggml_get_rel_pos(
2420 struct ggml_context * ctx,
2421 struct ggml_tensor * a,
2422 int qh,
2423 int kh);
2424
2425 // used in sam
2426 GGML_API struct ggml_tensor * ggml_add_rel_pos(
2427 struct ggml_context * ctx,
2428 struct ggml_tensor * a,
2429 struct ggml_tensor * pw,
2430 struct ggml_tensor * ph);
2431
2432 GGML_API struct ggml_tensor * ggml_add_rel_pos_inplace(
2433 struct ggml_context * ctx,
2434 struct ggml_tensor * a,
2435 struct ggml_tensor * pw,
2436 struct ggml_tensor * ph);
2437
2438 GGML_API struct ggml_tensor * ggml_rwkv_wkv6(
2439 struct ggml_context * ctx,
2440 struct ggml_tensor * k,
2441 struct ggml_tensor * v,
2442 struct ggml_tensor * r,
2443 struct ggml_tensor * tf,
2444 struct ggml_tensor * td,
2445 struct ggml_tensor * state);
2446
2447 GGML_API struct ggml_tensor * ggml_gated_linear_attn(
2448 struct ggml_context * ctx,
2449 struct ggml_tensor * k,
2450 struct ggml_tensor * v,
2451 struct ggml_tensor * q,
2452 struct ggml_tensor * g,
2453 struct ggml_tensor * state,
2454 float scale);
2455
2456 GGML_API struct ggml_tensor * ggml_rwkv_wkv7(
2457 struct ggml_context * ctx,
2458 struct ggml_tensor * r,
2459 struct ggml_tensor * w,
2460 struct ggml_tensor * k,
2461 struct ggml_tensor * v,
2462 struct ggml_tensor * a,
2463 struct ggml_tensor * b,
2464 struct ggml_tensor * state);
2465
2466 /* Solves a specific equation of the form Ax=B, where A is a triangular matrix
2467 * without zeroes on the diagonal (i.e. invertible).
2468 * B can have any number of columns, but must have the same number of rows as A
2469 * If A is [n, n] and B is [n, m], then the result will be [n, m] as well
2470 * Has O(n^3) complexity (unlike most matrix ops out there), so use on cases
2471 * where n > 100 sparingly, pre-chunk if necessary.
2472 *
2473 * If left = false, solves xA=B instead
2474 * If lower = false, assumes upper triangular instead
2475 * If uni = true, assumes diagonal of A to be all ones (will override actual values)
2476 *
2477 * TODO: currently only lower, right, non-unitriangular variant is implemented
2478 */
2479 GGML_API struct ggml_tensor * ggml_solve_tri(
2480 struct ggml_context * ctx,
2481 struct ggml_tensor * a,
2482 struct ggml_tensor * b,
2483 bool left,
2484 bool lower,
2485 bool uni);
2486
2487 // custom operators
2488
2489 typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);
2490 typedef void (*ggml_custom2_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, int ith, int nth, void * userdata);
2491 typedef void (*ggml_custom3_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, const struct ggml_tensor * c, int ith, int nth, void * userdata);
2492
2493 #define GGML_N_TASKS_MAX (-1)
2494 // n_tasks == GGML_N_TASKS_MAX means to use max number of tasks
2495
2496 GGML_API struct ggml_tensor * ggml_map_custom1(
2497 struct ggml_context * ctx,
2498 struct ggml_tensor * a,
2499 ggml_custom1_op_t fun,
2500 int n_tasks,
2501 void * userdata);
2502
2503 GGML_API struct ggml_tensor * ggml_map_custom1_inplace(
2504 struct ggml_context * ctx,
2505 struct ggml_tensor * a,
2506 ggml_custom1_op_t fun,
2507 int n_tasks,
2508 void * userdata);
2509
2510 GGML_API struct ggml_tensor * ggml_map_custom2(
2511 struct ggml_context * ctx,
2512 struct ggml_tensor * a,
2513 struct ggml_tensor * b,
2514 ggml_custom2_op_t fun,
2515 int n_tasks,
2516 void * userdata);
2517
2518 GGML_API struct ggml_tensor * ggml_map_custom2_inplace(
2519 struct ggml_context * ctx,
2520 struct ggml_tensor * a,
2521 struct ggml_tensor * b,
2522 ggml_custom2_op_t fun,
2523 int n_tasks,
2524 void * userdata);
2525
2526 GGML_API struct ggml_tensor * ggml_map_custom3(
2527 struct ggml_context * ctx,
2528 struct ggml_tensor * a,
2529 struct ggml_tensor * b,
2530 struct ggml_tensor * c,
2531 ggml_custom3_op_t fun,
2532 int n_tasks,
2533 void * userdata);
2534
2535 GGML_API struct ggml_tensor * ggml_map_custom3_inplace(
2536 struct ggml_context * ctx,
2537 struct ggml_tensor * a,
2538 struct ggml_tensor * b,
2539 struct ggml_tensor * c,
2540 ggml_custom3_op_t fun,
2541 int n_tasks,
2542 void * userdata);
2543
2544 typedef void (*ggml_custom_op_t)(struct ggml_tensor * dst , int ith, int nth, void * userdata);
2545
2546 GGML_API struct ggml_tensor * ggml_custom_4d(
2547 struct ggml_context * ctx,
2548 enum ggml_type type,
2549 int64_t ne0,
2550 int64_t ne1,
2551 int64_t ne2,
2552 int64_t ne3,
2553 struct ggml_tensor ** args,
2554 int n_args,
2555 ggml_custom_op_t fun,
2556 int n_tasks,
2557 void * userdata);
2558
2559 GGML_API struct ggml_tensor * ggml_custom_inplace(
2560 struct ggml_context * ctx,
2561 struct ggml_tensor * a,
2562 struct ggml_tensor ** args,
2563 int n_args,
2564 ggml_custom_op_t fun,
2565 int n_tasks,
2566 void * userdata);
2567
2568 // loss function
2569
2570 GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
2571 struct ggml_context * ctx,
2572 struct ggml_tensor * a, // logits
2573 struct ggml_tensor * b); // labels
2574
2575 GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back(
2576 struct ggml_context * ctx,
2577 struct ggml_tensor * a, // logits
2578 struct ggml_tensor * b, // labels
2579 struct ggml_tensor * c); // gradients of cross_entropy_loss result
2580
2581 // AdamW optimizer step
2582 // Paper: https://arxiv.org/pdf/1711.05101v3.pdf
2583 // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
2584 GGML_API struct ggml_tensor * ggml_opt_step_adamw(
2585 struct ggml_context * ctx,
2586 struct ggml_tensor * a,
2587 struct ggml_tensor * grad,
2588 struct ggml_tensor * m,
2589 struct ggml_tensor * v,
2590 struct ggml_tensor * adamw_params); // parameters such as the learning rate
2591
2592 // stochastic gradient descent step (with weight decay)
2593 GGML_API struct ggml_tensor * ggml_opt_step_sgd(
2594 struct ggml_context * ctx,
2595 struct ggml_tensor * a,
2596 struct ggml_tensor * grad,
2597 struct ggml_tensor * sgd_params); // alpha, weight decay
2598
2599 // build forward mutiple tensors and select one of them for computing
2600 // this is useful for creating graphs that have constant topology but compute different things based on the input
2601 // ref: https://github.com/ggml-org/llama.cpp/pull/18550
2602 //
2603 // nodes:
2604 // | - build forward into the graph but do not compute
2605 // c - build forward into the graph and compute
2606 //
2607 // | | ... c ... |
2608 // | | ... c ... |
2609 // | | ... c ... |
2610 // [0 1 ... idx ... n-1] <-- ggml_build_forward_select(..., n, idx)
2611 // c
2612 // c
2613 //
2614 // example:
2615 // struct ggml_tensor * curs[3];
2616 //
2617 // curs[0] = compute0(...);
2618 // curs[1] = compute1(...);
2619 // curs[2] = compute2(...);
2620 //
2621 // int idx = select_branch(some_input);
2622 //
2623 // struct ggml_tensor * out = ggml_build_forward_select(cgraph, curs, 3, idx);
2624 //
2625 GGML_API struct ggml_tensor * ggml_build_forward_select(
2626 struct ggml_cgraph * cgraph,
2627 struct ggml_tensor ** tensors,
2628 int n_tensors,
2629 int idx);
2630
2631 GGML_API void ggml_build_forward_expand(
2632 struct ggml_cgraph * cgraph,
2633 struct ggml_tensor * tensor);
2634
2635 GGML_API void ggml_build_backward_expand(
2636 struct ggml_context * ctx, // context for gradient computation
2637 struct ggml_cgraph * cgraph,
2638 struct ggml_tensor ** grad_accs);
2639
2640 // graph allocation in a context
2641 GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx); // size = GGML_DEFAULT_GRAPH_SIZE, grads = false
2642 GGML_API struct ggml_cgraph * ggml_new_graph_custom(struct ggml_context * ctx, size_t size, bool grads);
2643 GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, struct ggml_cgraph * cgraph, bool force_grads);
2644 GGML_API void ggml_graph_cpy (struct ggml_cgraph * src, struct ggml_cgraph * dst);
2645 GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
2646 GGML_API void ggml_graph_clear (struct ggml_cgraph * cgraph);
2647
2648 GGML_API int ggml_graph_size (struct ggml_cgraph * cgraph);
2649 GGML_API struct ggml_tensor * ggml_graph_node (struct ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i]
2650 GGML_API struct ggml_tensor ** ggml_graph_nodes (struct ggml_cgraph * cgraph);
2651 GGML_API int ggml_graph_n_nodes(struct ggml_cgraph * cgraph);
2652
2653 GGML_API void ggml_graph_add_node(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
2654
2655 GGML_API size_t ggml_graph_overhead(void);
2656 GGML_API size_t ggml_graph_overhead_custom(size_t size, bool grads);
2657
2658 GGML_API struct ggml_tensor * ggml_graph_get_tensor (const struct ggml_cgraph * cgraph, const char * name);
2659 GGML_API struct ggml_tensor * ggml_graph_get_grad (const struct ggml_cgraph * cgraph, const struct ggml_tensor * node);
2660 GGML_API struct ggml_tensor * ggml_graph_get_grad_acc(const struct ggml_cgraph * cgraph, const struct ggml_tensor * node);
2661
2662 // print info and performance information for the graph
2663 GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
2664
2665 // dump the graph into a file using the dot format
2666 GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * cgraph, const char * filename);
2667
2668 // TODO these functions were sandwiched in the old optimization interface, is there a better place for them?
2669 typedef void (*ggml_log_callback)(enum ggml_log_level level, const char * text, void * user_data);
2670
2671 // Set callback for all future logging events.
2672 // If this is not called, or NULL is supplied, everything is output on stderr.
2673 GGML_API void ggml_log_get(ggml_log_callback * log_callback, void ** user_data);
2674 GGML_API void ggml_log_set(ggml_log_callback log_callback, void * user_data);
2675
2676 GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor);
2677
2678 //
2679 // quantization
2680 //
2681
2682 // - ggml_quantize_init can be called multiple times with the same type
2683 // it will only initialize the quantization tables for the first call or after ggml_quantize_free
2684 // automatically called by ggml_quantize_chunk for convenience
2685 //
2686 // - ggml_quantize_free will free any memory allocated by ggml_quantize_init
2687 // call this at the end of the program to avoid memory leaks
2688 //
2689 // note: these are thread-safe
2690 //
2691 GGML_API void ggml_quantize_init(enum ggml_type type);
2692 GGML_API void ggml_quantize_free(void);
2693
2694 // some quantization type cannot be used without an importance matrix
2695 GGML_API bool ggml_quantize_requires_imatrix(enum ggml_type type);
2696
2697 // calls ggml_quantize_init internally (i.e. can allocate memory)
2698 GGML_API size_t ggml_quantize_chunk(
2699 enum ggml_type type,
2700 const float * src,
2701 void * dst,
2702 int64_t start,
2703 int64_t nrows,
2704 int64_t n_per_row,
2705 const float * imatrix);
2706
2707 #ifdef __cplusplus
2708 // restrict not standard in C++
2709 # if defined(__GNUC__)
2710 # define GGML_RESTRICT __restrict__
2711 # elif defined(__clang__)
2712 # define GGML_RESTRICT __restrict
2713 # elif defined(_MSC_VER)
2714 # define GGML_RESTRICT __restrict
2715 # else
2716 # define GGML_RESTRICT
2717 # endif
2718 #else
2719 # if defined (_MSC_VER) && (__STDC_VERSION__ < 201112L)
2720 # define GGML_RESTRICT __restrict
2721 # else
2722 # define GGML_RESTRICT restrict
2723 # endif
2724 #endif
2725 typedef void (*ggml_to_float_t) (const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);
2726 typedef void (*ggml_from_float_t)(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
2727
2728 struct ggml_type_traits {
2729 const char * type_name;
2730 int64_t blck_size;
2731 int64_t blck_size_interleave; // interleave elements in blocks
2732 size_t type_size;
2733 bool is_quantized;
2734 ggml_to_float_t to_float;
2735 ggml_from_float_t from_float_ref;
2736 };
2737
2738 GGML_API const struct ggml_type_traits * ggml_get_type_traits(enum ggml_type type);
2739
2740 // ggml threadpool
2741 // TODO: currently, only a few functions are in the base ggml API, while the rest are in the CPU backend
2742 // the goal should be to create an API that other backends can use move everything to the ggml base
2743
2744 // scheduling priorities
2745 enum ggml_sched_priority {
2746 GGML_SCHED_PRIO_LOW = -1,
2747 GGML_SCHED_PRIO_NORMAL,
2748 GGML_SCHED_PRIO_MEDIUM,
2749 GGML_SCHED_PRIO_HIGH,
2750 GGML_SCHED_PRIO_REALTIME
2751 };
2752
2753 // threadpool params
2754 // Use ggml_threadpool_params_default() or ggml_threadpool_params_init() to populate the defaults
2755 struct ggml_threadpool_params {
2756 bool cpumask[GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
2757 int n_threads; // number of threads
2758 enum ggml_sched_priority prio; // thread priority
2759 uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
2760 bool strict_cpu; // strict cpu placement
2761 bool paused; // start in paused state
2762 };
2763
2764 struct ggml_threadpool; // forward declaration, see ggml.c
2765
2766 typedef struct ggml_threadpool * ggml_threadpool_t;
2767
2768 GGML_API struct ggml_threadpool_params ggml_threadpool_params_default(int n_threads);
2769 GGML_API void ggml_threadpool_params_init (struct ggml_threadpool_params * p, int n_threads);
2770 GGML_API bool ggml_threadpool_params_match (const struct ggml_threadpool_params * p0, const struct ggml_threadpool_params * p1);
2771
2772 #ifdef __cplusplus
2773 }
2774 #endif