ggml-metal.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * llama.cpp - commit 46e3556e01b824e52395fb050b29804b6cff2a7c - 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. // Note: this description is outdated
  27. //
  28. // An interface allowing to compute ggml_cgraph with Metal
  29. //
  30. // This is a fully functional interface that extends ggml with GPU support for Apple devices.
  31. // A similar interface can be created for other GPU backends (e.g. Vulkan, CUDA, etc.)
  32. //
  33. // How it works?
  34. //
  35. // As long as your program can create and evaluate a ggml_cgraph on the CPU, you can use this
  36. // interface to evaluate the same graph on the GPU. Instead of using ggml_graph_compute(), you
  37. // use ggml_metal_graph_compute() (or ggml_vulkan_graph_compute(), etc.)
  38. //
  39. // You only need to make sure that all memory buffers that you used during the graph creation
  40. // are mapped to the device memory with the ggml_metal_add_buffer() function. This mapping is
  41. // used during the graph evaluation to determine the arguments of the compute kernels.
  42. //
  43. // Synchronization between device and host memory (for example for input and output tensors)
  44. // is done with the ggml_metal_set_tensor() and ggml_metal_get_tensor() functions.
  45. //
  46. #pragma once
  47. #include "ggml.h"
  48. #include "ggml-backend.h"
  49. #include <stddef.h>
  50. #include <stdbool.h>
  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_BACKEND_API ggml_backend_t ggml_backend_metal_init(void);
  61. GGML_BACKEND_API bool ggml_backend_is_metal(ggml_backend_t backend);
  62. GGML_DEPRECATED(
  63. GGML_BACKEND_API ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t size, size_t max_size),
  64. "obsoleted by the new device interface - https://github.com/ggerganov/llama.cpp/pull/9713");
  65. GGML_BACKEND_API void ggml_backend_metal_set_abort_callback(ggml_backend_t backend, ggml_abort_callback abort_callback, void * user_data);
  66. GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void);
  67. // helper to check if the device supports a specific family
  68. // ideally, the user code should be doing these checks
  69. // ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  70. GGML_BACKEND_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
  71. // capture all command buffers committed the next time `ggml_backend_graph_compute` is called
  72. GGML_BACKEND_API void ggml_backend_metal_capture_next_compute(ggml_backend_t backend);
  73. GGML_BACKEND_API ggml_backend_reg_t ggml_backend_metal_reg(void);
  74. #ifdef __cplusplus
  75. }
  76. #endif