okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-1a (451f3e8a6): own-stack phase 1A and cleanup 5.4; the ggml/MMQ sources the engines build are vendored in third_party/strata-ggml (frozen at the pin, manifest and check), default engine builds need no llama.cpp download, the TQ overlay is plain files, CMakeLists.txt is split into cmake/backends/*; every engine unchanged (CUDA/HIP gfx1201/gfx1151/SYCL device code identical, SYCL 22/22 checksums, Metal 104/105 objects with only __TIME__ differing, CUDA tokens 26/26); release scripts still pass -DSTRATA_GGML_DIR (owner go 11-08)508e6999a

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