ggml-metal.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * llama.cpp - git 5bf2a2771886ee86137e01dbc7492f78fb392066
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023 Georgi Gerganov
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. // An interface allowing to compute ggml_cgraph with Metal
  27. //
  28. // This is a fully functional interface that extends ggml with GPU support for Apple devices.
  29. // A similar interface can be created for other GPU backends (e.g. Vulkan, CUDA, OpenCL, etc.)
  30. //
  31. // How it works?
  32. //
  33. // As long as your program can create and evaluate a ggml_cgraph on the CPU, you can use this
  34. // interface to evaluate the same graph on the GPU. Instead of using ggml_graph_compute(), you
  35. // use ggml_metal_graph_compute() (or ggml_vulkan_graph_compute(), etc.)
  36. //
  37. // You only need to make sure that all memory buffers that you used during the graph creation
  38. // are mapped to the device memory with the ggml_metal_add_buffer() function. This mapping is
  39. // used during the graph evaluation to determine the arguments of the compute kernels.
  40. //
  41. // Synchronization between device and host memory (for example for input and output tensors)
  42. // is done with the ggml_metal_set_tensor() and ggml_metal_get_tensor() functions.
  43. //
  44. #pragma once
  45. #include <stddef.h>
  46. #include <stdbool.h>
  47. // max memory buffers that can be mapped to the device
  48. #define GGML_METAL_MAX_BUFFERS 16
  49. struct ggml_tensor;
  50. struct ggml_cgraph;
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. struct ggml_metal_context;
  55. // number of command buffers to use
  56. struct ggml_metal_context * ggml_metal_init(int n_cb);
  57. void ggml_metal_free(struct ggml_metal_context * ctx);
  58. // set the number of command buffers to use
  59. void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);
  60. // creates a mapping between a host memory buffer and a device memory buffer
  61. // - make sure to map all buffers used in the graph before calling ggml_metal_graph_compute
  62. // - the mapping is used during computation to determine the arguments of the compute kernels
  63. // - you don't need to keep the host memory buffer allocated as it is never accessed by Metal
  64. // - max_size specifies the maximum size of a tensor and is used to create shared views such
  65. // that it is guaranteed that the tensor will fit in at least one of the views
  66. //
  67. bool ggml_metal_add_buffer(
  68. struct ggml_metal_context * ctx,
  69. const char * name,
  70. void * data,
  71. size_t size,
  72. size_t max_size);
  73. // set data from host memory into the device
  74. void ggml_metal_set_tensor(struct ggml_metal_context * ctx, struct ggml_tensor * t);
  75. // get data from the device into host memory
  76. void ggml_metal_get_tensor(struct ggml_metal_context * ctx, struct ggml_tensor * t);
  77. // same as ggml_graph_compute but uses Metal
  78. // creates gf->n_threads command buffers in parallel
  79. void ggml_metal_graph_compute(struct ggml_metal_context * ctx, struct ggml_cgraph * gf);
  80. #ifdef __cplusplus
  81. }
  82. #endif