ggml-metal.h 2.1 KB

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