cui-llama.rn 1.4.3 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/README.md +93 -114
  2. package/android/src/main/CMakeLists.txt +5 -0
  3. package/android/src/main/java/com/rnllama/LlamaContext.java +91 -17
  4. package/android/src/main/java/com/rnllama/RNLlama.java +37 -4
  5. package/android/src/main/jni-utils.h +6 -0
  6. package/android/src/main/jni.cpp +289 -31
  7. package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
  10. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
  11. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
  12. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
  14. package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
  15. package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +7 -2
  16. package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +7 -2
  17. package/cpp/chat-template.hpp +529 -0
  18. package/cpp/chat.cpp +1779 -0
  19. package/cpp/chat.h +135 -0
  20. package/cpp/common.cpp +2064 -1873
  21. package/cpp/common.h +700 -699
  22. package/cpp/ggml-alloc.c +1039 -1042
  23. package/cpp/ggml-alloc.h +1 -1
  24. package/cpp/ggml-backend-impl.h +255 -255
  25. package/cpp/ggml-backend-reg.cpp +586 -582
  26. package/cpp/ggml-backend.cpp +2004 -2002
  27. package/cpp/ggml-backend.h +354 -354
  28. package/cpp/ggml-common.h +1851 -1853
  29. package/cpp/ggml-cpp.h +39 -39
  30. package/cpp/ggml-cpu-aarch64.cpp +4248 -4247
  31. package/cpp/ggml-cpu-aarch64.h +8 -8
  32. package/cpp/ggml-cpu-impl.h +531 -386
  33. package/cpp/ggml-cpu-quants.c +12527 -10920
  34. package/cpp/ggml-cpu-traits.cpp +36 -36
  35. package/cpp/ggml-cpu-traits.h +38 -38
  36. package/cpp/ggml-cpu.c +15766 -14391
  37. package/cpp/ggml-cpu.cpp +655 -635
  38. package/cpp/ggml-cpu.h +138 -135
  39. package/cpp/ggml-impl.h +567 -567
  40. package/cpp/ggml-metal-impl.h +235 -0
  41. package/cpp/ggml-metal.h +1 -1
  42. package/cpp/ggml-metal.m +5146 -4884
  43. package/cpp/ggml-opt.cpp +854 -854
  44. package/cpp/ggml-opt.h +216 -216
  45. package/cpp/ggml-quants.c +5238 -5238
  46. package/cpp/ggml-threading.h +14 -14
  47. package/cpp/ggml.c +6529 -6514
  48. package/cpp/ggml.h +2198 -2194
  49. package/cpp/gguf.cpp +1329 -1329
  50. package/cpp/gguf.h +202 -202
  51. package/cpp/json-schema-to-grammar.cpp +1024 -1045
  52. package/cpp/json-schema-to-grammar.h +21 -8
  53. package/cpp/json.hpp +24766 -24766
  54. package/cpp/llama-adapter.cpp +347 -347
  55. package/cpp/llama-adapter.h +74 -74
  56. package/cpp/llama-arch.cpp +1513 -1487
  57. package/cpp/llama-arch.h +403 -400
  58. package/cpp/llama-batch.cpp +368 -368
  59. package/cpp/llama-batch.h +88 -88
  60. package/cpp/llama-chat.cpp +588 -578
  61. package/cpp/llama-chat.h +53 -52
  62. package/cpp/llama-context.cpp +1775 -1775
  63. package/cpp/llama-context.h +128 -128
  64. package/cpp/llama-cparams.cpp +1 -1
  65. package/cpp/llama-cparams.h +37 -37
  66. package/cpp/llama-cpp.h +30 -30
  67. package/cpp/llama-grammar.cpp +1219 -1139
  68. package/cpp/llama-grammar.h +173 -143
  69. package/cpp/llama-hparams.cpp +71 -71
  70. package/cpp/llama-hparams.h +139 -139
  71. package/cpp/llama-impl.cpp +167 -167
  72. package/cpp/llama-impl.h +61 -61
  73. package/cpp/llama-kv-cache.cpp +718 -718
  74. package/cpp/llama-kv-cache.h +219 -218
  75. package/cpp/llama-mmap.cpp +600 -590
  76. package/cpp/llama-mmap.h +68 -67
  77. package/cpp/llama-model-loader.cpp +1124 -1124
  78. package/cpp/llama-model-loader.h +167 -167
  79. package/cpp/llama-model.cpp +4087 -3997
  80. package/cpp/llama-model.h +370 -370
  81. package/cpp/llama-sampling.cpp +2558 -2408
  82. package/cpp/llama-sampling.h +32 -32
  83. package/cpp/llama-vocab.cpp +3264 -3247
  84. package/cpp/llama-vocab.h +125 -125
  85. package/cpp/llama.cpp +10284 -10077
  86. package/cpp/llama.h +1354 -1323
  87. package/cpp/log.cpp +393 -401
  88. package/cpp/log.h +132 -121
  89. package/cpp/minja/chat-template.hpp +529 -0
  90. package/cpp/minja/minja.hpp +2915 -0
  91. package/cpp/minja.hpp +2915 -0
  92. package/cpp/rn-llama.cpp +66 -6
  93. package/cpp/rn-llama.h +26 -1
  94. package/cpp/sampling.cpp +570 -505
  95. package/cpp/sampling.h +3 -0
  96. package/cpp/sgemm.cpp +2598 -2597
  97. package/cpp/sgemm.h +14 -14
  98. package/cpp/speculative.cpp +278 -277
  99. package/cpp/speculative.h +28 -28
  100. package/cpp/unicode.cpp +9 -2
  101. package/ios/CMakeLists.txt +6 -0
  102. package/ios/RNLlama.h +0 -8
  103. package/ios/RNLlama.mm +27 -3
  104. package/ios/RNLlamaContext.h +10 -1
  105. package/ios/RNLlamaContext.mm +269 -57
  106. package/jest/mock.js +21 -2
  107. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  108. package/lib/commonjs/grammar.js +3 -0
  109. package/lib/commonjs/grammar.js.map +1 -1
  110. package/lib/commonjs/index.js +87 -13
  111. package/lib/commonjs/index.js.map +1 -1
  112. package/lib/module/NativeRNLlama.js.map +1 -1
  113. package/lib/module/grammar.js +3 -0
  114. package/lib/module/grammar.js.map +1 -1
  115. package/lib/module/index.js +86 -13
  116. package/lib/module/index.js.map +1 -1
  117. package/lib/typescript/NativeRNLlama.d.ts +107 -2
  118. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  119. package/lib/typescript/grammar.d.ts.map +1 -1
  120. package/lib/typescript/index.d.ts +32 -7
  121. package/lib/typescript/index.d.ts.map +1 -1
  122. package/llama-rn.podspec +1 -1
  123. package/package.json +3 -2
  124. package/src/NativeRNLlama.ts +115 -3
  125. package/src/grammar.ts +3 -0
  126. package/src/index.ts +138 -21
  127. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeCCompiler.cmake +0 -81
  128. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeSystem.cmake +0 -15
  129. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.c +0 -904
  130. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.o +0 -0
  131. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.cpp +0 -919
  132. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.o +0 -0
  133. package/android/src/main/build-arm64/CMakeFiles/CMakeConfigureLog.yaml +0 -55
  134. package/cpp/rn-llama.hpp +0 -913
