ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
| 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__ |
| 260 | template<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 |
| 345 | extern "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 | typedef struct { uint8_t bits; } ggml_fp8_e4m3_t; |
| 385 | |
| 386 | struct ggml_object; |
| 387 | struct ggml_context; |
| 388 | struct ggml_cgraph; |
| 389 | |
| 390 | // NOTE: always add types at the end of the enum to keep backward compatibility |
| 391 | enum ggml_type { |
| 392 | GGML_TYPE_F32 = 0, |
| 393 | GGML_TYPE_F16 = 1, |
| 394 | GGML_TYPE_Q4_0 = 2, |
| 395 | GGML_TYPE_Q4_1 = 3, |
| 396 | // GGML_TYPE_Q4_2 = 4, support has been removed |
| 397 | // GGML_TYPE_Q4_3 = 5, support has been removed |
| 398 | GGML_TYPE_Q5_0 = 6, |
| 399 | GGML_TYPE_Q5_1 = 7, |
| 400 | GGML_TYPE_Q8_0 = 8, |
| 401 | GGML_TYPE_Q8_1 = 9, |
| 402 | GGML_TYPE_Q2_K = 10, |
| 403 | GGML_TYPE_Q3_K = 11, |
| 404 | GGML_TYPE_Q4_K = 12, |
| 405 | GGML_TYPE_Q5_K = 13, |
| 406 | GGML_TYPE_Q6_K = 14, |
| 407 | GGML_TYPE_Q8_K = 15, |
| 408 | GGML_TYPE_IQ2_XXS = 16, |
| 409 | GGML_TYPE_IQ2_XS = 17, |
| 410 | GGML_TYPE_IQ3_XXS = 18, |
| 411 | GGML_TYPE_IQ1_S = 19, |
| 412 | GGML_TYPE_IQ4_NL = 20, |
| 413 | GGML_TYPE_IQ3_S = 21, |
| 414 | GGML_TYPE_IQ2_S = 22, |
| 415 | GGML_TYPE_IQ4_XS = 23, |
| 416 | GGML_TYPE_I8 = 24, |
| 417 | GGML_TYPE_I16 = 25, |
| 418 | GGML_TYPE_I32 = 26, |
| 419 | GGML_TYPE_I64 = 27, |
| 420 | GGML_TYPE_F64 = 28, |
| 421 | GGML_TYPE_IQ1_M = 29, |
| 422 | GGML_TYPE_BF16 = 30, |
| 423 | // GGML_TYPE_Q4_0_4_4 = 31, support has been removed from gguf files |
| 424 | // GGML_TYPE_Q4_0_4_8 = 32, |
| 425 | // GGML_TYPE_Q4_0_8_8 = 33, |
| 426 | GGML_TYPE_TQ1_0 = 34, |
| 427 | GGML_TYPE_TQ2_0 = 35, |
| 428 | // GGML_TYPE_IQ4_NL_4_4 = 36, |
| 429 | // GGML_TYPE_IQ4_NL_4_8 = 37, |
| 430 | // GGML_TYPE_IQ4_NL_8_8 = 38, |
| 431 | GGML_TYPE_MXFP4 = 39, // MXFP4 (1 block) |
| 432 | GGML_TYPE_NVFP4 = 40, // NVFP4 (4 blocks, E4M3 scale) |
| 433 | GGML_TYPE_Q1_0 = 41, |
| 434 | GGML_TYPE_Q2_0 = 42, |
| 435 | GGML_TYPE_F8_E4M3 = 43, |
| 436 | GGML_TYPE_COUNT = 44, |
| 437 | }; |
| 438 | |
| 439 | // [TAG_GGML_PREC] |
| 440 | // this enum is used to declare the allowed numerical precision/data-types types that can be used during the compute of an op |
| 441 | // the declared types can be: |
| 442 | // - result accumulation type |
| 443 | // - source tensor data representation type |
| 444 | // - etc. |
| 445 | // the precision parameters are stored as ggml_tensor.op_params to the respective ops |
| 446 | enum ggml_prec { |
| 447 | GGML_PREC_UNDEFINED = 0, |
| 448 | GGML_PREC_DEFAULT = 0, // note: deprecated, use GGML_PREC_UNDEFINED |
| 449 | GGML_PREC_F32 = 10, |
| 450 | GGML_PREC_BF16 = 15, |
| 451 | GGML_PREC_F16 = 20, |
| 452 | GGML_PREC_Q8 = 30, |
| 453 | GGML_PREC_Q4 = 40, |
| 454 | }; |
| 455 | |
| 456 | // op hint |
| 457 | enum ggml_op_hint { |
| 458 | GGML_HINT_NONE = 0, |
| 459 | GGML_HINT_SRC0_IS_HADAMARD = 1, |
| 460 | }; |
| 461 | |
| 462 | // model file types |
| 463 | enum ggml_ftype { |
| 464 | GGML_FTYPE_UNKNOWN = -1, |
| 465 | GGML_FTYPE_ALL_F32 = 0, |
| 466 | GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors |
| 467 | GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors |
| 468 | GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors |
| 469 | GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16 |
| 470 | GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors |
| 471 | GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors |
| 472 | GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors |
| 473 | GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors |
| 474 | GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors |
| 475 | GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors |
| 476 | GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors |
| 477 | GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors |
| 478 | GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors |
| 479 | GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors |
| 480 | GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors |
| 481 | GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors |
| 482 | GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors |
| 483 | GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors |
| 484 | GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors |
| 485 | GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors |
| 486 | GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors |
| 487 | GGML_FTYPE_MOSTLY_BF16 = 24, // except 1d tensors |
| 488 | GGML_FTYPE_MOSTLY_MXFP4 = 25, // except 1d tensors |
| 489 | GGML_FTYPE_MOSTLY_NVFP4 = 26, // except 1d tensors |
| 490 | GGML_FTYPE_MOSTLY_Q1_0 = 27, // except 1d tensors |
| 491 | GGML_FTYPE_MOSTLY_Q2_0 = 28, // except 1d tensors |
| 492 | GGML_FTYPE_MOSTLY_F8_E4M3 = 29, // except 1d tensors |
| 493 | }; |
| 494 | |
| 495 | // available tensor operations: |
| 496 | enum ggml_op { |
| 497 | GGML_OP_NONE = 0, |
| 498 | |
| 499 | GGML_OP_DUP, |
| 500 | GGML_OP_ADD, |
| 501 | GGML_OP_ADD_ID, |
| 502 | GGML_OP_ADD1, |
| 503 | GGML_OP_ACC, |
| 504 | GGML_OP_SUB, |
| 505 | GGML_OP_MUL, |
| 506 | GGML_OP_DIV, |
| 507 | GGML_OP_SQR, |
| 508 | GGML_OP_SQRT, |
| 509 | GGML_OP_LOG, |
| 510 | GGML_OP_SIN, |
| 511 | GGML_OP_COS, |
| 512 | GGML_OP_SUM, |
| 513 | GGML_OP_SUM_ROWS, |
| 514 | GGML_OP_CUMSUM, |
| 515 | GGML_OP_MEAN, |
| 516 | GGML_OP_ARGMAX, |
| 517 | GGML_OP_COUNT_EQUAL, |
| 518 | GGML_OP_REPEAT, |
| 519 | GGML_OP_REPEAT_BACK, |
| 520 | GGML_OP_CONCAT, |
| 521 | GGML_OP_SILU_BACK, |
| 522 | GGML_OP_NORM, // normalize |
| 523 | GGML_OP_RMS_NORM, |
| 524 | GGML_OP_RMS_NORM_BACK, |
| 525 | GGML_OP_GROUP_NORM, |
| 526 | GGML_OP_L2_NORM, |
| 527 | |
| 528 | GGML_OP_MUL_MAT, |
| 529 | GGML_OP_MUL_MAT_ID, |
| 530 | GGML_OP_OUT_PROD, |
| 531 | |
| 532 | GGML_OP_SCALE, |
| 533 | GGML_OP_SET, |
| 534 | GGML_OP_CPY, |
| 535 | GGML_OP_CONT, |
| 536 | GGML_OP_RESHAPE, |
| 537 | GGML_OP_VIEW, |
| 538 | GGML_OP_PERMUTE, |
| 539 | GGML_OP_TRANSPOSE, |
| 540 | GGML_OP_GET_ROWS, |
| 541 | GGML_OP_GET_ROWS_BACK, |
| 542 | GGML_OP_SET_ROWS, |
| 543 | GGML_OP_DIAG, |
| 544 | GGML_OP_DIAG_MASK_INF, |
| 545 | GGML_OP_DIAG_MASK_ZERO, |
| 546 | GGML_OP_SOFT_MAX, |
| 547 | GGML_OP_SOFT_MAX_BACK, |
| 548 | GGML_OP_ROPE, |
| 549 | GGML_OP_ROPE_BACK, |
| 550 | GGML_OP_CLAMP, |
| 551 | GGML_OP_CONV_TRANSPOSE_1D, |
| 552 | GGML_OP_IM2COL, |
| 553 | GGML_OP_IM2COL_BACK, |
| 554 | GGML_OP_IM2COL_3D, |
| 555 | GGML_OP_COL2IM_1D, |
| 556 | GGML_OP_CONV_2D, |
| 557 | GGML_OP_CONV_3D, |
| 558 | GGML_OP_CONV_2D_DW, |
| 559 | GGML_OP_CONV_TRANSPOSE_2D, |
| 560 | GGML_OP_POOL_1D, |
| 561 | GGML_OP_POOL_2D, |
| 562 | GGML_OP_POOL_2D_BACK, |
| 563 | GGML_OP_UPSCALE, |
| 564 | GGML_OP_PAD, |
| 565 | GGML_OP_PAD_REFLECT_1D, |
| 566 | GGML_OP_ROLL, |
| 567 | GGML_OP_ARANGE, |
| 568 | GGML_OP_TIMESTEP_EMBEDDING, |
| 569 | GGML_OP_ARGSORT, |
| 570 | GGML_OP_TOP_K, |
| 571 | GGML_OP_LEAKY_RELU, |
| 572 | GGML_OP_TRI, |
| 573 | GGML_OP_FILL, |
| 574 | |
| 575 | GGML_OP_FLASH_ATTN_EXT, |
| 576 | GGML_OP_FLASH_ATTN_BACK, |
| 577 | GGML_OP_SSM_CONV, |
| 578 | GGML_OP_SSM_SCAN, |
| 579 | GGML_OP_WIN_PART, |
| 580 | GGML_OP_WIN_UNPART, |
| 581 | GGML_OP_GET_REL_POS, |
| 582 | GGML_OP_ADD_REL_POS, |
| 583 | GGML_OP_RWKV_WKV6, |
| 584 | GGML_OP_GATED_LINEAR_ATTN, |
| 585 | GGML_OP_RWKV_WKV7, |
| 586 | GGML_OP_SOLVE_TRI, |
| 587 | GGML_OP_GATED_DELTA_NET, |
| 588 | GGML_OP_LIGHTNING_INDEXER, |
| 589 | GGML_OP_DSV4_HC_COMB, |
| 590 | GGML_OP_DSV4_HC_PRE, |
| 591 | GGML_OP_DSV4_HC_POST, |
| 592 | GGML_OP_TOP_K_MASK, |
| 593 | |
| 594 | GGML_OP_UNARY, |
| 595 | |
| 596 | GGML_OP_MAP_CUSTOM1, |
| 597 | GGML_OP_MAP_CUSTOM2, |
| 598 | GGML_OP_MAP_CUSTOM3, |
| 599 | |
| 600 | GGML_OP_CUSTOM, |
| 601 | |
| 602 | GGML_OP_CROSS_ENTROPY_LOSS, |
| 603 | GGML_OP_CROSS_ENTROPY_LOSS_BACK, |
| 604 | GGML_OP_OPT_STEP_ADAMW, |
| 605 | GGML_OP_OPT_STEP_SGD, |
| 606 | |
| 607 | GGML_OP_GLU, |
| 608 | |
| 609 | GGML_OP_COUNT, |
| 610 | }; |
| 611 | |
| 612 | enum ggml_unary_op { |
| 613 | GGML_UNARY_OP_ABS, |
| 614 | GGML_UNARY_OP_SGN, |
| 615 | GGML_UNARY_OP_NEG, |
| 616 | GGML_UNARY_OP_STEP, |
| 617 | GGML_UNARY_OP_TANH, |
| 618 | GGML_UNARY_OP_ELU, |
| 619 | GGML_UNARY_OP_RELU, |
| 620 | GGML_UNARY_OP_SIGMOID, |
| 621 | GGML_UNARY_OP_GELU, |
| 622 | GGML_UNARY_OP_GELU_QUICK, |
| 623 | GGML_UNARY_OP_SILU, |
| 624 | GGML_UNARY_OP_HARDSWISH, |
| 625 | GGML_UNARY_OP_HARDSIGMOID, |
| 626 | GGML_UNARY_OP_EXP, |
| 627 | GGML_UNARY_OP_EXPM1, |
| 628 | GGML_UNARY_OP_SOFTPLUS, |
| 629 | GGML_UNARY_OP_GELU_ERF, |
| 630 | GGML_UNARY_OP_XIELU, |
| 631 | GGML_UNARY_OP_FLOOR, |
| 632 | GGML_UNARY_OP_CEIL, |
| 633 | GGML_UNARY_OP_ROUND, |
| 634 | GGML_UNARY_OP_TRUNC, |
| 635 | |
| 636 | GGML_UNARY_OP_COUNT, |
| 637 | }; |
| 638 | |
| 639 | enum ggml_glu_op { |
| 640 | GGML_GLU_OP_REGLU, |
| 641 | GGML_GLU_OP_GEGLU, |
| 642 | GGML_GLU_OP_SWIGLU, |
| 643 | GGML_GLU_OP_SWIGLU_OAI, |
| 644 | GGML_GLU_OP_GEGLU_ERF, |
| 645 | GGML_GLU_OP_GEGLU_QUICK, |
| 646 | GGML_GLU_OP_SWIGLU_CLAMP, |
| 647 | |
| 648 | GGML_GLU_OP_COUNT, |
| 649 | }; |
| 650 | |
| 651 | enum ggml_object_type { |
| 652 | GGML_OBJECT_TYPE_TENSOR, |
| 653 | GGML_OBJECT_TYPE_GRAPH, |
| 654 | GGML_OBJECT_TYPE_WORK_BUFFER |
| 655 | }; |
| 656 | |
| 657 | enum ggml_log_level { |
| 658 | GGML_LOG_LEVEL_NONE = 0, |
| 659 | GGML_LOG_LEVEL_DEBUG = 1, |
| 660 | GGML_LOG_LEVEL_INFO = 2, |
| 661 | GGML_LOG_LEVEL_WARN = 3, |
| 662 | GGML_LOG_LEVEL_ERROR = 4, |
| 663 | GGML_LOG_LEVEL_CONT = 5, // continue previous log |
| 664 | }; |
| 665 | |
| 666 | // this tensor... |
| 667 | enum ggml_tensor_flag { |
| 668 | GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph |
| 669 | GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph |
| 670 | GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters |
| 671 | GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up) |
| 672 | GGML_TENSOR_FLAG_COMPUTE = 16, // ...must be computed |
| 673 | }; |
| 674 | |
| 675 | enum ggml_tri_type { |
| 676 | GGML_TRI_TYPE_UPPER_DIAG = 0, |
| 677 | GGML_TRI_TYPE_UPPER = 1, |
| 678 | GGML_TRI_TYPE_LOWER_DIAG = 2, |
| 679 | GGML_TRI_TYPE_LOWER = 3 |
| 680 | }; |
| 681 | |
| 682 | struct ggml_init_params { |
| 683 | // memory pool |
| 684 | size_t mem_size; // bytes |
| 685 | void * mem_buffer; // if NULL, memory will be allocated internally |
| 686 | bool no_alloc; // don't allocate memory for the tensor data |
| 687 | }; |
| 688 | |
| 689 | // n-dimensional tensor |
| 690 | struct ggml_tensor { |
| 691 | enum ggml_type type; |
| 692 | |
| 693 | struct ggml_backend_buffer * buffer; |
| 694 | |
| 695 | int64_t ne[GGML_MAX_DIMS]; // number of elements |
| 696 | size_t nb[GGML_MAX_DIMS]; // stride in bytes: |
| 697 | // nb[0] = ggml_type_size(type) |
| 698 | // nb[1] = nb[0] * (ne[0] / ggml_blck_size(type)) + padding |
| 699 | // nb[i] = nb[i-1] * ne[i-1] |
| 700 | |
| 701 | // compute data |
| 702 | enum ggml_op op; |
| 703 | |
| 704 | // op params - allocated as int32_t for alignment |
| 705 | int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)]; |
| 706 | |
| 707 | int32_t flags; |
| 708 | |
| 709 | struct ggml_tensor * src[GGML_MAX_SRC]; |
| 710 | |
| 711 | // source tensor and offset for views |
| 712 | struct ggml_tensor * view_src; |
| 713 | size_t view_offs; |
| 714 | |
| 715 | void * data; |
| 716 | |
| 717 | char name[GGML_MAX_NAME]; |
| 718 | |
| 719 | void * extra; // extra things e.g. for ggml-cuda.cu |
| 720 | |
| 721 | char padding[8]; |
| 722 | }; |
| 723 | |
| 724 | static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor); |
| 725 | |
| 726 | // Abort callback |
| 727 | // If not NULL, called before ggml computation |
| 728 | // If it returns true, the computation is aborted |
| 729 | typedef bool (*ggml_abort_callback)(void * data); |
| 730 | |
| 731 | |
| 732 | // |
| 733 | // GUID |
| 734 | // |
| 735 | |
| 736 | // GUID types |
| 737 | typedef uint8_t ggml_guid[16]; |
| 738 | typedef ggml_guid * ggml_guid_t; |
| 739 | |
| 740 | GGML_API bool ggml_guid_matches(ggml_guid_t guid_a, ggml_guid_t guid_b); |
| 741 | |
| 742 | // misc |
| 743 | |
| 744 | GGML_API const char * ggml_version(void); |
| 745 | GGML_API const char * ggml_commit(void); |
| 746 | |
| 747 | GGML_API void ggml_time_init(void); // call this once at the beginning of the program |
| 748 | GGML_API int64_t ggml_time_ms(void); |
| 749 | GGML_API int64_t ggml_time_us(void); |
| 750 | GGML_API int64_t ggml_cycles(void); |
| 751 | GGML_API int64_t ggml_cycles_per_ms(void); |
| 752 | |
| 753 | // accepts a UTF-8 path, even on Windows |
| 754 | GGML_API FILE * ggml_fopen(const char * fname, const char * mode); |
| 755 | |
| 756 | GGML_API void ggml_print_object (const struct ggml_object * obj); |
| 757 | GGML_API void ggml_print_objects(const struct ggml_context * ctx); |
| 758 | |
| 759 | GGML_API int64_t ggml_nelements (const struct ggml_tensor * tensor); |
| 760 | GGML_API int64_t ggml_nrows (const struct ggml_tensor * tensor); |
| 761 | GGML_API size_t ggml_nbytes (const struct ggml_tensor * tensor); |
| 762 | GGML_API size_t ggml_nbytes_pad(const struct ggml_tensor * tensor); // same as ggml_nbytes() but padded to GGML_MEM_ALIGN |
| 763 | |
| 764 | GGML_API int64_t ggml_blck_size(enum ggml_type type); |
| 765 | GGML_API size_t ggml_type_size(enum ggml_type type); // size in bytes for all elements in a block |
| 766 | GGML_API size_t ggml_row_size (enum ggml_type type, int64_t ne); // size in bytes for all elements in a row |
| 767 | |
| 768 | GGML_DEPRECATED( |
| 769 | GGML_API double ggml_type_sizef(enum ggml_type type), // ggml_type_size()/ggml_blck_size() as float |
| 770 | "use ggml_row_size() instead"); |
| 771 | |
| 772 | GGML_API const char * ggml_type_name(enum ggml_type type); |
| 773 | GGML_API const char * ggml_op_name (enum ggml_op op); |
| 774 | GGML_API const char * ggml_op_symbol(enum ggml_op op); |
| 775 | |
| 776 | GGML_API const char * ggml_unary_op_name(enum ggml_unary_op op); |
| 777 | GGML_API const char * ggml_glu_op_name(enum ggml_glu_op op); |
| 778 | GGML_API const char * ggml_op_desc(const struct ggml_tensor * t); // unary or op name |
| 779 | |
| 780 | GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor); |
| 781 | |
| 782 | GGML_API bool ggml_is_quantized(enum ggml_type type); |
| 783 | |
| 784 | // TODO: temporary until model loading of ggml examples is refactored |
| 785 | GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype); |
| 786 | |
| 787 | GGML_API bool ggml_is_transposed(const struct ggml_tensor * tensor); |
| 788 | GGML_API bool ggml_is_permuted (const struct ggml_tensor * tensor); |
| 789 | GGML_API bool ggml_is_empty (const struct ggml_tensor * tensor); |
| 790 | GGML_API bool ggml_is_view (const struct ggml_tensor * tensor); |
| 791 | GGML_API bool ggml_is_scalar (const struct ggml_tensor * tensor); |
| 792 | GGML_API bool ggml_is_vector (const struct ggml_tensor * tensor); |
| 793 | GGML_API bool ggml_is_matrix (const struct ggml_tensor * tensor); |
| 794 | GGML_API bool ggml_is_3d (const struct ggml_tensor * tensor); |
| 795 | GGML_API int ggml_n_dims (const struct ggml_tensor * tensor); // returns 1 for scalars |
| 796 | |
| 797 | // returns whether the tensor elements can be iterated over with a flattened index (no gaps, no permutation) |
| 798 | GGML_API bool ggml_is_contiguous (const struct ggml_tensor * tensor); |
| 799 | GGML_API bool ggml_is_contiguous_0(const struct ggml_tensor * tensor); // same as ggml_is_contiguous() |
| 800 | GGML_API bool ggml_is_contiguous_1(const struct ggml_tensor * tensor); // contiguous for dims >= 1 |
| 801 | GGML_API bool ggml_is_contiguous_2(const struct ggml_tensor * tensor); // contiguous for dims >= 2 |
| 802 | |
| 803 | GGML_API bool ggml_is_contiguous_to_1(const struct ggml_tensor * tensor); // contiguous for dims < 1 |
| 804 | GGML_API bool ggml_is_contiguous_to_2(const struct ggml_tensor * tensor); // contiguous for dims < 2 |
| 805 | GGML_API bool ggml_is_contiguous_to_3(const struct ggml_tensor * tensor); // contiguous for dims < 3 |
| 806 | |
| 807 | // returns whether the tensor elements are allocated as one contiguous block of memory (no gaps, but permutation ok) |
| 808 | GGML_API bool ggml_is_contiguously_allocated(const struct ggml_tensor * tensor); |
| 809 | |
| 810 | // true for tensor that is stored in memory as CxWxHxN and has been permuted to WxHxCxN |
| 811 | GGML_API bool ggml_is_contiguous_channels(const struct ggml_tensor * tensor); |
| 812 | |
| 813 | // true if the elements in dimension 0 are contiguous, or there is just 1 block of elements |
| 814 | GGML_API bool ggml_is_contiguous_rows(const struct ggml_tensor * tensor); |
| 815 | |
| 816 | GGML_API bool ggml_are_same_shape (const struct ggml_tensor * t0, const struct ggml_tensor * t1); |
| 817 | GGML_API bool ggml_are_same_stride(const struct ggml_tensor * t0, const struct ggml_tensor * t1); |
| 818 | |
| 819 | GGML_API bool ggml_can_repeat(const struct ggml_tensor * t0, const struct ggml_tensor * t1); |
| 820 | |
| 821 | // use this to compute the memory overhead of a tensor |
| 822 | GGML_API size_t ggml_tensor_overhead(void); |
| 823 | |
| 824 | GGML_API bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbytes); |
| 825 | |
| 826 | // main |
| 827 | |
| 828 | GGML_API struct ggml_context * ggml_init (struct ggml_init_params params); |
| 829 | GGML_API void ggml_reset(struct ggml_context * ctx); |
| 830 | GGML_API void ggml_free (struct ggml_context * ctx); |
| 831 | |
| 832 | GGML_API size_t ggml_used_mem(const struct ggml_context * ctx); |
| 833 | |
| 834 | GGML_API bool ggml_get_no_alloc(struct ggml_context * ctx); |
| 835 | GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc); |
| 836 | |
| 837 | GGML_API void * ggml_get_mem_buffer (const struct ggml_context * ctx); |
| 838 | GGML_API size_t ggml_get_mem_size (const struct ggml_context * ctx); |
| 839 | GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx); |
| 840 | |
| 841 | GGML_API struct ggml_tensor * ggml_new_tensor( |
| 842 | struct ggml_context * ctx, |
| 843 | enum ggml_type type, |
| 844 | int n_dims, |
| 845 | const int64_t *ne); |
| 846 | |
| 847 | GGML_API struct ggml_tensor * ggml_new_tensor_1d( |
| 848 | struct ggml_context * ctx, |
| 849 | enum ggml_type type, |
| 850 | int64_t ne0); |
| 851 | |
| 852 | GGML_API struct ggml_tensor * ggml_new_tensor_2d( |
| 853 | struct ggml_context * ctx, |
| 854 | enum ggml_type type, |
| 855 | int64_t ne0, |
| 856 | int64_t ne1); |
| 857 | |
| 858 | GGML_API struct ggml_tensor * ggml_new_tensor_3d( |
| 859 | struct ggml_context * ctx, |
| 860 | enum ggml_type type, |
| 861 | int64_t ne0, |
| 862 | int64_t ne1, |
| 863 | int64_t ne2); |
| 864 | |
| 865 | GGML_API struct ggml_tensor * ggml_new_tensor_4d( |
| 866 | struct ggml_context * ctx, |
| 867 | enum ggml_type type, |
| 868 | int64_t ne0, |
| 869 | int64_t ne1, |
| 870 | int64_t ne2, |
| 871 | int64_t ne3); |
| 872 | |
| 873 | GGML_API void * ggml_new_buffer(struct ggml_context * ctx, size_t nbytes); |
| 874 | |
| 875 | GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src); |
| 876 | GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, struct ggml_tensor * src); |
| 877 | |
| 878 | // Context tensor enumeration and lookup |
| 879 | GGML_API struct ggml_tensor * ggml_get_first_tensor(const struct ggml_context * ctx); |
| 880 | GGML_API struct ggml_tensor * ggml_get_next_tensor (const struct ggml_context * ctx, struct ggml_tensor * tensor); |
| 881 | GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name); |
| 882 | |
| 883 | // Converts a flat index into coordinates |
| 884 | 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); |
| 885 | |
| 886 | GGML_API enum ggml_unary_op ggml_get_unary_op(const struct ggml_tensor * tensor); |
| 887 | GGML_API enum ggml_glu_op ggml_get_glu_op(const struct ggml_tensor * tensor); |
| 888 | |
| 889 | GGML_API void * ggml_get_data (const struct ggml_tensor * tensor); |
| 890 | GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor); |
| 891 | |
| 892 | GGML_API const char * ggml_get_name (const struct ggml_tensor * tensor); |
| 893 | GGML_API struct ggml_tensor * ggml_set_name ( struct ggml_tensor * tensor, const char * name); |
| 894 | GGML_ATTRIBUTE_FORMAT(2, 3) |
| 895 | GGML_API struct ggml_tensor * ggml_format_name( struct ggml_tensor * tensor, const char * fmt, ...); |
| 896 | |
| 897 | // Tensor flags |
| 898 | GGML_API void ggml_set_input(struct ggml_tensor * tensor); |
| 899 | GGML_API void ggml_set_output(struct ggml_tensor * tensor); |
| 900 | GGML_API void ggml_set_param(struct ggml_tensor * tensor); |
| 901 | GGML_API void ggml_set_loss(struct ggml_tensor * tensor); |
| 902 | |
| 903 | // |
| 904 | // operations on tensors with backpropagation |
| 905 | // |
| 906 | |
| 907 | GGML_API struct ggml_tensor * ggml_dup( |
| 908 | struct ggml_context * ctx, |
| 909 | struct ggml_tensor * a); |
| 910 | |
| 911 | // in-place, returns view(a) |
| 912 | GGML_API struct ggml_tensor * ggml_dup_inplace( |
| 913 | struct ggml_context * ctx, |
| 914 | struct ggml_tensor * a); |
| 915 | |
| 916 | GGML_API struct ggml_tensor * ggml_add( |
| 917 | struct ggml_context * ctx, |
| 918 | struct ggml_tensor * a, |
| 919 | struct ggml_tensor * b); |
| 920 | |
| 921 | GGML_API struct ggml_tensor * ggml_add_inplace( |
| 922 | struct ggml_context * ctx, |
| 923 | struct ggml_tensor * a, |
| 924 | struct ggml_tensor * b); |
| 925 | |
| 926 | GGML_API struct ggml_tensor * ggml_add_cast( |
| 927 | struct ggml_context * ctx, |
| 928 | struct ggml_tensor * a, |
| 929 | struct ggml_tensor * b, |
| 930 | enum ggml_type type); |
| 931 | |
| 932 | // dst[i0, i1, i2] = a[i0, i1, i2] + b[i0, ids[i1, i2]] |
| 933 | // an id of -1 adds nothing: dst[i0, i1, i2] = a[i0, 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 | // [TAG_GGML_PREC] |
| 1486 | // get the accumulator / src[idx] precision set by the functions above |
| 1487 | // return GGML_PREC_UNDEFINED when the op does not carry the requested precision |
| 1488 | GGML_API enum ggml_prec ggml_prec_get_acc( |
| 1489 | const struct ggml_tensor * a); |
| 1490 | |
| 1491 | GGML_API enum ggml_prec ggml_prec_get_src( |
| 1492 | const struct ggml_tensor * a, |
| 1493 | int idx); |
| 1494 | |
| 1495 | // A: k columns, n rows => [ne03, ne02, n, k] |
| 1496 | // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k] |
| 1497 | // result is n columns, m rows => [ne03 * x, ne02 * y, m, n] |
| 1498 | GGML_API struct ggml_tensor * ggml_mul_mat( |
| 1499 | struct ggml_context * ctx, |
| 1500 | struct ggml_tensor * a, |
| 1501 | struct ggml_tensor * b); |
| 1502 | |
| 1503 | // change the precision of a matrix multiplication |
| 1504 | // set to GGML_PREC_F32 for higher precision (useful for phi-2) |
| 1505 | GGML_DEPRECATED(GGML_API void ggml_mul_mat_set_prec( |
| 1506 | struct ggml_tensor * a, |
| 1507 | enum ggml_prec prec), |
| 1508 | "use ggml_prec_set_acc() instead"); |
| 1509 | |
| 1510 | // change the hint of a matrix multiplication |
| 1511 | GGML_API void ggml_mul_mat_set_hint( |
| 1512 | struct ggml_tensor * a, |
| 1513 | enum ggml_op_hint hint); |
| 1514 | |
| 1515 | // indirect matrix multiplication |
| 1516 | // an id of -1 skips the slot and zeroes the matching row of the result |
| 1517 | // |
| 1518 | // [TAG_MUL_MAT_ID_DUPLICATE_ID] |
| 1519 | // note: this op has undefined behaviour if a given `a` is referenced more than once for a given `b` |
| 1520 | // (i.e. duplicate experts per token are not guaranteed to be supported in all backends) |
| 1521 | // TODO: add backend support: https://github.com/ggml-org/llama.cpp/pull/29998#issuecomment-6153742533 |
| 1522 | GGML_API struct ggml_tensor * ggml_mul_mat_id( |
| 1523 | struct ggml_context * ctx, |
| 1524 | struct ggml_tensor * as, |
| 1525 | struct ggml_tensor * b, |
| 1526 | struct ggml_tensor * ids); |
| 1527 | |
| 1528 | // A: m columns, n rows, |
| 1529 | // B: p columns, n rows, |
| 1530 | // result is m columns, p rows |
| 1531 | GGML_API struct ggml_tensor * ggml_out_prod( |
| 1532 | struct ggml_context * ctx, |
| 1533 | struct ggml_tensor * a, |
| 1534 | struct ggml_tensor * b); |
| 1535 | |
| 1536 | // |
| 1537 | // operations on tensors without backpropagation |
| 1538 | // |
| 1539 | |
| 1540 | GGML_API struct ggml_tensor * ggml_scale( |
| 1541 | struct ggml_context * ctx, |
| 1542 | struct ggml_tensor * a, |
| 1543 | float s); |
| 1544 | |
| 1545 | // in-place, returns view(a) |
| 1546 | GGML_API struct ggml_tensor * ggml_scale_inplace( |
| 1547 | struct ggml_context * ctx, |
| 1548 | struct ggml_tensor * a, |
| 1549 | float s); |
| 1550 | |
| 1551 | // x = s * a + b |
| 1552 | GGML_API struct ggml_tensor * ggml_scale_bias( |
| 1553 | struct ggml_context * ctx, |
| 1554 | struct ggml_tensor * a, |
| 1555 | float s, |
| 1556 | float b); |
| 1557 | |
| 1558 | GGML_API struct ggml_tensor * ggml_scale_bias_inplace( |
| 1559 | struct ggml_context * ctx, |
| 1560 | struct ggml_tensor * a, |
| 1561 | float s, |
| 1562 | float b); |
| 1563 | |
| 1564 | // b -> view(a,offset,nb1,nb2,3), return modified a |
| 1565 | GGML_API struct ggml_tensor * ggml_set( |
| 1566 | struct ggml_context * ctx, |
| 1567 | struct ggml_tensor * a, |
| 1568 | struct ggml_tensor * b, |
| 1569 | size_t nb1, |
| 1570 | size_t nb2, |
| 1571 | size_t nb3, |
| 1572 | size_t offset); // in bytes |
| 1573 | |
| 1574 | // b -> view(a,offset,nb1,nb2,3), return view(a) |
| 1575 | GGML_API struct ggml_tensor * ggml_set_inplace( |
| 1576 | struct ggml_context * ctx, |
| 1577 | struct ggml_tensor * a, |
| 1578 | struct ggml_tensor * b, |
| 1579 | size_t nb1, |
| 1580 | size_t nb2, |
| 1581 | size_t nb3, |
| 1582 | size_t offset); // in bytes |
| 1583 | |
| 1584 | GGML_API struct ggml_tensor * ggml_set_1d( |
| 1585 | struct ggml_context * ctx, |
| 1586 | struct ggml_tensor * a, |
| 1587 | struct ggml_tensor * b, |
| 1588 | size_t offset); // in bytes |
| 1589 | |
| 1590 | GGML_API struct ggml_tensor * ggml_set_1d_inplace( |
| 1591 | struct ggml_context * ctx, |
| 1592 | struct ggml_tensor * a, |
| 1593 | struct ggml_tensor * b, |
| 1594 | size_t offset); // in bytes |
| 1595 | |
| 1596 | // b -> view(a,offset,nb1,nb2,3), return modified a |
| 1597 | GGML_API struct ggml_tensor * ggml_set_2d( |
| 1598 | struct ggml_context * ctx, |
| 1599 | struct ggml_tensor * a, |
| 1600 | struct ggml_tensor * b, |
| 1601 | size_t nb1, |
| 1602 | size_t offset); // in bytes |
| 1603 | |
| 1604 | // b -> view(a,offset,nb1,nb2,3), return view(a) |
| 1605 | GGML_API struct ggml_tensor * ggml_set_2d_inplace( |
| 1606 | struct ggml_context * ctx, |
| 1607 | struct ggml_tensor * a, |
| 1608 | struct ggml_tensor * b, |
| 1609 | size_t nb1, |
| 1610 | size_t offset); // in bytes |
| 1611 | |
| 1612 | // a -> b, return view(b) |
| 1613 | GGML_API struct ggml_tensor * ggml_cpy( |
| 1614 | struct ggml_context * ctx, |
| 1615 | struct ggml_tensor * a, |
| 1616 | struct ggml_tensor * b); |
| 1617 | |
| 1618 | // note: casting from f32 to i32 will discard the fractional part |
| 1619 | GGML_API struct ggml_tensor * ggml_cast( |
| 1620 | struct ggml_context * ctx, |
| 1621 | struct ggml_tensor * a, |
| 1622 | enum ggml_type type); |
| 1623 | |
| 1624 | // make contiguous |
| 1625 | GGML_API struct ggml_tensor * ggml_cont( |
| 1626 | struct ggml_context * ctx, |
| 1627 | struct ggml_tensor * a); |
| 1628 | |
| 1629 | // make contiguous, with new shape |
| 1630 | GGML_API struct ggml_tensor * ggml_cont_1d( |
| 1631 | struct ggml_context * ctx, |
| 1632 | struct ggml_tensor * a, |
| 1633 | int64_t ne0); |
| 1634 | |
| 1635 | GGML_API struct ggml_tensor * ggml_cont_2d( |
| 1636 | struct ggml_context * ctx, |
| 1637 | struct ggml_tensor * a, |
| 1638 | int64_t ne0, |
| 1639 | int64_t ne1); |
| 1640 | |
| 1641 | GGML_API struct ggml_tensor * ggml_cont_3d( |
| 1642 | struct ggml_context * ctx, |
| 1643 | struct ggml_tensor * a, |
| 1644 | int64_t ne0, |
| 1645 | int64_t ne1, |
| 1646 | int64_t ne2); |
| 1647 | |
| 1648 | GGML_API struct ggml_tensor * ggml_cont_4d( |
| 1649 | struct ggml_context * ctx, |
| 1650 | struct ggml_tensor * a, |
| 1651 | int64_t ne0, |
| 1652 | int64_t ne1, |
| 1653 | int64_t ne2, |
| 1654 | int64_t ne3); |
| 1655 | |
| 1656 | // return view(a), b specifies the new shape |
| 1657 | // TODO: when we start computing gradient, make a copy instead of view |
| 1658 | GGML_API struct ggml_tensor * ggml_reshape( |
| 1659 | struct ggml_context * ctx, |
| 1660 | struct ggml_tensor * a, |
| 1661 | struct ggml_tensor * b); |
| 1662 | |
| 1663 | // return view(a) |
| 1664 | // TODO: when we start computing gradient, make a copy instead of view |
| 1665 | GGML_API struct ggml_tensor * ggml_reshape_1d( |
| 1666 | struct ggml_context * ctx, |
| 1667 | struct ggml_tensor * a, |
| 1668 | int64_t ne0); |
| 1669 | |
| 1670 | GGML_API struct ggml_tensor * ggml_reshape_2d( |
| 1671 | struct ggml_context * ctx, |
| 1672 | struct ggml_tensor * a, |
| 1673 | int64_t ne0, |
| 1674 | int64_t ne1); |
| 1675 | |
| 1676 | // return view(a) |
| 1677 | // TODO: when we start computing gradient, make a copy instead of view |
| 1678 | GGML_API struct ggml_tensor * ggml_reshape_3d( |
| 1679 | struct ggml_context * ctx, |
| 1680 | struct ggml_tensor * a, |
| 1681 | int64_t ne0, |
| 1682 | int64_t ne1, |
| 1683 | int64_t ne2); |
| 1684 | |
| 1685 | GGML_API struct ggml_tensor * ggml_reshape_4d( |
| 1686 | struct ggml_context * ctx, |
| 1687 | struct ggml_tensor * a, |
| 1688 | int64_t ne0, |
| 1689 | int64_t ne1, |
| 1690 | int64_t ne2, |
| 1691 | int64_t ne3); |
| 1692 | |
| 1693 | // offset in bytes |
| 1694 | GGML_API struct ggml_tensor * ggml_view_1d( |
| 1695 | struct ggml_context * ctx, |
| 1696 | struct ggml_tensor * a, |
| 1697 | int64_t ne0, |
| 1698 | size_t offset); |
| 1699 | |
| 1700 | GGML_API struct ggml_tensor * ggml_view_2d( |
| 1701 | struct ggml_context * ctx, |
| 1702 | struct ggml_tensor * a, |
| 1703 | int64_t ne0, |
| 1704 | int64_t ne1, |
| 1705 | size_t nb1, // row stride in bytes |
| 1706 | size_t offset); |
| 1707 | |
| 1708 | GGML_API struct ggml_tensor * ggml_view_3d( |
| 1709 | struct ggml_context * ctx, |
| 1710 | struct ggml_tensor * a, |
| 1711 | int64_t ne0, |
| 1712 | int64_t ne1, |
| 1713 | int64_t ne2, |
| 1714 | size_t nb1, // row stride in bytes |
| 1715 | size_t nb2, // slice stride in bytes |
| 1716 | size_t offset); |
| 1717 | |
| 1718 | GGML_API struct ggml_tensor * ggml_view_4d( |
| 1719 | struct ggml_context * ctx, |
| 1720 | struct ggml_tensor * a, |
| 1721 | int64_t ne0, |
| 1722 | int64_t ne1, |
| 1723 | int64_t ne2, |
| 1724 | int64_t ne3, |
| 1725 | size_t nb1, // row stride in bytes |
| 1726 | size_t nb2, // slice stride in bytes |
| 1727 | size_t nb3, |
| 1728 | size_t offset); |
| 1729 | |
| 1730 | GGML_API struct ggml_tensor * ggml_permute( |
| 1731 | struct ggml_context * ctx, |
| 1732 | struct ggml_tensor * a, |
| 1733 | int axis0, |
| 1734 | int axis1, |
| 1735 | int axis2, |
| 1736 | int axis3); |
| 1737 | |
| 1738 | // alias for ggml_permute(ctx, a, 1, 0, 2, 3) |
| 1739 | GGML_API struct ggml_tensor * ggml_transpose( |
| 1740 | struct ggml_context * ctx, |
| 1741 | struct ggml_tensor * a); |
| 1742 | |
| 1743 | // supports 4D a: |
| 1744 | // a [n_embd, ne1, ne2, ne3] |
| 1745 | // b I32 [n_rows, ne2, ne3, 1] |
| 1746 | // |
| 1747 | // return [n_embd, n_rows, ne2, ne3] |
| 1748 | GGML_API struct ggml_tensor * ggml_get_rows( |
| 1749 | struct ggml_context * ctx, |
| 1750 | struct ggml_tensor * a, // data |
| 1751 | struct ggml_tensor * b); // row indices |
| 1752 | |
| 1753 | GGML_API struct ggml_tensor * ggml_get_rows_back( |
| 1754 | struct ggml_context * ctx, |
| 1755 | struct ggml_tensor * a, // gradients of ggml_get_rows result |
| 1756 | struct ggml_tensor * b, // row indices |
| 1757 | struct ggml_tensor * c); // data for ggml_get_rows, only used for its shape |
| 1758 | |
| 1759 | // a TD [n_embd, ne1, ne2, ne3] |
| 1760 | // b TS [n_embd, n_rows, ne02, ne03] | ne02 == ne2, ne03 == ne3 |
| 1761 | // c I64 [n_rows, ne11, ne12, 1] | c[i] in [0, ne1) |
| 1762 | // |
| 1763 | // undefined behavior if destination rows overlap |
| 1764 | // |
| 1765 | // broadcast: |
| 1766 | // ne2 % ne11 == 0 |
| 1767 | // ne3 % ne12 == 0 |
| 1768 | // |
| 1769 | // return view(a) |
| 1770 | GGML_API struct ggml_tensor * ggml_set_rows( |
| 1771 | struct ggml_context * ctx, |
| 1772 | struct ggml_tensor * a, // destination |
| 1773 | struct ggml_tensor * b, // source |
| 1774 | struct ggml_tensor * c); // row indices |
| 1775 | |
| 1776 | GGML_API struct ggml_tensor * ggml_diag( |
| 1777 | struct ggml_context * ctx, |
| 1778 | struct ggml_tensor * a); |
| 1779 | |
| 1780 | // set elements above the diagonal to -INF |
| 1781 | GGML_API struct ggml_tensor * ggml_diag_mask_inf( |
| 1782 | struct ggml_context * ctx, |
| 1783 | struct ggml_tensor * a, |
| 1784 | int n_past); |
| 1785 | |
| 1786 | // in-place, returns view(a) |
| 1787 | GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace( |
| 1788 | struct ggml_context * ctx, |
| 1789 | struct ggml_tensor * a, |
| 1790 | int n_past); |
| 1791 | |
| 1792 | // set elements above the diagonal to 0 |
| 1793 | GGML_API struct ggml_tensor * ggml_diag_mask_zero( |
| 1794 | struct ggml_context * ctx, |
| 1795 | struct ggml_tensor * a, |
| 1796 | int n_past); |
| 1797 | |
| 1798 | // in-place, returns view(a) |
| 1799 | GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace( |
| 1800 | struct ggml_context * ctx, |
| 1801 | struct ggml_tensor * a, |
| 1802 | int n_past); |
| 1803 | |
| 1804 | GGML_API struct ggml_tensor * ggml_clamp( |
| 1805 | struct ggml_context * ctx, |
| 1806 | struct ggml_tensor * a, |
| 1807 | float min, |
| 1808 | float max); |
| 1809 | |
| 1810 | // in-place, returns view(a) |
| 1811 | GGML_API struct ggml_tensor * ggml_clamp_inplace( |
| 1812 | struct ggml_context * ctx, |
| 1813 | struct ggml_tensor * a, |
| 1814 | float min, |
| 1815 | float max); |
| 1816 | |
| 1817 | GGML_API struct ggml_tensor * ggml_soft_max( |
| 1818 | struct ggml_context * ctx, |
| 1819 | struct ggml_tensor * a); |
| 1820 | |
| 1821 | // in-place, returns view(a) |
| 1822 | GGML_API struct ggml_tensor * ggml_soft_max_inplace( |
| 1823 | struct ggml_context * ctx, |
| 1824 | struct ggml_tensor * a); |
| 1825 | |
| 1826 | // a [ne0, ne01, ne02, ne03] |
| 1827 | // mask [ne0, ne11, ne12, ne13] | ne11 >= ne01, F16 or F32, optional |
| 1828 | // |
| 1829 | // broadcast: |
| 1830 | // ne02 % ne12 == 0 |
| 1831 | // ne03 % ne13 == 0 |
| 1832 | // |
| 1833 | // fused soft_max(a*scale + mask*(ALiBi slope)) |
| 1834 | // max_bias = 0.0f for no ALiBi |
| 1835 | GGML_API struct ggml_tensor * ggml_soft_max_ext( |
| 1836 | struct ggml_context * ctx, |
| 1837 | struct ggml_tensor * a, |
| 1838 | struct ggml_tensor * mask, |
| 1839 | float scale, |
| 1840 | float max_bias); |
| 1841 | |
| 1842 | GGML_API struct ggml_tensor * ggml_soft_max_ext_inplace( |
| 1843 | struct ggml_context * ctx, |
| 1844 | struct ggml_tensor * a, |
| 1845 | struct ggml_tensor * mask, |
| 1846 | float scale, |
| 1847 | float max_bias); |
| 1848 | |
| 1849 | GGML_API void ggml_soft_max_add_sinks( |
| 1850 | struct ggml_tensor * a, |
| 1851 | struct ggml_tensor * sinks); |
| 1852 | |
| 1853 | GGML_API struct ggml_tensor * ggml_soft_max_ext_back( |
| 1854 | struct ggml_context * ctx, |
| 1855 | struct ggml_tensor * a, |
| 1856 | struct ggml_tensor * b, |
| 1857 | float scale, |
| 1858 | float max_bias); |
| 1859 | |
| 1860 | // in-place, returns view(a) |
| 1861 | GGML_API struct ggml_tensor * ggml_soft_max_ext_back_inplace( |
| 1862 | struct ggml_context * ctx, |
| 1863 | struct ggml_tensor * a, |
| 1864 | struct ggml_tensor * b, |
| 1865 | float scale, |
| 1866 | float max_bias); |
| 1867 | |
| 1868 | // rotary position embedding |
| 1869 | // if (mode & 1) - skip n_past elements (NOT SUPPORTED) |
| 1870 | // if (mode & GGML_ROPE_TYPE_NEOX) - GPT-NeoX style |
| 1871 | // |
| 1872 | // b is an int32 vector with size a->ne[2], it contains the positions |
| 1873 | GGML_API struct ggml_tensor * ggml_rope( |
| 1874 | struct ggml_context * ctx, |
| 1875 | struct ggml_tensor * a, |
| 1876 | struct ggml_tensor * b, |
| 1877 | int n_dims, |
| 1878 | int mode); |
| 1879 | |
| 1880 | // in-place, returns view(a) |
| 1881 | GGML_API struct ggml_tensor * ggml_rope_inplace( |
| 1882 | struct ggml_context * ctx, |
| 1883 | struct ggml_tensor * a, |
| 1884 | struct ggml_tensor * b, |
| 1885 | int n_dims, |
| 1886 | int mode); |
| 1887 | |
| 1888 | // RoPE operations with extended options |
| 1889 | // a is the input tensor to apply RoPE to, shape [n_embd, n_head, n_token] |
| 1890 | // b is an int32 vector with size n_token |
| 1891 | // c is freq factors (e.g. phi3-128k), (optional) |
| 1892 | // mode can be GGML_ROPE_TYPE_NORMAL or NEOX; for MROPE and VISION mode, use ggml_rope_multi |
| 1893 | // |
| 1894 | // pseudo-code for computing theta: |
| 1895 | // for i in [0, n_dims/2): |
| 1896 | // theta[i] = b[i] * powf(freq_base, -2.0 * i / n_dims); |
| 1897 | // theta[i] = theta[i] / c[i]; # if c is provided, divide theta by c |
| 1898 | // theta[i] = rope_yarn(theta[i], ...); # note: theta = theta * freq_scale is applied here |
| 1899 | // |
| 1900 | // other params are used by YaRN RoPE scaling, these default values will disable YaRN: |
| 1901 | // freq_scale = 1.0f |
| 1902 | // ext_factor = 0.0f |
| 1903 | // attn_factor = 1.0f |
| 1904 | // beta_fast = 0.0f |
| 1905 | // beta_slow = 0.0f |
| 1906 | // |
| 1907 | // example: |
| 1908 | // (marking: c = cos, s = sin, 0 = unrotated) |
| 1909 | // given a single head with size = 8 --> [00000000] |
| 1910 | // GGML_ROPE_TYPE_NORMAL n_dims = 4 --> [cscs0000] |
| 1911 | // GGML_ROPE_TYPE_NORMAL n_dims = 8 --> [cscscscs] |
| 1912 | // GGML_ROPE_TYPE_NEOX n_dims = 4 --> [ccss0000] |
| 1913 | // GGML_ROPE_TYPE_NEOX n_dims = 8 --> [ccccssss] |
| 1914 | GGML_API struct ggml_tensor * ggml_rope_ext( |
| 1915 | struct ggml_context * ctx, |
| 1916 | struct ggml_tensor * a, |
| 1917 | struct ggml_tensor * b, |
| 1918 | struct ggml_tensor * c, |
| 1919 | int n_dims, |
| 1920 | int mode, |
| 1921 | int n_ctx_orig, |
| 1922 | float freq_base, |
| 1923 | float freq_scale, |
| 1924 | float ext_factor, |
| 1925 | float attn_factor, |
| 1926 | float beta_fast, |
| 1927 | float beta_slow); |
| 1928 | |
| 1929 | // multi-dimensional RoPE, for Qwen-VL and similar vision models |
| 1930 | // mode can be either VISION, MROPE, IMROPE, cannot be combined with NORMAL or NEOX |
| 1931 | // sections specify how many dimensions to rotate in each section: |
| 1932 | // section length is equivalent to number of cos/sin pairs, NOT the number of dims |
| 1933 | // (i.e. sum of 4 sections are expected to be n_dims/2) |
| 1934 | // last sections can be 0, means ignored |
| 1935 | // all other options are identical to ggml_rope_ext |
| 1936 | // |
| 1937 | // important note: |
| 1938 | // - NEOX ordering is automatically applied and cannot be disabled for MROPE and VISION |
| 1939 | // if you need normal ordering, there are 2 methods: |
| 1940 | // (1) split the tensor manually using ggml_view |
| 1941 | // (2) permute the weight upon conversion |
| 1942 | // - for VISION, n_dims must be head_size/2 |
| 1943 | // |
| 1944 | // example M-RoPE: |
| 1945 | // given sections = [t=4, y=2, x=2, 0] |
| 1946 | // given a single head with size = 18 --> [000000000000000000] |
| 1947 | // GGML_ROPE_TYPE_MROPE n_dims = 16 --> [ttttyyxxttttyyxx00] (cos/sin are applied in NEOX ordering) |
| 1948 | // GGML_ROPE_TYPE_IMROPE n_dims = 16 --> [ttyxttyxttyxttyx00] (interleaved M-RoPE, still NEOX ordering) |
| 1949 | // note: the theta for each dim is computed the same way as ggml_rope_ext, no matter the section |
| 1950 | // in other words, idx used for theta: [0123456789... until n_dims/2], not reset for each section |
| 1951 | // |
| 1952 | // example vision RoPE: |
| 1953 | // given sections = [y=4, x=4, 0, 0] (last 2 sections are ignored) |
| 1954 | // given a single head with size = 8 --> [00000000] |
| 1955 | // GGML_ROPE_TYPE_VISION n_dims = 4 --> [yyyyxxxx] |
| 1956 | // other values of n_dims are untested and is undefined behavior |
| 1957 | // note: unlike MROPE, the theta for each dim is computed differently for each section |
| 1958 | // in other words, idx used for theta: [0123] for y section, then [0123] for x section |
| 1959 | GGML_API struct ggml_tensor * ggml_rope_multi( |
| 1960 | struct ggml_context * ctx, |
| 1961 | struct ggml_tensor * a, |
| 1962 | struct ggml_tensor * b, |
| 1963 | struct ggml_tensor * c, |
| 1964 | int n_dims, |
| 1965 | int sections[GGML_MROPE_SECTIONS], |
| 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 | // in-place, returns view(a) |
| 1976 | GGML_API struct ggml_tensor * ggml_rope_ext_inplace( |
| 1977 | struct ggml_context * ctx, |
| 1978 | struct ggml_tensor * a, |
| 1979 | struct ggml_tensor * b, |
| 1980 | struct ggml_tensor * c, |
| 1981 | int n_dims, |
| 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_API struct ggml_tensor * ggml_rope_multi_inplace( |
| 1992 | struct ggml_context * ctx, |
| 1993 | struct ggml_tensor * a, |
| 1994 | struct ggml_tensor * b, |
| 1995 | struct ggml_tensor * c, |
| 1996 | int n_dims, |
| 1997 | int sections[GGML_MROPE_SECTIONS], |
| 1998 | int mode, |
| 1999 | int n_ctx_orig, |
| 2000 | float freq_base, |
| 2001 | float freq_scale, |
| 2002 | float ext_factor, |
| 2003 | float attn_factor, |
| 2004 | float beta_fast, |
| 2005 | float beta_slow); |
| 2006 | |
| 2007 | GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom( |
| 2008 | struct ggml_context * ctx, |
| 2009 | struct ggml_tensor * a, |
| 2010 | struct ggml_tensor * b, |
| 2011 | int n_dims, |
| 2012 | int mode, |
| 2013 | int n_ctx_orig, |
| 2014 | float freq_base, |
| 2015 | float freq_scale, |
| 2016 | float ext_factor, |
| 2017 | float attn_factor, |
| 2018 | float beta_fast, |
| 2019 | float beta_slow), |
| 2020 | "use ggml_rope_ext instead"); |
| 2021 | |
| 2022 | GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom_inplace( |
| 2023 | struct ggml_context * ctx, |
| 2024 | struct ggml_tensor * a, |
| 2025 | struct ggml_tensor * b, |
| 2026 | int n_dims, |
| 2027 | int mode, |
| 2028 | int n_ctx_orig, |
| 2029 | float freq_base, |
| 2030 | float freq_scale, |
| 2031 | float ext_factor, |
| 2032 | float attn_factor, |
| 2033 | float beta_fast, |
| 2034 | float beta_slow), |
| 2035 | "use ggml_rope_ext_inplace instead"); |
| 2036 | |
| 2037 | // compute correction dims for YaRN RoPE scaling |
| 2038 | GGML_API void ggml_rope_yarn_corr_dims( |
| 2039 | int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]); |
| 2040 | |
| 2041 | // rotary position embedding backward, i.e compute dx from dy |
| 2042 | // a - dy |
| 2043 | GGML_API struct ggml_tensor * ggml_rope_ext_back( |
| 2044 | struct ggml_context * ctx, |
| 2045 | struct ggml_tensor * a, // gradients of ggml_rope result |
| 2046 | struct ggml_tensor * b, // positions |
| 2047 | struct ggml_tensor * c, // freq factors |
| 2048 | int n_dims, |
| 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 | GGML_API struct ggml_tensor * ggml_rope_multi_back( |
| 2059 | struct ggml_context * ctx, |
| 2060 | struct ggml_tensor * a, |
| 2061 | struct ggml_tensor * b, |
| 2062 | struct ggml_tensor * c, |
| 2063 | int n_dims, |
| 2064 | int sections[4], |
| 2065 | int mode, |
| 2066 | int n_ctx_orig, |
| 2067 | float freq_base, |
| 2068 | float freq_scale, |
| 2069 | float ext_factor, |
| 2070 | float attn_factor, |
| 2071 | float beta_fast, |
| 2072 | float beta_slow); |
| 2073 | |
| 2074 | // set the offset dims for RoPE |
| 2075 | // a must be GGML_OP_ROPE or GGML_OP_ROPE_BACK |
| 2076 | // vision RoPE is not supported |
| 2077 | // example: (marking: x = rotated, 0 = unrotated) |
| 2078 | // n_embd = 10, n_dims = 4, offset = 2 --> [00xxxx0000] |
| 2079 | GGML_API struct ggml_tensor * ggml_rope_set_offset( |
| 2080 | struct ggml_tensor * a, |
| 2081 | int n_offs); |
| 2082 | |
| 2083 | // im2col |
| 2084 | // converts data into a format that effectively results in a convolution when combined with matrix multiplication |
| 2085 | GGML_API struct ggml_tensor * ggml_im2col( |
| 2086 | struct ggml_context * ctx, |
| 2087 | struct ggml_tensor * a, // convolution kernel |
| 2088 | struct ggml_tensor * b, // data |
| 2089 | int s0, // stride dimension 0 |
| 2090 | int s1, // stride dimension 1 |
| 2091 | int p0, // padding dimension 0 |
| 2092 | int p1, // padding dimension 1 |
| 2093 | int d0, // dilation dimension 0 |
| 2094 | int d1, // dilation dimension 1 |
| 2095 | bool is_2D, |
| 2096 | enum ggml_type dst_type); |
| 2097 | |
| 2098 | GGML_API struct ggml_tensor * ggml_im2col_back( |
| 2099 | struct ggml_context * ctx, |
| 2100 | struct ggml_tensor * a, // convolution kernel |
| 2101 | struct ggml_tensor * b, // gradient of im2col output |
| 2102 | int64_t * ne, // shape of im2col input |
| 2103 | int s0, // stride dimension 0 |
| 2104 | int s1, // stride dimension 1 |
| 2105 | int p0, // padding dimension 0 |
| 2106 | int p1, // padding dimension 1 |
| 2107 | int d0, // dilation dimension 0 |
| 2108 | int d1, // dilation dimension 1 |
| 2109 | bool is_2D); |
| 2110 | |
| 2111 | // col2im_1d: scatter-add GEMM columns back to 1D signal |
| 2112 | // a: [K*OC, T_in] (columns from matmul, K = a->ne[0]/OC) |
| 2113 | // result: [T_out, OC] where T_out = (T_in - 1)*s0 + K - 2*p0 |
| 2114 | GGML_API struct ggml_tensor * ggml_col2im_1d( |
| 2115 | struct ggml_context * ctx, |
| 2116 | struct ggml_tensor * a, // columns [K*OC, T_in] |
| 2117 | int s0, // stride |
| 2118 | int oc, // output channels |
| 2119 | int p0); // padding to crop from both sides |
| 2120 | |
| 2121 | GGML_API struct ggml_tensor * ggml_conv_1d( |
| 2122 | struct ggml_context * ctx, |
| 2123 | struct ggml_tensor * a, // convolution kernel |
| 2124 | struct ggml_tensor * b, // data |
| 2125 | int s0, // stride |
| 2126 | int p0, // padding |
| 2127 | int d0); // dilation |
| 2128 | |
| 2129 | // conv_1d with padding = half |
| 2130 | // alias for ggml_conv_1d(a, b, s, a->ne[0]/2, d) |
| 2131 | GGML_API struct ggml_tensor* ggml_conv_1d_ph( |
| 2132 | struct ggml_context * ctx, |
| 2133 | struct ggml_tensor * a, // convolution kernel |
| 2134 | struct ggml_tensor * b, // data |
| 2135 | int s, // stride |
| 2136 | int d); // dilation |
| 2137 | |
| 2138 | // depthwise |
| 2139 | // TODO: this is very likely wrong for some cases! - needs more testing |
| 2140 | GGML_API struct ggml_tensor * ggml_conv_1d_dw( |
| 2141 | struct ggml_context * ctx, |
| 2142 | struct ggml_tensor * a, // convolution kernel |
| 2143 | struct ggml_tensor * b, // data |
| 2144 | int s0, // stride |
| 2145 | int p0, // padding |
| 2146 | int d0); // dilation |
| 2147 | |
| 2148 | GGML_API struct ggml_tensor * ggml_conv_1d_dw_ph( |
| 2149 | struct ggml_context * ctx, |
| 2150 | struct ggml_tensor * a, // convolution kernel |
| 2151 | struct ggml_tensor * b, // data |
| 2152 | int s0, // stride |
| 2153 | int d0); // dilation |
| 2154 | |
| 2155 | GGML_API struct ggml_tensor * ggml_conv_transpose_1d( |
| 2156 | struct ggml_context * ctx, |
| 2157 | struct ggml_tensor * a, // convolution kernel |
| 2158 | struct ggml_tensor * b, // data |
| 2159 | int s0, // stride |
| 2160 | int p0, // padding |
| 2161 | int d0); // dilation |
| 2162 | |
| 2163 | GGML_API struct ggml_tensor * ggml_conv_2d( |
| 2164 | struct ggml_context * ctx, |
| 2165 | struct ggml_tensor * a, // convolution kernel |
| 2166 | struct ggml_tensor * b, // data |
| 2167 | int s0, // stride dimension 0 |
| 2168 | int s1, // stride dimension 1 |
| 2169 | int p0, // padding dimension 0 |
| 2170 | int p1, // padding dimension 1 |
| 2171 | int d0, // dilation dimension 0 |
| 2172 | int d1); // dilation dimension 1 |
| 2173 | |
| 2174 | GGML_API struct ggml_tensor * ggml_im2col_3d( |
| 2175 | struct ggml_context * ctx, |
| 2176 | struct ggml_tensor * a, |
| 2177 | struct ggml_tensor * b, |
| 2178 | int64_t IC, |
| 2179 | int s0, // stride width |
| 2180 | int s1, // stride height |
| 2181 | int s2, // stride depth |
| 2182 | int p0, // padding width |
| 2183 | int p1, // padding height |
| 2184 | int p2, // padding depth |
| 2185 | int d0, // dilation width |
| 2186 | int d1, // dilation height |
| 2187 | int d2, // dilation depth |
| 2188 | enum ggml_type dst_type); |
| 2189 | |
| 2190 | // a: [OC*IC, KD, KH, KW] |
| 2191 | // b: [N*IC, ID, IH, IW] |
| 2192 | // result: [N*OC, OD, OH, OW] |
| 2193 | GGML_API struct ggml_tensor * ggml_conv_3d( |
| 2194 | struct ggml_context * ctx, |
| 2195 | struct ggml_tensor * a, |
| 2196 | struct ggml_tensor * b, |
| 2197 | int64_t IC, |
| 2198 | int s0, // stride width |
| 2199 | int s1, // stride height |
| 2200 | int s2, // stride depth |
| 2201 | int p0, // padding width |
| 2202 | int p1, // padding height |
| 2203 | int p2, // padding depth |
| 2204 | int d0, // dilation width |
| 2205 | int d1, // dilation height |
| 2206 | int d2 // dilation depth |
| 2207 | ); |
| 2208 | |
| 2209 | // kernel size is a->ne[0] x a->ne[1] |
| 2210 | // stride is equal to kernel size |
| 2211 | // padding is zero |
| 2212 | // example: |
| 2213 | // a: 16 16 3 768 |
| 2214 | // b: 1024 1024 3 1 |
| 2215 | // res: 64 64 768 1 |
| 2216 | // used in sam |
| 2217 | GGML_API struct ggml_tensor * ggml_conv_2d_sk_p0( |
| 2218 | struct ggml_context * ctx, |
| 2219 | struct ggml_tensor * a, |
| 2220 | struct ggml_tensor * b); |
| 2221 | |
| 2222 | // kernel size is a->ne[0] x a->ne[1] |
| 2223 | // stride is 1 |
| 2224 | // padding is half |
| 2225 | // example: |
| 2226 | // a: 3 3 256 256 |
| 2227 | // b: 64 64 256 1 |
| 2228 | // res: 64 64 256 1 |
| 2229 | // used in sam |
| 2230 | GGML_API struct ggml_tensor * ggml_conv_2d_s1_ph( |
| 2231 | struct ggml_context * ctx, |
| 2232 | struct ggml_tensor * a, |
| 2233 | struct ggml_tensor * b); |
| 2234 | |
| 2235 | // depthwise (via im2col and mul_mat) |
| 2236 | GGML_API struct ggml_tensor * ggml_conv_2d_dw( |
| 2237 | struct ggml_context * ctx, |
| 2238 | struct ggml_tensor * a, // convolution kernel |
| 2239 | struct ggml_tensor * b, // data |
| 2240 | int s0, // stride dimension 0 |
| 2241 | int s1, // stride dimension 1 |
| 2242 | int p0, // padding dimension 0 |
| 2243 | int p1, // padding dimension 1 |
| 2244 | int d0, // dilation dimension 0 |
| 2245 | int d1); // dilation dimension 1 |
| 2246 | |
| 2247 | // Depthwise 2D convolution |
| 2248 | // may be faster than ggml_conv_2d_dw, but not available in all backends |
| 2249 | // a: KW KH 1 C convolution kernel |
| 2250 | // b: W H C N input data |
| 2251 | // res: W_out H_out C N |
| 2252 | GGML_API struct ggml_tensor * ggml_conv_2d_dw_direct( |
| 2253 | struct ggml_context * ctx, |
| 2254 | struct ggml_tensor * a, |
| 2255 | struct ggml_tensor * b, |
| 2256 | int stride0, |
| 2257 | int stride1, |
| 2258 | int pad0, |
| 2259 | int pad1, |
| 2260 | int dilation0, |
| 2261 | int dilation1); |
| 2262 | |
| 2263 | GGML_API struct ggml_tensor * ggml_conv_transpose_2d_p0( |
| 2264 | struct ggml_context * ctx, |
| 2265 | struct ggml_tensor * a, |
| 2266 | struct ggml_tensor * b, |
| 2267 | int stride); |
| 2268 | |
| 2269 | GGML_API struct ggml_tensor * ggml_conv_2d_direct( |
| 2270 | struct ggml_context * ctx, |
| 2271 | struct ggml_tensor * a, // convolution kernel [KW, KH, IC, OC] |
| 2272 | struct ggml_tensor * b, // input data [W, H, C, N] |
| 2273 | int s0, // stride dimension 0 |
| 2274 | int s1, // stride dimension 1 |
| 2275 | int p0, // padding dimension 0 |
| 2276 | int p1, // padding dimension 1 |
| 2277 | int d0, // dilation dimension 0 |
| 2278 | int d1); // dilation dimension 1 |
| 2279 | |
| 2280 | GGML_API struct ggml_tensor * ggml_conv_3d_direct( |
| 2281 | struct ggml_context * ctx, |
| 2282 | struct ggml_tensor * a, // kernel [KW, KH, KD, IC * OC] |
| 2283 | struct ggml_tensor * b, // input [W, H, D, C * N] |
| 2284 | int s0, // stride |
| 2285 | int s1, |
| 2286 | int s2, |
| 2287 | int p0, // padding |
| 2288 | int p1, |
| 2289 | int p2, |
| 2290 | int d0, // dilation |
| 2291 | int d1, |
| 2292 | int d2, |
| 2293 | int n_channels, |
| 2294 | int n_batch, |
| 2295 | int n_channels_out); |
| 2296 | |
| 2297 | enum ggml_op_pool { |
| 2298 | GGML_OP_POOL_MAX, |
| 2299 | GGML_OP_POOL_AVG, |
| 2300 | GGML_OP_POOL_COUNT, |
| 2301 | }; |
| 2302 | |
| 2303 | GGML_API struct ggml_tensor * ggml_pool_1d( |
| 2304 | struct ggml_context * ctx, |
| 2305 | struct ggml_tensor * a, |
| 2306 | enum ggml_op_pool op, |
| 2307 | int k0, // kernel size |
| 2308 | int s0, // stride |
| 2309 | int p0); // padding |
| 2310 | |
| 2311 | // the result will have 2*p0 padding for the first dimension |
| 2312 | // and 2*p1 padding for the second dimension |
| 2313 | GGML_API struct ggml_tensor * ggml_pool_2d( |
| 2314 | struct ggml_context * ctx, |
| 2315 | struct ggml_tensor * a, |
| 2316 | enum ggml_op_pool op, |
| 2317 | int k0, |
| 2318 | int k1, |
| 2319 | int s0, |
| 2320 | int s1, |
| 2321 | float p0, |
| 2322 | float p1); |
| 2323 | |
| 2324 | GGML_API struct ggml_tensor * ggml_pool_2d_back( |
| 2325 | struct ggml_context * ctx, |
| 2326 | struct ggml_tensor * a, |
| 2327 | struct ggml_tensor * af, // "a"/input used in forward pass |
| 2328 | enum ggml_op_pool op, |
| 2329 | int k0, |
| 2330 | int k1, |
| 2331 | int s0, |
| 2332 | int s1, |
| 2333 | float p0, |
| 2334 | float p1); |
| 2335 | |
| 2336 | enum ggml_scale_mode { |
| 2337 | GGML_SCALE_MODE_NEAREST = 0, |
| 2338 | GGML_SCALE_MODE_BILINEAR = 1, |
| 2339 | GGML_SCALE_MODE_BICUBIC = 2, |
| 2340 | |
| 2341 | GGML_SCALE_MODE_COUNT |
| 2342 | }; |
| 2343 | |
| 2344 | enum ggml_scale_flag { |
| 2345 | GGML_SCALE_FLAG_ALIGN_CORNERS = (1 << 8), |
| 2346 | GGML_SCALE_FLAG_ANTIALIAS = (1 << 9), |
| 2347 | }; |
| 2348 | |
| 2349 | // interpolate |
| 2350 | // multiplies ne0 and ne1 by scale factor |
| 2351 | GGML_API struct ggml_tensor * ggml_upscale( |
| 2352 | struct ggml_context * ctx, |
| 2353 | struct ggml_tensor * a, |
| 2354 | int scale_factor, |
| 2355 | enum ggml_scale_mode mode); |
| 2356 | |
| 2357 | // interpolate |
| 2358 | // interpolate scale to specified dimensions |
| 2359 | GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_upscale_ext( |
| 2360 | struct ggml_context * ctx, |
| 2361 | struct ggml_tensor * a, |
| 2362 | int ne0, |
| 2363 | int ne1, |
| 2364 | int ne2, |
| 2365 | int ne3, |
| 2366 | enum ggml_scale_mode mode), |
| 2367 | "use ggml_interpolate instead"); |
| 2368 | |
| 2369 | // Up- or downsamples the input to the specified size. |
| 2370 | // 2D scale modes (eg. bilinear) are applied to the first two dimensions. |
| 2371 | GGML_API struct ggml_tensor * ggml_interpolate( |
| 2372 | struct ggml_context * ctx, |
| 2373 | struct ggml_tensor * a, |
| 2374 | int64_t ne0, |
| 2375 | int64_t ne1, |
| 2376 | int64_t ne2, |
| 2377 | int64_t ne3, |
| 2378 | uint32_t mode); // ggml_scale_mode [ | ggml_scale_flag...] |
| 2379 | |
| 2380 | // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0] |
| 2381 | GGML_API struct ggml_tensor * ggml_pad( |
| 2382 | struct ggml_context * ctx, |
| 2383 | struct ggml_tensor * a, |
| 2384 | int p0, |
| 2385 | int p1, |
| 2386 | int p2, |
| 2387 | int p3); |
| 2388 | |
| 2389 | // pad each dimension with values on the other side of the torus (looping around) |
| 2390 | GGML_API struct ggml_tensor * ggml_pad_circular( |
| 2391 | struct ggml_context * ctx, |
| 2392 | struct ggml_tensor * a, |
| 2393 | int p0, |
| 2394 | int p1, |
| 2395 | int p2, |
| 2396 | int p3); |
| 2397 | |
| 2398 | GGML_API struct ggml_tensor * ggml_pad_ext( |
| 2399 | struct ggml_context * ctx, |
| 2400 | struct ggml_tensor * a, |
| 2401 | int lp0, |
| 2402 | int rp0, |
| 2403 | int lp1, |
| 2404 | int rp1, |
| 2405 | int lp2, |
| 2406 | int rp2, |
| 2407 | int lp3, |
| 2408 | int rp3 |
| 2409 | ); |
| 2410 | |
| 2411 | // pad each dimension with values on the other side of the torus (looping around) |
| 2412 | GGML_API struct ggml_tensor * ggml_pad_ext_circular( |
| 2413 | struct ggml_context * ctx, |
| 2414 | struct ggml_tensor * a, |
| 2415 | int lp0, |
| 2416 | int rp0, |
| 2417 | int lp1, |
| 2418 | int rp1, |
| 2419 | int lp2, |
| 2420 | int rp2, |
| 2421 | int lp3, |
| 2422 | int rp3); |
| 2423 | |
| 2424 | // pad each dimension with reflection: [a, b, c, d] -> [b, a, b, c, d, c] |
| 2425 | GGML_API struct ggml_tensor * ggml_pad_reflect_1d( |
| 2426 | struct ggml_context * ctx, |
| 2427 | struct ggml_tensor * a, |
| 2428 | int p0, |
| 2429 | int p1); |
| 2430 | |
| 2431 | // Move tensor elements by an offset given for each dimension. Elements that |
| 2432 | // are shifted beyond the last position are wrapped around to the beginning. |
| 2433 | GGML_API struct ggml_tensor * ggml_roll( |
| 2434 | struct ggml_context * ctx, |
| 2435 | struct ggml_tensor * a, |
| 2436 | int shift0, |
| 2437 | int shift1, |
| 2438 | int shift2, |
| 2439 | int shift3); |
| 2440 | |
| 2441 | // Convert matrix into a triangular one (upper, strict upper, lower or strict lower) by writing |
| 2442 | // zeroes everywhere outside the masked area |
| 2443 | GGML_API struct ggml_tensor * ggml_tri( |
| 2444 | struct ggml_context * ctx, |
| 2445 | struct ggml_tensor * a, |
| 2446 | enum ggml_tri_type type); |
| 2447 | |
| 2448 | // Fill tensor a with constant c |
| 2449 | GGML_API struct ggml_tensor * ggml_fill( |
| 2450 | struct ggml_context * ctx, |
| 2451 | struct ggml_tensor * a, |
| 2452 | float c); |
| 2453 | |
| 2454 | GGML_API struct ggml_tensor * ggml_fill_inplace( |
| 2455 | struct ggml_context * ctx, |
| 2456 | struct ggml_tensor * a, |
| 2457 | float c); |
| 2458 | |
| 2459 | // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151 |
| 2460 | // timesteps: [N,] |
| 2461 | // return: [N, dim] |
| 2462 | GGML_API struct ggml_tensor * ggml_timestep_embedding( |
| 2463 | struct ggml_context * ctx, |
| 2464 | struct ggml_tensor * timesteps, |
| 2465 | int dim, |
| 2466 | int max_period); |
| 2467 | |
| 2468 | // sort rows |
| 2469 | enum ggml_sort_order { |
| 2470 | GGML_SORT_ORDER_ASC, |
| 2471 | GGML_SORT_ORDER_DESC, |
| 2472 | }; |
| 2473 | |
| 2474 | GGML_API struct ggml_tensor * ggml_argsort( |
| 2475 | struct ggml_context * ctx, |
| 2476 | struct ggml_tensor * a, |
| 2477 | enum ggml_sort_order order); |
| 2478 | |
| 2479 | // similar to ggml_top_k but implemented as `argsort` + `view` |
| 2480 | GGML_API struct ggml_tensor * ggml_argsort_top_k( |
| 2481 | struct ggml_context * ctx, |
| 2482 | struct ggml_tensor * a, |
| 2483 | int k); |
| 2484 | |
| 2485 | // top k elements per row |
| 2486 | // note: the resulting top k indices are in no particular order |
| 2487 | GGML_API struct ggml_tensor * ggml_top_k( |
| 2488 | struct ggml_context * ctx, |
| 2489 | struct ggml_tensor * a, |
| 2490 | int k); |
| 2491 | |
| 2492 | GGML_API struct ggml_tensor * ggml_arange( |
| 2493 | struct ggml_context * ctx, |
| 2494 | float start, |
| 2495 | float stop, |
| 2496 | float step); |
| 2497 | |
| 2498 | // q: [n_embd_k, n_batch, n_head, ne3 ] |
| 2499 | // k: [n_embd_k, n_kv, n_head_kv, ne3 ] |
| 2500 | // v: [n_embd_v, n_kv, n_head_kv, ne3 ] !! not transposed !! |
| 2501 | // mask: [n_kv, n_batch, ne32, ne33] |
| 2502 | // res: [n_embd_v, n_head, n_batch, ne3 ] !! permuted !! |
| 2503 | // |
| 2504 | // broadcast: |
| 2505 | // n_head % n_head_kv == 0 |
| 2506 | // n_head % ne32 == 0 |
| 2507 | // ne3 % ne33 == 0 |
| 2508 | // |
| 2509 | GGML_API struct ggml_tensor * ggml_flash_attn_ext( |
| 2510 | struct ggml_context * ctx, |
| 2511 | struct ggml_tensor * q, |
| 2512 | struct ggml_tensor * k, |
| 2513 | struct ggml_tensor * v, |
| 2514 | struct ggml_tensor * mask, |
| 2515 | float scale, |
| 2516 | float max_bias, |
| 2517 | float logit_softcap); |
| 2518 | |
| 2519 | // same as ggml_flash_attn_ext, but each q slice attends to its own list of K/V rows |
| 2520 | // q: [n_embd_k, n_batch, n_head, ne3] |
| 2521 | // k: [n_embd_k, n_rows, n_head_kv, 1 ] shared by all q slices |
| 2522 | // v: [n_embd_v, n_rows, n_head_kv, 1 ] |
| 2523 | // mask: [n_kv, n_batch, ne32, ne3] |
| 2524 | // kv_rows: [n_kv, ne3] I32, mask column i of slice i3 uses row kv_rows[i, i3] of k and v |
| 2525 | // negative rows are padding, their mask entries must be -inf |
| 2526 | GGML_API struct ggml_tensor * ggml_flash_attn_ext_rows( |
| 2527 | struct ggml_context * ctx, |
| 2528 | struct ggml_tensor * q, |
| 2529 | struct ggml_tensor * k, |
| 2530 | struct ggml_tensor * v, |
| 2531 | struct ggml_tensor * mask, |
| 2532 | struct ggml_tensor * kv_rows, |
| 2533 | float scale, |
| 2534 | float max_bias, |
| 2535 | float logit_softcap); |
| 2536 | |
| 2537 | GGML_DEPRECATED(GGML_API void ggml_flash_attn_ext_set_prec( |
| 2538 | struct ggml_tensor * a, |
| 2539 | enum ggml_prec prec), |
| 2540 | "use ggml_prec_set_acc() instead"); |
| 2541 | |
| 2542 | GGML_API enum ggml_prec ggml_flash_attn_ext_get_prec( |
| 2543 | const struct ggml_tensor * a); |
| 2544 | |
| 2545 | // Use finite mask entries as a sparse K/V set. Set 0 to disable. |
| 2546 | // n_kv_max must bound the number of finite entries in every mask row. |
| 2547 | GGML_API void ggml_flash_attn_ext_set_n_kv_max( |
| 2548 | struct ggml_tensor * a, |
| 2549 | int32_t n_kv_max); |
| 2550 | |
| 2551 | GGML_API void ggml_flash_attn_ext_add_sinks( |
| 2552 | struct ggml_tensor * a, |
| 2553 | struct ggml_tensor * sinks); |
| 2554 | |
| 2555 | // TODO: needs to be adapted to ggml_flash_attn_ext |
| 2556 | GGML_API struct ggml_tensor * ggml_flash_attn_back( |
| 2557 | struct ggml_context * ctx, |
| 2558 | struct ggml_tensor * q, |
| 2559 | struct ggml_tensor * k, |
| 2560 | struct ggml_tensor * v, |
| 2561 | struct ggml_tensor * d, |
| 2562 | bool masked); |
| 2563 | |
| 2564 | GGML_API struct ggml_tensor * ggml_ssm_conv( |
| 2565 | struct ggml_context * ctx, |
| 2566 | struct ggml_tensor * sx, |
| 2567 | struct ggml_tensor * c); |
| 2568 | |
| 2569 | GGML_API struct ggml_tensor * ggml_ssm_scan( |
| 2570 | struct ggml_context * ctx, |
| 2571 | struct ggml_tensor * s, |
| 2572 | struct ggml_tensor * x, |
| 2573 | struct ggml_tensor * dt, |
| 2574 | struct ggml_tensor * A, |
| 2575 | struct ggml_tensor * B, |
| 2576 | struct ggml_tensor * C, |
| 2577 | struct ggml_tensor * ids, |
| 2578 | int64_t K); |
| 2579 | |
| 2580 | // partition into non-overlapping windows with padding if needed |
| 2581 | // example: |
| 2582 | // a: 768 64 64 1 |
| 2583 | // w: 14 |
| 2584 | // res: 768 14 14 25 |
| 2585 | // used in sam |
| 2586 | GGML_API struct ggml_tensor * ggml_win_part( |
| 2587 | struct ggml_context * ctx, |
| 2588 | struct ggml_tensor * a, |
| 2589 | int w); |
| 2590 | |
| 2591 | // reverse of ggml_win_part |
| 2592 | // used in sam |
| 2593 | GGML_API struct ggml_tensor * ggml_win_unpart( |
| 2594 | struct ggml_context * ctx, |
| 2595 | struct ggml_tensor * a, |
| 2596 | int w0, |
| 2597 | int h0, |
| 2598 | int w); |
| 2599 | |
| 2600 | GGML_API struct ggml_tensor * ggml_unary( |
| 2601 | struct ggml_context * ctx, |
| 2602 | struct ggml_tensor * a, |
| 2603 | enum ggml_unary_op op); |
| 2604 | |
| 2605 | GGML_API struct ggml_tensor * ggml_unary_inplace( |
| 2606 | struct ggml_context * ctx, |
| 2607 | struct ggml_tensor * a, |
| 2608 | enum ggml_unary_op op); |
| 2609 | |
| 2610 | // used in sam |
| 2611 | GGML_API struct ggml_tensor * ggml_get_rel_pos( |
| 2612 | struct ggml_context * ctx, |
| 2613 | struct ggml_tensor * a, |
| 2614 | int qh, |
| 2615 | int kh); |
| 2616 | |
| 2617 | // used in sam |
| 2618 | GGML_API struct ggml_tensor * ggml_add_rel_pos( |
| 2619 | struct ggml_context * ctx, |
| 2620 | struct ggml_tensor * a, |
| 2621 | struct ggml_tensor * pw, |
| 2622 | struct ggml_tensor * ph); |
| 2623 | |
| 2624 | GGML_API struct ggml_tensor * ggml_add_rel_pos_inplace( |
| 2625 | struct ggml_context * ctx, |
| 2626 | struct ggml_tensor * a, |
| 2627 | struct ggml_tensor * pw, |
| 2628 | struct ggml_tensor * ph); |
| 2629 | |
| 2630 | GGML_API struct ggml_tensor * ggml_rwkv_wkv6( |
| 2631 | struct ggml_context * ctx, |
| 2632 | struct ggml_tensor * k, |
| 2633 | struct ggml_tensor * v, |
| 2634 | struct ggml_tensor * r, |
| 2635 | struct ggml_tensor * tf, |
| 2636 | struct ggml_tensor * td, |
| 2637 | struct ggml_tensor * state); |
| 2638 | |
| 2639 | GGML_API struct ggml_tensor * ggml_gated_linear_attn( |
| 2640 | struct ggml_context * ctx, |
| 2641 | struct ggml_tensor * k, |
| 2642 | struct ggml_tensor * v, |
| 2643 | struct ggml_tensor * q, |
| 2644 | struct ggml_tensor * g, |
| 2645 | struct ggml_tensor * state, |
| 2646 | float scale); |
| 2647 | |
| 2648 | GGML_API struct ggml_tensor * ggml_rwkv_wkv7( |
| 2649 | struct ggml_context * ctx, |
| 2650 | struct ggml_tensor * r, |
| 2651 | struct ggml_tensor * w, |
| 2652 | struct ggml_tensor * k, |
| 2653 | struct ggml_tensor * v, |
| 2654 | struct ggml_tensor * a, |
| 2655 | struct ggml_tensor * b, |
| 2656 | struct ggml_tensor * state); |
| 2657 | |
| 2658 | /* Solves a specific equation of the form Ax=B, where A is a triangular matrix |
| 2659 | * without zeroes on the diagonal (i.e. invertible). |
| 2660 | * B can have any number of columns, but must have the same number of rows as A |
| 2661 | * If A is [n, n] and B is [n, m], then the result will be [n, m] as well |
| 2662 | * Has O(n^3) complexity (unlike most matrix ops out there), so use on cases |
| 2663 | * where n > 100 sparingly, pre-chunk if necessary. |
| 2664 | * |
| 2665 | * If left = false, solves xA=B instead |
| 2666 | * If lower = false, assumes upper triangular instead |
| 2667 | * If uni = true, assumes diagonal of A to be all ones (will override actual values) |
| 2668 | * |
| 2669 | * TODO: currently only lower, right, non-unitriangular variant is implemented |
| 2670 | */ |
| 2671 | GGML_API struct ggml_tensor * ggml_solve_tri( |
| 2672 | struct ggml_context * ctx, |
| 2673 | struct ggml_tensor * a, |
| 2674 | struct ggml_tensor * b, |
| 2675 | bool left, |
| 2676 | bool lower, |
| 2677 | bool uni); |
| 2678 | |
| 2679 | // TODO: add ggml_gated_delta_net_set_bcast() to be able to configure Q, K broadcast type: tiled vs interleaved [TAG_GGML_GDN_BCAST] |
| 2680 | // ref: https://github.com/ggml-org/llama.cpp/pull/19468#discussion_r2786394306 |
| 2681 | // |
| 2682 | // tensor shapes (S_k == S_v, H_v % H_k == 0): |
| 2683 | // q, k : [S_k, H_k, n_tokens, n_seqs] |
| 2684 | // v : [S_v, H_v, n_tokens, n_seqs] |
| 2685 | // g : [1, H_v, n_tokens, n_seqs] (scalar gate) or [S_v, H_v, n_tokens, n_seqs] (KDA) |
| 2686 | // beta : [1, H_v, n_tokens, n_seqs] |
| 2687 | // state : [S_v, S_v, H_v, n_seqs] -- initial recurrent state s0 |
| 2688 | // |
| 2689 | // the output packs the attention scores [S_v, H_v, n_tokens, n_seqs] followed by K state |
| 2690 | // snapshots, most-recent first (slot 0 = final state, slot s = state s tokens back). K == 1 |
| 2691 | // keeps only the final state; when n_tokens < K only slots 0..n_tokens-1 are written. |
| 2692 | // when n_tokens > K only slot 0 is guaranteed; backends may leave slots 1..K-1 unwritten. |
| 2693 | GGML_API struct ggml_tensor * ggml_gated_delta_net( |
| 2694 | struct ggml_context * ctx, |
| 2695 | struct ggml_tensor * q, |
| 2696 | struct ggml_tensor * k, |
| 2697 | struct ggml_tensor * v, |
| 2698 | struct ggml_tensor * g, |
| 2699 | struct ggml_tensor * beta, |
| 2700 | struct ggml_tensor * state, |
| 2701 | int64_t K); |
| 2702 | |
| 2703 | // DSA lightning indexer |
| 2704 | // |
| 2705 | // q: [n_embd_idx, n_head_idx, n_batch, ne3 ] |
| 2706 | // k: [n_embd_idx, 1, n_kv, ne3 ] |
| 2707 | // weights: [n_head_idx, n_batch, 1, ne3 ] !! prescaled !! |
| 2708 | // mask: [n_kv, n_batch, 1, ne33] !! f16 !! |
| 2709 | // res: [n_kv, n_batch, 1, ne3 ] |
| 2710 | // |
| 2711 | // broadcast: |
| 2712 | // ne3 % ne33 == 0 |
| 2713 | // |
| 2714 | GGML_API struct ggml_tensor * ggml_lightning_indexer( |
| 2715 | struct ggml_context * ctx, |
| 2716 | struct ggml_tensor * q, |
| 2717 | struct ggml_tensor * k, |
| 2718 | struct ggml_tensor * weights, |
| 2719 | struct ggml_tensor * mask); |
| 2720 | |
| 2721 | // DeepSeek V4 hyper-connections (ref. https://arxiv.org/pdf/2512.24880) |
| 2722 | // In short these operations are replacements for the original residual connection (x = transformer(x) + x) |
| 2723 | // using a richer representation through streams. |
| 2724 | // |
| 2725 | // hc_comb: mixes [(2 + hc)*hc, n_tokens], scale [3], base [(2 + hc)*hc] |
| 2726 | // -> [dst_hc, src_hc, n_tokens] |
| 2727 | // logits[dst, src, t] = mixes[2*hc + dst + hc*src, t]*scale[2] |
| 2728 | // + base[2*hc + dst + hc*src] |
| 2729 | // Softmax over dst, add eps, normalize over src, then repeat normalization |
| 2730 | // over dst followed by src for iterations 1 through n_iter - 1. |
| 2731 | GGML_API struct ggml_tensor * ggml_dsv4_hc_comb( |
| 2732 | struct ggml_context * ctx, |
| 2733 | struct ggml_tensor * mixes, |
| 2734 | struct ggml_tensor * scale, |
| 2735 | struct ggml_tensor * base, |
| 2736 | float eps, |
| 2737 | int32_t n_iter); |
| 2738 | |
| 2739 | // hc_pre: x [n_embd, hc, n_tokens], weights [hc, n_tokens] -> [n_embd, n_tokens] |
| 2740 | // result[i, t] = sum_h x[i, h, t]*weights[h, t] |
| 2741 | // |
| 2742 | GGML_API struct ggml_tensor * ggml_dsv4_hc_pre( |
| 2743 | struct ggml_context * ctx, |
| 2744 | struct ggml_tensor * x, |
| 2745 | struct ggml_tensor * weights); |
| 2746 | |
| 2747 | // hc_pre with a per-element gate (Qwen3.8-Flash-Next): gate [n_embd, hc, n_tokens] |
| 2748 | // result[i, t] = scale*sum_h x[i, h, t]*sigmoid(gate[i, h, t]) |
| 2749 | // |
| 2750 | GGML_API struct ggml_tensor * ggml_dsv4_hc_pre_gated( |
| 2751 | struct ggml_context * ctx, |
| 2752 | struct ggml_tensor * x, |
| 2753 | struct ggml_tensor * gate, |
| 2754 | float scale); |
| 2755 | |
| 2756 | // hc_post: x [n_embd, n_tokens], residual [n_embd, hc, n_tokens], |
| 2757 | // post [hc, n_tokens], comb [dst_hc, src_hc, n_tokens] |
| 2758 | // -> [n_embd, hc, n_tokens] |
| 2759 | // result[i, dst, t] = x[i, t]*post[dst, t] |
| 2760 | // + sum_src residual[i, src, t]*comb[dst, src, t] |
| 2761 | // comb == NULL uses the identity: result[i, dst, t] = x[i, t]*post[dst, t] + residual[i, dst, t] |
| 2762 | // |
| 2763 | GGML_API struct ggml_tensor * ggml_dsv4_hc_post( |
| 2764 | struct ggml_context * ctx, |
| 2765 | struct ggml_tensor * x, |
| 2766 | struct ggml_tensor * residual, |
| 2767 | struct ggml_tensor * post, |
| 2768 | struct ggml_tensor * comb); |
| 2769 | |
| 2770 | // top-k sparse attention mask: keep the mask cells of the live picks and the extra cells, set the rest to -inf |
| 2771 | // |
| 2772 | // mask: [n_kv, m1, m2, m3] f16 or f32, broadcast to the rows of top_k |
| 2773 | // top_k: [n_top, ne1, ne2, ne3] i32, picks of each row |
| 2774 | // score: [n_pick, ne1, ne2, ne3] f32, can be NULL: a pick is live when its score is above -inf, else every pick is live |
| 2775 | // blocks: [n_blk, n_pick] i32, can be NULL: the member cells of each pick, else a pick is a cell |
| 2776 | // extra: [n_extra, ne1, ne2, ne3] i32, can be NULL: cells of each row kept without a pick |
| 2777 | // res: [n_kv, ne1, ne2, ne3] same type as mask |
| 2778 | // |
| 2779 | // res[c, i] = mask[c, i % m] when c is a cell of a live pick of row i or an extra cell of row i, else -inf |
| 2780 | // picks and cells out of range are skipped |
| 2781 | // |
| 2782 | GGML_API struct ggml_tensor * ggml_top_k_mask( |
| 2783 | struct ggml_context * ctx, |
| 2784 | struct ggml_tensor * mask, |
| 2785 | struct ggml_tensor * top_k, |
| 2786 | struct ggml_tensor * score, |
| 2787 | struct ggml_tensor * blocks, |
| 2788 | struct ggml_tensor * extra); |
| 2789 | |
| 2790 | // custom operators |
| 2791 | |
| 2792 | typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata); |
| 2793 | 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); |
| 2794 | 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); |
| 2795 | |
| 2796 | #define GGML_N_TASKS_MAX (-1) |
| 2797 | // n_tasks == GGML_N_TASKS_MAX means to use max number of tasks |
| 2798 | |
| 2799 | GGML_API struct ggml_tensor * ggml_map_custom1( |
| 2800 | struct ggml_context * ctx, |
| 2801 | struct ggml_tensor * a, |
| 2802 | ggml_custom1_op_t fun, |
| 2803 | int n_tasks, |
| 2804 | void * userdata); |
| 2805 | |
| 2806 | GGML_API struct ggml_tensor * ggml_map_custom1_inplace( |
| 2807 | struct ggml_context * ctx, |
| 2808 | struct ggml_tensor * a, |
| 2809 | ggml_custom1_op_t fun, |
| 2810 | int n_tasks, |
| 2811 | void * userdata); |
| 2812 | |
| 2813 | GGML_API struct ggml_tensor * ggml_map_custom2( |
| 2814 | struct ggml_context * ctx, |
| 2815 | struct ggml_tensor * a, |
| 2816 | struct ggml_tensor * b, |
| 2817 | ggml_custom2_op_t fun, |
| 2818 | int n_tasks, |
| 2819 | void * userdata); |
| 2820 | |
| 2821 | GGML_API struct ggml_tensor * ggml_map_custom2_inplace( |
| 2822 | struct ggml_context * ctx, |
| 2823 | struct ggml_tensor * a, |
| 2824 | struct ggml_tensor * b, |
| 2825 | ggml_custom2_op_t fun, |
| 2826 | int n_tasks, |
| 2827 | void * userdata); |
| 2828 | |
| 2829 | GGML_API struct ggml_tensor * ggml_map_custom3( |
| 2830 | struct ggml_context * ctx, |
| 2831 | struct ggml_tensor * a, |
| 2832 | struct ggml_tensor * b, |
| 2833 | struct ggml_tensor * c, |
| 2834 | ggml_custom3_op_t fun, |
| 2835 | int n_tasks, |
| 2836 | void * userdata); |
| 2837 | |
| 2838 | GGML_API struct ggml_tensor * ggml_map_custom3_inplace( |
| 2839 | struct ggml_context * ctx, |
| 2840 | struct ggml_tensor * a, |
| 2841 | struct ggml_tensor * b, |
| 2842 | struct ggml_tensor * c, |
| 2843 | ggml_custom3_op_t fun, |
| 2844 | int n_tasks, |
| 2845 | void * userdata); |
| 2846 | |
| 2847 | typedef void (*ggml_custom_op_t)(struct ggml_tensor * dst , int ith, int nth, void * userdata); |
| 2848 | |
| 2849 | GGML_API struct ggml_tensor * ggml_custom_4d( |
| 2850 | struct ggml_context * ctx, |
| 2851 | enum ggml_type type, |
| 2852 | int64_t ne0, |
| 2853 | int64_t ne1, |
| 2854 | int64_t ne2, |
| 2855 | int64_t ne3, |
| 2856 | struct ggml_tensor ** args, |
| 2857 | int n_args, |
| 2858 | ggml_custom_op_t fun, |
| 2859 | int n_tasks, |
| 2860 | void * userdata); |
| 2861 | |
| 2862 | GGML_API struct ggml_tensor * ggml_custom_inplace( |
| 2863 | struct ggml_context * ctx, |
| 2864 | struct ggml_tensor * a, |
| 2865 | struct ggml_tensor ** args, |
| 2866 | int n_args, |
| 2867 | ggml_custom_op_t fun, |
| 2868 | int n_tasks, |
| 2869 | void * userdata); |
| 2870 | |
| 2871 | // loss function |
| 2872 | |
| 2873 | GGML_API struct ggml_tensor * ggml_cross_entropy_loss( |
| 2874 | struct ggml_context * ctx, |
| 2875 | struct ggml_tensor * a, // logits |
| 2876 | struct ggml_tensor * b); // labels |
| 2877 | |
| 2878 | GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back( |
| 2879 | struct ggml_context * ctx, |
| 2880 | struct ggml_tensor * a, // logits |
| 2881 | struct ggml_tensor * b, // labels |
| 2882 | struct ggml_tensor * c); // gradients of cross_entropy_loss result |
| 2883 | |
| 2884 | // AdamW optimizer step |
| 2885 | // Paper: https://arxiv.org/pdf/1711.05101v3.pdf |
| 2886 | // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html |
| 2887 | GGML_API struct ggml_tensor * ggml_opt_step_adamw( |
| 2888 | struct ggml_context * ctx, |
| 2889 | struct ggml_tensor * a, |
| 2890 | struct ggml_tensor * grad, |
| 2891 | struct ggml_tensor * m, |
| 2892 | struct ggml_tensor * v, |
| 2893 | struct ggml_tensor * adamw_params); // parameters such as the learning rate |
| 2894 | |
| 2895 | // stochastic gradient descent step (with weight decay) |
| 2896 | GGML_API struct ggml_tensor * ggml_opt_step_sgd( |
| 2897 | struct ggml_context * ctx, |
| 2898 | struct ggml_tensor * a, |
| 2899 | struct ggml_tensor * grad, |
| 2900 | struct ggml_tensor * sgd_params); // alpha, weight decay |
| 2901 | |
| 2902 | // build forward multiple tensors and select one of them for computing |
| 2903 | // this is useful for creating graphs that have constant topology but compute different things based on the input |
| 2904 | // ref: https://github.com/ggml-org/llama.cpp/pull/18550 |
| 2905 | // |
| 2906 | // nodes: |
| 2907 | // | - build forward into the graph but do not compute |
| 2908 | // c - build forward into the graph and compute |
| 2909 | // |
| 2910 | // | | ... c ... | |
| 2911 | // | | ... c ... | |
| 2912 | // | | ... c ... | |
| 2913 | // [0 1 ... idx ... n-1] <-- ggml_build_forward_select(..., n, idx) |
| 2914 | // c |
| 2915 | // c |
| 2916 | // |
| 2917 | // example: |
| 2918 | // struct ggml_tensor * curs[3]; |
| 2919 | // |
| 2920 | // curs[0] = compute0(...); |
| 2921 | // curs[1] = compute1(...); |
| 2922 | // curs[2] = compute2(...); |
| 2923 | // |
| 2924 | // int idx = select_branch(some_input); |
| 2925 | // |
| 2926 | // struct ggml_tensor * out = ggml_build_forward_select(cgraph, curs, 3, idx); |
| 2927 | // |
| 2928 | GGML_API struct ggml_tensor * ggml_build_forward_select( |
| 2929 | struct ggml_cgraph * cgraph, |
| 2930 | struct ggml_tensor ** tensors, |
| 2931 | int n_tensors, |
| 2932 | int idx); |
| 2933 | |
| 2934 | GGML_API void ggml_build_forward_expand( |
| 2935 | struct ggml_cgraph * cgraph, |
| 2936 | struct ggml_tensor * tensor); |
| 2937 | |
| 2938 | // add the tensor and its parents to the graph without marking them for compute |
| 2939 | // the flag is set later, when the tensor is reached from a node that computes |
| 2940 | GGML_API void ggml_build_forward_order( |
| 2941 | struct ggml_cgraph * cgraph, |
| 2942 | struct ggml_tensor * tensor); |
| 2943 | |
| 2944 | GGML_API void ggml_build_backward_expand( |
| 2945 | struct ggml_context * ctx, // context for gradient computation |
| 2946 | struct ggml_cgraph * cgraph, |
| 2947 | struct ggml_tensor ** grad_accs); |
| 2948 | |
| 2949 | // graph allocation in a context |
| 2950 | GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx); // size = GGML_DEFAULT_GRAPH_SIZE, grads = false |
| 2951 | GGML_API struct ggml_cgraph * ggml_new_graph_custom(struct ggml_context * ctx, size_t size, bool grads); |
| 2952 | GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, struct ggml_cgraph * cgraph, bool force_grads); |
| 2953 | GGML_API void ggml_graph_cpy (struct ggml_cgraph * src, struct ggml_cgraph * dst); |
| 2954 | GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1 |
| 2955 | GGML_API void ggml_graph_clear (struct ggml_cgraph * cgraph); |
| 2956 | |
| 2957 | GGML_API int ggml_graph_size (struct ggml_cgraph * cgraph); |
| 2958 | GGML_API struct ggml_tensor * ggml_graph_node (struct ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i] |
| 2959 | GGML_API struct ggml_tensor ** ggml_graph_nodes (struct ggml_cgraph * cgraph); |
| 2960 | GGML_API int ggml_graph_n_nodes(struct ggml_cgraph * cgraph); |
| 2961 | |
| 2962 | GGML_API void ggml_graph_add_node(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor); |
| 2963 | |
| 2964 | GGML_API size_t ggml_graph_overhead(void); |
| 2965 | GGML_API size_t ggml_graph_overhead_custom(size_t size, bool grads); |
| 2966 | |
| 2967 | GGML_API struct ggml_tensor * ggml_graph_get_tensor (const struct ggml_cgraph * cgraph, const char * name); |
| 2968 | GGML_API struct ggml_tensor * ggml_graph_get_grad (const struct ggml_cgraph * cgraph, const struct ggml_tensor * node); |
| 2969 | GGML_API struct ggml_tensor * ggml_graph_get_grad_acc(const struct ggml_cgraph * cgraph, const struct ggml_tensor * node); |
| 2970 | |
| 2971 | // print info and performance information for the graph |
| 2972 | GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph); |
| 2973 | |
| 2974 | // dump the graph into a file using the dot format |
| 2975 | GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * cgraph, const char * filename); |
| 2976 | |
| 2977 | // TODO these functions were sandwiched in the old optimization interface, is there a better place for them? |
| 2978 | typedef void (*ggml_log_callback)(enum ggml_log_level level, const char * text, void * user_data); |
| 2979 | |
| 2980 | // Set callback for all future logging events. |
| 2981 | // If this is not called, or NULL is supplied, everything is output on stderr. |
| 2982 | GGML_API void ggml_log_get(ggml_log_callback * log_callback, void ** user_data); |
| 2983 | GGML_API void ggml_log_set(ggml_log_callback log_callback, void * user_data); |
| 2984 | |
| 2985 | GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor); |
| 2986 | |
| 2987 | // |
| 2988 | // quantization |
| 2989 | // |
| 2990 | |
| 2991 | // - ggml_quantize_init can be called multiple times with the same type |
| 2992 | // it will only initialize the quantization tables for the first call or after ggml_quantize_free |
| 2993 | // automatically called by ggml_quantize_chunk for convenience |
| 2994 | // |
| 2995 | // - ggml_quantize_free will free any memory allocated by ggml_quantize_init |
| 2996 | // call this at the end of the program to avoid memory leaks |
| 2997 | // |
| 2998 | // note: these are thread-safe |
| 2999 | // |
| 3000 | GGML_API void ggml_quantize_init(enum ggml_type type); |
| 3001 | GGML_API void ggml_quantize_free(void); |
| 3002 | |
| 3003 | // some quantization type cannot be used without an importance matrix |
| 3004 | GGML_API bool ggml_quantize_requires_imatrix(enum ggml_type type); |
| 3005 | |
| 3006 | // calls ggml_quantize_init internally (i.e. can allocate memory) |
| 3007 | GGML_API size_t ggml_quantize_chunk( |
| 3008 | enum ggml_type type, |
| 3009 | const float * src, |
| 3010 | void * dst, |
| 3011 | int64_t start, |
| 3012 | int64_t nrows, |
| 3013 | int64_t n_per_row, |
| 3014 | const float * imatrix); |
| 3015 | |
| 3016 | #ifdef __cplusplus |
| 3017 | // restrict not standard in C++ |
| 3018 | # if defined(__GNUC__) |
| 3019 | # define GGML_RESTRICT __restrict__ |
| 3020 | # elif defined(__clang__) |
| 3021 | # define GGML_RESTRICT __restrict |
| 3022 | # elif defined(_MSC_VER) |
| 3023 | # define GGML_RESTRICT __restrict |
| 3024 | # else |
| 3025 | # define GGML_RESTRICT |
| 3026 | # endif |
| 3027 | #else |
| 3028 | # if defined (_MSC_VER) && (__STDC_VERSION__ < 201112L) |
| 3029 | # define GGML_RESTRICT __restrict |
| 3030 | # else |
| 3031 | # define GGML_RESTRICT restrict |
| 3032 | # endif |
| 3033 | #endif |
| 3034 | typedef void (*ggml_to_float_t) (const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k); |
| 3035 | typedef void (*ggml_from_float_t)(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k); |
| 3036 | |
| 3037 | struct ggml_type_traits { |
| 3038 | const char * type_name; |
| 3039 | int64_t blck_size; |
| 3040 | int64_t blck_size_interleave; // interleave elements in blocks |
| 3041 | size_t type_size; |
| 3042 | bool is_quantized; |
| 3043 | ggml_to_float_t to_float; |
| 3044 | ggml_from_float_t from_float_ref; |
| 3045 | }; |
| 3046 | |
| 3047 | GGML_API const struct ggml_type_traits * ggml_get_type_traits(enum ggml_type type); |
| 3048 | |
| 3049 | // ggml threadpool |
| 3050 | // TODO: currently, only a few functions are in the base ggml API, while the rest are in the CPU backend |
| 3051 | // the goal should be to create an API that other backends can use move everything to the ggml base |
| 3052 | |
| 3053 | // scheduling priorities |
| 3054 | enum ggml_sched_priority { |
| 3055 | GGML_SCHED_PRIO_LOW = -1, |
| 3056 | GGML_SCHED_PRIO_NORMAL, |
| 3057 | GGML_SCHED_PRIO_MEDIUM, |
| 3058 | GGML_SCHED_PRIO_HIGH, |
| 3059 | GGML_SCHED_PRIO_REALTIME |
| 3060 | }; |
| 3061 | |
| 3062 | // threadpool params |
| 3063 | // Use ggml_threadpool_params_default() or ggml_threadpool_params_init() to populate the defaults |
| 3064 | struct ggml_threadpool_params { |
| 3065 | bool cpumask[GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings) |
| 3066 | int n_threads; // number of threads |
| 3067 | enum ggml_sched_priority prio; // thread priority |
| 3068 | uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling) |
| 3069 | bool strict_cpu; // strict cpu placement |
| 3070 | bool paused; // start in paused state |
| 3071 | }; |
| 3072 | |
| 3073 | struct ggml_threadpool; // forward declaration, see ggml.c |
| 3074 | |
| 3075 | typedef struct ggml_threadpool * ggml_threadpool_t; |
| 3076 | |
| 3077 | GGML_API struct ggml_threadpool_params ggml_threadpool_params_default(int n_threads); |
| 3078 | GGML_API void ggml_threadpool_params_init (struct ggml_threadpool_params * p, int n_threads); |
| 3079 | GGML_API bool ggml_threadpool_params_match (const struct ggml_threadpool_params * p0, const struct ggml_threadpool_params * p1); |
| 3080 | |
| 3081 | #ifdef __cplusplus |
| 3082 | } |
| 3083 | #endif |