char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Georgi Gerganovsync : ggml (#28379)64a155d24

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