ggml-alloc.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * llama.cpp - commit 8962422b1c6f9b8b15f5aeaea42600bcc2d44177 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #pragma once
  27. #include "ggml.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. typedef struct ggml_backend_buffer_type * ggml_backend_buffer_type_t;
  32. typedef struct ggml_backend_buffer * ggml_backend_buffer_t;
  33. typedef struct ggml_backend * ggml_backend_t;
  34. // Tensor allocator
  35. struct ggml_tallocr {
  36. ggml_backend_buffer_t buffer;
  37. void * base;
  38. size_t alignment;
  39. size_t offset;
  40. };
  41. GGML_API struct ggml_tallocr ggml_tallocr_new(ggml_backend_buffer_t buffer);
  42. GGML_API void ggml_tallocr_alloc(struct ggml_tallocr * talloc, struct ggml_tensor * tensor);
  43. // Graph allocator
  44. /*
  45. Example usage:
  46. ggml_gallocr_t galloc = ggml_gallocr_new(ggml_bacckend_cpu_buffer_type());
  47. // optional: create a worst-case graph and reserve the buffers to avoid reallocations
  48. ggml_gallocr_reserve(galloc, build_graph(max_batch));
  49. // allocate the graph
  50. struct ggml_cgraph * graph = build_graph(batch);
  51. ggml_gallocr_alloc_graph(galloc, graph);
  52. printf("compute buffer size: %zu bytes\n", ggml_gallocr_get_buffer_size(galloc, 0));
  53. // evaluate the graph
  54. ggml_backend_graph_compute(backend, graph);
  55. */
  56. // special tensor flags for use with the graph allocator:
  57. // ggml_set_input(): all input tensors are allocated at the beginning of the graph in non-overlapping addresses
  58. // ggml_set_output(): output tensors are never freed and never overwritten
  59. typedef struct ggml_gallocr * ggml_gallocr_t;
  60. GGML_API ggml_gallocr_t ggml_gallocr_new(ggml_backend_buffer_type_t buft);
  61. GGML_API ggml_gallocr_t ggml_gallocr_new_n(ggml_backend_buffer_type_t * bufts, int n_bufs);
  62. GGML_API void ggml_gallocr_free(ggml_gallocr_t galloc);
  63. // pre-allocate buffers from a measure graph - does not allocate or modify the graph
  64. // call with a worst-case graph to avoid buffer reallocations
  65. // not strictly required for single buffer usage: ggml_gallocr_alloc_graph will reallocate the buffers automatically if needed
  66. // returns false if the buffer allocation failed
  67. GGML_API bool ggml_gallocr_reserve(ggml_gallocr_t galloc, struct ggml_cgraph * graph);
  68. GGML_API bool ggml_gallocr_reserve_n(
  69. ggml_gallocr_t galloc,
  70. struct ggml_cgraph * graph,
  71. const int * node_buffer_ids,
  72. const int * leaf_buffer_ids);
  73. // automatic reallocation if the topology changes when using a single buffer
  74. // returns false if using multiple buffers and a re-allocation is needed (call ggml_gallocr_reserve_n first to set the node buffers)
  75. GGML_API bool ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, struct ggml_cgraph * graph);
  76. GGML_API size_t ggml_gallocr_get_buffer_size(ggml_gallocr_t galloc, int buffer_id);
  77. // Utils
  78. // Create a buffer and allocate all the tensors in a ggml_context
  79. GGML_API struct ggml_backend_buffer * ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_context * ctx, ggml_backend_buffer_type_t buft);
  80. GGML_API struct ggml_backend_buffer * ggml_backend_alloc_ctx_tensors(struct ggml_context * ctx, ggml_backend_t backend);
  81. #ifdef __cplusplus
  82. }
  83. #endif