ggml-metal.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <stddef.h>
  21. #include <stdbool.h>
  22. // max memory buffers that can be mapped to the device
  23. #define GGML_METAL_MAX_BUFFERS 16
  24. struct ggml_tensor;
  25. struct ggml_cgraph;
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. struct ggml_metal_context;
  30. // number of command buffers to use
  31. struct ggml_metal_context * ggml_metal_init(int n_cb);
  32. void ggml_metal_free(struct ggml_metal_context * ctx);
  33. // set the number of command buffers to use
  34. void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);
  35. // creates a mapping between a host memory buffer and a device memory buffer
  36. // - make sure to map all buffers used in the graph before calling ggml_metal_graph_compute
  37. // - the mapping is used during computation to determine the arguments of the compute kernels
  38. // - you don't need to keep the host memory buffer allocated as it is never accessed by Metal
  39. // - max_size specifies the maximum size of a tensor and is used to create shared views such
  40. // that it is guaranteed that the tensor will fit in at least one of the views
  41. //
  42. bool ggml_metal_add_buffer(
  43. struct ggml_metal_context * ctx,
  44. const char * name,
  45. void * data,
  46. size_t size,
  47. size_t max_size);
  48. // set data from host memory into the device
  49. void ggml_metal_set_tensor(struct ggml_metal_context * ctx, struct ggml_tensor * t);
  50. // get data from the device into host memory
  51. void ggml_metal_get_tensor(struct ggml_metal_context * ctx, struct ggml_tensor * t);
  52. // same as ggml_graph_compute but uses Metal
  53. // creates gf->n_threads command buffers in parallel
  54. void ggml_metal_graph_compute(struct ggml_metal_context * ctx, struct ggml_cgraph * gf);
  55. #ifdef __cplusplus
  56. }
  57. #endif