Skip to main content
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.
ggml_backend_buffer_type_t
required
The buffer type to query.
Returns a null-terminated string. The caller must not free it.
Allocates a new backend buffer of the given size.
ggml_backend_buffer_type_t
required
The buffer type that defines where the memory is allocated.
size_t
required
Size of the buffer in bytes.
Returns a new buffer, or NULL on failure. Free with ggml_backend_buffer_free.
Returns the required memory alignment for this buffer type in bytes.
ggml_backend_buffer_type_t
required
The buffer type to query.
Returns true if the buffer type is accessible directly from the host CPU.
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.
ggml_backend_buffer_t
required
The buffer to query.
Frees a backend buffer and releases all memory it holds.
ggml_backend_buffer_t
required
The buffer to free. Passing NULL is safe.
Returns a raw pointer to the start of the buffer’s memory region.
ggml_backend_buffer_t
required
The buffer to query.
Returns NULL for device buffers not directly accessible from the host.
Returns the total size of the buffer in bytes.
ggml_backend_buffer_t
required
The buffer to query.
Fills the entire buffer with a constant byte value.
ggml_backend_buffer_t
required
The buffer to clear.
uint8_t
required
The byte value to fill every byte of the buffer with.
Returns true if the buffer is in host-accessible memory.
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.
ggml_backend_t
required
The backend to query.
Returns the human-readable name of the backend.
ggml_backend_t
required
The backend to query.
Destroys the backend and releases its resources.
ggml_backend_t
required
The backend to free.
Returns the default buffer type for this backend. Use this type when allocating buffers without a specific device preference.
ggml_backend_t
required
The backend to query.
Allocates a buffer of the given size using the backend’s default buffer type.
ggml_backend_t
required
The backend that owns the allocation.
size_t
required
Size in bytes.
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).
struct ggml_tensor *
required
Destination tensor.
const void *
required
Source data in host memory.
size_t
required
Byte offset into tensor->data at which to start writing.
size_t
required
Number of bytes to copy.
Copies data from a tensor into a host buffer (synchronous).
const struct ggml_tensor *
required
Source tensor.
void *
required
Destination buffer in host memory.
size_t
required
Byte offset into tensor->data at which to start reading.
size_t
required
Number of bytes to copy.
Copies tensor data between two backends. Either or both may be device backends.
struct ggml_tensor *
required
Source tensor (can reside on any backend).
struct ggml_tensor *
required
Destination tensor (can reside on any backend).
The source and destination shapes and types must match.

Graph computation

Creates a reusable execution plan for a computation graph. Plans can be executed multiple times without re-analyzing the graph structure.
ggml_backend_t
required
The backend that will execute the plan.
struct ggml_cgraph *
required
The computation graph to plan.
Free the plan with ggml_backend_graph_plan_free when done.
Executes a previously created graph plan.
ggml_backend_t
required
The backend that owns the plan.
ggml_backend_graph_plan_t
required
The plan to execute.
Returns GGML_STATUS_SUCCESS on success.
Executes a computation graph directly, without a pre-created plan.
ggml_backend_t
required
The backend to run the graph on.
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.
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.
ggml_backend_event_t
required
The event to destroy.
Blocks the calling thread until the event has been recorded and all preceding operations on its backend have completed.
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").
ggml_backend_dev_t
required
The device to query.
Returns a longer human-readable description of the device (e.g. "NVIDIA GeForce RTX 4090").
ggml_backend_dev_t
required
The device to query.
Queries free and total memory available on the device.
ggml_backend_dev_t
required
The device to query.
size_t *
required
Output: free memory in bytes.
size_t *
required
Output: total memory in bytes.
Returns the type of the 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.
ggml_backend_t *
required
Array of backends to use. Index 0 has the highest priority.
ggml_backend_buffer_type_t *
Optional array of buffer types (one per backend). Pass NULL to use each backend’s default buffer type.
int
required
Number of backends in the array.
size_t
required
Maximum number of nodes expected in a computation graph. Use GGML_DEFAULT_GRAPH_SIZE if unsure.
bool
required
Whether to allow concurrent execution across backends.
bool
required
Whether to offload supported operations to non-CPU backends automatically.
Free with ggml_backend_sched_free.
Allocates (if needed) and executes the computation graph across all scheduled backends.
ggml_backend_sched_t
required
The scheduler.
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.
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.
const char *
required
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.
size_t
required
Zero-based device index. Must be less than ggml_backend_dev_count().