bigdl-core-npu 2.5.0__cp311-cp311-win_amd64.whl → 2.6.0__cp311-cp311-win_amd64.whl

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 (146) hide show
  1. bigdl-core-npu/__init__.py +0 -0
  2. bigdl-core-npu/common.lib +0 -0
  3. bigdl-core-npu/ggml.dll +0 -0
  4. bigdl-core-npu/ggml.lib +0 -0
  5. bigdl-core-npu/include/llamacpp/arg.h +77 -0
  6. bigdl-core-npu/include/llamacpp/common.h +563 -0
  7. bigdl-core-npu/include/llamacpp/ggml-alloc.h +76 -0
  8. bigdl-core-npu/include/llamacpp/ggml-backend.h +241 -0
  9. bigdl-core-npu/include/llamacpp/ggml.h +2679 -0
  10. bigdl-core-npu/include/llamacpp/llama.h +1234 -0
  11. bigdl-core-npu/include/llamacpp/log.h +92 -0
  12. bigdl-core-npu/include/npu/npu_common.h +119 -0
  13. bigdl-core-npu/include/npu/npu_llm.h +77 -0
  14. bigdl-core-npu/llama-cli-npu.exe +0 -0
  15. bigdl-core-npu/llama.dll +0 -0
  16. bigdl-core-npu/llama.lib +0 -0
  17. bigdl-core-npu/llm-cli.exe +0 -0
  18. bigdl-core-npu/npu_llm.dll +0 -0
  19. bigdl-core-npu/npu_llm.lib +0 -0
  20. bigdl-core-npu/zlib1.dll +0 -0
  21. bigdl_core_npu-2.6.0.data/scripts/init-llama-cpp.bat +29 -0
  22. {bigdl_core_npu-2.5.0.dist-info → bigdl_core_npu-2.6.0.dist-info}/METADATA +12 -3
  23. {bigdl_core_npu-2.5.0.dist-info → bigdl_core_npu-2.6.0.dist-info}/RECORD +146 -96
  24. {bigdl_core_npu-2.5.0.dist-info → bigdl_core_npu-2.6.0.dist-info}/WHEEL +1 -1
  25. {bigdl_core_npu-2.5.0.dist-info → bigdl_core_npu-2.6.0.dist-info}/top_level.txt +1 -0
  26. intel_npu_acceleration_library/_version.py +1 -1
  27. intel_npu_acceleration_library/backend/base.py +39 -4
  28. intel_npu_acceleration_library/backend/bindings.py +109 -5
  29. intel_npu_acceleration_library/backend/factory.py +264 -47
  30. intel_npu_acceleration_library/backend/ops.py +2 -1
  31. intel_npu_acceleration_library/backend/qlinear.py +8 -4
  32. intel_npu_acceleration_library/backend/runtime.py +7 -2
  33. intel_npu_acceleration_library/backend/tensor.py +73 -3
  34. intel_npu_acceleration_library/bigdl-core-npu/cache.json +113732 -0
  35. intel_npu_acceleration_library/bigdl-core-npu/openvino.dll +0 -0
  36. intel_npu_acceleration_library/bigdl-core-npu/openvino_auto_batch_plugin.dll +0 -0
  37. intel_npu_acceleration_library/bigdl-core-npu/openvino_auto_plugin.dll +0 -0
  38. intel_npu_acceleration_library/bigdl-core-npu/openvino_c.dll +0 -0
  39. intel_npu_acceleration_library/bigdl-core-npu/openvino_hetero_plugin.dll +0 -0
  40. intel_npu_acceleration_library/bigdl-core-npu/openvino_intel_cpu_plugin.dll +0 -0
  41. intel_npu_acceleration_library/bigdl-core-npu/openvino_intel_gpu_plugin.dll +0 -0
  42. intel_npu_acceleration_library/bigdl-core-npu/openvino_intel_npu_plugin.dll +0 -0
  43. intel_npu_acceleration_library/bigdl-core-npu/openvino_ir_frontend.dll +0 -0
  44. intel_npu_acceleration_library/bigdl-core-npu/openvino_onnx_frontend.dll +0 -0
  45. intel_npu_acceleration_library/bigdl-core-npu/openvino_paddle_frontend.dll +0 -0
  46. intel_npu_acceleration_library/bigdl-core-npu/openvino_pytorch_frontend.dll +0 -0
  47. intel_npu_acceleration_library/bigdl-core-npu/openvino_tensorflow_frontend.dll +0 -0
  48. intel_npu_acceleration_library/bigdl-core-npu/openvino_tensorflow_lite_frontend.dll +0 -0
  49. intel_npu_acceleration_library/bigdl-core-npu/tbb12.dll +0 -0
  50. intel_npu_acceleration_library/bigdl-core-npu/tbb12_debug.dll +0 -0
  51. intel_npu_acceleration_library/bigdl-core-npu/tbbbind_2_5.dll +0 -0
  52. intel_npu_acceleration_library/bigdl-core-npu/tbbbind_2_5_debug.dll +0 -0
  53. intel_npu_acceleration_library/bigdl-core-npu/tbbmalloc.dll +0 -0
  54. intel_npu_acceleration_library/bigdl-core-npu/tbbmalloc_debug.dll +0 -0
  55. intel_npu_acceleration_library/bigdl-core-npu/tbbmalloc_proxy.dll +0 -0
  56. intel_npu_acceleration_library/bigdl-core-npu/tbbmalloc_proxy_debug.dll +0 -0
  57. intel_npu_acceleration_library/device.py +2 -2
  58. intel_npu_acceleration_library/dtypes.py +34 -1
  59. intel_npu_acceleration_library/external/openvino/__init__.py +1 -0
  60. intel_npu_acceleration_library/external/openvino/_offline_transformations/__init__.py +1 -0
  61. intel_npu_acceleration_library/external/openvino/_pyopenvino.cp310-win_amd64.pyd +0 -0
  62. intel_npu_acceleration_library/external/openvino/_pyopenvino.cp311-win_amd64.pyd +0 -0
  63. intel_npu_acceleration_library/external/openvino/_pyopenvino.cp312-win_amd64.pyd +0 -0
  64. intel_npu_acceleration_library/external/openvino/_pyopenvino.cp38-win_amd64.pyd +0 -0
  65. intel_npu_acceleration_library/external/openvino/_pyopenvino.cp39-win_amd64.pyd +0 -0
  66. intel_npu_acceleration_library/external/openvino/experimental/__init__.py +14 -0
  67. intel_npu_acceleration_library/external/openvino/frontend/jax/__init__.py +15 -0
  68. intel_npu_acceleration_library/external/openvino/frontend/jax/jaxpr_decoder.py +293 -0
  69. intel_npu_acceleration_library/external/openvino/frontend/jax/passes.py +65 -0
  70. intel_npu_acceleration_library/external/openvino/frontend/jax/utils.py +182 -0
  71. intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp310-win_amd64.pyd +0 -0
  72. intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp311-win_amd64.pyd +0 -0
  73. intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp312-win_amd64.pyd +0 -0
  74. intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp38-win_amd64.pyd +0 -0
  75. intel_npu_acceleration_library/external/openvino/frontend/onnx/py_onnx_frontend.cp39-win_amd64.pyd +0 -0
  76. intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp310-win_amd64.pyd +0 -0
  77. intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp311-win_amd64.pyd +0 -0
  78. intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp312-win_amd64.pyd +0 -0
  79. intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp38-win_amd64.pyd +0 -0
  80. intel_npu_acceleration_library/external/openvino/frontend/paddle/py_paddle_frontend.cp39-win_amd64.pyd +0 -0
  81. intel_npu_acceleration_library/external/openvino/frontend/pytorch/fx_decoder.py +37 -19
  82. intel_npu_acceleration_library/external/openvino/frontend/pytorch/gptq.py +47 -6
  83. intel_npu_acceleration_library/external/openvino/frontend/pytorch/patch_model.py +28 -8
  84. intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp310-win_amd64.pyd +0 -0
  85. intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp311-win_amd64.pyd +0 -0
  86. intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp312-win_amd64.pyd +0 -0
  87. intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp38-win_amd64.pyd +0 -0
  88. intel_npu_acceleration_library/external/openvino/frontend/pytorch/py_pytorch_frontend.cp39-win_amd64.pyd +0 -0
  89. intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/backend.py +17 -5
  90. intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/op_support.py +1 -0
  91. intel_npu_acceleration_library/external/openvino/frontend/pytorch/torchdynamo/partition.py +55 -47
  92. intel_npu_acceleration_library/external/openvino/frontend/pytorch/ts_decoder.py +95 -63
  93. intel_npu_acceleration_library/external/openvino/frontend/pytorch/utils.py +12 -10
  94. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp310-win_amd64.pyd +0 -0
  95. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp311-win_amd64.pyd +0 -0
  96. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp312-win_amd64.pyd +0 -0
  97. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp38-win_amd64.pyd +0 -0
  98. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/py_tensorflow_frontend.cp39-win_amd64.pyd +0 -0
  99. intel_npu_acceleration_library/external/openvino/frontend/tensorflow/utils.py +31 -10
  100. intel_npu_acceleration_library/external/openvino/helpers/packing.py +4 -4
  101. intel_npu_acceleration_library/external/openvino/preprocess/__init__.py +2 -0
  102. intel_npu_acceleration_library/external/openvino/preprocess/torchvision/requirements.txt +1 -0
  103. intel_npu_acceleration_library/external/openvino/properties/__init__.py +1 -0
  104. intel_npu_acceleration_library/external/openvino/runtime/ie_api.py +1 -1
  105. intel_npu_acceleration_library/external/openvino/runtime/op/__init__.py +1 -0
  106. intel_npu_acceleration_library/external/openvino/runtime/opset1/ops.py +2 -1
  107. intel_npu_acceleration_library/external/openvino/runtime/opset13/ops.py +5 -6
  108. intel_npu_acceleration_library/external/openvino/runtime/opset15/__init__.py +7 -0
  109. intel_npu_acceleration_library/external/openvino/runtime/opset15/ops.py +193 -2
  110. intel_npu_acceleration_library/external/openvino/runtime/opset6/ops.py +69 -43
  111. intel_npu_acceleration_library/external/openvino/runtime/opset8/ops.py +4 -0
  112. intel_npu_acceleration_library/external/openvino/runtime/properties/__init__.py +2 -0
  113. intel_npu_acceleration_library/external/openvino/runtime/utils/data_helpers/data_dispatcher.py +21 -3
  114. intel_npu_acceleration_library/external/openvino/runtime/utils/decorators.py +88 -2
  115. intel_npu_acceleration_library/external/openvino/tools/benchmark/utils/inputs_filling.py +9 -9
  116. intel_npu_acceleration_library/external/openvino/tools/ovc/convert_impl.py +16 -2
  117. intel_npu_acceleration_library/external/openvino/tools/ovc/main.py +5 -0
  118. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/jax_frontend_utils.py +19 -0
  119. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/pipeline.py +68 -16
  120. intel_npu_acceleration_library/external/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py +69 -60
  121. intel_npu_acceleration_library/external/openvino/tools/ovc/utils.py +90 -3
  122. intel_npu_acceleration_library/external/openvino/utils.py +17 -0
  123. intel_npu_acceleration_library/lib/Release/intel_npu_acceleration_library.dll +0 -0
  124. intel_npu_acceleration_library/lib/Release/openvino.dll +0 -0
  125. intel_npu_acceleration_library/lib/Release/openvino_auto_batch_plugin.dll +0 -0
  126. intel_npu_acceleration_library/lib/Release/openvino_auto_plugin.dll +0 -0
  127. intel_npu_acceleration_library/lib/Release/openvino_c.dll +0 -0
  128. intel_npu_acceleration_library/lib/Release/openvino_hetero_plugin.dll +0 -0
  129. intel_npu_acceleration_library/lib/Release/openvino_intel_cpu_plugin.dll +0 -0
  130. intel_npu_acceleration_library/lib/Release/openvino_intel_gpu_plugin.dll +0 -0
  131. intel_npu_acceleration_library/lib/Release/openvino_intel_npu_plugin.dll +0 -0
  132. intel_npu_acceleration_library/lib/Release/openvino_ir_frontend.dll +0 -0
  133. intel_npu_acceleration_library/lib/Release/openvino_onnx_frontend.dll +0 -0
  134. intel_npu_acceleration_library/lib/Release/openvino_paddle_frontend.dll +0 -0
  135. intel_npu_acceleration_library/lib/Release/openvino_pytorch_frontend.dll +0 -0
  136. intel_npu_acceleration_library/lib/Release/openvino_tensorflow_frontend.dll +0 -0
  137. intel_npu_acceleration_library/lib/Release/openvino_tensorflow_lite_frontend.dll +0 -0
  138. intel_npu_acceleration_library/lib/Release/tbb12.dll +0 -0
  139. intel_npu_acceleration_library/lib/Release/tbb12_debug.dll +0 -0
  140. intel_npu_acceleration_library/lib/Release/tbbbind_2_5.dll +0 -0
  141. intel_npu_acceleration_library/lib/Release/tbbbind_2_5_debug.dll +0 -0
  142. intel_npu_acceleration_library/lib/Release/tbbmalloc.dll +0 -0
  143. intel_npu_acceleration_library/lib/Release/tbbmalloc_debug.dll +0 -0
  144. intel_npu_acceleration_library/lib/Release/tbbmalloc_proxy.dll +0 -0
  145. intel_npu_acceleration_library/lib/Release/tbbmalloc_proxy_debug.dll +0 -0
  146. intel_npu_acceleration_library/nn/module.py +17 -17
