cui-llama.rn 1.0.4 → 1.0.6

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