ggml-metal.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // An interface allowing to compute ggml_cgraph with Metal
  2. //
  3. // This is a fully functional interface that extends ggml with GPU support for Apple devices.
  4. // A similar interface can be created for other GPU backends (e.g. Vulkan, CUDA, OpenCL, etc.)
  5. //
  6. // How it works?
  7. //
  8. // As long as your program can create and evaluate a ggml_cgraph on the CPU, you can use this
  9. // interface to evaluate the same graph on the GPU. Instead of using ggml_graph_compute(), you
  10. // use ggml_metal_graph_compute() (or ggml_vulkan_graph_compute(), etc.)
  11. //
  12. // You only need to make sure that all memory buffers that you used during the graph creation
  13. // are mapped to the device memory with the ggml_metal_add_buffer() function. This mapping is
  14. // used during the graph evaluation to determine the arguments of the compute kernels.
  15. //
  16. // Synchronization between device and host memory (for example for input and output tensors)
  17. // is done with the ggml_metal_set_tensor() and ggml_metal_get_tensor() functions.
  18. //
  19. #pragma once
  20. #include "ggml.h"
  21. #include "ggml-backend.h"
  22. #include <stddef.h>
  23. #include <stdbool.h>
  24. // max memory buffers that can be mapped to the device
  25. #define GGML_METAL_MAX_BUFFERS 16
  26. #define GGML_METAL_MAX_COMMAND_BUFFERS 32
  27. struct ggml_tensor;
  28. struct ggml_cgraph;
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. //
  33. // internal API
  34. // temporary exposed to user-code
  35. //
  36. struct ggml_metal_context;
  37. void ggml_metal_log_set_callback(ggml_log_callback log_callback, void * user_data);
  38. // number of command buffers to use
  39. struct ggml_metal_context * ggml_metal_init(int n_cb);
  40. void ggml_metal_free(struct ggml_metal_context * ctx);
  41. void * ggml_metal_host_malloc(size_t n);
  42. void ggml_metal_host_free (void * data);
  43. // set the number of command buffers to use
  44. void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);
  45. // creates a mapping between a host memory buffer and a device memory buffer
  46. // - make sure to map all buffers used in the graph before calling ggml_metal_graph_compute
  47. // - the mapping is used during computation to determine the arguments of the compute kernels
  48. // - you don't need to keep the host memory buffer allocated as it is never accessed by Metal
  49. // - max_size specifies the maximum size of a tensor and is used to create shared views such
  50. // that it is guaranteed that the tensor will fit in at least one of the views
  51. //
  52. bool ggml_metal_add_buffer(
  53. struct ggml_metal_context * ctx,
  54. const char * name,
  55. void * data,
  56. size_t size,
  57. size_t max_size);
  58. // set data from host memory into the device
  59. void ggml_metal_set_tensor(struct ggml_metal_context * ctx, struct ggml_tensor * t);
  60. // get data from the device into host memory
  61. void ggml_metal_get_tensor(struct ggml_metal_context * ctx, struct ggml_tensor * t);
  62. // try to find operations that can be run concurrently in the graph
  63. // you should run it again if the topology of your graph changes
  64. void ggml_metal_graph_find_concurrency(struct ggml_metal_context * ctx, struct ggml_cgraph * gf, bool check_mem);
  65. // if the graph has been optimized for concurrently dispatch, return length of the concur_list if optimized
  66. int ggml_metal_if_optimized(struct ggml_metal_context * ctx);
  67. // output the concur_list for ggml_alloc
  68. int * ggml_metal_get_concur_list(struct ggml_metal_context * ctx);
  69. // same as ggml_graph_compute but uses Metal
  70. // creates gf->n_threads command buffers in parallel
  71. void ggml_metal_graph_compute(struct ggml_metal_context * ctx, struct ggml_cgraph * gf);
  72. //
  73. // backend API
  74. // user-code should use only these functions
  75. //
  76. GGML_API ggml_backend_t ggml_backend_metal_init(void);
  77. GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);
  78. GGML_API void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb);
  79. #ifdef __cplusplus
  80. }
  81. #endif