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