ggml-backend-impl.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * llama.cpp - commit 3f1ae2e32cde00c39b96be6d01c2997c29bae555 - 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 memset_tensor) (ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, uint8_t value, size_t offset, size_t size);
  62. void (*GGML_CALL set_tensor) (ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
  63. void (*GGML_CALL get_tensor) (ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
  64. 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
  65. void (*GGML_CALL clear) (ggml_backend_buffer_t buffer, uint8_t value);
  66. void (*GGML_CALL reset) (ggml_backend_buffer_t buffer); // reset any internal state due to tensor initialization, such as tensor extras
  67. };
  68. struct ggml_backend_buffer {
  69. struct ggml_backend_buffer_i iface;
  70. ggml_backend_buffer_type_t buft;
  71. ggml_backend_buffer_context_t context;
  72. size_t size;
  73. enum ggml_backend_buffer_usage usage;
  74. };
  75. GGML_CALL ggml_backend_buffer_t ggml_backend_buffer_init(
  76. ggml_backend_buffer_type_t buft,
  77. struct ggml_backend_buffer_i iface,
  78. ggml_backend_buffer_context_t context,
  79. size_t size);
  80. // do not use directly, use ggml_backend_tensor_copy instead
  81. bool ggml_backend_buffer_copy_tensor(const struct ggml_tensor * src, struct ggml_tensor * dst);
  82. // buffer that contains a collection of buffers
  83. GGML_CALL ggml_backend_buffer_t ggml_backend_multi_buffer_alloc_buffer(ggml_backend_buffer_t * buffers, size_t n_buffers);
  84. GGML_CALL bool ggml_backend_buffer_is_multi_buffer(ggml_backend_buffer_t buffer);
  85. GGML_CALL void ggml_backend_multi_buffer_set_usage(ggml_backend_buffer_t buffer, enum ggml_backend_buffer_usage usage);
  86. //
  87. // Backend
  88. //
  89. typedef void * ggml_backend_context_t;
  90. struct ggml_backend_i {
  91. const char * (*GGML_CALL get_name)(ggml_backend_t backend);
  92. void (*GGML_CALL free)(ggml_backend_t backend);
  93. // buffer allocation
  94. ggml_backend_buffer_type_t (*GGML_CALL get_default_buffer_type)(ggml_backend_t backend);
  95. // (optional) asynchronous tensor data access
  96. void (*GGML_CALL set_tensor_async)(ggml_backend_t backend, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
  97. void (*GGML_CALL get_tensor_async)(ggml_backend_t backend, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size);
  98. 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);
  99. // (optional) complete all pending operations
  100. void (*GGML_CALL synchronize)(ggml_backend_t backend);
  101. // compute graph with a plan (not used currently)
  102. // create a new plan for a graph
  103. ggml_backend_graph_plan_t (*GGML_CALL graph_plan_create) (ggml_backend_t backend, const struct ggml_cgraph * cgraph);
  104. void (*GGML_CALL graph_plan_free) (ggml_backend_t backend, ggml_backend_graph_plan_t plan);
  105. // update the plan with a new graph - this should be faster than creating a new plan when the graph has the same topology
  106. void (*GGML_CALL graph_plan_update) (ggml_backend_t backend, ggml_backend_graph_plan_t plan, const struct ggml_cgraph * cgraph);
  107. // compute the graph with the plan
  108. enum ggml_status (*GGML_CALL graph_plan_compute)(ggml_backend_t backend, ggml_backend_graph_plan_t plan);
  109. // compute graph without a plan (async)
  110. enum ggml_status (*GGML_CALL graph_compute) (ggml_backend_t backend, struct ggml_cgraph * cgraph);
  111. // check if the backend can compute an operation
  112. bool (*GGML_CALL supports_op)(ggml_backend_t backend, const struct ggml_tensor * op);
  113. // check if the backend can use tensors allocated in a buffer type
  114. bool (*GGML_CALL supports_buft)(ggml_backend_t backend, ggml_backend_buffer_type_t buft);
  115. // check if the backend wants to run an operation, even if the weights are allocated in a CPU buffer
  116. // these should be expensive operations with large batch sizes that may benefit from running on this backend
  117. // even if the weight has to be copied from the CPU temporarily
  118. bool (*GGML_CALL offload_op)(ggml_backend_t backend, const struct ggml_tensor * op);
  119. // (optional) event synchronization
  120. // create a new event that can record events on this backend instance
  121. ggml_backend_event_t (*GGML_CALL event_new) (ggml_backend_t backend);
  122. void (*GGML_CALL event_free) (ggml_backend_event_t event);
  123. // record an event on the backend instance that created it
  124. void (*GGML_CALL event_record) (ggml_backend_event_t event);
  125. // wait for an event on on a different backend instance
  126. void (*GGML_CALL event_wait) (ggml_backend_t backend, ggml_backend_event_t event);
  127. // block until an event is recorded
  128. void (*GGML_CALL event_synchronize) (ggml_backend_event_t event);
  129. };
  130. struct ggml_backend {
  131. ggml_guid_t guid;
  132. struct ggml_backend_i iface;
  133. ggml_backend_context_t context;
  134. };
  135. struct ggml_backend_event {
  136. ggml_backend_t backend;
  137. void * context;
  138. };
  139. //
  140. // Backend registry
  141. //
  142. typedef ggml_backend_t (*GGML_CALL ggml_backend_init_fn)(const char * params, void * user_data);
  143. 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);
  144. #ifdef __cplusplus
  145. }
  146. #endif