ggml-backend-impl.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // ggml-backend internal header
  28. #include "ggml-backend.h"
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. //
  33. // Backend buffer
  34. //
  35. // buffer type
  36. typedef void * ggml_backend_buffer_type_context_t;
  37. struct ggml_backend_buffer_type_i {
  38. const char * (*GGML_CALL get_name) (ggml_backend_buffer_type_t buft);
  39. // allocate a buffer of this type
  40. ggml_backend_buffer_t (*GGML_CALL alloc_buffer) (ggml_backend_buffer_type_t buft, size_t size);
  41. // tensor alignment
  42. size_t (*GGML_CALL get_alignment) (ggml_backend_buffer_type_t buft);
  43. // max buffer size that can be allocated
  44. size_t (*GGML_CALL get_max_size) (ggml_backend_buffer_type_t buft);
  45. // data size needed to allocate the tensor, including padding
  46. size_t (*GGML_CALL get_alloc_size) (ggml_backend_buffer_type_t buft, const struct ggml_tensor * tensor);
  47. // check if tensor data is in host memory
  48. bool (*GGML_CALL is_host) (ggml_backend_buffer_type_t buft);
  49. };
  50. struct ggml_backend_buffer_type {
  51. struct ggml_backend_buffer_type_i iface;
  52. ggml_backend_buffer_type_context_t context;
  53. };
  54. // buffer
  55. typedef void * ggml_backend_buffer_context_t;
  56. struct ggml_backend_buffer_i {
  57. const char * (*GGML_CALL get_name) (ggml_backend_buffer_t buffer);
  58. void (*GGML_CALL free_buffer)(ggml_backend_buffer_t buffer);
  59. void * (*GGML_CALL get_base) (ggml_backend_buffer_t buffer);
  60. void (*GGML_CALL init_tensor)(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor);
  61. void (*GGML_CALL set_tensor) (ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
  62. void (*GGML_CALL get_tensor) (ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
  63. bool (*GGML_CALL cpy_tensor) (ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst); // dst is in the buffer, src may be in any buffer
  64. void (*GGML_CALL clear) (ggml_backend_buffer_t buffer, uint8_t value);
  65. void (*GGML_CALL reset) (ggml_backend_buffer_t buffer); // reset any internal state due to tensor initialization, such as tensor extras
  66. };
  67. struct ggml_backend_buffer {
  68. struct ggml_backend_buffer_i iface;
  69. ggml_backend_buffer_type_t buft;
  70. ggml_backend_buffer_context_t context;
  71. size_t size;
  72. enum ggml_backend_buffer_usage usage;
  73. };
  74. GGML_CALL ggml_backend_buffer_t ggml_backend_buffer_init(
  75. ggml_backend_buffer_type_t buft,
  76. struct ggml_backend_buffer_i iface,
  77. ggml_backend_buffer_context_t context,
  78. size_t size);
  79. // do not use directly, use ggml_backend_tensor_copy instead
  80. bool ggml_backend_buffer_copy_tensor(const struct ggml_tensor * src, struct ggml_tensor * dst);
  81. // buffer that contains a collection of buffers
  82. GGML_CALL ggml_backend_buffer_t ggml_backend_multi_buffer_alloc_buffer(ggml_backend_buffer_t * buffers, size_t n_buffers);
  83. GGML_CALL bool ggml_backend_buffer_is_multi_buffer(ggml_backend_buffer_t buffer);
  84. GGML_CALL void ggml_backend_multi_buffer_set_usage(ggml_backend_buffer_t buffer, enum ggml_backend_buffer_usage usage);
  85. //
  86. // Backend
  87. //
  88. typedef void * ggml_backend_context_t;
  89. struct ggml_backend_i {
  90. const char * (*GGML_CALL get_name)(ggml_backend_t backend);
  91. void (*GGML_CALL free)(ggml_backend_t backend);
  92. // buffer allocation
  93. ggml_backend_buffer_type_t (*GGML_CALL get_default_buffer_type)(ggml_backend_t backend);
  94. // (optional) asynchronous tensor data access
  95. void (*GGML_CALL set_tensor_async)(ggml_backend_t backend, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
  96. void (*GGML_CALL get_tensor_async)(ggml_backend_t backend, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
  97. bool (*GGML_CALL cpy_tensor_async)(ggml_backend_t backend_src, ggml_backend_t backend_dst, const struct ggml_tensor * src, struct ggml_tensor * dst);
  98. // (optional) complete all pending operations
  99. void (*GGML_CALL synchronize)(ggml_backend_t backend);
  100. // compute graph with a plan (not used currently)
  101. // create a new plan for a graph
  102. ggml_backend_graph_plan_t (*GGML_CALL graph_plan_create) (ggml_backend_t backend, const struct ggml_cgraph * cgraph);
  103. void (*GGML_CALL graph_plan_free) (ggml_backend_t backend, ggml_backend_graph_plan_t plan);
  104. // update the plan with a new graph - this should be faster than creating a new plan when the graph has the same topology
  105. void (*GGML_CALL graph_plan_update) (ggml_backend_t backend, ggml_backend_graph_plan_t plan, const struct ggml_cgraph * cgraph);
  106. // compute the graph with the plan
  107. enum ggml_status (*GGML_CALL graph_plan_compute)(ggml_backend_t backend, ggml_backend_graph_plan_t plan);
  108. // compute graph without a plan (async)
  109. enum ggml_status (*GGML_CALL graph_compute) (ggml_backend_t backend, struct ggml_cgraph * cgraph);
  110. // check if the backend can compute an operation
  111. bool (*GGML_CALL supports_op)(ggml_backend_t backend, const struct ggml_tensor * op);
  112. // check if the backend can use tensors allocated in a buffer type
  113. bool (*GGML_CALL supports_buft)(ggml_backend_t backend, ggml_backend_buffer_type_t buft);
  114. // check if the backend wants to run an operation, even if the weights are allocated in a CPU buffer
  115. // these should be expensive operations with large batch sizes that may benefit from running on this backend
  116. // even if the weight has to be copied from the CPU temporarily
  117. bool (*GGML_CALL offload_op)(ggml_backend_t backend, const struct ggml_tensor * op);
  118. // (optional) event synchronization
  119. // create a new event that can record events on this backend instance
  120. ggml_backend_event_t (*GGML_CALL event_new) (ggml_backend_t backend);
  121. void (*GGML_CALL event_free) (ggml_backend_event_t event);
  122. // record an event on the backend instance that created it
  123. void (*GGML_CALL event_record) (ggml_backend_event_t event);
  124. // wait for an event on on a different backend instance
  125. void (*GGML_CALL event_wait) (ggml_backend_t backend, ggml_backend_event_t event);
  126. // block until an event is recorded
  127. void (*GGML_CALL event_synchronize) (ggml_backend_event_t event);
  128. };
  129. struct ggml_backend {
  130. ggml_guid_t guid;
  131. struct ggml_backend_i iface;
  132. ggml_backend_context_t context;
  133. };
  134. struct ggml_backend_event {
  135. ggml_backend_t backend;
  136. void * context;
  137. };
  138. //
  139. // Backend registry
  140. //
  141. typedef ggml_backend_t (*GGML_CALL ggml_backend_init_fn)(const char * params, void * user_data);
  142. GGML_CALL void ggml_backend_register(const char * name, ggml_backend_init_fn init_fn, ggml_backend_buffer_type_t default_buffer_type, void * user_data);
  143. #ifdef __cplusplus
  144. }
  145. #endif