@@ -0,0 +1,2679 @@
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 2
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
+ #define GGML_MAX_N_THREADS 512
233
+ #define GGML_MAX_OP_PARAMS 64
234
+
235
+ #ifndef GGML_MAX_NAME
236
+ # define GGML_MAX_NAME 64
237
+ #endif
238
+
239
+ #define GGML_DEFAULT_N_THREADS 4
240
+ #define GGML_DEFAULT_GRAPH_SIZE 2048
241
+
242
+ #if UINTPTR_MAX == 0xFFFFFFFF
243
+ #define GGML_MEM_ALIGN 4
244
+ #else
245
+ #define GGML_MEM_ALIGN 16
246
+ #endif
247
+
248
+ #define GGML_EXIT_SUCCESS 0
249
+ #define GGML_EXIT_ABORTED 1
250
+
251
+ #define GGML_ROPE_TYPE_NEOX 2
252
+
253
+ #define GGUF_MAGIC "GGUF"
254
+
255
+ #define GGUF_VERSION 3
256
+
257
+ #define GGUF_DEFAULT_ALIGNMENT 32
258
+
259
+ #define GGML_UNUSED(x) (void)(x)
260
+
261
+ #define GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
262
+
263
+ #ifndef NDEBUG
264
+ # define GGML_UNREACHABLE() do { fprintf(stderr, "statement should be unreachable\n"); abort(); } while(0)
265
+ #elif defined(__GNUC__)
266
+ # define GGML_UNREACHABLE() __builtin_unreachable()
267
+ #elif defined(_MSC_VER)
268
+ # define GGML_UNREACHABLE() __assume(0)
269
+ #else
270
+ # define GGML_UNREACHABLE() ((void) 0)
271
+ #endif
272
+
273
+ #ifdef __cplusplus
274
+ # define GGML_NORETURN [[noreturn]]
275
+ #elif defined(_MSC_VER)
276
+ # define GGML_NORETURN __declspec(noreturn)
277
+ #else
278
+ # define GGML_NORETURN _Noreturn
279
+ #endif
280
+
281
+ #define GGML_ABORT(...) ggml_abort(__FILE__, __LINE__, __VA_ARGS__)
282
+ #define GGML_ASSERT(x) if (!(x)) GGML_ABORT("GGML_ASSERT(%s) failed", #x)
283
+
284
+ // used to copy the number of elements and stride in bytes of tensors into local variables.
285
+ // main purpose is to reduce code duplication and improve readability.
286
+ //
287
+ // example:
288
+ //
289
+ // GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
290
+ // GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
291
+ //
292
+ #define GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
293
+ const type prefix##0 = (pointer)->array[0]; \
294
+ GGML_UNUSED(prefix##0);
295
+ #define GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
296
+ GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
297
+ const type prefix##1 = (pointer)->array[1]; \
298
+ GGML_UNUSED(prefix##1);
299
+ #define GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
300
+ GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
301
+ const type prefix##2 = (pointer)->array[2]; \
302
+ GGML_UNUSED(prefix##2);
303
+ #define GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
304
+ GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
305
+ const type prefix##3 = (pointer)->array[3]; \
306
+ GGML_UNUSED(prefix##3);
307
+
308
+ #define GGML_TENSOR_UNARY_OP_LOCALS \
309
+ GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
310
+ GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
311
+ GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
312
+ GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
313
+
314
+ #define GGML_TENSOR_BINARY_OP_LOCALS \
315
+ GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
316
+ GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
317
+ GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
318
+ GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
319
+ GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
320
+ GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
321
+
322
+ #define GGML_TENSOR_BINARY_OP_LOCALS01 \
323
+ GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
324
+ GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
325
+ GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
326
+ GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
327
+
328
+ #ifdef __cplusplus
329
+ extern "C" {
330
+ #endif
331
+
332
+ GGML_NORETURN GGML_ATTRIBUTE_FORMAT(3, 4)
333
+ GGML_API void ggml_abort(const char * file, int line, const char * fmt, ...);
334
+
335
+ enum ggml_status {
336
+ GGML_STATUS_ALLOC_FAILED = -2,
337
+ GGML_STATUS_FAILED = -1,
338
+ GGML_STATUS_SUCCESS = 0,
339
+ GGML_STATUS_ABORTED = 1,
340
+ };
341
+
342
+ // get ggml_status name string
343
+ GGML_API GGML_CALL const char * ggml_status_to_string(enum ggml_status status);
344
+
345
+ // ieee 754-2008 half-precision float16
346
+ // todo: make this not an integral type
347
+ typedef uint16_t ggml_fp16_t;
348
+ GGML_API float ggml_fp16_to_fp32(ggml_fp16_t);
349
+ GGML_API ggml_fp16_t ggml_fp32_to_fp16(float);
350
+ GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t *, float *, int64_t);
351
+ GGML_API void ggml_fp32_to_fp16_row(const float *, ggml_fp16_t *, int64_t);
352
+
353
+ // google brain half-precision bfloat16
354
+ typedef struct { uint16_t bits; } ggml_bf16_t;
355
+ GGML_API ggml_bf16_t ggml_fp32_to_bf16(float);
356
+ GGML_API float ggml_bf16_to_fp32(ggml_bf16_t); // consider just doing << 16
357
+ GGML_API void ggml_bf16_to_fp32_row(const ggml_bf16_t *, float *, int64_t);
358
+ GGML_API void ggml_fp32_to_bf16_row_ref(const float *, ggml_bf16_t *, int64_t);
359
+ GGML_API void ggml_fp32_to_bf16_row(const float *, ggml_bf16_t *, int64_t);
360
+
361
+ struct ggml_object;
362
+ struct ggml_context;
363
+ struct ggml_cgraph;
364
+
365
+ // NOTE: always add types at the end of the enum to keep backward compatibility
366
+ enum ggml_type {
367
+ GGML_TYPE_F32 = 0,
368
+ GGML_TYPE_F16 = 1,
369
+ GGML_TYPE_Q4_0 = 2,
370
+ GGML_TYPE_Q4_1 = 3,
371
+ // GGML_TYPE_Q4_2 = 4, support has been removed
372
+ // GGML_TYPE_Q4_3 = 5, support has been removed
373
+ GGML_TYPE_Q5_0 = 6,
374
+ GGML_TYPE_Q5_1 = 7,
375
+ GGML_TYPE_Q8_0 = 8,
376
+ GGML_TYPE_Q8_1 = 9,
377
+ GGML_TYPE_Q2_K = 10,
378
+ GGML_TYPE_Q3_K = 11,
379
+ GGML_TYPE_Q4_K = 12,
380
+ GGML_TYPE_Q5_K = 13,
381
+ GGML_TYPE_Q6_K = 14,
382
+ GGML_TYPE_Q8_K = 15,
383
+ GGML_TYPE_IQ2_XXS = 16,
384
+ GGML_TYPE_IQ2_XS = 17,
385
+ GGML_TYPE_IQ3_XXS = 18,
386
+ GGML_TYPE_IQ1_S = 19,
387
+ GGML_TYPE_IQ4_NL = 20,
388
+ GGML_TYPE_IQ3_S = 21,
389
+ GGML_TYPE_IQ2_S = 22,
390
+ GGML_TYPE_IQ4_XS = 23,
391
+ GGML_TYPE_I8 = 24,
392
+ GGML_TYPE_I16 = 25,
393
+ GGML_TYPE_I32 = 26,
394
+ GGML_TYPE_I64 = 27,
395
+ GGML_TYPE_F64 = 28,
396
+ GGML_TYPE_IQ1_M = 29,
397
+ GGML_TYPE_BF16 = 30,
398
+ GGML_TYPE_Q4_0_4_4 = 31,
399
+ GGML_TYPE_Q4_0_4_8 = 32,
400
+ GGML_TYPE_Q4_0_8_8 = 33,
401
+ GGML_TYPE_TQ1_0 = 34,
402
+ GGML_TYPE_TQ2_0 = 35,
403
+ GGML_TYPE_Q4_0_RTN = 36,
404
+ GGML_TYPE_Q4_1_RTN = 37,
405
+ GGML_TYPE_COUNT,
406
+ };
407
+
408
+ // precision
409
+ enum ggml_prec {
410
+ GGML_PREC_DEFAULT,
411
+ GGML_PREC_F32,
412
+ };
413
+
414
+ enum ggml_backend_type {
415
+ GGML_BACKEND_TYPE_CPU = 0,
416
+ GGML_BACKEND_TYPE_GPU = 10,
417
+ GGML_BACKEND_TYPE_GPU_SPLIT = 20,
418
+ };
419
+
420
+ // model file types
421
+ enum ggml_ftype {
422
+ GGML_FTYPE_UNKNOWN = -1,
423
+ GGML_FTYPE_ALL_F32 = 0,
424
+ GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
425
+ GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
426
+ GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
427
+ GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
428
+ GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
429
+ GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
430
+ GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
431
+ GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
432
+ GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
433
+ GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
434
+ GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
435
+ GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
436
+ GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors
437
+ GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors
438
+ GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors
439
+ GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors
440
+ GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors
441
+ GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors
442
+ GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors
443
+ GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors
444
+ GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors
445
+ GGML_FTYPE_MOSTLY_BF16 = 24, // except 1d tensors
446
+ GGML_FTYPE_MOSTLY_Q4_0_4_4 = 25, // except 1d tensors
447
+ GGML_FTYPE_MOSTLY_Q4_0_4_8 = 26, // except 1d tensors
448
+ GGML_FTYPE_MOSTLY_Q4_0_8_8 = 27, // except 1d tensors
449
+ };
450
+
451
+ // available tensor operations:
452
+ enum ggml_op {
453
+ GGML_OP_NONE = 0,
454
+
455
+ GGML_OP_DUP,
456
+ GGML_OP_ADD,
457
+ GGML_OP_ADD1,
458
+ GGML_OP_ACC,
459
+ GGML_OP_SUB,
460
+ GGML_OP_MUL,
461
+ GGML_OP_DIV,
462
+ GGML_OP_SQR,
463
+ GGML_OP_SQRT,
464
+ GGML_OP_LOG,
465
+ GGML_OP_SIN,
466
+ GGML_OP_COS,
467
+ GGML_OP_SUM,
468
+ GGML_OP_SUM_ROWS,
469
+ GGML_OP_MEAN,
470
+ GGML_OP_ARGMAX,
471
+ GGML_OP_REPEAT,
472
+ GGML_OP_REPEAT_BACK,
473
+ GGML_OP_CONCAT,
474
+ GGML_OP_SILU_BACK,
475
+ GGML_OP_NORM, // normalize
476
+ GGML_OP_RMS_NORM,
477
+ GGML_OP_RMS_NORM_BACK,
478
+ GGML_OP_GROUP_NORM,
479
+
480
+ GGML_OP_MUL_MAT,
481
+ GGML_OP_MUL_MAT_ID,
482
+ GGML_OP_OUT_PROD,
483
+
484
+ GGML_OP_SCALE,
485
+ GGML_OP_SET,
486
+ GGML_OP_CPY,
487
+ GGML_OP_CONT,
488
+ GGML_OP_RESHAPE,
489
+ GGML_OP_VIEW,
490
+ GGML_OP_PERMUTE,
491
+ GGML_OP_TRANSPOSE,
492
+ GGML_OP_GET_ROWS,
493
+ GGML_OP_GET_ROWS_BACK,
494
+ GGML_OP_DIAG,
495
+ GGML_OP_DIAG_MASK_INF,
496
+ GGML_OP_DIAG_MASK_ZERO,
497
+ GGML_OP_SOFT_MAX,
498
+ GGML_OP_SOFT_MAX_BACK,
499
+ GGML_OP_ROPE,
500
+ GGML_OP_ROPE_BACK,
501
+ GGML_OP_CLAMP,
502
+ GGML_OP_CONV_TRANSPOSE_1D,
503
+ GGML_OP_IM2COL,
504
+ GGML_OP_IM2COL_BACK,
505
+ GGML_OP_CONV_TRANSPOSE_2D,
506
+ GGML_OP_POOL_1D,
507
+ GGML_OP_POOL_2D,
508
+ GGML_OP_POOL_2D_BACK,
509
+ GGML_OP_UPSCALE, // nearest interpolate
510
+ GGML_OP_PAD,
511
+ GGML_OP_UNPAD,
512
+ GGML_OP_ARANGE,
513
+ GGML_OP_TIMESTEP_EMBEDDING,
514
+ GGML_OP_ARGSORT,
515
+ GGML_OP_LEAKY_RELU,
516
+
517
+ GGML_OP_FLASH_ATTN_EXT,
518
+ GGML_OP_FLASH_ATTN_BACK,
519
+ GGML_OP_SSM_CONV,
520
+ GGML_OP_SSM_SCAN,
521
+ GGML_OP_WIN_PART,
522
+ GGML_OP_WIN_UNPART,
523
+ GGML_OP_GET_REL_POS,
524
+ GGML_OP_ADD_REL_POS,
525
+ GGML_OP_RWKV_WKV,
526
+
527
+ GGML_OP_UNARY,
528
+
529
+ GGML_OP_MAP_UNARY,
530
+ GGML_OP_MAP_BINARY,
531
+
532
+ GGML_OP_MAP_CUSTOM1_F32,
533
+ GGML_OP_MAP_CUSTOM2_F32,
534
+ GGML_OP_MAP_CUSTOM3_F32,
535
+
536
+ GGML_OP_MAP_CUSTOM1,
537
+ GGML_OP_MAP_CUSTOM2,
538
+ GGML_OP_MAP_CUSTOM3,
539
+
540
+ GGML_OP_CROSS_ENTROPY_LOSS,
541
+ GGML_OP_CROSS_ENTROPY_LOSS_BACK,
542
+ GGML_OP_OPT_STEP_ADAMW,
543
+
544
+ GGML_OP_BIGDL_FP16_SDP,
545
+ GGML_OP_BIGDL_QKV_FUSION,
546
+ GGML_OP_BIGDL_WQKV_FUSION,
547
+ GGML_OP_BIGDL_MLP_FUSION,
548
+ GGML_OP_BIGDL_MLP_FUSION_UPGATE,
549
+ GGML_OP_BIGDL_QUANTIZE_KV,
550
+ GGML_OP_BIGDL_DEQUANTIZE_KV,
551
+
552
+ GGML_OP_COUNT,
553
+ };
554
+
555
+ enum ggml_unary_op {
556
+ GGML_UNARY_OP_ABS,
557
+ GGML_UNARY_OP_SGN,
558
+ GGML_UNARY_OP_NEG,
559
+ GGML_UNARY_OP_STEP,
560
+ GGML_UNARY_OP_TANH,
561
+ GGML_UNARY_OP_ELU,
562
+ GGML_UNARY_OP_RELU,
563
+ GGML_UNARY_OP_SIGMOID,
564
+ GGML_UNARY_OP_GELU,
565
+ GGML_UNARY_OP_GELU_QUICK,
566
+ GGML_UNARY_OP_SILU,
567
+ GGML_UNARY_OP_HARDSWISH,
568
+ GGML_UNARY_OP_HARDSIGMOID,
569
+ GGML_UNARY_OP_EXP,
570
+
571
+ GGML_UNARY_OP_COUNT,
572
+ };
573
+
574
+ enum ggml_object_type {
575
+ GGML_OBJECT_TYPE_TENSOR,
576
+ GGML_OBJECT_TYPE_GRAPH,
577
+ GGML_OBJECT_TYPE_WORK_BUFFER
578
+ };
579
+
580
+ enum ggml_log_level {
581
+ GGML_LOG_LEVEL_NONE = 0,
582
+ GGML_LOG_LEVEL_INFO = 1,
583
+ GGML_LOG_LEVEL_WARN = 2,
584
+ GGML_LOG_LEVEL_ERROR = 3,
585
+ GGML_LOG_LEVEL_DEBUG = 4,
586
+ GGML_LOG_LEVEL_CONT = 5, // continue previous log
587
+ };
588
+
589
+ // this tensor...
590
+ enum ggml_tensor_flag {
591
+ GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
592
+ GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
593
+ GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
594
+ GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up)
595
+ };
596
+
597
+ // n-dimensional tensor
598
+ struct ggml_tensor {
599
+ enum ggml_type type;
600
+
601
+ GGML_DEPRECATED(enum ggml_backend_type backend, "use the buffer type to find the storage location of the tensor");
602
+
603
+ struct ggml_backend_buffer * buffer;
604
+
605
+ int64_t ne[GGML_MAX_DIMS]; // number of elements
606
+ size_t nb[GGML_MAX_DIMS]; // stride in bytes:
607
+ // nb[0] = ggml_type_size(type)
608
+ // nb[1] = nb[0] * (ne[0] / ggml_blck_size(type)) + padding
609
+ // nb[i] = nb[i-1] * ne[i-1]
610
+
611
+ // compute data
612
+ enum ggml_op op;
613
+
614
+ // op params - allocated as int32_t for alignment
615
+ int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
616
+
617
+ int32_t flags;
618
+
619
+ struct ggml_tensor * grad;
620
+ struct ggml_tensor * src[GGML_MAX_SRC];
621
+
622
+ // source tensor and offset for views
623
+ struct ggml_tensor * view_src;
624
+ size_t view_offs;
625
+
626
+ void * data;
627
+
628
+ char name[GGML_MAX_NAME];
629
+
630
+ void * extra; // extra things e.g. for ggml-cuda.cu
631
+
632
+ // char padding[4];
633
+ };
634
+
635
+ static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor);
636
+
637
+ // Abort callback
638
+ // If not NULL, called before ggml computation
639
+ // If it returns true, the computation is aborted
640
+ typedef bool (*ggml_abort_callback)(void * data);
641
+
642
+ // Scheduling priorities
643
+ enum ggml_sched_priority {
644
+ GGML_SCHED_PRIO_NORMAL,
645
+ GGML_SCHED_PRIO_MEDIUM,
646
+ GGML_SCHED_PRIO_HIGH,
647
+ GGML_SCHED_PRIO_REALTIME
648
+ };
649
+
650
+ // Threadpool params
651
+ // Use ggml_threadpool_params_default() or ggml_threadpool_params_init() to populate the defaults
652
+ struct ggml_threadpool_params {
653
+ bool cpumask[GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
654
+ int n_threads; // number of threads
655
+ enum ggml_sched_priority prio; // thread priority
656
+ uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
657
+ bool strict_cpu; // strict cpu placement
658
+ bool paused; // start in paused state
659
+ };
660
+
661
+ struct ggml_threadpool; // forward declaration, see ggml.c
662
+
663
+ typedef struct ggml_threadpool * ggml_threadpool_t;
664
+
665
+ // the compute plan that needs to be prepared for ggml_graph_compute()
666
+ // since https://github.com/ggerganov/ggml/issues/287
667
+ struct ggml_cplan {
668
+ size_t work_size; // size of work buffer, calculated by `ggml_graph_plan()`
669
+ uint8_t * work_data; // work buffer, to be allocated by caller before calling to `ggml_graph_compute()`
670
+
671
+ int n_threads;
672
+ struct ggml_threadpool * threadpool;
673
+
674
+ // abort ggml_graph_compute when true
675
+ ggml_abort_callback abort_callback;
676
+ void * abort_callback_data;
677
+ };
678
+
679
+ // scratch buffer
680
+ struct ggml_scratch {
681
+ size_t offs;
682
+ size_t size;
683
+ void * data;
684
+ };
685
+
686
+ struct ggml_init_params {
687
+ // memory pool
688
+ size_t mem_size; // bytes
689
+ void * mem_buffer; // if NULL, memory will be allocated internally
690
+ bool no_alloc; // don't allocate memory for the tensor data
691
+ };
692
+
693
+ // numa strategies
694
+ enum ggml_numa_strategy {
695
+ GGML_NUMA_STRATEGY_DISABLED = 0,
696
+ GGML_NUMA_STRATEGY_DISTRIBUTE = 1,
697
+ GGML_NUMA_STRATEGY_ISOLATE = 2,
698
+ GGML_NUMA_STRATEGY_NUMACTL = 3,
699
+ GGML_NUMA_STRATEGY_MIRROR = 4,
700
+ GGML_NUMA_STRATEGY_COUNT
701
+ };
702
+
703
+ //
704
+ // GUID
705
+ //
706
+
707
+ // GUID types
708
+ typedef uint8_t ggml_guid[16];
709
+ typedef ggml_guid * ggml_guid_t;
710
+
711
+ GGML_API bool ggml_guid_matches(ggml_guid_t guid_a, ggml_guid_t guid_b);
712
+
713
+ // misc
714
+
715
+ GGML_API void ggml_time_init(void); // call this once at the beginning of the program
716
+ GGML_API int64_t ggml_time_ms(void);
717
+ GGML_API int64_t ggml_time_us(void);
718
+ GGML_API int64_t ggml_cycles(void);
719
+ GGML_API int64_t ggml_cycles_per_ms(void);
720
+
721
+ // accepts a UTF-8 path, even on Windows
722
+ GGML_API FILE * ggml_fopen(const char * fname, const char * mode);
723
+
724
+ GGML_API void ggml_numa_init(enum ggml_numa_strategy numa); // call once for better performance on NUMA systems
725
+ GGML_API bool ggml_is_numa(void); // true if init detected that system has >1 NUMA node
726
+
727
+ GGML_API void ggml_print_object (const struct ggml_object * obj);
728
+ GGML_API void ggml_print_objects(const struct ggml_context * ctx);
729
+
730
+ GGML_API GGML_CALL int64_t ggml_nelements (const struct ggml_tensor * tensor);
731
+ GGML_API GGML_CALL int64_t ggml_nrows (const struct ggml_tensor * tensor);
732
+ GGML_API GGML_CALL size_t ggml_nbytes (const struct ggml_tensor * tensor);
733
+ GGML_API size_t ggml_nbytes_pad (const struct ggml_tensor * tensor); // same as ggml_nbytes() but padded to GGML_MEM_ALIGN
734
+
735
+ GGML_API GGML_CALL int64_t ggml_blck_size(enum ggml_type type);
736
+ GGML_API GGML_CALL size_t ggml_type_size(enum ggml_type type); // size in bytes for all elements in a block
737
+ 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
738
+
739
+ GGML_DEPRECATED(
740
+ GGML_API double ggml_type_sizef(enum ggml_type type), // ggml_type_size()/ggml_blck_size() as float
741
+ "use ggml_row_size() instead");
742
+
743
+ GGML_API GGML_CALL const char * ggml_type_name(enum ggml_type type);
744
+ GGML_API GGML_CALL const char * ggml_op_name (enum ggml_op op);
745
+ GGML_API const char * ggml_op_symbol(enum ggml_op op);
746
+
747
+ GGML_API const char * ggml_unary_op_name(enum ggml_unary_op op);
748
+ GGML_API GGML_CALL const char * ggml_op_desc(const struct ggml_tensor * t); // unary or op name
749
+
750
+ GGML_API GGML_CALL size_t ggml_element_size(const struct ggml_tensor * tensor);
751
+
752
+ GGML_API GGML_CALL bool ggml_is_quantized(enum ggml_type type);
753
+
754
+ // TODO: temporary until model loading of ggml examples is refactored
755
+ GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
756
+
757
+ GGML_API GGML_CALL bool ggml_is_transposed(const struct ggml_tensor * tensor);
758
+ GGML_API GGML_CALL bool ggml_is_permuted (const struct ggml_tensor * tensor);
759
+ GGML_API GGML_CALL bool ggml_is_empty (const struct ggml_tensor * tensor);
760
+ GGML_API bool ggml_is_scalar (const struct ggml_tensor * tensor);
761
+ GGML_API bool ggml_is_vector (const struct ggml_tensor * tensor);
762
+ GGML_API bool ggml_is_matrix (const struct ggml_tensor * tensor);
763
+ GGML_API bool ggml_is_3d (const struct ggml_tensor * tensor);
764
+ GGML_API int ggml_n_dims (const struct ggml_tensor * tensor); // returns 1 for scalars
765
+
766
+ GGML_API GGML_CALL bool ggml_is_contiguous (const struct ggml_tensor * tensor);
767
+ GGML_API GGML_CALL bool ggml_is_contiguous_0(const struct ggml_tensor * tensor); // same as ggml_is_contiguous()
768
+ GGML_API GGML_CALL bool ggml_is_contiguous_1(const struct ggml_tensor * tensor); // contiguous for dims >= 1
769
+ GGML_API GGML_CALL bool ggml_is_contiguous_2(const struct ggml_tensor * tensor); // contiguous for dims >= 2
770
+
771
+ GGML_API bool ggml_are_same_shape (const struct ggml_tensor * t0, const struct ggml_tensor * t1);
772
+ GGML_API bool ggml_are_same_stride(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
773
+
774
+ GGML_API bool ggml_can_repeat(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
775
+
776
+ // use this to compute the memory overhead of a tensor
777
+ GGML_API size_t ggml_tensor_overhead(void);
778
+
779
+ GGML_API bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbytes);
780
+
781
+ // main
782
+
783
+ GGML_API struct ggml_context * ggml_init(struct ggml_init_params params);
784
+ GGML_API void ggml_free(struct ggml_context * ctx);
785
+
786
+ GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
787
+
788
+ GGML_API size_t ggml_set_scratch (struct ggml_context * ctx, struct ggml_scratch scratch);
789
+ GGML_API bool ggml_get_no_alloc(struct ggml_context * ctx);
790
+ GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
791
+
792
+ GGML_API void * ggml_get_mem_buffer (const struct ggml_context * ctx);
793
+ GGML_API size_t ggml_get_mem_size (const struct ggml_context * ctx);
794
+ GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx);
795
+
796
+ GGML_API struct ggml_tensor * ggml_new_tensor(
797
+ struct ggml_context * ctx,
798
+ enum ggml_type type,
799
+ int n_dims,
800
+ const int64_t *ne);
801
+
802
+ GGML_API struct ggml_tensor * ggml_new_tensor_1d(
803
+ struct ggml_context * ctx,
804
+ enum ggml_type type,
805
+ int64_t ne0);
806
+
807
+ GGML_API struct ggml_tensor * ggml_new_tensor_2d(
808
+ struct ggml_context * ctx,
809
+ enum ggml_type type,
810
+ int64_t ne0,
811
+ int64_t ne1);
812
+
813
+ GGML_API struct ggml_tensor * ggml_new_tensor_3d(
814
+ struct ggml_context * ctx,
815
+ enum ggml_type type,
816
+ int64_t ne0,
817
+ int64_t ne1,
818
+ int64_t ne2);
819
+
820
+ GGML_API struct ggml_tensor * ggml_new_tensor_4d(
821
+ struct ggml_context * ctx,
822
+ enum ggml_type type,
823
+ int64_t ne0,
824
+ int64_t ne1,
825
+ int64_t ne2,
826
+ int64_t ne3);
827
+
828
+ GGML_API struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value);
829
+ GGML_API struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value);
830
+
831
+ GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src);
832
+ GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, struct ggml_tensor * src);
833
+
834
+ // Context tensor enumeration and lookup
835
+ GGML_API struct ggml_tensor * ggml_get_first_tensor(const struct ggml_context * ctx);
836
+ GGML_API struct ggml_tensor * ggml_get_next_tensor (const struct ggml_context * ctx, struct ggml_tensor * tensor);
837
+ GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name);
838
+
839
+ GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor);
840
+ GGML_API struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value);
841
+ GGML_API struct ggml_tensor * ggml_set_f32 (struct ggml_tensor * tensor, float value);
842
+
843
+ // Converts a flat index into coordinates
844
+ 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);
845
+
846
+ GGML_API int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i);
847
+ GGML_API void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value);
848
+
849
+ GGML_API int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
850
+ GGML_API void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value);
851
+
852
+ GGML_API float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i);
853
+ GGML_API void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value);
854
+
855
+ GGML_API float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3);
856
+ GGML_API void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value);
857
+
858
+ GGML_API void * ggml_get_data (const struct ggml_tensor * tensor);
859
+ GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor);
860
+
861
+ GGML_API GGML_CALL enum ggml_unary_op ggml_get_unary_op(const struct ggml_tensor * tensor);
862
+
863
+ GGML_API const char * ggml_get_name (const struct ggml_tensor * tensor);
864
+ GGML_API struct ggml_tensor * ggml_set_name ( struct ggml_tensor * tensor, const char * name);
865
+ GGML_ATTRIBUTE_FORMAT(2, 3)
866
+ GGML_API struct ggml_tensor * ggml_format_name( struct ggml_tensor * tensor, const char * fmt, ...);
867
+
868
+ //
869
+ // operations on tensors with backpropagation
870
+ //
871
+
872
+ GGML_API struct ggml_tensor * ggml_dup(
873
+ struct ggml_context * ctx,
874
+ struct ggml_tensor * a);
875
+
876
+ // in-place, returns view(a)
877
+ GGML_API struct ggml_tensor * ggml_dup_inplace(
878
+ struct ggml_context * ctx,
879
+ struct ggml_tensor * a);
880
+
881
+ GGML_API struct ggml_tensor * ggml_add(
882
+ struct ggml_context * ctx,
883
+ struct ggml_tensor * a,
884
+ struct ggml_tensor * b);
885
+
886
+ GGML_API struct ggml_tensor * ggml_add_inplace(
887
+ struct ggml_context * ctx,
888
+ struct ggml_tensor * a,
889
+ struct ggml_tensor * b);
890
+
891
+ GGML_API struct ggml_tensor * ggml_add_cast(
892
+ struct ggml_context * ctx,
893
+ struct ggml_tensor * a,
894
+ struct ggml_tensor * b,
895
+ enum ggml_type type);
896
+
897
+ GGML_API struct ggml_tensor * ggml_add1(
898
+ struct ggml_context * ctx,
899
+ struct ggml_tensor * a,
900
+ struct ggml_tensor * b);
901
+
902
+ GGML_API struct ggml_tensor * ggml_add1_inplace(
903
+ struct ggml_context * ctx,
904
+ struct ggml_tensor * a,
905
+ struct ggml_tensor * b);
906
+
907
+ // dst = a
908
+ // view(dst, nb1, nb2, nb3, offset) += b
909
+ // return dst
910
+ GGML_API struct ggml_tensor * ggml_acc(
911
+ struct ggml_context * ctx,
912
+ struct ggml_tensor * a,
913
+ struct ggml_tensor * b,
914
+ size_t nb1,
915
+ size_t nb2,
916
+ size_t nb3,
917
+ size_t offset);
918
+
919
+ GGML_API struct ggml_tensor * ggml_acc_inplace(
920
+ struct ggml_context * ctx,
921
+ struct ggml_tensor * a,
922
+ struct ggml_tensor * b,
923
+ size_t nb1,
924
+ size_t nb2,
925
+ size_t nb3,
926
+ size_t offset);
927
+
928
+ GGML_API struct ggml_tensor * ggml_sub(
929
+ struct ggml_context * ctx,
930
+ struct ggml_tensor * a,
931
+ struct ggml_tensor * b);
932
+
933
+ GGML_API struct ggml_tensor * ggml_sub_inplace(
934
+ struct ggml_context * ctx,
935
+ struct ggml_tensor * a,
936
+ struct ggml_tensor * b);
937
+
938
+ GGML_API struct ggml_tensor * ggml_mul(
939
+ struct ggml_context * ctx,
940
+ struct ggml_tensor * a,
941
+ struct ggml_tensor * b);
942
+
943
+ GGML_API struct ggml_tensor * ggml_mul_inplace(
944
+ struct ggml_context * ctx,
945
+ struct ggml_tensor * a,
946
+ struct ggml_tensor * b);
947
+
948
+ GGML_API struct ggml_tensor * ggml_div(
949
+ struct ggml_context * ctx,
950
+ struct ggml_tensor * a,
951
+ struct ggml_tensor * b);
952
+
953
+ GGML_API struct ggml_tensor * ggml_div_inplace(
954
+ struct ggml_context * ctx,
955
+ struct ggml_tensor * a,
956
+ struct ggml_tensor * b);
957
+
958
+ GGML_API struct ggml_tensor * ggml_sqr(
959
+ struct ggml_context * ctx,
960
+ struct ggml_tensor * a);
961
+
962
+ GGML_API struct ggml_tensor * ggml_sqr_inplace(
963
+ struct ggml_context * ctx,
964
+ struct ggml_tensor * a);
965
+
966
+ GGML_API struct ggml_tensor * ggml_sqrt(
967
+ struct ggml_context * ctx,
968
+ struct ggml_tensor * a);
969
+
970
+ GGML_API struct ggml_tensor * ggml_sqrt_inplace(
971
+ struct ggml_context * ctx,
972
+ struct ggml_tensor * a);
973
+
974
+ GGML_API struct ggml_tensor * ggml_log(
975
+ struct ggml_context * ctx,
976
+ struct ggml_tensor * a);
977
+
978
+ GGML_API struct ggml_tensor * ggml_log_inplace(
979
+ struct ggml_context * ctx,
980
+ struct ggml_tensor * a);
981
+
982
+ GGML_API struct ggml_tensor * ggml_sin(
983
+ struct ggml_context * ctx,
984
+ struct ggml_tensor * a);
985
+
986
+ GGML_API struct ggml_tensor * ggml_sin_inplace(
987
+ struct ggml_context * ctx,
988
+ struct ggml_tensor * a);
989
+
990
+ GGML_API struct ggml_tensor * ggml_cos(
991
+ struct ggml_context * ctx,
992
+ struct ggml_tensor * a);
993
+
994
+ GGML_API struct ggml_tensor * ggml_cos_inplace(
995
+ struct ggml_context * ctx,
996
+ struct ggml_tensor * a);
997
+
998
+ // return scalar
999
+ GGML_API struct ggml_tensor * ggml_sum(
1000
+ struct ggml_context * ctx,
1001
+ struct ggml_tensor * a);
1002
+
1003
+ // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
1004
+ GGML_API struct ggml_tensor * ggml_sum_rows(
1005
+ struct ggml_context * ctx,
1006
+ struct ggml_tensor * a);
1007
+
1008
+ // mean along rows
1009
+ GGML_API struct ggml_tensor * ggml_mean(
1010
+ struct ggml_context * ctx,
1011
+ struct ggml_tensor * a);
1012
+
1013
+ // argmax along rows
1014
+ GGML_API struct ggml_tensor * ggml_argmax(
1015
+ struct ggml_context * ctx,
1016
+ struct ggml_tensor * a);
1017
+
1018
+ // if a is the same shape as b, and a is not parameter, return a
1019
+ // otherwise, return a new tensor: repeat(a) to fit in b
1020
+ GGML_API struct ggml_tensor * ggml_repeat(
1021
+ struct ggml_context * ctx,
1022
+ struct ggml_tensor * a,
1023
+ struct ggml_tensor * b);
1024
+
1025
+ // sums repetitions in a into shape of b
1026
+ GGML_API struct ggml_tensor * ggml_repeat_back(
1027
+ struct ggml_context * ctx,
1028
+ struct ggml_tensor * a,
1029
+ struct ggml_tensor * b);
1030
+
1031
+ // concat a and b along dim
1032
+ // used in stable-diffusion
1033
+ GGML_API struct ggml_tensor * ggml_concat(
1034
+ struct ggml_context * ctx,
1035
+ struct ggml_tensor * a,
1036
+ struct ggml_tensor * b,
1037
+ int dim);
1038
+
1039
+ GGML_API struct ggml_tensor * ggml_abs(
1040
+ struct ggml_context * ctx,
1041
+ struct ggml_tensor * a);
1042
+
1043
+ GGML_API struct ggml_tensor * ggml_abs_inplace(
1044
+ struct ggml_context * ctx,
1045
+ struct ggml_tensor * a);
1046
+
1047
+ GGML_API struct ggml_tensor * ggml_sgn(
1048
+ struct ggml_context * ctx,
1049
+ struct ggml_tensor * a);
1050
+
1051
+ GGML_API struct ggml_tensor * ggml_sgn_inplace(
1052
+ struct ggml_context * ctx,
1053
+ struct ggml_tensor * a);
1054
+
1055
+ GGML_API struct ggml_tensor * ggml_neg(
1056
+ struct ggml_context * ctx,
1057
+ struct ggml_tensor * a);
1058
+
1059
+ GGML_API struct ggml_tensor * ggml_neg_inplace(
1060
+ struct ggml_context * ctx,
1061
+ struct ggml_tensor * a);
1062
+
1063
+ GGML_API struct ggml_tensor * ggml_step(
1064
+ struct ggml_context * ctx,
1065
+ struct ggml_tensor * a);
1066
+
1067
+ GGML_API struct ggml_tensor * ggml_step_inplace(
1068
+ struct ggml_context * ctx,
1069
+ struct ggml_tensor * a);
1070
+
1071
+ GGML_API struct ggml_tensor * ggml_tanh(
1072
+ struct ggml_context * ctx,
1073
+ struct ggml_tensor * a);
1074
+
1075
+ GGML_API struct ggml_tensor * ggml_tanh_inplace(
1076
+ struct ggml_context * ctx,
1077
+ struct ggml_tensor * a);
1078
+
1079
+ GGML_API struct ggml_tensor * ggml_elu(
1080
+ struct ggml_context * ctx,
1081
+ struct ggml_tensor * a);
1082
+
1083
+ GGML_API struct ggml_tensor * ggml_elu_inplace(
1084
+ struct ggml_context * ctx,
1085
+ struct ggml_tensor * a);
1086
+
1087
+ GGML_API struct ggml_tensor * ggml_relu(
1088
+ struct ggml_context * ctx,
1089
+ struct ggml_tensor * a);
1090
+
1091
+ GGML_API struct ggml_tensor * ggml_leaky_relu(
1092
+ struct ggml_context * ctx,
1093
+ struct ggml_tensor * a, float negative_slope, bool inplace);
1094
+
1095
+ GGML_API struct ggml_tensor * ggml_relu_inplace(
1096
+ struct ggml_context * ctx,
1097
+ struct ggml_tensor * a);
1098
+
1099
+ GGML_API struct ggml_tensor * ggml_sigmoid(
1100
+ struct ggml_context * ctx,
1101
+ struct ggml_tensor * a);
1102
+
1103
+ GGML_API struct ggml_tensor * ggml_sigmoid_inplace(
1104
+ struct ggml_context * ctx,
1105
+ struct ggml_tensor * a);
1106
+
1107
+ GGML_API struct ggml_tensor * ggml_gelu(
1108
+ struct ggml_context * ctx,
1109
+ struct ggml_tensor * a);
1110
+
1111
+ GGML_API struct ggml_tensor * ggml_gelu_inplace(
1112
+ struct ggml_context * ctx,
1113
+ struct ggml_tensor * a);
1114
+
1115
+ GGML_API struct ggml_tensor * ggml_gelu_quick(
1116
+ struct ggml_context * ctx,
1117
+ struct ggml_tensor * a);
1118
+
1119
+ GGML_API struct ggml_tensor * ggml_gelu_quick_inplace(
1120
+ struct ggml_context * ctx,
1121
+ struct ggml_tensor * a);
1122
+
1123
+ GGML_API struct ggml_tensor * ggml_silu(
1124
+ struct ggml_context * ctx,
1125
+ struct ggml_tensor * a);
1126
+
1127
+ GGML_API struct ggml_tensor * ggml_silu_inplace(
1128
+ struct ggml_context * ctx,
1129
+ struct ggml_tensor * a);
1130
+
1131
+ // a - x
1132
+ // b - dy
1133
+ GGML_API struct ggml_tensor * ggml_silu_back(
1134
+ struct ggml_context * ctx,
1135
+ struct ggml_tensor * a,
1136
+ struct ggml_tensor * b);
1137
+
1138
+ // hardswish(x) = x * relu6(x + 3) / 6
1139
+ GGML_API struct ggml_tensor * ggml_hardswish(
1140
+ struct ggml_context * ctx,
1141
+ struct ggml_tensor * a);
1142
+
1143
+ // hardsigmoid(x) = relu6(x + 3) / 6
1144
+ GGML_API struct ggml_tensor * ggml_hardsigmoid(
1145
+ struct ggml_context * ctx,
1146
+ struct ggml_tensor * a);
1147
+
1148
+ GGML_API struct ggml_tensor * ggml_exp(
1149
+ struct ggml_context * ctx,
1150
+ struct ggml_tensor * a);
1151
+
1152
+ GGML_API struct ggml_tensor * ggml_exp_inplace(
1153
+ struct ggml_context * ctx,
1154
+ struct ggml_tensor * a);
1155
+
1156
+ // normalize along rows
1157
+ GGML_API struct ggml_tensor * ggml_norm(
1158
+ struct ggml_context * ctx,
1159
+ struct ggml_tensor * a,
1160
+ float eps);
1161
+
1162
+ GGML_API struct ggml_tensor * ggml_norm_inplace(
1163
+ struct ggml_context * ctx,
1164
+ struct ggml_tensor * a,
1165
+ float eps);
1166
+
1167
+ GGML_API struct ggml_tensor * ggml_rms_norm(
1168
+ struct ggml_context * ctx,
1169
+ struct ggml_tensor * a,
1170
+ float eps);
1171
+
1172
+ GGML_API struct ggml_tensor * ggml_rms_norm_inplace(
1173
+ struct ggml_context * ctx,
1174
+ struct ggml_tensor * a,
1175
+ float eps);
1176
+
1177
+ // group normalize along ne0*ne1*n_groups
1178
+ // used in stable-diffusion
1179
+ GGML_API struct ggml_tensor * ggml_group_norm(
1180
+ struct ggml_context * ctx,
1181
+ struct ggml_tensor * a,
1182
+ int n_groups,
1183
+ float eps);
1184
+
1185
+ GGML_API struct ggml_tensor * ggml_group_norm_inplace(
1186
+ struct ggml_context * ctx,
1187
+ struct ggml_tensor * a,
1188
+ int n_groups,
1189
+ float eps);
1190
+
1191
+ // a - x
1192
+ // b - dy
1193
+ GGML_API struct ggml_tensor * ggml_rms_norm_back(
1194
+ struct ggml_context * ctx,
1195
+ struct ggml_tensor * a,
1196
+ struct ggml_tensor * b,
1197
+ float eps);
1198
+
1199
+ // A: k columns, n rows => [ne03, ne02, n, k]
1200
+ // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
1201
+ // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
1202
+ GGML_API struct ggml_tensor * ggml_mul_mat(
1203
+ struct ggml_context * ctx,
1204
+ struct ggml_tensor * a,
1205
+ struct ggml_tensor * b);
1206
+
1207
+ // change the precision of a matrix multiplication
1208
+ // set to GGML_PREC_F32 for higher precision (useful for phi-2)
1209
+ GGML_API void ggml_mul_mat_set_prec(
1210
+ struct ggml_tensor * a,
1211
+ enum ggml_prec prec);
1212
+
1213
+ // indirect matrix multiplication
1214
+ GGML_API struct ggml_tensor * ggml_mul_mat_id(
1215
+ struct ggml_context * ctx,
1216
+ struct ggml_tensor * as,
1217
+ struct ggml_tensor * b,
1218
+ struct ggml_tensor * ids);
1219
+
1220
+ // A: m columns, n rows,
1221
+ // B: p columns, n rows,
1222
+ // result is m columns, p rows
1223
+ GGML_API struct ggml_tensor * ggml_out_prod(
1224
+ struct ggml_context * ctx,
1225
+ struct ggml_tensor * a,
1226
+ struct ggml_tensor * b);
1227
+
1228
+ //
1229
+ // operations on tensors without backpropagation
1230
+ //
1231
+
1232
+ GGML_API struct ggml_tensor * ggml_scale(
1233
+ struct ggml_context * ctx,
1234
+ struct ggml_tensor * a,
1235
+ float s);
1236
+
1237
+ // in-place, returns view(a)
1238
+ GGML_API struct ggml_tensor * ggml_scale_inplace(
1239
+ struct ggml_context * ctx,
1240
+ struct ggml_tensor * a,
1241
+ float s);
1242
+
1243
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1244
+ GGML_API struct ggml_tensor * ggml_set(
1245
+ struct ggml_context * ctx,
1246
+ struct ggml_tensor * a,
1247
+ struct ggml_tensor * b,
1248
+ size_t nb1,
1249
+ size_t nb2,
1250
+ size_t nb3,
1251
+ size_t offset); // in bytes
1252
+
1253
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1254
+ GGML_API struct ggml_tensor * ggml_set_inplace(
1255
+ struct ggml_context * ctx,
1256
+ struct ggml_tensor * a,
1257
+ struct ggml_tensor * b,
1258
+ size_t nb1,
1259
+ size_t nb2,
1260
+ size_t nb3,
1261
+ size_t offset); // in bytes
1262
+
1263
+ GGML_API struct ggml_tensor * ggml_set_1d(
1264
+ struct ggml_context * ctx,
1265
+ struct ggml_tensor * a,
1266
+ struct ggml_tensor * b,
1267
+ size_t offset); // in bytes
1268
+
1269
+ GGML_API struct ggml_tensor * ggml_set_1d_inplace(
1270
+ struct ggml_context * ctx,
1271
+ struct ggml_tensor * a,
1272
+ struct ggml_tensor * b,
1273
+ size_t offset); // in bytes
1274
+
1275
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1276
+ GGML_API struct ggml_tensor * ggml_set_2d(
1277
+ struct ggml_context * ctx,
1278
+ struct ggml_tensor * a,
1279
+ struct ggml_tensor * b,
1280
+ size_t nb1,
1281
+ size_t offset); // in bytes
1282
+
1283
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1284
+ GGML_API struct ggml_tensor * ggml_set_2d_inplace(
1285
+ struct ggml_context * ctx,
1286
+ struct ggml_tensor * a,
1287
+ struct ggml_tensor * b,
1288
+ size_t nb1,
1289
+ size_t offset); // in bytes
1290
+
1291
+ // a -> b, return view(b)
1292
+ GGML_API struct ggml_tensor * ggml_cpy(
1293
+ struct ggml_context * ctx,
1294
+ struct ggml_tensor * a,
1295
+ struct ggml_tensor * b);
1296
+
1297
+ GGML_API struct ggml_tensor * ggml_cast(
1298
+ struct ggml_context * ctx,
1299
+ struct ggml_tensor * a,
1300
+ enum ggml_type type);
1301
+
1302
+ // make contiguous
1303
+ GGML_API struct ggml_tensor * ggml_cont(
1304
+ struct ggml_context * ctx,
1305
+ struct ggml_tensor * a);
1306
+
1307
+ // make contiguous, with new shape
1308
+ GGML_API struct ggml_tensor * ggml_cont_1d(
1309
+ struct ggml_context * ctx,
1310
+ struct ggml_tensor * a,
1311
+ int64_t ne0);
1312
+
1313
+ GGML_API struct ggml_tensor * ggml_cont_2d(
1314
+ struct ggml_context * ctx,
1315
+ struct ggml_tensor * a,
1316
+ int64_t ne0,
1317
+ int64_t ne1);
1318
+
1319
+ GGML_API struct ggml_tensor * ggml_cont_3d(
1320
+ struct ggml_context * ctx,
1321
+ struct ggml_tensor * a,
1322
+ int64_t ne0,
1323
+ int64_t ne1,
1324
+ int64_t ne2);
1325
+
1326
+ GGML_API struct ggml_tensor * ggml_cont_4d(
1327
+ struct ggml_context * ctx,
1328
+ struct ggml_tensor * a,
1329
+ int64_t ne0,
1330
+ int64_t ne1,
1331
+ int64_t ne2,
1332
+ int64_t ne3);
1333
+
1334
+ // return view(a), b specifies the new shape
1335
+ // TODO: when we start computing gradient, make a copy instead of view
1336
+ GGML_API struct ggml_tensor * ggml_reshape(
1337
+ struct ggml_context * ctx,
1338
+ struct ggml_tensor * a,
1339
+ struct ggml_tensor * b);
1340
+
1341
+ // return view(a)
1342
+ // TODO: when we start computing gradient, make a copy instead of view
1343
+ GGML_API struct ggml_tensor * ggml_reshape_1d(
1344
+ struct ggml_context * ctx,
1345
+ struct ggml_tensor * a,
1346
+ int64_t ne0);
1347
+
1348
+ GGML_API struct ggml_tensor * ggml_reshape_2d(
1349
+ struct ggml_context * ctx,
1350
+ struct ggml_tensor * a,
1351
+ int64_t ne0,
1352
+ int64_t ne1);
1353
+
1354
+ // return view(a)
1355
+ // TODO: when we start computing gradient, make a copy instead of view
1356
+ GGML_API struct ggml_tensor * ggml_reshape_3d(
1357
+ struct ggml_context * ctx,
1358
+ struct ggml_tensor * a,
1359
+ int64_t ne0,
1360
+ int64_t ne1,
1361
+ int64_t ne2);
1362
+
1363
+ GGML_API struct ggml_tensor * ggml_reshape_4d(
1364
+ struct ggml_context * ctx,
1365
+ struct ggml_tensor * a,
1366
+ int64_t ne0,
1367
+ int64_t ne1,
1368
+ int64_t ne2,
1369
+ int64_t ne3);
1370
+
1371
+ // offset in bytes
1372
+ GGML_API struct ggml_tensor * ggml_view_1d(
1373
+ struct ggml_context * ctx,
1374
+ struct ggml_tensor * a,
1375
+ int64_t ne0,
1376
+ size_t offset);
1377
+
1378
+ GGML_API struct ggml_tensor * ggml_view_2d(
1379
+ struct ggml_context * ctx,
1380
+ struct ggml_tensor * a,
1381
+ int64_t ne0,
1382
+ int64_t ne1,
1383
+ size_t nb1, // row stride in bytes
1384
+ size_t offset);
1385
+
1386
+ GGML_API struct ggml_tensor * ggml_view_3d(
1387
+ struct ggml_context * ctx,
1388
+ struct ggml_tensor * a,
1389
+ int64_t ne0,
1390
+ int64_t ne1,
1391
+ int64_t ne2,
1392
+ size_t nb1, // row stride in bytes
1393
+ size_t nb2, // slice stride in bytes
1394
+ size_t offset);
1395
+
1396
+ GGML_API struct ggml_tensor * ggml_view_4d(
1397
+ struct ggml_context * ctx,
1398
+ struct ggml_tensor * a,
1399
+ int64_t ne0,
1400
+ int64_t ne1,
1401
+ int64_t ne2,
1402
+ int64_t ne3,
1403
+ size_t nb1, // row stride in bytes
1404
+ size_t nb2, // slice stride in bytes
1405
+ size_t nb3,
1406
+ size_t offset);
1407
+
1408
+ GGML_API struct ggml_tensor * ggml_permute(
1409
+ struct ggml_context * ctx,
1410
+ struct ggml_tensor * a,
1411
+ int axis0,
1412
+ int axis1,
1413
+ int axis2,
1414
+ int axis3);
1415
+
1416
+ // alias for ggml_permute(ctx, a, 1, 0, 2, 3)
1417
+ GGML_API struct ggml_tensor * ggml_transpose(
1418
+ struct ggml_context * ctx,
1419
+ struct ggml_tensor * a);
1420
+
1421
+ // supports 3D: a->ne[2] == b->ne[1]
1422
+ GGML_API struct ggml_tensor * ggml_get_rows(
1423
+ struct ggml_context * ctx,
1424
+ struct ggml_tensor * a, // data
1425
+ struct ggml_tensor * b); // row indices
1426
+
1427
+ GGML_API struct ggml_tensor * ggml_get_rows_back(
1428
+ struct ggml_context * ctx,
1429
+ struct ggml_tensor * a, // gradients of ggml_get_rows result
1430
+ struct ggml_tensor * b, // row indices
1431
+ struct ggml_tensor * c); // data for ggml_get_rows, only used for its shape
1432
+
1433
+ GGML_API struct ggml_tensor * ggml_diag(
1434
+ struct ggml_context * ctx,
1435
+ struct ggml_tensor * a);
1436
+
1437
+ // set elements above the diagonal to -INF
1438
+ GGML_API struct ggml_tensor * ggml_diag_mask_inf(
1439
+ struct ggml_context * ctx,
1440
+ struct ggml_tensor * a,
1441
+ int n_past);
1442
+
1443
+ // in-place, returns view(a)
1444
+ GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace(
1445
+ struct ggml_context * ctx,
1446
+ struct ggml_tensor * a,
1447
+ int n_past);
1448
+
1449
+ // set elements above the diagonal to 0
1450
+ GGML_API struct ggml_tensor * ggml_diag_mask_zero(
1451
+ struct ggml_context * ctx,
1452
+ struct ggml_tensor * a,
1453
+ int n_past);
1454
+
1455
+ // in-place, returns view(a)
1456
+ GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace(
1457
+ struct ggml_context * ctx,
1458
+ struct ggml_tensor * a,
1459
+ int n_past);
1460
+
1461
+ GGML_API struct ggml_tensor * ggml_soft_max(
1462
+ struct ggml_context * ctx,
1463
+ struct ggml_tensor * a);
1464
+
1465
+ // in-place, returns view(a)
1466
+ GGML_API struct ggml_tensor * ggml_soft_max_inplace(
1467
+ struct ggml_context * ctx,
1468
+ struct ggml_tensor * a);
1469
+
1470
+ // fused soft_max(a*scale + mask*(ALiBi slope))
1471
+ // mask is optional
1472
+ // max_bias = 0.0f for no ALiBi
1473
+ GGML_API struct ggml_tensor * ggml_soft_max_ext(
1474
+ struct ggml_context * ctx,
1475
+ struct ggml_tensor * a,
1476
+ struct ggml_tensor * mask,
1477
+ float scale,
1478
+ float max_bias);
1479
+
1480
+ GGML_API struct ggml_tensor * ggml_soft_max_back(
1481
+ struct ggml_context * ctx,
1482
+ struct ggml_tensor * a,
1483
+ struct ggml_tensor * b);
1484
+
1485
+ // in-place, returns view(a)
1486
+ GGML_API struct ggml_tensor * ggml_soft_max_back_inplace(
1487
+ struct ggml_context * ctx,
1488
+ struct ggml_tensor * a,
1489
+ struct ggml_tensor * b);
1490
+
1491
+ // rotary position embedding
1492
+ // if (mode & 1) - skip n_past elements (NOT SUPPORTED)
1493
+ // if (mode & GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
1494
+ //
1495
+ // b is an int32 vector with size a->ne[2], it contains the positions
1496
+ GGML_API struct ggml_tensor * ggml_rope(
1497
+ struct ggml_context * ctx,
1498
+ struct ggml_tensor * a,
1499
+ struct ggml_tensor * b,
1500
+ int n_dims,
1501
+ int mode);
1502
+
1503
+ // in-place, returns view(a)
1504
+ GGML_API struct ggml_tensor * ggml_rope_inplace(
1505
+ struct ggml_context * ctx,
1506
+ struct ggml_tensor * a,
1507
+ struct ggml_tensor * b,
1508
+ int n_dims,
1509
+ int mode);
1510
+
1511
+ // custom RoPE
1512
+ // c is freq factors (e.g. phi3-128k), (optional)
1513
+ GGML_API struct ggml_tensor * ggml_rope_ext(
1514
+ struct ggml_context * ctx,
1515
+ struct ggml_tensor * a,
1516
+ struct ggml_tensor * b,
1517
+ struct ggml_tensor * c,
1518
+ int n_dims,
1519
+ int mode,
1520
+ int n_ctx_orig,
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
+
1528
+ // in-place, returns view(a)
1529
+ GGML_API struct ggml_tensor * ggml_rope_ext_inplace(
1530
+ struct ggml_context * ctx,
1531
+ struct ggml_tensor * a,
1532
+ struct ggml_tensor * b,
1533
+ struct ggml_tensor * c,
1534
+ int n_dims,
1535
+ int mode,
1536
+ int n_ctx_orig,
1537
+ float freq_base,
1538
+ float freq_scale,
1539
+ float ext_factor,
1540
+ float attn_factor,
1541
+ float beta_fast,
1542
+ float beta_slow);
1543
+
1544
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom(
1545
+ struct ggml_context * ctx,
1546
+ struct ggml_tensor * a,
1547
+ struct ggml_tensor * b,
1548
+ int n_dims,
1549
+ int mode,
1550
+ int n_ctx_orig,
1551
+ float freq_base,
1552
+ float freq_scale,
1553
+ float ext_factor,
1554
+ float attn_factor,
1555
+ float beta_fast,
1556
+ float beta_slow),
1557
+ "use ggml_rope_ext instead");
1558
+
1559
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom_inplace(
1560
+ struct ggml_context * ctx,
1561
+ struct ggml_tensor * a,
1562
+ struct ggml_tensor * b,
1563
+ int n_dims,
1564
+ int mode,
1565
+ int n_ctx_orig,
1566
+ float freq_base,
1567
+ float freq_scale,
1568
+ float ext_factor,
1569
+ float attn_factor,
1570
+ float beta_fast,
1571
+ float beta_slow),
1572
+ "use ggml_rope_ext_inplace instead");
1573
+
1574
+ // compute correction dims for YaRN RoPE scaling
1575
+ GGML_CALL void ggml_rope_yarn_corr_dims(
1576
+ int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]);
1577
+
1578
+ // rotary position embedding backward, i.e compute dx from dy
1579
+ // a - dy
1580
+ GGML_API struct ggml_tensor * ggml_rope_back(
1581
+ struct ggml_context * ctx,
1582
+ struct ggml_tensor * a, // gradients of ggml_rope result
1583
+ struct ggml_tensor * b, // positions
1584
+ struct ggml_tensor * c, // freq factors
1585
+ int n_dims,
1586
+ int mode,
1587
+ int n_ctx_orig,
1588
+ float freq_base,
1589
+ float freq_scale,
1590
+ float ext_factor,
1591
+ float attn_factor,
1592
+ float beta_fast,
1593
+ float beta_slow);
1594
+
1595
+ // clamp
1596
+ // in-place, returns view(a)
1597
+ GGML_API struct ggml_tensor * ggml_clamp(
1598
+ struct ggml_context * ctx,
1599
+ struct ggml_tensor * a,
1600
+ float min,
1601
+ float max);
1602
+
1603
+ // im2col
1604
+ // converts data into a format that effectively results in a convolution when combined with matrix multiplication
1605
+ GGML_API struct ggml_tensor * ggml_im2col(
1606
+ struct ggml_context * ctx,
1607
+ struct ggml_tensor * a, // convolution kernel
1608
+ struct ggml_tensor * b, // data
1609
+ int s0, // stride dimension 0
1610
+ int s1, // stride dimension 1
1611
+ int p0, // padding dimension 0
1612
+ int p1, // padding dimension 1
1613
+ int d0, // dilation dimension 0
1614
+ int d1, // dilation dimension 1
1615
+ bool is_2D,
1616
+ enum ggml_type dst_type);
1617
+
1618
+ GGML_API struct ggml_tensor * ggml_im2col_back(
1619
+ struct ggml_context * ctx,
1620
+ struct ggml_tensor * a, // convolution kernel
1621
+ struct ggml_tensor * b, // gradient of im2col output
1622
+ int64_t * ne, // shape of im2col input
1623
+ int s0, // stride dimension 0
1624
+ int s1, // stride dimension 1
1625
+ int p0, // padding dimension 0
1626
+ int p1, // padding dimension 1
1627
+ int d0, // dilation dimension 0
1628
+ int d1, // dilation dimension 1
1629
+ bool is_2D);
1630
+
1631
+ GGML_API struct ggml_tensor * ggml_conv_depthwise_2d(
1632
+ struct ggml_context * ctx,
1633
+ struct ggml_tensor * a, // convolution kernel
1634
+ struct ggml_tensor * b, // data
1635
+ int s0, // stride dimension 0
1636
+ int s1, // stride dimension 1
1637
+ int p0, // padding dimension 0
1638
+ int p1, // padding dimension 1
1639
+ int d0, // dilation dimension 0
1640
+ int d1); // dilation dimension 1
1641
+
1642
+ GGML_API struct ggml_tensor * ggml_conv_1d(
1643
+ struct ggml_context * ctx,
1644
+ struct ggml_tensor * a, // convolution kernel
1645
+ struct ggml_tensor * b, // data
1646
+ int s0, // stride
1647
+ int p0, // padding
1648
+ int d0); // dilation
1649
+
1650
+ // conv_1d with padding = half
1651
+ // alias for ggml_conv_1d(a, b, s, a->ne[0]/2, d)
1652
+ GGML_API struct ggml_tensor* ggml_conv_1d_ph(
1653
+ struct ggml_context * ctx,
1654
+ struct ggml_tensor * a, // convolution kernel
1655
+ struct ggml_tensor * b, // data
1656
+ int s, // stride
1657
+ int d); // dilation
1658
+
1659
+ GGML_API struct ggml_tensor * ggml_conv_transpose_1d(
1660
+ struct ggml_context * ctx,
1661
+ struct ggml_tensor * a, // convolution kernel
1662
+ struct ggml_tensor * b, // data
1663
+ int s0, // stride
1664
+ int p0, // padding
1665
+ int d0); // dilation
1666
+
1667
+ GGML_API struct ggml_tensor * ggml_conv_2d(
1668
+ struct ggml_context * ctx,
1669
+ struct ggml_tensor * a, // convolution kernel
1670
+ struct ggml_tensor * b, // data
1671
+ int s0, // stride dimension 0
1672
+ int s1, // stride dimension 1
1673
+ int p0, // padding dimension 0
1674
+ int p1, // padding dimension 1
1675
+ int d0, // dilation dimension 0
1676
+ int d1); // dilation dimension 1
1677
+
1678
+
1679
+ // kernel size is a->ne[0] x a->ne[1]
1680
+ // stride is equal to kernel size
1681
+ // padding is zero
1682
+ // example:
1683
+ // a: 16 16 3 768
1684
+ // b: 1024 1024 3 1
1685
+ // res: 64 64 768 1
1686
+ // used in sam
1687
+ GGML_API struct ggml_tensor * ggml_conv_2d_sk_p0(
1688
+ struct ggml_context * ctx,
1689
+ struct ggml_tensor * a,
1690
+ struct ggml_tensor * b);
1691
+
1692
+ // kernel size is a->ne[0] x a->ne[1]
1693
+ // stride is 1
1694
+ // padding is half
1695
+ // example:
1696
+ // a: 3 3 256 256
1697
+ // b: 64 64 256 1
1698
+ // res: 64 64 256 1
1699
+ // used in sam
1700
+ GGML_API struct ggml_tensor * ggml_conv_2d_s1_ph(
1701
+ struct ggml_context * ctx,
1702
+ struct ggml_tensor * a,
1703
+ struct ggml_tensor * b);
1704
+
1705
+ GGML_API struct ggml_tensor * ggml_conv_transpose_2d_p0(
1706
+ struct ggml_context * ctx,
1707
+ struct ggml_tensor * a,
1708
+ struct ggml_tensor * b,
1709
+ int stride);
1710
+
1711
+ enum ggml_op_pool {
1712
+ GGML_OP_POOL_MAX,
1713
+ GGML_OP_POOL_AVG,
1714
+ GGML_OP_POOL_COUNT,
1715
+ };
1716
+
1717
+ GGML_API struct ggml_tensor * ggml_pool_1d(
1718
+ struct ggml_context * ctx,
1719
+ struct ggml_tensor * a,
1720
+ enum ggml_op_pool op,
1721
+ int k0, // kernel size
1722
+ int s0, // stride
1723
+ int p0); // padding
1724
+
1725
+ // the result will have 2*p0 padding for the first dimension
1726
+ // and 2*p1 padding for the second dimension
1727
+ GGML_API struct ggml_tensor * ggml_pool_2d(
1728
+ struct ggml_context * ctx,
1729
+ struct ggml_tensor * a,
1730
+ enum ggml_op_pool op,
1731
+ int k0,
1732
+ int k1,
1733
+ int s0,
1734
+ int s1,
1735
+ float p0,
1736
+ float p1);
1737
+
1738
+ GGML_API struct ggml_tensor * ggml_pool_2d_back(
1739
+ struct ggml_context * ctx,
1740
+ struct ggml_tensor * a,
1741
+ struct ggml_tensor * af, // "a"/input used in forward pass
1742
+ enum ggml_op_pool op,
1743
+ int k0,
1744
+ int k1,
1745
+ int s0,
1746
+ int s1,
1747
+ float p0,
1748
+ float p1);
1749
+
1750
+ // nearest interpolate
1751
+ // multiplies ne0 and ne1 by scale factor
1752
+ // used in stable-diffusion
1753
+ GGML_API struct ggml_tensor * ggml_upscale(
1754
+ struct ggml_context * ctx,
1755
+ struct ggml_tensor * a,
1756
+ int scale_factor);
1757
+
1758
+ // nearest interpolate
1759
+ // nearest interpolate to specified dimensions
1760
+ // used in tortoise.cpp
1761
+ GGML_API struct ggml_tensor * ggml_upscale_ext(
1762
+ struct ggml_context * ctx,
1763
+ struct ggml_tensor * a,
1764
+ int ne0,
1765
+ int ne1,
1766
+ int ne2,
1767
+ int ne3);
1768
+
1769
+ // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
1770
+ GGML_API struct ggml_tensor * ggml_pad(
1771
+ struct ggml_context * ctx,
1772
+ struct ggml_tensor * a,
1773
+ int p0,
1774
+ int p1,
1775
+ int p2,
1776
+ int p3);
1777
+
1778
+ // unpad each dimension: [x, ..., x, y, ..., y] -> [x, ..., x]
1779
+ GGML_API struct ggml_tensor * ggml_unpad(
1780
+ struct ggml_context * ctx,
1781
+ struct ggml_tensor * a,
1782
+ int p0,
1783
+ int p1,
1784
+ int p2,
1785
+ int p3);
1786
+
1787
+ // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
1788
+ // timesteps: [N,]
1789
+ // return: [N, dim]
1790
+ GGML_API struct ggml_tensor * ggml_timestep_embedding(
1791
+ struct ggml_context * ctx,
1792
+ struct ggml_tensor * timesteps,
1793
+ int dim,
1794
+ int max_period);
1795
+
1796
+ // q: [n_embd, n_batch, n_head, 1]
1797
+ // k: [n_embd, n_kv, n_head_kv, 1]
1798
+ // v: [n_embd, n_kv, n_head_kv, 1]
1799
+ GGML_API struct ggml_tensor * ggml_bigdl_fp16_sdp(
1800
+ struct ggml_context * ctx,
1801
+ struct ggml_tensor * q,
1802
+ struct ggml_tensor * k,
1803
+ struct ggml_tensor * v,
1804
+ struct ggml_tensor * mask,
1805
+ float kq_scale,
1806
+ uint32_t real_kv_len,
1807
+ bool casual);
1808
+
1809
+ GGML_API struct ggml_tensor * ggml_bigdl_qkv_fusion(
1810
+ struct ggml_context * ctx,
1811
+ struct ggml_tensor * cur,
1812
+ struct ggml_tensor * wq,
1813
+ struct ggml_tensor * wk,
1814
+ struct ggml_tensor * wv,
1815
+ struct ggml_tensor * bq,
1816
+ struct ggml_tensor * bk,
1817
+ struct ggml_tensor * bv,
1818
+ struct ggml_tensor * inp_pos,
1819
+ struct ggml_tensor * k_cache,
1820
+ struct ggml_tensor * v_cache,
1821
+ int rope_type,
1822
+ float freq_base,
1823
+ float freq_scale,
1824
+ float attn_factor,
1825
+ uint32_t kv_head,
1826
+ uint32_t n_embd_head,
1827
+ uint32_t n_head,
1828
+ uint32_t n_head_kv);
1829
+
1830
+ GGML_API struct ggml_tensor * ggml_bigdl_wqkv_fusion(
1831
+ struct ggml_context * ctx,
1832
+ struct ggml_tensor * cur,
1833
+ struct ggml_tensor * wqkv,
1834
+ struct ggml_tensor * bqkv,
1835
+ struct ggml_tensor * inp_pos,
1836
+ struct ggml_tensor * k_cache,
1837
+ struct ggml_tensor * v_cache,
1838
+ float freq_base,
1839
+ float freq_scale,
1840
+ float attn_factor,
1841
+ uint32_t kv_head,
1842
+ uint32_t n_embd_head,
1843
+ uint32_t n_head,
1844
+ uint32_t n_head_kv);
1845
+
1846
+ GGML_API struct ggml_tensor * ggml_bigdl_mlp_fusion(
1847
+ struct ggml_context * ctx,
1848
+ struct ggml_tensor * cur,
1849
+ struct ggml_tensor * up,
1850
+ struct ggml_tensor * up_b,
1851
+ struct ggml_tensor * gate,
1852
+ struct ggml_tensor * gate_b,
1853
+ int type_op);
1854
+
1855
+ GGML_API struct ggml_tensor * ggml_bigdl_mlp_fusion_upgate(
1856
+ struct ggml_context * ctx,
1857
+ struct ggml_tensor * cur,
1858
+ struct ggml_tensor * upgate,
1859
+ int type_op);
1860
+
1861
+ GGML_API struct ggml_tensor * ggml_bigdl_quantize_kv(
1862
+ struct ggml_context * ctx,
1863
+ struct ggml_tensor * k_cur,
1864
+ struct ggml_tensor * k_dst,
1865
+ struct ggml_tensor * v_cur,
1866
+ struct ggml_tensor * v_dst,
1867
+ uint32_t head_dim,
1868
+ uint32_t n_blocks);
1869
+
1870
+ GGML_API struct ggml_tensor * ggml_bigdl_dequantize_kv(
1871
+ struct ggml_context * ctx,
1872
+ struct ggml_tensor * cur,
1873
+ uint32_t is_k);
1874
+
1875
+ // sort rows
1876
+ enum ggml_sort_order {
1877
+ GGML_SORT_ORDER_ASC,
1878
+ GGML_SORT_ORDER_DESC,
1879
+ };
1880
+
1881
+ GGML_API struct ggml_tensor * ggml_argsort(
1882
+ struct ggml_context * ctx,
1883
+ struct ggml_tensor * a,
1884
+ enum ggml_sort_order order);
1885
+
1886
+ GGML_API struct ggml_tensor * ggml_arange(
1887
+ struct ggml_context * ctx,
1888
+ float start,
1889
+ float stop,
1890
+ float step);
1891
+
1892
+ // top k elements per row
1893
+ GGML_API struct ggml_tensor * ggml_top_k(
1894
+ struct ggml_context * ctx,
1895
+ struct ggml_tensor * a,
1896
+ int k);
1897
+
1898
+ #define GGML_KQ_MASK_PAD 32
1899
+
1900
+ // q: [n_embd, n_batch, n_head, 1]
1901
+ // k: [n_embd, n_kv, n_head_kv, 1]
1902
+ // v: [n_embd, n_kv, n_head_kv, 1] !! not transposed !!
1903
+ // mask: [n_kv, n_batch_pad, 1, 1] !! n_batch_pad = GGML_PAD(n_batch, GGML_KQ_MASK_PAD) !!
1904
+ // res: [n_embd, n_head, n_batch, 1] !! permuted !!
1905
+ GGML_API struct ggml_tensor * ggml_flash_attn_ext(
1906
+ struct ggml_context * ctx,
1907
+ struct ggml_tensor * q,
1908
+ struct ggml_tensor * k,
1909
+ struct ggml_tensor * v,
1910
+ struct ggml_tensor * mask,
1911
+ float scale,
1912
+ float max_bias,
1913
+ float logit_softcap);
1914
+
1915
+ GGML_API void ggml_flash_attn_ext_set_prec(
1916
+ struct ggml_tensor * a,
1917
+ enum ggml_prec prec);
1918
+
1919
+ // TODO: needs to be adapted to ggml_flash_attn_ext
1920
+ GGML_API struct ggml_tensor * ggml_flash_attn_back(
1921
+ struct ggml_context * ctx,
1922
+ struct ggml_tensor * q,
1923
+ struct ggml_tensor * k,
1924
+ struct ggml_tensor * v,
1925
+ struct ggml_tensor * d,
1926
+ bool masked);
1927
+
1928
+ GGML_API struct ggml_tensor * ggml_ssm_conv(
1929
+ struct ggml_context * ctx,
1930
+ struct ggml_tensor * sx,
1931
+ struct ggml_tensor * c);
1932
+
1933
+ GGML_API struct ggml_tensor * ggml_ssm_scan(
1934
+ struct ggml_context * ctx,
1935
+ struct ggml_tensor * s,
1936
+ struct ggml_tensor * x,
1937
+ struct ggml_tensor * dt,
1938
+ struct ggml_tensor * A,
1939
+ struct ggml_tensor * B,
1940
+ struct ggml_tensor * C);
1941
+
1942
+ // partition into non-overlapping windows with padding if needed
1943
+ // example:
1944
+ // a: 768 64 64 1
1945
+ // w: 14
1946
+ // res: 768 14 14 25
1947
+ // used in sam
1948
+ GGML_API struct ggml_tensor * ggml_win_part(
1949
+ struct ggml_context * ctx,
1950
+ struct ggml_tensor * a,
1951
+ int w);
1952
+
1953
+ // reverse of ggml_win_part
1954
+ // used in sam
1955
+ GGML_API struct ggml_tensor * ggml_win_unpart(
1956
+ struct ggml_context * ctx,
1957
+ struct ggml_tensor * a,
1958
+ int w0,
1959
+ int h0,
1960
+ int w);
1961
+
1962
+ GGML_API struct ggml_tensor * ggml_unary(
1963
+ struct ggml_context * ctx,
1964
+ struct ggml_tensor * a,
1965
+ enum ggml_unary_op op);
1966
+
1967
+ GGML_API struct ggml_tensor * ggml_unary_inplace(
1968
+ struct ggml_context * ctx,
1969
+ struct ggml_tensor * a,
1970
+ enum ggml_unary_op op);
1971
+
1972
+ // used in sam
1973
+ GGML_API struct ggml_tensor * ggml_get_rel_pos(
1974
+ struct ggml_context * ctx,
1975
+ struct ggml_tensor * a,
1976
+ int qh,
1977
+ int kh);
1978
+
1979
+ // used in sam
1980
+ GGML_API struct ggml_tensor * ggml_add_rel_pos(
1981
+ struct ggml_context * ctx,
1982
+ struct ggml_tensor * a,
1983
+ struct ggml_tensor * pw,
1984
+ struct ggml_tensor * ph);
1985
+
1986
+ GGML_API struct ggml_tensor * ggml_add_rel_pos_inplace(
1987
+ struct ggml_context * ctx,
1988
+ struct ggml_tensor * a,
1989
+ struct ggml_tensor * pw,
1990
+ struct ggml_tensor * ph);
1991
+
1992
+ GGML_API struct ggml_tensor * ggml_rwkv_wkv(
1993
+ struct ggml_context * ctx,
1994
+ struct ggml_tensor * k,
1995
+ struct ggml_tensor * v,
1996
+ struct ggml_tensor * r,
1997
+ struct ggml_tensor * tf,
1998
+ struct ggml_tensor * td,
1999
+ struct ggml_tensor * state);
2000
+
2001
+ // custom operators
2002
+
2003
+ typedef void (*ggml_unary_op_f32_t) (const int, float *, const float *);
2004
+ typedef void (*ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
2005
+
2006
+ typedef void (*ggml_custom1_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *);
2007
+ typedef void (*ggml_custom2_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
2008
+ typedef void (*ggml_custom3_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
2009
+
2010
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_unary_f32(
2011
+ struct ggml_context * ctx,
2012
+ struct ggml_tensor * a,
2013
+ ggml_unary_op_f32_t fun),
2014
+ "use ggml_map_custom1 instead");
2015
+
2016
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_unary_inplace_f32(
2017
+ struct ggml_context * ctx,
2018
+ struct ggml_tensor * a,
2019
+ ggml_unary_op_f32_t fun),
2020
+ "use ggml_map_custom1_inplace instead");
2021
+
2022
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_binary_f32(
2023
+ struct ggml_context * ctx,
2024
+ struct ggml_tensor * a,
2025
+ struct ggml_tensor * b,
2026
+ ggml_binary_op_f32_t fun),
2027
+ "use ggml_map_custom2 instead");
2028
+
2029
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_binary_inplace_f32(
2030
+ struct ggml_context * ctx,
2031
+ struct ggml_tensor * a,
2032
+ struct ggml_tensor * b,
2033
+ ggml_binary_op_f32_t fun),
2034
+ "use ggml_map_custom2_inplace instead");
2035
+
2036
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom1_f32(
2037
+ struct ggml_context * ctx,
2038
+ struct ggml_tensor * a,
2039
+ ggml_custom1_op_f32_t fun),
2040
+ "use ggml_map_custom1 instead");
2041
+
2042
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom1_inplace_f32(
2043
+ struct ggml_context * ctx,
2044
+ struct ggml_tensor * a,
2045
+ ggml_custom1_op_f32_t fun),
2046
+ "use ggml_map_custom1_inplace instead");
2047
+
2048
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom2_f32(
2049
+ struct ggml_context * ctx,
2050
+ struct ggml_tensor * a,
2051
+ struct ggml_tensor * b,
2052
+ ggml_custom2_op_f32_t fun),
2053
+ "use ggml_map_custom2 instead");
2054
+
2055
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom2_inplace_f32(
2056
+ struct ggml_context * ctx,
2057
+ struct ggml_tensor * a,
2058
+ struct ggml_tensor * b,
2059
+ ggml_custom2_op_f32_t fun),
2060
+ "use ggml_map_custom2_inplace instead");
2061
+
2062
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom3_f32(
2063
+ struct ggml_context * ctx,
2064
+ struct ggml_tensor * a,
2065
+ struct ggml_tensor * b,
2066
+ struct ggml_tensor * c,
2067
+ ggml_custom3_op_f32_t fun),
2068
+ "use ggml_map_custom3 instead");
2069
+
2070
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom3_inplace_f32(
2071
+ struct ggml_context * ctx,
2072
+ struct ggml_tensor * a,
2073
+ struct ggml_tensor * b,
2074
+ struct ggml_tensor * c,
2075
+ ggml_custom3_op_f32_t fun),
2076
+ "use ggml_map_custom3_inplace instead");
2077
+
2078
+ // custom operators v2
2079
+
2080
+ typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);
2081
+ 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);
2082
+ 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);
2083
+
2084
+ #define GGML_N_TASKS_MAX (-1)
2085
+ // n_tasks == GGML_N_TASKS_MAX means to use max number of tasks
2086
+
2087
+ GGML_API struct ggml_tensor * ggml_map_custom1(
2088
+ struct ggml_context * ctx,
2089
+ struct ggml_tensor * a,
2090
+ ggml_custom1_op_t fun,
2091
+ int n_tasks,
2092
+ void * userdata);
2093
+
2094
+ GGML_API struct ggml_tensor * ggml_map_custom1_inplace(
2095
+ struct ggml_context * ctx,
2096
+ struct ggml_tensor * a,
2097
+ ggml_custom1_op_t fun,
2098
+ int n_tasks,
2099
+ void * userdata);
2100
+
2101
+ GGML_API struct ggml_tensor * ggml_map_custom2(
2102
+ struct ggml_context * ctx,
2103
+ struct ggml_tensor * a,
2104
+ struct ggml_tensor * b,
2105
+ ggml_custom2_op_t fun,
2106
+ int n_tasks,
2107
+ void * userdata);
2108
+
2109
+ GGML_API struct ggml_tensor * ggml_map_custom2_inplace(
2110
+ struct ggml_context * ctx,
2111
+ struct ggml_tensor * a,
2112
+ struct ggml_tensor * b,
2113
+ ggml_custom2_op_t fun,
2114
+ int n_tasks,
2115
+ void * userdata);
2116
+
2117
+ GGML_API struct ggml_tensor * ggml_map_custom3(
2118
+ struct ggml_context * ctx,
2119
+ struct ggml_tensor * a,
2120
+ struct ggml_tensor * b,
2121
+ struct ggml_tensor * c,
2122
+ ggml_custom3_op_t fun,
2123
+ int n_tasks,
2124
+ void * userdata);
2125
+
2126
+ GGML_API struct ggml_tensor * ggml_map_custom3_inplace(
2127
+ struct ggml_context * ctx,
2128
+ struct ggml_tensor * a,
2129
+ struct ggml_tensor * b,
2130
+ struct ggml_tensor * c,
2131
+ ggml_custom3_op_t fun,
2132
+ int n_tasks,
2133
+ void * userdata);
2134
+
2135
+ // loss function
2136
+
2137
+ GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
2138
+ struct ggml_context * ctx,
2139
+ struct ggml_tensor * a, // logits
2140
+ struct ggml_tensor * b); // labels
2141
+
2142
+ GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back(
2143
+ struct ggml_context * ctx,
2144
+ struct ggml_tensor * a, // logits
2145
+ struct ggml_tensor * b, // labels
2146
+ struct ggml_tensor * c); // gradients of cross_entropy_loss result
2147
+
2148
+ // AdamW optimizer step
2149
+ // Paper: https://arxiv.org/pdf/1711.05101v3.pdf
2150
+ // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
2151
+ GGML_API struct ggml_tensor * ggml_opt_step_adamw(
2152
+ struct ggml_context * ctx,
2153
+ struct ggml_tensor * a,
2154
+ struct ggml_tensor * grad,
2155
+ float alpha,
2156
+ float beta1,
2157
+ float beta2,
2158
+ float eps,
2159
+ float wd); // weight decay
2160
+
2161
+ //
2162
+ // automatic differentiation
2163
+ //
2164
+
2165
+ GGML_API void ggml_set_param(struct ggml_context * ctx, struct ggml_tensor * tensor);
2166
+ GGML_API void ggml_set_loss(struct ggml_tensor * tensor);
2167
+
2168
+ GGML_API void ggml_build_forward_expand (struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
2169
+ GGML_API void ggml_build_backward_expand(struct ggml_context * ctx, struct ggml_cgraph * gf, struct ggml_cgraph * gb, bool accumulate);
2170
+
2171
+ GGML_API void ggml_build_opt_adamw(
2172
+ struct ggml_context * ctx,
2173
+ struct ggml_cgraph * gf,
2174
+ struct ggml_cgraph * gb,
2175
+ float alpha,
2176
+ float beta1,
2177
+ float beta2,
2178
+ float eps,
2179
+ float wd); // weight decay
2180
+
2181
+ // graph allocation in a context
2182
+ GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx); // size = GGML_DEFAULT_GRAPH_SIZE, grads = false
2183
+ GGML_API struct ggml_cgraph * ggml_new_graph_custom(struct ggml_context * ctx, size_t size, bool grads);
2184
+ GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, struct ggml_cgraph * cgraph);
2185
+ GGML_API void ggml_graph_cpy (struct ggml_cgraph * src, struct ggml_cgraph * dst);
2186
+ GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
2187
+ GGML_API void ggml_graph_clear (struct ggml_cgraph * cgraph);
2188
+
2189
+ GGML_API int ggml_graph_size (struct ggml_cgraph * cgraph);
2190
+ GGML_API struct ggml_tensor * ggml_graph_node (struct ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i]
2191
+ GGML_API struct ggml_tensor ** ggml_graph_nodes (struct ggml_cgraph * cgraph);
2192
+ GGML_API int ggml_graph_n_nodes(struct ggml_cgraph * cgraph);
2193
+
2194
+ GGML_API void ggml_graph_add_node(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
2195
+
2196
+ GGML_API size_t ggml_graph_overhead(void);
2197
+ GGML_API size_t ggml_graph_overhead_custom(size_t size, bool grads);
2198
+
2199
+ GGML_API struct ggml_threadpool_params ggml_threadpool_params_default(int n_threads);
2200
+ GGML_API void ggml_threadpool_params_init (struct ggml_threadpool_params * p, int n_threads);
2201
+ GGML_API bool ggml_threadpool_params_match (const struct ggml_threadpool_params * p0, const struct ggml_threadpool_params * p1);
2202
+ GGML_API struct ggml_threadpool * ggml_threadpool_new (struct ggml_threadpool_params * params);
2203
+ GGML_API void ggml_threadpool_free (struct ggml_threadpool * threadpool);
2204
+ GGML_API int ggml_threadpool_get_n_threads(struct ggml_threadpool * threadpool);
2205
+ GGML_API void ggml_threadpool_pause (struct ggml_threadpool * threadpool);
2206
+ GGML_API void ggml_threadpool_resume (struct ggml_threadpool * threadpool);
2207
+
2208
+ // ggml_graph_plan() has to be called before ggml_graph_compute()
2209
+ // when plan.work_size > 0, caller must allocate memory for plan.work_data
2210
+ GGML_API struct ggml_cplan ggml_graph_plan(
2211
+ const struct ggml_cgraph * cgraph,
2212
+ int n_threads, /* = GGML_DEFAULT_N_THREADS */
2213
+ struct ggml_threadpool * threadpool /* = NULL */ );
2214
+ GGML_API enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan);
2215
+
2216
+ // same as ggml_graph_compute() but the work data is allocated as a part of the context
2217
+ // note: the drawback of this API is that you must have ensured that the context has enough memory for the work data
2218
+ GGML_API enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads);
2219
+
2220
+ GGML_API struct ggml_tensor * ggml_graph_get_tensor(struct ggml_cgraph * cgraph, const char * name);
2221
+
2222
+ GGML_API void ggml_graph_export(const struct ggml_cgraph * cgraph, const char * fname);
2223
+ GGML_API struct ggml_cgraph * ggml_graph_import(const char * fname, struct ggml_context ** ctx_data, struct ggml_context ** ctx_eval);
2224
+
2225
+ // print info and performance information for the graph
2226
+ GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
2227
+
2228
+ // dump the graph into a file using the dot format
2229
+ GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename);
2230
+
2231
+ // build gradient checkpointing backward graph gb for gf using provided checkpoints
2232
+ // gb_tmp will contain original backward graph with rewritten backward process nodes,
2233
+ // but without the second forward pass nodes.
2234
+ GGML_API void ggml_build_backward_gradient_checkpointing(
2235
+ struct ggml_context * ctx,
2236
+ struct ggml_cgraph * gf,
2237
+ struct ggml_cgraph * gb,
2238
+ struct ggml_cgraph * gb_tmp,
2239
+ struct ggml_tensor * * checkpoints,
2240
+ int n_checkpoints);
2241
+ //
2242
+ // optimization
2243
+ //
2244
+
2245
+ // optimization methods
2246
+ enum ggml_opt_type {
2247
+ GGML_OPT_TYPE_ADAM,
2248
+ GGML_OPT_TYPE_LBFGS,
2249
+ };
2250
+
2251
+ // linesearch methods
2252
+ enum ggml_linesearch {
2253
+ GGML_LINESEARCH_DEFAULT = 1,
2254
+
2255
+ GGML_LINESEARCH_BACKTRACKING_ARMIJO = 0,
2256
+ GGML_LINESEARCH_BACKTRACKING_WOLFE = 1,
2257
+ GGML_LINESEARCH_BACKTRACKING_STRONG_WOLFE = 2,
2258
+ };
2259
+
2260
+ // optimization return values
2261
+ enum ggml_opt_result {
2262
+ GGML_OPT_RESULT_OK = 0,
2263
+ GGML_OPT_RESULT_DID_NOT_CONVERGE,
2264
+ GGML_OPT_RESULT_NO_CONTEXT,
2265
+ GGML_OPT_RESULT_INVALID_WOLFE,
2266
+ GGML_OPT_RESULT_FAIL,
2267
+ GGML_OPT_RESULT_CANCEL,
2268
+
2269
+ GGML_LINESEARCH_FAIL = -128,
2270
+ GGML_LINESEARCH_MINIMUM_STEP,
2271
+ GGML_LINESEARCH_MAXIMUM_STEP,
2272
+ GGML_LINESEARCH_MAXIMUM_ITERATIONS,
2273
+ GGML_LINESEARCH_INVALID_PARAMETERS,
2274
+ };
2275
+
2276
+ typedef void (*ggml_opt_callback)(void * data, int accum_step, float * sched, bool * cancel);
2277
+ typedef void (*ggml_log_callback)(enum ggml_log_level level, const char * text, void * user_data);
2278
+
2279
+ // optimization parameters
2280
+ //
2281
+ // see ggml.c (ggml_opt_default_params) for default values
2282
+ //
2283
+ struct ggml_opt_params {
2284
+ enum ggml_opt_type type;
2285
+
2286
+ size_t graph_size;
2287
+
2288
+ int n_threads;
2289
+
2290
+ // delta-based convergence test
2291
+ //
2292
+ // if past == 0 - disabled
2293
+ // if past > 0:
2294
+ // stop if |f(x) - f(x_past)| < delta * max(1, |f(x)|)
2295
+ //
2296
+ int past;
2297
+ float delta;
2298
+
2299
+ // maximum number of iterations without improvement
2300
+ //
2301
+ // if 0 - disabled
2302
+ // if > 0:
2303
+ // assume convergence if no cost improvement in this number of iterations
2304
+ //
2305
+ int max_no_improvement;
2306
+
2307
+ bool print_forward_graph;
2308
+ bool print_backward_graph;
2309
+
2310
+ int n_gradient_accumulation;
2311
+
2312
+ // ADAM parameters
2313
+ struct {
2314
+ int n_iter;
2315
+
2316
+ float sched; // schedule multiplier (fixed, decay or warmup)
2317
+ float decay; // weight decay for AdamW, use 0.0f to disable
2318
+ int decay_min_ndim; // minimum number of tensor dimension to apply weight decay
2319
+ float alpha; // learning rate
2320
+ float beta1;
2321
+ float beta2;
2322
+ float eps; // epsilon for numerical stability
2323
+ float eps_f; // epsilon for convergence test
2324
+ float eps_g; // epsilon for convergence test
2325
+ float gclip; // gradient clipping
2326
+ } adam;
2327
+
2328
+ // LBFGS parameters
2329
+ struct {
2330
+ int m; // number of corrections to approximate the inv. Hessian
2331
+ int n_iter;
2332
+ int max_linesearch;
2333
+
2334
+ float eps; // convergence tolerance
2335
+ float ftol; // line search tolerance
2336
+ float wolfe;
2337
+ float min_step;
2338
+ float max_step;
2339
+
2340
+ enum ggml_linesearch linesearch;
2341
+ } lbfgs;
2342
+ };
2343
+
2344
+ struct ggml_opt_context {
2345
+ struct ggml_context * ctx;
2346
+ struct ggml_opt_params params;
2347
+
2348
+ int iter;
2349
+ int64_t nx; // number of parameter elements
2350
+
2351
+ bool just_initialized;
2352
+
2353
+ float loss_before;
2354
+ float loss_after;
2355
+
2356
+ struct {
2357
+ struct ggml_tensor * g; // current gradient
2358
+ struct ggml_tensor * m; // first moment
2359
+ struct ggml_tensor * v; // second moment
2360
+ struct ggml_tensor * pf; // past function values
2361
+ float fx_best;
2362
+ float fx_prev;
2363
+ int n_no_improvement;
2364
+ } adam;
2365
+
2366
+ struct {
2367
+ struct ggml_tensor * x; // current parameters
2368
+ struct ggml_tensor * xp; // previous parameters
2369
+ struct ggml_tensor * g; // current gradient
2370
+ struct ggml_tensor * gp; // previous gradient
2371
+ struct ggml_tensor * d; // search direction
2372
+ struct ggml_tensor * pf; // past function values
2373
+ struct ggml_tensor * lmal; // the L-BFGS memory alpha
2374
+ struct ggml_tensor * lmys; // the L-BFGS memory ys
2375
+ struct ggml_tensor * lms; // the L-BFGS memory s
2376
+ struct ggml_tensor * lmy; // the L-BFGS memory y
2377
+ float fx_best;
2378
+ float step;
2379
+ int j;
2380
+ int k;
2381
+ int end;
2382
+ int n_no_improvement;
2383
+ } lbfgs;
2384
+ };
2385
+
2386
+ GGML_API struct ggml_opt_params ggml_opt_default_params(enum ggml_opt_type type);
2387
+
2388
+ // optimize the function defined by the tensor f
2389
+ GGML_API enum ggml_opt_result ggml_opt(
2390
+ struct ggml_context * ctx,
2391
+ struct ggml_opt_params params,
2392
+ struct ggml_tensor * f);
2393
+
2394
+ // initialize optimizer context
2395
+ GGML_API void ggml_opt_init(
2396
+ struct ggml_context * ctx,
2397
+ struct ggml_opt_context * opt,
2398
+ struct ggml_opt_params params,
2399
+ int64_t nx);
2400
+
2401
+ // continue optimizing the function defined by the tensor f
2402
+ GGML_API enum ggml_opt_result ggml_opt_resume(
2403
+ struct ggml_context * ctx,
2404
+ struct ggml_opt_context * opt,
2405
+ struct ggml_tensor * f);
2406
+
2407
+ // continue optimizing the function defined by the tensor f
2408
+ GGML_API enum ggml_opt_result ggml_opt_resume_g(
2409
+ struct ggml_context * ctx,
2410
+ struct ggml_opt_context * opt,
2411
+ struct ggml_tensor * f,
2412
+ struct ggml_cgraph * gf,
2413
+ struct ggml_cgraph * gb,
2414
+ ggml_opt_callback callback,
2415
+ void * callback_data);
2416
+
2417
+ //
2418
+ // tensor flags
2419
+ //
2420
+ GGML_API void ggml_set_input(struct ggml_tensor * tensor);
2421
+ GGML_API void ggml_set_output(struct ggml_tensor * tensor);
2422
+
2423
+ //
2424
+ // quantization
2425
+ //
2426
+
2427
+ // - ggml_quantize_init can be called multiple times with the same type
2428
+ // it will only initialize the quantization tables for the first call or after ggml_quantize_free
2429
+ // automatically called by ggml_quantize_chunk for convenience
2430
+ //
2431
+ // - ggml_quantize_free will free any memory allocated by ggml_quantize_init
2432
+ // call this at the end of the program to avoid memory leaks
2433
+ //
2434
+ // note: these are thread-safe
2435
+ //
2436
+ GGML_API void ggml_quantize_init(enum ggml_type type);
2437
+ GGML_API void ggml_quantize_free(void);
2438
+
2439
+ // some quantization type cannot be used without an importance matrix
2440
+ GGML_API bool ggml_quantize_requires_imatrix(enum ggml_type type);
2441
+
2442
+ // calls ggml_quantize_init internally (i.e. can allocate memory)
2443
+ GGML_API size_t ggml_quantize_chunk(
2444
+ enum ggml_type type,
2445
+ const float * src,
2446
+ void * dst,
2447
+ int64_t start,
2448
+ int64_t nrows,
2449
+ int64_t n_per_row,
2450
+ const float * imatrix);
2451
+
2452
+ //
2453
+ // gguf
2454
+ //
2455
+
2456
+ enum gguf_type {
2457
+ GGUF_TYPE_UINT8 = 0,
2458
+ GGUF_TYPE_INT8 = 1,
2459
+ GGUF_TYPE_UINT16 = 2,
2460
+ GGUF_TYPE_INT16 = 3,
2461
+ GGUF_TYPE_UINT32 = 4,
2462
+ GGUF_TYPE_INT32 = 5,
2463
+ GGUF_TYPE_FLOAT32 = 6,
2464
+ GGUF_TYPE_BOOL = 7,
2465
+ GGUF_TYPE_STRING = 8,
2466
+ GGUF_TYPE_ARRAY = 9,
2467
+ GGUF_TYPE_UINT64 = 10,
2468
+ GGUF_TYPE_INT64 = 11,
2469
+ GGUF_TYPE_FLOAT64 = 12,
2470
+ GGUF_TYPE_COUNT, // marks the end of the enum
2471
+ };
2472
+
2473
+ struct gguf_context;
2474
+
2475
+ struct gguf_init_params {
2476
+ bool no_alloc;
2477
+
2478
+ // if not NULL, create a ggml_context and allocate the tensor data in it
2479
+ struct ggml_context ** ctx;
2480
+ };
2481
+
2482
+ GGML_API struct gguf_context * gguf_init_empty(void);
2483
+ GGML_API struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_params params);
2484
+ //GGML_API struct gguf_context * gguf_init_from_buffer(..);
2485
+
2486
+ GGML_API void gguf_free(struct gguf_context * ctx);
2487
+
2488
+ GGML_API const char * gguf_type_name(enum gguf_type type);
2489
+
2490
+ GGML_API int gguf_get_version (const struct gguf_context * ctx);
2491
+ GGML_API size_t gguf_get_alignment (const struct gguf_context * ctx);
2492
+ GGML_API size_t gguf_get_data_offset(const struct gguf_context * ctx);
2493
+ GGML_API void * gguf_get_data (const struct gguf_context * ctx);
2494
+
2495
+ GGML_API int gguf_get_n_kv(const struct gguf_context * ctx);
2496
+ GGML_API int gguf_find_key(const struct gguf_context * ctx, const char * key);
2497
+ GGML_API const char * gguf_get_key (const struct gguf_context * ctx, int key_id);
2498
+
2499
+ GGML_API enum gguf_type gguf_get_kv_type (const struct gguf_context * ctx, int key_id);
2500
+ GGML_API enum gguf_type gguf_get_arr_type(const struct gguf_context * ctx, int key_id);
2501
+
2502
+ // will abort if the wrong type is used for the key
2503
+ GGML_API uint8_t gguf_get_val_u8 (const struct gguf_context * ctx, int key_id);
2504
+ GGML_API int8_t gguf_get_val_i8 (const struct gguf_context * ctx, int key_id);
2505
+ GGML_API uint16_t gguf_get_val_u16 (const struct gguf_context * ctx, int key_id);
2506
+ GGML_API int16_t gguf_get_val_i16 (const struct gguf_context * ctx, int key_id);
2507
+ GGML_API uint32_t gguf_get_val_u32 (const struct gguf_context * ctx, int key_id);
2508
+ GGML_API int32_t gguf_get_val_i32 (const struct gguf_context * ctx, int key_id);
2509
+ GGML_API float gguf_get_val_f32 (const struct gguf_context * ctx, int key_id);
2510
+ GGML_API uint64_t gguf_get_val_u64 (const struct gguf_context * ctx, int key_id);
2511
+ GGML_API int64_t gguf_get_val_i64 (const struct gguf_context * ctx, int key_id);
2512
+ GGML_API double gguf_get_val_f64 (const struct gguf_context * ctx, int key_id);
2513
+ GGML_API bool gguf_get_val_bool(const struct gguf_context * ctx, int key_id);
2514
+ GGML_API const char * gguf_get_val_str (const struct gguf_context * ctx, int key_id);
2515
+ GGML_API const void * gguf_get_val_data(const struct gguf_context * ctx, int key_id);
2516
+ GGML_API int gguf_get_arr_n (const struct gguf_context * ctx, int key_id);
2517
+ GGML_API const void * gguf_get_arr_data(const struct gguf_context * ctx, int key_id);
2518
+ GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int key_id, int i);
2519
+
2520
+ GGML_API int gguf_get_n_tensors (const struct gguf_context * ctx);
2521
+ GGML_API int gguf_find_tensor (const struct gguf_context * ctx, const char * name);
2522
+ GGML_API size_t gguf_get_tensor_offset(const struct gguf_context * ctx, int i);
2523
+ GGML_API char * gguf_get_tensor_name (const struct gguf_context * ctx, int i);
2524
+ GGML_API enum ggml_type gguf_get_tensor_type (const struct gguf_context * ctx, int i);
2525
+
2526
+ // removes key if it exists
2527
+ GGML_API void gguf_remove_key(struct gguf_context * ctx, const char * key);
2528
+
2529
+ // overrides existing values or adds a new one
2530
+ GGML_API void gguf_set_val_u8 (struct gguf_context * ctx, const char * key, uint8_t val);
2531
+ GGML_API void gguf_set_val_i8 (struct gguf_context * ctx, const char * key, int8_t val);
2532
+ GGML_API void gguf_set_val_u16 (struct gguf_context * ctx, const char * key, uint16_t val);
2533
+ GGML_API void gguf_set_val_i16 (struct gguf_context * ctx, const char * key, int16_t val);
2534
+ GGML_API void gguf_set_val_u32 (struct gguf_context * ctx, const char * key, uint32_t val);
2535
+ GGML_API void gguf_set_val_i32 (struct gguf_context * ctx, const char * key, int32_t val);
2536
+ GGML_API void gguf_set_val_f32 (struct gguf_context * ctx, const char * key, float val);
2537
+ GGML_API void gguf_set_val_u64 (struct gguf_context * ctx, const char * key, uint64_t val);
2538
+ GGML_API void gguf_set_val_i64 (struct gguf_context * ctx, const char * key, int64_t val);
2539
+ GGML_API void gguf_set_val_f64 (struct gguf_context * ctx, const char * key, double val);
2540
+ GGML_API void gguf_set_val_bool(struct gguf_context * ctx, const char * key, bool val);
2541
+ GGML_API void gguf_set_val_str (struct gguf_context * ctx, const char * key, const char * val);
2542
+ GGML_API void gguf_set_arr_data(struct gguf_context * ctx, const char * key, enum gguf_type type, const void * data, int n);
2543
+ GGML_API void gguf_set_arr_str (struct gguf_context * ctx, const char * key, const char ** data, int n);
2544
+
2545
+ // set or add KV pairs from another context
2546
+ GGML_API void gguf_set_kv(struct gguf_context * ctx, struct gguf_context * src);
2547
+
2548
+ // manage tensor info
2549
+ GGML_API void gguf_add_tensor(struct gguf_context * ctx, const struct ggml_tensor * tensor);
2550
+ GGML_API void gguf_set_tensor_type(struct gguf_context * ctx, const char * name, enum ggml_type type);
2551
+ GGML_API void gguf_set_tensor_data(struct gguf_context * ctx, const char * name, const void * data, size_t size);
2552
+
2553
+ // writing gguf files can be done in 2 ways:
2554
+ //
2555
+ // - write the entire gguf_context to a binary file in a single pass:
2556
+ //
2557
+ // gguf_write_to_file(ctx, fname);
2558
+ //
2559
+ // - first prepare a file with a placeholder for the meta data, write the tensor data, then write the meta data:
2560
+ //
2561
+ // FILE * f = fopen(fname, "wb");
2562
+ // fseek(f, gguf_get_meta_size(ctx), SEEK_SET);
2563
+ // fwrite(f, ...);
2564
+ // void * data = gguf_meta_get_meta_data(ctx);
2565
+ // fseek(f, 0, SEEK_SET);
2566
+ // fwrite(f, data, gguf_get_meta_size(ctx));
2567
+ // free(data);
2568
+ // fclose(f);
2569
+ //
2570
+
2571
+ // write the entire context to a binary file
2572
+ GGML_API void gguf_write_to_file(const struct gguf_context * ctx, const char * fname, bool only_meta);
2573
+
2574
+ // get the size in bytes of the meta data (header, kv pairs, tensor info) including padding
2575
+ GGML_API size_t gguf_get_meta_size(const struct gguf_context * ctx);
2576
+ GGML_API void gguf_get_meta_data(const struct gguf_context * ctx, void * data);
2577
+
2578
+ //
2579
+ // system info
2580
+ //
2581
+
2582
+ GGML_API int ggml_cpu_has_avx (void);
2583
+ GGML_API int ggml_cpu_has_avx_vnni (void);
2584
+ GGML_API int ggml_cpu_has_avx2 (void);
2585
+ GGML_API int ggml_cpu_has_avx512 (void);
2586
+ GGML_API int ggml_cpu_has_avx512_vbmi(void);
2587
+ GGML_API int ggml_cpu_has_avx512_vnni(void);
2588
+ GGML_API int ggml_cpu_has_avx512_bf16(void);
2589
+ GGML_API int ggml_cpu_has_fma (void);
2590
+ GGML_API int ggml_cpu_has_neon (void);
2591
+ GGML_API int ggml_cpu_has_sve (void);
2592
+ GGML_API int ggml_cpu_has_arm_fma (void);
2593
+ GGML_API int ggml_cpu_has_metal (void);
2594
+ GGML_API int ggml_cpu_has_f16c (void);
2595
+ GGML_API int ggml_cpu_has_fp16_va (void);
2596
+ GGML_API int ggml_cpu_has_wasm_simd (void);
2597
+ GGML_API int ggml_cpu_has_blas (void);
2598
+ GGML_API int ggml_cpu_has_cuda (void);
2599
+ GGML_API int ggml_cpu_has_vulkan (void);
2600
+ GGML_API int ggml_cpu_has_kompute (void);
2601
+ GGML_API int ggml_cpu_has_gpublas (void);
2602
+ GGML_API int ggml_cpu_has_sse3 (void);
2603
+ GGML_API int ggml_cpu_has_ssse3 (void);
2604
+ GGML_API int ggml_cpu_has_riscv_v (void);
2605
+ GGML_API int ggml_cpu_has_sycl (void);
2606
+ GGML_API int ggml_cpu_has_rpc (void);
2607
+ GGML_API int ggml_cpu_has_vsx (void);
2608
+ GGML_API int ggml_cpu_has_matmul_int8(void);
2609
+ GGML_API int ggml_cpu_has_cann (void);
2610
+ GGML_API int ggml_cpu_has_llamafile (void);
2611
+
2612
+ // get the sve vector length in bytes
2613
+ GGML_API int ggml_cpu_get_sve_cnt(void);
2614
+
2615
+ //
2616
+ // Internal types and functions exposed for tests and benchmarks
2617
+ //
2618
+
2619
+ #ifdef __cplusplus
2620
+ // restrict not standard in C++
2621
+ #define GGML_RESTRICT
2622
+ #else
2623
+ #define GGML_RESTRICT restrict
2624
+ #endif
2625
+ typedef void (*ggml_to_float_t) (const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);
2626
+ typedef void (*ggml_from_float_t)(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
2627
+ typedef void (*ggml_from_float_to_mat_t)
2628
+ (const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t nr, int64_t k, int64_t bs);
2629
+ typedef void (*ggml_vec_dot_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x, size_t bx,
2630
+ const void * GGML_RESTRICT y, size_t by, int nrc);
2631
+ typedef void (*ggml_gemv_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x,
2632
+ const void * GGML_RESTRICT y, int nr, int nc);
2633
+ typedef void (*ggml_gemm_t) (int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT x,
2634
+ const void * GGML_RESTRICT y, int nr, int nc);
2635
+
2636
+ typedef struct {
2637
+ const char * type_name;
2638
+ int64_t blck_size;
2639
+ int64_t blck_size_interleave; // interleave elements in blocks
2640
+ size_t type_size;
2641
+ bool is_quantized;
2642
+ ggml_to_float_t to_float;
2643
+ ggml_from_float_t from_float;
2644
+ ggml_from_float_t from_float_ref;
2645
+ ggml_from_float_to_mat_t from_float_to_mat;
2646
+ ggml_vec_dot_t vec_dot;
2647
+ enum ggml_type vec_dot_type;
2648
+ int64_t nrows; // number of rows to process simultaneously
2649
+ int64_t ncols; // number of columns to process simultaneously
2650
+ ggml_gemv_t gemv;
2651
+ ggml_gemm_t gemm;
2652
+ } ggml_type_traits_t;
2653
+
2654
+ GGML_API ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type type);
2655
+
2656
+ enum gguf_npu_qtype {
2657
+ GGUF_TYPE_NPU_CW_Q4_0 = 0,
2658
+ GGUF_TYPE_NPU_CW_Q4_1 = 1,
2659
+ GGUF_TYPE_NPU_GW_Q4_0 = 2,
2660
+ };
2661
+
2662
+ GGML_API void ggml_dequantize_to_fp32(struct ggml_tensor * tensor, float *y, size_t n);
2663
+
2664
+ GGML_API void ggml_requantize_to_npu(struct ggml_tensor * tensor, float* y, const char* layer, const int weight_idx, enum gguf_npu_qtype npu_type, const char* dir_path, int n_splits);
2665
+
2666
+ GGML_API void ggml_convert_to_fp16(struct ggml_tensor * tensor, const char* layer, const int weight_idx, const char* dir_path);
2667
+
2668
+ GGML_API size_t ggml_quantize_chunk_with_splits(
2669
+ enum ggml_type type,
2670
+ const float * src,
2671
+ void * dst,
2672
+ int64_t start,
2673
+ int64_t nrows,
2674
+ int64_t n_per_row,
2675
+ const float * imatrix,
2676
+ int n_splits);
2677
+ #ifdef __cplusplus
2678
+ }
2679
+ #endif