ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / include / ggml-backend.h
| 1 | #pragma once |
| 2 | |
| 3 | #include "ggml.h" |
| 4 | #include "ggml-alloc.h" |
| 5 | |
| 6 | #ifdef GGML_BACKEND_SHARED |
| 7 | # if defined(_WIN32) && !defined(__MINGW32__) |
| 8 | # ifdef GGML_BACKEND_BUILD |
| 9 | # define GGML_BACKEND_API __declspec(dllexport) extern |
| 10 | # else |
| 11 | # define GGML_BACKEND_API __declspec(dllimport) extern |
| 12 | # endif |
| 13 | # else |
| 14 | # define GGML_BACKEND_API __attribute__ ((visibility ("default"))) extern |
| 15 | # endif |
| 16 | #else |
| 17 | # define GGML_BACKEND_API extern |
| 18 | #endif |
| 19 | |
| 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | typedef struct ggml_backend_buffer_type * ggml_backend_buffer_type_t; |
| 25 | typedef struct ggml_backend_buffer * ggml_backend_buffer_t; |
| 26 | typedef struct ggml_backend_event * ggml_backend_event_t; |
| 27 | typedef struct ggml_backend * ggml_backend_t; |
| 28 | typedef void * ggml_backend_graph_plan_t; |
| 29 | typedef struct ggml_backend_reg * ggml_backend_reg_t; |
| 30 | typedef struct ggml_backend_device * ggml_backend_dev_t; |
| 31 | |
| 32 | |
| 33 | // |
| 34 | // Backend buffer type |
| 35 | // |
| 36 | |
| 37 | GGML_API const char * ggml_backend_buft_name (ggml_backend_buffer_type_t buft); |
| 38 | GGML_API ggml_backend_buffer_t ggml_backend_buft_alloc_buffer (ggml_backend_buffer_type_t buft, size_t size); |
| 39 | GGML_API ggml_backend_buffer_t ggml_backend_buft_alloc_buffer_n (ggml_backend_buffer_type_t buft, struct ggml_tensor ** tensors, int n_tensors); |
| 40 | GGML_API size_t ggml_backend_buft_get_alignment (ggml_backend_buffer_type_t buft); |
| 41 | GGML_API size_t ggml_backend_buft_get_max_size (ggml_backend_buffer_type_t buft); |
| 42 | GGML_API size_t ggml_backend_buft_get_alloc_size (ggml_backend_buffer_type_t buft, const struct ggml_tensor * tensor); |
| 43 | GGML_API size_t ggml_backend_buft_get_alloc_size_n(ggml_backend_buffer_type_t buft, struct ggml_tensor ** tensors, int n_tensors); |
| 44 | GGML_API bool ggml_backend_buft_is_host (ggml_backend_buffer_type_t buft); |
| 45 | GGML_API ggml_backend_dev_t ggml_backend_buft_get_device (ggml_backend_buffer_type_t buft); |
| 46 | |
| 47 | // |
| 48 | // Backend buffer |
| 49 | // |
| 50 | |
| 51 | enum ggml_backend_buffer_usage { |
| 52 | GGML_BACKEND_BUFFER_USAGE_ANY = 0, |
| 53 | GGML_BACKEND_BUFFER_USAGE_WEIGHTS = 1, |
| 54 | GGML_BACKEND_BUFFER_USAGE_COMPUTE = 2, |
| 55 | }; |
| 56 | |
| 57 | GGML_API const char * ggml_backend_buffer_name (ggml_backend_buffer_t buffer); |
| 58 | GGML_API void ggml_backend_buffer_free (ggml_backend_buffer_t buffer); |
| 59 | GGML_API void * ggml_backend_buffer_get_base (ggml_backend_buffer_t buffer); |
| 60 | GGML_API size_t ggml_backend_buffer_get_size (ggml_backend_buffer_t buffer); |
| 61 | GGML_API enum ggml_status ggml_backend_buffer_init_tensor (ggml_backend_buffer_t buffer, struct ggml_tensor * tensor); |
| 62 | GGML_API size_t ggml_backend_buffer_get_alignment (ggml_backend_buffer_t buffer); |
| 63 | GGML_API size_t ggml_backend_buffer_get_max_size (ggml_backend_buffer_t buffer); |
| 64 | GGML_API size_t ggml_backend_buffer_get_alloc_size(ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor); |
| 65 | GGML_API void ggml_backend_buffer_clear (ggml_backend_buffer_t buffer, uint8_t value); |
| 66 | GGML_API bool ggml_backend_buffer_is_host (ggml_backend_buffer_t buffer); |
| 67 | GGML_API void ggml_backend_buffer_set_usage (ggml_backend_buffer_t buffer, enum ggml_backend_buffer_usage usage); |
| 68 | GGML_API enum ggml_backend_buffer_usage ggml_backend_buffer_get_usage (ggml_backend_buffer_t buffer); |
| 69 | GGML_API ggml_backend_buffer_type_t ggml_backend_buffer_get_type (ggml_backend_buffer_t buffer); |
| 70 | GGML_API void ggml_backend_buffer_reset (ggml_backend_buffer_t buffer); |
| 71 | |
| 72 | // tensor copy between different backends |
| 73 | GGML_API void ggml_backend_tensor_copy (const struct ggml_tensor * src, struct ggml_tensor * dst); |
| 74 | GGML_API void ggml_backend_tensor_copy_n(const struct ggml_tensor * const * srcs, struct ggml_tensor * const * dsts, size_t n_tensors); |
| 75 | |
| 76 | // |
| 77 | // Backend (stream) |
| 78 | // |
| 79 | |
| 80 | GGML_API ggml_guid_t ggml_backend_guid(ggml_backend_t backend); |
| 81 | GGML_API const char * ggml_backend_name(ggml_backend_t backend); |
| 82 | GGML_API void ggml_backend_free(ggml_backend_t backend); |
| 83 | |
| 84 | GGML_API ggml_backend_buffer_type_t ggml_backend_get_default_buffer_type(ggml_backend_t backend); |
| 85 | GGML_API ggml_backend_buffer_t ggml_backend_alloc_buffer(ggml_backend_t backend, size_t size); |
| 86 | GGML_API size_t ggml_backend_get_alignment(ggml_backend_t backend); |
| 87 | GGML_API size_t ggml_backend_get_max_size(ggml_backend_t backend); |
| 88 | |
| 89 | GGML_API void ggml_backend_tensor_set_async (ggml_backend_t backend, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size); |
| 90 | GGML_API void ggml_backend_tensor_get_async (ggml_backend_t backend, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size); |
| 91 | GGML_API void ggml_backend_tensor_set_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); |
| 92 | GGML_API void ggml_backend_tensor_get_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); |
| 93 | |
| 94 | // "offset" refers to the offset in tensor->data for setting/getting data |
| 95 | GGML_API void ggml_backend_tensor_set ( struct ggml_tensor * tensor, const void * data, size_t offset, size_t size); |
| 96 | GGML_API void ggml_backend_tensor_get (const struct ggml_tensor * tensor, void * data, size_t offset, size_t size); |
| 97 | GGML_API void ggml_backend_tensor_set_2d( 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); |
| 98 | GGML_API void ggml_backend_tensor_get_2d(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); |
| 99 | GGML_API void ggml_backend_tensor_set_n ( struct ggml_tensor * const * tensors, const void * data, const size_t * offsets, const size_t * sizes, const size_t * data_offsets, size_t n_tensors); |
| 100 | GGML_API void ggml_backend_tensor_get_n (const struct ggml_tensor * const * tensors, void * data, const size_t * offsets, const size_t * sizes, const size_t * data_offsets, size_t n_tensors); |
| 101 | GGML_API void ggml_backend_tensor_memset( struct ggml_tensor * tensor, uint8_t value, size_t offset, size_t size); |
| 102 | |
| 103 | GGML_API void ggml_backend_synchronize(ggml_backend_t backend); |
| 104 | |
| 105 | GGML_API ggml_backend_graph_plan_t ggml_backend_graph_plan_create(ggml_backend_t backend, struct ggml_cgraph * cgraph); |
| 106 | GGML_API void ggml_backend_graph_plan_free (ggml_backend_t backend, ggml_backend_graph_plan_t plan); |
| 107 | |
| 108 | GGML_API enum ggml_status ggml_backend_graph_plan_compute (ggml_backend_t backend, ggml_backend_graph_plan_t plan); |
| 109 | GGML_API enum ggml_status ggml_backend_graph_compute (ggml_backend_t backend, struct ggml_cgraph * cgraph); |
| 110 | GGML_API enum ggml_status ggml_backend_graph_compute_async(ggml_backend_t backend, struct ggml_cgraph * cgraph); |
| 111 | |
| 112 | // NOTE: will be removed, use device version instead |
| 113 | GGML_API bool ggml_backend_supports_op(ggml_backend_t backend, const struct ggml_tensor * op); |
| 114 | GGML_API bool ggml_backend_supports_buft(ggml_backend_t backend, ggml_backend_buffer_type_t buft); |
| 115 | GGML_API bool ggml_backend_offload_op(ggml_backend_t backend, const struct ggml_tensor * op); |
| 116 | |
| 117 | // asynchronous copy |
| 118 | // the copy is performed after all the currently queued operations in backend_src |
| 119 | // backend_dst will wait for the copy to complete before performing other operations |
| 120 | // automatic fallback to sync copy if async is not supported |
| 121 | GGML_API void ggml_backend_tensor_copy_async(ggml_backend_t backend_src, ggml_backend_t backend_dst, const struct ggml_tensor * src, struct ggml_tensor * dst); |
| 122 | |
| 123 | GGML_API ggml_backend_dev_t ggml_backend_get_device(ggml_backend_t backend); |
| 124 | |
| 125 | // |
| 126 | // Events |
| 127 | // |
| 128 | |
| 129 | GGML_API ggml_backend_event_t ggml_backend_event_new(ggml_backend_dev_t device); |
| 130 | GGML_API void ggml_backend_event_free(ggml_backend_event_t event); |
| 131 | GGML_API void ggml_backend_event_record(ggml_backend_event_t event, ggml_backend_t backend); |
| 132 | GGML_API void ggml_backend_event_synchronize(ggml_backend_event_t event); |
| 133 | GGML_API void ggml_backend_event_wait(ggml_backend_t backend, ggml_backend_event_t event); |
| 134 | |
| 135 | // |
| 136 | // Backend device |
| 137 | // |
| 138 | |
| 139 | enum ggml_backend_dev_type { |
| 140 | // CPU device using system memory |
| 141 | GGML_BACKEND_DEVICE_TYPE_CPU, |
| 142 | // GPU device using dedicated memory |
| 143 | GGML_BACKEND_DEVICE_TYPE_GPU, |
| 144 | // integrated GPU device using host memory |
| 145 | GGML_BACKEND_DEVICE_TYPE_IGPU, |
| 146 | // accelerator devices intended to be used together with the CPU backend (e.g. BLAS or AMX) |
| 147 | GGML_BACKEND_DEVICE_TYPE_ACCEL, |
| 148 | // "meta" device wrapping multiple other devices for tensor parallelism |
| 149 | GGML_BACKEND_DEVICE_TYPE_META, |
| 150 | }; |
| 151 | |
| 152 | // functionality supported by the device |
| 153 | struct ggml_backend_dev_caps { |
| 154 | // asynchronous operations |
| 155 | bool async; |
| 156 | // pinned host buffer |
| 157 | bool host_buffer; |
| 158 | // creating buffers from host ptr |
| 159 | bool buffer_from_host_ptr; |
| 160 | // event synchronization |
| 161 | bool events; |
| 162 | // mmap is supported for loading |
| 163 | bool mmap_support; |
| 164 | }; |
| 165 | |
| 166 | // all the device properties |
| 167 | struct ggml_backend_dev_props { |
| 168 | // device name |
| 169 | const char * name; |
| 170 | // device description |
| 171 | const char * description; |
| 172 | // device free memory in bytes |
| 173 | size_t memory_free; |
| 174 | // device total memory in bytes |
| 175 | size_t memory_total; |
| 176 | // device type |
| 177 | enum ggml_backend_dev_type type; |
| 178 | // device id |
| 179 | // for PCI devices, this should be the lower-case PCI bus id formatted as "domain:bus:device.function" (e.g. "0000:c1:00.0") |
| 180 | // if the id is unknown, this should be NULL |
| 181 | const char * device_id; |
| 182 | // device capabilities |
| 183 | struct ggml_backend_dev_caps caps; |
| 184 | }; |
| 185 | |
| 186 | GGML_API const char * ggml_backend_dev_name(ggml_backend_dev_t device); |
| 187 | GGML_API const char * ggml_backend_dev_description(ggml_backend_dev_t device); |
| 188 | GGML_API void ggml_backend_dev_memory(ggml_backend_dev_t device, size_t * free, size_t * total); |
| 189 | GGML_API enum ggml_backend_dev_type ggml_backend_dev_type(ggml_backend_dev_t device); |
| 190 | GGML_API void ggml_backend_dev_get_props(ggml_backend_dev_t device, struct ggml_backend_dev_props * props); |
| 191 | GGML_API ggml_backend_reg_t ggml_backend_dev_backend_reg(ggml_backend_dev_t device); |
| 192 | GGML_API ggml_backend_t ggml_backend_dev_init(ggml_backend_dev_t device, const char * params); |
| 193 | GGML_API ggml_backend_buffer_type_t ggml_backend_dev_buffer_type(ggml_backend_dev_t device); |
| 194 | GGML_API ggml_backend_buffer_type_t ggml_backend_dev_host_buffer_type(ggml_backend_dev_t device); |
| 195 | GGML_API ggml_backend_buffer_t ggml_backend_dev_buffer_from_host_ptr(ggml_backend_dev_t device, void * ptr, size_t size, size_t max_tensor_size); |
| 196 | |
| 197 | GGML_API bool ggml_backend_dev_supports_op(ggml_backend_dev_t device, const struct ggml_tensor * op); |
| 198 | GGML_API bool ggml_backend_dev_supports_buft(ggml_backend_dev_t device, ggml_backend_buffer_type_t buft); |
| 199 | GGML_API bool ggml_backend_dev_offload_op(ggml_backend_dev_t device, const struct ggml_tensor * op); |
| 200 | |
| 201 | // |
| 202 | // Backend (reg) |
| 203 | // |
| 204 | |
| 205 | GGML_API const char * ggml_backend_reg_name(ggml_backend_reg_t reg); |
| 206 | GGML_API size_t ggml_backend_reg_dev_count(ggml_backend_reg_t reg); |
| 207 | GGML_API ggml_backend_dev_t ggml_backend_reg_dev_get(ggml_backend_reg_t reg, size_t index); |
| 208 | GGML_API void * ggml_backend_reg_get_proc_address(ggml_backend_reg_t reg, const char * name); |
| 209 | |
| 210 | // Common functions that may be obtained using ggml_backend_reg_get_proc_address |
| 211 | |
| 212 | // Context management and operations for faster communication between backends, used for tensor parallelism (meta backend) |
| 213 | typedef void * (*ggml_backend_comm_init_t)(ggml_backend_t * backends, size_t n_backends); |
| 214 | |
| 215 | // Builds the communicator for the case where each rank is a separate process, so a process |
| 216 | // owns one backend of the communicator rather than all of them. |
| 217 | // |
| 218 | // The caller provides bootstrap_callback, which moves bytes between the ranks before the |
| 219 | // communicator exists. Each rank passes size bytes in send_buf, and recv_buf receives |
| 220 | // world_size * size bytes in rank order. The backend decides what to exchange and how many |
| 221 | // times to call it, and every rank must make the same calls in the same order. |
| 222 | // comm_init_rank blocks until all world_size ranks have joined. |
| 223 | typedef bool (*ggml_backend_comm_bootstrap_callback)(void * user_data, const void * send_buf, void * recv_buf, size_t size); |
| 224 | typedef void * (*ggml_backend_comm_init_rank_t)(ggml_backend_t backend, int rank, int world_size, ggml_backend_comm_bootstrap_callback bootstrap_callback, void * bootstrap_callback_data); |
| 225 | |
| 226 | typedef void (*ggml_backend_comm_free_t)(void * comm_ctx); |
| 227 | typedef bool (*ggml_backend_comm_allreduce_tensor_t)(void * comm_ctx, struct ggml_tensor ** tensors); |
| 228 | |
| 229 | // Split buffer type for tensor parallelism (old) |
| 230 | typedef ggml_backend_buffer_type_t (*ggml_backend_split_buffer_type_t)(int main_device, const float * tensor_split); |
| 231 | // Set the number of threads for the backend |
| 232 | typedef void (*ggml_backend_set_n_threads_t)(ggml_backend_t backend, int n_threads); |
| 233 | // Get additional buffer types provided by the device (returns a NULL-terminated array) |
| 234 | typedef ggml_backend_buffer_type_t * (*ggml_backend_dev_get_extra_bufts_t)(ggml_backend_dev_t device); |
| 235 | // Set the abort callback for the backend |
| 236 | typedef void (*ggml_backend_set_abort_callback_t)(ggml_backend_t backend, ggml_abort_callback abort_callback, void * abort_callback_data); |
| 237 | // Get a list of feature flags supported by the backend (returns a NULL-terminated array) |
| 238 | struct ggml_backend_feature { |
| 239 | const char * name; |
| 240 | const char * value; |
| 241 | }; |
| 242 | typedef struct ggml_backend_feature * (*ggml_backend_get_features_t)(ggml_backend_reg_t reg); |
| 243 | |
| 244 | // |
| 245 | // Backend registry |
| 246 | // |
| 247 | |
| 248 | GGML_API void ggml_backend_register(ggml_backend_reg_t reg); |
| 249 | |
| 250 | GGML_API void ggml_backend_device_register(ggml_backend_dev_t device); |
| 251 | |
| 252 | // Backend (reg) enumeration |
| 253 | GGML_API size_t ggml_backend_reg_count(void); |
| 254 | GGML_API ggml_backend_reg_t ggml_backend_reg_get(size_t index); |
| 255 | GGML_API ggml_backend_reg_t ggml_backend_reg_by_name(const char * name); |
| 256 | |
| 257 | // Device enumeration |
| 258 | GGML_API size_t ggml_backend_dev_count(void); |
| 259 | GGML_API ggml_backend_dev_t ggml_backend_dev_get(size_t index); |
| 260 | GGML_API ggml_backend_dev_t ggml_backend_dev_by_name(const char * name); |
| 261 | GGML_API ggml_backend_dev_t ggml_backend_dev_by_type(enum ggml_backend_dev_type type); |
| 262 | |
| 263 | // Direct backend (stream) initialization |
| 264 | // = ggml_backend_dev_init(ggml_backend_dev_by_name(name), params) |
| 265 | GGML_API ggml_backend_t ggml_backend_init_by_name(const char * name, const char * params); |
| 266 | // = ggml_backend_dev_init(ggml_backend_dev_by_type(type), params) |
| 267 | GGML_API ggml_backend_t ggml_backend_init_by_type(enum ggml_backend_dev_type type, const char * params); |
| 268 | // = ggml_backend_dev_init(ggml_backend_dev_by_type(GPU) OR ggml_backend_dev_by_type(CPU), NULL) |
| 269 | GGML_API ggml_backend_t ggml_backend_init_best(void); |
| 270 | |
| 271 | // Load a backend from a dynamic library and register it |
| 272 | GGML_API ggml_backend_reg_t ggml_backend_load(const char * path); |
| 273 | // Unload a backend if loaded dynamically and unregister it |
| 274 | GGML_API void ggml_backend_unload(ggml_backend_reg_t reg); |
| 275 | // Load all known backends from dynamic libraries |
| 276 | GGML_API void ggml_backend_load_all(void); |
| 277 | GGML_API void ggml_backend_load_all_from_path(const char * dir_path); |
| 278 | |
| 279 | // |
| 280 | // Backend scheduler |
| 281 | // |
| 282 | |
| 283 | // The backend scheduler allows for multiple backend devices to be used together |
| 284 | // Handles compute buffer allocation, assignment of tensors to backends, and copying of tensors between backends |
| 285 | // The backends are selected based on: |
| 286 | // - the backend that supports the operation |
| 287 | // - the location of the pre-allocated tensors (e.g. the weights) |
| 288 | /* |
| 289 | Example usage: |
| 290 | |
| 291 | // operations that use tensors allocated in a buffer with USAGE_WEIGHTS will be assigned |
| 292 | // preferably to run on the same backend as the buffer |
| 293 | ggml_backend_buffer_set_usage(buf_weights, GGML_BACKEND_BUFFER_USAGE_WEIGHTS); |
| 294 | |
| 295 | sched = ggml_backend_sched_new({backend_gpu, backend_gpu2, backend_cpu}, NULL, num_backends, GGML_DEFAULT_GRAPH_SIZE, false, true); |
| 296 | |
| 297 | // initialize buffers from a max size graph (optional) |
| 298 | reserve_graph = build_graph(sched, max_batch_size); |
| 299 | |
| 300 | // manually assign nodes to a backend (optional, should not be needed in most cases) |
| 301 | struct ggml_tensor * node = ggml_mul_mat(ctx, ...); |
| 302 | ggml_backend_sched_set_tensor_backend(sched, node, backend_gpu); |
| 303 | |
| 304 | ggml_backend_sched_reserve(sched, reserve_graph); |
| 305 | |
| 306 | // compute |
| 307 | graph = build_graph(sched); // the graph and its tensors are single-use in terms of allocation, multi-use in terms of computation |
| 308 | for (int i = 0; i < 10; ++i) { |
| 309 | ggml_backend_sched_graph_compute(sched, graph); // on the first iteration the graph is allocated automatically |
| 310 | } |
| 311 | |
| 312 | // if there are graph inputs: |
| 313 | graph = build_graph(sched); // get a new graph that is not allocated (the metadata for the old graph is freed once ggml_free is called) |
| 314 | ggml_backend_sched_reset(sched); // clear the allocation of the previous graph |
| 315 | ggml_backend_sched_alloc_graph(sched, graph); // explicitly allocate the new graph but do not execute it |
| 316 | ggml_backend_tensor_set(input_tensor, ...); // copy data to the newly allocated graph tensors |
| 317 | ggml_backend_sched_graph_compute(sched, graph); // execute the graph |
| 318 | |
| 319 | // as an alternative to the above it is also possible to assign the inputs to a dedicated context and |
| 320 | // allocate them statically via ggml_backend_alloc_ctx_tensors |
| 321 | } |
| 322 | */ |
| 323 | |
| 324 | typedef struct ggml_backend_sched * ggml_backend_sched_t; |
| 325 | |
| 326 | // Evaluation callback for each node in the graph (set with ggml_backend_sched_set_eval_callback) |
| 327 | // when ask == true, the scheduler wants to know if the user wants to observe this node |
| 328 | // this allows the scheduler to batch nodes together in order to evaluate them in a single call |
| 329 | // |
| 330 | // when ask == false, the scheduler is passing the node tensor to the user for observation |
| 331 | // if the user returns false, the scheduler will cancel the graph compute |
| 332 | // |
| 333 | typedef bool (*ggml_backend_sched_eval_callback)(struct ggml_tensor * t, bool ask, void * user_data); |
| 334 | |
| 335 | // Callback while copying input weights of a split |
| 336 | // if the user returns false the scheduler simply copies the entire weight |
| 337 | // the callback is called only for input weights in host buffers |
| 338 | // the callback is called after all non-weight inputs of the split have been copied |
| 339 | // `src` is the tensor in the previous split |
| 340 | // `dst` is the copy of `src` in the split |
| 341 | // `graph` is the compute graph of the split |
| 342 | typedef bool (*ggml_backend_sched_copy_callback)(ggml_backend_t backend, const struct ggml_tensor * src, struct ggml_tensor * dst, struct ggml_cgraph * graph, void * user_data); |
| 343 | |
| 344 | // Callback when the buffers reserved for a graph are not sufficient and have to be reserved again |
| 345 | // it happens on the graph allocation path and costs a synchronization of the backends |
| 346 | // `sched` is the scheduler |
| 347 | // `graph` is the compute graph being allocated |
| 348 | // `backend_ids_changed` is true if the backend assignment of the graph nodes changed since the last allocation |
| 349 | // `graph_size_changed` is true if the graph size changed since the last allocation |
| 350 | typedef void (*ggml_backend_sched_realloc_callback)(ggml_backend_sched_t sched, struct ggml_cgraph * graph, bool backend_ids_changed, bool graph_size_changed, void * user_data); |
| 351 | |
| 352 | // Initialize a backend scheduler, backends with low index are given priority over backends with high index |
| 353 | GGML_API ggml_backend_sched_t ggml_backend_sched_new(ggml_backend_t * backends, ggml_backend_buffer_type_t * bufts, int n_backends, size_t graph_size, bool parallel, bool op_offload); |
| 354 | GGML_API void ggml_backend_sched_free(ggml_backend_sched_t sched); |
| 355 | |
| 356 | // Initialize backend buffers from a measure graph |
| 357 | GGML_API void ggml_backend_sched_reserve_size(ggml_backend_sched_t sched, struct ggml_cgraph * measure_graph, size_t * sizes); |
| 358 | GGML_API bool ggml_backend_sched_reserve(ggml_backend_sched_t sched, struct ggml_cgraph * measure_graph); // returns success |
| 359 | |
| 360 | GGML_API int ggml_backend_sched_get_n_backends(ggml_backend_sched_t sched); |
| 361 | GGML_API ggml_backend_t ggml_backend_sched_get_backend(ggml_backend_sched_t sched, int i); |
| 362 | |
| 363 | // Get the number of splits of the last graph |
| 364 | GGML_API int ggml_backend_sched_get_n_splits(ggml_backend_sched_t sched); |
| 365 | GGML_API int ggml_backend_sched_get_n_copies(ggml_backend_sched_t sched); |
| 366 | |
| 367 | GGML_API ggml_backend_buffer_type_t ggml_backend_sched_get_buffer_type(ggml_backend_sched_t sched, ggml_backend_t backend); |
| 368 | GGML_API size_t ggml_backend_sched_get_buffer_size(ggml_backend_sched_t sched, ggml_backend_t backend); |
| 369 | |
| 370 | GGML_API void ggml_backend_sched_set_tensor_backend(ggml_backend_sched_t sched, struct ggml_tensor * node, ggml_backend_t backend); |
| 371 | GGML_API ggml_backend_t ggml_backend_sched_get_tensor_backend(ggml_backend_sched_t sched, struct ggml_tensor * node); |
| 372 | |
| 373 | // Split graph without allocating it |
| 374 | GGML_API void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgraph * graph); |
| 375 | |
| 376 | // Allocate and compute graph on the backend scheduler |
| 377 | GGML_API bool ggml_backend_sched_alloc_graph(ggml_backend_sched_t sched, struct ggml_cgraph * graph); // returns success |
| 378 | GGML_API enum ggml_status ggml_backend_sched_graph_compute(ggml_backend_sched_t sched, struct ggml_cgraph * graph); |
| 379 | GGML_API enum ggml_status ggml_backend_sched_graph_compute_async(ggml_backend_sched_t sched, struct ggml_cgraph * graph); |
| 380 | GGML_API void ggml_backend_sched_synchronize(ggml_backend_sched_t sched); |
| 381 | |
| 382 | // Reset all assignments and allocators - must be called before changing the node backends or allocating a new graph. |
| 383 | // This in effect deallocates all tensors that were previously allocated and leaves them with dangling pointers. |
| 384 | // The correct way to use this API is to discard the deallocated tensors and create new ones. |
| 385 | GGML_API void ggml_backend_sched_reset(ggml_backend_sched_t sched); |
| 386 | |
| 387 | // Set a callback to be called for each resulting node during graph compute |
| 388 | GGML_API void ggml_backend_sched_set_eval_callback(ggml_backend_sched_t sched, ggml_backend_sched_eval_callback callback, void * user_data); |
| 389 | |
| 390 | // Set a callback to be called when the inputs weights of a split are being copied |
| 391 | GGML_API void ggml_backend_sched_set_copy_callback(ggml_backend_sched_t sched, ggml_backend_sched_copy_callback callback, void * user_data); |
| 392 | |
| 393 | // Set a callback to be called when the compute buffers of a graph are being reallocated |
| 394 | GGML_API void ggml_backend_sched_set_realloc_callback(ggml_backend_sched_t sched, ggml_backend_sched_realloc_callback callback, void * user_data); |
| 395 | |
| 396 | // |
| 397 | // Meta backend |
| 398 | // |
| 399 | |
| 400 | #define GGML_BACKEND_META_MAX_DEVICES 16 |
| 401 | |
| 402 | enum ggml_backend_meta_split_axis { |
| 403 | // tensor split by tensor dimensions: |
| 404 | GGML_BACKEND_SPLIT_AXIS_0 = 0, |
| 405 | GGML_BACKEND_SPLIT_AXIS_1 = 1, |
| 406 | GGML_BACKEND_SPLIT_AXIS_2 = 2, |
| 407 | GGML_BACKEND_SPLIT_AXIS_3 = 3, |
| 408 | |
| 409 | GGML_BACKEND_SPLIT_AXIS_MIRRORED = 10, // all values on all backends |
| 410 | GGML_BACKEND_SPLIT_AXIS_PARTIAL = 11, // each backend has a partial sum |
| 411 | |
| 412 | // for internal bookkeeping only: |
| 413 | GGML_BACKEND_SPLIT_AXIS_NONE = 98, |
| 414 | GGML_BACKEND_SPLIT_AXIS_UNKNOWN = 99, |
| 415 | }; |
| 416 | GGML_API const char * ggml_backend_meta_split_axis_name(enum ggml_backend_meta_split_axis split_axis); |
| 417 | |
| 418 | struct ggml_backend_meta_split_state { |
| 419 | enum ggml_backend_meta_split_axis axis; |
| 420 | |
| 421 | // for tensors with axis >= 0 && axis < GGML_MAX_DIMS: |
| 422 | // - each device has a slice of the tensor along the split axis |
| 423 | // - most tensors have n_segments == 1 and a contiguous slice of the tensor data |
| 424 | // - some tensors have an inhomogenenous data layout along the split axis, |
| 425 | // those tensors are divided into segments which are each individually split across devices |
| 426 | // (this usually happens when multiple tensors are fused into a single one) |
| 427 | // - ne has one entry per segment and device and that segment repeats nr times, |
| 428 | // in total when accounting for repetitions the segments add up to ggml_tensor::ne for that axis, |
| 429 | // the outer/inner loops are over segments/devices like [seg0_dev0_r0, seg0_dev1_r0, seg0_dev0_r1, seg0_dev1_r1, seg1_dev0_r0, seg1_dev1_r0], |
| 430 | // - for example, a transformer may have a fused QKV matrix rather than 3 matrices, those would be 3 separate segments |
| 431 | // that each need to be split individually across devices so that each device gets a slice of Q, K, and V, |
| 432 | // the Q matrix can be larger than the K and V matrices so this can either be expressed as 3 segments or as 2 segments |
| 433 | // where the segment for K/V repeats twice |
| 434 | int64_t ne[16*GGML_BACKEND_META_MAX_DEVICES]; |
| 435 | uint32_t nr[16]; |
| 436 | uint32_t n_segments; |
| 437 | }; |
| 438 | |
| 439 | // function to assign split states for statically allocated tensors, compute tensor split states will be assigned to be compatible: |
| 440 | typedef struct ggml_backend_meta_split_state(*ggml_backend_meta_get_split_state_t)(const struct ggml_tensor * tensor, void * userdata); |
| 441 | |
| 442 | // create a new meta device from "simple" devices, meta buffer type/buffer/backend is then derived from this: |
| 443 | // TODO: this looks a bit strange - a backend API creates a device. I think we should try |
| 444 | // express this as a backend registry functionality instead |
| 445 | GGML_API ggml_backend_dev_t ggml_backend_meta_device( |
| 446 | ggml_backend_dev_t * devs, size_t n_devs, ggml_backend_meta_get_split_state_t get_split_state, void * get_split_state_ud); |
| 447 | |
| 448 | // |
| 449 | // Utils |
| 450 | // |
| 451 | |
| 452 | struct ggml_backend_graph_copy { |
| 453 | ggml_backend_buffer_t buffer; |
| 454 | struct ggml_context * ctx_allocated; |
| 455 | struct ggml_context * ctx_unallocated; |
| 456 | struct ggml_cgraph * graph; |
| 457 | }; |
| 458 | |
| 459 | // Copy a graph to a different backend |
| 460 | GGML_API struct ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t backend, struct ggml_cgraph * graph); |
| 461 | GGML_API void ggml_backend_graph_copy_free(struct ggml_backend_graph_copy copy); |
| 462 | |
| 463 | typedef bool (*ggml_backend_eval_callback)(int node_index, struct ggml_tensor * t1, struct ggml_tensor * t2, void * user_data); |
| 464 | |
| 465 | // Compare the output of two backends |
| 466 | GGML_API bool ggml_backend_compare_graph_backend(ggml_backend_t backend1, ggml_backend_t backend2, struct ggml_cgraph * graph, ggml_backend_eval_callback callback, void * user_data, struct ggml_tensor const * const * test_nodes, size_t num_test_nodes); |
| 467 | |
| 468 | // Tensor initialization |
| 469 | GGML_API enum ggml_status ggml_backend_tensor_alloc(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, void * addr); |
| 470 | GGML_API enum ggml_status ggml_backend_view_init(struct ggml_tensor * tensor); |
| 471 | |
| 472 | // CPU buffer types are always available |
| 473 | GGML_API ggml_backend_buffer_t ggml_backend_cpu_buffer_from_ptr(void * ptr, size_t size); |
| 474 | GGML_API ggml_backend_buffer_type_t ggml_backend_cpu_buffer_type(void); |
| 475 | |
| 476 | #ifdef __cplusplus |
| 477 | } |
| 478 | #endif |