@fugood/llama.node 0.0.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/CMakeLists.txt +85 -0
  2. package/README.md +56 -0
  3. package/bin/darwin/arm64/llama-node.node +0 -0
  4. package/bin/darwin/x64/llama-node.node +0 -0
  5. package/bin/linux/arm64/llama-node.node +0 -0
  6. package/bin/linux/x64/llama-node.node +0 -0
  7. package/bin/win32/arm64/llama-node.node +0 -0
  8. package/bin/win32/arm64/node.lib +0 -0
  9. package/bin/win32/x64/llama-node.node +0 -0
  10. package/bin/win32/x64/node.lib +0 -0
  11. package/lib/binding.js +13 -0
  12. package/lib/binding.ts +57 -0
  13. package/lib/index.js +24 -0
  14. package/lib/index.ts +13 -0
  15. package/package.json +65 -0
  16. package/src/addons.cpp +506 -0
  17. package/src/llama.cpp/CMakeLists.txt +1320 -0
  18. package/src/llama.cpp/build.zig +172 -0
  19. package/src/llama.cpp/cmake/FindSIMD.cmake +100 -0
  20. package/src/llama.cpp/common/CMakeLists.txt +87 -0
  21. package/src/llama.cpp/common/base64.hpp +392 -0
  22. package/src/llama.cpp/common/common.cpp +2949 -0
  23. package/src/llama.cpp/common/common.h +324 -0
  24. package/src/llama.cpp/common/console.cpp +501 -0
  25. package/src/llama.cpp/common/console.h +19 -0
  26. package/src/llama.cpp/common/grammar-parser.cpp +440 -0
  27. package/src/llama.cpp/common/grammar-parser.h +29 -0
  28. package/src/llama.cpp/common/json-schema-to-grammar.cpp +764 -0
  29. package/src/llama.cpp/common/json-schema-to-grammar.h +4 -0
  30. package/src/llama.cpp/common/json.hpp +24766 -0
  31. package/src/llama.cpp/common/log.h +724 -0
  32. package/src/llama.cpp/common/ngram-cache.cpp +282 -0
  33. package/src/llama.cpp/common/ngram-cache.h +94 -0
  34. package/src/llama.cpp/common/sampling.cpp +353 -0
  35. package/src/llama.cpp/common/sampling.h +147 -0
  36. package/src/llama.cpp/common/stb_image.h +8396 -0
  37. package/src/llama.cpp/common/train.cpp +1513 -0
  38. package/src/llama.cpp/common/train.h +233 -0
  39. package/src/llama.cpp/examples/CMakeLists.txt +52 -0
  40. package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +5 -0
  41. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +1640 -0
  42. package/src/llama.cpp/examples/batched/CMakeLists.txt +5 -0
  43. package/src/llama.cpp/examples/batched/batched.cpp +262 -0
  44. package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +5 -0
  45. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +261 -0
  46. package/src/llama.cpp/examples/beam-search/CMakeLists.txt +5 -0
  47. package/src/llama.cpp/examples/beam-search/beam-search.cpp +188 -0
  48. package/src/llama.cpp/examples/benchmark/CMakeLists.txt +6 -0
  49. package/src/llama.cpp/examples/benchmark/benchmark-matmult.cpp +275 -0
  50. package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +5 -0
  51. package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +936 -0
  52. package/src/llama.cpp/examples/embedding/CMakeLists.txt +5 -0
  53. package/src/llama.cpp/examples/embedding/embedding.cpp +211 -0
  54. package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +9 -0
  55. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +195 -0
  56. package/src/llama.cpp/examples/export-lora/CMakeLists.txt +5 -0
  57. package/src/llama.cpp/examples/export-lora/export-lora.cpp +462 -0
  58. package/src/llama.cpp/examples/finetune/CMakeLists.txt +5 -0
  59. package/src/llama.cpp/examples/finetune/finetune.cpp +1861 -0
  60. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +5 -0
  61. package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +132 -0
  62. package/src/llama.cpp/examples/gguf/CMakeLists.txt +5 -0
  63. package/src/llama.cpp/examples/gguf/gguf.cpp +256 -0
  64. package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +5 -0
  65. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +553 -0
  66. package/src/llama.cpp/examples/gritlm/CMakeLists.txt +5 -0
  67. package/src/llama.cpp/examples/gritlm/gritlm.cpp +215 -0
  68. package/src/llama.cpp/examples/imatrix/CMakeLists.txt +5 -0
  69. package/src/llama.cpp/examples/imatrix/imatrix.cpp +655 -0
  70. package/src/llama.cpp/examples/infill/CMakeLists.txt +5 -0
  71. package/src/llama.cpp/examples/infill/infill.cpp +767 -0
  72. package/src/llama.cpp/examples/jeopardy/questions.txt +100 -0
  73. package/src/llama.cpp/examples/llama-bench/CMakeLists.txt +5 -0
  74. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +1286 -0
  75. package/src/llama.cpp/examples/llama.android/app/src/main/cpp/CMakeLists.txt +50 -0
  76. package/src/llama.cpp/examples/llama.android/app/src/main/cpp/llama-android.cpp +443 -0
  77. package/src/llama.cpp/examples/llava/CMakeLists.txt +37 -0
  78. package/src/llama.cpp/examples/llava/clip.cpp +2027 -0
  79. package/src/llama.cpp/examples/llava/clip.h +85 -0
  80. package/src/llama.cpp/examples/llava/llava-cli.cpp +309 -0
  81. package/src/llama.cpp/examples/llava/llava.cpp +426 -0
  82. package/src/llama.cpp/examples/llava/llava.h +50 -0
  83. package/src/llama.cpp/examples/llava/requirements.txt +3 -0
  84. package/src/llama.cpp/examples/lookahead/CMakeLists.txt +5 -0
  85. package/src/llama.cpp/examples/lookahead/lookahead.cpp +485 -0
  86. package/src/llama.cpp/examples/lookup/CMakeLists.txt +23 -0
  87. package/src/llama.cpp/examples/lookup/lookup-create.cpp +41 -0
  88. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +47 -0
  89. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +160 -0
  90. package/src/llama.cpp/examples/lookup/lookup.cpp +258 -0
  91. package/src/llama.cpp/examples/main/CMakeLists.txt +5 -0
  92. package/src/llama.cpp/examples/main/main.cpp +957 -0
  93. package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +33 -0
  94. package/src/llama.cpp/examples/parallel/CMakeLists.txt +5 -0
  95. package/src/llama.cpp/examples/parallel/parallel.cpp +427 -0
  96. package/src/llama.cpp/examples/passkey/CMakeLists.txt +5 -0
  97. package/src/llama.cpp/examples/passkey/passkey.cpp +302 -0
  98. package/src/llama.cpp/examples/perplexity/CMakeLists.txt +5 -0
  99. package/src/llama.cpp/examples/perplexity/perplexity.cpp +1943 -0
  100. package/src/llama.cpp/examples/quantize/CMakeLists.txt +6 -0
  101. package/src/llama.cpp/examples/quantize/quantize.cpp +423 -0
  102. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +6 -0
  103. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +424 -0
  104. package/src/llama.cpp/examples/retrieval/CMakeLists.txt +5 -0
  105. package/src/llama.cpp/examples/retrieval/retrieval.cpp +350 -0
  106. package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +5 -0
  107. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +246 -0
  108. package/src/llama.cpp/examples/server/CMakeLists.txt +40 -0
  109. package/src/llama.cpp/examples/server/bench/requirements.txt +2 -0
  110. package/src/llama.cpp/examples/server/httplib.h +9465 -0
  111. package/src/llama.cpp/examples/server/server.cpp +3826 -0
  112. package/src/llama.cpp/examples/server/tests/requirements.txt +6 -0
  113. package/src/llama.cpp/examples/server/utils.hpp +653 -0
  114. package/src/llama.cpp/examples/simple/CMakeLists.txt +5 -0
  115. package/src/llama.cpp/examples/simple/simple.cpp +183 -0
  116. package/src/llama.cpp/examples/speculative/CMakeLists.txt +5 -0
  117. package/src/llama.cpp/examples/speculative/speculative.cpp +614 -0
  118. package/src/llama.cpp/examples/sycl/CMakeLists.txt +9 -0
  119. package/src/llama.cpp/examples/sycl/ls-sycl-device.cpp +13 -0
  120. package/src/llama.cpp/examples/tokenize/CMakeLists.txt +5 -0
  121. package/src/llama.cpp/examples/tokenize/tokenize.cpp +42 -0
  122. package/src/llama.cpp/examples/train-text-from-scratch/CMakeLists.txt +5 -0
  123. package/src/llama.cpp/examples/train-text-from-scratch/train-text-from-scratch.cpp +1252 -0
  124. package/src/llama.cpp/ggml-alloc.c +985 -0
  125. package/src/llama.cpp/ggml-alloc.h +76 -0
  126. package/src/llama.cpp/ggml-backend-impl.h +141 -0
  127. package/src/llama.cpp/ggml-backend.c +2099 -0
  128. package/src/llama.cpp/ggml-backend.h +233 -0
  129. package/src/llama.cpp/ggml-common.h +1853 -0
  130. package/src/llama.cpp/ggml-cuda.h +43 -0
  131. package/src/llama.cpp/ggml-impl.h +265 -0
  132. package/src/llama.cpp/ggml-kompute.cpp +2006 -0
  133. package/src/llama.cpp/ggml-kompute.h +46 -0
  134. package/src/llama.cpp/ggml-metal.h +66 -0
  135. package/src/llama.cpp/ggml-mpi.c +216 -0
  136. package/src/llama.cpp/ggml-mpi.h +39 -0
  137. package/src/llama.cpp/ggml-opencl.cpp +2301 -0
  138. package/src/llama.cpp/ggml-opencl.h +36 -0
  139. package/src/llama.cpp/ggml-quants.c +12678 -0
  140. package/src/llama.cpp/ggml-quants.h +133 -0
  141. package/src/llama.cpp/ggml-sycl.cpp +17882 -0
  142. package/src/llama.cpp/ggml-sycl.h +49 -0
  143. package/src/llama.cpp/ggml-vulkan-shaders.hpp +69849 -0
  144. package/src/llama.cpp/ggml-vulkan.cpp +6442 -0
  145. package/src/llama.cpp/ggml-vulkan.h +29 -0
  146. package/src/llama.cpp/ggml.c +21819 -0
  147. package/src/llama.cpp/ggml.h +2403 -0
  148. package/src/llama.cpp/llama.cpp +17468 -0
  149. package/src/llama.cpp/llama.h +1117 -0
  150. package/src/llama.cpp/pocs/CMakeLists.txt +12 -0
  151. package/src/llama.cpp/pocs/vdot/CMakeLists.txt +9 -0
  152. package/src/llama.cpp/pocs/vdot/q8dot.cpp +172 -0
  153. package/src/llama.cpp/pocs/vdot/vdot.cpp +310 -0
  154. package/src/llama.cpp/prompts/LLM-questions.txt +49 -0
  155. package/src/llama.cpp/prompts/alpaca.txt +1 -0
  156. package/src/llama.cpp/prompts/assistant.txt +31 -0
  157. package/src/llama.cpp/prompts/chat-with-baichuan.txt +4 -0
  158. package/src/llama.cpp/prompts/chat-with-bob.txt +7 -0
  159. package/src/llama.cpp/prompts/chat-with-qwen.txt +1 -0
  160. package/src/llama.cpp/prompts/chat-with-vicuna-v0.txt +7 -0
  161. package/src/llama.cpp/prompts/chat-with-vicuna-v1.txt +7 -0
  162. package/src/llama.cpp/prompts/chat.txt +28 -0
  163. package/src/llama.cpp/prompts/dan-modified.txt +1 -0
  164. package/src/llama.cpp/prompts/dan.txt +1 -0
  165. package/src/llama.cpp/prompts/mnemonics.txt +93 -0
  166. package/src/llama.cpp/prompts/parallel-questions.txt +43 -0
  167. package/src/llama.cpp/prompts/reason-act.txt +18 -0
  168. package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt +3 -0
  169. package/src/llama.cpp/requirements/requirements-convert-llama-ggml-to-gguf.txt +1 -0
  170. package/src/llama.cpp/requirements/requirements-convert-lora-to-ggml.txt +2 -0
  171. package/src/llama.cpp/requirements/requirements-convert-persimmon-to-gguf.txt +2 -0
  172. package/src/llama.cpp/requirements/requirements-convert.txt +5 -0
  173. package/src/llama.cpp/requirements.txt +12 -0
  174. package/src/llama.cpp/scripts/gen-build-info-cpp.cmake +24 -0
  175. package/src/llama.cpp/scripts/xxd.cmake +16 -0
  176. package/src/llama.cpp/sgemm.cpp +999 -0
  177. package/src/llama.cpp/sgemm.h +12 -0
  178. package/src/llama.cpp/tests/CMakeLists.txt +78 -0
  179. package/src/llama.cpp/tests/get-model.cpp +21 -0
  180. package/src/llama.cpp/tests/get-model.h +2 -0
  181. package/src/llama.cpp/tests/test-autorelease.cpp +24 -0
  182. package/src/llama.cpp/tests/test-backend-ops.cpp +2266 -0
  183. package/src/llama.cpp/tests/test-c.c +7 -0
  184. package/src/llama.cpp/tests/test-chat-template.cpp +107 -0
  185. package/src/llama.cpp/tests/test-double-float.cpp +57 -0
  186. package/src/llama.cpp/tests/test-grad0.cpp +1606 -0
  187. package/src/llama.cpp/tests/test-grammar-integration.cpp +243 -0
  188. package/src/llama.cpp/tests/test-grammar-parser.cpp +250 -0
  189. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +899 -0
  190. package/src/llama.cpp/tests/test-llama-grammar.cpp +402 -0
  191. package/src/llama.cpp/tests/test-model-load-cancel.cpp +27 -0
  192. package/src/llama.cpp/tests/test-opt.cpp +181 -0
  193. package/src/llama.cpp/tests/test-quantize-fns.cpp +185 -0
  194. package/src/llama.cpp/tests/test-quantize-perf.cpp +363 -0
  195. package/src/llama.cpp/tests/test-rope.cpp +221 -0
  196. package/src/llama.cpp/tests/test-sampling.cpp +301 -0
  197. package/src/llama.cpp/tests/test-tokenizer-0-falcon.cpp +187 -0
  198. package/src/llama.cpp/tests/test-tokenizer-0-llama.cpp +190 -0
  199. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +123 -0
  200. package/src/llama.cpp/tests/test-tokenizer-1-llama.cpp +111 -0
  201. package/src/llama.cpp/unicode-data.cpp +1651 -0
  202. package/src/llama.cpp/unicode-data.h +16 -0
  203. package/src/llama.cpp/unicode.cpp +277 -0
  204. package/src/llama.cpp/unicode.h +28 -0
