cui-llama.rn 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/android/src/main/jni.cpp +9 -9
  2. package/cpp/common.cpp +163 -60
  3. package/cpp/common.h +43 -12
  4. package/cpp/ggml-alloc.c +1042 -1037
  5. package/cpp/ggml-backend-impl.h +255 -256
  6. package/cpp/ggml-backend-reg.cpp +582 -582
  7. package/cpp/ggml-backend.cpp +2002 -2002
  8. package/cpp/ggml-backend.h +354 -352
  9. package/cpp/ggml-common.h +1853 -1853
  10. package/cpp/ggml-cpp.h +39 -39
  11. package/cpp/ggml-cpu-aarch64.cpp +4247 -4247
  12. package/cpp/ggml-cpu-aarch64.h +8 -8
  13. package/cpp/ggml-cpu-impl.h +386 -386
  14. package/cpp/ggml-cpu-quants.c +10920 -10839
  15. package/cpp/ggml-cpu-traits.cpp +36 -36
  16. package/cpp/ggml-cpu-traits.h +38 -38
  17. package/cpp/ggml-cpu.c +329 -60
  18. package/cpp/ggml-cpu.cpp +10 -2
  19. package/cpp/ggml-cpu.h +135 -135
  20. package/cpp/ggml-impl.h +567 -567
  21. package/cpp/ggml-metal-impl.h +17 -17
  22. package/cpp/ggml-metal.m +4884 -4884
  23. package/cpp/ggml-quants.c +5238 -5238
  24. package/cpp/ggml-threading.h +14 -14
  25. package/cpp/ggml.c +6514 -6448
  26. package/cpp/ggml.h +2194 -2163
  27. package/cpp/gguf.cpp +1329 -1325
  28. package/cpp/gguf.h +202 -202
  29. package/cpp/json-schema-to-grammar.cpp +1045 -1045
  30. package/cpp/json-schema-to-grammar.h +8 -8
  31. package/cpp/json.hpp +24766 -24766
  32. package/cpp/llama-adapter.cpp +347 -346
  33. package/cpp/llama-adapter.h +74 -73
  34. package/cpp/llama-arch.cpp +1487 -1434
  35. package/cpp/llama-arch.h +400 -395
  36. package/cpp/llama-batch.cpp +368 -368
  37. package/cpp/llama-batch.h +88 -88
  38. package/cpp/llama-chat.cpp +578 -567
  39. package/cpp/llama-chat.h +52 -51
  40. package/cpp/llama-context.cpp +1775 -1771
  41. package/cpp/llama-context.h +128 -128
  42. package/cpp/llama-cparams.cpp +1 -1
  43. package/cpp/llama-cparams.h +37 -37
  44. package/cpp/llama-cpp.h +30 -30
  45. package/cpp/llama-grammar.cpp +1139 -1139
  46. package/cpp/llama-grammar.h +143 -143
  47. package/cpp/llama-hparams.cpp +71 -71
  48. package/cpp/llama-hparams.h +139 -140
  49. package/cpp/llama-impl.cpp +167 -167
  50. package/cpp/llama-impl.h +61 -61
  51. package/cpp/llama-kv-cache.cpp +718 -718
  52. package/cpp/llama-kv-cache.h +218 -218
  53. package/cpp/llama-mmap.cpp +2 -1
  54. package/cpp/llama-mmap.h +67 -67
  55. package/cpp/llama-model-loader.cpp +1124 -1011
  56. package/cpp/llama-model-loader.h +167 -158
  57. package/cpp/llama-model.cpp +3997 -2202
  58. package/cpp/llama-model.h +370 -391
  59. package/cpp/llama-sampling.cpp +2408 -2406
  60. package/cpp/llama-sampling.h +32 -48
  61. package/cpp/llama-vocab.cpp +3247 -1982
  62. package/cpp/llama-vocab.h +125 -182
  63. package/cpp/llama.cpp +416 -2886
  64. package/cpp/llama.h +1323 -1285
  65. package/cpp/log.cpp +401 -401
  66. package/cpp/log.h +121 -121
  67. package/cpp/rn-llama.hpp +18 -12
  68. package/cpp/sampling.cpp +505 -500
  69. package/cpp/sgemm.cpp +2597 -2597
  70. package/cpp/speculative.cpp +277 -274
  71. package/cpp/speculative.h +28 -28
  72. package/cpp/unicode.cpp +2 -3
  73. package/package.json +1 -1