package/cpp/ggml.h CHANGED
@@ -1,2194 +1,2198 @@
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
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__) && !defined(__clang__)
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 64
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
+ # if defined (_MSC_VER) && (__STDC_VERSION__ < 201112L)
2145
+ # define LM_GGML_RESTRICT __restrict
2146
+ # else
2147
+ # define LM_GGML_RESTRICT restrict
2148
+ # endif
2149
+ #endif
2150
+ typedef void (*lm_ggml_to_float_t) (const void * LM_GGML_RESTRICT x, float * LM_GGML_RESTRICT y, int64_t k);
2151
+ typedef void (*lm_ggml_from_float_t)(const float * LM_GGML_RESTRICT x, void * LM_GGML_RESTRICT y, int64_t k);
2152
+
2153
+ struct lm_ggml_type_traits {
2154
+ const char * type_name;
2155
+ int64_t blck_size;
2156
+ int64_t blck_size_interleave; // interleave elements in blocks
2157
+ size_t type_size;
2158
+ bool is_quantized;
2159
+ lm_ggml_to_float_t to_float;
2160
+ lm_ggml_from_float_t from_float_ref;
2161
+ };
2162
+
2163
+ LM_GGML_API const struct lm_ggml_type_traits * lm_ggml_get_type_traits(enum lm_ggml_type type);
2164
+
2165
+ // ggml threadpool
2166
+ // TODO: currently, only a few functions are in the base ggml API, while the rest are in the CPU backend
2167
+ // the goal should be to create an API that other backends can use move everything to the ggml base
2168
+
2169
+ // scheduling priorities
2170
+ enum lm_ggml_sched_priority {
2171
+ LM_GGML_SCHED_PRIO_NORMAL,
2172
+ LM_GGML_SCHED_PRIO_MEDIUM,
2173
+ LM_GGML_SCHED_PRIO_HIGH,
2174
+ LM_GGML_SCHED_PRIO_REALTIME
2175
+ };
2176
+
2177
+ // threadpool params
2178
+ // Use lm_ggml_threadpool_params_default() or lm_ggml_threadpool_params_init() to populate the defaults
2179
+ struct lm_ggml_threadpool_params {
2180
+ bool cpumask[LM_GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
2181
+ int n_threads; // number of threads
2182
+ enum lm_ggml_sched_priority prio; // thread priority
2183
+ uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
2184
+ bool strict_cpu; // strict cpu placement
2185
+ bool paused; // start in paused state
2186
+ };
2187
+
2188
+ struct lm_ggml_threadpool; // forward declaration, see ggml.c
2189
+
2190
+ typedef struct lm_ggml_threadpool * lm_ggml_threadpool_t;
2191
+
2192
+ LM_GGML_API struct lm_ggml_threadpool_params lm_ggml_threadpool_params_default(int n_threads);
2193
+ LM_GGML_API void lm_ggml_threadpool_params_init (struct lm_ggml_threadpool_params * p, int n_threads);
2194
+ LM_GGML_API bool lm_ggml_threadpool_params_match (const struct lm_ggml_threadpool_params * p0, const struct lm_ggml_threadpool_params * p1);
2195
+
2196
+ #ifdef __cplusplus
2197
+ }
2198
+ #endif