cui-llama.rn 1.3.6 → 1.4.1

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