cui-llama.rn 1.3.0 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/android/src/main/CMakeLists.txt +6 -1
  2. package/android/src/main/jni.cpp +6 -6
  3. package/cpp/amx/amx.cpp +196 -0
  4. package/cpp/amx/amx.h +20 -0
  5. package/cpp/amx/common.h +101 -0
  6. package/cpp/amx/mmq.cpp +2524 -0
  7. package/cpp/amx/mmq.h +16 -0
  8. package/cpp/common.cpp +1981 -1682
  9. package/cpp/common.h +636 -600
  10. package/cpp/ggml-aarch64.c +129 -129
  11. package/cpp/ggml-aarch64.h +19 -19
  12. package/cpp/ggml-alloc.c +1038 -1040
  13. package/cpp/ggml-alloc.h +76 -76
  14. package/cpp/ggml-backend-impl.h +238 -216
  15. package/cpp/ggml-backend-reg.cpp +423 -195
  16. package/cpp/ggml-backend.cpp +1999 -1997
  17. package/cpp/ggml-backend.h +351 -328
  18. package/cpp/ggml-common.h +1859 -1853
  19. package/cpp/ggml-cpp.h +38 -38
  20. package/cpp/ggml-cpu-aarch64.c +3823 -3560
  21. package/cpp/ggml-cpu-aarch64.h +32 -30
  22. package/cpp/ggml-cpu-impl.h +386 -371
  23. package/cpp/ggml-cpu-quants.c +10835 -10822
  24. package/cpp/ggml-cpu-quants.h +63 -63
  25. package/cpp/ggml-cpu.c +99 -103
  26. package/cpp/ggml-cpu.cpp +69 -17
  27. package/cpp/ggml-cpu.h +152 -177
  28. package/cpp/ggml-impl.h +556 -550
  29. package/cpp/ggml-metal.h +66 -66
  30. package/cpp/ggml-metal.m +4426 -4294
  31. package/cpp/ggml-quants.c +5247 -5247
  32. package/cpp/ggml-quants.h +100 -100
  33. package/cpp/ggml-threading.cpp +12 -12
  34. package/cpp/ggml-threading.h +12 -12
  35. package/cpp/ggml.c +7618 -8180
  36. package/cpp/ggml.h +2255 -2411
  37. package/cpp/json-schema-to-grammar.cpp +1045 -0
  38. package/cpp/json-schema-to-grammar.h +8 -0
  39. package/cpp/json.hpp +24766 -0
  40. package/cpp/llama-grammar.cpp +1138 -1138
  41. package/cpp/llama-grammar.h +144 -144
  42. package/cpp/llama-impl.h +181 -181
  43. package/cpp/llama-sampling.cpp +2348 -2348
  44. package/cpp/llama-sampling.h +48 -48
  45. package/cpp/llama-vocab.cpp +1984 -1984
  46. package/cpp/llama-vocab.h +170 -170
  47. package/cpp/llama.cpp +22332 -22132
  48. package/cpp/llama.h +1259 -1253
  49. package/cpp/log.cpp +401 -401
  50. package/cpp/log.h +121 -121
  51. package/cpp/rn-llama.hpp +6 -6
  52. package/cpp/sampling.cpp +505 -466
  53. package/cpp/sampling.h +22 -1
  54. package/cpp/sgemm.cpp +1884 -1884
  55. package/cpp/speculative.cpp +270 -0
  56. package/cpp/speculative.h +28 -0
  57. package/cpp/unicode.cpp +11 -0
  58. package/ios/RNLlamaContext.mm +13 -0
  59. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  60. package/lib/commonjs/grammar.js +4 -2
  61. package/lib/commonjs/grammar.js.map +1 -1
  62. package/lib/commonjs/index.js.map +1 -1
  63. package/lib/module/NativeRNLlama.js.map +1 -1
  64. package/lib/module/grammar.js +2 -1
  65. package/lib/module/grammar.js.map +1 -1
  66. package/lib/module/index.js.map +1 -1
  67. package/lib/typescript/NativeRNLlama.d.ts +94 -4
  68. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  69. package/lib/typescript/grammar.d.ts +5 -6
  70. package/lib/typescript/grammar.d.ts.map +1 -1
  71. package/lib/typescript/index.d.ts +4 -2
  72. package/lib/typescript/index.d.ts.map +1 -1
  73. package/package.json +2 -1
  74. package/src/NativeRNLlama.ts +97 -10
  75. package/src/grammar.ts +10 -8
  76. package/src/index.ts +22 -1
