cui-llama.rn 1.3.0 → 1.3.3

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 (76) hide show
  1. package/android/src/main/CMakeLists.txt +6 -1
  2. package/android/src/main/jni.cpp +6 -6
  3. package/cpp/amx/amx.cpp +196 -0
  4. package/cpp/amx/amx.h +20 -0
  5. package/cpp/amx/common.h +101 -0
  6. package/cpp/amx/mmq.cpp +2524 -0
  7. package/cpp/amx/mmq.h +16 -0
  8. package/cpp/common.cpp +1981 -1682
  9. package/cpp/common.h +636 -600
  10. package/cpp/ggml-aarch64.c +129 -129
  11. package/cpp/ggml-aarch64.h +19 -19
  12. package/cpp/ggml-alloc.c +1038 -1040
  13. package/cpp/ggml-alloc.h +76 -76
  14. package/cpp/ggml-backend-impl.h +238 -216
  15. package/cpp/ggml-backend-reg.cpp +423 -195
  16. package/cpp/ggml-backend.cpp +1999 -1997
  17. package/cpp/ggml-backend.h +351 -328
  18. package/cpp/ggml-common.h +1859 -1853
  19. package/cpp/ggml-cpp.h +38 -38
  20. package/cpp/ggml-cpu-aarch64.c +3823 -3560
  21. package/cpp/ggml-cpu-aarch64.h +32 -30
  22. package/cpp/ggml-cpu-impl.h +386 -371
  23. package/cpp/ggml-cpu-quants.c +10835 -10822
  24. package/cpp/ggml-cpu-quants.h +63 -63
  25. package/cpp/ggml-cpu.c +99 -103
  26. package/cpp/ggml-cpu.cpp +69 -17
  27. package/cpp/ggml-cpu.h +152 -177
  28. package/cpp/ggml-impl.h +556 -550
  29. package/cpp/ggml-metal.h +66 -66
  30. package/cpp/ggml-metal.m +4426 -4294
  31. package/cpp/ggml-quants.c +5247 -5247
  32. package/cpp/ggml-quants.h +100 -100
  33. package/cpp/ggml-threading.cpp +12 -12
  34. package/cpp/ggml-threading.h +12 -12
  35. package/cpp/ggml.c +7618 -8180
  36. package/cpp/ggml.h +2255 -2411
  37. package/cpp/json-schema-to-grammar.cpp +1045 -0
  38. package/cpp/json-schema-to-grammar.h +8 -0
  39. package/cpp/json.hpp +24766 -0
  40. package/cpp/llama-grammar.cpp +1138 -1138
  41. package/cpp/llama-grammar.h +144 -144
  42. package/cpp/llama-impl.h +181 -181
  43. package/cpp/llama-sampling.cpp +2348 -2348
  44. package/cpp/llama-sampling.h +48 -48
  45. package/cpp/llama-vocab.cpp +1984 -1984
  46. package/cpp/llama-vocab.h +170 -170
  47. package/cpp/llama.cpp +22332 -22132
  48. package/cpp/llama.h +1259 -1253
  49. package/cpp/log.cpp +401 -401
  50. package/cpp/log.h +121 -121
  51. package/cpp/rn-llama.hpp +6 -6
  52. package/cpp/sampling.cpp +505 -466
  53. package/cpp/sampling.h +22 -1
  54. package/cpp/sgemm.cpp +1884 -1884
  55. package/cpp/speculative.cpp +270 -0
  56. package/cpp/speculative.h +28 -0
  57. package/cpp/unicode.cpp +11 -0
  58. package/ios/RNLlamaContext.mm +13 -0
  59. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  60. package/lib/commonjs/grammar.js +4 -2
  61. package/lib/commonjs/grammar.js.map +1 -1
  62. package/lib/commonjs/index.js.map +1 -1
  63. package/lib/module/NativeRNLlama.js.map +1 -1
  64. package/lib/module/grammar.js +2 -1
  65. package/lib/module/grammar.js.map +1 -1
  66. package/lib/module/index.js.map +1 -1
  67. package/lib/typescript/NativeRNLlama.d.ts +94 -4
  68. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  69. package/lib/typescript/grammar.d.ts +5 -6
  70. package/lib/typescript/grammar.d.ts.map +1 -1
  71. package/lib/typescript/index.d.ts +4 -2
  72. package/lib/typescript/index.d.ts.map +1 -1
  73. package/package.json +2 -1
  74. package/src/NativeRNLlama.ts +97 -10
  75. package/src/grammar.ts +10 -8
  76. package/src/index.ts +22 -1
