ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-backend-impl.h
| 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 GGML_BACKEND_API_VERSION 3 |
| 12 | |
| 13 | // |
| 14 | // Backend buffer type |
| 15 | // |
| 16 | |
| 17 | struct ggml_backend_buffer_type_i { |
| 18 | const char * (*get_name) (ggml_backend_buffer_type_t buft); |
| 19 | // allocate a buffer of this type |
| 20 | ggml_backend_buffer_t (*alloc_buffer) (ggml_backend_buffer_type_t buft, size_t size); |
| 21 | // (optional) allocate tensors from a list into a buffer of this type (defaults to alloc_buffer + linear allocator) |
| 22 | ggml_backend_buffer_t (*alloc_buffer_n) (ggml_backend_buffer_type_t buft, struct ggml_tensor ** tensors, int n_tensors); |
| 23 | // tensor alignment |
| 24 | size_t (*get_alignment) (ggml_backend_buffer_type_t buft); |
| 25 | // (optional) max buffer size that can be allocated (defaults to SIZE_MAX) |
| 26 | size_t (*get_max_size) (ggml_backend_buffer_type_t buft); |
| 27 | // (optional) data size needed to allocate the tensor, including padding (defaults to ggml_nbytes) |
| 28 | size_t (*get_alloc_size) (ggml_backend_buffer_type_t buft, const struct ggml_tensor * tensor); |
| 29 | // (optional) total data size needed to allocate the given tensors, including padding and splitting (defaults to per-tensor get_alloc_size) |
| 30 | size_t (*get_alloc_size_n)(ggml_backend_buffer_type_t buft, struct ggml_tensor ** tensors, int n_tensors); |
| 31 | // (optional) check if tensor data is in host memory and uses standard ggml tensor layout (defaults to false) |
| 32 | bool (*is_host) (ggml_backend_buffer_type_t buft); |
| 33 | }; |
| 34 | |
| 35 | struct ggml_backend_buffer_type { |
| 36 | struct ggml_backend_buffer_type_i iface; |
| 37 | ggml_backend_dev_t device; |
| 38 | void * context; |
| 39 | }; |
| 40 | |
| 41 | // [TAG_ALLOC_SIZE_EXPAND] |
| 42 | // returns true for ops that may require additional memory for fleeting data on some backends, |
| 43 | // i.e. the backend buffer type's get_alloc_size may return more than ggml_nbytes for the output tensor |
| 44 | GGML_API bool ggml_op_alloc_size_may_expand(enum ggml_op op); |
| 45 | |
| 46 | // |
| 47 | // Backend buffer |
| 48 | // |
| 49 | |
| 50 | // TODO: remove `set_tensor`, `get_tensor`, `set_tensor_2d`, `get_tensor_2d` and `cpy_tensor` from the buffer interface |
| 51 | // they are superseded by the `set_tensor_n`, `get_tensor_n`, `cpy_tensor_n` interface |
| 52 | // ref: https://github.com/ggml-org/llama.cpp/pull/30881#discussion_r4400299490 |
| 53 | struct ggml_backend_buffer_i { |
| 54 | // (optional) free the buffer |
| 55 | void (*free_buffer) (ggml_backend_buffer_t buffer); |
| 56 | // base address of the buffer |
| 57 | void * (*get_base) (ggml_backend_buffer_t buffer); |
| 58 | // (optional) initialize a tensor in the buffer (eg. add tensor extras) |
| 59 | enum ggml_status (*init_tensor)(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor); |
| 60 | // tensor data access |
| 61 | void (*memset_tensor)(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, uint8_t value, size_t offset, size_t size); |
| 62 | void (*set_tensor) (ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size); |
| 63 | void (*get_tensor) (ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size); |
| 64 | // (optional) 2d data copies |
| 65 | void (*set_tensor_2d)(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size, size_t n_copies, size_t stride_tensor, size_t stride_data); |
| 66 | void (*get_tensor_2d)(ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size, size_t n_copies, size_t stride_tensor, size_t stride_data); |
| 67 | |
| 68 | // (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) |
| 69 | bool (*cpy_tensor) (ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst); |
| 70 | // clear the entire buffer |
| 71 | void (*clear) (ggml_backend_buffer_t buffer, uint8_t value); |
| 72 | // (optional) reset any internal state due to tensor initialization, such as tensor extras |
| 73 | void (*reset) (ggml_backend_buffer_t buffer); |
| 74 | // (optional) batched tensor data copies |
| 75 | void (*set_tensor_n) (ggml_backend_buffer_t buffer, struct ggml_tensor * const * tensors, const void * const * data, const size_t * offsets, const size_t * sizes, size_t n_tensors); |
| 76 | void (*get_tensor_n) (ggml_backend_buffer_t buffer, const struct ggml_tensor * const * tensors, void * const * data, const size_t * offsets, const size_t * sizes, size_t n_tensors); |
| 77 | // (optional) batched tensor copies: returns true if all copies were performed, false if unsupported and no copies were performed |
| 78 | bool (*cpy_tensor_n) (ggml_backend_buffer_t buffer, const struct ggml_tensor * const * srcs, struct ggml_tensor * const * dsts, size_t n_tensors); |
| 79 | }; |
| 80 | |
| 81 | struct ggml_backend_buffer { |
| 82 | struct ggml_backend_buffer_i iface; |
| 83 | ggml_backend_buffer_type_t buft; |
| 84 | void * context; |
| 85 | size_t size; |
| 86 | enum ggml_backend_buffer_usage usage; |
| 87 | }; |
| 88 | |
| 89 | GGML_API ggml_backend_buffer_t ggml_backend_buffer_init( |
| 90 | ggml_backend_buffer_type_t buft, |
| 91 | struct ggml_backend_buffer_i iface, |
| 92 | void * context, |
| 93 | size_t size); |
| 94 | |
| 95 | // do not use directly, use ggml_backend_tensor_copy instead |
| 96 | GGML_API bool ggml_backend_buffer_copy_tensor(const struct ggml_tensor * src, struct ggml_tensor * dst); |
| 97 | |
| 98 | // multi-buffer |
| 99 | // buffer that contains a collection of buffers |
| 100 | GGML_API ggml_backend_buffer_t ggml_backend_multi_buffer_alloc_buffer(ggml_backend_buffer_t * buffers, size_t n_buffers); |
| 101 | GGML_API bool ggml_backend_buffer_is_multi_buffer(ggml_backend_buffer_t buffer); |
| 102 | GGML_API void ggml_backend_multi_buffer_set_usage(ggml_backend_buffer_t buffer, enum ggml_backend_buffer_usage usage); |
| 103 | GGML_API void ggml_backend_meta_buffer_set_usage (ggml_backend_buffer_t buffer, enum ggml_backend_buffer_usage usage); |
| 104 | |
| 105 | // |
| 106 | // Backend (meta) |
| 107 | // |
| 108 | |
| 109 | GGML_API bool ggml_backend_is_meta (ggml_backend_t backend); |
| 110 | GGML_API bool ggml_backend_buffer_is_meta(ggml_backend_buffer_t buf); |
| 111 | GGML_API bool ggml_backend_buft_is_meta (ggml_backend_buffer_type_t buft); |
| 112 | |
| 113 | GGML_API size_t ggml_backend_meta_n_backends (ggml_backend_t meta_backend); |
| 114 | GGML_API ggml_backend_t ggml_backend_meta_simple_backend(ggml_backend_t meta_backend, size_t index); |
| 115 | |
| 116 | // |
| 117 | // Backend (stream) |
| 118 | // |
| 119 | |
| 120 | // passed to graph_optimize so the backend can add allocation dependencies: |
| 121 | // if the backend executes parts of the graph out of order (e.g. on concurrent streams), |
| 122 | // it must keep the affected tensors allocated until a node where execution is known to have joined |
| 123 | struct ggml_backend_graph_optimize_params { |
| 124 | // keep `tensor` allocated at least until `until` (a node of the same graph) has been computed |
| 125 | // can be called multiple times for the same tensor: the longest lifetime applies |
| 126 | void (*add_alloc_dep)(void * user_data, struct ggml_tensor * tensor, struct ggml_tensor * until); |
| 127 | void * user_data; |
| 128 | }; |
| 129 | |
| 130 | struct ggml_backend_i { |
| 131 | const char * (*get_name)(ggml_backend_t backend); |
| 132 | |
| 133 | void (*free)(ggml_backend_t backend); |
| 134 | |
| 135 | // (optional) asynchronous tensor data access |
| 136 | void (*set_tensor_async) (ggml_backend_t backend, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size); |
| 137 | void (*get_tensor_async) (ggml_backend_t backend, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size); |
| 138 | void (*set_tensor_2d_async)(ggml_backend_t backend, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size, size_t n_copies, size_t stride_tensor, size_t stride_data); |
| 139 | void (*get_tensor_2d_async)(ggml_backend_t backend, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size, size_t n_copies, size_t stride_tensor, size_t stride_data); |
| 140 | bool (*cpy_tensor_async)(ggml_backend_t backend_src, ggml_backend_t backend_dst, const struct ggml_tensor * src, struct ggml_tensor * dst); |
| 141 | |
| 142 | // (optional) complete all pending operations (required if the backend supports async operations) |
| 143 | void (*synchronize)(ggml_backend_t backend); |
| 144 | |
| 145 | // (optional) graph plans (not used currently) |
| 146 | // compute graph with a plan |
| 147 | ggml_backend_graph_plan_t (*graph_plan_create) (ggml_backend_t backend, const struct ggml_cgraph * cgraph); |
| 148 | void (*graph_plan_free) (ggml_backend_t backend, ggml_backend_graph_plan_t plan); |
| 149 | // update the plan with a new graph - this should be faster than creating a new plan when the graph has the same topology |
| 150 | void (*graph_plan_update) (ggml_backend_t backend, ggml_backend_graph_plan_t plan, const struct ggml_cgraph * cgraph); |
| 151 | // compute the graph with the plan |
| 152 | enum ggml_status (*graph_plan_compute)(ggml_backend_t backend, ggml_backend_graph_plan_t plan); |
| 153 | |
| 154 | // compute graph (always async if supported by the backend) |
| 155 | enum ggml_status (*graph_compute) (ggml_backend_t backend, struct ggml_cgraph * cgraph); |
| 156 | |
| 157 | // (optional) event synchronization |
| 158 | // record an event on this stream |
| 159 | void (*event_record)(ggml_backend_t backend, ggml_backend_event_t event); |
| 160 | // wait for an event on on a different stream |
| 161 | void (*event_wait) (ggml_backend_t backend, ggml_backend_event_t event); |
| 162 | |
| 163 | // (optional) sort/optimize the nodes in the graph |
| 164 | void (*graph_optimize) (ggml_backend_t backend, struct ggml_cgraph * cgraph, struct ggml_backend_graph_optimize_params * params); |
| 165 | }; |
| 166 | |
| 167 | struct ggml_backend { |
| 168 | ggml_guid_t guid; |
| 169 | struct ggml_backend_i iface; |
| 170 | ggml_backend_dev_t device; |
| 171 | void * context; |
| 172 | }; |
| 173 | |
| 174 | struct ggml_backend_event { |
| 175 | struct ggml_backend_device * device; |
| 176 | void * context; |
| 177 | }; |
| 178 | |
| 179 | // |
| 180 | // Backend device |
| 181 | // |
| 182 | |
| 183 | // Note: if additional properties are needed, we should add a struct with all of them |
| 184 | // the current functions to obtain the properties can remain, since they are more convenient for often used properties |
| 185 | struct ggml_backend_device_i { |
| 186 | // device name: short identifier for this device, such as "CPU" or "CUDA0" |
| 187 | const char * (*get_name)(ggml_backend_dev_t dev); |
| 188 | |
| 189 | // device description: short informative description of the device, could be the model name |
| 190 | const char * (*get_description)(ggml_backend_dev_t dev); |
| 191 | |
| 192 | // device memory in bytes: 0 bytes to indicate no memory to report |
| 193 | void (*get_memory)(ggml_backend_dev_t dev, size_t * free, size_t * total); |
| 194 | |
| 195 | // device type |
| 196 | enum ggml_backend_dev_type (*get_type)(ggml_backend_dev_t dev); |
| 197 | |
| 198 | // device properties |
| 199 | void (*get_props)(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props); |
| 200 | |
| 201 | // backend (stream) initialization |
| 202 | ggml_backend_t (*init_backend)(ggml_backend_dev_t dev, const char * params); |
| 203 | |
| 204 | // preferred buffer type |
| 205 | ggml_backend_buffer_type_t (*get_buffer_type)(ggml_backend_dev_t dev); |
| 206 | |
| 207 | // (optional) host buffer type (in system memory, typically this is a pinned memory buffer for faster transfers between host and device) |
| 208 | ggml_backend_buffer_type_t (*get_host_buffer_type)(ggml_backend_dev_t dev); |
| 209 | |
| 210 | // (optional) buffer from pointer: create a buffer from a host pointer (useful for memory mapped models and importing data from other libraries) |
| 211 | ggml_backend_buffer_t (*buffer_from_host_ptr)(ggml_backend_dev_t dev, void * ptr, size_t size, size_t max_tensor_size); |
| 212 | |
| 213 | // check if the backend can compute an operation |
| 214 | bool (*supports_op)(ggml_backend_dev_t dev, const struct ggml_tensor * op); |
| 215 | |
| 216 | // check if the backend can use tensors allocated in a buffer type |
| 217 | bool (*supports_buft)(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft); |
| 218 | |
| 219 | // (optional) check if the backend wants to run an operation, even if the weights are allocated in an incompatible buffer |
| 220 | // these should be expensive operations that may benefit from running on this backend instead of the CPU backend |
| 221 | bool (*offload_op)(ggml_backend_dev_t dev, const struct ggml_tensor * op); |
| 222 | |
| 223 | // (optional) event synchronization |
| 224 | ggml_backend_event_t (*event_new) (ggml_backend_dev_t dev); |
| 225 | void (*event_free) (ggml_backend_dev_t dev, ggml_backend_event_t event); |
| 226 | void (*event_synchronize) (ggml_backend_dev_t dev, ggml_backend_event_t event); |
| 227 | }; |
| 228 | |
| 229 | struct ggml_backend_device { |
| 230 | struct ggml_backend_device_i iface; |
| 231 | ggml_backend_reg_t reg; |
| 232 | void * context; |
| 233 | }; |
| 234 | |
| 235 | // |
| 236 | // Backend (reg) |
| 237 | // |
| 238 | |
| 239 | struct ggml_backend_reg_i { |
| 240 | const char * (*get_name)(ggml_backend_reg_t reg); |
| 241 | |
| 242 | // enumerate available devices |
| 243 | size_t (*get_device_count)(ggml_backend_reg_t reg); |
| 244 | ggml_backend_dev_t (*get_device)(ggml_backend_reg_t reg, size_t index); |
| 245 | |
| 246 | // (optional) get a pointer to a function in the backend |
| 247 | // backends can add custom functions that are not part of the standard ggml-backend interface |
| 248 | void * (*get_proc_address)(ggml_backend_reg_t reg, const char * name); |
| 249 | }; |
| 250 | |
| 251 | struct ggml_backend_reg { |
| 252 | int api_version; // initialize to GGML_BACKEND_API_VERSION |
| 253 | struct ggml_backend_reg_i iface; |
| 254 | void * context; |
| 255 | }; |
| 256 | |
| 257 | // Add backend dynamic loading support to the backend |
| 258 | |
| 259 | // Initialize the backend |
| 260 | typedef ggml_backend_reg_t (*ggml_backend_init_t)(void); |
| 261 | // Optional: obtain a score for the backend based on the system configuration |
| 262 | // Higher scores are preferred, 0 means the backend is not supported in the current system |
| 263 | typedef int (*ggml_backend_score_t)(void); |
| 264 | |
| 265 | #ifdef GGML_BACKEND_DL |
| 266 | # ifdef __cplusplus |
| 267 | # define GGML_BACKEND_DL_IMPL(reg_fn) \ |
| 268 | extern "C" { \ |
| 269 | GGML_BACKEND_API ggml_backend_reg_t ggml_backend_init(void); \ |
| 270 | } \ |
| 271 | ggml_backend_reg_t ggml_backend_init(void) { \ |
| 272 | return reg_fn(); \ |
| 273 | } |
| 274 | # define GGML_BACKEND_DL_SCORE_IMPL(score_fn) \ |
| 275 | extern "C" { \ |
| 276 | GGML_BACKEND_API int ggml_backend_score(void); \ |
| 277 | } \ |
| 278 | int ggml_backend_score(void) { \ |
| 279 | return score_fn(); \ |
| 280 | } |
| 281 | # else |
| 282 | # define GGML_BACKEND_DL_IMPL(reg_fn) \ |
| 283 | GGML_BACKEND_API ggml_backend_reg_t ggml_backend_init(void); \ |
| 284 | ggml_backend_reg_t ggml_backend_init(void) { \ |
| 285 | return reg_fn(); \ |
| 286 | } |
| 287 | # define GGML_BACKEND_DL_SCORE_IMPL(score_fn) \ |
| 288 | GGML_BACKEND_API int ggml_backend_score(void); \ |
| 289 | int ggml_backend_score(void) { \ |
| 290 | return score_fn(); \ |
| 291 | } |
| 292 | # endif |
| 293 | #else |
| 294 | # define GGML_BACKEND_DL_IMPL(reg_fn) |
| 295 | # define GGML_BACKEND_DL_SCORE_IMPL(score_fn) |
| 296 | #endif |
| 297 | |
| 298 | #ifdef __cplusplus |
| 299 | } |
| 300 | #endif |