ggml-metal.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Note: this description is outdated
  2. //
  3. // An interface allowing to compute ggml_cgraph with Metal
  4. //
  5. // This is a fully functional interface that extends ggml with GPU support for Apple devices.
  6. // A similar interface can be created for other GPU backends (e.g. Vulkan, CUDA, etc.)
  7. //
  8. // How it works?
  9. //
  10. // As long as your program can create and evaluate a ggml_cgraph on the CPU, you can use this
  11. // interface to evaluate the same graph on the GPU. Instead of using ggml_graph_compute(), you
  12. // use ggml_metal_graph_compute() (or ggml_vulkan_graph_compute(), etc.)
  13. //
  14. // You only need to make sure that all memory buffers that you used during the graph creation
  15. // are mapped to the device memory with the ggml_metal_add_buffer() function. This mapping is
  16. // used during the graph evaluation to determine the arguments of the compute kernels.
  17. //
  18. // Synchronization between device and host memory (for example for input and output tensors)
  19. // is done with the ggml_metal_set_tensor() and ggml_metal_get_tensor() functions.
  20. //
  21. #pragma once
  22. #include "ggml.h"
  23. #include "ggml-backend.h"
  24. #include <stddef.h>
  25. #include <stdbool.h>
  26. struct ggml_tensor;
  27. struct ggml_cgraph;
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //
  32. // backend API
  33. // user-code should use only these functions
  34. //
  35. // TODO: remove in the future
  36. GGML_BACKEND_API ggml_backend_t ggml_backend_metal_init(void);
  37. GGML_BACKEND_API bool ggml_backend_is_metal(ggml_backend_t backend);
  38. GGML_BACKEND_API void ggml_backend_metal_set_abort_callback(ggml_backend_t backend, ggml_abort_callback abort_callback, void * user_data);
  39. // helper to check if the device supports a specific family
  40. // ideally, the user code should be doing these checks
  41. // ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  42. GGML_BACKEND_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
  43. // capture all command buffers committed the next time `ggml_backend_graph_compute` is called
  44. GGML_BACKEND_API void ggml_backend_metal_capture_next_compute(ggml_backend_t backend);
  45. GGML_BACKEND_API ggml_backend_reg_t ggml_backend_metal_reg(void);
  46. #ifdef __cplusplus
  47. }
  48. #endif