@@ -0,0 +1,2403 @@
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 ggml_init_params params = {
36
+ // .mem_size = 16*1024*1024,
37
+ // .mem_buffer = NULL,
38
+ // };
39
+ //
40
+ // // memory allocation happens here
41
+ // struct ggml_context * ctx = ggml_init(params);
42
+ //
43
+ // struct ggml_tensor * x = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
44
+ //
45
+ // ggml_set_param(ctx, x); // x is an input variable
46
+ //
47
+ // struct ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
48
+ // struct ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
49
+ // struct ggml_tensor * x2 = ggml_mul(ctx, x, x);
50
+ // struct ggml_tensor * f = ggml_add(ctx, 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 ggml_cgraph * gf = ggml_new_graph(ctx);
62
+ // ggml_build_forward_expand(gf, f);
63
+ //
64
+ // // set the input variable and parameter values
65
+ // ggml_set_f32(x, 2.0f);
66
+ // ggml_set_f32(a, 3.0f);
67
+ // ggml_set_f32(b, 4.0f);
68
+ //
69
+ // ggml_graph_compute_with_ctx(ctx, &gf, n_threads);
70
+ //
71
+ // printf("f = %f\n", ggml_get_f32_1d(f, 0));
72
+ //
73
+ // ...
74
+ // }
75
+ //
76
+ // The actual computation is performed in the ggml_graph_compute() function.
77
+ //
78
+ // The ggml_new_tensor_...() functions create new tensors. They are allocated in the memory buffer provided to the
79
+ // 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 ggml_used_mem() function to find out how much memory was
82
+ // actually needed.
83
+ //
84
+ // The 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 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
+ // - ggml_permute()
100
+ // - ggml_conv_1d_1s()
101
+ // - 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 ggml_tensor)
113
+ //
114
+ // The tensors are stored in memory via the 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 ggml_tensor * c = 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 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 ggml_tensor * a = ggml_new_tensor_2d(ctx, 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 ggml_get_f32_1d() and ggml_set_f32_1d() that can be used.
149
+ //
150
+ // ## The matrix multiplication operator (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 GGML_SHARED
177
+ # if defined(_WIN32) && !defined(__MINGW32__)
178
+ # ifdef GGML_BUILD
179
+ # define GGML_API __declspec(dllexport)
180
+ # else
181
+ # define GGML_API __declspec(dllimport)
182
+ # endif
183
+ # else
184
+ # define GGML_API __attribute__ ((visibility ("default")))
185
+ # endif
186
+ #else
187
+ # define GGML_API
188
+ #endif
189
+
190
+ #ifdef GGML_MULTIPLATFORM
191
+ # if defined(_WIN32)
192
+ # define GGML_CALL
193
+ # else
194
+ # define GGML_CALL __attribute__((__ms_abi__))
195
+ # endif
196
+ #else
197
+ # define GGML_CALL
198
+ #endif
199
+
200
+ // TODO: support for clang
201
+ #ifdef __GNUC__
202
+ # define GGML_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
203
+ #elif defined(_MSC_VER)
204
+ # define GGML_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
205
+ #else
206
+ # define GGML_DEPRECATED(func, hint) func
207
+ #endif
208
+
209
+ #ifndef __GNUC__
210
+ # define GGML_ATTRIBUTE_FORMAT(...)
211
+ #elif defined(__MINGW32__)
212
+ # define GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
213
+ #else
214
+ # define GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
215
+ #endif
216
+
217
+ #include <stdbool.h>
218
+ #include <stddef.h>
219
+ #include <stdint.h>
220
+ #include <stdio.h>
221
+
222
+ #define GGML_FILE_MAGIC 0x67676d6c // "ggml"
223
+ #define GGML_FILE_VERSION 1
224
+
225
+ #define GGML_QNT_VERSION 2 // bump this on quantization format changes
226
+ #define GGML_QNT_VERSION_FACTOR 1000 // do not change this
227
+
228
+ #define GGML_MAX_DIMS 4
229
+ #define GGML_MAX_PARAMS 2048
230
+ #define GGML_MAX_CONTEXTS 64
231
+ #define GGML_MAX_SRC 10
232
+ #ifndef GGML_MAX_NAME
233
+ #define GGML_MAX_NAME 64
234
+ #endif
235
+ #define GGML_MAX_OP_PARAMS 64
236
+ #define GGML_DEFAULT_N_THREADS 4
237
+ #define GGML_DEFAULT_GRAPH_SIZE 2048
238
+ #if UINTPTR_MAX == 0xFFFFFFFF
239
+ #define GGML_MEM_ALIGN 4
240
+ #else
241
+ #define GGML_MEM_ALIGN 16
242
+ #endif
243
+
244
+ #define GGML_EXIT_SUCCESS 0
245
+ #define GGML_EXIT_ABORTED 1
246
+
247
+ #define GGUF_MAGIC "GGUF"
248
+
249
+ #define GGUF_VERSION 3
250
+
251
+ #define GGUF_DEFAULT_ALIGNMENT 32
252
+
253
+ #define GGML_UNUSED(x) (void)(x)
254
+
255
+ #define GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
256
+
257
+ #define GGML_ASSERT(x) \
258
+ do { \
259
+ if (!(x)) { \
260
+ fflush(stdout); \
261
+ fprintf(stderr, "GGML_ASSERT: %s:%d: %s\n", __FILE__, __LINE__, #x); \
262
+ ggml_print_backtrace(); \
263
+ abort(); \
264
+ } \
265
+ } while (0)
266
+
267
+ #ifndef NDEBUG
268
+ #define GGML_UNREACHABLE() GGML_ASSERT(!"statement should not be reached")
269
+ #elif defined(__GNUC__)
270
+ #define GGML_UNREACHABLE() __builtin_unreachable()
271
+ #elif defined(_MSC_VER)
272
+ #define GGML_UNREACHABLE() __assume(0)
273
+ #else
274
+ #define GGML_UNREACHABLE() ((void) 0)
275
+ #endif
276
+
277
+ // used to copy the number of elements and stride in bytes of tensors into local variables.
278
+ // main purpose is to reduce code duplication and improve readability.
279
+ //
280
+ // example:
281
+ //
282
+ // GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
283
+ // GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
284
+ //
285
+ #define GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
286
+ const type prefix##0 = (pointer)->array[0]; \
287
+ GGML_UNUSED(prefix##0);
288
+ #define GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
289
+ GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
290
+ const type prefix##1 = (pointer)->array[1]; \
291
+ GGML_UNUSED(prefix##1);
292
+ #define GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
293
+ GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
294
+ const type prefix##2 = (pointer)->array[2]; \
295
+ GGML_UNUSED(prefix##2);
296
+ #define GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
297
+ GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
298
+ const type prefix##3 = (pointer)->array[3]; \
299
+ GGML_UNUSED(prefix##3);
300
+
301
+ #define GGML_TENSOR_UNARY_OP_LOCALS \
302
+ GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
303
+ GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
304
+ GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
305
+ GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
306
+
307
+ #define GGML_TENSOR_BINARY_OP_LOCALS \
308
+ GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
309
+ GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
310
+ GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
311
+ GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
312
+ GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
313
+ GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
314
+
315
+ #ifdef __cplusplus
316
+ extern "C" {
317
+ #endif
318
+
319
+ enum ggml_status {
320
+ GGML_STATUS_ALLOC_FAILED = -2,
321
+ GGML_STATUS_FAILED = -1,
322
+ GGML_STATUS_SUCCESS = 0,
323
+ GGML_STATUS_ABORTED = 1,
324
+ };
325
+
326
+ // get ggml_status name string
327
+ GGML_API GGML_CALL const char * ggml_status_to_string(enum ggml_status status);
328
+
329
+ typedef uint16_t ggml_fp16_t;
330
+
331
+ // convert FP16 <-> FP32
332
+ GGML_API float ggml_fp16_to_fp32(ggml_fp16_t x);
333
+ GGML_API ggml_fp16_t ggml_fp32_to_fp16(float x);
334
+
335
+ GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t * x, float * y, int64_t n);
336
+ GGML_API void ggml_fp32_to_fp16_row(const float * x, ggml_fp16_t * y, int64_t n);
337
+
338
+ struct ggml_object;
339
+ struct ggml_context;
340
+
341
+ // NOTE: always add types at the end of the enum to keep backward compatibility
342
+ enum ggml_type {
343
+ GGML_TYPE_F32 = 0,
344
+ GGML_TYPE_F16 = 1,
345
+ GGML_TYPE_Q4_0 = 2,
346
+ GGML_TYPE_Q4_1 = 3,
347
+ // GGML_TYPE_Q4_2 = 4, support has been removed
348
+ // GGML_TYPE_Q4_3 = 5, support has been removed
349
+ GGML_TYPE_Q5_0 = 6,
350
+ GGML_TYPE_Q5_1 = 7,
351
+ GGML_TYPE_Q8_0 = 8,
352
+ GGML_TYPE_Q8_1 = 9,
353
+ GGML_TYPE_Q2_K = 10,
354
+ GGML_TYPE_Q3_K = 11,
355
+ GGML_TYPE_Q4_K = 12,
356
+ GGML_TYPE_Q5_K = 13,
357
+ GGML_TYPE_Q6_K = 14,
358
+ GGML_TYPE_Q8_K = 15,
359
+ GGML_TYPE_IQ2_XXS = 16,
360
+ GGML_TYPE_IQ2_XS = 17,
361
+ GGML_TYPE_IQ3_XXS = 18,
362
+ GGML_TYPE_IQ1_S = 19,
363
+ GGML_TYPE_IQ4_NL = 20,
364
+ GGML_TYPE_IQ3_S = 21,
365
+ GGML_TYPE_IQ2_S = 22,
366
+ GGML_TYPE_IQ4_XS = 23,
367
+ GGML_TYPE_I8 = 24,
368
+ GGML_TYPE_I16 = 25,
369
+ GGML_TYPE_I32 = 26,
370
+ GGML_TYPE_I64 = 27,
371
+ GGML_TYPE_F64 = 28,
372
+ GGML_TYPE_IQ1_M = 29,
373
+ GGML_TYPE_COUNT,
374
+ };
375
+
376
+ // precision
377
+ enum ggml_prec {
378
+ GGML_PREC_DEFAULT,
379
+ GGML_PREC_F32,
380
+ };
381
+
382
+ enum ggml_backend_type {
383
+ GGML_BACKEND_TYPE_CPU = 0,
384
+ GGML_BACKEND_TYPE_GPU = 10,
385
+ GGML_BACKEND_TYPE_GPU_SPLIT = 20,
386
+ };
387
+
388
+ // model file types
389
+ enum ggml_ftype {
390
+ GGML_FTYPE_UNKNOWN = -1,
391
+ GGML_FTYPE_ALL_F32 = 0,
392
+ GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
393
+ GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
394
+ GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
395
+ GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
396
+ GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
397
+ GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
398
+ GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
399
+ GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
400
+ GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
401
+ GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
402
+ GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
403
+ GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
404
+ GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors
405
+ GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors
406
+ GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors
407
+ GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors
408
+ GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors
409
+ GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors
410
+ GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors
411
+ GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors
412
+ GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors
413
+ };
414
+
415
+ // available tensor operations:
416
+ enum ggml_op {
417
+ GGML_OP_NONE = 0,
418
+
419
+ GGML_OP_DUP,
420
+ GGML_OP_ADD,
421
+ GGML_OP_ADD1,
422
+ GGML_OP_ACC,
423
+ GGML_OP_SUB,
424
+ GGML_OP_MUL,
425
+ GGML_OP_DIV,
426
+ GGML_OP_SQR,
427
+ GGML_OP_SQRT,
428
+ GGML_OP_LOG,
429
+ GGML_OP_SUM,
430
+ GGML_OP_SUM_ROWS,
431
+ GGML_OP_MEAN,
432
+ GGML_OP_ARGMAX,
433
+ GGML_OP_REPEAT,
434
+ GGML_OP_REPEAT_BACK,
435
+ GGML_OP_CONCAT,
436
+ GGML_OP_SILU_BACK,
437
+ GGML_OP_NORM, // normalize
438
+ GGML_OP_RMS_NORM,
439
+ GGML_OP_RMS_NORM_BACK,
440
+ GGML_OP_GROUP_NORM,
441
+
442
+ GGML_OP_MUL_MAT,
443
+ GGML_OP_MUL_MAT_ID,
444
+ GGML_OP_OUT_PROD,
445
+
446
+ GGML_OP_SCALE,
447
+ GGML_OP_SET,
448
+ GGML_OP_CPY,
449
+ GGML_OP_CONT,
450
+ GGML_OP_RESHAPE,
451
+ GGML_OP_VIEW,
452
+ GGML_OP_PERMUTE,
453
+ GGML_OP_TRANSPOSE,
454
+ GGML_OP_GET_ROWS,
455
+ GGML_OP_GET_ROWS_BACK,
456
+ GGML_OP_DIAG,
457
+ GGML_OP_DIAG_MASK_INF,
458
+ GGML_OP_DIAG_MASK_ZERO,
459
+ GGML_OP_SOFT_MAX,
460
+ GGML_OP_SOFT_MAX_BACK,
461
+ GGML_OP_ROPE,
462
+ GGML_OP_ROPE_BACK,
463
+ GGML_OP_ALIBI,
464
+ GGML_OP_CLAMP,
465
+ GGML_OP_CONV_TRANSPOSE_1D,
466
+ GGML_OP_IM2COL,
467
+ GGML_OP_CONV_TRANSPOSE_2D,
468
+ GGML_OP_POOL_1D,
469
+ GGML_OP_POOL_2D,
470
+ GGML_OP_UPSCALE, // nearest interpolate
471
+ GGML_OP_PAD,
472
+ GGML_OP_ARANGE,
473
+ GGML_OP_TIMESTEP_EMBEDDING,
474
+ GGML_OP_ARGSORT,
475
+ GGML_OP_LEAKY_RELU,
476
+
477
+ GGML_OP_FLASH_ATTN,
478
+ GGML_OP_FLASH_FF,
479
+ GGML_OP_FLASH_ATTN_BACK,
480
+ GGML_OP_SSM_CONV,
481
+ GGML_OP_SSM_SCAN,
482
+ GGML_OP_WIN_PART,
483
+ GGML_OP_WIN_UNPART,
484
+ GGML_OP_GET_REL_POS,
485
+ GGML_OP_ADD_REL_POS,
486
+
487
+ GGML_OP_UNARY,
488
+
489
+ GGML_OP_MAP_UNARY,
490
+ GGML_OP_MAP_BINARY,
491
+
492
+ GGML_OP_MAP_CUSTOM1_F32,
493
+ GGML_OP_MAP_CUSTOM2_F32,
494
+ GGML_OP_MAP_CUSTOM3_F32,
495
+
496
+ GGML_OP_MAP_CUSTOM1,
497
+ GGML_OP_MAP_CUSTOM2,
498
+ GGML_OP_MAP_CUSTOM3,
499
+
500
+ GGML_OP_CROSS_ENTROPY_LOSS,
501
+ GGML_OP_CROSS_ENTROPY_LOSS_BACK,
502
+
503
+ GGML_OP_COUNT,
504
+ };
505
+
506
+ enum ggml_unary_op {
507
+ GGML_UNARY_OP_ABS,
508
+ GGML_UNARY_OP_SGN,
509
+ GGML_UNARY_OP_NEG,
510
+ GGML_UNARY_OP_STEP,
511
+ GGML_UNARY_OP_TANH,
512
+ GGML_UNARY_OP_ELU,
513
+ GGML_UNARY_OP_RELU,
514
+ GGML_UNARY_OP_GELU,
515
+ GGML_UNARY_OP_GELU_QUICK,
516
+ GGML_UNARY_OP_SILU,
517
+ GGML_UNARY_OP_HARDSWISH,
518
+ GGML_UNARY_OP_HARDSIGMOID,
519
+
520
+ GGML_UNARY_OP_COUNT,
521
+ };
522
+
523
+ enum ggml_object_type {
524
+ GGML_OBJECT_TYPE_TENSOR,
525
+ GGML_OBJECT_TYPE_GRAPH,
526
+ GGML_OBJECT_TYPE_WORK_BUFFER
527
+ };
528
+
529
+ enum ggml_log_level {
530
+ GGML_LOG_LEVEL_ERROR = 2,
531
+ GGML_LOG_LEVEL_WARN = 3,
532
+ GGML_LOG_LEVEL_INFO = 4,
533
+ GGML_LOG_LEVEL_DEBUG = 5
534
+ };
535
+
536
+ enum ggml_tensor_flag {
537
+ GGML_TENSOR_FLAG_INPUT = 1,
538
+ GGML_TENSOR_FLAG_OUTPUT = 2,
539
+ GGML_TENSOR_FLAG_PARAM = 4,
540
+ };
541
+
542
+ // ggml object
543
+ struct ggml_object {
544
+ size_t offs;
545
+ size_t size;
546
+
547
+ struct ggml_object * next;
548
+
549
+ enum ggml_object_type type;
550
+
551
+ char padding[4];
552
+ };
553
+
554
+ static const size_t GGML_OBJECT_SIZE = sizeof(struct ggml_object);
555
+
556
+ // n-dimensional tensor
557
+ struct ggml_tensor {
558
+ enum ggml_type type;
559
+ enum ggml_backend_type backend;
560
+
561
+ struct ggml_backend_buffer * buffer;
562
+
563
+ int64_t ne[GGML_MAX_DIMS]; // number of elements
564
+ size_t nb[GGML_MAX_DIMS]; // stride in bytes:
565
+ // nb[0] = ggml_type_size(type)
566
+ // nb[1] = nb[0] * (ne[0] / ggml_blck_size(type)) + padding
567
+ // nb[i] = nb[i-1] * ne[i-1]
568
+
569
+ // compute data
570
+ enum ggml_op op;
571
+
572
+ // op params - allocated as int32_t for alignment
573
+ int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
574
+
575
+ int32_t flags;
576
+
577
+ struct ggml_tensor * grad;
578
+ struct ggml_tensor * src[GGML_MAX_SRC];
579
+
580
+ // performance
581
+ int perf_runs;
582
+ int64_t perf_cycles;
583
+ int64_t perf_time_us;
584
+
585
+ struct ggml_tensor * view_src;
586
+ size_t view_offs;
587
+
588
+ void * data;
589
+
590
+ char name[GGML_MAX_NAME];
591
+
592
+ void * extra; // extra things e.g. for ggml-cuda.cu
593
+
594
+ char padding[8];
595
+ };
596
+
597
+ static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor);
598
+
599
+ // Abort callback
600
+ // If not NULL, called before ggml computation
601
+ // If it returns true, the computation is aborted
602
+ typedef bool (*ggml_abort_callback)(void * data);
603
+
604
+ // the compute plan that needs to be prepared for ggml_graph_compute()
605
+ // since https://github.com/ggerganov/ggml/issues/287
606
+ struct ggml_cplan {
607
+ size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()`
608
+ uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()`
609
+
610
+ int n_threads;
611
+
612
+ // abort ggml_graph_compute when true
613
+ ggml_abort_callback abort_callback;
614
+ void * abort_callback_data;
615
+ };
616
+
617
+ enum ggml_cgraph_eval_order {
618
+ GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
619
+ GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
620
+ GGML_CGRAPH_EVAL_ORDER_COUNT
621
+ };
622
+
623
+ struct ggml_hash_set {
624
+ size_t size;
625
+ struct ggml_tensor ** keys;
626
+ };
627
+
628
+ // computation graph
629
+ struct ggml_cgraph {
630
+ int size;
631
+ int n_nodes;
632
+ int n_leafs;
633
+
634
+ struct ggml_tensor ** nodes;
635
+ struct ggml_tensor ** grads;
636
+ struct ggml_tensor ** leafs;
637
+
638
+ struct ggml_hash_set visited_hash_table;
639
+
640
+ enum ggml_cgraph_eval_order order;
641
+
642
+ // performance
643
+ int perf_runs;
644
+ int64_t perf_cycles;
645
+ int64_t perf_time_us;
646
+ };
647
+
648
+ // scratch buffer
649
+ struct ggml_scratch {
650
+ size_t offs;
651
+ size_t size;
652
+ void * data;
653
+ };
654
+
655
+ struct ggml_init_params {
656
+ // memory pool
657
+ size_t mem_size; // bytes
658
+ void * mem_buffer; // if NULL, memory will be allocated internally
659
+ bool no_alloc; // don't allocate memory for the tensor data
660
+ };
661
+
662
+
663
+ // compute types
664
+
665
+ // NOTE: the INIT or FINALIZE pass is not scheduled unless explicitly enabled.
666
+ // This behavior was changed since https://github.com/ggerganov/llama.cpp/pull/1995.
667
+ enum ggml_task_type {
668
+ GGML_TASK_TYPE_INIT = 0,
669
+ GGML_TASK_TYPE_COMPUTE,
670
+ GGML_TASK_TYPE_FINALIZE,
671
+ };
672
+
673
+ struct ggml_compute_params {
674
+ enum ggml_task_type type;
675
+
676
+ // ith = thread index, nth = number of threads
677
+ int ith, nth;
678
+
679
+ // work buffer for all threads
680
+ size_t wsize;
681
+ void * wdata;
682
+ };
683
+
684
+ // numa strategies
685
+ enum ggml_numa_strategy {
686
+ GGML_NUMA_STRATEGY_DISABLED = 0,
687
+ GGML_NUMA_STRATEGY_DISTRIBUTE = 1,
688
+ GGML_NUMA_STRATEGY_ISOLATE = 2,
689
+ GGML_NUMA_STRATEGY_NUMACTL = 3,
690
+ GGML_NUMA_STRATEGY_MIRROR = 4,
691
+ GGML_NUMA_STRATEGY_COUNT
692
+ };
693
+
694
+ //
695
+ // GUID
696
+ //
697
+
698
+ // GUID types
699
+ typedef uint8_t ggml_guid[16];
700
+ typedef ggml_guid * ggml_guid_t;
701
+
702
+ GGML_API bool ggml_guid_matches(ggml_guid_t guid_a, ggml_guid_t guid_b);
703
+
704
+ // misc
705
+
706
+ GGML_API void ggml_time_init(void); // call this once at the beginning of the program
707
+ GGML_API int64_t ggml_time_ms(void);
708
+ GGML_API int64_t ggml_time_us(void);
709
+ GGML_API int64_t ggml_cycles(void);
710
+ GGML_API int64_t ggml_cycles_per_ms(void);
711
+
712
+ GGML_API void ggml_print_backtrace(void);
713
+
714
+ // accepts a UTF-8 path, even on Windows
715
+ GGML_API FILE * ggml_fopen(const char * fname, const char * mode);
716
+
717
+ GGML_API void ggml_numa_init(enum ggml_numa_strategy numa); // call once for better performance on NUMA systems
718
+ GGML_API bool ggml_is_numa(void); // true if init detected that system has >1 NUMA node
719
+
720
+ GGML_API void ggml_print_object (const struct ggml_object * obj);
721
+ GGML_API void ggml_print_objects(const struct ggml_context * ctx);
722
+
723
+ GGML_API GGML_CALL int64_t ggml_nelements (const struct ggml_tensor * tensor);
724
+ GGML_API GGML_CALL int64_t ggml_nrows (const struct ggml_tensor * tensor);
725
+ GGML_API GGML_CALL size_t ggml_nbytes (const struct ggml_tensor * tensor);
726
+ GGML_API size_t ggml_nbytes_pad (const struct ggml_tensor * tensor); // same as ggml_nbytes() but padded to GGML_MEM_ALIGN
727
+
728
+ GGML_API GGML_CALL int ggml_blck_size(enum ggml_type type);
729
+ GGML_API GGML_CALL size_t ggml_type_size(enum ggml_type type); // size in bytes for all elements in a block
730
+ GGML_API GGML_CALL size_t ggml_row_size (enum ggml_type type, int64_t ne); // size in bytes for all elements in a row
731
+
732
+ GGML_DEPRECATED(
733
+ GGML_API double ggml_type_sizef(enum ggml_type type), // ggml_type_size()/ggml_blck_size() as float
734
+ "use ggml_row_size() instead");
735
+
736
+ GGML_API GGML_CALL const char * ggml_type_name(enum ggml_type type);
737
+ GGML_API GGML_CALL const char * ggml_op_name (enum ggml_op op);
738
+ GGML_API const char * ggml_op_symbol(enum ggml_op op);
739
+
740
+ GGML_API const char * ggml_unary_op_name(enum ggml_unary_op op);
741
+ GGML_API GGML_CALL const char * ggml_op_desc(const struct ggml_tensor * t); // unary or op name
742
+
743
+ GGML_API GGML_CALL size_t ggml_element_size(const struct ggml_tensor * tensor);
744
+
745
+ GGML_API GGML_CALL bool ggml_is_quantized(enum ggml_type type);
746
+
747
+ // TODO: temporary until model loading of ggml examples is refactored
748
+ GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
749
+
750
+ GGML_API GGML_CALL bool ggml_is_transposed(const struct ggml_tensor * tensor);
751
+ GGML_API GGML_CALL bool ggml_is_contiguous(const struct ggml_tensor * tensor);
752
+ GGML_API GGML_CALL bool ggml_is_permuted (const struct ggml_tensor * tensor);
753
+ GGML_API GGML_CALL bool ggml_is_empty (const struct ggml_tensor * tensor);
754
+ GGML_API bool ggml_is_scalar (const struct ggml_tensor * tensor);
755
+ GGML_API bool ggml_is_vector (const struct ggml_tensor * tensor);
756
+ GGML_API bool ggml_is_matrix (const struct ggml_tensor * tensor);
757
+ GGML_API bool ggml_is_3d (const struct ggml_tensor * tensor);
758
+ GGML_API int ggml_n_dims (const struct ggml_tensor * tensor); // returns 1 for scalars
759
+
760
+ GGML_API bool ggml_are_same_shape(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
761
+
762
+ // use this to compute the memory overhead of a tensor
763
+ GGML_API size_t ggml_tensor_overhead(void);
764
+
765
+ // main
766
+
767
+ GGML_API struct ggml_context * ggml_init(struct ggml_init_params params);
768
+ GGML_API void ggml_free(struct ggml_context * ctx);
769
+
770
+ GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
771
+
772
+ GGML_API size_t ggml_set_scratch (struct ggml_context * ctx, struct ggml_scratch scratch);
773
+ GGML_API bool ggml_get_no_alloc(struct ggml_context * ctx);
774
+ GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
775
+
776
+ GGML_API void * ggml_get_mem_buffer (const struct ggml_context * ctx);
777
+ GGML_API size_t ggml_get_mem_size (const struct ggml_context * ctx);
778
+ GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx);
779
+
780
+ GGML_API struct ggml_tensor * ggml_new_tensor(
781
+ struct ggml_context * ctx,
782
+ enum ggml_type type,
783
+ int n_dims,
784
+ const int64_t *ne);
785
+
786
+ GGML_API struct ggml_tensor * ggml_new_tensor_1d(
787
+ struct ggml_context * ctx,
788
+ enum ggml_type type,
789
+ int64_t ne0);
790
+
791
+ GGML_API struct ggml_tensor * ggml_new_tensor_2d(
792
+ struct ggml_context * ctx,
793
+ enum ggml_type type,
794
+ int64_t ne0,
795
+ int64_t ne1);
796
+
797
+ GGML_API struct ggml_tensor * ggml_new_tensor_3d(
798
+ struct ggml_context * ctx,
799
+ enum ggml_type type,
800
+ int64_t ne0,
801
+ int64_t ne1,
802
+ int64_t ne2);
803
+
804
+ GGML_API struct ggml_tensor * ggml_new_tensor_4d(
805
+ struct ggml_context * ctx,
806
+ enum ggml_type type,
807
+ int64_t ne0,
808
+ int64_t ne1,
809
+ int64_t ne2,
810
+ int64_t ne3);
811
+
812
+ GGML_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value);
813
+ GGML_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value);
814
+
815
+ GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src);
816
+ GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, struct ggml_tensor * src);
817
+
818
+ // Context tensor enumeration and lookup
819
+ GGML_API struct ggml_tensor * ggml_get_first_tensor(const struct ggml_context * ctx);
820
+ GGML_API struct ggml_tensor * ggml_get_next_tensor (const struct ggml_context * ctx, struct ggml_tensor * tensor);
821
+ GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name);
822
+
823
+ GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor);
824
+ GGML_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value);
825
+ GGML_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value);
826
+
827
+ // Converts a flat index into coordinates
828
+ GGML_API void ggml_unravel_index(const struct ggml_tensor * tensor, int64_t i, int64_t * i0, int64_t * i1, int64_t * i2, int64_t * i3);
829
+
830
+ GGML_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i);
831
+ GGML_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value);
832
+
833
+ GGML_API int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
834
+ GGML_API void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value);
835
+
836
+ GGML_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i);
837
+ GGML_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value);
838
+
839
+ GGML_API float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
840
+ GGML_API void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value);
841
+
842
+ GGML_API void * ggml_get_data (const struct ggml_tensor * tensor);
843
+ GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor);
844
+
845
+ GGML_API GGML_CALL enum ggml_unary_op ggml_get_unary_op(const struct ggml_tensor * tensor);
846
+
847
+ GGML_API const char * ggml_get_name (const struct ggml_tensor * tensor);
848
+ GGML_API struct ggml_tensor * ggml_set_name ( struct ggml_tensor * tensor, const char * name);
849
+ GGML_ATTRIBUTE_FORMAT(2, 3)
850
+ GGML_API struct ggml_tensor * ggml_format_name( struct ggml_tensor * tensor, const char * fmt, ...);
851
+
852
+ //
853
+ // operations on tensors with backpropagation
854
+ //
855
+
856
+ GGML_API struct ggml_tensor * ggml_dup(
857
+ struct ggml_context * ctx,
858
+ struct ggml_tensor * a);
859
+
860
+ // in-place, returns view(a)
861
+ GGML_API struct ggml_tensor * ggml_dup_inplace(
862
+ struct ggml_context * ctx,
863
+ struct ggml_tensor * a);
864
+
865
+ GGML_API struct ggml_tensor * ggml_add(
866
+ struct ggml_context * ctx,
867
+ struct ggml_tensor * a,
868
+ struct ggml_tensor * b);
869
+
870
+ GGML_API struct ggml_tensor * ggml_add_inplace(
871
+ struct ggml_context * ctx,
872
+ struct ggml_tensor * a,
873
+ struct ggml_tensor * b);
874
+
875
+ GGML_API struct ggml_tensor * ggml_add_cast(
876
+ struct ggml_context * ctx,
877
+ struct ggml_tensor * a,
878
+ struct ggml_tensor * b,
879
+ enum ggml_type type);
880
+
881
+ GGML_API struct ggml_tensor * ggml_add1(
882
+ struct ggml_context * ctx,
883
+ struct ggml_tensor * a,
884
+ struct ggml_tensor * b);
885
+
886
+ GGML_API struct ggml_tensor * ggml_add1_inplace(
887
+ struct ggml_context * ctx,
888
+ struct ggml_tensor * a,
889
+ struct ggml_tensor * b);
890
+
891
+ // dst = a
892
+ // view(dst, nb1, nb2, nb3, offset) += b
893
+ // return dst
894
+ GGML_API struct ggml_tensor * ggml_acc(
895
+ struct ggml_context * ctx,
896
+ struct ggml_tensor * a,
897
+ struct ggml_tensor * b,
898
+ size_t nb1,
899
+ size_t nb2,
900
+ size_t nb3,
901
+ size_t offset);
902
+
903
+ GGML_API struct ggml_tensor * ggml_acc_inplace(
904
+ struct ggml_context * ctx,
905
+ struct ggml_tensor * a,
906
+ struct ggml_tensor * b,
907
+ size_t nb1,
908
+ size_t nb2,
909
+ size_t nb3,
910
+ size_t offset);
911
+
912
+ GGML_API struct ggml_tensor * ggml_sub(
913
+ struct ggml_context * ctx,
914
+ struct ggml_tensor * a,
915
+ struct ggml_tensor * b);
916
+
917
+ GGML_API struct ggml_tensor * ggml_sub_inplace(
918
+ struct ggml_context * ctx,
919
+ struct ggml_tensor * a,
920
+ struct ggml_tensor * b);
921
+
922
+ GGML_API struct ggml_tensor * ggml_mul(
923
+ struct ggml_context * ctx,
924
+ struct ggml_tensor * a,
925
+ struct ggml_tensor * b);
926
+
927
+ GGML_API struct ggml_tensor * ggml_mul_inplace(
928
+ struct ggml_context * ctx,
929
+ struct ggml_tensor * a,
930
+ struct ggml_tensor * b);
931
+
932
+ GGML_API struct ggml_tensor * ggml_div(
933
+ struct ggml_context * ctx,
934
+ struct ggml_tensor * a,
935
+ struct ggml_tensor * b);
936
+
937
+ GGML_API struct ggml_tensor * ggml_div_inplace(
938
+ struct ggml_context * ctx,
939
+ struct ggml_tensor * a,
940
+ struct ggml_tensor * b);
941
+
942
+ GGML_API struct ggml_tensor * ggml_sqr(
943
+ struct ggml_context * ctx,
944
+ struct ggml_tensor * a);
945
+
946
+ GGML_API struct ggml_tensor * ggml_sqr_inplace(
947
+ struct ggml_context * ctx,
948
+ struct ggml_tensor * a);
949
+
950
+ GGML_API struct ggml_tensor * ggml_sqrt(
951
+ struct ggml_context * ctx,
952
+ struct ggml_tensor * a);
953
+
954
+ GGML_API struct ggml_tensor * ggml_sqrt_inplace(
955
+ struct ggml_context * ctx,
956
+ struct ggml_tensor * a);
957
+
958
+ GGML_API struct ggml_tensor * ggml_log(
959
+ struct ggml_context * ctx,
960
+ struct ggml_tensor * a);
961
+
962
+ GGML_API struct ggml_tensor * ggml_log_inplace(
963
+ struct ggml_context * ctx,
964
+ struct ggml_tensor * a);
965
+
966
+ // return scalar
967
+ GGML_API struct ggml_tensor * ggml_sum(
968
+ struct ggml_context * ctx,
969
+ struct ggml_tensor * a);
970
+
971
+ // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
972
+ GGML_API struct ggml_tensor * ggml_sum_rows(
973
+ struct ggml_context * ctx,
974
+ struct ggml_tensor * a);
975
+
976
+ // mean along rows
977
+ GGML_API struct ggml_tensor * ggml_mean(
978
+ struct ggml_context * ctx,
979
+ struct ggml_tensor * a);
980
+
981
+ // argmax along rows
982
+ GGML_API struct ggml_tensor * ggml_argmax(
983
+ struct ggml_context * ctx,
984
+ struct ggml_tensor * a);
985
+
986
+ // if a is the same shape as b, and a is not parameter, return a
987
+ // otherwise, return a new tensor: repeat(a) to fit in b
988
+ GGML_API struct ggml_tensor * ggml_repeat(
989
+ struct ggml_context * ctx,
990
+ struct ggml_tensor * a,
991
+ struct ggml_tensor * b);
992
+
993
+ // sums repetitions in a into shape of b
994
+ GGML_API struct ggml_tensor * ggml_repeat_back(
995
+ struct ggml_context * ctx,
996
+ struct ggml_tensor * a,
997
+ struct ggml_tensor * b);
998
+
999
+ // concat a and b on dim 2
1000
+ // used in stable-diffusion
1001
+ GGML_API struct ggml_tensor * ggml_concat(
1002
+ struct ggml_context * ctx,
1003
+ struct ggml_tensor * a,
1004
+ struct ggml_tensor * b);
1005
+
1006
+ GGML_API struct ggml_tensor * ggml_abs(
1007
+ struct ggml_context * ctx,
1008
+ struct ggml_tensor * a);
1009
+
1010
+ GGML_API struct ggml_tensor * ggml_abs_inplace(
1011
+ struct ggml_context * ctx,
1012
+ struct ggml_tensor * a);
1013
+
1014
+ GGML_API struct ggml_tensor * ggml_sgn(
1015
+ struct ggml_context * ctx,
1016
+ struct ggml_tensor * a);
1017
+
1018
+ GGML_API struct ggml_tensor * ggml_sgn_inplace(
1019
+ struct ggml_context * ctx,
1020
+ struct ggml_tensor * a);
1021
+
1022
+ GGML_API struct ggml_tensor * ggml_neg(
1023
+ struct ggml_context * ctx,
1024
+ struct ggml_tensor * a);
1025
+
1026
+ GGML_API struct ggml_tensor * ggml_neg_inplace(
1027
+ struct ggml_context * ctx,
1028
+ struct ggml_tensor * a);
1029
+
1030
+ GGML_API struct ggml_tensor * ggml_step(
1031
+ struct ggml_context * ctx,
1032
+ struct ggml_tensor * a);
1033
+
1034
+ GGML_API struct ggml_tensor * ggml_step_inplace(
1035
+ struct ggml_context * ctx,
1036
+ struct ggml_tensor * a);
1037
+
1038
+ GGML_API struct ggml_tensor * ggml_tanh(
1039
+ struct ggml_context * ctx,
1040
+ struct ggml_tensor * a);
1041
+
1042
+ GGML_API struct ggml_tensor * ggml_tanh_inplace(
1043
+ struct ggml_context * ctx,
1044
+ struct ggml_tensor * a);
1045
+
1046
+ GGML_API struct ggml_tensor * ggml_elu(
1047
+ struct ggml_context * ctx,
1048
+ struct ggml_tensor * a);
1049
+
1050
+ GGML_API struct ggml_tensor * ggml_elu_inplace(
1051
+ struct ggml_context * ctx,
1052
+ struct ggml_tensor * a);
1053
+
1054
+ GGML_API struct ggml_tensor * ggml_relu(
1055
+ struct ggml_context * ctx,
1056
+ struct ggml_tensor * a);
1057
+
1058
+ GGML_API struct ggml_tensor * ggml_leaky_relu(
1059
+ struct ggml_context * ctx,
1060
+ struct ggml_tensor * a, float negative_slope, bool inplace);
1061
+
1062
+ GGML_API struct ggml_tensor * ggml_relu_inplace(
1063
+ struct ggml_context * ctx,
1064
+ struct ggml_tensor * a);
1065
+
1066
+ GGML_API struct ggml_tensor * ggml_gelu(
1067
+ struct ggml_context * ctx,
1068
+ struct ggml_tensor * a);
1069
+
1070
+ GGML_API struct ggml_tensor * ggml_gelu_inplace(
1071
+ struct ggml_context * ctx,
1072
+ struct ggml_tensor * a);
1073
+
1074
+ GGML_API struct ggml_tensor * ggml_gelu_quick(
1075
+ struct ggml_context * ctx,
1076
+ struct ggml_tensor * a);
1077
+
1078
+ GGML_API struct ggml_tensor * ggml_gelu_quick_inplace(
1079
+ struct ggml_context * ctx,
1080
+ struct ggml_tensor * a);
1081
+
1082
+ GGML_API struct ggml_tensor * ggml_silu(
1083
+ struct ggml_context * ctx,
1084
+ struct ggml_tensor * a);
1085
+
1086
+ GGML_API struct ggml_tensor * ggml_silu_inplace(
1087
+ struct ggml_context * ctx,
1088
+ struct ggml_tensor * a);
1089
+
1090
+ // a - x
1091
+ // b - dy
1092
+ GGML_API struct ggml_tensor * ggml_silu_back(
1093
+ struct ggml_context * ctx,
1094
+ struct ggml_tensor * a,
1095
+ struct ggml_tensor * b);
1096
+
1097
+ // hardswish(x) = x * relu6(x + 3) / 6
1098
+ GGML_API struct ggml_tensor * ggml_hardswish(
1099
+ struct ggml_context * ctx,
1100
+ struct ggml_tensor * a);
1101
+
1102
+ // hardsigmoid(x) = relu6(x + 3) / 6
1103
+ GGML_API struct ggml_tensor * ggml_hardsigmoid(
1104
+ struct ggml_context * ctx,
1105
+ struct ggml_tensor * a);
1106
+
1107
+ // normalize along rows
1108
+ GGML_API struct ggml_tensor * ggml_norm(
1109
+ struct ggml_context * ctx,
1110
+ struct ggml_tensor * a,
1111
+ float eps);
1112
+
1113
+ GGML_API struct ggml_tensor * ggml_norm_inplace(
1114
+ struct ggml_context * ctx,
1115
+ struct ggml_tensor * a,
1116
+ float eps);
1117
+
1118
+ GGML_API struct ggml_tensor * ggml_rms_norm(
1119
+ struct ggml_context * ctx,
1120
+ struct ggml_tensor * a,
1121
+ float eps);
1122
+
1123
+ GGML_API struct ggml_tensor * ggml_rms_norm_inplace(
1124
+ struct ggml_context * ctx,
1125
+ struct ggml_tensor * a,
1126
+ float eps);
1127
+
1128
+ // group normalize along ne0*ne1*n_groups
1129
+ // used in stable-diffusion
1130
+ // TODO: eps is hardcoded to 1e-6 for now
1131
+ GGML_API struct ggml_tensor * ggml_group_norm(
1132
+ struct ggml_context * ctx,
1133
+ struct ggml_tensor * a,
1134
+ int n_groups);
1135
+
1136
+ GGML_API struct ggml_tensor * ggml_group_norm_inplace(
1137
+ struct ggml_context * ctx,
1138
+ struct ggml_tensor * a,
1139
+ int n_groups);
1140
+
1141
+ // a - x
1142
+ // b - dy
1143
+ GGML_API struct ggml_tensor * ggml_rms_norm_back(
1144
+ struct ggml_context * ctx,
1145
+ struct ggml_tensor * a,
1146
+ struct ggml_tensor * b,
1147
+ float eps);
1148
+
1149
+ // A: k columns, n rows => [ne03, ne02, n, k]
1150
+ // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
1151
+ // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
1152
+ GGML_API struct ggml_tensor * ggml_mul_mat(
1153
+ struct ggml_context * ctx,
1154
+ struct ggml_tensor * a,
1155
+ struct ggml_tensor * b);
1156
+
1157
+ // change the precision of a matrix multiplication
1158
+ // set to GGML_PREC_F32 for higher precision (useful for phi-2)
1159
+ GGML_API void ggml_mul_mat_set_prec(
1160
+ struct ggml_tensor * a,
1161
+ enum ggml_prec prec);
1162
+
1163
+ // indirect matrix multiplication
1164
+ GGML_API struct ggml_tensor * ggml_mul_mat_id(
1165
+ struct ggml_context * ctx,
1166
+ struct ggml_tensor * as,
1167
+ struct ggml_tensor * b,
1168
+ struct ggml_tensor * ids);
1169
+
1170
+ // A: m columns, n rows,
1171
+ // B: p columns, n rows,
1172
+ // result is m columns, p rows
1173
+ GGML_API struct ggml_tensor * ggml_out_prod(
1174
+ struct ggml_context * ctx,
1175
+ struct ggml_tensor * a,
1176
+ struct ggml_tensor * b);
1177
+
1178
+ //
1179
+ // operations on tensors without backpropagation
1180
+ //
1181
+
1182
+ GGML_API struct ggml_tensor * ggml_scale(
1183
+ struct ggml_context * ctx,
1184
+ struct ggml_tensor * a,
1185
+ float s);
1186
+
1187
+ // in-place, returns view(a)
1188
+ GGML_API struct ggml_tensor * ggml_scale_inplace(
1189
+ struct ggml_context * ctx,
1190
+ struct ggml_tensor * a,
1191
+ float s);
1192
+
1193
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1194
+ GGML_API struct ggml_tensor * ggml_set(
1195
+ struct ggml_context * ctx,
1196
+ struct ggml_tensor * a,
1197
+ struct ggml_tensor * b,
1198
+ size_t nb1,
1199
+ size_t nb2,
1200
+ size_t nb3,
1201
+ size_t offset);
1202
+
1203
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1204
+ GGML_API struct ggml_tensor * ggml_set_inplace(
1205
+ struct ggml_context * ctx,
1206
+ struct ggml_tensor * a,
1207
+ struct ggml_tensor * b,
1208
+ size_t nb1,
1209
+ size_t nb2,
1210
+ size_t nb3,
1211
+ size_t offset);
1212
+
1213
+ GGML_API struct ggml_tensor * ggml_set_1d(
1214
+ struct ggml_context * ctx,
1215
+ struct ggml_tensor * a,
1216
+ struct ggml_tensor * b,
1217
+ size_t offset);
1218
+
1219
+ GGML_API struct ggml_tensor * ggml_set_1d_inplace(
1220
+ struct ggml_context * ctx,
1221
+ struct ggml_tensor * a,
1222
+ struct ggml_tensor * b,
1223
+ size_t offset);
1224
+
1225
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1226
+ GGML_API struct ggml_tensor * ggml_set_2d(
1227
+ struct ggml_context * ctx,
1228
+ struct ggml_tensor * a,
1229
+ struct ggml_tensor * b,
1230
+ size_t nb1,
1231
+ size_t offset);
1232
+
1233
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1234
+ GGML_API struct ggml_tensor * ggml_set_2d_inplace(
1235
+ struct ggml_context * ctx,
1236
+ struct ggml_tensor * a,
1237
+ struct ggml_tensor * b,
1238
+ size_t nb1,
1239
+ size_t offset);
1240
+
1241
+ // a -> b, return view(b)
1242
+ GGML_API struct ggml_tensor * ggml_cpy(
1243
+ struct ggml_context * ctx,
1244
+ struct ggml_tensor * a,
1245
+ struct ggml_tensor * b);
1246
+
1247
+ GGML_API struct ggml_tensor * ggml_cast(
1248
+ struct ggml_context * ctx,
1249
+ struct ggml_tensor * a,
1250
+ enum ggml_type type);
1251
+
1252
+ // make contiguous
1253
+ GGML_API struct ggml_tensor * ggml_cont(
1254
+ struct ggml_context * ctx,
1255
+ struct ggml_tensor * a);
1256
+
1257
+ // make contiguous, with new shape
1258
+ GGML_API struct ggml_tensor * ggml_cont_1d(
1259
+ struct ggml_context * ctx,
1260
+ struct ggml_tensor * a,
1261
+ int64_t ne0);
1262
+
1263
+ GGML_API struct ggml_tensor * ggml_cont_2d(
1264
+ struct ggml_context * ctx,
1265
+ struct ggml_tensor * a,
1266
+ int64_t ne0,
1267
+ int64_t ne1);
1268
+
1269
+ GGML_API struct ggml_tensor * ggml_cont_3d(
1270
+ struct ggml_context * ctx,
1271
+ struct ggml_tensor * a,
1272
+ int64_t ne0,
1273
+ int64_t ne1,
1274
+ int64_t ne2);
1275
+
1276
+ GGML_API struct ggml_tensor * ggml_cont_4d(
1277
+ struct ggml_context * ctx,
1278
+ struct ggml_tensor * a,
1279
+ int64_t ne0,
1280
+ int64_t ne1,
1281
+ int64_t ne2,
1282
+ int64_t ne3);
1283
+
1284
+ // return view(a), b specifies the new shape
1285
+ // TODO: when we start computing gradient, make a copy instead of view
1286
+ GGML_API struct ggml_tensor * ggml_reshape(
1287
+ struct ggml_context * ctx,
1288
+ struct ggml_tensor * a,
1289
+ struct ggml_tensor * b);
1290
+
1291
+ // return view(a)
1292
+ // TODO: when we start computing gradient, make a copy instead of view
1293
+ GGML_API struct ggml_tensor * ggml_reshape_1d(
1294
+ struct ggml_context * ctx,
1295
+ struct ggml_tensor * a,
1296
+ int64_t ne0);
1297
+
1298
+ GGML_API struct ggml_tensor * ggml_reshape_2d(
1299
+ struct ggml_context * ctx,
1300
+ struct ggml_tensor * a,
1301
+ int64_t ne0,
1302
+ int64_t ne1);
1303
+
1304
+ // return view(a)
1305
+ // TODO: when we start computing gradient, make a copy instead of view
1306
+ GGML_API struct ggml_tensor * ggml_reshape_3d(
1307
+ struct ggml_context * ctx,
1308
+ struct ggml_tensor * a,
1309
+ int64_t ne0,
1310
+ int64_t ne1,
1311
+ int64_t ne2);
1312
+
1313
+ GGML_API struct ggml_tensor * ggml_reshape_4d(
1314
+ struct ggml_context * ctx,
1315
+ struct ggml_tensor * a,
1316
+ int64_t ne0,
1317
+ int64_t ne1,
1318
+ int64_t ne2,
1319
+ int64_t ne3);
1320
+
1321
+ // offset in bytes
1322
+ GGML_API struct ggml_tensor * ggml_view_1d(
1323
+ struct ggml_context * ctx,
1324
+ struct ggml_tensor * a,
1325
+ int64_t ne0,
1326
+ size_t offset);
1327
+
1328
+ GGML_API struct ggml_tensor * ggml_view_2d(
1329
+ struct ggml_context * ctx,
1330
+ struct ggml_tensor * a,
1331
+ int64_t ne0,
1332
+ int64_t ne1,
1333
+ size_t nb1, // row stride in bytes
1334
+ size_t offset);
1335
+
1336
+ GGML_API struct ggml_tensor * ggml_view_3d(
1337
+ struct ggml_context * ctx,
1338
+ struct ggml_tensor * a,
1339
+ int64_t ne0,
1340
+ int64_t ne1,
1341
+ int64_t ne2,
1342
+ size_t nb1, // row stride in bytes
1343
+ size_t nb2, // slice stride in bytes
1344
+ size_t offset);
1345
+
1346
+ GGML_API struct ggml_tensor * ggml_view_4d(
1347
+ struct ggml_context * ctx,
1348
+ struct ggml_tensor * a,
1349
+ int64_t ne0,
1350
+ int64_t ne1,
1351
+ int64_t ne2,
1352
+ int64_t ne3,
1353
+ size_t nb1, // row stride in bytes
1354
+ size_t nb2, // slice stride in bytes
1355
+ size_t nb3,
1356
+ size_t offset);
1357
+
1358
+ GGML_API struct ggml_tensor * ggml_permute(
1359
+ struct ggml_context * ctx,
1360
+ struct ggml_tensor * a,
1361
+ int axis0,
1362
+ int axis1,
1363
+ int axis2,
1364
+ int axis3);
1365
+
1366
+ // alias for ggml_permute(ctx, a, 1, 0, 2, 3)
1367
+ GGML_API struct ggml_tensor * ggml_transpose(
1368
+ struct ggml_context * ctx,
1369
+ struct ggml_tensor * a);
1370
+
1371
+ // supports 3D: a->ne[2] == b->ne[1]
1372
+ GGML_API struct ggml_tensor * ggml_get_rows(
1373
+ struct ggml_context * ctx,
1374
+ struct ggml_tensor * a,
1375
+ struct ggml_tensor * b);
1376
+
1377
+ GGML_API struct ggml_tensor * ggml_get_rows_back(
1378
+ struct ggml_context * ctx,
1379
+ struct ggml_tensor * a,
1380
+ struct ggml_tensor * b,
1381
+ struct ggml_tensor * c);
1382
+
1383
+ GGML_API struct ggml_tensor * ggml_diag(
1384
+ struct ggml_context * ctx,
1385
+ struct ggml_tensor * a);
1386
+
1387
+ // set elements above the diagonal to -INF
1388
+ GGML_API struct ggml_tensor * ggml_diag_mask_inf(
1389
+ struct ggml_context * ctx,
1390
+ struct ggml_tensor * a,
1391
+ int n_past);
1392
+
1393
+ // in-place, returns view(a)
1394
+ GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace(
1395
+ struct ggml_context * ctx,
1396
+ struct ggml_tensor * a,
1397
+ int n_past);
1398
+
1399
+ // set elements above the diagonal to 0
1400
+ GGML_API struct ggml_tensor * ggml_diag_mask_zero(
1401
+ struct ggml_context * ctx,
1402
+ struct ggml_tensor * a,
1403
+ int n_past);
1404
+
1405
+ // in-place, returns view(a)
1406
+ GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace(
1407
+ struct ggml_context * ctx,
1408
+ struct ggml_tensor * a,
1409
+ int n_past);
1410
+
1411
+ GGML_API struct ggml_tensor * ggml_soft_max(
1412
+ struct ggml_context * ctx,
1413
+ struct ggml_tensor * a);
1414
+
1415
+ // in-place, returns view(a)
1416
+ GGML_API struct ggml_tensor * ggml_soft_max_inplace(
1417
+ struct ggml_context * ctx,
1418
+ struct ggml_tensor * a);
1419
+
1420
+ // fused soft_max(a*scale + mask + pos[i]*(ALiBi slope))
1421
+ // mask is optional
1422
+ // pos is required when max_bias > 0.0f
1423
+ // max_bias = 0.0f for no ALiBi
1424
+ GGML_API struct ggml_tensor * ggml_soft_max_ext(
1425
+ struct ggml_context * ctx,
1426
+ struct ggml_tensor * a,
1427
+ struct ggml_tensor * mask,
1428
+ struct ggml_tensor * pos,
1429
+ float scale,
1430
+ float max_bias);
1431
+
1432
+ GGML_API struct ggml_tensor * ggml_soft_max_back(
1433
+ struct ggml_context * ctx,
1434
+ struct ggml_tensor * a,
1435
+ struct ggml_tensor * b);
1436
+
1437
+ // in-place, returns view(a)
1438
+ GGML_API struct ggml_tensor * ggml_soft_max_back_inplace(
1439
+ struct ggml_context * ctx,
1440
+ struct ggml_tensor * a,
1441
+ struct ggml_tensor * b);
1442
+
1443
+ // rotary position embedding
1444
+ // if mode & 1 == 1, skip n_past elements (DEPRECATED)
1445
+ // if mode & 2 == 1, GPT-NeoX style
1446
+ // if mode & 4 == 1, ChatGLM style
1447
+ //
1448
+ // b is an int32 vector with size a->ne[2], it contains the positions
1449
+ GGML_API struct ggml_tensor * ggml_rope(
1450
+ struct ggml_context * ctx,
1451
+ struct ggml_tensor * a,
1452
+ struct ggml_tensor * b,
1453
+ int n_dims,
1454
+ int mode,
1455
+ int n_ctx);
1456
+
1457
+ // in-place, returns view(a)
1458
+ GGML_API struct ggml_tensor * ggml_rope_inplace(
1459
+ struct ggml_context * ctx,
1460
+ struct ggml_tensor * a,
1461
+ struct ggml_tensor * b,
1462
+ int n_dims,
1463
+ int mode,
1464
+ int n_ctx);
1465
+
1466
+ // custom RoPE
1467
+ GGML_API struct ggml_tensor * ggml_rope_custom(
1468
+ struct ggml_context * ctx,
1469
+ struct ggml_tensor * a,
1470
+ struct ggml_tensor * b,
1471
+ int n_dims,
1472
+ int mode,
1473
+ int n_ctx,
1474
+ int n_orig_ctx,
1475
+ float freq_base,
1476
+ float freq_scale,
1477
+ float ext_factor,
1478
+ float attn_factor,
1479
+ float beta_fast,
1480
+ float beta_slow);
1481
+
1482
+ // in-place, returns view(a)
1483
+ GGML_API struct ggml_tensor * ggml_rope_custom_inplace(
1484
+ struct ggml_context * ctx,
1485
+ struct ggml_tensor * a,
1486
+ struct ggml_tensor * b,
1487
+ int n_dims,
1488
+ int mode,
1489
+ int n_ctx,
1490
+ int n_orig_ctx,
1491
+ float freq_base,
1492
+ float freq_scale,
1493
+ float ext_factor,
1494
+ float attn_factor,
1495
+ float beta_fast,
1496
+ float beta_slow);
1497
+
1498
+ // compute correction dims for YaRN RoPE scaling
1499
+ GGML_CALL void ggml_rope_yarn_corr_dims(
1500
+ int n_dims, int n_orig_ctx, float freq_base, float beta_fast, float beta_slow, float dims[2]);
1501
+
1502
+ // xPos RoPE, in-place, returns view(a)
1503
+ GGML_API struct ggml_tensor * ggml_rope_xpos_inplace(
1504
+ struct ggml_context * ctx,
1505
+ struct ggml_tensor * a,
1506
+ struct ggml_tensor * b,
1507
+ int n_dims,
1508
+ float base,
1509
+ bool down);
1510
+
1511
+ // rotary position embedding backward, i.e compute dx from dy
1512
+ // a - dy
1513
+ GGML_API struct ggml_tensor * ggml_rope_back(
1514
+ struct ggml_context * ctx,
1515
+ struct ggml_tensor * a,
1516
+ struct ggml_tensor * b,
1517
+ int n_dims,
1518
+ int mode,
1519
+ int n_ctx,
1520
+ int n_orig_ctx,
1521
+ float freq_base,
1522
+ float freq_scale,
1523
+ float ext_factor,
1524
+ float attn_factor,
1525
+ float beta_fast,
1526
+ float beta_slow,
1527
+ float xpos_base,
1528
+ bool xpos_down);
1529
+
1530
+ // alibi position embedding
1531
+ // in-place, returns view(a)
1532
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_alibi(
1533
+ struct ggml_context * ctx,
1534
+ struct ggml_tensor * a,
1535
+ int n_past,
1536
+ int n_head,
1537
+ float bias_max),
1538
+ "use ggml_soft_max_ext instead (will be removed in Mar 2024)");
1539
+
1540
+ // clamp
1541
+ // in-place, returns view(a)
1542
+ GGML_API struct ggml_tensor * ggml_clamp(
1543
+ struct ggml_context * ctx,
1544
+ struct ggml_tensor * a,
1545
+ float min,
1546
+ float max);
1547
+
1548
+ GGML_API struct ggml_tensor * ggml_im2col(
1549
+ struct ggml_context * ctx,
1550
+ struct ggml_tensor * a,
1551
+ struct ggml_tensor * b,
1552
+ int s0,
1553
+ int s1,
1554
+ int p0,
1555
+ int p1,
1556
+ int d0,
1557
+ int d1,
1558
+ bool is_2D,
1559
+ enum ggml_type dst_type);
1560
+
1561
+ GGML_API struct ggml_tensor * ggml_conv_depthwise_2d(
1562
+ struct ggml_context * ctx,
1563
+ struct ggml_tensor * a,
1564
+ struct ggml_tensor * b,
1565
+ int s0,
1566
+ int s1,
1567
+ int p0,
1568
+ int p1,
1569
+ int d0,
1570
+ int d1);
1571
+
1572
+ GGML_API struct ggml_tensor * ggml_conv_1d(
1573
+ struct ggml_context * ctx,
1574
+ struct ggml_tensor * a,
1575
+ struct ggml_tensor * b,
1576
+ int s0, // stride
1577
+ int p0, // padding
1578
+ int d0); // dilation
1579
+
1580
+ // conv_1d with padding = half
1581
+ // alias for ggml_conv_1d(a, b, s, a->ne[0]/2, d)
1582
+ GGML_API struct ggml_tensor* ggml_conv_1d_ph(
1583
+ struct ggml_context * ctx,
1584
+ struct ggml_tensor * a,
1585
+ struct ggml_tensor * b,
1586
+ int s,
1587
+ int d);
1588
+
1589
+ GGML_API struct ggml_tensor * ggml_conv_transpose_1d(
1590
+ struct ggml_context * ctx,
1591
+ struct ggml_tensor * a,
1592
+ struct ggml_tensor * b,
1593
+ int s0,
1594
+ int p0,
1595
+ int d0);
1596
+
1597
+ GGML_API struct ggml_tensor * ggml_conv_2d(
1598
+ struct ggml_context * ctx,
1599
+ struct ggml_tensor * a,
1600
+ struct ggml_tensor * b,
1601
+ int s0,
1602
+ int s1,
1603
+ int p0,
1604
+ int p1,
1605
+ int d0,
1606
+ int d1);
1607
+
1608
+
1609
+ // kernel size is a->ne[0] x a->ne[1]
1610
+ // stride is equal to kernel size
1611
+ // padding is zero
1612
+ // example:
1613
+ // a: 16 16 3 768
1614
+ // b: 1024 1024 3 1
1615
+ // res: 64 64 768 1
1616
+ // used in sam
1617
+ GGML_API struct ggml_tensor * ggml_conv_2d_sk_p0(
1618
+ struct ggml_context * ctx,
1619
+ struct ggml_tensor * a,
1620
+ struct ggml_tensor * b);
1621
+
1622
+ // kernel size is a->ne[0] x a->ne[1]
1623
+ // stride is 1
1624
+ // padding is half
1625
+ // example:
1626
+ // a: 3 3 256 256
1627
+ // b: 64 64 256 1
1628
+ // res: 64 64 256 1
1629
+ // used in sam
1630
+ GGML_API struct ggml_tensor * ggml_conv_2d_s1_ph(
1631
+ struct ggml_context * ctx,
1632
+ struct ggml_tensor * a,
1633
+ struct ggml_tensor * b);
1634
+
1635
+ GGML_API struct ggml_tensor * ggml_conv_transpose_2d_p0(
1636
+ struct ggml_context * ctx,
1637
+ struct ggml_tensor * a,
1638
+ struct ggml_tensor * b,
1639
+ int stride);
1640
+
1641
+ enum ggml_op_pool {
1642
+ GGML_OP_POOL_MAX,
1643
+ GGML_OP_POOL_AVG,
1644
+ GGML_OP_POOL_COUNT,
1645
+ };
1646
+
1647
+ GGML_API struct ggml_tensor * ggml_pool_1d(
1648
+ struct ggml_context * ctx,
1649
+ struct ggml_tensor * a,
1650
+ enum ggml_op_pool op,
1651
+ int k0, // kernel size
1652
+ int s0, // stride
1653
+ int p0); // padding
1654
+
1655
+ // the result will have 2*p0 padding for the first dimension
1656
+ // and 2*p1 padding for the second dimension
1657
+ GGML_API struct ggml_tensor * ggml_pool_2d(
1658
+ struct ggml_context * ctx,
1659
+ struct ggml_tensor * a,
1660
+ enum ggml_op_pool op,
1661
+ int k0,
1662
+ int k1,
1663
+ int s0,
1664
+ int s1,
1665
+ float p0,
1666
+ float p1);
1667
+
1668
+ // nearest interpolate
1669
+ // used in stable-diffusion
1670
+ GGML_API struct ggml_tensor * ggml_upscale(
1671
+ struct ggml_context * ctx,
1672
+ struct ggml_tensor * a,
1673
+ int scale_factor);
1674
+
1675
+ // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
1676
+ GGML_API struct ggml_tensor * ggml_pad(
1677
+ struct ggml_context * ctx,
1678
+ struct ggml_tensor * a,
1679
+ int p0,
1680
+ int p1,
1681
+ int p2,
1682
+ int p3);
1683
+
1684
+ // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
1685
+ // timesteps: [N,]
1686
+ // return: [N, dim]
1687
+ GGML_API struct ggml_tensor * ggml_timestep_embedding(
1688
+ struct ggml_context * ctx,
1689
+ struct ggml_tensor * timesteps,
1690
+ int dim,
1691
+ int max_period);
1692
+
1693
+ // sort rows
1694
+ enum ggml_sort_order {
1695
+ GGML_SORT_ORDER_ASC,
1696
+ GGML_SORT_ORDER_DESC,
1697
+ };
1698
+
1699
+ GGML_API struct ggml_tensor * ggml_argsort(
1700
+ struct ggml_context * ctx,
1701
+ struct ggml_tensor * a,
1702
+ enum ggml_sort_order order);
1703
+
1704
+ GGML_API struct ggml_tensor * ggml_arange(
1705
+ struct ggml_context * ctx,
1706
+ float start,
1707
+ float stop,
1708
+ float step);
1709
+
1710
+ // top k elements per row
1711
+ GGML_API struct ggml_tensor * ggml_top_k(
1712
+ struct ggml_context * ctx,
1713
+ struct ggml_tensor * a,
1714
+ int k);
1715
+
1716
+ GGML_API struct ggml_tensor * ggml_flash_attn(
1717
+ struct ggml_context * ctx,
1718
+ struct ggml_tensor * q,
1719
+ struct ggml_tensor * k,
1720
+ struct ggml_tensor * v,
1721
+ bool masked);
1722
+
1723
+ GGML_API struct ggml_tensor * ggml_flash_attn_back(
1724
+ struct ggml_context * ctx,
1725
+ struct ggml_tensor * q,
1726
+ struct ggml_tensor * k,
1727
+ struct ggml_tensor * v,
1728
+ struct ggml_tensor * d,
1729
+ bool masked);
1730
+
1731
+ GGML_API struct ggml_tensor * ggml_flash_ff(
1732
+ struct ggml_context * ctx,
1733
+ struct ggml_tensor * a,
1734
+ struct ggml_tensor * b0,
1735
+ struct ggml_tensor * b1,
1736
+ struct ggml_tensor * c0,
1737
+ struct ggml_tensor * c1);
1738
+
1739
+ GGML_API struct ggml_tensor * ggml_ssm_conv(
1740
+ struct ggml_context * ctx,
1741
+ struct ggml_tensor * s,
1742
+ struct ggml_tensor * x,
1743
+ struct ggml_tensor * c,
1744
+ struct ggml_tensor * sq);
1745
+
1746
+ GGML_API struct ggml_tensor * ggml_ssm_scan(
1747
+ struct ggml_context * ctx,
1748
+ struct ggml_tensor * s,
1749
+ struct ggml_tensor * x,
1750
+ struct ggml_tensor * dt,
1751
+ struct ggml_tensor * A,
1752
+ struct ggml_tensor * B,
1753
+ struct ggml_tensor * C,
1754
+ struct ggml_tensor * sq);
1755
+
1756
+ // partition into non-overlapping windows with padding if needed
1757
+ // example:
1758
+ // a: 768 64 64 1
1759
+ // w: 14
1760
+ // res: 768 14 14 25
1761
+ // used in sam
1762
+ GGML_API struct ggml_tensor * ggml_win_part(
1763
+ struct ggml_context * ctx,
1764
+ struct ggml_tensor * a,
1765
+ int w);
1766
+
1767
+ // reverse of ggml_win_part
1768
+ // used in sam
1769
+ GGML_API struct ggml_tensor * ggml_win_unpart(
1770
+ struct ggml_context * ctx,
1771
+ struct ggml_tensor * a,
1772
+ int w0,
1773
+ int h0,
1774
+ int w);
1775
+
1776
+ GGML_API struct ggml_tensor * ggml_unary(
1777
+ struct ggml_context * ctx,
1778
+ struct ggml_tensor * a,
1779
+ enum ggml_unary_op op);
1780
+
1781
+ GGML_API struct ggml_tensor * ggml_unary_inplace(
1782
+ struct ggml_context * ctx,
1783
+ struct ggml_tensor * a,
1784
+ enum ggml_unary_op op);
1785
+
1786
+ // used in sam
1787
+ GGML_API struct ggml_tensor * ggml_get_rel_pos(
1788
+ struct ggml_context * ctx,
1789
+ struct ggml_tensor * a,
1790
+ int qh,
1791
+ int kh);
1792
+
1793
+ // used in sam
1794
+ GGML_API struct ggml_tensor * ggml_add_rel_pos(
1795
+ struct ggml_context * ctx,
1796
+ struct ggml_tensor * a,
1797
+ struct ggml_tensor * pw,
1798
+ struct ggml_tensor * ph);
1799
+
1800
+ GGML_API struct ggml_tensor * ggml_add_rel_pos_inplace(
1801
+ struct ggml_context * ctx,
1802
+ struct ggml_tensor * a,
1803
+ struct ggml_tensor * pw,
1804
+ struct ggml_tensor * ph);
1805
+
1806
+ // custom operators
1807
+
1808
+ typedef void (*ggml_unary_op_f32_t) (const int, float *, const float *);
1809
+ typedef void (*ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
1810
+
1811
+ typedef void (*ggml_custom1_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *);
1812
+ typedef void (*ggml_custom2_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
1813
+ typedef void (*ggml_custom3_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
1814
+
1815
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_unary_f32(
1816
+ struct ggml_context * ctx,
1817
+ struct ggml_tensor * a,
1818
+ ggml_unary_op_f32_t fun),
1819
+ "use ggml_map_custom1 instead");
1820
+
1821
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_unary_inplace_f32(
1822
+ struct ggml_context * ctx,
1823
+ struct ggml_tensor * a,
1824
+ ggml_unary_op_f32_t fun),
1825
+ "use ggml_map_custom1_inplace instead");
1826
+
1827
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_binary_f32(
1828
+ struct ggml_context * ctx,
1829
+ struct ggml_tensor * a,
1830
+ struct ggml_tensor * b,
1831
+ ggml_binary_op_f32_t fun),
1832
+ "use ggml_map_custom2 instead");
1833
+
1834
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_binary_inplace_f32(
1835
+ struct ggml_context * ctx,
1836
+ struct ggml_tensor * a,
1837
+ struct ggml_tensor * b,
1838
+ ggml_binary_op_f32_t fun),
1839
+ "use ggml_map_custom2_inplace instead");
1840
+
1841
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom1_f32(
1842
+ struct ggml_context * ctx,
1843
+ struct ggml_tensor * a,
1844
+ ggml_custom1_op_f32_t fun),
1845
+ "use ggml_map_custom1 instead");
1846
+
1847
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom1_inplace_f32(
1848
+ struct ggml_context * ctx,
1849
+ struct ggml_tensor * a,
1850
+ ggml_custom1_op_f32_t fun),
1851
+ "use ggml_map_custom1_inplace instead");
1852
+
1853
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom2_f32(
1854
+ struct ggml_context * ctx,
1855
+ struct ggml_tensor * a,
1856
+ struct ggml_tensor * b,
1857
+ ggml_custom2_op_f32_t fun),
1858
+ "use ggml_map_custom2 instead");
1859
+
1860
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom2_inplace_f32(
1861
+ struct ggml_context * ctx,
1862
+ struct ggml_tensor * a,
1863
+ struct ggml_tensor * b,
1864
+ ggml_custom2_op_f32_t fun),
1865
+ "use ggml_map_custom2_inplace instead");
1866
+
1867
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom3_f32(
1868
+ struct ggml_context * ctx,
1869
+ struct ggml_tensor * a,
1870
+ struct ggml_tensor * b,
1871
+ struct ggml_tensor * c,
1872
+ ggml_custom3_op_f32_t fun),
1873
+ "use ggml_map_custom3 instead");
1874
+
1875
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom3_inplace_f32(
1876
+ struct ggml_context * ctx,
1877
+ struct ggml_tensor * a,
1878
+ struct ggml_tensor * b,
1879
+ struct ggml_tensor * c,
1880
+ ggml_custom3_op_f32_t fun),
1881
+ "use ggml_map_custom3_inplace instead");
1882
+
1883
+ // custom operators v2
1884
+
1885
+ typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);
1886
+ typedef void (*ggml_custom2_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, int ith, int nth, void * userdata);
1887
+ typedef void (*ggml_custom3_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, const struct ggml_tensor * c, int ith, int nth, void * userdata);
1888
+
1889
+ #define GGML_N_TASKS_MAX -1
1890
+
1891
+ GGML_API struct ggml_tensor * ggml_map_custom1(
1892
+ struct ggml_context * ctx,
1893
+ struct ggml_tensor * a,
1894
+ ggml_custom1_op_t fun,
1895
+ int n_tasks,
1896
+ void * userdata);
1897
+
1898
+ GGML_API struct ggml_tensor * ggml_map_custom1_inplace(
1899
+ struct ggml_context * ctx,
1900
+ struct ggml_tensor * a,
1901
+ ggml_custom1_op_t fun,
1902
+ int n_tasks,
1903
+ void * userdata);
1904
+
1905
+ GGML_API struct ggml_tensor * ggml_map_custom2(
1906
+ struct ggml_context * ctx,
1907
+ struct ggml_tensor * a,
1908
+ struct ggml_tensor * b,
1909
+ ggml_custom2_op_t fun,
1910
+ int n_tasks,
1911
+ void * userdata);
1912
+
1913
+ GGML_API struct ggml_tensor * ggml_map_custom2_inplace(
1914
+ struct ggml_context * ctx,
1915
+ struct ggml_tensor * a,
1916
+ struct ggml_tensor * b,
1917
+ ggml_custom2_op_t fun,
1918
+ int n_tasks,
1919
+ void * userdata);
1920
+
1921
+ GGML_API struct ggml_tensor * ggml_map_custom3(
1922
+ struct ggml_context * ctx,
1923
+ struct ggml_tensor * a,
1924
+ struct ggml_tensor * b,
1925
+ struct ggml_tensor * c,
1926
+ ggml_custom3_op_t fun,
1927
+ int n_tasks,
1928
+ void * userdata);
1929
+
1930
+ GGML_API struct ggml_tensor * ggml_map_custom3_inplace(
1931
+ struct ggml_context * ctx,
1932
+ struct ggml_tensor * a,
1933
+ struct ggml_tensor * b,
1934
+ struct ggml_tensor * c,
1935
+ ggml_custom3_op_t fun,
1936
+ int n_tasks,
1937
+ void * userdata);
1938
+
1939
+ // loss function
1940
+
1941
+ GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
1942
+ struct ggml_context * ctx,
1943
+ struct ggml_tensor * a,
1944
+ struct ggml_tensor * b);
1945
+
1946
+ GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back(
1947
+ struct ggml_context * ctx,
1948
+ struct ggml_tensor * a,
1949
+ struct ggml_tensor * b,
1950
+ struct ggml_tensor * c);
1951
+
1952
+ //
1953
+ // automatic differentiation
1954
+ //
1955
+
1956
+ GGML_API void ggml_set_param(
1957
+ struct ggml_context * ctx,
1958
+ struct ggml_tensor * tensor);
1959
+
1960
+
1961
+ GGML_API void ggml_build_forward_expand (struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
1962
+ GGML_API void ggml_build_backward_expand(struct ggml_context * ctx, struct ggml_cgraph * gf, struct ggml_cgraph * gb, bool keep);
1963
+
1964
+ // graph allocation in a context
1965
+ GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx); // size = GGML_DEFAULT_GRAPH_SIZE, grads = false
1966
+ GGML_API struct ggml_cgraph * ggml_new_graph_custom (struct ggml_context * ctx, size_t size, bool grads);
1967
+ GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, struct ggml_cgraph * cgraph);
1968
+ GGML_API struct ggml_cgraph ggml_graph_view (struct ggml_cgraph * cgraph, int i0, int i1);
1969
+ GGML_API void ggml_graph_cpy (struct ggml_cgraph * src, struct ggml_cgraph * dst);
1970
+ GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph); // zero grads
1971
+ GGML_API void ggml_graph_clear (struct ggml_cgraph * cgraph);
1972
+
1973
+ GGML_API size_t ggml_graph_overhead(void);
1974
+ GGML_API size_t ggml_graph_overhead_custom(size_t size, bool grads);
1975
+
1976
+ // ggml_graph_plan() has to be called before ggml_graph_compute()
1977
+ // when plan.work_size > 0, caller must allocate memory for plan.work_data
1978
+ GGML_API struct ggml_cplan ggml_graph_plan (const struct ggml_cgraph * cgraph, int n_threads /*= GGML_DEFAULT_N_THREADS*/);
1979
+ GGML_API enum ggml_status ggml_graph_compute ( struct ggml_cgraph * cgraph, struct ggml_cplan * cplan);
1980
+ // same as ggml_graph_compute() but the work data is allocated as a part of the context
1981
+ // note: the drawback of this API is that you must have ensured that the context has enough memory for the work data
1982
+ GGML_API enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads);
1983
+
1984
+ GGML_API struct ggml_tensor * ggml_graph_get_tensor(struct ggml_cgraph * cgraph, const char * name);
1985
+
1986
+ GGML_API void ggml_graph_export(const struct ggml_cgraph * cgraph, const char * fname);
1987
+ GGML_API struct ggml_cgraph * ggml_graph_import(const char * fname, struct ggml_context ** ctx_data, struct ggml_context ** ctx_eval);
1988
+
1989
+ // print info and performance information for the graph
1990
+ GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
1991
+
1992
+ // dump the graph into a file using the dot format
1993
+ GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename);
1994
+
1995
+ // build gradient checkpointing backward graph gb for gf using provided checkpoints
1996
+ // gb_tmp will contain original backward graph with rewritten backward process nodes,
1997
+ // but without the second forward pass nodes.
1998
+ GGML_API void ggml_build_backward_gradient_checkpointing(
1999
+ struct ggml_context * ctx,
2000
+ struct ggml_cgraph * gf,
2001
+ struct ggml_cgraph * gb,
2002
+ struct ggml_cgraph * gb_tmp,
2003
+ struct ggml_tensor * * checkpoints,
2004
+ int n_checkpoints);
2005
+ //
2006
+ // optimization
2007
+ //
2008
+
2009
+ // optimization methods
2010
+ enum ggml_opt_type {
2011
+ GGML_OPT_TYPE_ADAM,
2012
+ GGML_OPT_TYPE_LBFGS,
2013
+ };
2014
+
2015
+ // linesearch methods
2016
+ enum ggml_linesearch {
2017
+ GGML_LINESEARCH_DEFAULT = 1,
2018
+
2019
+ GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0,
2020
+ GGML_LINESEARCH_BACKTRACKING_WOLFE = 1,
2021
+ GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2,
2022
+ };
2023
+
2024
+ // optimization return values
2025
+ enum ggml_opt_result {
2026
+ GGML_OPT_RESULT_OK = 0,
2027
+ GGML_OPT_RESULT_DID_NOT_CONVERGE,
2028
+ GGML_OPT_RESULT_NO_CONTEXT,
2029
+ GGML_OPT_RESULT_INVALID_WOLFE,
2030
+ GGML_OPT_RESULT_FAIL,
2031
+ GGML_OPT_RESULT_CANCEL,
2032
+
2033
+ GGML_LINESEARCH_FAIL = -128,
2034
+ GGML_LINESEARCH_MINIMUM_STEP,
2035
+ GGML_LINESEARCH_MAXIMUM_STEP,
2036
+ GGML_LINESEARCH_MAXIMUM_ITERATIONS,
2037
+ GGML_LINESEARCH_INVALID_PARAMETERS,
2038
+ };
2039
+
2040
+ typedef void (*ggml_opt_callback)(void * data, int accum_step, float * sched, bool * cancel);
2041
+ typedef void (*ggml_log_callback)(enum ggml_log_level level, const char * text, void * user_data);
2042
+
2043
+ // optimization parameters
2044
+ //
2045
+ // see ggml.c (ggml_opt_default_params) for default values
2046
+ //
2047
+ struct ggml_opt_params {
2048
+ enum ggml_opt_type type;
2049
+
2050
+ size_t graph_size;
2051
+
2052
+ int n_threads;
2053
+
2054
+ // delta-based convergence test
2055
+ //
2056
+ // if past == 0 - disabled
2057
+ // if past > 0:
2058
+ // stop if |f(x) - f(x_past)| < delta * max(1, |f(x)|)
2059
+ //
2060
+ int past;
2061
+ float delta;
2062
+
2063
+ // maximum number of iterations without improvement
2064
+ //
2065
+ // if 0 - disabled
2066
+ // if > 0:
2067
+ // assume convergence if no cost improvement in this number of iterations
2068
+ //
2069
+ int max_no_improvement;
2070
+
2071
+ bool print_forward_graph;
2072
+ bool print_backward_graph;
2073
+
2074
+ int n_gradient_accumulation;
2075
+
2076
+ // ADAM parameters
2077
+ struct {
2078
+ int n_iter;
2079
+
2080
+ float sched; // schedule multiplier (fixed, decay or warmup)
2081
+ float decay; // weight decay for AdamW, use 0.0f to disable
2082
+ int decay_min_ndim; // minimum number of tensor dimension to apply weight decay
2083
+ float alpha; // learning rate
2084
+ float beta1;
2085
+ float beta2;
2086
+ float eps; // epsilon for numerical stability
2087
+ float eps_f; // epsilon for convergence test
2088
+ float eps_g; // epsilon for convergence test
2089
+ float gclip; // gradient clipping
2090
+ } adam;
2091
+
2092
+ // LBFGS parameters
2093
+ struct {
2094
+ int m; // number of corrections to approximate the inv. Hessian
2095
+ int n_iter;
2096
+ int max_linesearch;
2097
+
2098
+ float eps; // convergence tolerance
2099
+ float ftol; // line search tolerance
2100
+ float wolfe;
2101
+ float min_step;
2102
+ float max_step;
2103
+
2104
+ enum ggml_linesearch linesearch;
2105
+ } lbfgs;
2106
+ };
2107
+
2108
+ struct ggml_opt_context {
2109
+ struct ggml_context * ctx;
2110
+ struct ggml_opt_params params;
2111
+
2112
+ int iter;
2113
+ int64_t nx; // number of parameter elements
2114
+
2115
+ bool just_initialized;
2116
+
2117
+ float loss_before;
2118
+ float loss_after;
2119
+
2120
+ struct {
2121
+ struct ggml_tensor * g; // current gradient
2122
+ struct ggml_tensor * m; // first moment
2123
+ struct ggml_tensor * v; // second moment
2124
+ struct ggml_tensor * pf; // past function values
2125
+ float fx_best;
2126
+ float fx_prev;
2127
+ int n_no_improvement;
2128
+ } adam;
2129
+
2130
+ struct {
2131
+ struct ggml_tensor * x; // current parameters
2132
+ struct ggml_tensor * xp; // previous parameters
2133
+ struct ggml_tensor * g; // current gradient
2134
+ struct ggml_tensor * gp; // previous gradient
2135
+ struct ggml_tensor * d; // search direction
2136
+ struct ggml_tensor * pf; // past function values
2137
+ struct ggml_tensor * lmal; // the L-BFGS memory alpha
2138
+ struct ggml_tensor * lmys; // the L-BFGS memory ys
2139
+ struct ggml_tensor * lms; // the L-BFGS memory s
2140
+ struct ggml_tensor * lmy; // the L-BFGS memory y
2141
+ float fx_best;
2142
+ float step;
2143
+ int j;
2144
+ int k;
2145
+ int end;
2146
+ int n_no_improvement;
2147
+ } lbfgs;
2148
+ };
2149
+
2150
+ GGML_API struct ggml_opt_params ggml_opt_default_params(enum ggml_opt_type type);
2151
+
2152
+ // optimize the function defined by the tensor f
2153
+ GGML_API enum ggml_opt_result ggml_opt(
2154
+ struct ggml_context * ctx,
2155
+ struct ggml_opt_params params,
2156
+ struct ggml_tensor * f);
2157
+
2158
+ // initialize optimizer context
2159
+ GGML_API void ggml_opt_init(
2160
+ struct ggml_context * ctx,
2161
+ struct ggml_opt_context * opt,
2162
+ struct ggml_opt_params params,
2163
+ int64_t nx);
2164
+
2165
+ // continue optimizing the function defined by the tensor f
2166
+ GGML_API enum ggml_opt_result ggml_opt_resume(
2167
+ struct ggml_context * ctx,
2168
+ struct ggml_opt_context * opt,
2169
+ struct ggml_tensor * f);
2170
+
2171
+ // continue optimizing the function defined by the tensor f
2172
+ GGML_API enum ggml_opt_result ggml_opt_resume_g(
2173
+ struct ggml_context * ctx,
2174
+ struct ggml_opt_context * opt,
2175
+ struct ggml_tensor * f,
2176
+ struct ggml_cgraph * gf,
2177
+ struct ggml_cgraph * gb,
2178
+ ggml_opt_callback callback,
2179
+ void * callback_data);
2180
+
2181
+ //
2182
+ // tensor flags
2183
+ //
2184
+ GGML_API void ggml_set_input(struct ggml_tensor * tensor);
2185
+ GGML_API void ggml_set_output(struct ggml_tensor * tensor);
2186
+
2187
+ //
2188
+ // quantization
2189
+ //
2190
+
2191
+ // - ggml_quantize_init can be called multiple times with the same type
2192
+ // it will only initialize the quantization tables for the first call or after ggml_quantize_free
2193
+ // automatically called by ggml_quantize_chunk for convenience
2194
+ //
2195
+ // - ggml_quantize_free will free any memory allocated by ggml_quantize_init
2196
+ // call this at the end of the program to avoid memory leaks
2197
+ //
2198
+ // note: these are thread-safe
2199
+ //
2200
+ GGML_API void ggml_quantize_init(enum ggml_type type);
2201
+ GGML_API void ggml_quantize_free(void);
2202
+
2203
+ // some quantization type cannot be used without an importance matrix
2204
+ GGML_API bool ggml_quantize_requires_imatrix(enum ggml_type type);
2205
+
2206
+ // calls ggml_quantize_init internally (i.e. can allocate memory)
2207
+ GGML_API size_t ggml_quantize_chunk(
2208
+ enum ggml_type type,
2209
+ const float * src,
2210
+ void * dst,
2211
+ int64_t start,
2212
+ int64_t nrows,
2213
+ int64_t n_per_row,
2214
+ const float * imatrix);
2215
+
2216
+ //
2217
+ // gguf
2218
+ //
2219
+
2220
+ enum gguf_type {
2221
+ GGUF_TYPE_UINT8 = 0,
2222
+ GGUF_TYPE_INT8 = 1,
2223
+ GGUF_TYPE_UINT16 = 2,
2224
+ GGUF_TYPE_INT16 = 3,
2225
+ GGUF_TYPE_UINT32 = 4,
2226
+ GGUF_TYPE_INT32 = 5,
2227
+ GGUF_TYPE_FLOAT32 = 6,
2228
+ GGUF_TYPE_BOOL = 7,
2229
+ GGUF_TYPE_STRING = 8,
2230
+ GGUF_TYPE_ARRAY = 9,
2231
+ GGUF_TYPE_UINT64 = 10,
2232
+ GGUF_TYPE_INT64 = 11,
2233
+ GGUF_TYPE_FLOAT64 = 12,
2234
+ GGUF_TYPE_COUNT, // marks the end of the enum
2235
+ };
2236
+
2237
+ struct gguf_context;
2238
+
2239
+ struct gguf_init_params {
2240
+ bool no_alloc;
2241
+
2242
+ // if not NULL, create a ggml_context and allocate the tensor data in it
2243
+ struct ggml_context ** ctx;
2244
+ };
2245
+
2246
+ GGML_API struct gguf_context * gguf_init_empty(void);
2247
+ GGML_API struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params);
2248
+ //GGML_API struct gguf_context * gguf_init_from_buffer(..);
2249
+
2250
+ GGML_API void gguf_free(struct gguf_context * ctx);
2251
+
2252
+ GGML_API const char * gguf_type_name(enum gguf_type type);
2253
+
2254
+ GGML_API int gguf_get_version (const struct gguf_context * ctx);
2255
+ GGML_API size_t gguf_get_alignment (const struct gguf_context * ctx);
2256
+ GGML_API size_t gguf_get_data_offset(const struct gguf_context * ctx);
2257
+ GGML_API void * gguf_get_data (const struct gguf_context * ctx);
2258
+
2259
+ GGML_API int gguf_get_n_kv(const struct gguf_context * ctx);
2260
+ GGML_API int gguf_find_key(const struct gguf_context * ctx, const char * key);
2261
+ GGML_API const char * gguf_get_key (const struct gguf_context * ctx, int key_id);
2262
+
2263
+ GGML_API enum gguf_type gguf_get_kv_type (const struct gguf_context * ctx, int key_id);
2264
+ GGML_API enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int key_id);
2265
+
2266
+ // will abort if the wrong type is used for the key
2267
+ GGML_API uint8_t gguf_get_val_u8 (const struct gguf_context * ctx, int key_id);
2268
+ GGML_API int8_t gguf_get_val_i8 (const struct gguf_context * ctx, int key_id);
2269
+ GGML_API uint16_t gguf_get_val_u16 (const struct gguf_context * ctx, int key_id);
2270
+ GGML_API int16_t gguf_get_val_i16 (const struct gguf_context * ctx, int key_id);
2271
+ GGML_API uint32_t gguf_get_val_u32 (const struct gguf_context * ctx, int key_id);
2272
+ GGML_API int32_t gguf_get_val_i32 (const struct gguf_context * ctx, int key_id);
2273
+ GGML_API float gguf_get_val_f32 (const struct gguf_context * ctx, int key_id);
2274
+ GGML_API uint64_t gguf_get_val_u64 (const struct gguf_context * ctx, int key_id);
2275
+ GGML_API int64_t gguf_get_val_i64 (const struct gguf_context * ctx, int key_id);
2276
+ GGML_API double gguf_get_val_f64 (const struct gguf_context * ctx, int key_id);
2277
+ GGML_API bool gguf_get_val_bool(const struct gguf_context * ctx, int key_id);
2278
+ GGML_API const char * gguf_get_val_str (const struct gguf_context * ctx, int key_id);
2279
+ GGML_API const void * gguf_get_val_data(const struct gguf_context * ctx, int key_id);
2280
+ GGML_API int gguf_get_arr_n (const struct gguf_context * ctx, int key_id);
2281
+ GGML_API const void * gguf_get_arr_data(const struct gguf_context * ctx, int key_id);
2282
+ GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int key_id, int i);
2283
+
2284
+ GGML_API int gguf_get_n_tensors (const struct gguf_context * ctx);
2285
+ GGML_API int gguf_find_tensor (const struct gguf_context * ctx, const char * name);
2286
+ GGML_API size_t gguf_get_tensor_offset(const struct gguf_context * ctx, int i);
2287
+ GGML_API char * gguf_get_tensor_name (const struct gguf_context * ctx, int i);
2288
+ GGML_API enum ggml_type gguf_get_tensor_type (const struct gguf_context * ctx, int i);
2289
+
2290
+ // removes key if it exists
2291
+ GGML_API void gguf_remove_key(struct gguf_context * ctx, const char * key);
2292
+
2293
+ // overrides existing values or adds a new one
2294
+ GGML_API void gguf_set_val_u8 (struct gguf_context * ctx, const char * key, uint8_t val);
2295
+ GGML_API void gguf_set_val_i8 (struct gguf_context * ctx, const char * key, int8_t val);
2296
+ GGML_API void gguf_set_val_u16 (struct gguf_context * ctx, const char * key, uint16_t val);
2297
+ GGML_API void gguf_set_val_i16 (struct gguf_context * ctx, const char * key, int16_t val);
2298
+ GGML_API void gguf_set_val_u32 (struct gguf_context * ctx, const char * key, uint32_t val);
2299
+ GGML_API void gguf_set_val_i32 (struct gguf_context * ctx, const char * key, int32_t val);
2300
+ GGML_API void gguf_set_val_f32 (struct gguf_context * ctx, const char * key, float val);
2301
+ GGML_API void gguf_set_val_u64 (struct gguf_context * ctx, const char * key, uint64_t val);
2302
+ GGML_API void gguf_set_val_i64 (struct gguf_context * ctx, const char * key, int64_t val);
2303
+ GGML_API void gguf_set_val_f64 (struct gguf_context * ctx, const char * key, double val);
2304
+ GGML_API void gguf_set_val_bool(struct gguf_context * ctx, const char * key, bool val);
2305
+ GGML_API void gguf_set_val_str (struct gguf_context * ctx, const char * key, const char * val);
2306
+ GGML_API void gguf_set_arr_data(struct gguf_context * ctx, const char * key, enum gguf_type type, const void * data, int n);
2307
+ GGML_API void gguf_set_arr_str (struct gguf_context * ctx, const char * key, const char ** data, int n);
2308
+
2309
+ // set or add KV pairs from another context
2310
+ GGML_API void gguf_set_kv(struct gguf_context * ctx, struct gguf_context * src);
2311
+
2312
+ // manage tensor info
2313
+ GGML_API void gguf_add_tensor(struct gguf_context * ctx, const struct ggml_tensor * tensor);
2314
+ GGML_API void gguf_set_tensor_type(struct gguf_context * ctx, const char * name, enum ggml_type type);
2315
+ GGML_API void gguf_set_tensor_data(struct gguf_context * ctx, const char * name, const void * data, size_t size);
2316
+
2317
+ // writing gguf files can be done in 2 ways:
2318
+ //
2319
+ // - write the entire gguf_context to a binary file in a single pass:
2320
+ //
2321
+ // gguf_write_to_file(ctx, fname);
2322
+ //
2323
+ // - first prepare a file with a placeholder for the meta data, write the tensor data, then write the meta data:
2324
+ //
2325
+ // FILE * f = fopen(fname, "wb");
2326
+ // fseek(f, gguf_get_meta_size(ctx), SEEK_SET);
2327
+ // fwrite(f, ...);
2328
+ // void * data = gguf_meta_get_meta_data(ctx);
2329
+ // fseek(f, 0, SEEK_SET);
2330
+ // fwrite(f, data, gguf_get_meta_size(ctx));
2331
+ // free(data);
2332
+ // fclose(f);
2333
+ //
2334
+
2335
+ // write the entire context to a binary file
2336
+ GGML_API void gguf_write_to_file(const struct gguf_context * ctx, const char * fname, bool only_meta);
2337
+
2338
+ // get the size in bytes of the meta data (header, kv pairs, tensor info) including padding
2339
+ GGML_API size_t gguf_get_meta_size(const struct gguf_context * ctx);
2340
+ GGML_API void gguf_get_meta_data(const struct gguf_context * ctx, void * data);
2341
+
2342
+ //
2343
+ // system info
2344
+ //
2345
+
2346
+ GGML_API int ggml_cpu_has_avx (void);
2347
+ GGML_API int ggml_cpu_has_avx_vnni (void);
2348
+ GGML_API int ggml_cpu_has_avx2 (void);
2349
+ GGML_API int ggml_cpu_has_avx512 (void);
2350
+ GGML_API int ggml_cpu_has_avx512_vbmi(void);
2351
+ GGML_API int ggml_cpu_has_avx512_vnni(void);
2352
+ GGML_API int ggml_cpu_has_fma (void);
2353
+ GGML_API int ggml_cpu_has_neon (void);
2354
+ GGML_API int ggml_cpu_has_arm_fma (void);
2355
+ GGML_API int ggml_cpu_has_metal (void);
2356
+ GGML_API int ggml_cpu_has_f16c (void);
2357
+ GGML_API int ggml_cpu_has_fp16_va (void);
2358
+ GGML_API int ggml_cpu_has_wasm_simd (void);
2359
+ GGML_API int ggml_cpu_has_blas (void);
2360
+ GGML_API int ggml_cpu_has_cuda (void);
2361
+ GGML_API int ggml_cpu_has_clblast (void);
2362
+ GGML_API int ggml_cpu_has_vulkan (void);
2363
+ GGML_API int ggml_cpu_has_kompute (void);
2364
+ GGML_API int ggml_cpu_has_gpublas (void);
2365
+ GGML_API int ggml_cpu_has_sse3 (void);
2366
+ GGML_API int ggml_cpu_has_ssse3 (void);
2367
+ GGML_API int ggml_cpu_has_sycl (void);
2368
+ GGML_API int ggml_cpu_has_vsx (void);
2369
+ GGML_API int ggml_cpu_has_matmul_int8(void);
2370
+
2371
+ //
2372
+ // Internal types and functions exposed for tests and benchmarks
2373
+ //
2374
+
2375
+ #ifdef __cplusplus
2376
+ // restrict not standard in C++
2377
+ #define GGML_RESTRICT
2378
+ #else
2379
+ #define GGML_RESTRICT restrict
2380
+ #endif
2381
+ typedef void (*ggml_to_float_t) (const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);
2382
+ typedef void (*ggml_from_float_t)(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
2383
+ typedef void (*ggml_vec_dot_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x, size_t bx,
2384
+ const void * GGML_RESTRICT y, size_t by, int nrc);
2385
+
2386
+ typedef struct {
2387
+ const char * type_name;
2388
+ int blck_size;
2389
+ size_t type_size;
2390
+ bool is_quantized;
2391
+ ggml_to_float_t to_float;
2392
+ ggml_from_float_t from_float;
2393
+ ggml_from_float_t from_float_reference;
2394
+ ggml_vec_dot_t vec_dot;
2395
+ enum ggml_type vec_dot_type;
2396
+ int64_t nrows; // number of rows to process simultaneously;
2397
+ } ggml_type_traits_t;
2398
+
2399
+ GGML_API ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type type);
2400
+
2401
+ #ifdef __cplusplus
2402
+ }
2403
+ #endif