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