cui-llama.rn 1.2.4 → 1.3.0

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