2023-06-04 20:34:30 +00:00
|
|
|
// An interface allowing to compute ggml_cgraph with Metal
|
|
|
|
//
|
|
|
|
// This is a fully functional interface that extends ggml with GPU support for Apple devices.
|
2024-06-04 18:23:20 +00:00
|
|
|
// A similar interface can be created for other GPU backends (e.g. Vulkan, CUDA, etc.)
|
2023-06-04 20:34:30 +00:00
|
|
|
//
|
|
|
|
// How it works?
|
|
|
|
//
|
|
|
|
// As long as your program can create and evaluate a ggml_cgraph on the CPU, you can use this
|
|
|
|
// interface to evaluate the same graph on the GPU. Instead of using ggml_graph_compute(), you
|
|
|
|
// use ggml_metal_graph_compute() (or ggml_vulkan_graph_compute(), etc.)
|
|
|
|
//
|
|
|
|
// You only need to make sure that all memory buffers that you used during the graph creation
|
|
|
|
// are mapped to the device memory with the ggml_metal_add_buffer() function. This mapping is
|
|
|
|
// used during the graph evaluation to determine the arguments of the compute kernels.
|
|
|
|
//
|
|
|
|
// Synchronization between device and host memory (for example for input and output tensors)
|
|
|
|
// is done with the ggml_metal_set_tensor() and ggml_metal_get_tensor() functions.
|
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-09-27 15:48:33 +00:00
|
|
|
#include "ggml.h"
|
2023-10-08 17:19:14 +00:00
|
|
|
#include "ggml-backend.h"
|
2023-09-27 15:48:33 +00:00
|
|
|
|
2023-06-04 20:34:30 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
// max memory buffers that can be mapped to the device
|
2023-11-13 14:55:52 +00:00
|
|
|
#define GGML_METAL_MAX_BUFFERS 64
|
2023-06-04 20:34:30 +00:00
|
|
|
|
|
|
|
struct ggml_tensor;
|
|
|
|
struct ggml_cgraph;
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2023-10-08 17:19:14 +00:00
|
|
|
//
|
|
|
|
// backend API
|
|
|
|
// user-code should use only these functions
|
|
|
|
//
|
|
|
|
|
2024-01-13 18:45:45 +00:00
|
|
|
GGML_API void ggml_backend_metal_log_set_callback(ggml_log_callback log_callback, void * user_data);
|
|
|
|
|
2023-10-08 17:19:14 +00:00
|
|
|
GGML_API ggml_backend_t ggml_backend_metal_init(void);
|
|
|
|
|
|
|
|
GGML_API bool ggml_backend_is_metal(ggml_backend_t backend);
|
|
|
|
|
2024-01-16 11:16:33 +00:00
|
|
|
GGML_API GGML_CALL ggml_backend_buffer_t ggml_backend_metal_buffer_from_ptr(void * data, size_t size, size_t max_size);
|
2023-12-21 20:07:46 +00:00
|
|
|
|
2023-10-08 17:19:14 +00:00
|
|
|
GGML_API void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb);
|
2023-12-21 20:07:46 +00:00
|
|
|
|
2024-08-07 06:55:49 +00:00
|
|
|
GGML_API void ggml_backend_metal_set_abort_callback(ggml_backend_t backend, ggml_abort_callback abort_callback, void * user_data);
|
|
|
|
|
2024-01-16 11:16:33 +00:00
|
|
|
GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_metal_buffer_type(void);
|
2023-12-07 20:26:54 +00:00
|
|
|
|
|
|
|
// helper to check if the device supports a specific family
|
|
|
|
// ideally, the user code should be doing these checks
|
|
|
|
// ref: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
|
|
|
|
GGML_API bool ggml_backend_metal_supports_family(ggml_backend_t backend, int family);
|
2023-10-08 17:19:14 +00:00
|
|
|
|
2024-01-29 09:22:23 +00:00
|
|
|
// capture all command buffers committed the next time `ggml_backend_graph_compute` is called
|
|
|
|
GGML_API void ggml_backend_metal_capture_next_compute(ggml_backend_t backend);
|
|
|
|
|
2023-06-04 20:34:30 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|