package/cpp/ggml-alloc.h CHANGED
@@ -1,76 +1,76 @@
1
- #pragma once
2
-
3
- #include "ggml.h"
4
-
5
- #ifdef __cplusplus
6
- extern "C" {
7
- #endif
8
-
9
- typedef struct lm_ggml_backend_buffer_type * lm_ggml_backend_buffer_type_t;
10
- typedef struct lm_ggml_backend_buffer * lm_ggml_backend_buffer_t;
11
- typedef struct lm_ggml_backend * lm_ggml_backend_t;
12
-
13
- // Tensor allocator
14
- struct lm_ggml_tallocr {
15
- lm_ggml_backend_buffer_t buffer;
16
- void * base;
17
- size_t alignment;
18
- size_t offset;
19
- };
20
-
21
- LM_GGML_API struct lm_ggml_tallocr lm_ggml_tallocr_new(lm_ggml_backend_buffer_t buffer);
22
- LM_GGML_API void lm_ggml_tallocr_alloc(struct lm_ggml_tallocr * talloc, struct lm_ggml_tensor * tensor);
23
-
24
- // Graph allocator
25
- /*
26
- Example usage:
27
- lm_ggml_gallocr_t galloc = lm_ggml_gallocr_new(lm_ggml_backend_cpu_buffer_type());
28
-
29
- // optional: create a worst-case graph and reserve the buffers to avoid reallocations
30
- lm_ggml_gallocr_reserve(galloc, build_graph(max_batch));
31
-
32
- // allocate the graph
33
- struct lm_ggml_cgraph * graph = build_graph(batch);
34
- lm_ggml_gallocr_alloc_graph(galloc, graph);
35
-
36
- printf("compute buffer size: %zu bytes\n", lm_ggml_gallocr_get_buffer_size(galloc, 0));
37
-
38
- // evaluate the graph
39
- lm_ggml_backend_graph_compute(backend, graph);
40
- */
41
-
42
- // special tensor flags for use with the graph allocator:
43
- // lm_ggml_set_input(): all input tensors are allocated at the beginning of the graph in non-overlapping addresses
44
- // lm_ggml_set_output(): output tensors are never freed and never overwritten
45
-
46
- typedef struct lm_ggml_gallocr * lm_ggml_gallocr_t;
47
-
48
- LM_GGML_API lm_ggml_gallocr_t lm_ggml_gallocr_new(lm_ggml_backend_buffer_type_t buft);
49
- LM_GGML_API lm_ggml_gallocr_t lm_ggml_gallocr_new_n(lm_ggml_backend_buffer_type_t * bufts, int n_bufs);
50
- LM_GGML_API void lm_ggml_gallocr_free(lm_ggml_gallocr_t galloc);
51
-
52
- // pre-allocate buffers from a measure graph - does not allocate or modify the graph
53
- // call with a worst-case graph to avoid buffer reallocations
54
- // not strictly required for single buffer usage: lm_ggml_gallocr_alloc_graph will reallocate the buffers automatically if needed
55
- // returns false if the buffer allocation failed
56
- LM_GGML_API bool lm_ggml_gallocr_reserve(lm_ggml_gallocr_t galloc, struct lm_ggml_cgraph * graph);
57
- LM_GGML_API bool lm_ggml_gallocr_reserve_n(
58
- lm_ggml_gallocr_t galloc,
59
- struct lm_ggml_cgraph * graph,
60
- const int * node_buffer_ids,
61
- const int * leaf_buffer_ids);
62
-
63
- // automatic reallocation if the topology changes when using a single buffer
64
- // returns false if using multiple buffers and a re-allocation is needed (call lm_ggml_gallocr_reserve_n first to set the node buffers)
65
- LM_GGML_API bool lm_ggml_gallocr_alloc_graph(lm_ggml_gallocr_t galloc, struct lm_ggml_cgraph * graph);
66
-
67
- LM_GGML_API size_t lm_ggml_gallocr_get_buffer_size(lm_ggml_gallocr_t galloc, int buffer_id);
68
-
69
- // Utils
70
- // Create a buffer and allocate all the tensors in a lm_ggml_context
71
- LM_GGML_API struct lm_ggml_backend_buffer * lm_ggml_backend_alloc_ctx_tensors_from_buft(struct lm_ggml_context * ctx, lm_ggml_backend_buffer_type_t buft);
72
- LM_GGML_API struct lm_ggml_backend_buffer * lm_ggml_backend_alloc_ctx_tensors(struct lm_ggml_context * ctx, lm_ggml_backend_t backend);
73
-
74
- #ifdef __cplusplus
75
- }
76
- #endif
1
+ #pragma once
2
+
3
+ #include "ggml.h"
4
+
5
+ #ifdef __cplusplus
6
+ extern "C" {
7
+ #endif
8
+
9
+ typedef struct lm_ggml_backend_buffer_type * lm_ggml_backend_buffer_type_t;
10
+ typedef struct lm_ggml_backend_buffer * lm_ggml_backend_buffer_t;
11
+ typedef struct lm_ggml_backend * lm_ggml_backend_t;
12
+
13
+ // Tensor allocator
14
+ struct lm_ggml_tallocr {
15
+ lm_ggml_backend_buffer_t buffer;
16
+ void * base;
17
+ size_t alignment;
18
+ size_t offset;
19
+ };
20
+
21
+ LM_GGML_API struct lm_ggml_tallocr lm_ggml_tallocr_new(lm_ggml_backend_buffer_t buffer);
22
+ LM_GGML_API void lm_ggml_tallocr_alloc(struct lm_ggml_tallocr * talloc, struct lm_ggml_tensor * tensor);
23
+
24
+ // Graph allocator
25
+ /*
26
+ Example usage:
27
+ lm_ggml_gallocr_t galloc = lm_ggml_gallocr_new(lm_ggml_backend_cpu_buffer_type());
28
+
29
+ // optional: create a worst-case graph and reserve the buffers to avoid reallocations
30
+ lm_ggml_gallocr_reserve(galloc, build_graph(max_batch));
31
+
32
+ // allocate the graph
33
+ struct lm_ggml_cgraph * graph = build_graph(batch);
34
+ lm_ggml_gallocr_alloc_graph(galloc, graph);
35
+
36
+ printf("compute buffer size: %zu bytes\n", lm_ggml_gallocr_get_buffer_size(galloc, 0));
37
+
38
+ // evaluate the graph
39
+ lm_ggml_backend_graph_compute(backend, graph);
40
+ */
41
+
42
+ // special tensor flags for use with the graph allocator:
43
+ // lm_ggml_set_input(): all input tensors are allocated at the beginning of the graph in non-overlapping addresses
44
+ // lm_ggml_set_output(): output tensors are never freed and never overwritten
45
+
46
+ typedef struct lm_ggml_gallocr * lm_ggml_gallocr_t;
47
+
48
+ LM_GGML_API lm_ggml_gallocr_t lm_ggml_gallocr_new(lm_ggml_backend_buffer_type_t buft);
49
+ LM_GGML_API lm_ggml_gallocr_t lm_ggml_gallocr_new_n(lm_ggml_backend_buffer_type_t * bufts, int n_bufs);
50
+ LM_GGML_API void lm_ggml_gallocr_free(lm_ggml_gallocr_t galloc);
51
+
52
+ // pre-allocate buffers from a measure graph - does not allocate or modify the graph
53
+ // call with a worst-case graph to avoid buffer reallocations
54
+ // not strictly required for single buffer usage: lm_ggml_gallocr_alloc_graph will reallocate the buffers automatically if needed
55
+ // returns false if the buffer allocation failed
56
+ LM_GGML_API bool lm_ggml_gallocr_reserve(lm_ggml_gallocr_t galloc, struct lm_ggml_cgraph * graph);
57
+ LM_GGML_API bool lm_ggml_gallocr_reserve_n(
58
+ lm_ggml_gallocr_t galloc,
59
+ struct lm_ggml_cgraph * graph,
60
+ const int * node_buffer_ids,
61
+ const int * leaf_buffer_ids);
62
+
63
+ // automatic reallocation if the topology changes when using a single buffer
64
+ // returns false if using multiple buffers and a re-allocation is needed (call lm_ggml_gallocr_reserve_n first to set the node buffers)
65
+ LM_GGML_API bool lm_ggml_gallocr_alloc_graph(lm_ggml_gallocr_t galloc, struct lm_ggml_cgraph * graph);
66
+
67
+ LM_GGML_API size_t lm_ggml_gallocr_get_buffer_size(lm_ggml_gallocr_t galloc, int buffer_id);
68
+
69
+ // Utils
70
+ // Create a buffer and allocate all the tensors in a lm_ggml_context
71
+ LM_GGML_API struct lm_ggml_backend_buffer * lm_ggml_backend_alloc_ctx_tensors_from_buft(struct lm_ggml_context * ctx, lm_ggml_backend_buffer_type_t buft);
72
+ LM_GGML_API struct lm_ggml_backend_buffer * lm_ggml_backend_alloc_ctx_tensors(struct lm_ggml_context * ctx, lm_ggml_backend_t backend);
73
+
74
+ #ifdef __cplusplus
75
+ }
76
+ #endif
@@ -1,216 +1,238 @@
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
- //
12
- // Backend buffer type
13
- //
14
-
15
- struct lm_ggml_backend_buffer_type_i {
16
- const char * (*get_name) (lm_ggml_backend_buffer_type_t buft);
17
- // allocate a buffer of this type
18
- lm_ggml_backend_buffer_t (*alloc_buffer) (lm_ggml_backend_buffer_type_t buft, size_t size);
19
- // tensor alignment
20
- size_t (*get_alignment) (lm_ggml_backend_buffer_type_t buft);
21
- // (optional) max buffer size that can be allocated (defaults to SIZE_MAX)
22
- size_t (*get_max_size) (lm_ggml_backend_buffer_type_t buft);
23
- // (optional) data size needed to allocate the tensor, including padding (defaults to lm_ggml_nbytes)
24
- size_t (*get_alloc_size)(lm_ggml_backend_buffer_type_t buft, const struct lm_ggml_tensor * tensor);
25
- // (optional) check if tensor data is in host memory and uses standard ggml tensor layout (defaults to false)
26
- bool (*is_host) (lm_ggml_backend_buffer_type_t buft);
27
- };
28
-
29
- struct lm_ggml_backend_buffer_type {
30
- struct lm_ggml_backend_buffer_type_i iface;
31
- lm_ggml_backend_dev_t device;
32
- void * context;
33
- };
34
-
35
- //
36
- // Backend buffer
37
- //
38
-
39
- struct lm_ggml_backend_buffer_i {
40
- // (optional) free the buffer
41
- void (*free_buffer) (lm_ggml_backend_buffer_t buffer);
42
- // base address of the buffer
43
- void * (*get_base) (lm_ggml_backend_buffer_t buffer);
44
- // (optional) initialize a tensor in the buffer (eg. add tensor extras)
45
- void (*init_tensor) (lm_ggml_backend_buffer_t buffer, struct lm_ggml_tensor * tensor);
46
- // tensor data access
47
- void (*memset_tensor)(lm_ggml_backend_buffer_t buffer, struct lm_ggml_tensor * tensor, uint8_t value, size_t offset, size_t size);
48
- void (*set_tensor) (lm_ggml_backend_buffer_t buffer, struct lm_ggml_tensor * tensor, const void * data, size_t offset, size_t size);
49
- void (*get_tensor) (lm_ggml_backend_buffer_t buffer, const struct lm_ggml_tensor * tensor, void * data, size_t offset, size_t size);
50
- // (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)
51
- bool (*cpy_tensor) (lm_ggml_backend_buffer_t buffer, const struct lm_ggml_tensor * src, struct lm_ggml_tensor * dst);
52
- // clear the entire buffer
53
- void (*clear) (lm_ggml_backend_buffer_t buffer, uint8_t value);
54
- // (optional) reset any internal state due to tensor initialization, such as tensor extras
55
- void (*reset) (lm_ggml_backend_buffer_t buffer);
56
- };
57
-
58
- struct lm_ggml_backend_buffer {
59
- struct lm_ggml_backend_buffer_i iface;
60
- lm_ggml_backend_buffer_type_t buft;
61
- void * context;
62
- size_t size;
63
- enum lm_ggml_backend_buffer_usage usage;
64
- };
65
-
66
- lm_ggml_backend_buffer_t lm_ggml_backend_buffer_init(
67
- lm_ggml_backend_buffer_type_t buft,
68
- struct lm_ggml_backend_buffer_i iface,
69
- void * context,
70
- size_t size);
71
-
72
- // do not use directly, use lm_ggml_backend_tensor_copy instead
73
- bool lm_ggml_backend_buffer_copy_tensor(const struct lm_ggml_tensor * src, struct lm_ggml_tensor * dst);
74
-
75
- // multi-buffer
76
- // buffer that contains a collection of buffers
77
- lm_ggml_backend_buffer_t lm_ggml_backend_multi_buffer_alloc_buffer(lm_ggml_backend_buffer_t * buffers, size_t n_buffers);
78
- bool lm_ggml_backend_buffer_is_multi_buffer(lm_ggml_backend_buffer_t buffer);
79
- void lm_ggml_backend_multi_buffer_set_usage(lm_ggml_backend_buffer_t buffer, enum lm_ggml_backend_buffer_usage usage);
80
-
81
- //
82
- // Backend (stream)
83
- //
84
-
85
- struct lm_ggml_backend_i {
86
- const char * (*get_name)(lm_ggml_backend_t backend);
87
-
88
- void (*free)(lm_ggml_backend_t backend);
89
-
90
- // (optional) asynchronous tensor data access
91
- void (*set_tensor_async)(lm_ggml_backend_t backend, struct lm_ggml_tensor * tensor, const void * data, size_t offset, size_t size);
92
- void (*get_tensor_async)(lm_ggml_backend_t backend, const struct lm_ggml_tensor * tensor, void * data, size_t offset, size_t size);
93
- 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);
94
-
95
- // (optional) complete all pending operations (required if the backend supports async operations)
96
- void (*synchronize)(lm_ggml_backend_t backend);
97
-
98
- // (optional) graph plans (not used currently)
99
- // compute graph with a plan
100
- lm_ggml_backend_graph_plan_t (*graph_plan_create) (lm_ggml_backend_t backend, const struct lm_ggml_cgraph * cgraph);
101
- void (*graph_plan_free) (lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan);
102
- // update the plan with a new graph - this should be faster than creating a new plan when the graph has the same topology
103
- void (*graph_plan_update) (lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan, const struct lm_ggml_cgraph * cgraph);
104
- // compute the graph with the plan
105
- enum lm_ggml_status (*graph_plan_compute)(lm_ggml_backend_t backend, lm_ggml_backend_graph_plan_t plan);
106
-
107
- // compute graph (always async if supported by the backend)
108
- enum lm_ggml_status (*graph_compute) (lm_ggml_backend_t backend, struct lm_ggml_cgraph * cgraph);
109
-
110
- // (optional) event synchronization
111
- // record an event on this stream
112
- void (*event_record)(lm_ggml_backend_t backend, lm_ggml_backend_event_t event);
113
- // wait for an event on on a different stream
114
- void (*event_wait) (lm_ggml_backend_t backend, lm_ggml_backend_event_t event);
115
- };
116
-
117
- struct lm_ggml_backend {
118
- lm_ggml_guid_t guid;
119
- struct lm_ggml_backend_i iface;
120
- lm_ggml_backend_dev_t device;
121
- void * context;
122
- };
123
-
124
- struct lm_ggml_backend_event {
125
- struct lm_ggml_backend_device * device;
126
- void * context;
127
- };
128
-
129
- //
130
- // Backend device
131
- //
132
-
133
- // Note: if additional properties are needed, we should add a struct with all of them
134
- // the current functions to obtain the properties can remain, since they are more convenient for often used properties
135
- struct lm_ggml_backend_device_i {
136
- // device name: short identifier for this device, such as "CPU" or "CUDA0"
137
- const char * (*get_name)(lm_ggml_backend_dev_t dev);
138
-
139
- // device description: short informative description of the device, could be the model name
140
- const char * (*get_description)(lm_ggml_backend_dev_t dev);
141
-
142
- // device memory in bytes
143
- void (*get_memory)(lm_ggml_backend_dev_t dev, size_t * free, size_t * total);
144
-
145
- // device type
146
- enum lm_ggml_backend_dev_type (*get_type)(lm_ggml_backend_dev_t dev);
147
-
148
- // device properties
149
- void (*get_props)(lm_ggml_backend_dev_t dev, struct lm_ggml_backend_dev_props * props);
150
-
151
- // backend (stream) initialization
152
- lm_ggml_backend_t (*init_backend)(lm_ggml_backend_dev_t dev, const char * params);
153
-
154
- // preferred buffer type
155
- lm_ggml_backend_buffer_type_t (*get_buffer_type)(lm_ggml_backend_dev_t dev);
156
-
157
- // (optional) host buffer type (in system memory, typically this is a pinned memory buffer for faster transfers between host and device)
158
- lm_ggml_backend_buffer_type_t (*get_host_buffer_type)(lm_ggml_backend_dev_t dev);
159
-
160
- // (optional) buffer from pointer: create a buffer from a host pointer (useful for memory mapped models and importing data from other libraries)
161
- 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);
162
-
163
- // check if the backend can compute an operation
164
- bool (*supports_op)(lm_ggml_backend_dev_t dev, const struct lm_ggml_tensor * op);
165
-
166
- // check if the backend can use tensors allocated in a buffer type
167
- bool (*supports_buft)(lm_ggml_backend_dev_t dev, lm_ggml_backend_buffer_type_t buft);
168
-
169
- // (optional) check if the backend wants to run an operation, even if the weights are allocated in an incompatible buffer
170
- // these should be expensive operations that may benefit from running on this backend instead of the CPU backend
171
- bool (*offload_op)(lm_ggml_backend_dev_t dev, const struct lm_ggml_tensor * op);
172
-
173
- // (optional) event synchronization
174
- lm_ggml_backend_event_t (*event_new) (lm_ggml_backend_dev_t dev);
175
- void (*event_free) (lm_ggml_backend_dev_t dev, lm_ggml_backend_event_t event);
176
- void (*event_synchronize) (lm_ggml_backend_dev_t dev, lm_ggml_backend_event_t event);
177
- };
178
-
179
- struct lm_ggml_backend_device {
180
- struct lm_ggml_backend_device_i iface;
181
- lm_ggml_backend_reg_t reg;
182
- void * context;
183
- };
184
-
185
- //
186
- // Backend (reg)
187
- //
188
-
189
- struct lm_ggml_backend_reg_i {
190
- const char * (*get_name)(lm_ggml_backend_reg_t reg);
191
-
192
- // enumerate available devices
193
- size_t (*get_device_count)(lm_ggml_backend_reg_t reg);
194
- lm_ggml_backend_dev_t (*get_device)(lm_ggml_backend_reg_t reg, size_t index);
195
-
196
- // (optional) get a pointer to a function in the backend
197
- // backends can add custom functions that are not part of the standard ggml-backend interface
198
- void * (*get_proc_address)(lm_ggml_backend_reg_t reg, const char * name);
199
- };
200
-
201
- struct lm_ggml_backend_reg {
202
- // int api_version; // TODO: for dynamic loading
203
- struct lm_ggml_backend_reg_i iface;
204
- void * context;
205
- };
206
-
207
-
208
- // Internal backend registry API
209
- void lm_ggml_backend_register(lm_ggml_backend_reg_t reg);
210
- void lm_ggml_backend_device_register(lm_ggml_backend_dev_t device);
211
- // TODO: backends can be loaded as a dynamic library, in which case it needs to export this function
212
- // typedef lm_ggml_backend_register_t * (*lm_ggml_backend_init)(void);
213
-
214
- #ifdef __cplusplus
215
- }
216
- #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
+ 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
+ typedef lm_ggml_backend_reg_t (*lm_ggml_backend_init_t)(void);
215
+
216
+ #ifdef LM_GGML_BACKEND_DL
217
+ #ifdef __cplusplus
218
+ # define LM_GGML_BACKEND_DL_IMPL(reg_fn) \
219
+ extern "C" { \
220
+ LM_GGML_BACKEND_API lm_ggml_backend_reg_t lm_ggml_backend_init(void); \
221
+ } \
222
+ lm_ggml_backend_reg_t lm_ggml_backend_init(void) { \
223
+ return reg_fn(); \
224
+ }
225
+ #else
226
+ # define LM_GGML_BACKEND_DL_IMPL(reg_fn) \
227
+ LM_GGML_BACKEND_API lm_ggml_backend_reg_t lm_ggml_backend_init(void); \
228
+ lm_ggml_backend_reg_t lm_ggml_backend_init(void) { \
229
+ return reg_fn(); \
230
+ }
231
+ #endif
232
+ #else
233
+ # define LM_GGML_BACKEND_DL_IMPL(reg_fn)
234
+ #endif
235
+
236
+ #ifdef __cplusplus
237
+ }
238
+ #endif