@@ -1,256 +1,255 @@
1
- #pragma once
2
-
3
- // ggml-backend internal header
4
-
5
- #include "ggml-backend.h"
6
-
7
- #ifdef __cplusplus
8
- extern "C" {
9
- #endif
10
-
11
- #define LM_GGML_BACKEND_API_VERSION 1
12
-
13
- //
14
- // Backend buffer type
15
- //
16
-
17
- struct lm_ggml_backend_buffer_type_i {
18
- const char * (*get_name) (lm_ggml_backend_buffer_type_t buft);
19
- // allocate a buffer of this type
20
- lm_ggml_backend_buffer_t (*alloc_buffer) (lm_ggml_backend_buffer_type_t buft, size_t size);
21
- // tensor alignment
22
- size_t (*get_alignment) (lm_ggml_backend_buffer_type_t buft);
23
- // (optional) max buffer size that can be allocated (defaults to SIZE_MAX)
24
- size_t (*get_max_size) (lm_ggml_backend_buffer_type_t buft);
25
- // (optional) data size needed to allocate the tensor, including padding (defaults to lm_ggml_nbytes)
26
- size_t (*get_alloc_size)(lm_ggml_backend_buffer_type_t buft, const struct lm_ggml_tensor * tensor);
27
- // (optional) check if tensor data is in host memory and uses standard ggml tensor layout (defaults to false)
28
- bool (*is_host) (lm_ggml_backend_buffer_type_t buft);
29
- };
30
-
31
- struct lm_ggml_backend_buffer_type {
32
- struct lm_ggml_backend_buffer_type_i iface;
33
- lm_ggml_backend_dev_t device;
34
- void * context;
35
- };
36
-
37
- //
38
- // Backend buffer
39
- //
40
-
41
- struct lm_ggml_backend_buffer_i {
42
- // (optional) free the buffer
43
- void (*free_buffer) (lm_ggml_backend_buffer_t buffer);
44
- // base address of the buffer
45
- void * (*get_base) (lm_ggml_backend_buffer_t buffer);
46
- // (optional) initialize a tensor in the buffer (eg. add tensor extras)
47
- void (*init_tensor) (lm_ggml_backend_buffer_t buffer, struct lm_ggml_tensor * tensor);
48
- // tensor data access
49
- void (*memset_tensor)(lm_ggml_backend_buffer_t buffer, struct lm_ggml_tensor * tensor, uint8_t value, size_t offset, size_t size);
50
- void (*set_tensor) (lm_ggml_backend_buffer_t buffer, struct lm_ggml_tensor * tensor, const void * data, size_t offset, size_t size);
51
- void (*get_tensor) (lm_ggml_backend_buffer_t buffer, const struct lm_ggml_tensor * tensor, void * data, size_t offset, size_t size);
52
- // (optional) tensor copy: dst is in the buffer, src may be in any buffer, including buffers from a different backend (return false if not supported)
53
- bool (*cpy_tensor) (lm_ggml_backend_buffer_t buffer, const struct lm_ggml_tensor * src, struct lm_ggml_tensor * dst);
54
- // clear the entire buffer
55
- void (*clear) (lm_ggml_backend_buffer_t buffer, uint8_t value);
56
- // (optional) reset any internal state due to tensor initialization, such as tensor extras
57
- void (*reset) (lm_ggml_backend_buffer_t buffer);
58
- };
59
-
60
- struct lm_ggml_backend_buffer {
61
- struct lm_ggml_backend_buffer_i iface;
62
- lm_ggml_backend_buffer_type_t buft;
63
- void * context;
64
- size_t size;
65
- enum lm_ggml_backend_buffer_usage usage;
66
- };
67
-
68
- LM_GGML_API lm_ggml_backend_buffer_t lm_ggml_backend_buffer_init(
69
- lm_ggml_backend_buffer_type_t buft,
70
- struct lm_ggml_backend_buffer_i iface,
71
- void * context,
72
- size_t size);
73
-
74
- // do not use directly, use lm_ggml_backend_tensor_copy instead
75
- LM_GGML_API bool lm_ggml_backend_buffer_copy_tensor(const struct lm_ggml_tensor * src, struct lm_ggml_tensor * dst);
76
-
77
- // multi-buffer
78
- // buffer that contains a collection of buffers
79
- LM_GGML_API lm_ggml_backend_buffer_t lm_ggml_backend_multi_buffer_alloc_buffer(lm_ggml_backend_buffer_t * buffers, size_t n_buffers);
80
- LM_GGML_API bool lm_ggml_backend_buffer_is_multi_buffer(lm_ggml_backend_buffer_t buffer);
81
- LM_GGML_API void lm_ggml_backend_multi_buffer_set_usage(lm_ggml_backend_buffer_t buffer, enum lm_ggml_backend_buffer_usage usage);
82
-
83
- //
84
- // Backend (stream)
85
- //
86
-
87
- struct lm_ggml_backend_i {
88
- const char * (*get_name)(lm_ggml_backend_t backend);
89
-
90
- void (*free)(lm_ggml_backend_t backend);
91
-
92
- // (optional) asynchronous tensor data access
93
- void (*set_tensor_async)(lm_ggml_backend_t backend, struct lm_ggml_tensor * tensor, const void * data, size_t offset, size_t size);
94
- void (*get_tensor_async)(lm_ggml_backend_t backend, const struct lm_ggml_tensor * tensor, void * data, size_t offset, size_t size);
95
- bool (*cpy_tensor_async)(lm_ggml_backend_t backend_src, lm_ggml_backend_t backend_dst, const struct lm_ggml_tensor * src, struct lm_ggml_tensor * dst);
96
-
97
- // (optional) complete all pending operations (required if the backend supports async operations)
98
- void (*synchronize)(lm_ggml_backend_t backend);
99
-
100
- // (optional) graph plans (not used currently)
101
- // compute graph with a plan
102
- lm_ggml_backend_graph_plan_t (*graph_plan_create) (lm_ggml_backend_t backend, const struct lm_ggml_cgraph * cgraph);
103
- void (*graph_plan_free) (lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan);
104
- // update the plan with a new graph - this should be faster than creating a new plan when the graph has the same topology
105
- void (*graph_plan_update) (lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan, const struct lm_ggml_cgraph * cgraph);
106
- // compute the graph with the plan
107
- enum lm_ggml_status (*graph_plan_compute)(lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan);
108
-
109
- // compute graph (always async if supported by the backend)
110
- enum lm_ggml_status (*graph_compute) (lm_ggml_backend_t backend, struct lm_ggml_cgraph * cgraph);
111
-
112
- // (optional) event synchronization
113
- // record an event on this stream
114
- void (*event_record)(lm_ggml_backend_t backend, lm_ggml_backend_event_t event);
115
- // wait for an event on on a different stream
116
- void (*event_wait) (lm_ggml_backend_t backend, lm_ggml_backend_event_t event);
117
- };
118
-
119
- struct lm_ggml_backend {
120
- lm_ggml_guid_t guid;
121
- struct lm_ggml_backend_i iface;
122
- lm_ggml_backend_dev_t device;
123
- void * context;
124
- };
125
-
126
- struct lm_ggml_backend_event {
127
- struct lm_ggml_backend_device * device;
128
- void * context;
129
- };
130
-
131
- //
132
- // Backend device
133
- //
134
-
135
- // Note: if additional properties are needed, we should add a struct with all of them
136
- // the current functions to obtain the properties can remain, since they are more convenient for often used properties
137
- struct lm_ggml_backend_device_i {
138
- // device name: short identifier for this device, such as "CPU" or "CUDA0"
139
- const char * (*get_name)(lm_ggml_backend_dev_t dev);
140
-
141
- // device description: short informative description of the device, could be the model name
142
- const char * (*get_description)(lm_ggml_backend_dev_t dev);
143
-
144
- // device memory in bytes
145
- void (*get_memory)(lm_ggml_backend_dev_t dev, size_t * free, size_t * total);
146
-
147
- // device type
148
- enum lm_ggml_backend_dev_type (*get_type)(lm_ggml_backend_dev_t dev);
149
-
150
- // device properties
151
- void (*get_props)(lm_ggml_backend_dev_t dev, struct lm_ggml_backend_dev_props * props);
152
-
153
- // backend (stream) initialization
154
- lm_ggml_backend_t (*init_backend)(lm_ggml_backend_dev_t dev, const char * params);
155
-
156
- // preferred buffer type
157
- lm_ggml_backend_buffer_type_t (*get_buffer_type)(lm_ggml_backend_dev_t dev);
158
-
159
- // (optional) host buffer type (in system memory, typically this is a pinned memory buffer for faster transfers between host and device)
160
- lm_ggml_backend_buffer_type_t (*get_host_buffer_type)(lm_ggml_backend_dev_t dev);
161
-
162
- // (optional) buffer from pointer: create a buffer from a host pointer (useful for memory mapped models and importing data from other libraries)
163
- lm_ggml_backend_buffer_t (*buffer_from_host_ptr)(lm_ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size);
164
-
165
- // check if the backend can compute an operation
166
- bool (*supports_op)(lm_ggml_backend_dev_t dev, const struct lm_ggml_tensor * op);
167
-
168
- // check if the backend can use tensors allocated in a buffer type
169
- bool (*supports_buft)(lm_ggml_backend_dev_t dev, lm_ggml_backend_buffer_type_t buft);
170
-
171
- // (optional) check if the backend wants to run an operation, even if the weights are allocated in an incompatible buffer
172
- // these should be expensive operations that may benefit from running on this backend instead of the CPU backend
173
- bool (*offload_op)(lm_ggml_backend_dev_t dev, const struct lm_ggml_tensor * op);
174
-
175
- // (optional) event synchronization
176
- lm_ggml_backend_event_t (*event_new) (lm_ggml_backend_dev_t dev);
177
- void (*event_free) (lm_ggml_backend_dev_t dev, lm_ggml_backend_event_t event);
178
- void (*event_synchronize) (lm_ggml_backend_dev_t dev, lm_ggml_backend_event_t event);
179
- };
180
-
181
- struct lm_ggml_backend_device {
182
- struct lm_ggml_backend_device_i iface;
183
- lm_ggml_backend_reg_t reg;
184
- void * context;
185
- };
186
-
187
- //
188
- // Backend (reg)
189
- //
190
-
191
- struct lm_ggml_backend_reg_i {
192
- const char * (*get_name)(lm_ggml_backend_reg_t reg);
193
-
194
- // enumerate available devices
195
- size_t (*get_device_count)(lm_ggml_backend_reg_t reg);
196
- lm_ggml_backend_dev_t (*get_device)(lm_ggml_backend_reg_t reg, size_t index);
197
-
198
- // (optional) get a pointer to a function in the backend
199
- // backends can add custom functions that are not part of the standard ggml-backend interface
200
- void * (*get_proc_address)(lm_ggml_backend_reg_t reg, const char * name);
201
- };
202
-
203
- struct lm_ggml_backend_reg {
204
- int api_version; // initialize to LM_GGML_BACKEND_API_VERSION
205
- struct lm_ggml_backend_reg_i iface;
206
- void * context;
207
- };
208
-
209
- // Internal backend registry API
210
- LM_GGML_API void lm_ggml_backend_register(lm_ggml_backend_reg_t reg);
211
- LM_GGML_API void lm_ggml_backend_device_register(lm_ggml_backend_dev_t device);
212
-
213
- // Add backend dynamic loading support to the backend
214
-
215
- // Initialize the backend
216
- typedef lm_ggml_backend_reg_t (*lm_ggml_backend_init_t)(void);
217
- // Optional: obtain a score for the backend based on the system configuration
218
- // Higher scores are preferred, 0 means the backend is not supported in the current system
219
- typedef int (*lm_ggml_backend_score_t)(void);
220
-
221
- #ifdef LM_GGML_BACKEND_DL
222
- # ifdef __cplusplus
223
- # define LM_GGML_BACKEND_DL_IMPL(reg_fn) \
224
- extern "C" { \
225
- LM_GGML_BACKEND_API lm_ggml_backend_reg_t lm_ggml_backend_init(void); \
226
- } \
227
- lm_ggml_backend_reg_t lm_ggml_backend_init(void) { \
228
- return reg_fn(); \
229
- }
230
- # define LM_GGML_BACKEND_DL_SCORE_IMPL(score_fn) \
231
- extern "C" { \
232
- LM_GGML_BACKEND_API int lm_ggml_backend_score(void); \
233
- } \
234
- int lm_ggml_backend_score(void) { \
235
- return score_fn(); \
236
- }
237
- # else
238
- # define LM_GGML_BACKEND_DL_IMPL(reg_fn) \
239
- LM_GGML_BACKEND_API lm_ggml_backend_reg_t lm_ggml_backend_init(void); \
240
- lm_ggml_backend_reg_t lm_ggml_backend_init(void) { \
241
- return reg_fn(); \
242
- }
243
- # define LM_GGML_BACKEND_DL_SCORE_IMPL(score_fn) \
244
- LM_GGML_BACKEND_API int lm_ggml_backend_score(void); \
245
- int lm_ggml_backend_score(void) { \
246
- return score_fn(); \
247
- }
248
- # endif
249
- #else
250
- # define LM_GGML_BACKEND_DL_IMPL(reg_fn)
251
- # define LM_GGML_BACKEND_DL_SCORE_IMPL(score_fn)
252
- #endif
253
-
254
- #ifdef __cplusplus
255
- }
256
- #endif
1
+ #pragma once
2
+
3
+ // ggml-backend internal header
4
+
5
+ #include "ggml-backend.h"
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+
11
+ #define LM_GGML_BACKEND_API_VERSION 1
12
+
13
+ //
14
+ // Backend buffer type
15
+ //
16
+
17
+ struct lm_ggml_backend_buffer_type_i {
18
+ const char * (*get_name) (lm_ggml_backend_buffer_type_t buft);
19
+ // allocate a buffer of this type
20
+ lm_ggml_backend_buffer_t (*alloc_buffer) (lm_ggml_backend_buffer_type_t buft, size_t size);
21
+ // tensor alignment
22
+ size_t (*get_alignment) (lm_ggml_backend_buffer_type_t buft);
23
+ // (optional) max buffer size that can be allocated (defaults to SIZE_MAX)
24
+ size_t (*get_max_size) (lm_ggml_backend_buffer_type_t buft);
25
+ // (optional) data size needed to allocate the tensor, including padding (defaults to lm_ggml_nbytes)
26
+ size_t (*get_alloc_size)(lm_ggml_backend_buffer_type_t buft, const struct lm_ggml_tensor * tensor);
27
+ // (optional) check if tensor data is in host memory and uses standard ggml tensor layout (defaults to false)
28
+ bool (*is_host) (lm_ggml_backend_buffer_type_t buft);
29
+ };
30
+
31
+ struct lm_ggml_backend_buffer_type {
32
+ struct lm_ggml_backend_buffer_type_i iface;
33
+ lm_ggml_backend_dev_t device;
34
+ void * context;
35
+ };
36
+
37
+ //
38
+ // Backend buffer
39
+ //
40
+
41
+ struct lm_ggml_backend_buffer_i {
42
+ // (optional) free the buffer
43
+ void (*free_buffer) (lm_ggml_backend_buffer_t buffer);
44
+ // base address of the buffer
45
+ void * (*get_base) (lm_ggml_backend_buffer_t buffer);
46
+ // (optional) initialize a tensor in the buffer (eg. add tensor extras)
47
+ void (*init_tensor) (lm_ggml_backend_buffer_t buffer, struct lm_ggml_tensor * tensor);
48
+ // tensor data access
49
+ void (*memset_tensor)(lm_ggml_backend_buffer_t buffer, struct lm_ggml_tensor * tensor, uint8_t value, size_t offset, size_t size);
50
+ void (*set_tensor) (lm_ggml_backend_buffer_t buffer, struct lm_ggml_tensor * tensor, const void * data, size_t offset, size_t size);
51
+ void (*get_tensor) (lm_ggml_backend_buffer_t buffer, const struct lm_ggml_tensor * tensor, void * data, size_t offset, size_t size);
52
+ // (optional) tensor copy: dst is in the buffer, src may be in any buffer, including buffers from a different backend (return false if not supported)
53
+ bool (*cpy_tensor) (lm_ggml_backend_buffer_t buffer, const struct lm_ggml_tensor * src, struct lm_ggml_tensor * dst);
54
+ // clear the entire buffer
55
+ void (*clear) (lm_ggml_backend_buffer_t buffer, uint8_t value);
56
+ // (optional) reset any internal state due to tensor initialization, such as tensor extras
57
+ void (*reset) (lm_ggml_backend_buffer_t buffer);
58
+ };
59
+
60
+ struct lm_ggml_backend_buffer {
61
+ struct lm_ggml_backend_buffer_i iface;
62
+ lm_ggml_backend_buffer_type_t buft;
63
+ void * context;
64
+ size_t size;
65
+ enum lm_ggml_backend_buffer_usage usage;
66
+ };
67
+
68
+ LM_GGML_API lm_ggml_backend_buffer_t lm_ggml_backend_buffer_init(
69
+ lm_ggml_backend_buffer_type_t buft,
70
+ struct lm_ggml_backend_buffer_i iface,
71
+ void * context,
72
+ size_t size);
73
+
74
+ // do not use directly, use lm_ggml_backend_tensor_copy instead
75
+ LM_GGML_API bool lm_ggml_backend_buffer_copy_tensor(const struct lm_ggml_tensor * src, struct lm_ggml_tensor * dst);
76
+
77
+ // multi-buffer
78
+ // buffer that contains a collection of buffers
79
+ LM_GGML_API lm_ggml_backend_buffer_t lm_ggml_backend_multi_buffer_alloc_buffer(lm_ggml_backend_buffer_t * buffers, size_t n_buffers);
80
+ LM_GGML_API bool lm_ggml_backend_buffer_is_multi_buffer(lm_ggml_backend_buffer_t buffer);
81
+ LM_GGML_API void lm_ggml_backend_multi_buffer_set_usage(lm_ggml_backend_buffer_t buffer, enum lm_ggml_backend_buffer_usage usage);
82
+
83
+ //
84
+ // Backend (stream)
85
+ //
86
+
87
+ struct lm_ggml_backend_i {
88
+ const char * (*get_name)(lm_ggml_backend_t backend);
89
+
90
+ void (*free)(lm_ggml_backend_t backend);
91
+
92
+ // (optional) asynchronous tensor data access
93
+ void (*set_tensor_async)(lm_ggml_backend_t backend, struct lm_ggml_tensor * tensor, const void * data, size_t offset, size_t size);
94
+ void (*get_tensor_async)(lm_ggml_backend_t backend, const struct lm_ggml_tensor * tensor, void * data, size_t offset, size_t size);
95
+ bool (*cpy_tensor_async)(lm_ggml_backend_t backend_src, lm_ggml_backend_t backend_dst, const struct lm_ggml_tensor * src, struct lm_ggml_tensor * dst);
96
+
97
+ // (optional) complete all pending operations (required if the backend supports async operations)
98
+ void (*synchronize)(lm_ggml_backend_t backend);
99
+
100
+ // (optional) graph plans (not used currently)
101
+ // compute graph with a plan
102
+ lm_ggml_backend_graph_plan_t (*graph_plan_create) (lm_ggml_backend_t backend, const struct lm_ggml_cgraph * cgraph);
103
+ void (*graph_plan_free) (lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan);
104
+ // update the plan with a new graph - this should be faster than creating a new plan when the graph has the same topology
105
+ void (*graph_plan_update) (lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan, const struct lm_ggml_cgraph * cgraph);
106
+ // compute the graph with the plan
107
+ enum lm_ggml_status (*graph_plan_compute)(lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan);
108
+
109
+ // compute graph (always async if supported by the backend)
110
+ enum lm_ggml_status (*graph_compute) (lm_ggml_backend_t backend, struct lm_ggml_cgraph * cgraph);
111
+
112
+ // (optional) event synchronization
113
+ // record an event on this stream
114
+ void (*event_record)(lm_ggml_backend_t backend, lm_ggml_backend_event_t event);
115
+ // wait for an event on on a different stream
116
+ void (*event_wait) (lm_ggml_backend_t backend, lm_ggml_backend_event_t event);
117
+ };
118
+
119
+ struct lm_ggml_backend {
120
+ lm_ggml_guid_t guid;
121
+ struct lm_ggml_backend_i iface;
122
+ lm_ggml_backend_dev_t device;
123
+ void * context;
124
+ };
125
+
126
+ struct lm_ggml_backend_event {
127
+ struct lm_ggml_backend_device * device;
128
+ void * context;
129
+ };
130
+
131
+ //
132
+ // Backend device
133
+ //
134
+
135
+ // Note: if additional properties are needed, we should add a struct with all of them
136
+ // the current functions to obtain the properties can remain, since they are more convenient for often used properties
137
+ struct lm_ggml_backend_device_i {
138
+ // device name: short identifier for this device, such as "CPU" or "CUDA0"
139
+ const char * (*get_name)(lm_ggml_backend_dev_t dev);
140
+
141
+ // device description: short informative description of the device, could be the model name
142
+ const char * (*get_description)(lm_ggml_backend_dev_t dev);
143
+
144
+ // device memory in bytes
145
+ void (*get_memory)(lm_ggml_backend_dev_t dev, size_t * free, size_t * total);
146
+
147
+ // device type
148
+ enum lm_ggml_backend_dev_type (*get_type)(lm_ggml_backend_dev_t dev);
149
+
150
+ // device properties
151
+ void (*get_props)(lm_ggml_backend_dev_t dev, struct lm_ggml_backend_dev_props * props);
152
+
153
+ // backend (stream) initialization
154
+ lm_ggml_backend_t (*init_backend)(lm_ggml_backend_dev_t dev, const char * params);
155
+
156
+ // preferred buffer type
157
+ lm_ggml_backend_buffer_type_t (*get_buffer_type)(lm_ggml_backend_dev_t dev);
158
+
159
+ // (optional) host buffer type (in system memory, typically this is a pinned memory buffer for faster transfers between host and device)
160
+ lm_ggml_backend_buffer_type_t (*get_host_buffer_type)(lm_ggml_backend_dev_t dev);
161
+
162
+ // (optional) buffer from pointer: create a buffer from a host pointer (useful for memory mapped models and importing data from other libraries)
163
+ lm_ggml_backend_buffer_t (*buffer_from_host_ptr)(lm_ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size);
164
+
165
+ // check if the backend can compute an operation
166
+ bool (*supports_op)(lm_ggml_backend_dev_t dev, const struct lm_ggml_tensor * op);
167
+
168
+ // check if the backend can use tensors allocated in a buffer type
169
+ bool (*supports_buft)(lm_ggml_backend_dev_t dev, lm_ggml_backend_buffer_type_t buft);
170
+
171
+ // (optional) check if the backend wants to run an operation, even if the weights are allocated in an incompatible buffer
172
+ // these should be expensive operations that may benefit from running on this backend instead of the CPU backend
173
+ bool (*offload_op)(lm_ggml_backend_dev_t dev, const struct lm_ggml_tensor * op);
174
+
175
+ // (optional) event synchronization
176
+ lm_ggml_backend_event_t (*event_new) (lm_ggml_backend_dev_t dev);
177
+ void (*event_free) (lm_ggml_backend_dev_t dev, lm_ggml_backend_event_t event);
178
+ void (*event_synchronize) (lm_ggml_backend_dev_t dev, lm_ggml_backend_event_t event);
179
+ };
180
+
181
+ struct lm_ggml_backend_device {
182
+ struct lm_ggml_backend_device_i iface;
183
+ lm_ggml_backend_reg_t reg;
184
+ void * context;
185
+ };
186
+
187
+ //
188
+ // Backend (reg)
189
+ //
190
+
191
+ struct lm_ggml_backend_reg_i {
192
+ const char * (*get_name)(lm_ggml_backend_reg_t reg);
193
+
194
+ // enumerate available devices
195
+ size_t (*get_device_count)(lm_ggml_backend_reg_t reg);
196
+ lm_ggml_backend_dev_t (*get_device)(lm_ggml_backend_reg_t reg, size_t index);
197
+
198
+ // (optional) get a pointer to a function in the backend
199
+ // backends can add custom functions that are not part of the standard ggml-backend interface
200
+ void * (*get_proc_address)(lm_ggml_backend_reg_t reg, const char * name);
201
+ };
202
+
203
+ struct lm_ggml_backend_reg {
204
+ int api_version; // initialize to LM_GGML_BACKEND_API_VERSION
205
+ struct lm_ggml_backend_reg_i iface;
206
+ void * context;
207
+ };
208
+
209
+ // Internal backend registry API
210
+ LM_GGML_API void lm_ggml_backend_register(lm_ggml_backend_reg_t reg);
211
+
212
+ // Add backend dynamic loading support to the backend
213
+
214
+ // Initialize the backend
215
+ typedef lm_ggml_backend_reg_t (*lm_ggml_backend_init_t)(void);
216
+ // Optional: obtain a score for the backend based on the system configuration
217
+ // Higher scores are preferred, 0 means the backend is not supported in the current system
218
+ typedef int (*lm_ggml_backend_score_t)(void);
219
+
220
+ #ifdef LM_GGML_BACKEND_DL
221
+ # ifdef __cplusplus
222
+ # define LM_GGML_BACKEND_DL_IMPL(reg_fn) \
223
+ extern "C" { \
224
+ LM_GGML_BACKEND_API lm_ggml_backend_reg_t lm_ggml_backend_init(void); \
225
+ } \
226
+ lm_ggml_backend_reg_t lm_ggml_backend_init(void) { \
227
+ return reg_fn(); \
228
+ }
229
+ # define LM_GGML_BACKEND_DL_SCORE_IMPL(score_fn) \
230
+ extern "C" { \
231
+ LM_GGML_BACKEND_API int lm_ggml_backend_score(void); \
232
+ } \
233
+ int lm_ggml_backend_score(void) { \
234
+ return score_fn(); \
235
+ }
236
+ # else
237
+ # define LM_GGML_BACKEND_DL_IMPL(reg_fn) \
238
+ LM_GGML_BACKEND_API lm_ggml_backend_reg_t lm_ggml_backend_init(void); \
239
+ lm_ggml_backend_reg_t lm_ggml_backend_init(void) { \
240
+ return reg_fn(); \
241
+ }
242
+ # define LM_GGML_BACKEND_DL_SCORE_IMPL(score_fn) \
243
+ LM_GGML_BACKEND_API int lm_ggml_backend_score(void); \
244
+ int lm_ggml_backend_score(void) { \
245
+ return score_fn(); \
246
+ }
247
+ # endif
248
+ #else
249
+ # define LM_GGML_BACKEND_DL_IMPL(reg_fn)
250
+ # define LM_GGML_BACKEND_DL_SCORE_IMPL(score_fn)
251
+ #endif
252
+
253
+ #ifdef __cplusplus
254
+ }
255
+ #endif