ggml-metal.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 64
  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. GGML_API void ggml_backend_metal_log_set_callback(ggml_log_callback log_callback, void * user_data);
  36. GGML_API ggml_backend_t ggml_backend_metal_init(void);
  37. GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);
  38. GGML_API GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t size, size_t max_size);
  39. GGML_API void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb);
  40. GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void);
  41. // helper to check if the device supports a specific family
  42. // ideally, the user code should be doing these checks
  43. // ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  44. GGML_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
  45. // capture all command buffers committed the next time `ggml_backend_graph_compute` is called
  46. GGML_API void ggml_backend_metal_capture_next_compute(ggml_backend_t backend);
  47. #ifdef __cplusplus
  48. }
  49. #endif