Skip to main content
ggml separates the description of a computation graph from its execution. A backend is a pluggable execution target — CPU cores, a CUDA device, Apple Silicon GPU, or a remote machine. You write one graph-building routine and ggml dispatches it to whatever hardware is available.

Core types

ggml_backend_t

ggml_backend_t is an opaque handle to an initialized backend instance. It holds an execution stream and is the primary object you pass to graph compute calls.

ggml_backend_buffer_t and ggml_backend_buffer_type_t

Buffers hold the raw memory for tensors. A buffer type (ggml_backend_buffer_type_t) is a descriptor that tells ggml where and how to allocate memory. You get one from a backend and use it to allocate buffers:
Buffer usage hints let the scheduler make better decisions:

ggml_backend_dev_t and device discovery

Every registered backend exposes one or more ggml_backend_dev_t objects. You can enumerate all available devices at runtime:
Device types are defined by ggml_backend_dev_type: Convenience initializers select a backend without enumerating devices manually:

The backend scheduler

ggml_backend_sched_t lets you run a single computation graph across multiple backends simultaneously. The scheduler:
  • Assigns each graph node to the backend that best supports the operation
  • Copies tensors between backends automatically when needed
  • Allocates compute buffers on each backend
  • Prioritises backends with a lower index in the array you supply
Tensors allocated in buffers marked GGML_BACKEND_BUFFER_USAGE_WEIGHTS are preferentially assigned to whichever backend owns those weights.
The scheduler API follows a straightforward lifecycle:
1

Reserve (optional)

Pass a representative max-size graph to pre-allocate buffers. This avoids allocation at compute time.
2

Reset

Clear allocations from the previous graph before computing a new one.
3

Allocate

Explicitly allocate the graph (skipped automatically on first compute).
4

Set inputs

Copy data into the allocated input tensors.
5

Compute

Execute the graph. Returns a ggml_status value.
6

Read outputs

Copy results back to host memory.

Complete example

The following is drawn directly from examples/simple/simple-backend.cpp and shows the full lifecycle — backend selection, graph construction, scheduling, and result retrieval.

Available backends

CPU backend

SIMD-optimised execution on x86 and ARM with configurable thread pools.

CUDA backend

NVIDIA GPU acceleration with multi-GPU and split-tensor support.

Metal backend

Native Apple GPU compute for macOS and Apple Silicon.

Vulkan backend

Cross-vendor GPU support for Linux, Windows, and Android.

RPC backend

Distribute computation to remote machines over the network.