ggml-metal.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * llama.cpp - git 059031b8c40e1f4ba60586842c5b1ed3ddf61842
  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, 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 "ggml.h"
  46. #include "ggml-backend.h"
  47. #include <stddef.h>
  48. #include <stdbool.h>
  49. // max memory buffers that can be mapped to the device
  50. #define GGML_METAL_MAX_BUFFERS 64
  51. struct ggml_tensor;
  52. struct ggml_cgraph;
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. //
  57. // backend API
  58. // user-code should use only these functions
  59. //
  60. GGML_API void ggml_backend_metal_log_set_callback(ggml_log_callback log_callback, void * user_data);
  61. GGML_API ggml_backend_t ggml_backend_metal_init(void);
  62. GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);
  63. GGML_API GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t size, size_t max_size);
  64. GGML_API void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb);
  65. GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void);
  66. // helper to check if the device supports a specific family
  67. // ideally, the user code should be doing these checks
  68. // ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  69. GGML_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
  70. // capture all command buffers committed the next time `ggml_backend_graph_compute` is called
  71. GGML_API void ggml_backend_metal_capture_next_compute(ggml_backend_t backend);
  72. #ifdef __cplusplus
  73. }
  74. #endif