ggml-metal.h 2.4 KB

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