ggml-metal.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * llama.cpp - commit 3f1ae2e32cde00c39b96be6d01c2997c29bae555 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  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, 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 "ggml.h"
  46. #include "ggml-backend.h"
  47. #include <stddef.h>
  48. #include <stdbool.h>
  49. struct ggml_tensor;
  50. struct ggml_cgraph;
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. //
  55. // backend API
  56. // user-code should use only these functions
  57. //
  58. GGML_API void ggml_backend_metal_log_set_callback(ggml_log_callback log_callback, void * user_data);
  59. GGML_API ggml_backend_t ggml_backend_metal_init(void);
  60. GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);
  61. GGML_API GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t size, size_t max_size);
  62. GGML_API void ggml_backend_metal_set_abort_callback(ggml_backend_t backend, ggml_abort_callback abort_callback, void * user_data);
  63. GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void);
  64. // helper to check if the device supports a specific family
  65. // ideally, the user code should be doing these checks
  66. // ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  67. GGML_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
  68. // capture all command buffers committed the next time `ggml_backend_graph_compute` is called
  69. GGML_API void ggml_backend_metal_capture_next_compute(ggml_backend_t backend);
  70. #ifdef __cplusplus
  71. }
  72. #endif