The backend API provides a hardware-agnostic abstraction for executing GGML computation graphs. Backends represent specific hardware devices (CPU, GPU, etc.) and manage memory buffers, tensor data transfer, and graph execution.
Type definitions
Buffer usage enum
Set weight buffers to GGML_BACKEND_BUFFER_USAGE_WEIGHTS before creating a scheduler. This lets the scheduler co-locate operations with the weights and reduce cross-device copies.
Buffer type API
A buffer type (ggml_backend_buffer_type_t) describes how a backend allocates and manages memory. You use it to create concrete buffers.
Returns the human-readable name of a buffer type. buft ggml_backend_buffer_type_t
required
The buffer type to query.
Returns a null-terminated string. The caller must not free it.
ggml_backend_buft_alloc_buffer
Allocates a new backend buffer of the given size. buft ggml_backend_buffer_type_t
required
The buffer type that defines where the memory is allocated.
Size of the buffer in bytes.
Returns a new buffer, or NULL on failure. Free with ggml_backend_buffer_free.
ggml_backend_buft_get_alignment
Returns the required memory alignment for this buffer type in bytes. buft ggml_backend_buffer_type_t
required
The buffer type to query.
ggml_backend_buft_is_host
Returns true if the buffer type is accessible directly from the host CPU. buft ggml_backend_buffer_type_t
required
The buffer type to query.
Buffer API
A buffer (ggml_backend_buffer_t) is a concrete allocation of device memory. Tensors are assigned into buffers before being used in graph computation.
Returns the human-readable name of a buffer. buffer ggml_backend_buffer_t
required
The buffer to query.
Frees a backend buffer and releases all memory it holds. buffer ggml_backend_buffer_t
required
The buffer to free. Passing NULL is safe.
ggml_backend_buffer_get_base
Returns a raw pointer to the start of the buffer’s memory region. buffer ggml_backend_buffer_t
required
The buffer to query.
Returns NULL for device buffers not directly accessible from the host.
ggml_backend_buffer_get_size
Returns the total size of the buffer in bytes. buffer ggml_backend_buffer_t
required
The buffer to query.
ggml_backend_buffer_clear
Fills the entire buffer with a constant byte value. buffer ggml_backend_buffer_t
required
The buffer to clear.
The byte value to fill every byte of the buffer with.
ggml_backend_buffer_is_host
Returns true if the buffer is in host-accessible memory. buffer ggml_backend_buffer_t
required
The buffer to query.
Backend (stream) API
A backend (ggml_backend_t) represents a compute stream on a device. Most programs create one backend per device and use it throughout the session.
Returns a globally unique identifier for the backend instance.
Returns the human-readable name of the backend.
Destroys the backend and releases its resources.
ggml_backend_get_default_buffer_type
Returns the default buffer type for this backend. Use this type when allocating buffers without a specific device preference.
ggml_backend_alloc_buffer
Allocates a buffer of the given size using the backend’s default buffer type. The backend that owns the allocation.
Equivalent to calling ggml_backend_buft_alloc_buffer with ggml_backend_get_default_buffer_type(backend).
Tensor operations
Copies data from a host buffer into a tensor (synchronous). tensor struct ggml_tensor *
required
Destination tensor.
Source data in host memory.
Byte offset into tensor->data at which to start writing.
Copies data from a tensor into a host buffer (synchronous). tensor const struct ggml_tensor *
required
Source tensor.
Destination buffer in host memory.
Byte offset into tensor->data at which to start reading.
Copies tensor data between two backends. Either or both may be device backends. src struct ggml_tensor *
required
Source tensor (can reside on any backend).
dst struct ggml_tensor *
required
Destination tensor (can reside on any backend).
The source and destination shapes and types must match.
Graph computation
ggml_backend_graph_plan_create
Creates a reusable execution plan for a computation graph. Plans can be executed multiple times without re-analyzing the graph structure. The backend that will execute the plan.
cgraph struct ggml_cgraph *
required
The computation graph to plan.
Free the plan with ggml_backend_graph_plan_free when done.
ggml_backend_graph_plan_compute
Executes a previously created graph plan. The backend that owns the plan.
plan ggml_backend_graph_plan_t
required
The plan to execute.
Returns GGML_STATUS_SUCCESS on success.
ggml_backend_graph_compute
Executes a computation graph directly, without a pre-created plan. The backend to run the graph on.
cgraph struct ggml_cgraph *
required
The computation graph to execute.
Returns GGML_STATUS_SUCCESS on success. For repeated execution of the same graph topology, prefer creating a plan with ggml_backend_graph_plan_create.
Synchronization
Creates a new synchronization event on the given device. device ggml_backend_dev_t
required
The device that will record and wait on the event.
Free with ggml_backend_event_free.
Destroys a synchronization event. event ggml_backend_event_t
required
The event to destroy.
ggml_backend_event_synchronize
Blocks the calling thread until the event has been recorded and all preceding operations on its backend have completed. event ggml_backend_event_t
required
The event to wait on.
Device API
A device (ggml_backend_dev_t) represents a physical or logical hardware unit. Multiple backend streams can be created from a single device.
Device type enum
Returns the short name of the device (e.g. "CUDA0"). device ggml_backend_dev_t
required
The device to query.
ggml_backend_dev_description
Returns a longer human-readable description of the device (e.g. "NVIDIA GeForce RTX 4090"). device ggml_backend_dev_t
required
The device to query.
Queries free and total memory available on the device. device ggml_backend_dev_t
required
The device to query.
Output: free memory in bytes.
Output: total memory in bytes.
Returns the type of the device. device ggml_backend_dev_t
required
The device to query.
Backend scheduler
The scheduler (ggml_backend_sched_t) enables transparent multi-device execution. It partitions the computation graph, assigns operations to the most suitable backend, and handles buffer allocation and inter-device tensor copies automatically.
Backends with a lower index in the array passed to ggml_backend_sched_new have higher scheduling priority.
Example usage
Creates a new backend scheduler. Array of backends to use. Index 0 has the highest priority.
bufts ggml_backend_buffer_type_t *
Optional array of buffer types (one per backend). Pass NULL to use each backend’s default buffer type.
Number of backends in the array.
Maximum number of nodes expected in a computation graph. Use GGML_DEFAULT_GRAPH_SIZE if unsure.
Whether to allow concurrent execution across backends.
Whether to offload supported operations to non-CPU backends automatically.
Free with ggml_backend_sched_free.
ggml_backend_sched_graph_compute
Allocates (if needed) and executes the computation graph across all scheduled backends. sched ggml_backend_sched_t
required
The scheduler.
graph struct ggml_cgraph *
required
The computation graph to execute.
Returns GGML_STATUS_SUCCESS on success. On the first call, buffers are allocated automatically.
Destroys the scheduler and releases all associated resources. sched ggml_backend_sched_t
required
The scheduler to free.
Backend registry
The registry tracks all loaded backends and their devices. Use these functions to enumerate available hardware and load dynamic backend plugins.
Loads a backend from a dynamic library file and registers it. File system path to the shared library (e.g. "libggml-cuda.so").
Returns the registration handle, or NULL on failure. Unload with ggml_backend_unload.
Discovers and loads all known backend shared libraries from the default search path. Call this once at startup if you want automatic hardware discovery without manually specifying backend paths.
Returns the total number of registered devices across all loaded backends.
Returns the device at the given index in the global device list. Zero-based device index. Must be less than ggml_backend_dev_count().