package/cpp/ggml.h CHANGED
@@ -1,2411 +1,2255 @@
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/ggerganov/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 lm_ggml_init_params params = {
36
- // .mem_size = 16*1024*1024,
37
- // .mem_buffer = NULL,
38
- // };
39
- //
40
- // // memory allocation happens here
41
- // struct lm_ggml_context * ctx = lm_ggml_init(params);
42
- //
43
- // struct lm_ggml_tensor * x = lm_ggml_new_tensor_1d(ctx, LM_GGML_TYPE_F32, 1);
44
- //
45
- // lm_ggml_set_param(ctx, x); // x is an input variable
46
- //
47
- // struct lm_ggml_tensor * a = lm_ggml_new_tensor_1d(ctx, LM_GGML_TYPE_F32, 1);
48
- // struct lm_ggml_tensor * b = lm_ggml_new_tensor_1d(ctx, LM_GGML_TYPE_F32, 1);
49
- // struct lm_ggml_tensor * x2 = lm_ggml_mul(ctx, x, x);
50
- // struct lm_ggml_tensor * f = lm_ggml_add(ctx, lm_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 lm_ggml_cgraph * gf = lm_ggml_new_graph(ctx);
62
- // lm_ggml_build_forward_expand(gf, f);
63
- //
64
- // // set the input variable and parameter values
65
- // lm_ggml_set_f32(x, 2.0f);
66
- // lm_ggml_set_f32(a, 3.0f);
67
- // lm_ggml_set_f32(b, 4.0f);
68
- //
69
- // lm_ggml_graph_compute_with_ctx(ctx, &gf, n_threads);
70
- //
71
- // printf("f = %f\n", lm_ggml_get_f32_1d(f, 0));
72
- //
73
- // ...
74
- // }
75
- //
76
- // The actual computation is performed in the lm_ggml_graph_compute() function.
77
- //
78
- // The lm_ggml_new_tensor_...() functions create new tensors. They are allocated in the memory buffer provided to the
79
- // lm_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 lm_ggml_used_mem() function to find out how much memory was
82
- // actually needed.
83
- //
84
- // The lm_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 lm_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
- // - lm_ggml_permute()
100
- // - lm_ggml_conv_1d_1s()
101
- // - lm_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 lm_ggml_tensor)
113
- //
114
- // The tensors are stored in memory via the lm_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 lm_ggml_tensor * c = lm_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 lm_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 lm_ggml_tensor * a = lm_ggml_new_tensor_2d(ctx, LM_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 lm_ggml_get_f32_1d() and lm_ggml_set_f32_1d() that can be used.
149
- //
150
- // ## The matrix multiplication operator (lm_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 LM_GGML_SHARED
177
- # if defined(_WIN32) && !defined(__MINGW32__)
178
- # ifdef LM_GGML_BUILD
179
- # define LM_GGML_API __declspec(dllexport) extern
180
- # else
181
- # define LM_GGML_API __declspec(dllimport) extern
182
- # endif
183
- # else
184
- # define LM_GGML_API __attribute__ ((visibility ("default"))) extern
185
- # endif
186
- #else
187
- # define LM_GGML_API extern
188
- #endif
189
-
190
- // TODO: support for clang
191
- #ifdef __GNUC__
192
- # define LM_GGML_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
193
- #elif defined(_MSC_VER)
194
- # define LM_GGML_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
195
- #else
196
- # define LM_GGML_DEPRECATED(func, hint) func
197
- #endif
198
-
199
- #ifndef __GNUC__
200
- # define LM_GGML_ATTRIBUTE_FORMAT(...)
201
- #elif defined(__MINGW32__)
202
- # define LM_GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
203
- #else
204
- # define LM_GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
205
- #endif
206
-
207
- #include <stdbool.h>
208
- #include <stddef.h>
209
- #include <stdint.h>
210
- #include <stdio.h>
211
- #include <string.h>
212
-
213
- #define LM_GGML_FILE_MAGIC 0x67676d6c // "ggml"
214
- #define LM_GGML_FILE_VERSION 2
215
-
216
- #define LM_GGML_QNT_VERSION 2 // bump this on quantization format changes
217
- #define LM_GGML_QNT_VERSION_FACTOR 1000 // do not change this
218
-
219
- #define LM_GGML_MAX_DIMS 4
220
- #define LM_GGML_MAX_PARAMS 2048
221
- #define LM_GGML_MAX_SRC 10
222
- #define LM_GGML_MAX_N_THREADS 512
223
- #define LM_GGML_MAX_OP_PARAMS 64
224
-
225
- #ifndef LM_GGML_MAX_NAME
226
- # define LM_GGML_MAX_NAME 64
227
- #endif
228
-
229
- #define LM_GGML_DEFAULT_N_THREADS 4
230
- #define LM_GGML_DEFAULT_GRAPH_SIZE 2048
231
-
232
- #if UINTPTR_MAX == 0xFFFFFFFF
233
- #define LM_GGML_MEM_ALIGN 4
234
- #else
235
- #define LM_GGML_MEM_ALIGN 16
236
- #endif
237
-
238
- #define LM_GGML_EXIT_SUCCESS 0
239
- #define LM_GGML_EXIT_ABORTED 1
240
-
241
- #define LM_GGML_ROPE_TYPE_NEOX 2
242
-
243
- #define LM_GGUF_MAGIC "GGUF"
244
-
245
- #define LM_GGUF_VERSION 3
246
-
247
- #define LM_GGUF_DEFAULT_ALIGNMENT 32
248
-
249
- #define LM_GGML_UNUSED(x) (void)(x)
250
-
251
- #define LM_GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
252
-
253
- #ifndef NDEBUG
254
- # define LM_GGML_UNREACHABLE() do { fprintf(stderr, "statement should be unreachable\n"); abort(); } while(0)
255
- #elif defined(__GNUC__)
256
- # define LM_GGML_UNREACHABLE() __builtin_unreachable()
257
- #elif defined(_MSC_VER)
258
- # define LM_GGML_UNREACHABLE() __assume(0)
259
- #else
260
- # define LM_GGML_UNREACHABLE() ((void) 0)
261
- #endif
262
-
263
- #ifdef __cplusplus
264
- # define LM_GGML_NORETURN [[noreturn]]
265
- #elif defined(_MSC_VER)
266
- # define LM_GGML_NORETURN __declspec(noreturn)
267
- #else
268
- # define LM_GGML_NORETURN _Noreturn
269
- #endif
270
-
271
- #define LM_GGML_ABORT(...) lm_ggml_abort((strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __VA_ARGS__)
272
- #define LM_GGML_ASSERT(x) if (!(x)) LM_GGML_ABORT("LM_GGML_ASSERT(%s) failed", #x)
273
-
274
- // used to copy the number of elements and stride in bytes of tensors into local variables.
275
- // main purpose is to reduce code duplication and improve readability.
276
- //
277
- // example:
278
- //
279
- // LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
280
- // LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
281
- //
282
- #define LM_GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
283
- const type prefix##0 = (pointer)->array[0]; \
284
- LM_GGML_UNUSED(prefix##0);
285
- #define LM_GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
286
- LM_GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
287
- const type prefix##1 = (pointer)->array[1]; \
288
- LM_GGML_UNUSED(prefix##1);
289
- #define LM_GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
290
- LM_GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
291
- const type prefix##2 = (pointer)->array[2]; \
292
- LM_GGML_UNUSED(prefix##2);
293
- #define LM_GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
294
- LM_GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
295
- const type prefix##3 = (pointer)->array[3]; \
296
- LM_GGML_UNUSED(prefix##3);
297
-
298
- #define LM_GGML_TENSOR_UNARY_OP_LOCALS \
299
- LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
300
- LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
301
- LM_GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
302
- LM_GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
303
-
304
- #define LM_GGML_TENSOR_BINARY_OP_LOCALS \
305
- LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
306
- LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
307
- LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
308
- LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
309
- LM_GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
310
- LM_GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
311
-
312
- #define LM_GGML_TENSOR_BINARY_OP_LOCALS01 \
313
- LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
314
- LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
315
- LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
316
- LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
317
-
318
- #ifdef __cplusplus
319
- extern "C" {
320
- #endif
321
-
322
- LM_GGML_NORETURN LM_GGML_ATTRIBUTE_FORMAT(3, 4)
323
- LM_GGML_API void lm_ggml_abort(const char * file, int line, const char * fmt, ...);
324
-
325
- enum lm_ggml_status {
326
- LM_GGML_STATUS_ALLOC_FAILED = -2,
327
- LM_GGML_STATUS_FAILED = -1,
328
- LM_GGML_STATUS_SUCCESS = 0,
329
- LM_GGML_STATUS_ABORTED = 1,
330
- };
331
-
332
- // get lm_ggml_status name string
333
- LM_GGML_API const char * lm_ggml_status_to_string(enum lm_ggml_status status);
334
-
335
- // ieee 754-2008 half-precision float16
336
- // todo: make this not an integral type
337
- typedef uint16_t lm_ggml_fp16_t;
338
- LM_GGML_API float lm_ggml_fp16_to_fp32(lm_ggml_fp16_t);
339
- LM_GGML_API lm_ggml_fp16_t lm_ggml_fp32_to_fp16(float);
340
- LM_GGML_API void lm_ggml_fp16_to_fp32_row(const lm_ggml_fp16_t *, float *, int64_t);
341
- LM_GGML_API void lm_ggml_fp32_to_fp16_row(const float *, lm_ggml_fp16_t *, int64_t);
342
-
343
- // google brain half-precision bfloat16
344
- typedef struct { uint16_t bits; } lm_ggml_bf16_t;
345
- LM_GGML_API lm_ggml_bf16_t lm_ggml_fp32_to_bf16(float);
346
- LM_GGML_API float lm_ggml_bf16_to_fp32(lm_ggml_bf16_t); // consider just doing << 16
347
- LM_GGML_API void lm_ggml_bf16_to_fp32_row(const lm_ggml_bf16_t *, float *, int64_t);
348
- LM_GGML_API void lm_ggml_fp32_to_bf16_row_ref(const float *, lm_ggml_bf16_t *, int64_t);
349
- LM_GGML_API void lm_ggml_fp32_to_bf16_row(const float *, lm_ggml_bf16_t *, int64_t);
350
-
351
- struct lm_ggml_object;
352
- struct lm_ggml_context;
353
- struct lm_ggml_cgraph;
354
-
355
- // NOTE: always add types at the end of the enum to keep backward compatibility
356
- enum lm_ggml_type {
357
- LM_GGML_TYPE_F32 = 0,
358
- LM_GGML_TYPE_F16 = 1,
359
- LM_GGML_TYPE_Q4_0 = 2,
360
- LM_GGML_TYPE_Q4_1 = 3,
361
- // LM_GGML_TYPE_Q4_2 = 4, support has been removed
362
- // LM_GGML_TYPE_Q4_3 = 5, support has been removed
363
- LM_GGML_TYPE_Q5_0 = 6,
364
- LM_GGML_TYPE_Q5_1 = 7,
365
- LM_GGML_TYPE_Q8_0 = 8,
366
- LM_GGML_TYPE_Q8_1 = 9,
367
- LM_GGML_TYPE_Q2_K = 10,
368
- LM_GGML_TYPE_Q3_K = 11,
369
- LM_GGML_TYPE_Q4_K = 12,
370
- LM_GGML_TYPE_Q5_K = 13,
371
- LM_GGML_TYPE_Q6_K = 14,
372
- LM_GGML_TYPE_Q8_K = 15,
373
- LM_GGML_TYPE_IQ2_XXS = 16,
374
- LM_GGML_TYPE_IQ2_XS = 17,
375
- LM_GGML_TYPE_IQ3_XXS = 18,
376
- LM_GGML_TYPE_IQ1_S = 19,
377
- LM_GGML_TYPE_IQ4_NL = 20,
378
- LM_GGML_TYPE_IQ3_S = 21,
379
- LM_GGML_TYPE_IQ2_S = 22,
380
- LM_GGML_TYPE_IQ4_XS = 23,
381
- LM_GGML_TYPE_I8 = 24,
382
- LM_GGML_TYPE_I16 = 25,
383
- LM_GGML_TYPE_I32 = 26,
384
- LM_GGML_TYPE_I64 = 27,
385
- LM_GGML_TYPE_F64 = 28,
386
- LM_GGML_TYPE_IQ1_M = 29,
387
- LM_GGML_TYPE_BF16 = 30,
388
- LM_GGML_TYPE_Q4_0_4_4 = 31,
389
- LM_GGML_TYPE_Q4_0_4_8 = 32,
390
- LM_GGML_TYPE_Q4_0_8_8 = 33,
391
- LM_GGML_TYPE_TQ1_0 = 34,
392
- LM_GGML_TYPE_TQ2_0 = 35,
393
- LM_GGML_TYPE_COUNT,
394
- };
395
-
396
- // precision
397
- enum lm_ggml_prec {
398
- LM_GGML_PREC_DEFAULT,
399
- LM_GGML_PREC_F32,
400
- };
401
-
402
- enum lm_ggml_backend_type {
403
- LM_GGML_BACKEND_TYPE_CPU = 0,
404
- LM_GGML_BACKEND_TYPE_GPU = 10,
405
- LM_GGML_BACKEND_TYPE_GPU_SPLIT = 20,
406
- };
407
-
408
- // model file types
409
- enum lm_ggml_ftype {
410
- LM_GGML_FTYPE_UNKNOWN = -1,
411
- LM_GGML_FTYPE_ALL_F32 = 0,
412
- LM_GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
413
- LM_GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
414
- LM_GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
415
- LM_GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
416
- LM_GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
417
- LM_GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
418
- LM_GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
419
- LM_GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
420
- LM_GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
421
- LM_GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
422
- LM_GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
423
- LM_GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
424
- LM_GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors
425
- LM_GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors
426
- LM_GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors
427
- LM_GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors
428
- LM_GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors
429
- LM_GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors
430
- LM_GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors
431
- LM_GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors
432
- LM_GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors
433
- LM_GGML_FTYPE_MOSTLY_BF16 = 24, // except 1d tensors
434
- LM_GGML_FTYPE_MOSTLY_Q4_0_4_4 = 25, // except 1d tensors
435
- LM_GGML_FTYPE_MOSTLY_Q4_0_4_8 = 26, // except 1d tensors
436
- LM_GGML_FTYPE_MOSTLY_Q4_0_8_8 = 27, // except 1d tensors
437
- };
438
-
439
- // available tensor operations:
440
- enum lm_ggml_op {
441
- LM_GGML_OP_NONE = 0,
442
-
443
- LM_GGML_OP_DUP,
444
- LM_GGML_OP_ADD,
445
- LM_GGML_OP_ADD1,
446
- LM_GGML_OP_ACC,
447
- LM_GGML_OP_SUB,
448
- LM_GGML_OP_MUL,
449
- LM_GGML_OP_DIV,
450
- LM_GGML_OP_SQR,
451
- LM_GGML_OP_SQRT,
452
- LM_GGML_OP_LOG,
453
- LM_GGML_OP_SIN,
454
- LM_GGML_OP_COS,
455
- LM_GGML_OP_SUM,
456
- LM_GGML_OP_SUM_ROWS,
457
- LM_GGML_OP_MEAN,
458
- LM_GGML_OP_ARGMAX,
459
- LM_GGML_OP_COUNT_EQUAL,
460
- LM_GGML_OP_REPEAT,
461
- LM_GGML_OP_REPEAT_BACK,
462
- LM_GGML_OP_CONCAT,
463
- LM_GGML_OP_SILU_BACK,
464
- LM_GGML_OP_NORM, // normalize
465
- LM_GGML_OP_RMS_NORM,
466
- LM_GGML_OP_RMS_NORM_BACK,
467
- LM_GGML_OP_GROUP_NORM,
468
-
469
- LM_GGML_OP_MUL_MAT,
470
- LM_GGML_OP_MUL_MAT_ID,
471
- LM_GGML_OP_OUT_PROD,
472
-
473
- LM_GGML_OP_SCALE,
474
- LM_GGML_OP_SET,
475
- LM_GGML_OP_CPY,
476
- LM_GGML_OP_CONT,
477
- LM_GGML_OP_RESHAPE,
478
- LM_GGML_OP_VIEW,
479
- LM_GGML_OP_PERMUTE,
480
- LM_GGML_OP_TRANSPOSE,
481
- LM_GGML_OP_GET_ROWS,
482
- LM_GGML_OP_GET_ROWS_BACK,
483
- LM_GGML_OP_DIAG,
484
- LM_GGML_OP_DIAG_MASK_INF,
485
- LM_GGML_OP_DIAG_MASK_ZERO,
486
- LM_GGML_OP_SOFT_MAX,
487
- LM_GGML_OP_SOFT_MAX_BACK,
488
- LM_GGML_OP_ROPE,
489
- LM_GGML_OP_ROPE_BACK,
490
- LM_GGML_OP_CLAMP,
491
- LM_GGML_OP_CONV_TRANSPOSE_1D,
492
- LM_GGML_OP_IM2COL,
493
- LM_GGML_OP_IM2COL_BACK,
494
- LM_GGML_OP_CONV_TRANSPOSE_2D,
495
- LM_GGML_OP_POOL_1D,
496
- LM_GGML_OP_POOL_2D,
497
- LM_GGML_OP_POOL_2D_BACK,
498
- LM_GGML_OP_UPSCALE, // nearest interpolate
499
- LM_GGML_OP_PAD,
500
- LM_GGML_OP_ARANGE,
501
- LM_GGML_OP_TIMESTEP_EMBEDDING,
502
- LM_GGML_OP_ARGSORT,
503
- LM_GGML_OP_LEAKY_RELU,
504
-
505
- LM_GGML_OP_FLASH_ATTN_EXT,
506
- LM_GGML_OP_FLASH_ATTN_BACK,
507
- LM_GGML_OP_SSM_CONV,
508
- LM_GGML_OP_SSM_SCAN,
509
- LM_GGML_OP_WIN_PART,
510
- LM_GGML_OP_WIN_UNPART,
511
- LM_GGML_OP_GET_REL_POS,
512
- LM_GGML_OP_ADD_REL_POS,
513
- LM_GGML_OP_RWKV_WKV6,
514
-
515
- LM_GGML_OP_UNARY,
516
-
517
- LM_GGML_OP_MAP_UNARY,
518
- LM_GGML_OP_MAP_BINARY,
519
-
520
- LM_GGML_OP_MAP_CUSTOM1_F32,
521
- LM_GGML_OP_MAP_CUSTOM2_F32,
522
- LM_GGML_OP_MAP_CUSTOM3_F32,
523
-
524
- LM_GGML_OP_MAP_CUSTOM1,
525
- LM_GGML_OP_MAP_CUSTOM2,
526
- LM_GGML_OP_MAP_CUSTOM3,
527
-
528
- LM_GGML_OP_CROSS_ENTROPY_LOSS,
529
- LM_GGML_OP_CROSS_ENTROPY_LOSS_BACK,
530
- LM_GGML_OP_OPT_STEP_ADAMW,
531
-
532
- LM_GGML_OP_COUNT,
533
- };
534
-
535
- enum lm_ggml_unary_op {
536
- LM_GGML_UNARY_OP_ABS,
537
- LM_GGML_UNARY_OP_SGN,
538
- LM_GGML_UNARY_OP_NEG,
539
- LM_GGML_UNARY_OP_STEP,
540
- LM_GGML_UNARY_OP_TANH,
541
- LM_GGML_UNARY_OP_ELU,
542
- LM_GGML_UNARY_OP_RELU,
543
- LM_GGML_UNARY_OP_SIGMOID,
544
- LM_GGML_UNARY_OP_GELU,
545
- LM_GGML_UNARY_OP_GELU_QUICK,
546
- LM_GGML_UNARY_OP_SILU,
547
- LM_GGML_UNARY_OP_HARDSWISH,
548
- LM_GGML_UNARY_OP_HARDSIGMOID,
549
- LM_GGML_UNARY_OP_EXP,
550
-
551
- LM_GGML_UNARY_OP_COUNT,
552
- };
553
-
554
- enum lm_ggml_object_type {
555
- LM_GGML_OBJECT_TYPE_TENSOR,
556
- LM_GGML_OBJECT_TYPE_GRAPH,
557
- LM_GGML_OBJECT_TYPE_WORK_BUFFER
558
- };
559
-
560
- enum lm_ggml_log_level {
561
- LM_GGML_LOG_LEVEL_NONE = 0,
562
- LM_GGML_LOG_LEVEL_DEBUG = 1,
563
- LM_GGML_LOG_LEVEL_INFO = 2,
564
- LM_GGML_LOG_LEVEL_WARN = 3,
565
- LM_GGML_LOG_LEVEL_ERROR = 4,
566
- LM_GGML_LOG_LEVEL_CONT = 5, // continue previous log
567
- };
568
-
569
- // this tensor...
570
- enum lm_ggml_tensor_flag {
571
- LM_GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
572
- LM_GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
573
- LM_GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
574
- LM_GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up)
575
- };
576
-
577
- struct lm_ggml_init_params {
578
- // memory pool
579
- size_t mem_size; // bytes
580
- void * mem_buffer; // if NULL, memory will be allocated internally
581
- bool no_alloc; // don't allocate memory for the tensor data
582
- };
583
-
584
- // n-dimensional tensor
585
- struct lm_ggml_tensor {
586
- enum lm_ggml_type type;
587
-
588
- LM_GGML_DEPRECATED(enum lm_ggml_backend_type backend, "use the buffer type to find the storage location of the tensor");
589
-
590
- struct lm_ggml_backend_buffer * buffer;
591
-
592
- int64_t ne[LM_GGML_MAX_DIMS]; // number of elements
593
- size_t nb[LM_GGML_MAX_DIMS]; // stride in bytes:
594
- // nb[0] = lm_ggml_type_size(type)
595
- // nb[1] = nb[0] * (ne[0] / lm_ggml_blck_size(type)) + padding
596
- // nb[i] = nb[i-1] * ne[i-1]
597
-
598
- // compute data
599
- enum lm_ggml_op op;
600
-
601
- // op params - allocated as int32_t for alignment
602
- int32_t op_params[LM_GGML_MAX_OP_PARAMS / sizeof(int32_t)];
603
-
604
- int32_t flags;
605
-
606
- struct lm_ggml_tensor * grad;
607
- struct lm_ggml_tensor * src[LM_GGML_MAX_SRC];
608
-
609
- // source tensor and offset for views
610
- struct lm_ggml_tensor * view_src;
611
- size_t view_offs;
612
-
613
- void * data;
614
-
615
- char name[LM_GGML_MAX_NAME];
616
-
617
- void * extra; // extra things e.g. for ggml-cuda.cu
618
-
619
- // char padding[4];
620
- };
621
-
622
- static const size_t LM_GGML_TENSOR_SIZE = sizeof(struct lm_ggml_tensor);
623
-
624
- // Abort callback
625
- // If not NULL, called before ggml computation
626
- // If it returns true, the computation is aborted
627
- typedef bool (*lm_ggml_abort_callback)(void * data);
628
-
629
-
630
- //
631
- // GUID
632
- //
633
-
634
- // GUID types
635
- typedef uint8_t lm_ggml_guid[16];
636
- typedef lm_ggml_guid * lm_ggml_guid_t;
637
-
638
- LM_GGML_API bool lm_ggml_guid_matches(lm_ggml_guid_t guid_a, lm_ggml_guid_t guid_b);
639
-
640
- // misc
641
-
642
- LM_GGML_API void lm_ggml_time_init(void); // call this once at the beginning of the program
643
- LM_GGML_API int64_t lm_ggml_time_ms(void);
644
- LM_GGML_API int64_t lm_ggml_time_us(void);
645
- LM_GGML_API int64_t lm_ggml_cycles(void);
646
- LM_GGML_API int64_t lm_ggml_cycles_per_ms(void);
647
-
648
- // accepts a UTF-8 path, even on Windows
649
- LM_GGML_API FILE * lm_ggml_fopen(const char * fname, const char * mode);
650
-
651
- LM_GGML_API void lm_ggml_print_object (const struct lm_ggml_object * obj);
652
- LM_GGML_API void lm_ggml_print_objects(const struct lm_ggml_context * ctx);
653
-
654
- LM_GGML_API int64_t lm_ggml_nelements (const struct lm_ggml_tensor * tensor);
655
- LM_GGML_API int64_t lm_ggml_nrows (const struct lm_ggml_tensor * tensor);
656
- LM_GGML_API size_t lm_ggml_nbytes (const struct lm_ggml_tensor * tensor);
657
- LM_GGML_API size_t lm_ggml_nbytes_pad(const struct lm_ggml_tensor * tensor); // same as lm_ggml_nbytes() but padded to LM_GGML_MEM_ALIGN
658
-
659
- LM_GGML_API int64_t lm_ggml_blck_size(enum lm_ggml_type type);
660
- LM_GGML_API size_t lm_ggml_type_size(enum lm_ggml_type type); // size in bytes for all elements in a block
661
- LM_GGML_API size_t lm_ggml_row_size (enum lm_ggml_type type, int64_t ne); // size in bytes for all elements in a row
662
-
663
- LM_GGML_DEPRECATED(
664
- LM_GGML_API double lm_ggml_type_sizef(enum lm_ggml_type type), // lm_ggml_type_size()/lm_ggml_blck_size() as float
665
- "use lm_ggml_row_size() instead");
666
-
667
- LM_GGML_API const char * lm_ggml_type_name(enum lm_ggml_type type);
668
- LM_GGML_API const char * lm_ggml_op_name (enum lm_ggml_op op);
669
- LM_GGML_API const char * lm_ggml_op_symbol(enum lm_ggml_op op);
670
-
671
- LM_GGML_API const char * lm_ggml_unary_op_name(enum lm_ggml_unary_op op);
672
- LM_GGML_API const char * lm_ggml_op_desc(const struct lm_ggml_tensor * t); // unary or op name
673
-
674
- LM_GGML_API size_t lm_ggml_element_size(const struct lm_ggml_tensor * tensor);
675
-
676
- LM_GGML_API bool lm_ggml_is_quantized(enum lm_ggml_type type);
677
-
678
- // TODO: temporary until model loading of ggml examples is refactored
679
- LM_GGML_API enum lm_ggml_type lm_ggml_ftype_to_lm_ggml_type(enum lm_ggml_ftype ftype);
680
-
681
- LM_GGML_API bool lm_ggml_is_transposed(const struct lm_ggml_tensor * tensor);
682
- LM_GGML_API bool lm_ggml_is_permuted (const struct lm_ggml_tensor * tensor);
683
- LM_GGML_API bool lm_ggml_is_empty (const struct lm_ggml_tensor * tensor);
684
- LM_GGML_API bool lm_ggml_is_scalar (const struct lm_ggml_tensor * tensor);
685
- LM_GGML_API bool lm_ggml_is_vector (const struct lm_ggml_tensor * tensor);
686
- LM_GGML_API bool lm_ggml_is_matrix (const struct lm_ggml_tensor * tensor);
687
- LM_GGML_API bool lm_ggml_is_3d (const struct lm_ggml_tensor * tensor);
688
- LM_GGML_API int lm_ggml_n_dims (const struct lm_ggml_tensor * tensor); // returns 1 for scalars
689
-
690
- LM_GGML_API bool lm_ggml_is_contiguous (const struct lm_ggml_tensor * tensor);
691
- LM_GGML_API bool lm_ggml_is_contiguous_0(const struct lm_ggml_tensor * tensor); // same as lm_ggml_is_contiguous()
692
- LM_GGML_API bool lm_ggml_is_contiguous_1(const struct lm_ggml_tensor * tensor); // contiguous for dims >= 1
693
- LM_GGML_API bool lm_ggml_is_contiguous_2(const struct lm_ggml_tensor * tensor); // contiguous for dims >= 2
694
-
695
- LM_GGML_API bool lm_ggml_are_same_shape (const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
696
- LM_GGML_API bool lm_ggml_are_same_stride(const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
697
-
698
- LM_GGML_API bool lm_ggml_can_repeat(const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
699
-
700
- // use this to compute the memory overhead of a tensor
701
- LM_GGML_API size_t lm_ggml_tensor_overhead(void);
702
-
703
- LM_GGML_API bool lm_ggml_validate_row_data(enum lm_ggml_type type, const void * data, size_t nbytes);
704
-
705
- // main
706
-
707
- LM_GGML_API struct lm_ggml_context * lm_ggml_init (struct lm_ggml_init_params params);
708
- LM_GGML_API void lm_ggml_reset(struct lm_ggml_context * ctx);
709
- LM_GGML_API void lm_ggml_free (struct lm_ggml_context * ctx);
710
-
711
- LM_GGML_API size_t lm_ggml_used_mem(const struct lm_ggml_context * ctx);
712
-
713
- LM_GGML_API bool lm_ggml_get_no_alloc(struct lm_ggml_context * ctx);
714
- LM_GGML_API void lm_ggml_set_no_alloc(struct lm_ggml_context * ctx, bool no_alloc);
715
-
716
- LM_GGML_API void * lm_ggml_get_mem_buffer (const struct lm_ggml_context * ctx);
717
- LM_GGML_API size_t lm_ggml_get_mem_size (const struct lm_ggml_context * ctx);
718
- LM_GGML_API size_t lm_ggml_get_max_tensor_size(const struct lm_ggml_context * ctx);
719
-
720
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor(
721
- struct lm_ggml_context * ctx,
722
- enum lm_ggml_type type,
723
- int n_dims,
724
- const int64_t *ne);
725
-
726
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_1d(
727
- struct lm_ggml_context * ctx,
728
- enum lm_ggml_type type,
729
- int64_t ne0);
730
-
731
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_2d(
732
- struct lm_ggml_context * ctx,
733
- enum lm_ggml_type type,
734
- int64_t ne0,
735
- int64_t ne1);
736
-
737
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_3d(
738
- struct lm_ggml_context * ctx,
739
- enum lm_ggml_type type,
740
- int64_t ne0,
741
- int64_t ne1,
742
- int64_t ne2);
743
-
744
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_4d(
745
- struct lm_ggml_context * ctx,
746
- enum lm_ggml_type type,
747
- int64_t ne0,
748
- int64_t ne1,
749
- int64_t ne2,
750
- int64_t ne3);
751
-
752
- LM_GGML_API void * lm_ggml_new_buffer(struct lm_ggml_context * ctx, size_t nbytes);
753
-
754
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup_tensor (struct lm_ggml_context * ctx, const struct lm_ggml_tensor * src);
755
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_tensor(struct lm_ggml_context * ctx, struct lm_ggml_tensor * src);
756
-
757
- // Context tensor enumeration and lookup
758
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_first_tensor(const struct lm_ggml_context * ctx);
759
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_next_tensor (const struct lm_ggml_context * ctx, struct lm_ggml_tensor * tensor);
760
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_tensor(struct lm_ggml_context * ctx, const char * name);
761
-
762
- // Converts a flat index into coordinates
763
- LM_GGML_API void lm_ggml_unravel_index(const struct lm_ggml_tensor * tensor, int64_t i, int64_t * i0, int64_t * i1, int64_t * i2, int64_t * i3);
764
-
765
- LM_GGML_API enum lm_ggml_unary_op lm_ggml_get_unary_op(const struct lm_ggml_tensor * tensor);
766
-
767
- LM_GGML_API void * lm_ggml_get_data (const struct lm_ggml_tensor * tensor);
768
- LM_GGML_API float * lm_ggml_get_data_f32(const struct lm_ggml_tensor * tensor);
769
-
770
- LM_GGML_API const char * lm_ggml_get_name (const struct lm_ggml_tensor * tensor);
771
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_name ( struct lm_ggml_tensor * tensor, const char * name);
772
- LM_GGML_ATTRIBUTE_FORMAT(2, 3)
773
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_format_name( struct lm_ggml_tensor * tensor, const char * fmt, ...);
774
-
775
- // Tensor flags
776
- LM_GGML_API void lm_ggml_set_input(struct lm_ggml_tensor * tensor);
777
- LM_GGML_API void lm_ggml_set_output(struct lm_ggml_tensor * tensor);
778
- LM_GGML_API void lm_ggml_set_param(struct lm_ggml_context * ctx, struct lm_ggml_tensor * tensor);
779
- LM_GGML_API void lm_ggml_set_loss(struct lm_ggml_tensor * tensor);
780
-
781
- //
782
- // operations on tensors with backpropagation
783
- //
784
-
785
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup(
786
- struct lm_ggml_context * ctx,
787
- struct lm_ggml_tensor * a);
788
-
789
- // in-place, returns view(a)
790
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup_inplace(
791
- struct lm_ggml_context * ctx,
792
- struct lm_ggml_tensor * a);
793
-
794
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_add(
795
- struct lm_ggml_context * ctx,
796
- struct lm_ggml_tensor * a,
797
- struct lm_ggml_tensor * b);
798
-
799
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_inplace(
800
- struct lm_ggml_context * ctx,
801
- struct lm_ggml_tensor * a,
802
- struct lm_ggml_tensor * b);
803
-
804
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_cast(
805
- struct lm_ggml_context * ctx,
806
- struct lm_ggml_tensor * a,
807
- struct lm_ggml_tensor * b,
808
- enum lm_ggml_type type);
809
-
810
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_add1(
811
- struct lm_ggml_context * ctx,
812
- struct lm_ggml_tensor * a,
813
- struct lm_ggml_tensor * b);
814
-
815
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_add1_inplace(
816
- struct lm_ggml_context * ctx,
817
- struct lm_ggml_tensor * a,
818
- struct lm_ggml_tensor * b);
819
-
820
- // dst = a
821
- // view(dst, nb1, nb2, nb3, offset) += b
822
- // return dst
823
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_acc(
824
- struct lm_ggml_context * ctx,
825
- struct lm_ggml_tensor * a,
826
- struct lm_ggml_tensor * b,
827
- size_t nb1,
828
- size_t nb2,
829
- size_t nb3,
830
- size_t offset);
831
-
832
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_acc_inplace(
833
- struct lm_ggml_context * ctx,
834
- struct lm_ggml_tensor * a,
835
- struct lm_ggml_tensor * b,
836
- size_t nb1,
837
- size_t nb2,
838
- size_t nb3,
839
- size_t offset);
840
-
841
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sub(
842
- struct lm_ggml_context * ctx,
843
- struct lm_ggml_tensor * a,
844
- struct lm_ggml_tensor * b);
845
-
846
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sub_inplace(
847
- struct lm_ggml_context * ctx,
848
- struct lm_ggml_tensor * a,
849
- struct lm_ggml_tensor * b);
850
-
851
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul(
852
- struct lm_ggml_context * ctx,
853
- struct lm_ggml_tensor * a,
854
- struct lm_ggml_tensor * b);
855
-
856
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_inplace(
857
- struct lm_ggml_context * ctx,
858
- struct lm_ggml_tensor * a,
859
- struct lm_ggml_tensor * b);
860
-
861
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_div(
862
- struct lm_ggml_context * ctx,
863
- struct lm_ggml_tensor * a,
864
- struct lm_ggml_tensor * b);
865
-
866
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_div_inplace(
867
- struct lm_ggml_context * ctx,
868
- struct lm_ggml_tensor * a,
869
- struct lm_ggml_tensor * b);
870
-
871
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqr(
872
- struct lm_ggml_context * ctx,
873
- struct lm_ggml_tensor * a);
874
-
875
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqr_inplace(
876
- struct lm_ggml_context * ctx,
877
- struct lm_ggml_tensor * a);
878
-
879
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqrt(
880
- struct lm_ggml_context * ctx,
881
- struct lm_ggml_tensor * a);
882
-
883
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqrt_inplace(
884
- struct lm_ggml_context * ctx,
885
- struct lm_ggml_tensor * a);
886
-
887
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_log(
888
- struct lm_ggml_context * ctx,
889
- struct lm_ggml_tensor * a);
890
-
891
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_log_inplace(
892
- struct lm_ggml_context * ctx,
893
- struct lm_ggml_tensor * a);
894
-
895
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sin(
896
- struct lm_ggml_context * ctx,
897
- struct lm_ggml_tensor * a);
898
-
899
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sin_inplace(
900
- struct lm_ggml_context * ctx,
901
- struct lm_ggml_tensor * a);
902
-
903
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cos(
904
- struct lm_ggml_context * ctx,
905
- struct lm_ggml_tensor * a);
906
-
907
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cos_inplace(
908
- struct lm_ggml_context * ctx,
909
- struct lm_ggml_tensor * a);
910
-
911
- // return scalar
912
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sum(
913
- struct lm_ggml_context * ctx,
914
- struct lm_ggml_tensor * a);
915
-
916
- // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
917
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sum_rows(
918
- struct lm_ggml_context * ctx,
919
- struct lm_ggml_tensor * a);
920
-
921
- // mean along rows
922
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_mean(
923
- struct lm_ggml_context * ctx,
924
- struct lm_ggml_tensor * a);
925
-
926
- // argmax along rows
927
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_argmax(
928
- struct lm_ggml_context * ctx,
929
- struct lm_ggml_tensor * a);
930
-
931
- // count number of equal elements in a and b
932
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_count_equal(
933
- struct lm_ggml_context * ctx,
934
- struct lm_ggml_tensor * a,
935
- struct lm_ggml_tensor * b);
936
-
937
- // if a is the same shape as b, and a is not parameter, return a
938
- // otherwise, return a new tensor: repeat(a) to fit in b
939
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat(
940
- struct lm_ggml_context * ctx,
941
- struct lm_ggml_tensor * a,
942
- struct lm_ggml_tensor * b);
943
-
944
- // sums repetitions in a into shape of b
945
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat_back(
946
- struct lm_ggml_context * ctx,
947
- struct lm_ggml_tensor * a,
948
- struct lm_ggml_tensor * b);
949
-
950
- // concat a and b along dim
951
- // used in stable-diffusion
952
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_concat(
953
- struct lm_ggml_context * ctx,
954
- struct lm_ggml_tensor * a,
955
- struct lm_ggml_tensor * b,
956
- int dim);
957
-
958
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_abs(
959
- struct lm_ggml_context * ctx,
960
- struct lm_ggml_tensor * a);
961
-
962
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_abs_inplace(
963
- struct lm_ggml_context * ctx,
964
- struct lm_ggml_tensor * a);
965
-
966
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sgn(
967
- struct lm_ggml_context * ctx,
968
- struct lm_ggml_tensor * a);
969
-
970
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sgn_inplace(
971
- struct lm_ggml_context * ctx,
972
- struct lm_ggml_tensor * a);
973
-
974
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_neg(
975
- struct lm_ggml_context * ctx,
976
- struct lm_ggml_tensor * a);
977
-
978
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_neg_inplace(
979
- struct lm_ggml_context * ctx,
980
- struct lm_ggml_tensor * a);
981
-
982
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_step(
983
- struct lm_ggml_context * ctx,
984
- struct lm_ggml_tensor * a);
985
-
986
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_step_inplace(
987
- struct lm_ggml_context * ctx,
988
- struct lm_ggml_tensor * a);
989
-
990
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_tanh(
991
- struct lm_ggml_context * ctx,
992
- struct lm_ggml_tensor * a);
993
-
994
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_tanh_inplace(
995
- struct lm_ggml_context * ctx,
996
- struct lm_ggml_tensor * a);
997
-
998
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_elu(
999
- struct lm_ggml_context * ctx,
1000
- struct lm_ggml_tensor * a);
1001
-
1002
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_elu_inplace(
1003
- struct lm_ggml_context * ctx,
1004
- struct lm_ggml_tensor * a);
1005
-
1006
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_relu(
1007
- struct lm_ggml_context * ctx,
1008
- struct lm_ggml_tensor * a);
1009
-
1010
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_leaky_relu(
1011
- struct lm_ggml_context * ctx,
1012
- struct lm_ggml_tensor * a, float negative_slope, bool inplace);
1013
-
1014
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_relu_inplace(
1015
- struct lm_ggml_context * ctx,
1016
- struct lm_ggml_tensor * a);
1017
-
1018
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sigmoid(
1019
- struct lm_ggml_context * ctx,
1020
- struct lm_ggml_tensor * a);
1021
-
1022
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_sigmoid_inplace(
1023
- struct lm_ggml_context * ctx,
1024
- struct lm_ggml_tensor * a);
1025
-
1026
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu(
1027
- struct lm_ggml_context * ctx,
1028
- struct lm_ggml_tensor * a);
1029
-
1030
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_inplace(
1031
- struct lm_ggml_context * ctx,
1032
- struct lm_ggml_tensor * a);
1033
-
1034
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_quick(
1035
- struct lm_ggml_context * ctx,
1036
- struct lm_ggml_tensor * a);
1037
-
1038
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_quick_inplace(
1039
- struct lm_ggml_context * ctx,
1040
- struct lm_ggml_tensor * a);
1041
-
1042
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu(
1043
- struct lm_ggml_context * ctx,
1044
- struct lm_ggml_tensor * a);
1045
-
1046
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu_inplace(
1047
- struct lm_ggml_context * ctx,
1048
- struct lm_ggml_tensor * a);
1049
-
1050
- // a - x
1051
- // b - dy
1052
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu_back(
1053
- struct lm_ggml_context * ctx,
1054
- struct lm_ggml_tensor * a,
1055
- struct lm_ggml_tensor * b);
1056
-
1057
- // hardswish(x) = x * relu6(x + 3) / 6
1058
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_hardswish(
1059
- struct lm_ggml_context * ctx,
1060
- struct lm_ggml_tensor * a);
1061
-
1062
- // hardsigmoid(x) = relu6(x + 3) / 6
1063
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_hardsigmoid(
1064
- struct lm_ggml_context * ctx,
1065
- struct lm_ggml_tensor * a);
1066
-
1067
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_exp(
1068
- struct lm_ggml_context * ctx,
1069
- struct lm_ggml_tensor * a);
1070
-
1071
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_exp_inplace(
1072
- struct lm_ggml_context * ctx,
1073
- struct lm_ggml_tensor * a);
1074
-
1075
- // normalize along rows
1076
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_norm(
1077
- struct lm_ggml_context * ctx,
1078
- struct lm_ggml_tensor * a,
1079
- float eps);
1080
-
1081
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_norm_inplace(
1082
- struct lm_ggml_context * ctx,
1083
- struct lm_ggml_tensor * a,
1084
- float eps);
1085
-
1086
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm(
1087
- struct lm_ggml_context * ctx,
1088
- struct lm_ggml_tensor * a,
1089
- float eps);
1090
-
1091
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm_inplace(
1092
- struct lm_ggml_context * ctx,
1093
- struct lm_ggml_tensor * a,
1094
- float eps);
1095
-
1096
- // group normalize along ne0*ne1*n_groups
1097
- // used in stable-diffusion
1098
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_group_norm(
1099
- struct lm_ggml_context * ctx,
1100
- struct lm_ggml_tensor * a,
1101
- int n_groups,
1102
- float eps);
1103
-
1104
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_group_norm_inplace(
1105
- struct lm_ggml_context * ctx,
1106
- struct lm_ggml_tensor * a,
1107
- int n_groups,
1108
- float eps);
1109
-
1110
- // a - x
1111
- // b - dy
1112
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm_back(
1113
- struct lm_ggml_context * ctx,
1114
- struct lm_ggml_tensor * a,
1115
- struct lm_ggml_tensor * b,
1116
- float eps);
1117
-
1118
- // A: k columns, n rows => [ne03, ne02, n, k]
1119
- // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
1120
- // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
1121
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_mat(
1122
- struct lm_ggml_context * ctx,
1123
- struct lm_ggml_tensor * a,
1124
- struct lm_ggml_tensor * b);
1125
-
1126
- // change the precision of a matrix multiplication
1127
- // set to LM_GGML_PREC_F32 for higher precision (useful for phi-2)
1128
- LM_GGML_API void lm_ggml_mul_mat_set_prec(
1129
- struct lm_ggml_tensor * a,
1130
- enum lm_ggml_prec prec);
1131
-
1132
- // indirect matrix multiplication
1133
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_mat_id(
1134
- struct lm_ggml_context * ctx,
1135
- struct lm_ggml_tensor * as,
1136
- struct lm_ggml_tensor * b,
1137
- struct lm_ggml_tensor * ids);
1138
-
1139
- // A: m columns, n rows,
1140
- // B: p columns, n rows,
1141
- // result is m columns, p rows
1142
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_out_prod(
1143
- struct lm_ggml_context * ctx,
1144
- struct lm_ggml_tensor * a,
1145
- struct lm_ggml_tensor * b);
1146
-
1147
- //
1148
- // operations on tensors without backpropagation
1149
- //
1150
-
1151
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale(
1152
- struct lm_ggml_context * ctx,
1153
- struct lm_ggml_tensor * a,
1154
- float s);
1155
-
1156
- // in-place, returns view(a)
1157
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale_inplace(
1158
- struct lm_ggml_context * ctx,
1159
- struct lm_ggml_tensor * a,
1160
- float s);
1161
-
1162
- // b -> view(a,offset,nb1,nb2,3), return modified a
1163
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_set(
1164
- struct lm_ggml_context * ctx,
1165
- struct lm_ggml_tensor * a,
1166
- struct lm_ggml_tensor * b,
1167
- size_t nb1,
1168
- size_t nb2,
1169
- size_t nb3,
1170
- size_t offset); // in bytes
1171
-
1172
- // b -> view(a,offset,nb1,nb2,3), return view(a)
1173
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_inplace(
1174
- struct lm_ggml_context * ctx,
1175
- struct lm_ggml_tensor * a,
1176
- struct lm_ggml_tensor * b,
1177
- size_t nb1,
1178
- size_t nb2,
1179
- size_t nb3,
1180
- size_t offset); // in bytes
1181
-
1182
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_1d(
1183
- struct lm_ggml_context * ctx,
1184
- struct lm_ggml_tensor * a,
1185
- struct lm_ggml_tensor * b,
1186
- size_t offset); // in bytes
1187
-
1188
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_1d_inplace(
1189
- struct lm_ggml_context * ctx,
1190
- struct lm_ggml_tensor * a,
1191
- struct lm_ggml_tensor * b,
1192
- size_t offset); // in bytes
1193
-
1194
- // b -> view(a,offset,nb1,nb2,3), return modified a
1195
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_2d(
1196
- struct lm_ggml_context * ctx,
1197
- struct lm_ggml_tensor * a,
1198
- struct lm_ggml_tensor * b,
1199
- size_t nb1,
1200
- size_t offset); // in bytes
1201
-
1202
- // b -> view(a,offset,nb1,nb2,3), return view(a)
1203
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_2d_inplace(
1204
- struct lm_ggml_context * ctx,
1205
- struct lm_ggml_tensor * a,
1206
- struct lm_ggml_tensor * b,
1207
- size_t nb1,
1208
- size_t offset); // in bytes
1209
-
1210
- // a -> b, return view(b)
1211
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cpy(
1212
- struct lm_ggml_context * ctx,
1213
- struct lm_ggml_tensor * a,
1214
- struct lm_ggml_tensor * b);
1215
-
1216
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cast(
1217
- struct lm_ggml_context * ctx,
1218
- struct lm_ggml_tensor * a,
1219
- enum lm_ggml_type type);
1220
-
1221
- // make contiguous
1222
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont(
1223
- struct lm_ggml_context * ctx,
1224
- struct lm_ggml_tensor * a);
1225
-
1226
- // make contiguous, with new shape
1227
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_1d(
1228
- struct lm_ggml_context * ctx,
1229
- struct lm_ggml_tensor * a,
1230
- int64_t ne0);
1231
-
1232
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_2d(
1233
- struct lm_ggml_context * ctx,
1234
- struct lm_ggml_tensor * a,
1235
- int64_t ne0,
1236
- int64_t ne1);
1237
-
1238
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_3d(
1239
- struct lm_ggml_context * ctx,
1240
- struct lm_ggml_tensor * a,
1241
- int64_t ne0,
1242
- int64_t ne1,
1243
- int64_t ne2);
1244
-
1245
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_4d(
1246
- struct lm_ggml_context * ctx,
1247
- struct lm_ggml_tensor * a,
1248
- int64_t ne0,
1249
- int64_t ne1,
1250
- int64_t ne2,
1251
- int64_t ne3);
1252
-
1253
- // return view(a), b specifies the new shape
1254
- // TODO: when we start computing gradient, make a copy instead of view
1255
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape(
1256
- struct lm_ggml_context * ctx,
1257
- struct lm_ggml_tensor * a,
1258
- struct lm_ggml_tensor * b);
1259
-
1260
- // return view(a)
1261
- // TODO: when we start computing gradient, make a copy instead of view
1262
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_1d(
1263
- struct lm_ggml_context * ctx,
1264
- struct lm_ggml_tensor * a,
1265
- int64_t ne0);
1266
-
1267
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_2d(
1268
- struct lm_ggml_context * ctx,
1269
- struct lm_ggml_tensor * a,
1270
- int64_t ne0,
1271
- int64_t ne1);
1272
-
1273
- // return view(a)
1274
- // TODO: when we start computing gradient, make a copy instead of view
1275
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_3d(
1276
- struct lm_ggml_context * ctx,
1277
- struct lm_ggml_tensor * a,
1278
- int64_t ne0,
1279
- int64_t ne1,
1280
- int64_t ne2);
1281
-
1282
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_4d(
1283
- struct lm_ggml_context * ctx,
1284
- struct lm_ggml_tensor * a,
1285
- int64_t ne0,
1286
- int64_t ne1,
1287
- int64_t ne2,
1288
- int64_t ne3);
1289
-
1290
- // offset in bytes
1291
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_1d(
1292
- struct lm_ggml_context * ctx,
1293
- struct lm_ggml_tensor * a,
1294
- int64_t ne0,
1295
- size_t offset);
1296
-
1297
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_2d(
1298
- struct lm_ggml_context * ctx,
1299
- struct lm_ggml_tensor * a,
1300
- int64_t ne0,
1301
- int64_t ne1,
1302
- size_t nb1, // row stride in bytes
1303
- size_t offset);
1304
-
1305
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_3d(
1306
- struct lm_ggml_context * ctx,
1307
- struct lm_ggml_tensor * a,
1308
- int64_t ne0,
1309
- int64_t ne1,
1310
- int64_t ne2,
1311
- size_t nb1, // row stride in bytes
1312
- size_t nb2, // slice stride in bytes
1313
- size_t offset);
1314
-
1315
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_4d(
1316
- struct lm_ggml_context * ctx,
1317
- struct lm_ggml_tensor * a,
1318
- int64_t ne0,
1319
- int64_t ne1,
1320
- int64_t ne2,
1321
- int64_t ne3,
1322
- size_t nb1, // row stride in bytes
1323
- size_t nb2, // slice stride in bytes
1324
- size_t nb3,
1325
- size_t offset);
1326
-
1327
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_permute(
1328
- struct lm_ggml_context * ctx,
1329
- struct lm_ggml_tensor * a,
1330
- int axis0,
1331
- int axis1,
1332
- int axis2,
1333
- int axis3);
1334
-
1335
- // alias for lm_ggml_permute(ctx, a, 1, 0, 2, 3)
1336
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_transpose(
1337
- struct lm_ggml_context * ctx,
1338
- struct lm_ggml_tensor * a);
1339
-
1340
- // supports 3D: a->ne[2] == b->ne[1]
1341
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rows(
1342
- struct lm_ggml_context * ctx,
1343
- struct lm_ggml_tensor * a, // data
1344
- struct lm_ggml_tensor * b); // row indices
1345
-
1346
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rows_back(
1347
- struct lm_ggml_context * ctx,
1348
- struct lm_ggml_tensor * a, // gradients of lm_ggml_get_rows result
1349
- struct lm_ggml_tensor * b, // row indices
1350
- struct lm_ggml_tensor * c); // data for lm_ggml_get_rows, only used for its shape
1351
-
1352
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag(
1353
- struct lm_ggml_context * ctx,
1354
- struct lm_ggml_tensor * a);
1355
-
1356
- // set elements above the diagonal to -INF
1357
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_inf(
1358
- struct lm_ggml_context * ctx,
1359
- struct lm_ggml_tensor * a,
1360
- int n_past);
1361
-
1362
- // in-place, returns view(a)
1363
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_inf_inplace(
1364
- struct lm_ggml_context * ctx,
1365
- struct lm_ggml_tensor * a,
1366
- int n_past);
1367
-
1368
- // set elements above the diagonal to 0
1369
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_zero(
1370
- struct lm_ggml_context * ctx,
1371
- struct lm_ggml_tensor * a,
1372
- int n_past);
1373
-
1374
- // in-place, returns view(a)
1375
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_zero_inplace(
1376
- struct lm_ggml_context * ctx,
1377
- struct lm_ggml_tensor * a,
1378
- int n_past);
1379
-
1380
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max(
1381
- struct lm_ggml_context * ctx,
1382
- struct lm_ggml_tensor * a);
1383
-
1384
- // in-place, returns view(a)
1385
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_inplace(
1386
- struct lm_ggml_context * ctx,
1387
- struct lm_ggml_tensor * a);
1388
-
1389
- // fused soft_max(a*scale + mask*(ALiBi slope))
1390
- // mask is optional
1391
- // max_bias = 0.0f for no ALiBi
1392
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext(
1393
- struct lm_ggml_context * ctx,
1394
- struct lm_ggml_tensor * a,
1395
- struct lm_ggml_tensor * mask,
1396
- float scale,
1397
- float max_bias);
1398
-
1399
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_back(
1400
- struct lm_ggml_context * ctx,
1401
- struct lm_ggml_tensor * a,
1402
- struct lm_ggml_tensor * b);
1403
-
1404
- // in-place, returns view(a)
1405
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_back_inplace(
1406
- struct lm_ggml_context * ctx,
1407
- struct lm_ggml_tensor * a,
1408
- struct lm_ggml_tensor * b);
1409
-
1410
- // rotary position embedding
1411
- // if (mode & 1) - skip n_past elements (NOT SUPPORTED)
1412
- // if (mode & LM_GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
1413
- //
1414
- // b is an int32 vector with size a->ne[2], it contains the positions
1415
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope(
1416
- struct lm_ggml_context * ctx,
1417
- struct lm_ggml_tensor * a,
1418
- struct lm_ggml_tensor * b,
1419
- int n_dims,
1420
- int mode);
1421
-
1422
- // in-place, returns view(a)
1423
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_inplace(
1424
- struct lm_ggml_context * ctx,
1425
- struct lm_ggml_tensor * a,
1426
- struct lm_ggml_tensor * b,
1427
- int n_dims,
1428
- int mode);
1429
-
1430
- // custom RoPE
1431
- // c is freq factors (e.g. phi3-128k), (optional)
1432
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext(
1433
- struct lm_ggml_context * ctx,
1434
- struct lm_ggml_tensor * a,
1435
- struct lm_ggml_tensor * b,
1436
- struct lm_ggml_tensor * c,
1437
- int n_dims,
1438
- int mode,
1439
- int n_ctx_orig,
1440
- float freq_base,
1441
- float freq_scale,
1442
- float ext_factor,
1443
- float attn_factor,
1444
- float beta_fast,
1445
- float beta_slow);
1446
-
1447
- // in-place, returns view(a)
1448
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext_inplace(
1449
- struct lm_ggml_context * ctx,
1450
- struct lm_ggml_tensor * a,
1451
- struct lm_ggml_tensor * b,
1452
- struct lm_ggml_tensor * c,
1453
- int n_dims,
1454
- int mode,
1455
- int n_ctx_orig,
1456
- float freq_base,
1457
- float freq_scale,
1458
- float ext_factor,
1459
- float attn_factor,
1460
- float beta_fast,
1461
- float beta_slow);
1462
-
1463
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_custom(
1464
- struct lm_ggml_context * ctx,
1465
- struct lm_ggml_tensor * a,
1466
- struct lm_ggml_tensor * b,
1467
- int n_dims,
1468
- int mode,
1469
- int n_ctx_orig,
1470
- float freq_base,
1471
- float freq_scale,
1472
- float ext_factor,
1473
- float attn_factor,
1474
- float beta_fast,
1475
- float beta_slow),
1476
- "use lm_ggml_rope_ext instead");
1477
-
1478
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_custom_inplace(
1479
- struct lm_ggml_context * ctx,
1480
- struct lm_ggml_tensor * a,
1481
- struct lm_ggml_tensor * b,
1482
- int n_dims,
1483
- int mode,
1484
- int n_ctx_orig,
1485
- float freq_base,
1486
- float freq_scale,
1487
- float ext_factor,
1488
- float attn_factor,
1489
- float beta_fast,
1490
- float beta_slow),
1491
- "use lm_ggml_rope_ext_inplace instead");
1492
-
1493
- // compute correction dims for YaRN RoPE scaling
1494
- LM_GGML_API void lm_ggml_rope_yarn_corr_dims(
1495
- int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]);
1496
-
1497
- // rotary position embedding backward, i.e compute dx from dy
1498
- // a - dy
1499
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_back(
1500
- struct lm_ggml_context * ctx,
1501
- struct lm_ggml_tensor * a, // gradients of lm_ggml_rope result
1502
- struct lm_ggml_tensor * b, // positions
1503
- struct lm_ggml_tensor * c, // freq factors
1504
- int n_dims,
1505
- int mode,
1506
- int n_ctx_orig,
1507
- float freq_base,
1508
- float freq_scale,
1509
- float ext_factor,
1510
- float attn_factor,
1511
- float beta_fast,
1512
- float beta_slow);
1513
-
1514
- // clamp
1515
- // in-place, returns view(a)
1516
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_clamp(
1517
- struct lm_ggml_context * ctx,
1518
- struct lm_ggml_tensor * a,
1519
- float min,
1520
- float max);
1521
-
1522
- // im2col
1523
- // converts data into a format that effectively results in a convolution when combined with matrix multiplication
1524
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_im2col(
1525
- struct lm_ggml_context * ctx,
1526
- struct lm_ggml_tensor * a, // convolution kernel
1527
- struct lm_ggml_tensor * b, // data
1528
- int s0, // stride dimension 0
1529
- int s1, // stride dimension 1
1530
- int p0, // padding dimension 0
1531
- int p1, // padding dimension 1
1532
- int d0, // dilation dimension 0
1533
- int d1, // dilation dimension 1
1534
- bool is_2D,
1535
- enum lm_ggml_type dst_type);
1536
-
1537
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_im2col_back(
1538
- struct lm_ggml_context * ctx,
1539
- struct lm_ggml_tensor * a, // convolution kernel
1540
- struct lm_ggml_tensor * b, // gradient of im2col output
1541
- int64_t * ne, // shape of im2col input
1542
- int s0, // stride dimension 0
1543
- int s1, // stride dimension 1
1544
- int p0, // padding dimension 0
1545
- int p1, // padding dimension 1
1546
- int d0, // dilation dimension 0
1547
- int d1, // dilation dimension 1
1548
- bool is_2D);
1549
-
1550
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_depthwise_2d(
1551
- struct lm_ggml_context * ctx,
1552
- struct lm_ggml_tensor * a, // convolution kernel
1553
- struct lm_ggml_tensor * b, // data
1554
- int s0, // stride dimension 0
1555
- int s1, // stride dimension 1
1556
- int p0, // padding dimension 0
1557
- int p1, // padding dimension 1
1558
- int d0, // dilation dimension 0
1559
- int d1); // dilation dimension 1
1560
-
1561
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d(
1562
- struct lm_ggml_context * ctx,
1563
- struct lm_ggml_tensor * a, // convolution kernel
1564
- struct lm_ggml_tensor * b, // data
1565
- int s0, // stride
1566
- int p0, // padding
1567
- int d0); // dilation
1568
-
1569
- // conv_1d with padding = half
1570
- // alias for lm_ggml_conv_1d(a, b, s, a->ne[0]/2, d)
1571
- LM_GGML_API struct lm_ggml_tensor* lm_ggml_conv_1d_ph(
1572
- struct lm_ggml_context * ctx,
1573
- struct lm_ggml_tensor * a, // convolution kernel
1574
- struct lm_ggml_tensor * b, // data
1575
- int s, // stride
1576
- int d); // dilation
1577
-
1578
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_transpose_1d(
1579
- struct lm_ggml_context * ctx,
1580
- struct lm_ggml_tensor * a, // convolution kernel
1581
- struct lm_ggml_tensor * b, // data
1582
- int s0, // stride
1583
- int p0, // padding
1584
- int d0); // dilation
1585
-
1586
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d(
1587
- struct lm_ggml_context * ctx,
1588
- struct lm_ggml_tensor * a, // convolution kernel
1589
- struct lm_ggml_tensor * b, // data
1590
- int s0, // stride dimension 0
1591
- int s1, // stride dimension 1
1592
- int p0, // padding dimension 0
1593
- int p1, // padding dimension 1
1594
- int d0, // dilation dimension 0
1595
- int d1); // dilation dimension 1
1596
-
1597
-
1598
- // kernel size is a->ne[0] x a->ne[1]
1599
- // stride is equal to kernel size
1600
- // padding is zero
1601
- // example:
1602
- // a: 16 16 3 768
1603
- // b: 1024 1024 3 1
1604
- // res: 64 64 768 1
1605
- // used in sam
1606
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_sk_p0(
1607
- struct lm_ggml_context * ctx,
1608
- struct lm_ggml_tensor * a,
1609
- struct lm_ggml_tensor * b);
1610
-
1611
- // kernel size is a->ne[0] x a->ne[1]
1612
- // stride is 1
1613
- // padding is half
1614
- // example:
1615
- // a: 3 3 256 256
1616
- // b: 64 64 256 1
1617
- // res: 64 64 256 1
1618
- // used in sam
1619
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_s1_ph(
1620
- struct lm_ggml_context * ctx,
1621
- struct lm_ggml_tensor * a,
1622
- struct lm_ggml_tensor * b);
1623
-
1624
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_transpose_2d_p0(
1625
- struct lm_ggml_context * ctx,
1626
- struct lm_ggml_tensor * a,
1627
- struct lm_ggml_tensor * b,
1628
- int stride);
1629
-
1630
- enum lm_ggml_op_pool {
1631
- LM_GGML_OP_POOL_MAX,
1632
- LM_GGML_OP_POOL_AVG,
1633
- LM_GGML_OP_POOL_COUNT,
1634
- };
1635
-
1636
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_1d(
1637
- struct lm_ggml_context * ctx,
1638
- struct lm_ggml_tensor * a,
1639
- enum lm_ggml_op_pool op,
1640
- int k0, // kernel size
1641
- int s0, // stride
1642
- int p0); // padding
1643
-
1644
- // the result will have 2*p0 padding for the first dimension
1645
- // and 2*p1 padding for the second dimension
1646
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_2d(
1647
- struct lm_ggml_context * ctx,
1648
- struct lm_ggml_tensor * a,
1649
- enum lm_ggml_op_pool op,
1650
- int k0,
1651
- int k1,
1652
- int s0,
1653
- int s1,
1654
- float p0,
1655
- float p1);
1656
-
1657
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_2d_back(
1658
- struct lm_ggml_context * ctx,
1659
- struct lm_ggml_tensor * a,
1660
- struct lm_ggml_tensor * af, // "a"/input used in forward pass
1661
- enum lm_ggml_op_pool op,
1662
- int k0,
1663
- int k1,
1664
- int s0,
1665
- int s1,
1666
- float p0,
1667
- float p1);
1668
-
1669
- // nearest interpolate
1670
- // multiplies ne0 and ne1 by scale factor
1671
- // used in stable-diffusion
1672
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_upscale(
1673
- struct lm_ggml_context * ctx,
1674
- struct lm_ggml_tensor * a,
1675
- int scale_factor);
1676
-
1677
- // nearest interpolate
1678
- // nearest interpolate to specified dimensions
1679
- // used in tortoise.cpp
1680
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_upscale_ext(
1681
- struct lm_ggml_context * ctx,
1682
- struct lm_ggml_tensor * a,
1683
- int ne0,
1684
- int ne1,
1685
- int ne2,
1686
- int ne3);
1687
-
1688
- // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
1689
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_pad(
1690
- struct lm_ggml_context * ctx,
1691
- struct lm_ggml_tensor * a,
1692
- int p0,
1693
- int p1,
1694
- int p2,
1695
- int p3);
1696
-
1697
- // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
1698
- // timesteps: [N,]
1699
- // return: [N, dim]
1700
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_timestep_embedding(
1701
- struct lm_ggml_context * ctx,
1702
- struct lm_ggml_tensor * timesteps,
1703
- int dim,
1704
- int max_period);
1705
-
1706
- // sort rows
1707
- enum lm_ggml_sort_order {
1708
- LM_GGML_SORT_ORDER_ASC,
1709
- LM_GGML_SORT_ORDER_DESC,
1710
- };
1711
-
1712
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_argsort(
1713
- struct lm_ggml_context * ctx,
1714
- struct lm_ggml_tensor * a,
1715
- enum lm_ggml_sort_order order);
1716
-
1717
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_arange(
1718
- struct lm_ggml_context * ctx,
1719
- float start,
1720
- float stop,
1721
- float step);
1722
-
1723
- // top k elements per row
1724
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_top_k(
1725
- struct lm_ggml_context * ctx,
1726
- struct lm_ggml_tensor * a,
1727
- int k);
1728
-
1729
- #define LM_GGML_KQ_MASK_PAD 32
1730
-
1731
- // q: [n_embd, n_batch, n_head, 1]
1732
- // k: [n_embd, n_kv, n_head_kv, 1]
1733
- // v: [n_embd, n_kv, n_head_kv, 1] !! not transposed !!
1734
- // mask: [n_kv, n_batch_pad, 1, 1] !! n_batch_pad = LM_GGML_PAD(n_batch, LM_GGML_KQ_MASK_PAD) !!
1735
- // res: [n_embd, n_head, n_batch, 1] !! permuted !!
1736
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_flash_attn_ext(
1737
- struct lm_ggml_context * ctx,
1738
- struct lm_ggml_tensor * q,
1739
- struct lm_ggml_tensor * k,
1740
- struct lm_ggml_tensor * v,
1741
- struct lm_ggml_tensor * mask,
1742
- float scale,
1743
- float max_bias,
1744
- float logit_softcap);
1745
-
1746
- LM_GGML_API void lm_ggml_flash_attn_ext_set_prec(
1747
- struct lm_ggml_tensor * a,
1748
- enum lm_ggml_prec prec);
1749
-
1750
- LM_GGML_API enum lm_ggml_prec lm_ggml_flash_attn_ext_get_prec(
1751
- const struct lm_ggml_tensor * a);
1752
-
1753
- // TODO: needs to be adapted to lm_ggml_flash_attn_ext
1754
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_flash_attn_back(
1755
- struct lm_ggml_context * ctx,
1756
- struct lm_ggml_tensor * q,
1757
- struct lm_ggml_tensor * k,
1758
- struct lm_ggml_tensor * v,
1759
- struct lm_ggml_tensor * d,
1760
- bool masked);
1761
-
1762
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_ssm_conv(
1763
- struct lm_ggml_context * ctx,
1764
- struct lm_ggml_tensor * sx,
1765
- struct lm_ggml_tensor * c);
1766
-
1767
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_ssm_scan(
1768
- struct lm_ggml_context * ctx,
1769
- struct lm_ggml_tensor * s,
1770
- struct lm_ggml_tensor * x,
1771
- struct lm_ggml_tensor * dt,
1772
- struct lm_ggml_tensor * A,
1773
- struct lm_ggml_tensor * B,
1774
- struct lm_ggml_tensor * C);
1775
-
1776
- // partition into non-overlapping windows with padding if needed
1777
- // example:
1778
- // a: 768 64 64 1
1779
- // w: 14
1780
- // res: 768 14 14 25
1781
- // used in sam
1782
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_win_part(
1783
- struct lm_ggml_context * ctx,
1784
- struct lm_ggml_tensor * a,
1785
- int w);
1786
-
1787
- // reverse of lm_ggml_win_part
1788
- // used in sam
1789
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_win_unpart(
1790
- struct lm_ggml_context * ctx,
1791
- struct lm_ggml_tensor * a,
1792
- int w0,
1793
- int h0,
1794
- int w);
1795
-
1796
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_unary(
1797
- struct lm_ggml_context * ctx,
1798
- struct lm_ggml_tensor * a,
1799
- enum lm_ggml_unary_op op);
1800
-
1801
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_unary_inplace(
1802
- struct lm_ggml_context * ctx,
1803
- struct lm_ggml_tensor * a,
1804
- enum lm_ggml_unary_op op);
1805
-
1806
- // used in sam
1807
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rel_pos(
1808
- struct lm_ggml_context * ctx,
1809
- struct lm_ggml_tensor * a,
1810
- int qh,
1811
- int kh);
1812
-
1813
- // used in sam
1814
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_rel_pos(
1815
- struct lm_ggml_context * ctx,
1816
- struct lm_ggml_tensor * a,
1817
- struct lm_ggml_tensor * pw,
1818
- struct lm_ggml_tensor * ph);
1819
-
1820
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_rel_pos_inplace(
1821
- struct lm_ggml_context * ctx,
1822
- struct lm_ggml_tensor * a,
1823
- struct lm_ggml_tensor * pw,
1824
- struct lm_ggml_tensor * ph);
1825
-
1826
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_rwkv_wkv6(
1827
- struct lm_ggml_context * ctx,
1828
- struct lm_ggml_tensor * k,
1829
- struct lm_ggml_tensor * v,
1830
- struct lm_ggml_tensor * r,
1831
- struct lm_ggml_tensor * tf,
1832
- struct lm_ggml_tensor * td,
1833
- struct lm_ggml_tensor * state);
1834
-
1835
- // custom operators
1836
-
1837
- typedef void (*lm_ggml_unary_op_f32_t) (const int, float *, const float *);
1838
- typedef void (*lm_ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
1839
-
1840
- typedef void (*lm_ggml_custom1_op_f32_t)(struct lm_ggml_tensor *, const struct lm_ggml_tensor *);
1841
- typedef void (*lm_ggml_custom2_op_f32_t)(struct lm_ggml_tensor *, const struct lm_ggml_tensor *, const struct lm_ggml_tensor *);
1842
- typedef void (*lm_ggml_custom3_op_f32_t)(struct lm_ggml_tensor *, const struct lm_ggml_tensor *, const struct lm_ggml_tensor *, const struct lm_ggml_tensor *);
1843
-
1844
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_unary_f32(
1845
- struct lm_ggml_context * ctx,
1846
- struct lm_ggml_tensor * a,
1847
- lm_ggml_unary_op_f32_t fun),
1848
- "use lm_ggml_map_custom1 instead");
1849
-
1850
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_unary_inplace_f32(
1851
- struct lm_ggml_context * ctx,
1852
- struct lm_ggml_tensor * a,
1853
- lm_ggml_unary_op_f32_t fun),
1854
- "use lm_ggml_map_custom1_inplace instead");
1855
-
1856
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_binary_f32(
1857
- struct lm_ggml_context * ctx,
1858
- struct lm_ggml_tensor * a,
1859
- struct lm_ggml_tensor * b,
1860
- lm_ggml_binary_op_f32_t fun),
1861
- "use lm_ggml_map_custom2 instead");
1862
-
1863
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_binary_inplace_f32(
1864
- struct lm_ggml_context * ctx,
1865
- struct lm_ggml_tensor * a,
1866
- struct lm_ggml_tensor * b,
1867
- lm_ggml_binary_op_f32_t fun),
1868
- "use lm_ggml_map_custom2_inplace instead");
1869
-
1870
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_f32(
1871
- struct lm_ggml_context * ctx,
1872
- struct lm_ggml_tensor * a,
1873
- lm_ggml_custom1_op_f32_t fun),
1874
- "use lm_ggml_map_custom1 instead");
1875
-
1876
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_inplace_f32(
1877
- struct lm_ggml_context * ctx,
1878
- struct lm_ggml_tensor * a,
1879
- lm_ggml_custom1_op_f32_t fun),
1880
- "use lm_ggml_map_custom1_inplace instead");
1881
-
1882
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_f32(
1883
- struct lm_ggml_context * ctx,
1884
- struct lm_ggml_tensor * a,
1885
- struct lm_ggml_tensor * b,
1886
- lm_ggml_custom2_op_f32_t fun),
1887
- "use lm_ggml_map_custom2 instead");
1888
-
1889
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_inplace_f32(
1890
- struct lm_ggml_context * ctx,
1891
- struct lm_ggml_tensor * a,
1892
- struct lm_ggml_tensor * b,
1893
- lm_ggml_custom2_op_f32_t fun),
1894
- "use lm_ggml_map_custom2_inplace instead");
1895
-
1896
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_f32(
1897
- struct lm_ggml_context * ctx,
1898
- struct lm_ggml_tensor * a,
1899
- struct lm_ggml_tensor * b,
1900
- struct lm_ggml_tensor * c,
1901
- lm_ggml_custom3_op_f32_t fun),
1902
- "use lm_ggml_map_custom3 instead");
1903
-
1904
- LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_inplace_f32(
1905
- struct lm_ggml_context * ctx,
1906
- struct lm_ggml_tensor * a,
1907
- struct lm_ggml_tensor * b,
1908
- struct lm_ggml_tensor * c,
1909
- lm_ggml_custom3_op_f32_t fun),
1910
- "use lm_ggml_map_custom3_inplace instead");
1911
-
1912
- // custom operators v2
1913
-
1914
- typedef void (*lm_ggml_custom1_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, int ith, int nth, void * userdata);
1915
- typedef void (*lm_ggml_custom2_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, const struct lm_ggml_tensor * b, int ith, int nth, void * userdata);
1916
- typedef void (*lm_ggml_custom3_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, const struct lm_ggml_tensor * b, const struct lm_ggml_tensor * c, int ith, int nth, void * userdata);
1917
-
1918
- #define LM_GGML_N_TASKS_MAX (-1)
1919
- // n_tasks == LM_GGML_N_TASKS_MAX means to use max number of tasks
1920
-
1921
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1(
1922
- struct lm_ggml_context * ctx,
1923
- struct lm_ggml_tensor * a,
1924
- lm_ggml_custom1_op_t fun,
1925
- int n_tasks,
1926
- void * userdata);
1927
-
1928
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_inplace(
1929
- struct lm_ggml_context * ctx,
1930
- struct lm_ggml_tensor * a,
1931
- lm_ggml_custom1_op_t fun,
1932
- int n_tasks,
1933
- void * userdata);
1934
-
1935
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2(
1936
- struct lm_ggml_context * ctx,
1937
- struct lm_ggml_tensor * a,
1938
- struct lm_ggml_tensor * b,
1939
- lm_ggml_custom2_op_t fun,
1940
- int n_tasks,
1941
- void * userdata);
1942
-
1943
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_inplace(
1944
- struct lm_ggml_context * ctx,
1945
- struct lm_ggml_tensor * a,
1946
- struct lm_ggml_tensor * b,
1947
- lm_ggml_custom2_op_t fun,
1948
- int n_tasks,
1949
- void * userdata);
1950
-
1951
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3(
1952
- struct lm_ggml_context * ctx,
1953
- struct lm_ggml_tensor * a,
1954
- struct lm_ggml_tensor * b,
1955
- struct lm_ggml_tensor * c,
1956
- lm_ggml_custom3_op_t fun,
1957
- int n_tasks,
1958
- void * userdata);
1959
-
1960
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_inplace(
1961
- struct lm_ggml_context * ctx,
1962
- struct lm_ggml_tensor * a,
1963
- struct lm_ggml_tensor * b,
1964
- struct lm_ggml_tensor * c,
1965
- lm_ggml_custom3_op_t fun,
1966
- int n_tasks,
1967
- void * userdata);
1968
-
1969
- // loss function
1970
-
1971
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cross_entropy_loss(
1972
- struct lm_ggml_context * ctx,
1973
- struct lm_ggml_tensor * a, // logits
1974
- struct lm_ggml_tensor * b); // labels
1975
-
1976
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_cross_entropy_loss_back(
1977
- struct lm_ggml_context * ctx,
1978
- struct lm_ggml_tensor * a, // logits
1979
- struct lm_ggml_tensor * b, // labels
1980
- struct lm_ggml_tensor * c); // gradients of cross_entropy_loss result
1981
-
1982
- // AdamW optimizer step
1983
- // Paper: https://arxiv.org/pdf/1711.05101v3.pdf
1984
- // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
1985
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_opt_step_adamw(
1986
- struct lm_ggml_context * ctx,
1987
- struct lm_ggml_tensor * a,
1988
- struct lm_ggml_tensor * grad,
1989
- float alpha,
1990
- float beta1,
1991
- float beta2,
1992
- float eps,
1993
- float wd); // weight decay
1994
-
1995
- //
1996
- // automatic differentiation
1997
- //
1998
-
1999
- LM_GGML_API void lm_ggml_build_forward_expand (struct lm_ggml_cgraph * cgraph, struct lm_ggml_tensor * tensor);
2000
- LM_GGML_API void lm_ggml_build_backward_expand(struct lm_ggml_context * ctx, struct lm_ggml_cgraph * gf, struct lm_ggml_cgraph * gb, bool accumulate);
2001
-
2002
- LM_GGML_API void lm_ggml_build_opt_adamw(
2003
- struct lm_ggml_context * ctx,
2004
- struct lm_ggml_cgraph * gf,
2005
- struct lm_ggml_cgraph * gb,
2006
- float alpha,
2007
- float beta1,
2008
- float beta2,
2009
- float eps,
2010
- float wd); // weight decay
2011
-
2012
- // graph allocation in a context
2013
- LM_GGML_API struct lm_ggml_cgraph * lm_ggml_new_graph (struct lm_ggml_context * ctx); // size = LM_GGML_DEFAULT_GRAPH_SIZE, grads = false
2014
- LM_GGML_API struct lm_ggml_cgraph * lm_ggml_new_graph_custom(struct lm_ggml_context * ctx, size_t size, bool grads);
2015
- LM_GGML_API struct lm_ggml_cgraph * lm_ggml_graph_dup (struct lm_ggml_context * ctx, struct lm_ggml_cgraph * cgraph);
2016
- LM_GGML_API void lm_ggml_graph_cpy (struct lm_ggml_cgraph * src, struct lm_ggml_cgraph * dst);
2017
- LM_GGML_API void lm_ggml_graph_reset (struct lm_ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
2018
- LM_GGML_API void lm_ggml_graph_clear (struct lm_ggml_cgraph * cgraph);
2019
-
2020
- LM_GGML_API int lm_ggml_graph_size (struct lm_ggml_cgraph * cgraph);
2021
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_node (struct lm_ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i]
2022
- LM_GGML_API struct lm_ggml_tensor ** lm_ggml_graph_nodes (struct lm_ggml_cgraph * cgraph);
2023
- LM_GGML_API int lm_ggml_graph_n_nodes(struct lm_ggml_cgraph * cgraph);
2024
-
2025
- LM_GGML_API void lm_ggml_graph_add_node(struct lm_ggml_cgraph * cgraph, struct lm_ggml_tensor * tensor);
2026
-
2027
- LM_GGML_API size_t lm_ggml_graph_overhead(void);
2028
- LM_GGML_API size_t lm_ggml_graph_overhead_custom(size_t size, bool grads);
2029
-
2030
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_tensor(struct lm_ggml_cgraph * cgraph, const char * name);
2031
-
2032
- LM_GGML_API void lm_ggml_graph_export(const struct lm_ggml_cgraph * cgraph, const char * fname);
2033
- LM_GGML_API struct lm_ggml_cgraph * lm_ggml_graph_import(const char * fname, struct lm_ggml_context ** ctx_data, struct lm_ggml_context ** ctx_eval);
2034
-
2035
- // print info and performance information for the graph
2036
- LM_GGML_API void lm_ggml_graph_print(const struct lm_ggml_cgraph * cgraph);
2037
-
2038
- // dump the graph into a file using the dot format
2039
- LM_GGML_API void lm_ggml_graph_dump_dot(const struct lm_ggml_cgraph * gb, const struct lm_ggml_cgraph * gf, const char * filename);
2040
-
2041
- // build gradient checkpointing backward graph gb for gf using provided checkpoints
2042
- // gb_tmp will contain original backward graph with rewritten backward process nodes,
2043
- // but without the second forward pass nodes.
2044
- LM_GGML_API void lm_ggml_build_backward_gradient_checkpointing(
2045
- struct lm_ggml_context * ctx,
2046
- struct lm_ggml_cgraph * gf,
2047
- struct lm_ggml_cgraph * gb,
2048
- struct lm_ggml_cgraph * gb_tmp,
2049
- struct lm_ggml_tensor * * checkpoints,
2050
- int n_checkpoints);
2051
- //
2052
- // optimization
2053
- //
2054
-
2055
- // optimization methods
2056
- enum lm_ggml_opt_type {
2057
- LM_GGML_OPT_TYPE_ADAM,
2058
- LM_GGML_OPT_TYPE_LBFGS,
2059
- };
2060
-
2061
- // linesearch methods
2062
- enum lm_ggml_linesearch {
2063
- LM_GGML_LINESEARCH_DEFAULT = 1,
2064
-
2065
- LM_GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0,
2066
- LM_GGML_LINESEARCH_BACKTRACKING_WOLFE = 1,
2067
- LM_GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2,
2068
- };
2069
-
2070
- // optimization return values
2071
- enum lm_ggml_opt_result {
2072
- LM_GGML_OPT_RESULT_OK = 0,
2073
- LM_GGML_OPT_RESULT_DID_NOT_CONVERGE,
2074
- LM_GGML_OPT_RESULT_NO_CONTEXT,
2075
- LM_GGML_OPT_RESULT_INVALID_WOLFE,
2076
- LM_GGML_OPT_RESULT_FAIL,
2077
- LM_GGML_OPT_RESULT_CANCEL,
2078
-
2079
- LM_GGML_LINESEARCH_FAIL = -128,
2080
- LM_GGML_LINESEARCH_MINIMUM_STEP,
2081
- LM_GGML_LINESEARCH_MAXIMUM_STEP,
2082
- LM_GGML_LINESEARCH_MAXIMUM_ITERATIONS,
2083
- LM_GGML_LINESEARCH_INVALID_PARAMETERS,
2084
- };
2085
-
2086
- typedef void (*lm_ggml_opt_callback)(void * data, int accum_step, float * sched, bool * cancel);
2087
- typedef void (*lm_ggml_log_callback)(enum lm_ggml_log_level level, const char * text, void * user_data);
2088
-
2089
- // Set callback for all future logging events.
2090
- // If this is not called, or NULL is supplied, everything is output on stderr.
2091
- LM_GGML_API void lm_ggml_log_set(lm_ggml_log_callback log_callback, void * user_data);
2092
-
2093
- // optimization parameters
2094
- //
2095
- // see ggml.c (lm_ggml_opt_default_params) for default values
2096
- //
2097
- struct lm_ggml_opt_params {
2098
- enum lm_ggml_opt_type type;
2099
-
2100
- size_t graph_size;
2101
-
2102
- int n_threads;
2103
-
2104
- // delta-based convergence test
2105
- //
2106
- // if past == 0 - disabled
2107
- // if past > 0:
2108
- // stop if |f(x) - f(x_past)| < delta * max(1, |f(x)|)
2109
- //
2110
- int past;
2111
- float delta;
2112
-
2113
- // maximum number of iterations without improvement
2114
- //
2115
- // if 0 - disabled
2116
- // if > 0:
2117
- // assume convergence if no cost improvement in this number of iterations
2118
- //
2119
- int max_no_improvement;
2120
-
2121
- bool print_forward_graph;
2122
- bool print_backward_graph;
2123
-
2124
- int n_gradient_accumulation;
2125
-
2126
- // ADAM parameters
2127
- struct {
2128
- int n_iter;
2129
-
2130
- float sched; // schedule multiplier (fixed, decay or warmup)
2131
- float decay; // weight decay for AdamW, use 0.0f to disable
2132
- int decay_min_ndim; // minimum number of tensor dimension to apply weight decay
2133
- float alpha; // learning rate
2134
- float beta1;
2135
- float beta2;
2136
- float eps; // epsilon for numerical stability
2137
- float eps_f; // epsilon for convergence test
2138
- float eps_g; // epsilon for convergence test
2139
- float gclip; // gradient clipping
2140
- } adam;
2141
-
2142
- // LBFGS parameters
2143
- struct {
2144
- int m; // number of corrections to approximate the inv. Hessian
2145
- int n_iter;
2146
- int max_linesearch;
2147
-
2148
- float eps; // convergence tolerance
2149
- float ftol; // line search tolerance
2150
- float wolfe;
2151
- float min_step;
2152
- float max_step;
2153
-
2154
- enum lm_ggml_linesearch linesearch;
2155
- } lbfgs;
2156
- };
2157
-
2158
- struct lm_ggml_opt_context {
2159
- struct lm_ggml_context * ctx;
2160
- struct lm_ggml_opt_params params;
2161
-
2162
- int iter;
2163
- int64_t nx; // number of parameter elements
2164
-
2165
- bool just_initialized;
2166
-
2167
- float loss_before;
2168
- float loss_after;
2169
-
2170
- struct {
2171
- struct lm_ggml_tensor * g; // current gradient
2172
- struct lm_ggml_tensor * m; // first moment
2173
- struct lm_ggml_tensor * v; // second moment
2174
- struct lm_ggml_tensor * pf; // past function values
2175
- float fx_best;
2176
- float fx_prev;
2177
- int n_no_improvement;
2178
- } adam;
2179
-
2180
- struct {
2181
- struct lm_ggml_tensor * x; // current parameters
2182
- struct lm_ggml_tensor * xp; // previous parameters
2183
- struct lm_ggml_tensor * g; // current gradient
2184
- struct lm_ggml_tensor * gp; // previous gradient
2185
- struct lm_ggml_tensor * d; // search direction
2186
- struct lm_ggml_tensor * pf; // past function values
2187
- struct lm_ggml_tensor * lmal; // the L-BFGS memory alpha
2188
- struct lm_ggml_tensor * lmys; // the L-BFGS memory ys
2189
- struct lm_ggml_tensor * lms; // the L-BFGS memory s
2190
- struct lm_ggml_tensor * lmy; // the L-BFGS memory y
2191
- float fx_best;
2192
- float step;
2193
- int j;
2194
- int k;
2195
- int end;
2196
- int n_no_improvement;
2197
- } lbfgs;
2198
- };
2199
-
2200
- LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_zero(struct lm_ggml_tensor * tensor);
2201
-
2202
- LM_GGML_API struct lm_ggml_opt_params lm_ggml_opt_default_params(enum lm_ggml_opt_type type);
2203
-
2204
- // optimize the function defined by the tensor f
2205
- LM_GGML_API enum lm_ggml_opt_result lm_ggml_opt(
2206
- struct lm_ggml_context * ctx,
2207
- struct lm_ggml_opt_params params,
2208
- struct lm_ggml_tensor * f);
2209
-
2210
- // initialize optimizer context
2211
- LM_GGML_API void lm_ggml_opt_init(
2212
- struct lm_ggml_context * ctx,
2213
- struct lm_ggml_opt_context * opt,
2214
- struct lm_ggml_opt_params params,
2215
- int64_t nx);
2216
-
2217
- // continue optimizing the function defined by the tensor f
2218
- LM_GGML_API enum lm_ggml_opt_result lm_ggml_opt_resume(
2219
- struct lm_ggml_context * ctx,
2220
- struct lm_ggml_opt_context * opt,
2221
- struct lm_ggml_tensor * f);
2222
-
2223
- // continue optimizing the function defined by the tensor f
2224
- LM_GGML_API enum lm_ggml_opt_result lm_ggml_opt_resume_g(
2225
- struct lm_ggml_context * ctx,
2226
- struct lm_ggml_opt_context * opt,
2227
- struct lm_ggml_tensor * f,
2228
- struct lm_ggml_cgraph * gf,
2229
- struct lm_ggml_cgraph * gb,
2230
- lm_ggml_opt_callback callback,
2231
- void * callback_data);
2232
-
2233
- //
2234
- // quantization
2235
- //
2236
-
2237
- // - lm_ggml_quantize_init can be called multiple times with the same type
2238
- // it will only initialize the quantization tables for the first call or after lm_ggml_quantize_free
2239
- // automatically called by lm_ggml_quantize_chunk for convenience
2240
- //
2241
- // - lm_ggml_quantize_free will free any memory allocated by lm_ggml_quantize_init
2242
- // call this at the end of the program to avoid memory leaks
2243
- //
2244
- // note: these are thread-safe
2245
- //
2246
- LM_GGML_API void lm_ggml_quantize_init(enum lm_ggml_type type);
2247
- LM_GGML_API void lm_ggml_quantize_free(void);
2248
-
2249
- // some quantization type cannot be used without an importance matrix
2250
- LM_GGML_API bool lm_ggml_quantize_requires_imatrix(enum lm_ggml_type type);
2251
-
2252
- // calls lm_ggml_quantize_init internally (i.e. can allocate memory)
2253
- LM_GGML_API size_t lm_ggml_quantize_chunk(
2254
- enum lm_ggml_type type,
2255
- const float * src,
2256
- void * dst,
2257
- int64_t start,
2258
- int64_t nrows,
2259
- int64_t n_per_row,
2260
- const float * imatrix);
2261
-
2262
- //
2263
- // gguf
2264
- //
2265
-
2266
- enum lm_gguf_type {
2267
- LM_GGUF_TYPE_UINT8 = 0,
2268
- LM_GGUF_TYPE_INT8 = 1,
2269
- LM_GGUF_TYPE_UINT16 = 2,
2270
- LM_GGUF_TYPE_INT16 = 3,
2271
- LM_GGUF_TYPE_UINT32 = 4,
2272
- LM_GGUF_TYPE_INT32 = 5,
2273
- LM_GGUF_TYPE_FLOAT32 = 6,
2274
- LM_GGUF_TYPE_BOOL = 7,
2275
- LM_GGUF_TYPE_STRING = 8,
2276
- LM_GGUF_TYPE_ARRAY = 9,
2277
- LM_GGUF_TYPE_UINT64 = 10,
2278
- LM_GGUF_TYPE_INT64 = 11,
2279
- LM_GGUF_TYPE_FLOAT64 = 12,
2280
- LM_GGUF_TYPE_COUNT, // marks the end of the enum
2281
- };
2282
-
2283
- struct lm_gguf_context;
2284
-
2285
- struct lm_gguf_init_params {
2286
- bool no_alloc;
2287
-
2288
- // if not NULL, create a lm_ggml_context and allocate the tensor data in it
2289
- struct lm_ggml_context ** ctx;
2290
- };
2291
-
2292
- LM_GGML_API struct lm_gguf_context * lm_gguf_init_empty(void);
2293
- LM_GGML_API struct lm_gguf_context * lm_gguf_init_from_file(const char * fname, struct lm_gguf_init_params params);
2294
- //LM_GGML_API struct lm_gguf_context * lm_gguf_init_from_buffer(..);
2295
-
2296
- LM_GGML_API void lm_gguf_free(struct lm_gguf_context * ctx);
2297
-
2298
- LM_GGML_API const char * lm_gguf_type_name(enum lm_gguf_type type);
2299
-
2300
- LM_GGML_API int lm_gguf_get_version (const struct lm_gguf_context * ctx);
2301
- LM_GGML_API size_t lm_gguf_get_alignment (const struct lm_gguf_context * ctx);
2302
- LM_GGML_API size_t lm_gguf_get_data_offset(const struct lm_gguf_context * ctx);
2303
- LM_GGML_API void * lm_gguf_get_data (const struct lm_gguf_context * ctx);
2304
-
2305
- LM_GGML_API int lm_gguf_get_n_kv(const struct lm_gguf_context * ctx);
2306
- LM_GGML_API int lm_gguf_find_key(const struct lm_gguf_context * ctx, const char * key);
2307
- LM_GGML_API const char * lm_gguf_get_key (const struct lm_gguf_context * ctx, int key_id);
2308
-
2309
- LM_GGML_API enum lm_gguf_type lm_gguf_get_kv_type (const struct lm_gguf_context * ctx, int key_id);
2310
- LM_GGML_API enum lm_gguf_type lm_gguf_get_arr_type(const struct lm_gguf_context * ctx, int key_id);
2311
-
2312
- // will abort if the wrong type is used for the key
2313
- LM_GGML_API uint8_t lm_gguf_get_val_u8 (const struct lm_gguf_context * ctx, int key_id);
2314
- LM_GGML_API int8_t lm_gguf_get_val_i8 (const struct lm_gguf_context * ctx, int key_id);
2315
- LM_GGML_API uint16_t lm_gguf_get_val_u16 (const struct lm_gguf_context * ctx, int key_id);
2316
- LM_GGML_API int16_t lm_gguf_get_val_i16 (const struct lm_gguf_context * ctx, int key_id);
2317
- LM_GGML_API uint32_t lm_gguf_get_val_u32 (const struct lm_gguf_context * ctx, int key_id);
2318
- LM_GGML_API int32_t lm_gguf_get_val_i32 (const struct lm_gguf_context * ctx, int key_id);
2319
- LM_GGML_API float lm_gguf_get_val_f32 (const struct lm_gguf_context * ctx, int key_id);
2320
- LM_GGML_API uint64_t lm_gguf_get_val_u64 (const struct lm_gguf_context * ctx, int key_id);
2321
- LM_GGML_API int64_t lm_gguf_get_val_i64 (const struct lm_gguf_context * ctx, int key_id);
2322
- LM_GGML_API double lm_gguf_get_val_f64 (const struct lm_gguf_context * ctx, int key_id);
2323
- LM_GGML_API bool lm_gguf_get_val_bool(const struct lm_gguf_context * ctx, int key_id);
2324
- LM_GGML_API const char * lm_gguf_get_val_str (const struct lm_gguf_context * ctx, int key_id);
2325
- LM_GGML_API const void * lm_gguf_get_val_data(const struct lm_gguf_context * ctx, int key_id);
2326
- LM_GGML_API int lm_gguf_get_arr_n (const struct lm_gguf_context * ctx, int key_id);
2327
- LM_GGML_API const void * lm_gguf_get_arr_data(const struct lm_gguf_context * ctx, int key_id);
2328
- LM_GGML_API const char * lm_gguf_get_arr_str (const struct lm_gguf_context * ctx, int key_id, int i);
2329
-
2330
- LM_GGML_API int lm_gguf_get_n_tensors (const struct lm_gguf_context * ctx);
2331
- LM_GGML_API int lm_gguf_find_tensor (const struct lm_gguf_context * ctx, const char * name);
2332
- LM_GGML_API size_t lm_gguf_get_tensor_offset(const struct lm_gguf_context * ctx, int i);
2333
- LM_GGML_API char * lm_gguf_get_tensor_name (const struct lm_gguf_context * ctx, int i);
2334
- LM_GGML_API enum lm_ggml_type lm_gguf_get_tensor_type (const struct lm_gguf_context * ctx, int i);
2335
-
2336
- // removes key if it exists
2337
- LM_GGML_API void lm_gguf_remove_key(struct lm_gguf_context * ctx, const char * key);
2338
-
2339
- // overrides existing values or adds a new one
2340
- LM_GGML_API void lm_gguf_set_val_u8 (struct lm_gguf_context * ctx, const char * key, uint8_t val);
2341
- LM_GGML_API void lm_gguf_set_val_i8 (struct lm_gguf_context * ctx, const char * key, int8_t val);
2342
- LM_GGML_API void lm_gguf_set_val_u16 (struct lm_gguf_context * ctx, const char * key, uint16_t val);
2343
- LM_GGML_API void lm_gguf_set_val_i16 (struct lm_gguf_context * ctx, const char * key, int16_t val);
2344
- LM_GGML_API void lm_gguf_set_val_u32 (struct lm_gguf_context * ctx, const char * key, uint32_t val);
2345
- LM_GGML_API void lm_gguf_set_val_i32 (struct lm_gguf_context * ctx, const char * key, int32_t val);
2346
- LM_GGML_API void lm_gguf_set_val_f32 (struct lm_gguf_context * ctx, const char * key, float val);
2347
- LM_GGML_API void lm_gguf_set_val_u64 (struct lm_gguf_context * ctx, const char * key, uint64_t val);
2348
- LM_GGML_API void lm_gguf_set_val_i64 (struct lm_gguf_context * ctx, const char * key, int64_t val);
2349
- LM_GGML_API void lm_gguf_set_val_f64 (struct lm_gguf_context * ctx, const char * key, double val);
2350
- LM_GGML_API void lm_gguf_set_val_bool(struct lm_gguf_context * ctx, const char * key, bool val);
2351
- LM_GGML_API void lm_gguf_set_val_str (struct lm_gguf_context * ctx, const char * key, const char * val);
2352
- LM_GGML_API void lm_gguf_set_arr_data(struct lm_gguf_context * ctx, const char * key, enum lm_gguf_type type, const void * data, int n);
2353
- LM_GGML_API void lm_gguf_set_arr_str (struct lm_gguf_context * ctx, const char * key, const char ** data, int n);
2354
-
2355
- // set or add KV pairs from another context
2356
- LM_GGML_API void lm_gguf_set_kv(struct lm_gguf_context * ctx, struct lm_gguf_context * src);
2357
-
2358
- // manage tensor info
2359
- LM_GGML_API void lm_gguf_add_tensor(struct lm_gguf_context * ctx, const struct lm_ggml_tensor * tensor);
2360
- LM_GGML_API void lm_gguf_set_tensor_type(struct lm_gguf_context * ctx, const char * name, enum lm_ggml_type type);
2361
- LM_GGML_API void lm_gguf_set_tensor_data(struct lm_gguf_context * ctx, const char * name, const void * data, size_t size);
2362
-
2363
- // writing gguf files can be done in 2 ways:
2364
- //
2365
- // - write the entire lm_gguf_context to a binary file in a single pass:
2366
- //
2367
- // lm_gguf_write_to_file(ctx, fname);
2368
- //
2369
- // - first prepare a file with a placeholder for the meta data, write the tensor data, then write the meta data:
2370
- //
2371
- // FILE * f = fopen(fname, "wb");
2372
- // fseek(f, lm_gguf_get_meta_size(ctx), SEEK_SET);
2373
- // fwrite(f, ...);
2374
- // void * data = lm_gguf_meta_get_meta_data(ctx);
2375
- // fseek(f, 0, SEEK_SET);
2376
- // fwrite(f, data, lm_gguf_get_meta_size(ctx));
2377
- // free(data);
2378
- // fclose(f);
2379
- //
2380
-
2381
- // write the entire context to a binary file
2382
- LM_GGML_API void lm_gguf_write_to_file(const struct lm_gguf_context * ctx, const char * fname, bool only_meta);
2383
-
2384
- // get the size in bytes of the meta data (header, kv pairs, tensor info) including padding
2385
- LM_GGML_API size_t lm_gguf_get_meta_size(const struct lm_gguf_context * ctx);
2386
- LM_GGML_API void lm_gguf_get_meta_data(const struct lm_gguf_context * ctx, void * data);
2387
-
2388
- #ifdef __cplusplus
2389
- // restrict not standard in C++
2390
- #define LM_GGML_RESTRICT
2391
- #else
2392
- #define LM_GGML_RESTRICT restrict
2393
- #endif
2394
- typedef void (*lm_ggml_to_float_t) (const void * LM_GGML_RESTRICT x, float * LM_GGML_RESTRICT y, int64_t k);
2395
- typedef void (*lm_ggml_from_float_t)(const float * LM_GGML_RESTRICT x, void * LM_GGML_RESTRICT y, int64_t k);
2396
-
2397
- struct lm_ggml_type_traits {
2398
- const char * type_name;
2399
- int64_t blck_size;
2400
- int64_t blck_size_interleave; // interleave elements in blocks
2401
- size_t type_size;
2402
- bool is_quantized;
2403
- lm_ggml_to_float_t to_float;
2404
- lm_ggml_from_float_t from_float_ref;
2405
- };
2406
-
2407
- LM_GGML_API const struct lm_ggml_type_traits * lm_ggml_get_type_traits(enum lm_ggml_type type);
2408
-
2409
- #ifdef __cplusplus
2410
- }
2411
- #endif
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/ggerganov/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 lm_ggml_init_params params = {
36
+ // .mem_size = 16*1024*1024,
37
+ // .mem_buffer = NULL,
38
+ // };
39
+ //
40
+ // // memory allocation happens here
41
+ // struct lm_ggml_context * ctx = lm_ggml_init(params);
42
+ //
43
+ // struct lm_ggml_tensor * x = lm_ggml_new_tensor_1d(ctx, LM_GGML_TYPE_F32, 1);
44
+ //
45
+ // lm_ggml_set_param(ctx, x); // x is an input variable
46
+ //
47
+ // struct lm_ggml_tensor * a = lm_ggml_new_tensor_1d(ctx, LM_GGML_TYPE_F32, 1);
48
+ // struct lm_ggml_tensor * b = lm_ggml_new_tensor_1d(ctx, LM_GGML_TYPE_F32, 1);
49
+ // struct lm_ggml_tensor * x2 = lm_ggml_mul(ctx, x, x);
50
+ // struct lm_ggml_tensor * f = lm_ggml_add(ctx, lm_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 lm_ggml_cgraph * gf = lm_ggml_new_graph(ctx);
62
+ // lm_ggml_build_forward_expand(gf, f);
63
+ //
64
+ // // set the input variable and parameter values
65
+ // lm_ggml_set_f32(x, 2.0f);
66
+ // lm_ggml_set_f32(a, 3.0f);
67
+ // lm_ggml_set_f32(b, 4.0f);
68
+ //
69
+ // lm_ggml_graph_compute_with_ctx(ctx, &gf, n_threads);
70
+ //
71
+ // printf("f = %f\n", lm_ggml_get_f32_1d(f, 0));
72
+ //
73
+ // ...
74
+ // }
75
+ //
76
+ // The actual computation is performed in the lm_ggml_graph_compute() function.
77
+ //
78
+ // The lm_ggml_new_tensor_...() functions create new tensors. They are allocated in the memory buffer provided to the
79
+ // lm_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 lm_ggml_used_mem() function to find out how much memory was
82
+ // actually needed.
83
+ //
84
+ // The lm_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 lm_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
+ // - lm_ggml_permute()
100
+ // - lm_ggml_conv_1d_1s()
101
+ // - lm_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 lm_ggml_tensor)
113
+ //
114
+ // The tensors are stored in memory via the lm_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 lm_ggml_tensor * c = lm_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 lm_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 lm_ggml_tensor * a = lm_ggml_new_tensor_2d(ctx, LM_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 lm_ggml_get_f32_1d() and lm_ggml_set_f32_1d() that can be used.
149
+ //
150
+ // ## The matrix multiplication operator (lm_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 LM_GGML_SHARED
177
+ # if defined(_WIN32) && !defined(__MINGW32__)
178
+ # ifdef LM_GGML_BUILD
179
+ # define LM_GGML_API __declspec(dllexport) extern
180
+ # else
181
+ # define LM_GGML_API __declspec(dllimport) extern
182
+ # endif
183
+ # else
184
+ # define LM_GGML_API __attribute__ ((visibility ("default"))) extern
185
+ # endif
186
+ #else
187
+ # define LM_GGML_API extern
188
+ #endif
189
+
190
+ // TODO: support for clang
191
+ #ifdef __GNUC__
192
+ # define LM_GGML_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
193
+ #elif defined(_MSC_VER)
194
+ # define LM_GGML_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
195
+ #else
196
+ # define LM_GGML_DEPRECATED(func, hint) func
197
+ #endif
198
+
199
+ #ifndef __GNUC__
200
+ # define LM_GGML_ATTRIBUTE_FORMAT(...)
201
+ #elif defined(__MINGW32__)
202
+ # define LM_GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
203
+ #else
204
+ # define LM_GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
205
+ #endif
206
+
207
+ #include <stdbool.h>
208
+ #include <stddef.h>
209
+ #include <stdint.h>
210
+ #include <stdio.h>
211
+ #include <string.h>
212
+
213
+ #define LM_GGML_FILE_MAGIC 0x67676d6c // "ggml"
214
+ #define LM_GGML_FILE_VERSION 2
215
+
216
+ #define LM_GGML_QNT_VERSION 2 // bump this on quantization format changes
217
+ #define LM_GGML_QNT_VERSION_FACTOR 1000 // do not change this
218
+
219
+ #define LM_GGML_MAX_DIMS 4
220
+ #define LM_GGML_MAX_PARAMS 2048
221
+ #define LM_GGML_MAX_SRC 10
222
+ #define LM_GGML_MAX_N_THREADS 512
223
+ #define LM_GGML_MAX_OP_PARAMS 64
224
+
225
+ #ifndef LM_GGML_MAX_NAME
226
+ # define LM_GGML_MAX_NAME 64
227
+ #endif
228
+
229
+ #define LM_GGML_DEFAULT_N_THREADS 4
230
+ #define LM_GGML_DEFAULT_GRAPH_SIZE 2048
231
+
232
+ #if UINTPTR_MAX == 0xFFFFFFFF
233
+ #define LM_GGML_MEM_ALIGN 4
234
+ #else
235
+ #define LM_GGML_MEM_ALIGN 16
236
+ #endif
237
+
238
+ #define LM_GGML_EXIT_SUCCESS 0
239
+ #define LM_GGML_EXIT_ABORTED 1
240
+
241
+ #define LM_GGML_ROPE_TYPE_NEOX 2
242
+
243
+ #define LM_GGUF_MAGIC "GGUF"
244
+
245
+ #define LM_GGUF_VERSION 3
246
+
247
+ #define LM_GGUF_DEFAULT_ALIGNMENT 32
248
+
249
+ #define LM_GGML_UNUSED(x) (void)(x)
250
+
251
+ #define LM_GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
252
+
253
+ #ifndef NDEBUG
254
+ # define LM_GGML_UNREACHABLE() do { fprintf(stderr, "statement should be unreachable\n"); abort(); } while(0)
255
+ #elif defined(__GNUC__)
256
+ # define LM_GGML_UNREACHABLE() __builtin_unreachable()
257
+ #elif defined(_MSC_VER)
258
+ # define LM_GGML_UNREACHABLE() __assume(0)
259
+ #else
260
+ # define LM_GGML_UNREACHABLE() ((void) 0)
261
+ #endif
262
+
263
+ #ifdef __cplusplus
264
+ # define LM_GGML_NORETURN [[noreturn]]
265
+ #elif defined(_MSC_VER)
266
+ # define LM_GGML_NORETURN __declspec(noreturn)
267
+ #else
268
+ # define LM_GGML_NORETURN _Noreturn
269
+ #endif
270
+
271
+ #define LM_GGML_ABORT(...) lm_ggml_abort((strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __VA_ARGS__)
272
+ #define LM_GGML_ASSERT(x) if (!(x)) LM_GGML_ABORT("LM_GGML_ASSERT(%s) failed", #x)
273
+
274
+ // used to copy the number of elements and stride in bytes of tensors into local variables.
275
+ // main purpose is to reduce code duplication and improve readability.
276
+ //
277
+ // example:
278
+ //
279
+ // LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
280
+ // LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
281
+ //
282
+ #define LM_GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
283
+ const type prefix##0 = (pointer)->array[0]; \
284
+ LM_GGML_UNUSED(prefix##0);
285
+ #define LM_GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
286
+ LM_GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
287
+ const type prefix##1 = (pointer)->array[1]; \
288
+ LM_GGML_UNUSED(prefix##1);
289
+ #define LM_GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
290
+ LM_GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
291
+ const type prefix##2 = (pointer)->array[2]; \
292
+ LM_GGML_UNUSED(prefix##2);
293
+ #define LM_GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
294
+ LM_GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
295
+ const type prefix##3 = (pointer)->array[3]; \
296
+ LM_GGML_UNUSED(prefix##3);
297
+
298
+ #define LM_GGML_TENSOR_UNARY_OP_LOCALS \
299
+ LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
300
+ LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
301
+ LM_GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
302
+ LM_GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
303
+
304
+ #define LM_GGML_TENSOR_BINARY_OP_LOCALS \
305
+ LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
306
+ LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
307
+ LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
308
+ LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
309
+ LM_GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
310
+ LM_GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
311
+
312
+ #define LM_GGML_TENSOR_BINARY_OP_LOCALS01 \
313
+ LM_GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
314
+ LM_GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
315
+ LM_GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
316
+ LM_GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
317
+
318
+ #ifdef __cplusplus
319
+ extern "C" {
320
+ #endif
321
+
322
+ LM_GGML_NORETURN LM_GGML_ATTRIBUTE_FORMAT(3, 4)
323
+ LM_GGML_API void lm_ggml_abort(const char * file, int line, const char * fmt, ...);
324
+
325
+ enum lm_ggml_status {
326
+ LM_GGML_STATUS_ALLOC_FAILED = -2,
327
+ LM_GGML_STATUS_FAILED = -1,
328
+ LM_GGML_STATUS_SUCCESS = 0,
329
+ LM_GGML_STATUS_ABORTED = 1,
330
+ };
331
+
332
+ // get lm_ggml_status name string
333
+ LM_GGML_API const char * lm_ggml_status_to_string(enum lm_ggml_status status);
334
+
335
+ // ieee 754-2008 half-precision float16
336
+ // todo: make this not an integral type
337
+ typedef uint16_t lm_ggml_fp16_t;
338
+ LM_GGML_API float lm_ggml_fp16_to_fp32(lm_ggml_fp16_t);
339
+ LM_GGML_API lm_ggml_fp16_t lm_ggml_fp32_to_fp16(float);
340
+ LM_GGML_API void lm_ggml_fp16_to_fp32_row(const lm_ggml_fp16_t *, float *, int64_t);
341
+ LM_GGML_API void lm_ggml_fp32_to_fp16_row(const float *, lm_ggml_fp16_t *, int64_t);
342
+
343
+ // google brain half-precision bfloat16
344
+ typedef struct { uint16_t bits; } lm_ggml_bf16_t;
345
+ LM_GGML_API lm_ggml_bf16_t lm_ggml_fp32_to_bf16(float);
346
+ LM_GGML_API float lm_ggml_bf16_to_fp32(lm_ggml_bf16_t); // consider just doing << 16
347
+ LM_GGML_API void lm_ggml_bf16_to_fp32_row(const lm_ggml_bf16_t *, float *, int64_t);
348
+ LM_GGML_API void lm_ggml_fp32_to_bf16_row_ref(const float *, lm_ggml_bf16_t *, int64_t);
349
+ LM_GGML_API void lm_ggml_fp32_to_bf16_row(const float *, lm_ggml_bf16_t *, int64_t);
350
+
351
+ struct lm_ggml_object;
352
+ struct lm_ggml_context;
353
+ struct lm_ggml_cgraph;
354
+
355
+ // NOTE: always add types at the end of the enum to keep backward compatibility
356
+ enum lm_ggml_type {
357
+ LM_GGML_TYPE_F32 = 0,
358
+ LM_GGML_TYPE_F16 = 1,
359
+ LM_GGML_TYPE_Q4_0 = 2,
360
+ LM_GGML_TYPE_Q4_1 = 3,
361
+ // LM_GGML_TYPE_Q4_2 = 4, support has been removed
362
+ // LM_GGML_TYPE_Q4_3 = 5, support has been removed
363
+ LM_GGML_TYPE_Q5_0 = 6,
364
+ LM_GGML_TYPE_Q5_1 = 7,
365
+ LM_GGML_TYPE_Q8_0 = 8,
366
+ LM_GGML_TYPE_Q8_1 = 9,
367
+ LM_GGML_TYPE_Q2_K = 10,
368
+ LM_GGML_TYPE_Q3_K = 11,
369
+ LM_GGML_TYPE_Q4_K = 12,
370
+ LM_GGML_TYPE_Q5_K = 13,
371
+ LM_GGML_TYPE_Q6_K = 14,
372
+ LM_GGML_TYPE_Q8_K = 15,
373
+ LM_GGML_TYPE_IQ2_XXS = 16,
374
+ LM_GGML_TYPE_IQ2_XS = 17,
375
+ LM_GGML_TYPE_IQ3_XXS = 18,
376
+ LM_GGML_TYPE_IQ1_S = 19,
377
+ LM_GGML_TYPE_IQ4_NL = 20,
378
+ LM_GGML_TYPE_IQ3_S = 21,
379
+ LM_GGML_TYPE_IQ2_S = 22,
380
+ LM_GGML_TYPE_IQ4_XS = 23,
381
+ LM_GGML_TYPE_I8 = 24,
382
+ LM_GGML_TYPE_I16 = 25,
383
+ LM_GGML_TYPE_I32 = 26,
384
+ LM_GGML_TYPE_I64 = 27,
385
+ LM_GGML_TYPE_F64 = 28,
386
+ LM_GGML_TYPE_IQ1_M = 29,
387
+ LM_GGML_TYPE_BF16 = 30,
388
+ LM_GGML_TYPE_Q4_0_4_4 = 31,
389
+ LM_GGML_TYPE_Q4_0_4_8 = 32,
390
+ LM_GGML_TYPE_Q4_0_8_8 = 33,
391
+ LM_GGML_TYPE_TQ1_0 = 34,
392
+ LM_GGML_TYPE_TQ2_0 = 35,
393
+ LM_GGML_TYPE_IQ4_NL_4_4 = 36,
394
+ // LM_GGML_TYPE_IQ4_NL_4_8 = 37,
395
+ // LM_GGML_TYPE_IQ4_NL_8_8 = 38,
396
+ LM_GGML_TYPE_COUNT,
397
+ };
398
+
399
+ // precision
400
+ enum lm_ggml_prec {
401
+ LM_GGML_PREC_DEFAULT,
402
+ LM_GGML_PREC_F32,
403
+ };
404
+
405
+ enum lm_ggml_backend_type {
406
+ LM_GGML_BACKEND_TYPE_CPU = 0,
407
+ LM_GGML_BACKEND_TYPE_GPU = 10,
408
+ LM_GGML_BACKEND_TYPE_GPU_SPLIT = 20,
409
+ };
410
+
411
+ // model file types
412
+ enum lm_ggml_ftype {
413
+ LM_GGML_FTYPE_UNKNOWN = -1,
414
+ LM_GGML_FTYPE_ALL_F32 = 0,
415
+ LM_GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
416
+ LM_GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
417
+ LM_GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
418
+ LM_GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
419
+ LM_GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
420
+ LM_GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
421
+ LM_GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
422
+ LM_GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
423
+ LM_GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
424
+ LM_GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
425
+ LM_GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
426
+ LM_GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
427
+ LM_GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors
428
+ LM_GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors
429
+ LM_GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors
430
+ LM_GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors
431
+ LM_GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors
432
+ LM_GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors
433
+ LM_GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors
434
+ LM_GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors
435
+ LM_GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors
436
+ LM_GGML_FTYPE_MOSTLY_BF16 = 24, // except 1d tensors
437
+ LM_GGML_FTYPE_MOSTLY_Q4_0_4_4 = 25, // except 1d tensors
438
+ LM_GGML_FTYPE_MOSTLY_Q4_0_4_8 = 26, // except 1d tensors
439
+ LM_GGML_FTYPE_MOSTLY_Q4_0_8_8 = 27, // except 1d tensors
440
+ };
441
+
442
+ // available tensor operations:
443
+ enum lm_ggml_op {
444
+ LM_GGML_OP_NONE = 0,
445
+
446
+ LM_GGML_OP_DUP,
447
+ LM_GGML_OP_ADD,
448
+ LM_GGML_OP_ADD1,
449
+ LM_GGML_OP_ACC,
450
+ LM_GGML_OP_SUB,
451
+ LM_GGML_OP_MUL,
452
+ LM_GGML_OP_DIV,
453
+ LM_GGML_OP_SQR,
454
+ LM_GGML_OP_SQRT,
455
+ LM_GGML_OP_LOG,
456
+ LM_GGML_OP_SIN,
457
+ LM_GGML_OP_COS,
458
+ LM_GGML_OP_SUM,
459
+ LM_GGML_OP_SUM_ROWS,
460
+ LM_GGML_OP_MEAN,
461
+ LM_GGML_OP_ARGMAX,
462
+ LM_GGML_OP_COUNT_EQUAL,
463
+ LM_GGML_OP_REPEAT,
464
+ LM_GGML_OP_REPEAT_BACK,
465
+ LM_GGML_OP_CONCAT,
466
+ LM_GGML_OP_SILU_BACK,
467
+ LM_GGML_OP_NORM, // normalize
468
+ LM_GGML_OP_RMS_NORM,
469
+ LM_GGML_OP_RMS_NORM_BACK,
470
+ LM_GGML_OP_GROUP_NORM,
471
+
472
+ LM_GGML_OP_MUL_MAT,
473
+ LM_GGML_OP_MUL_MAT_ID,
474
+ LM_GGML_OP_OUT_PROD,
475
+
476
+ LM_GGML_OP_SCALE,
477
+ LM_GGML_OP_SET,
478
+ LM_GGML_OP_CPY,
479
+ LM_GGML_OP_CONT,
480
+ LM_GGML_OP_RESHAPE,
481
+ LM_GGML_OP_VIEW,
482
+ LM_GGML_OP_PERMUTE,
483
+ LM_GGML_OP_TRANSPOSE,
484
+ LM_GGML_OP_GET_ROWS,
485
+ LM_GGML_OP_GET_ROWS_BACK,
486
+ LM_GGML_OP_DIAG,
487
+ LM_GGML_OP_DIAG_MASK_INF,
488
+ LM_GGML_OP_DIAG_MASK_ZERO,
489
+ LM_GGML_OP_SOFT_MAX,
490
+ LM_GGML_OP_SOFT_MAX_BACK,
491
+ LM_GGML_OP_ROPE,
492
+ LM_GGML_OP_ROPE_BACK,
493
+ LM_GGML_OP_CLAMP,
494
+ LM_GGML_OP_CONV_TRANSPOSE_1D,
495
+ LM_GGML_OP_IM2COL,
496
+ LM_GGML_OP_IM2COL_BACK,
497
+ LM_GGML_OP_CONV_TRANSPOSE_2D,
498
+ LM_GGML_OP_POOL_1D,
499
+ LM_GGML_OP_POOL_2D,
500
+ LM_GGML_OP_POOL_2D_BACK,
501
+ LM_GGML_OP_UPSCALE, // nearest interpolate
502
+ LM_GGML_OP_PAD,
503
+ LM_GGML_OP_ARANGE,
504
+ LM_GGML_OP_TIMESTEP_EMBEDDING,
505
+ LM_GGML_OP_ARGSORT,
506
+ LM_GGML_OP_LEAKY_RELU,
507
+
508
+ LM_GGML_OP_FLASH_ATTN_EXT,
509
+ LM_GGML_OP_FLASH_ATTN_BACK,
510
+ LM_GGML_OP_SSM_CONV,
511
+ LM_GGML_OP_SSM_SCAN,
512
+ LM_GGML_OP_WIN_PART,
513
+ LM_GGML_OP_WIN_UNPART,
514
+ LM_GGML_OP_GET_REL_POS,
515
+ LM_GGML_OP_ADD_REL_POS,
516
+ LM_GGML_OP_RWKV_WKV6,
517
+
518
+ LM_GGML_OP_UNARY,
519
+
520
+ LM_GGML_OP_MAP_UNARY,
521
+ LM_GGML_OP_MAP_BINARY,
522
+
523
+ LM_GGML_OP_MAP_CUSTOM1_F32,
524
+ LM_GGML_OP_MAP_CUSTOM2_F32,
525
+ LM_GGML_OP_MAP_CUSTOM3_F32,
526
+
527
+ LM_GGML_OP_MAP_CUSTOM1,
528
+ LM_GGML_OP_MAP_CUSTOM2,
529
+ LM_GGML_OP_MAP_CUSTOM3,
530
+
531
+ LM_GGML_OP_CROSS_ENTROPY_LOSS,
532
+ LM_GGML_OP_CROSS_ENTROPY_LOSS_BACK,
533
+ LM_GGML_OP_OPT_STEP_ADAMW,
534
+
535
+ LM_GGML_OP_COUNT,
536
+ };
537
+
538
+ enum lm_ggml_unary_op {
539
+ LM_GGML_UNARY_OP_ABS,
540
+ LM_GGML_UNARY_OP_SGN,
541
+ LM_GGML_UNARY_OP_NEG,
542
+ LM_GGML_UNARY_OP_STEP,
543
+ LM_GGML_UNARY_OP_TANH,
544
+ LM_GGML_UNARY_OP_ELU,
545
+ LM_GGML_UNARY_OP_RELU,
546
+ LM_GGML_UNARY_OP_SIGMOID,
547
+ LM_GGML_UNARY_OP_GELU,
548
+ LM_GGML_UNARY_OP_GELU_QUICK,
549
+ LM_GGML_UNARY_OP_SILU,
550
+ LM_GGML_UNARY_OP_HARDSWISH,
551
+ LM_GGML_UNARY_OP_HARDSIGMOID,
552
+ LM_GGML_UNARY_OP_EXP,
553
+
554
+ LM_GGML_UNARY_OP_COUNT,
555
+ };
556
+
557
+ enum lm_ggml_object_type {
558
+ LM_GGML_OBJECT_TYPE_TENSOR,
559
+ LM_GGML_OBJECT_TYPE_GRAPH,
560
+ LM_GGML_OBJECT_TYPE_WORK_BUFFER
561
+ };
562
+
563
+ enum lm_ggml_log_level {
564
+ LM_GGML_LOG_LEVEL_NONE = 0,
565
+ LM_GGML_LOG_LEVEL_DEBUG = 1,
566
+ LM_GGML_LOG_LEVEL_INFO = 2,
567
+ LM_GGML_LOG_LEVEL_WARN = 3,
568
+ LM_GGML_LOG_LEVEL_ERROR = 4,
569
+ LM_GGML_LOG_LEVEL_CONT = 5, // continue previous log
570
+ };
571
+
572
+ // this tensor...
573
+ enum lm_ggml_tensor_flag {
574
+ LM_GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
575
+ LM_GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
576
+ LM_GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
577
+ LM_GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up)
578
+ };
579
+
580
+ struct lm_ggml_init_params {
581
+ // memory pool
582
+ size_t mem_size; // bytes
583
+ void * mem_buffer; // if NULL, memory will be allocated internally
584
+ bool no_alloc; // don't allocate memory for the tensor data
585
+ };
586
+
587
+ // n-dimensional tensor
588
+ struct lm_ggml_tensor {
589
+ enum lm_ggml_type type;
590
+
591
+ LM_GGML_DEPRECATED(enum lm_ggml_backend_type backend, "use the buffer type to find the storage location of the tensor");
592
+
593
+ struct lm_ggml_backend_buffer * buffer;
594
+
595
+ int64_t ne[LM_GGML_MAX_DIMS]; // number of elements
596
+ size_t nb[LM_GGML_MAX_DIMS]; // stride in bytes:
597
+ // nb[0] = lm_ggml_type_size(type)
598
+ // nb[1] = nb[0] * (ne[0] / lm_ggml_blck_size(type)) + padding
599
+ // nb[i] = nb[i-1] * ne[i-1]
600
+
601
+ // compute data
602
+ enum lm_ggml_op op;
603
+
604
+ // op params - allocated as int32_t for alignment
605
+ int32_t op_params[LM_GGML_MAX_OP_PARAMS / sizeof(int32_t)];
606
+
607
+ int32_t flags;
608
+
609
+ struct lm_ggml_tensor * src[LM_GGML_MAX_SRC];
610
+
611
+ // source tensor and offset for views
612
+ struct lm_ggml_tensor * view_src;
613
+ size_t view_offs;
614
+
615
+ void * data;
616
+
617
+ char name[LM_GGML_MAX_NAME];
618
+
619
+ void * extra; // extra things e.g. for ggml-cuda.cu
620
+
621
+ char padding[8];
622
+ };
623
+
624
+ static const size_t LM_GGML_TENSOR_SIZE = sizeof(struct lm_ggml_tensor);
625
+
626
+ // Abort callback
627
+ // If not NULL, called before ggml computation
628
+ // If it returns true, the computation is aborted
629
+ typedef bool (*lm_ggml_abort_callback)(void * data);
630
+
631
+
632
+ //
633
+ // GUID
634
+ //
635
+
636
+ // GUID types
637
+ typedef uint8_t lm_ggml_guid[16];
638
+ typedef lm_ggml_guid * lm_ggml_guid_t;
639
+
640
+ LM_GGML_API bool lm_ggml_guid_matches(lm_ggml_guid_t guid_a, lm_ggml_guid_t guid_b);
641
+
642
+ // misc
643
+
644
+ LM_GGML_API void lm_ggml_time_init(void); // call this once at the beginning of the program
645
+ LM_GGML_API int64_t lm_ggml_time_ms(void);
646
+ LM_GGML_API int64_t lm_ggml_time_us(void);
647
+ LM_GGML_API int64_t lm_ggml_cycles(void);
648
+ LM_GGML_API int64_t lm_ggml_cycles_per_ms(void);
649
+
650
+ // accepts a UTF-8 path, even on Windows
651
+ LM_GGML_API FILE * lm_ggml_fopen(const char * fname, const char * mode);
652
+
653
+ LM_GGML_API void lm_ggml_print_object (const struct lm_ggml_object * obj);
654
+ LM_GGML_API void lm_ggml_print_objects(const struct lm_ggml_context * ctx);
655
+
656
+ LM_GGML_API int64_t lm_ggml_nelements (const struct lm_ggml_tensor * tensor);
657
+ LM_GGML_API int64_t lm_ggml_nrows (const struct lm_ggml_tensor * tensor);
658
+ LM_GGML_API size_t lm_ggml_nbytes (const struct lm_ggml_tensor * tensor);
659
+ LM_GGML_API size_t lm_ggml_nbytes_pad(const struct lm_ggml_tensor * tensor); // same as lm_ggml_nbytes() but padded to LM_GGML_MEM_ALIGN
660
+
661
+ LM_GGML_API int64_t lm_ggml_blck_size(enum lm_ggml_type type);
662
+ LM_GGML_API size_t lm_ggml_type_size(enum lm_ggml_type type); // size in bytes for all elements in a block
663
+ LM_GGML_API size_t lm_ggml_row_size (enum lm_ggml_type type, int64_t ne); // size in bytes for all elements in a row
664
+
665
+ LM_GGML_DEPRECATED(
666
+ LM_GGML_API double lm_ggml_type_sizef(enum lm_ggml_type type), // lm_ggml_type_size()/lm_ggml_blck_size() as float
667
+ "use lm_ggml_row_size() instead");
668
+
669
+ LM_GGML_API const char * lm_ggml_type_name(enum lm_ggml_type type);
670
+ LM_GGML_API const char * lm_ggml_op_name (enum lm_ggml_op op);
671
+ LM_GGML_API const char * lm_ggml_op_symbol(enum lm_ggml_op op);
672
+
673
+ LM_GGML_API const char * lm_ggml_unary_op_name(enum lm_ggml_unary_op op);
674
+ LM_GGML_API const char * lm_ggml_op_desc(const struct lm_ggml_tensor * t); // unary or op name
675
+
676
+ LM_GGML_API size_t lm_ggml_element_size(const struct lm_ggml_tensor * tensor);
677
+
678
+ LM_GGML_API bool lm_ggml_is_quantized(enum lm_ggml_type type);
679
+
680
+ // TODO: temporary until model loading of ggml examples is refactored
681
+ LM_GGML_API enum lm_ggml_type lm_ggml_ftype_to_lm_ggml_type(enum lm_ggml_ftype ftype);
682
+
683
+ LM_GGML_API bool lm_ggml_is_transposed(const struct lm_ggml_tensor * tensor);
684
+ LM_GGML_API bool lm_ggml_is_permuted (const struct lm_ggml_tensor * tensor);
685
+ LM_GGML_API bool lm_ggml_is_empty (const struct lm_ggml_tensor * tensor);
686
+ LM_GGML_API bool lm_ggml_is_scalar (const struct lm_ggml_tensor * tensor);
687
+ LM_GGML_API bool lm_ggml_is_vector (const struct lm_ggml_tensor * tensor);
688
+ LM_GGML_API bool lm_ggml_is_matrix (const struct lm_ggml_tensor * tensor);
689
+ LM_GGML_API bool lm_ggml_is_3d (const struct lm_ggml_tensor * tensor);
690
+ LM_GGML_API int lm_ggml_n_dims (const struct lm_ggml_tensor * tensor); // returns 1 for scalars
691
+
692
+ LM_GGML_API bool lm_ggml_is_contiguous (const struct lm_ggml_tensor * tensor);
693
+ LM_GGML_API bool lm_ggml_is_contiguous_0(const struct lm_ggml_tensor * tensor); // same as lm_ggml_is_contiguous()
694
+ LM_GGML_API bool lm_ggml_is_contiguous_1(const struct lm_ggml_tensor * tensor); // contiguous for dims >= 1
695
+ LM_GGML_API bool lm_ggml_is_contiguous_2(const struct lm_ggml_tensor * tensor); // contiguous for dims >= 2
696
+
697
+ LM_GGML_API bool lm_ggml_are_same_shape (const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
698
+ LM_GGML_API bool lm_ggml_are_same_stride(const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
699
+
700
+ LM_GGML_API bool lm_ggml_can_repeat(const struct lm_ggml_tensor * t0, const struct lm_ggml_tensor * t1);
701
+
702
+ // use this to compute the memory overhead of a tensor
703
+ LM_GGML_API size_t lm_ggml_tensor_overhead(void);
704
+
705
+ LM_GGML_API bool lm_ggml_validate_row_data(enum lm_ggml_type type, const void * data, size_t nbytes);
706
+
707
+ // main
708
+
709
+ LM_GGML_API struct lm_ggml_context * lm_ggml_init (struct lm_ggml_init_params params);
710
+ LM_GGML_API void lm_ggml_reset(struct lm_ggml_context * ctx);
711
+ LM_GGML_API void lm_ggml_free (struct lm_ggml_context * ctx);
712
+
713
+ LM_GGML_API size_t lm_ggml_used_mem(const struct lm_ggml_context * ctx);
714
+
715
+ LM_GGML_API bool lm_ggml_get_no_alloc(struct lm_ggml_context * ctx);
716
+ LM_GGML_API void lm_ggml_set_no_alloc(struct lm_ggml_context * ctx, bool no_alloc);
717
+
718
+ LM_GGML_API void * lm_ggml_get_mem_buffer (const struct lm_ggml_context * ctx);
719
+ LM_GGML_API size_t lm_ggml_get_mem_size (const struct lm_ggml_context * ctx);
720
+ LM_GGML_API size_t lm_ggml_get_max_tensor_size(const struct lm_ggml_context * ctx);
721
+
722
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor(
723
+ struct lm_ggml_context * ctx,
724
+ enum lm_ggml_type type,
725
+ int n_dims,
726
+ const int64_t *ne);
727
+
728
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_1d(
729
+ struct lm_ggml_context * ctx,
730
+ enum lm_ggml_type type,
731
+ int64_t ne0);
732
+
733
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_2d(
734
+ struct lm_ggml_context * ctx,
735
+ enum lm_ggml_type type,
736
+ int64_t ne0,
737
+ int64_t ne1);
738
+
739
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_3d(
740
+ struct lm_ggml_context * ctx,
741
+ enum lm_ggml_type type,
742
+ int64_t ne0,
743
+ int64_t ne1,
744
+ int64_t ne2);
745
+
746
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_new_tensor_4d(
747
+ struct lm_ggml_context * ctx,
748
+ enum lm_ggml_type type,
749
+ int64_t ne0,
750
+ int64_t ne1,
751
+ int64_t ne2,
752
+ int64_t ne3);
753
+
754
+ LM_GGML_API void * lm_ggml_new_buffer(struct lm_ggml_context * ctx, size_t nbytes);
755
+
756
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup_tensor (struct lm_ggml_context * ctx, const struct lm_ggml_tensor * src);
757
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_tensor(struct lm_ggml_context * ctx, struct lm_ggml_tensor * src);
758
+
759
+ // Context tensor enumeration and lookup
760
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_first_tensor(const struct lm_ggml_context * ctx);
761
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_next_tensor (const struct lm_ggml_context * ctx, struct lm_ggml_tensor * tensor);
762
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_tensor(struct lm_ggml_context * ctx, const char * name);
763
+
764
+ // Converts a flat index into coordinates
765
+ LM_GGML_API void lm_ggml_unravel_index(const struct lm_ggml_tensor * tensor, int64_t i, int64_t * i0, int64_t * i1, int64_t * i2, int64_t * i3);
766
+
767
+ LM_GGML_API enum lm_ggml_unary_op lm_ggml_get_unary_op(const struct lm_ggml_tensor * tensor);
768
+
769
+ LM_GGML_API void * lm_ggml_get_data (const struct lm_ggml_tensor * tensor);
770
+ LM_GGML_API float * lm_ggml_get_data_f32(const struct lm_ggml_tensor * tensor);
771
+
772
+ LM_GGML_API const char * lm_ggml_get_name (const struct lm_ggml_tensor * tensor);
773
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_name ( struct lm_ggml_tensor * tensor, const char * name);
774
+ LM_GGML_ATTRIBUTE_FORMAT(2, 3)
775
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_format_name( struct lm_ggml_tensor * tensor, const char * fmt, ...);
776
+
777
+ // Tensor flags
778
+ LM_GGML_API void lm_ggml_set_input(struct lm_ggml_tensor * tensor);
779
+ LM_GGML_API void lm_ggml_set_output(struct lm_ggml_tensor * tensor);
780
+ LM_GGML_API void lm_ggml_set_param(struct lm_ggml_context * ctx, struct lm_ggml_tensor * tensor);
781
+ LM_GGML_API void lm_ggml_set_loss(struct lm_ggml_tensor * tensor);
782
+
783
+ //
784
+ // operations on tensors with backpropagation
785
+ //
786
+
787
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup(
788
+ struct lm_ggml_context * ctx,
789
+ struct lm_ggml_tensor * a);
790
+
791
+ // in-place, returns view(a)
792
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_dup_inplace(
793
+ struct lm_ggml_context * ctx,
794
+ struct lm_ggml_tensor * a);
795
+
796
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add(
797
+ struct lm_ggml_context * ctx,
798
+ struct lm_ggml_tensor * a,
799
+ struct lm_ggml_tensor * b);
800
+
801
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_inplace(
802
+ struct lm_ggml_context * ctx,
803
+ struct lm_ggml_tensor * a,
804
+ struct lm_ggml_tensor * b);
805
+
806
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_cast(
807
+ struct lm_ggml_context * ctx,
808
+ struct lm_ggml_tensor * a,
809
+ struct lm_ggml_tensor * b,
810
+ enum lm_ggml_type type);
811
+
812
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add1(
813
+ struct lm_ggml_context * ctx,
814
+ struct lm_ggml_tensor * a,
815
+ struct lm_ggml_tensor * b);
816
+
817
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add1_inplace(
818
+ struct lm_ggml_context * ctx,
819
+ struct lm_ggml_tensor * a,
820
+ struct lm_ggml_tensor * b);
821
+
822
+ // dst = a
823
+ // view(dst, nb1, nb2, nb3, offset) += b
824
+ // return dst
825
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_acc(
826
+ struct lm_ggml_context * ctx,
827
+ struct lm_ggml_tensor * a,
828
+ struct lm_ggml_tensor * b,
829
+ size_t nb1,
830
+ size_t nb2,
831
+ size_t nb3,
832
+ size_t offset);
833
+
834
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_acc_inplace(
835
+ struct lm_ggml_context * ctx,
836
+ struct lm_ggml_tensor * a,
837
+ struct lm_ggml_tensor * b,
838
+ size_t nb1,
839
+ size_t nb2,
840
+ size_t nb3,
841
+ size_t offset);
842
+
843
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sub(
844
+ struct lm_ggml_context * ctx,
845
+ struct lm_ggml_tensor * a,
846
+ struct lm_ggml_tensor * b);
847
+
848
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sub_inplace(
849
+ struct lm_ggml_context * ctx,
850
+ struct lm_ggml_tensor * a,
851
+ struct lm_ggml_tensor * b);
852
+
853
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul(
854
+ struct lm_ggml_context * ctx,
855
+ struct lm_ggml_tensor * a,
856
+ struct lm_ggml_tensor * b);
857
+
858
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_inplace(
859
+ struct lm_ggml_context * ctx,
860
+ struct lm_ggml_tensor * a,
861
+ struct lm_ggml_tensor * b);
862
+
863
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_div(
864
+ struct lm_ggml_context * ctx,
865
+ struct lm_ggml_tensor * a,
866
+ struct lm_ggml_tensor * b);
867
+
868
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_div_inplace(
869
+ struct lm_ggml_context * ctx,
870
+ struct lm_ggml_tensor * a,
871
+ struct lm_ggml_tensor * b);
872
+
873
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqr(
874
+ struct lm_ggml_context * ctx,
875
+ struct lm_ggml_tensor * a);
876
+
877
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqr_inplace(
878
+ struct lm_ggml_context * ctx,
879
+ struct lm_ggml_tensor * a);
880
+
881
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqrt(
882
+ struct lm_ggml_context * ctx,
883
+ struct lm_ggml_tensor * a);
884
+
885
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sqrt_inplace(
886
+ struct lm_ggml_context * ctx,
887
+ struct lm_ggml_tensor * a);
888
+
889
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_log(
890
+ struct lm_ggml_context * ctx,
891
+ struct lm_ggml_tensor * a);
892
+
893
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_log_inplace(
894
+ struct lm_ggml_context * ctx,
895
+ struct lm_ggml_tensor * a);
896
+
897
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sin(
898
+ struct lm_ggml_context * ctx,
899
+ struct lm_ggml_tensor * a);
900
+
901
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sin_inplace(
902
+ struct lm_ggml_context * ctx,
903
+ struct lm_ggml_tensor * a);
904
+
905
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cos(
906
+ struct lm_ggml_context * ctx,
907
+ struct lm_ggml_tensor * a);
908
+
909
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cos_inplace(
910
+ struct lm_ggml_context * ctx,
911
+ struct lm_ggml_tensor * a);
912
+
913
+ // return scalar
914
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sum(
915
+ struct lm_ggml_context * ctx,
916
+ struct lm_ggml_tensor * a);
917
+
918
+ // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
919
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sum_rows(
920
+ struct lm_ggml_context * ctx,
921
+ struct lm_ggml_tensor * a);
922
+
923
+ // mean along rows
924
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mean(
925
+ struct lm_ggml_context * ctx,
926
+ struct lm_ggml_tensor * a);
927
+
928
+ // argmax along rows
929
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_argmax(
930
+ struct lm_ggml_context * ctx,
931
+ struct lm_ggml_tensor * a);
932
+
933
+ // count number of equal elements in a and b
934
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_count_equal(
935
+ struct lm_ggml_context * ctx,
936
+ struct lm_ggml_tensor * a,
937
+ struct lm_ggml_tensor * b);
938
+
939
+ // if a is the same shape as b, and a is not parameter, return a
940
+ // otherwise, return a new tensor: repeat(a) to fit in b
941
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat(
942
+ struct lm_ggml_context * ctx,
943
+ struct lm_ggml_tensor * a,
944
+ struct lm_ggml_tensor * b);
945
+
946
+ // sums repetitions in a into shape of b
947
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_repeat_back(
948
+ struct lm_ggml_context * ctx,
949
+ struct lm_ggml_tensor * a,
950
+ struct lm_ggml_tensor * b);
951
+
952
+ // concat a and b along dim
953
+ // used in stable-diffusion
954
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_concat(
955
+ struct lm_ggml_context * ctx,
956
+ struct lm_ggml_tensor * a,
957
+ struct lm_ggml_tensor * b,
958
+ int dim);
959
+
960
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_abs(
961
+ struct lm_ggml_context * ctx,
962
+ struct lm_ggml_tensor * a);
963
+
964
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_abs_inplace(
965
+ struct lm_ggml_context * ctx,
966
+ struct lm_ggml_tensor * a);
967
+
968
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sgn(
969
+ struct lm_ggml_context * ctx,
970
+ struct lm_ggml_tensor * a);
971
+
972
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sgn_inplace(
973
+ struct lm_ggml_context * ctx,
974
+ struct lm_ggml_tensor * a);
975
+
976
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_neg(
977
+ struct lm_ggml_context * ctx,
978
+ struct lm_ggml_tensor * a);
979
+
980
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_neg_inplace(
981
+ struct lm_ggml_context * ctx,
982
+ struct lm_ggml_tensor * a);
983
+
984
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_step(
985
+ struct lm_ggml_context * ctx,
986
+ struct lm_ggml_tensor * a);
987
+
988
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_step_inplace(
989
+ struct lm_ggml_context * ctx,
990
+ struct lm_ggml_tensor * a);
991
+
992
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_tanh(
993
+ struct lm_ggml_context * ctx,
994
+ struct lm_ggml_tensor * a);
995
+
996
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_tanh_inplace(
997
+ struct lm_ggml_context * ctx,
998
+ struct lm_ggml_tensor * a);
999
+
1000
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_elu(
1001
+ struct lm_ggml_context * ctx,
1002
+ struct lm_ggml_tensor * a);
1003
+
1004
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_elu_inplace(
1005
+ struct lm_ggml_context * ctx,
1006
+ struct lm_ggml_tensor * a);
1007
+
1008
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_relu(
1009
+ struct lm_ggml_context * ctx,
1010
+ struct lm_ggml_tensor * a);
1011
+
1012
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_leaky_relu(
1013
+ struct lm_ggml_context * ctx,
1014
+ struct lm_ggml_tensor * a, float negative_slope, bool inplace);
1015
+
1016
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_relu_inplace(
1017
+ struct lm_ggml_context * ctx,
1018
+ struct lm_ggml_tensor * a);
1019
+
1020
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sigmoid(
1021
+ struct lm_ggml_context * ctx,
1022
+ struct lm_ggml_tensor * a);
1023
+
1024
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_sigmoid_inplace(
1025
+ struct lm_ggml_context * ctx,
1026
+ struct lm_ggml_tensor * a);
1027
+
1028
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu(
1029
+ struct lm_ggml_context * ctx,
1030
+ struct lm_ggml_tensor * a);
1031
+
1032
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_inplace(
1033
+ struct lm_ggml_context * ctx,
1034
+ struct lm_ggml_tensor * a);
1035
+
1036
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_quick(
1037
+ struct lm_ggml_context * ctx,
1038
+ struct lm_ggml_tensor * a);
1039
+
1040
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_gelu_quick_inplace(
1041
+ struct lm_ggml_context * ctx,
1042
+ struct lm_ggml_tensor * a);
1043
+
1044
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu(
1045
+ struct lm_ggml_context * ctx,
1046
+ struct lm_ggml_tensor * a);
1047
+
1048
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu_inplace(
1049
+ struct lm_ggml_context * ctx,
1050
+ struct lm_ggml_tensor * a);
1051
+
1052
+ // a - x
1053
+ // b - dy
1054
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_silu_back(
1055
+ struct lm_ggml_context * ctx,
1056
+ struct lm_ggml_tensor * a,
1057
+ struct lm_ggml_tensor * b);
1058
+
1059
+ // hardswish(x) = x * relu6(x + 3) / 6
1060
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_hardswish(
1061
+ struct lm_ggml_context * ctx,
1062
+ struct lm_ggml_tensor * a);
1063
+
1064
+ // hardsigmoid(x) = relu6(x + 3) / 6
1065
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_hardsigmoid(
1066
+ struct lm_ggml_context * ctx,
1067
+ struct lm_ggml_tensor * a);
1068
+
1069
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_exp(
1070
+ struct lm_ggml_context * ctx,
1071
+ struct lm_ggml_tensor * a);
1072
+
1073
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_exp_inplace(
1074
+ struct lm_ggml_context * ctx,
1075
+ struct lm_ggml_tensor * a);
1076
+
1077
+ // normalize along rows
1078
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_norm(
1079
+ struct lm_ggml_context * ctx,
1080
+ struct lm_ggml_tensor * a,
1081
+ float eps);
1082
+
1083
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_norm_inplace(
1084
+ struct lm_ggml_context * ctx,
1085
+ struct lm_ggml_tensor * a,
1086
+ float eps);
1087
+
1088
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm(
1089
+ struct lm_ggml_context * ctx,
1090
+ struct lm_ggml_tensor * a,
1091
+ float eps);
1092
+
1093
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm_inplace(
1094
+ struct lm_ggml_context * ctx,
1095
+ struct lm_ggml_tensor * a,
1096
+ float eps);
1097
+
1098
+ // group normalize along ne0*ne1*n_groups
1099
+ // used in stable-diffusion
1100
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_group_norm(
1101
+ struct lm_ggml_context * ctx,
1102
+ struct lm_ggml_tensor * a,
1103
+ int n_groups,
1104
+ float eps);
1105
+
1106
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_group_norm_inplace(
1107
+ struct lm_ggml_context * ctx,
1108
+ struct lm_ggml_tensor * a,
1109
+ int n_groups,
1110
+ float eps);
1111
+
1112
+ // a - x
1113
+ // b - dy
1114
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rms_norm_back(
1115
+ struct lm_ggml_context * ctx,
1116
+ struct lm_ggml_tensor * a,
1117
+ struct lm_ggml_tensor * b,
1118
+ float eps);
1119
+
1120
+ // A: k columns, n rows => [ne03, ne02, n, k]
1121
+ // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
1122
+ // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
1123
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_mat(
1124
+ struct lm_ggml_context * ctx,
1125
+ struct lm_ggml_tensor * a,
1126
+ struct lm_ggml_tensor * b);
1127
+
1128
+ // change the precision of a matrix multiplication
1129
+ // set to LM_GGML_PREC_F32 for higher precision (useful for phi-2)
1130
+ LM_GGML_API void lm_ggml_mul_mat_set_prec(
1131
+ struct lm_ggml_tensor * a,
1132
+ enum lm_ggml_prec prec);
1133
+
1134
+ // indirect matrix multiplication
1135
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_mul_mat_id(
1136
+ struct lm_ggml_context * ctx,
1137
+ struct lm_ggml_tensor * as,
1138
+ struct lm_ggml_tensor * b,
1139
+ struct lm_ggml_tensor * ids);
1140
+
1141
+ // A: m columns, n rows,
1142
+ // B: p columns, n rows,
1143
+ // result is m columns, p rows
1144
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_out_prod(
1145
+ struct lm_ggml_context * ctx,
1146
+ struct lm_ggml_tensor * a,
1147
+ struct lm_ggml_tensor * b);
1148
+
1149
+ //
1150
+ // operations on tensors without backpropagation
1151
+ //
1152
+
1153
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale(
1154
+ struct lm_ggml_context * ctx,
1155
+ struct lm_ggml_tensor * a,
1156
+ float s);
1157
+
1158
+ // in-place, returns view(a)
1159
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_scale_inplace(
1160
+ struct lm_ggml_context * ctx,
1161
+ struct lm_ggml_tensor * a,
1162
+ float s);
1163
+
1164
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1165
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set(
1166
+ struct lm_ggml_context * ctx,
1167
+ struct lm_ggml_tensor * a,
1168
+ struct lm_ggml_tensor * b,
1169
+ size_t nb1,
1170
+ size_t nb2,
1171
+ size_t nb3,
1172
+ size_t offset); // in bytes
1173
+
1174
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1175
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_inplace(
1176
+ struct lm_ggml_context * ctx,
1177
+ struct lm_ggml_tensor * a,
1178
+ struct lm_ggml_tensor * b,
1179
+ size_t nb1,
1180
+ size_t nb2,
1181
+ size_t nb3,
1182
+ size_t offset); // in bytes
1183
+
1184
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_1d(
1185
+ struct lm_ggml_context * ctx,
1186
+ struct lm_ggml_tensor * a,
1187
+ struct lm_ggml_tensor * b,
1188
+ size_t offset); // in bytes
1189
+
1190
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_1d_inplace(
1191
+ struct lm_ggml_context * ctx,
1192
+ struct lm_ggml_tensor * a,
1193
+ struct lm_ggml_tensor * b,
1194
+ size_t offset); // in bytes
1195
+
1196
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1197
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_2d(
1198
+ struct lm_ggml_context * ctx,
1199
+ struct lm_ggml_tensor * a,
1200
+ struct lm_ggml_tensor * b,
1201
+ size_t nb1,
1202
+ size_t offset); // in bytes
1203
+
1204
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1205
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_2d_inplace(
1206
+ struct lm_ggml_context * ctx,
1207
+ struct lm_ggml_tensor * a,
1208
+ struct lm_ggml_tensor * b,
1209
+ size_t nb1,
1210
+ size_t offset); // in bytes
1211
+
1212
+ // a -> b, return view(b)
1213
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cpy(
1214
+ struct lm_ggml_context * ctx,
1215
+ struct lm_ggml_tensor * a,
1216
+ struct lm_ggml_tensor * b);
1217
+
1218
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cast(
1219
+ struct lm_ggml_context * ctx,
1220
+ struct lm_ggml_tensor * a,
1221
+ enum lm_ggml_type type);
1222
+
1223
+ // make contiguous
1224
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont(
1225
+ struct lm_ggml_context * ctx,
1226
+ struct lm_ggml_tensor * a);
1227
+
1228
+ // make contiguous, with new shape
1229
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_1d(
1230
+ struct lm_ggml_context * ctx,
1231
+ struct lm_ggml_tensor * a,
1232
+ int64_t ne0);
1233
+
1234
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_2d(
1235
+ struct lm_ggml_context * ctx,
1236
+ struct lm_ggml_tensor * a,
1237
+ int64_t ne0,
1238
+ int64_t ne1);
1239
+
1240
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_3d(
1241
+ struct lm_ggml_context * ctx,
1242
+ struct lm_ggml_tensor * a,
1243
+ int64_t ne0,
1244
+ int64_t ne1,
1245
+ int64_t ne2);
1246
+
1247
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cont_4d(
1248
+ struct lm_ggml_context * ctx,
1249
+ struct lm_ggml_tensor * a,
1250
+ int64_t ne0,
1251
+ int64_t ne1,
1252
+ int64_t ne2,
1253
+ int64_t ne3);
1254
+
1255
+ // return view(a), b specifies the new shape
1256
+ // TODO: when we start computing gradient, make a copy instead of view
1257
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape(
1258
+ struct lm_ggml_context * ctx,
1259
+ struct lm_ggml_tensor * a,
1260
+ struct lm_ggml_tensor * b);
1261
+
1262
+ // return view(a)
1263
+ // TODO: when we start computing gradient, make a copy instead of view
1264
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_1d(
1265
+ struct lm_ggml_context * ctx,
1266
+ struct lm_ggml_tensor * a,
1267
+ int64_t ne0);
1268
+
1269
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_2d(
1270
+ struct lm_ggml_context * ctx,
1271
+ struct lm_ggml_tensor * a,
1272
+ int64_t ne0,
1273
+ int64_t ne1);
1274
+
1275
+ // return view(a)
1276
+ // TODO: when we start computing gradient, make a copy instead of view
1277
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_3d(
1278
+ struct lm_ggml_context * ctx,
1279
+ struct lm_ggml_tensor * a,
1280
+ int64_t ne0,
1281
+ int64_t ne1,
1282
+ int64_t ne2);
1283
+
1284
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_reshape_4d(
1285
+ struct lm_ggml_context * ctx,
1286
+ struct lm_ggml_tensor * a,
1287
+ int64_t ne0,
1288
+ int64_t ne1,
1289
+ int64_t ne2,
1290
+ int64_t ne3);
1291
+
1292
+ // offset in bytes
1293
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_1d(
1294
+ struct lm_ggml_context * ctx,
1295
+ struct lm_ggml_tensor * a,
1296
+ int64_t ne0,
1297
+ size_t offset);
1298
+
1299
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_2d(
1300
+ struct lm_ggml_context * ctx,
1301
+ struct lm_ggml_tensor * a,
1302
+ int64_t ne0,
1303
+ int64_t ne1,
1304
+ size_t nb1, // row stride in bytes
1305
+ size_t offset);
1306
+
1307
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_3d(
1308
+ struct lm_ggml_context * ctx,
1309
+ struct lm_ggml_tensor * a,
1310
+ int64_t ne0,
1311
+ int64_t ne1,
1312
+ int64_t ne2,
1313
+ size_t nb1, // row stride in bytes
1314
+ size_t nb2, // slice stride in bytes
1315
+ size_t offset);
1316
+
1317
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_view_4d(
1318
+ struct lm_ggml_context * ctx,
1319
+ struct lm_ggml_tensor * a,
1320
+ int64_t ne0,
1321
+ int64_t ne1,
1322
+ int64_t ne2,
1323
+ int64_t ne3,
1324
+ size_t nb1, // row stride in bytes
1325
+ size_t nb2, // slice stride in bytes
1326
+ size_t nb3,
1327
+ size_t offset);
1328
+
1329
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_permute(
1330
+ struct lm_ggml_context * ctx,
1331
+ struct lm_ggml_tensor * a,
1332
+ int axis0,
1333
+ int axis1,
1334
+ int axis2,
1335
+ int axis3);
1336
+
1337
+ // alias for lm_ggml_permute(ctx, a, 1, 0, 2, 3)
1338
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_transpose(
1339
+ struct lm_ggml_context * ctx,
1340
+ struct lm_ggml_tensor * a);
1341
+
1342
+ // supports 3D: a->ne[2] == b->ne[1]
1343
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rows(
1344
+ struct lm_ggml_context * ctx,
1345
+ struct lm_ggml_tensor * a, // data
1346
+ struct lm_ggml_tensor * b); // row indices
1347
+
1348
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rows_back(
1349
+ struct lm_ggml_context * ctx,
1350
+ struct lm_ggml_tensor * a, // gradients of lm_ggml_get_rows result
1351
+ struct lm_ggml_tensor * b, // row indices
1352
+ struct lm_ggml_tensor * c); // data for lm_ggml_get_rows, only used for its shape
1353
+
1354
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag(
1355
+ struct lm_ggml_context * ctx,
1356
+ struct lm_ggml_tensor * a);
1357
+
1358
+ // set elements above the diagonal to -INF
1359
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_inf(
1360
+ struct lm_ggml_context * ctx,
1361
+ struct lm_ggml_tensor * a,
1362
+ int n_past);
1363
+
1364
+ // in-place, returns view(a)
1365
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_inf_inplace(
1366
+ struct lm_ggml_context * ctx,
1367
+ struct lm_ggml_tensor * a,
1368
+ int n_past);
1369
+
1370
+ // set elements above the diagonal to 0
1371
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_zero(
1372
+ struct lm_ggml_context * ctx,
1373
+ struct lm_ggml_tensor * a,
1374
+ int n_past);
1375
+
1376
+ // in-place, returns view(a)
1377
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_diag_mask_zero_inplace(
1378
+ struct lm_ggml_context * ctx,
1379
+ struct lm_ggml_tensor * a,
1380
+ int n_past);
1381
+
1382
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max(
1383
+ struct lm_ggml_context * ctx,
1384
+ struct lm_ggml_tensor * a);
1385
+
1386
+ // in-place, returns view(a)
1387
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_inplace(
1388
+ struct lm_ggml_context * ctx,
1389
+ struct lm_ggml_tensor * a);
1390
+
1391
+ // fused soft_max(a*scale + mask*(ALiBi slope))
1392
+ // mask is optional
1393
+ // max_bias = 0.0f for no ALiBi
1394
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_ext(
1395
+ struct lm_ggml_context * ctx,
1396
+ struct lm_ggml_tensor * a,
1397
+ struct lm_ggml_tensor * mask,
1398
+ float scale,
1399
+ float max_bias);
1400
+
1401
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_back(
1402
+ struct lm_ggml_context * ctx,
1403
+ struct lm_ggml_tensor * a,
1404
+ struct lm_ggml_tensor * b);
1405
+
1406
+ // in-place, returns view(a)
1407
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_soft_max_back_inplace(
1408
+ struct lm_ggml_context * ctx,
1409
+ struct lm_ggml_tensor * a,
1410
+ struct lm_ggml_tensor * b);
1411
+
1412
+ // rotary position embedding
1413
+ // if (mode & 1) - skip n_past elements (NOT SUPPORTED)
1414
+ // if (mode & LM_GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
1415
+ //
1416
+ // b is an int32 vector with size a->ne[2], it contains the positions
1417
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope(
1418
+ struct lm_ggml_context * ctx,
1419
+ struct lm_ggml_tensor * a,
1420
+ struct lm_ggml_tensor * b,
1421
+ int n_dims,
1422
+ int mode);
1423
+
1424
+ // in-place, returns view(a)
1425
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_inplace(
1426
+ struct lm_ggml_context * ctx,
1427
+ struct lm_ggml_tensor * a,
1428
+ struct lm_ggml_tensor * b,
1429
+ int n_dims,
1430
+ int mode);
1431
+
1432
+ // custom RoPE
1433
+ // c is freq factors (e.g. phi3-128k), (optional)
1434
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext(
1435
+ struct lm_ggml_context * ctx,
1436
+ struct lm_ggml_tensor * a,
1437
+ struct lm_ggml_tensor * b,
1438
+ struct lm_ggml_tensor * c,
1439
+ int n_dims,
1440
+ int mode,
1441
+ int n_ctx_orig,
1442
+ float freq_base,
1443
+ float freq_scale,
1444
+ float ext_factor,
1445
+ float attn_factor,
1446
+ float beta_fast,
1447
+ float beta_slow);
1448
+
1449
+ // in-place, returns view(a)
1450
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_ext_inplace(
1451
+ struct lm_ggml_context * ctx,
1452
+ struct lm_ggml_tensor * a,
1453
+ struct lm_ggml_tensor * b,
1454
+ struct lm_ggml_tensor * c,
1455
+ int n_dims,
1456
+ int mode,
1457
+ int n_ctx_orig,
1458
+ float freq_base,
1459
+ float freq_scale,
1460
+ float ext_factor,
1461
+ float attn_factor,
1462
+ float beta_fast,
1463
+ float beta_slow);
1464
+
1465
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_custom(
1466
+ struct lm_ggml_context * ctx,
1467
+ struct lm_ggml_tensor * a,
1468
+ struct lm_ggml_tensor * b,
1469
+ int n_dims,
1470
+ int mode,
1471
+ int n_ctx_orig,
1472
+ float freq_base,
1473
+ float freq_scale,
1474
+ float ext_factor,
1475
+ float attn_factor,
1476
+ float beta_fast,
1477
+ float beta_slow),
1478
+ "use lm_ggml_rope_ext instead");
1479
+
1480
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_custom_inplace(
1481
+ struct lm_ggml_context * ctx,
1482
+ struct lm_ggml_tensor * a,
1483
+ struct lm_ggml_tensor * b,
1484
+ int n_dims,
1485
+ int mode,
1486
+ int n_ctx_orig,
1487
+ float freq_base,
1488
+ float freq_scale,
1489
+ float ext_factor,
1490
+ float attn_factor,
1491
+ float beta_fast,
1492
+ float beta_slow),
1493
+ "use lm_ggml_rope_ext_inplace instead");
1494
+
1495
+ // compute correction dims for YaRN RoPE scaling
1496
+ LM_GGML_API void lm_ggml_rope_yarn_corr_dims(
1497
+ int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]);
1498
+
1499
+ // rotary position embedding backward, i.e compute dx from dy
1500
+ // a - dy
1501
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rope_back(
1502
+ struct lm_ggml_context * ctx,
1503
+ struct lm_ggml_tensor * a, // gradients of lm_ggml_rope result
1504
+ struct lm_ggml_tensor * b, // positions
1505
+ struct lm_ggml_tensor * c, // freq factors
1506
+ int n_dims,
1507
+ int mode,
1508
+ int n_ctx_orig,
1509
+ float freq_base,
1510
+ float freq_scale,
1511
+ float ext_factor,
1512
+ float attn_factor,
1513
+ float beta_fast,
1514
+ float beta_slow);
1515
+
1516
+ // clamp
1517
+ // in-place, returns view(a)
1518
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_clamp(
1519
+ struct lm_ggml_context * ctx,
1520
+ struct lm_ggml_tensor * a,
1521
+ float min,
1522
+ float max);
1523
+
1524
+ // im2col
1525
+ // converts data into a format that effectively results in a convolution when combined with matrix multiplication
1526
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_im2col(
1527
+ struct lm_ggml_context * ctx,
1528
+ struct lm_ggml_tensor * a, // convolution kernel
1529
+ struct lm_ggml_tensor * b, // data
1530
+ int s0, // stride dimension 0
1531
+ int s1, // stride dimension 1
1532
+ int p0, // padding dimension 0
1533
+ int p1, // padding dimension 1
1534
+ int d0, // dilation dimension 0
1535
+ int d1, // dilation dimension 1
1536
+ bool is_2D,
1537
+ enum lm_ggml_type dst_type);
1538
+
1539
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_im2col_back(
1540
+ struct lm_ggml_context * ctx,
1541
+ struct lm_ggml_tensor * a, // convolution kernel
1542
+ struct lm_ggml_tensor * b, // gradient of im2col output
1543
+ int64_t * ne, // shape of im2col input
1544
+ int s0, // stride dimension 0
1545
+ int s1, // stride dimension 1
1546
+ int p0, // padding dimension 0
1547
+ int p1, // padding dimension 1
1548
+ int d0, // dilation dimension 0
1549
+ int d1, // dilation dimension 1
1550
+ bool is_2D);
1551
+
1552
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_depthwise_2d(
1553
+ struct lm_ggml_context * ctx,
1554
+ struct lm_ggml_tensor * a, // convolution kernel
1555
+ struct lm_ggml_tensor * b, // data
1556
+ int s0, // stride dimension 0
1557
+ int s1, // stride dimension 1
1558
+ int p0, // padding dimension 0
1559
+ int p1, // padding dimension 1
1560
+ int d0, // dilation dimension 0
1561
+ int d1); // dilation dimension 1
1562
+
1563
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_1d(
1564
+ struct lm_ggml_context * ctx,
1565
+ struct lm_ggml_tensor * a, // convolution kernel
1566
+ struct lm_ggml_tensor * b, // data
1567
+ int s0, // stride
1568
+ int p0, // padding
1569
+ int d0); // dilation
1570
+
1571
+ // conv_1d with padding = half
1572
+ // alias for lm_ggml_conv_1d(a, b, s, a->ne[0]/2, d)
1573
+ LM_GGML_API struct lm_ggml_tensor* lm_ggml_conv_1d_ph(
1574
+ struct lm_ggml_context * ctx,
1575
+ struct lm_ggml_tensor * a, // convolution kernel
1576
+ struct lm_ggml_tensor * b, // data
1577
+ int s, // stride
1578
+ int d); // dilation
1579
+
1580
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_transpose_1d(
1581
+ struct lm_ggml_context * ctx,
1582
+ struct lm_ggml_tensor * a, // convolution kernel
1583
+ struct lm_ggml_tensor * b, // data
1584
+ int s0, // stride
1585
+ int p0, // padding
1586
+ int d0); // dilation
1587
+
1588
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d(
1589
+ struct lm_ggml_context * ctx,
1590
+ struct lm_ggml_tensor * a, // convolution kernel
1591
+ struct lm_ggml_tensor * b, // data
1592
+ int s0, // stride dimension 0
1593
+ int s1, // stride dimension 1
1594
+ int p0, // padding dimension 0
1595
+ int p1, // padding dimension 1
1596
+ int d0, // dilation dimension 0
1597
+ int d1); // dilation dimension 1
1598
+
1599
+
1600
+ // kernel size is a->ne[0] x a->ne[1]
1601
+ // stride is equal to kernel size
1602
+ // padding is zero
1603
+ // example:
1604
+ // a: 16 16 3 768
1605
+ // b: 1024 1024 3 1
1606
+ // res: 64 64 768 1
1607
+ // used in sam
1608
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_sk_p0(
1609
+ struct lm_ggml_context * ctx,
1610
+ struct lm_ggml_tensor * a,
1611
+ struct lm_ggml_tensor * b);
1612
+
1613
+ // kernel size is a->ne[0] x a->ne[1]
1614
+ // stride is 1
1615
+ // padding is half
1616
+ // example:
1617
+ // a: 3 3 256 256
1618
+ // b: 64 64 256 1
1619
+ // res: 64 64 256 1
1620
+ // used in sam
1621
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_2d_s1_ph(
1622
+ struct lm_ggml_context * ctx,
1623
+ struct lm_ggml_tensor * a,
1624
+ struct lm_ggml_tensor * b);
1625
+
1626
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_conv_transpose_2d_p0(
1627
+ struct lm_ggml_context * ctx,
1628
+ struct lm_ggml_tensor * a,
1629
+ struct lm_ggml_tensor * b,
1630
+ int stride);
1631
+
1632
+ enum lm_ggml_op_pool {
1633
+ LM_GGML_OP_POOL_MAX,
1634
+ LM_GGML_OP_POOL_AVG,
1635
+ LM_GGML_OP_POOL_COUNT,
1636
+ };
1637
+
1638
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_1d(
1639
+ struct lm_ggml_context * ctx,
1640
+ struct lm_ggml_tensor * a,
1641
+ enum lm_ggml_op_pool op,
1642
+ int k0, // kernel size
1643
+ int s0, // stride
1644
+ int p0); // padding
1645
+
1646
+ // the result will have 2*p0 padding for the first dimension
1647
+ // and 2*p1 padding for the second dimension
1648
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_2d(
1649
+ struct lm_ggml_context * ctx,
1650
+ struct lm_ggml_tensor * a,
1651
+ enum lm_ggml_op_pool op,
1652
+ int k0,
1653
+ int k1,
1654
+ int s0,
1655
+ int s1,
1656
+ float p0,
1657
+ float p1);
1658
+
1659
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pool_2d_back(
1660
+ struct lm_ggml_context * ctx,
1661
+ struct lm_ggml_tensor * a,
1662
+ struct lm_ggml_tensor * af, // "a"/input used in forward pass
1663
+ enum lm_ggml_op_pool op,
1664
+ int k0,
1665
+ int k1,
1666
+ int s0,
1667
+ int s1,
1668
+ float p0,
1669
+ float p1);
1670
+
1671
+ // nearest interpolate
1672
+ // multiplies ne0 and ne1 by scale factor
1673
+ // used in stable-diffusion
1674
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_upscale(
1675
+ struct lm_ggml_context * ctx,
1676
+ struct lm_ggml_tensor * a,
1677
+ int scale_factor);
1678
+
1679
+ // nearest interpolate
1680
+ // nearest interpolate to specified dimensions
1681
+ // used in tortoise.cpp
1682
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_upscale_ext(
1683
+ struct lm_ggml_context * ctx,
1684
+ struct lm_ggml_tensor * a,
1685
+ int ne0,
1686
+ int ne1,
1687
+ int ne2,
1688
+ int ne3);
1689
+
1690
+ // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
1691
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_pad(
1692
+ struct lm_ggml_context * ctx,
1693
+ struct lm_ggml_tensor * a,
1694
+ int p0,
1695
+ int p1,
1696
+ int p2,
1697
+ int p3);
1698
+
1699
+ // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
1700
+ // timesteps: [N,]
1701
+ // return: [N, dim]
1702
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_timestep_embedding(
1703
+ struct lm_ggml_context * ctx,
1704
+ struct lm_ggml_tensor * timesteps,
1705
+ int dim,
1706
+ int max_period);
1707
+
1708
+ // sort rows
1709
+ enum lm_ggml_sort_order {
1710
+ LM_GGML_SORT_ORDER_ASC,
1711
+ LM_GGML_SORT_ORDER_DESC,
1712
+ };
1713
+
1714
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_argsort(
1715
+ struct lm_ggml_context * ctx,
1716
+ struct lm_ggml_tensor * a,
1717
+ enum lm_ggml_sort_order order);
1718
+
1719
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_arange(
1720
+ struct lm_ggml_context * ctx,
1721
+ float start,
1722
+ float stop,
1723
+ float step);
1724
+
1725
+ // top k elements per row
1726
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_top_k(
1727
+ struct lm_ggml_context * ctx,
1728
+ struct lm_ggml_tensor * a,
1729
+ int k);
1730
+
1731
+ #define LM_GGML_KQ_MASK_PAD 32
1732
+
1733
+ // q: [n_embd, n_batch, n_head, 1]
1734
+ // k: [n_embd, n_kv, n_head_kv, 1]
1735
+ // v: [n_embd, n_kv, n_head_kv, 1] !! not transposed !!
1736
+ // mask: [n_kv, n_batch_pad, 1, 1] !! n_batch_pad = LM_GGML_PAD(n_batch, LM_GGML_KQ_MASK_PAD) !!
1737
+ // res: [n_embd, n_head, n_batch, 1] !! permuted !!
1738
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_flash_attn_ext(
1739
+ struct lm_ggml_context * ctx,
1740
+ struct lm_ggml_tensor * q,
1741
+ struct lm_ggml_tensor * k,
1742
+ struct lm_ggml_tensor * v,
1743
+ struct lm_ggml_tensor * mask,
1744
+ float scale,
1745
+ float max_bias,
1746
+ float logit_softcap);
1747
+
1748
+ LM_GGML_API void lm_ggml_flash_attn_ext_set_prec(
1749
+ struct lm_ggml_tensor * a,
1750
+ enum lm_ggml_prec prec);
1751
+
1752
+ LM_GGML_API enum lm_ggml_prec lm_ggml_flash_attn_ext_get_prec(
1753
+ const struct lm_ggml_tensor * a);
1754
+
1755
+ // TODO: needs to be adapted to lm_ggml_flash_attn_ext
1756
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_flash_attn_back(
1757
+ struct lm_ggml_context * ctx,
1758
+ struct lm_ggml_tensor * q,
1759
+ struct lm_ggml_tensor * k,
1760
+ struct lm_ggml_tensor * v,
1761
+ struct lm_ggml_tensor * d,
1762
+ bool masked);
1763
+
1764
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_ssm_conv(
1765
+ struct lm_ggml_context * ctx,
1766
+ struct lm_ggml_tensor * sx,
1767
+ struct lm_ggml_tensor * c);
1768
+
1769
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_ssm_scan(
1770
+ struct lm_ggml_context * ctx,
1771
+ struct lm_ggml_tensor * s,
1772
+ struct lm_ggml_tensor * x,
1773
+ struct lm_ggml_tensor * dt,
1774
+ struct lm_ggml_tensor * A,
1775
+ struct lm_ggml_tensor * B,
1776
+ struct lm_ggml_tensor * C);
1777
+
1778
+ // partition into non-overlapping windows with padding if needed
1779
+ // example:
1780
+ // a: 768 64 64 1
1781
+ // w: 14
1782
+ // res: 768 14 14 25
1783
+ // used in sam
1784
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_win_part(
1785
+ struct lm_ggml_context * ctx,
1786
+ struct lm_ggml_tensor * a,
1787
+ int w);
1788
+
1789
+ // reverse of lm_ggml_win_part
1790
+ // used in sam
1791
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_win_unpart(
1792
+ struct lm_ggml_context * ctx,
1793
+ struct lm_ggml_tensor * a,
1794
+ int w0,
1795
+ int h0,
1796
+ int w);
1797
+
1798
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_unary(
1799
+ struct lm_ggml_context * ctx,
1800
+ struct lm_ggml_tensor * a,
1801
+ enum lm_ggml_unary_op op);
1802
+
1803
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_unary_inplace(
1804
+ struct lm_ggml_context * ctx,
1805
+ struct lm_ggml_tensor * a,
1806
+ enum lm_ggml_unary_op op);
1807
+
1808
+ // used in sam
1809
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_get_rel_pos(
1810
+ struct lm_ggml_context * ctx,
1811
+ struct lm_ggml_tensor * a,
1812
+ int qh,
1813
+ int kh);
1814
+
1815
+ // used in sam
1816
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_rel_pos(
1817
+ struct lm_ggml_context * ctx,
1818
+ struct lm_ggml_tensor * a,
1819
+ struct lm_ggml_tensor * pw,
1820
+ struct lm_ggml_tensor * ph);
1821
+
1822
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_add_rel_pos_inplace(
1823
+ struct lm_ggml_context * ctx,
1824
+ struct lm_ggml_tensor * a,
1825
+ struct lm_ggml_tensor * pw,
1826
+ struct lm_ggml_tensor * ph);
1827
+
1828
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_rwkv_wkv6(
1829
+ struct lm_ggml_context * ctx,
1830
+ struct lm_ggml_tensor * k,
1831
+ struct lm_ggml_tensor * v,
1832
+ struct lm_ggml_tensor * r,
1833
+ struct lm_ggml_tensor * tf,
1834
+ struct lm_ggml_tensor * td,
1835
+ struct lm_ggml_tensor * state);
1836
+
1837
+ // custom operators
1838
+
1839
+ typedef void (*lm_ggml_unary_op_f32_t) (const int, float *, const float *);
1840
+ typedef void (*lm_ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
1841
+
1842
+ typedef void (*lm_ggml_custom1_op_f32_t)(struct lm_ggml_tensor *, const struct lm_ggml_tensor *);
1843
+ typedef void (*lm_ggml_custom2_op_f32_t)(struct lm_ggml_tensor *, const struct lm_ggml_tensor *, const struct lm_ggml_tensor *);
1844
+ typedef void (*lm_ggml_custom3_op_f32_t)(struct lm_ggml_tensor *, const struct lm_ggml_tensor *, const struct lm_ggml_tensor *, const struct lm_ggml_tensor *);
1845
+
1846
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_unary_f32(
1847
+ struct lm_ggml_context * ctx,
1848
+ struct lm_ggml_tensor * a,
1849
+ lm_ggml_unary_op_f32_t fun),
1850
+ "use lm_ggml_map_custom1 instead");
1851
+
1852
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_unary_inplace_f32(
1853
+ struct lm_ggml_context * ctx,
1854
+ struct lm_ggml_tensor * a,
1855
+ lm_ggml_unary_op_f32_t fun),
1856
+ "use lm_ggml_map_custom1_inplace instead");
1857
+
1858
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_binary_f32(
1859
+ struct lm_ggml_context * ctx,
1860
+ struct lm_ggml_tensor * a,
1861
+ struct lm_ggml_tensor * b,
1862
+ lm_ggml_binary_op_f32_t fun),
1863
+ "use lm_ggml_map_custom2 instead");
1864
+
1865
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_binary_inplace_f32(
1866
+ struct lm_ggml_context * ctx,
1867
+ struct lm_ggml_tensor * a,
1868
+ struct lm_ggml_tensor * b,
1869
+ lm_ggml_binary_op_f32_t fun),
1870
+ "use lm_ggml_map_custom2_inplace instead");
1871
+
1872
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_f32(
1873
+ struct lm_ggml_context * ctx,
1874
+ struct lm_ggml_tensor * a,
1875
+ lm_ggml_custom1_op_f32_t fun),
1876
+ "use lm_ggml_map_custom1 instead");
1877
+
1878
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_inplace_f32(
1879
+ struct lm_ggml_context * ctx,
1880
+ struct lm_ggml_tensor * a,
1881
+ lm_ggml_custom1_op_f32_t fun),
1882
+ "use lm_ggml_map_custom1_inplace instead");
1883
+
1884
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_f32(
1885
+ struct lm_ggml_context * ctx,
1886
+ struct lm_ggml_tensor * a,
1887
+ struct lm_ggml_tensor * b,
1888
+ lm_ggml_custom2_op_f32_t fun),
1889
+ "use lm_ggml_map_custom2 instead");
1890
+
1891
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_inplace_f32(
1892
+ struct lm_ggml_context * ctx,
1893
+ struct lm_ggml_tensor * a,
1894
+ struct lm_ggml_tensor * b,
1895
+ lm_ggml_custom2_op_f32_t fun),
1896
+ "use lm_ggml_map_custom2_inplace instead");
1897
+
1898
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_f32(
1899
+ struct lm_ggml_context * ctx,
1900
+ struct lm_ggml_tensor * a,
1901
+ struct lm_ggml_tensor * b,
1902
+ struct lm_ggml_tensor * c,
1903
+ lm_ggml_custom3_op_f32_t fun),
1904
+ "use lm_ggml_map_custom3 instead");
1905
+
1906
+ LM_GGML_DEPRECATED(LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_inplace_f32(
1907
+ struct lm_ggml_context * ctx,
1908
+ struct lm_ggml_tensor * a,
1909
+ struct lm_ggml_tensor * b,
1910
+ struct lm_ggml_tensor * c,
1911
+ lm_ggml_custom3_op_f32_t fun),
1912
+ "use lm_ggml_map_custom3_inplace instead");
1913
+
1914
+ // custom operators v2
1915
+
1916
+ typedef void (*lm_ggml_custom1_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, int ith, int nth, void * userdata);
1917
+ typedef void (*lm_ggml_custom2_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, const struct lm_ggml_tensor * b, int ith, int nth, void * userdata);
1918
+ typedef void (*lm_ggml_custom3_op_t)(struct lm_ggml_tensor * dst , const struct lm_ggml_tensor * a, const struct lm_ggml_tensor * b, const struct lm_ggml_tensor * c, int ith, int nth, void * userdata);
1919
+
1920
+ #define LM_GGML_N_TASKS_MAX (-1)
1921
+ // n_tasks == LM_GGML_N_TASKS_MAX means to use max number of tasks
1922
+
1923
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1(
1924
+ struct lm_ggml_context * ctx,
1925
+ struct lm_ggml_tensor * a,
1926
+ lm_ggml_custom1_op_t fun,
1927
+ int n_tasks,
1928
+ void * userdata);
1929
+
1930
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom1_inplace(
1931
+ struct lm_ggml_context * ctx,
1932
+ struct lm_ggml_tensor * a,
1933
+ lm_ggml_custom1_op_t fun,
1934
+ int n_tasks,
1935
+ void * userdata);
1936
+
1937
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2(
1938
+ struct lm_ggml_context * ctx,
1939
+ struct lm_ggml_tensor * a,
1940
+ struct lm_ggml_tensor * b,
1941
+ lm_ggml_custom2_op_t fun,
1942
+ int n_tasks,
1943
+ void * userdata);
1944
+
1945
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom2_inplace(
1946
+ struct lm_ggml_context * ctx,
1947
+ struct lm_ggml_tensor * a,
1948
+ struct lm_ggml_tensor * b,
1949
+ lm_ggml_custom2_op_t fun,
1950
+ int n_tasks,
1951
+ void * userdata);
1952
+
1953
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3(
1954
+ struct lm_ggml_context * ctx,
1955
+ struct lm_ggml_tensor * a,
1956
+ struct lm_ggml_tensor * b,
1957
+ struct lm_ggml_tensor * c,
1958
+ lm_ggml_custom3_op_t fun,
1959
+ int n_tasks,
1960
+ void * userdata);
1961
+
1962
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_map_custom3_inplace(
1963
+ struct lm_ggml_context * ctx,
1964
+ struct lm_ggml_tensor * a,
1965
+ struct lm_ggml_tensor * b,
1966
+ struct lm_ggml_tensor * c,
1967
+ lm_ggml_custom3_op_t fun,
1968
+ int n_tasks,
1969
+ void * userdata);
1970
+
1971
+ // loss function
1972
+
1973
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cross_entropy_loss(
1974
+ struct lm_ggml_context * ctx,
1975
+ struct lm_ggml_tensor * a, // logits
1976
+ struct lm_ggml_tensor * b); // labels
1977
+
1978
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_cross_entropy_loss_back(
1979
+ struct lm_ggml_context * ctx,
1980
+ struct lm_ggml_tensor * a, // logits
1981
+ struct lm_ggml_tensor * b, // labels
1982
+ struct lm_ggml_tensor * c); // gradients of cross_entropy_loss result
1983
+
1984
+ // AdamW optimizer step
1985
+ // Paper: https://arxiv.org/pdf/1711.05101v3.pdf
1986
+ // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
1987
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_opt_step_adamw(
1988
+ struct lm_ggml_context * ctx,
1989
+ struct lm_ggml_tensor * a,
1990
+ struct lm_ggml_tensor * grad,
1991
+ struct lm_ggml_tensor * m,
1992
+ struct lm_ggml_tensor * v,
1993
+ struct lm_ggml_tensor * adamw_params); // parameters such a the learning rate
1994
+
1995
+ //
1996
+ // automatic differentiation
1997
+ //
1998
+
1999
+ LM_GGML_API void lm_ggml_build_forward_expand(struct lm_ggml_cgraph * cgraph, struct lm_ggml_tensor * tensor);
2000
+ LM_GGML_API void lm_ggml_build_backward_expand(
2001
+ struct lm_ggml_context * ctx_static, // context for static gradients (loss + gradient accumulation)
2002
+ struct lm_ggml_context * ctx_compute, // context for gradient computation
2003
+ struct lm_ggml_cgraph * cgraph,
2004
+ bool accumulate); // whether or not gradients should be accumulated, requires static allocation of tensors in ctx_static
2005
+
2006
+ // graph allocation in a context
2007
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_new_graph (struct lm_ggml_context * ctx); // size = LM_GGML_DEFAULT_GRAPH_SIZE, grads = false
2008
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_new_graph_custom(struct lm_ggml_context * ctx, size_t size, bool grads);
2009
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_graph_dup (struct lm_ggml_context * ctx, struct lm_ggml_cgraph * cgraph);
2010
+ LM_GGML_API void lm_ggml_graph_cpy (struct lm_ggml_cgraph * src, struct lm_ggml_cgraph * dst);
2011
+ LM_GGML_API void lm_ggml_graph_reset (struct lm_ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
2012
+ LM_GGML_API void lm_ggml_graph_clear (struct lm_ggml_cgraph * cgraph);
2013
+
2014
+ LM_GGML_API int lm_ggml_graph_size (struct lm_ggml_cgraph * cgraph);
2015
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_node (struct lm_ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i]
2016
+ LM_GGML_API struct lm_ggml_tensor ** lm_ggml_graph_nodes (struct lm_ggml_cgraph * cgraph);
2017
+ LM_GGML_API int lm_ggml_graph_n_nodes(struct lm_ggml_cgraph * cgraph);
2018
+
2019
+ LM_GGML_API void lm_ggml_graph_add_node(struct lm_ggml_cgraph * cgraph, struct lm_ggml_tensor * tensor);
2020
+
2021
+ LM_GGML_API size_t lm_ggml_graph_overhead(void);
2022
+ LM_GGML_API size_t lm_ggml_graph_overhead_custom(size_t size, bool grads);
2023
+
2024
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_tensor (const struct lm_ggml_cgraph * cgraph, const char * name);
2025
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_grad (const struct lm_ggml_cgraph * cgraph, const struct lm_ggml_tensor * node);
2026
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_graph_get_grad_acc(const struct lm_ggml_cgraph * cgraph, const struct lm_ggml_tensor * node);
2027
+
2028
+ LM_GGML_API void lm_ggml_graph_export(const struct lm_ggml_cgraph * cgraph, const char * fname);
2029
+ LM_GGML_API struct lm_ggml_cgraph * lm_ggml_graph_import(const char * fname, struct lm_ggml_context ** ctx_data, struct lm_ggml_context ** ctx_eval);
2030
+
2031
+ // print info and performance information for the graph
2032
+ LM_GGML_API void lm_ggml_graph_print(const struct lm_ggml_cgraph * cgraph);
2033
+
2034
+ // dump the graph into a file using the dot format
2035
+ LM_GGML_API void lm_ggml_graph_dump_dot(const struct lm_ggml_cgraph * gb, const struct lm_ggml_cgraph * gf, const char * filename);
2036
+
2037
+ // TODO these functions were sandwiched in the old optimization interface, is there a better place for them?
2038
+ typedef void (*lm_ggml_log_callback)(enum lm_ggml_log_level level, const char * text, void * user_data);
2039
+
2040
+ // Set callback for all future logging events.
2041
+ // If this is not called, or NULL is supplied, everything is output on stderr.
2042
+ LM_GGML_API void lm_ggml_log_set(lm_ggml_log_callback log_callback, void * user_data);
2043
+
2044
+ LM_GGML_API struct lm_ggml_tensor * lm_ggml_set_zero(struct lm_ggml_tensor * tensor);
2045
+
2046
+ //
2047
+ // quantization
2048
+ //
2049
+
2050
+ // - lm_ggml_quantize_init can be called multiple times with the same type
2051
+ // it will only initialize the quantization tables for the first call or after lm_ggml_quantize_free
2052
+ // automatically called by lm_ggml_quantize_chunk for convenience
2053
+ //
2054
+ // - lm_ggml_quantize_free will free any memory allocated by lm_ggml_quantize_init
2055
+ // call this at the end of the program to avoid memory leaks
2056
+ //
2057
+ // note: these are thread-safe
2058
+ //
2059
+ LM_GGML_API void lm_ggml_quantize_init(enum lm_ggml_type type);
2060
+ LM_GGML_API void lm_ggml_quantize_free(void);
2061
+
2062
+ // some quantization type cannot be used without an importance matrix
2063
+ LM_GGML_API bool lm_ggml_quantize_requires_imatrix(enum lm_ggml_type type);
2064
+
2065
+ // calls lm_ggml_quantize_init internally (i.e. can allocate memory)
2066
+ LM_GGML_API size_t lm_ggml_quantize_chunk(
2067
+ enum lm_ggml_type type,
2068
+ const float * src,
2069
+ void * dst,
2070
+ int64_t start,
2071
+ int64_t nrows,
2072
+ int64_t n_per_row,
2073
+ const float * imatrix);
2074
+
2075
+ //
2076
+ // gguf
2077
+ //
2078
+
2079
+ enum lm_gguf_type {
2080
+ LM_GGUF_TYPE_UINT8 = 0,
2081
+ LM_GGUF_TYPE_INT8 = 1,
2082
+ LM_GGUF_TYPE_UINT16 = 2,
2083
+ LM_GGUF_TYPE_INT16 = 3,
2084
+ LM_GGUF_TYPE_UINT32 = 4,
2085
+ LM_GGUF_TYPE_INT32 = 5,
2086
+ LM_GGUF_TYPE_FLOAT32 = 6,
2087
+ LM_GGUF_TYPE_BOOL = 7,
2088
+ LM_GGUF_TYPE_STRING = 8,
2089
+ LM_GGUF_TYPE_ARRAY = 9,
2090
+ LM_GGUF_TYPE_UINT64 = 10,
2091
+ LM_GGUF_TYPE_INT64 = 11,
2092
+ LM_GGUF_TYPE_FLOAT64 = 12,
2093
+ LM_GGUF_TYPE_COUNT, // marks the end of the enum
2094
+ };
2095
+
2096
+ struct lm_gguf_context;
2097
+
2098
+ struct lm_gguf_init_params {
2099
+ bool no_alloc;
2100
+
2101
+ // if not NULL, create a lm_ggml_context and allocate the tensor data in it
2102
+ struct lm_ggml_context ** ctx;
2103
+ };
2104
+
2105
+ LM_GGML_API struct lm_gguf_context * lm_gguf_init_empty(void);
2106
+ LM_GGML_API struct lm_gguf_context * lm_gguf_init_from_file(const char * fname, struct lm_gguf_init_params params);
2107
+ //LM_GGML_API struct lm_gguf_context * lm_gguf_init_from_buffer(..);
2108
+
2109
+ LM_GGML_API void lm_gguf_free(struct lm_gguf_context * ctx);
2110
+
2111
+ LM_GGML_API const char * lm_gguf_type_name(enum lm_gguf_type type);
2112
+
2113
+ LM_GGML_API int lm_gguf_get_version (const struct lm_gguf_context * ctx);
2114
+ LM_GGML_API size_t lm_gguf_get_alignment (const struct lm_gguf_context * ctx);
2115
+ LM_GGML_API size_t lm_gguf_get_data_offset(const struct lm_gguf_context * ctx);
2116
+ LM_GGML_API void * lm_gguf_get_data (const struct lm_gguf_context * ctx);
2117
+
2118
+ LM_GGML_API int lm_gguf_get_n_kv(const struct lm_gguf_context * ctx);
2119
+ LM_GGML_API int lm_gguf_find_key(const struct lm_gguf_context * ctx, const char * key);
2120
+ LM_GGML_API const char * lm_gguf_get_key (const struct lm_gguf_context * ctx, int key_id);
2121
+
2122
+ LM_GGML_API enum lm_gguf_type lm_gguf_get_kv_type (const struct lm_gguf_context * ctx, int key_id);
2123
+ LM_GGML_API enum lm_gguf_type lm_gguf_get_arr_type(const struct lm_gguf_context * ctx, int key_id);
2124
+
2125
+ // will abort if the wrong type is used for the key
2126
+ LM_GGML_API uint8_t lm_gguf_get_val_u8 (const struct lm_gguf_context * ctx, int key_id);
2127
+ LM_GGML_API int8_t lm_gguf_get_val_i8 (const struct lm_gguf_context * ctx, int key_id);
2128
+ LM_GGML_API uint16_t lm_gguf_get_val_u16 (const struct lm_gguf_context * ctx, int key_id);
2129
+ LM_GGML_API int16_t lm_gguf_get_val_i16 (const struct lm_gguf_context * ctx, int key_id);
2130
+ LM_GGML_API uint32_t lm_gguf_get_val_u32 (const struct lm_gguf_context * ctx, int key_id);
2131
+ LM_GGML_API int32_t lm_gguf_get_val_i32 (const struct lm_gguf_context * ctx, int key_id);
2132
+ LM_GGML_API float lm_gguf_get_val_f32 (const struct lm_gguf_context * ctx, int key_id);
2133
+ LM_GGML_API uint64_t lm_gguf_get_val_u64 (const struct lm_gguf_context * ctx, int key_id);
2134
+ LM_GGML_API int64_t lm_gguf_get_val_i64 (const struct lm_gguf_context * ctx, int key_id);
2135
+ LM_GGML_API double lm_gguf_get_val_f64 (const struct lm_gguf_context * ctx, int key_id);
2136
+ LM_GGML_API bool lm_gguf_get_val_bool(const struct lm_gguf_context * ctx, int key_id);
2137
+ LM_GGML_API const char * lm_gguf_get_val_str (const struct lm_gguf_context * ctx, int key_id);
2138
+ LM_GGML_API const void * lm_gguf_get_val_data(const struct lm_gguf_context * ctx, int key_id);
2139
+ LM_GGML_API int lm_gguf_get_arr_n (const struct lm_gguf_context * ctx, int key_id);
2140
+ LM_GGML_API const void * lm_gguf_get_arr_data(const struct lm_gguf_context * ctx, int key_id);
2141
+ LM_GGML_API const char * lm_gguf_get_arr_str (const struct lm_gguf_context * ctx, int key_id, int i);
2142
+
2143
+ LM_GGML_API int lm_gguf_get_n_tensors (const struct lm_gguf_context * ctx);
2144
+ LM_GGML_API int lm_gguf_find_tensor (const struct lm_gguf_context * ctx, const char * name);
2145
+ LM_GGML_API size_t lm_gguf_get_tensor_offset(const struct lm_gguf_context * ctx, int i);
2146
+ LM_GGML_API char * lm_gguf_get_tensor_name (const struct lm_gguf_context * ctx, int i);
2147
+ LM_GGML_API enum lm_ggml_type lm_gguf_get_tensor_type (const struct lm_gguf_context * ctx, int i);
2148
+
2149
+ // removes key if it exists
2150
+ LM_GGML_API void lm_gguf_remove_key(struct lm_gguf_context * ctx, const char * key);
2151
+
2152
+ // overrides existing values or adds a new one
2153
+ LM_GGML_API void lm_gguf_set_val_u8 (struct lm_gguf_context * ctx, const char * key, uint8_t val);
2154
+ LM_GGML_API void lm_gguf_set_val_i8 (struct lm_gguf_context * ctx, const char * key, int8_t val);
2155
+ LM_GGML_API void lm_gguf_set_val_u16 (struct lm_gguf_context * ctx, const char * key, uint16_t val);
2156
+ LM_GGML_API void lm_gguf_set_val_i16 (struct lm_gguf_context * ctx, const char * key, int16_t val);
2157
+ LM_GGML_API void lm_gguf_set_val_u32 (struct lm_gguf_context * ctx, const char * key, uint32_t val);
2158
+ LM_GGML_API void lm_gguf_set_val_i32 (struct lm_gguf_context * ctx, const char * key, int32_t val);
2159
+ LM_GGML_API void lm_gguf_set_val_f32 (struct lm_gguf_context * ctx, const char * key, float val);
2160
+ LM_GGML_API void lm_gguf_set_val_u64 (struct lm_gguf_context * ctx, const char * key, uint64_t val);
2161
+ LM_GGML_API void lm_gguf_set_val_i64 (struct lm_gguf_context * ctx, const char * key, int64_t val);
2162
+ LM_GGML_API void lm_gguf_set_val_f64 (struct lm_gguf_context * ctx, const char * key, double val);
2163
+ LM_GGML_API void lm_gguf_set_val_bool(struct lm_gguf_context * ctx, const char * key, bool val);
2164
+ LM_GGML_API void lm_gguf_set_val_str (struct lm_gguf_context * ctx, const char * key, const char * val);
2165
+ LM_GGML_API void lm_gguf_set_arr_data(struct lm_gguf_context * ctx, const char * key, enum lm_gguf_type type, const void * data, int n);
2166
+ LM_GGML_API void lm_gguf_set_arr_str (struct lm_gguf_context * ctx, const char * key, const char ** data, int n);
2167
+
2168
+ // set or add KV pairs from another context
2169
+ LM_GGML_API void lm_gguf_set_kv(struct lm_gguf_context * ctx, struct lm_gguf_context * src);
2170
+
2171
+ // manage tensor info
2172
+ LM_GGML_API void lm_gguf_add_tensor(struct lm_gguf_context * ctx, const struct lm_ggml_tensor * tensor);
2173
+ LM_GGML_API void lm_gguf_set_tensor_type(struct lm_gguf_context * ctx, const char * name, enum lm_ggml_type type);
2174
+ LM_GGML_API void lm_gguf_set_tensor_data(struct lm_gguf_context * ctx, const char * name, const void * data, size_t size);
2175
+
2176
+ // writing gguf files can be done in 2 ways:
2177
+ //
2178
+ // - write the entire lm_gguf_context to a binary file in a single pass:
2179
+ //
2180
+ // lm_gguf_write_to_file(ctx, fname);
2181
+ //
2182
+ // - first prepare a file with a placeholder for the meta data, write the tensor data, then write the meta data:
2183
+ //
2184
+ // FILE * f = fopen(fname, "wb");
2185
+ // fseek(f, lm_gguf_get_meta_size(ctx), SEEK_SET);
2186
+ // fwrite(f, ...);
2187
+ // void * data = lm_gguf_meta_get_meta_data(ctx);
2188
+ // fseek(f, 0, SEEK_SET);
2189
+ // fwrite(f, data, lm_gguf_get_meta_size(ctx));
2190
+ // free(data);
2191
+ // fclose(f);
2192
+ //
2193
+
2194
+ // write the entire context to a binary file
2195
+ LM_GGML_API void lm_gguf_write_to_file(const struct lm_gguf_context * ctx, const char * fname, bool only_meta);
2196
+
2197
+ // get the size in bytes of the meta data (header, kv pairs, tensor info) including padding
2198
+ LM_GGML_API size_t lm_gguf_get_meta_size(const struct lm_gguf_context * ctx);
2199
+ LM_GGML_API void lm_gguf_get_meta_data(const struct lm_gguf_context * ctx, void * data);
2200
+
2201
+ #ifdef __cplusplus
2202
+ // restrict not standard in C++
2203
+ #define LM_GGML_RESTRICT
2204
+ #else
2205
+ #define LM_GGML_RESTRICT restrict
2206
+ #endif
2207
+ typedef void (*lm_ggml_to_float_t) (const void * LM_GGML_RESTRICT x, float * LM_GGML_RESTRICT y, int64_t k);
2208
+ typedef void (*lm_ggml_from_float_t)(const float * LM_GGML_RESTRICT x, void * LM_GGML_RESTRICT y, int64_t k);
2209
+
2210
+ struct lm_ggml_type_traits {
2211
+ const char * type_name;
2212
+ int64_t blck_size;
2213
+ int64_t blck_size_interleave; // interleave elements in blocks
2214
+ size_t type_size;
2215
+ bool is_quantized;
2216
+ lm_ggml_to_float_t to_float;
2217
+ lm_ggml_from_float_t from_float_ref;
2218
+ };
2219
+
2220
+ LM_GGML_API const struct lm_ggml_type_traits * lm_ggml_get_type_traits(enum lm_ggml_type type);
2221
+
2222
+ // ggml threadpool
2223
+ // TODO: currently, only a few functions are in the base ggml API, while the rest are in the CPU backend
2224
+ // the goal should be to create an API that other backends can use move everything to the ggml base
2225
+
2226
+ // scheduling priorities
2227
+ enum lm_ggml_sched_priority {
2228
+ LM_GGML_SCHED_PRIO_NORMAL,
2229
+ LM_GGML_SCHED_PRIO_MEDIUM,
2230
+ LM_GGML_SCHED_PRIO_HIGH,
2231
+ LM_GGML_SCHED_PRIO_REALTIME
2232
+ };
2233
+
2234
+ // threadpool params
2235
+ // Use lm_ggml_threadpool_params_default() or lm_ggml_threadpool_params_init() to populate the defaults
2236
+ struct lm_ggml_threadpool_params {
2237
+ bool cpumask[LM_GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
2238
+ int n_threads; // number of threads
2239
+ enum lm_ggml_sched_priority prio; // thread priority
2240
+ uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
2241
+ bool strict_cpu; // strict cpu placement
2242
+ bool paused; // start in paused state
2243
+ };
2244
+
2245
+ struct lm_ggml_threadpool; // forward declaration, see ggml.c
2246
+
2247
+ typedef struct lm_ggml_threadpool * lm_ggml_threadpool_t;
2248
+
2249
+ LM_GGML_API struct lm_ggml_threadpool_params lm_ggml_threadpool_params_default(int n_threads);
2250
+ LM_GGML_API void lm_ggml_threadpool_params_init (struct lm_ggml_threadpool_params * p, int n_threads);
2251
+ LM_GGML_API bool lm_ggml_threadpool_params_match (const struct lm_ggml_threadpool_params * p0, const struct lm_ggml_threadpool_params * p1);
2252
+
2253
+ #ifdef __cplusplus
2254
+ }
2255
+ #endif