ggml-alloc.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include "ggml.h"
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. typedef struct ggml_backend_buffer_type * ggml_backend_buffer_type_t;
  7. typedef struct ggml_backend_buffer * ggml_backend_buffer_t;
  8. typedef struct ggml_backend * ggml_backend_t;
  9. // Tensor allocator
  10. struct ggml_tallocr {
  11. ggml_backend_buffer_t buffer;
  12. void * base;
  13. size_t alignment;
  14. size_t offset;
  15. };
  16. GGML_API struct ggml_tallocr ggml_tallocr_new(ggml_backend_buffer_t buffer);
  17. GGML_API void ggml_tallocr_alloc(struct ggml_tallocr * talloc, struct ggml_tensor * tensor);
  18. // Graph allocator
  19. /*
  20. Example usage:
  21. ggml_gallocr_t galloc = ggml_gallocr_new(ggml_bacckend_cpu_buffer_type());
  22. // optional: create a worst-case graph and reserve the buffers to avoid reallocations
  23. ggml_gallocr_reserve(galloc, build_graph(max_batch));
  24. // allocate the graph
  25. struct ggml_cgraph * graph = build_graph(batch);
  26. ggml_gallocr_alloc_graph(galloc, graph);
  27. printf("compute buffer size: %zu bytes\n", ggml_gallocr_get_buffer_size(galloc, 0));
  28. // evaluate the graph
  29. ggml_backend_graph_compute(backend, graph);
  30. */
  31. // special tensor flags for use with the graph allocator:
  32. // ggml_set_input(): all input tensors are allocated at the beginning of the graph in non-overlapping addresses
  33. // ggml_set_output(): output tensors are never freed and never overwritten
  34. typedef struct ggml_gallocr * ggml_gallocr_t;
  35. GGML_API ggml_gallocr_t ggml_gallocr_new(ggml_backend_buffer_type_t buft);
  36. GGML_API ggml_gallocr_t ggml_gallocr_new_n(ggml_backend_buffer_type_t * bufts, int n_bufs);
  37. GGML_API void ggml_gallocr_free(ggml_gallocr_t galloc);
  38. // pre-allocate buffers from a measure graph - does not allocate or modify the graph
  39. // call with a worst-case graph to avoid buffer reallocations
  40. // not strictly required for single buffer usage: ggml_gallocr_alloc_graph will reallocate the buffers automatically if needed
  41. // returns false if the buffer allocation failed
  42. GGML_API bool ggml_gallocr_reserve(ggml_gallocr_t galloc, struct ggml_cgraph * graph);
  43. GGML_API bool ggml_gallocr_reserve_n(
  44. ggml_gallocr_t galloc,
  45. struct ggml_cgraph * graph,
  46. const int * node_buffer_ids,
  47. const int * leaf_buffer_ids);
  48. // automatic reallocation if the topology changes when using a single buffer
  49. // returns false if using multiple buffers and a re-allocation is needed (call ggml_gallocr_reserve_n first to set the node buffers)
  50. GGML_API bool ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, struct ggml_cgraph * graph);
  51. GGML_API size_t ggml_gallocr_get_buffer_size(ggml_gallocr_t galloc, int buffer_id);
  52. // Utils
  53. // Create a buffer and allocate all the tensors in a ggml_context
  54. GGML_API struct ggml_backend_buffer * ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_context * ctx, ggml_backend_buffer_type_t buft);
  55. GGML_API struct ggml_backend_buffer * ggml_backend_alloc_ctx_tensors(struct ggml_context * ctx, ggml_backend_t backend);
  56. #ifdef __cplusplus
  57. }
  58. #endif