Skip to main content
ggml uses an explicit computation graph (ggml_cgraph) to separate the definition of computations from their execution. You define operations using the tensor API, which records nodes in the graph. You then call ggml_graph_compute to execute the graph.

Creating graphs

ggml_new_graph

Allocates a new computation graph with the default capacity (GGML_DEFAULT_GRAPH_SIZE = 2048 nodes) and no gradient storage. The graph memory is drawn from ctx’s pool.

ggml_new_graph_custom

Allocates a graph with an explicit node capacity and optional gradient bookkeeping.
size_t
required
Maximum number of nodes (tensors) the graph can hold.
bool
required
When true, the graph allocates gradient accumulator storage. Required before calling ggml_build_backward_expand.

ggml_graph_overhead / ggml_graph_overhead_custom

Returns the number of bytes consumed by a graph structure in the context pool. Add this to your mem_size budget before calling ggml_new_graph or ggml_new_graph_custom.

Building graphs

ggml_build_forward_expand

Adds tensor and all of its transitive dependencies (source tensors) to the graph as forward-pass nodes. Call this once for each output tensor you want to compute.

ggml_build_backward_expand

Appends backward-pass nodes to cgraph for automatic differentiation. Must be called after all ggml_build_forward_expand calls. The graph must have been created with grads = true.
struct ggml_context *
required
Context used to allocate gradient tensors.
struct ggml_cgraph *
required
The forward graph to differentiate. Backward nodes are appended in place.
struct ggml_tensor **
required
Array of gradient accumulator tensors, one per node in the graph. Typically obtained via ggml_graph_get_grad_acc.

Computing graphs

Compute functions are declared in ggml-cpu.h and operate on the CPU backend.

ggml_graph_compute

Executes the graph using the plan previously prepared by ggml_graph_plan. Returns GGML_STATUS_SUCCESS on success. Typical usage:

ggml_graph_plan

Prepares a compute plan by determining the required work buffer size and associating a thread pool. Must be called before ggml_graph_compute. When cplan.work_size > 0, the caller must allocate cplan.work_data before passing it to ggml_graph_compute.
int
required
Number of threads to use. Pass GGML_DEFAULT_N_THREADS (4) for the default.
struct ggml_threadpool *
Pre-created thread pool. Pass NULL to create a temporary pool internally.

ggml_graph_compute_with_ctx

Convenience wrapper that allocates the work buffer inside ctx instead of requiring the caller to manage it separately. The context must have enough remaining space for the work data.
The trade-off of ggml_graph_compute_with_ctx over ggml_graph_compute is that you must reserve extra memory in the context for the work buffer. Use ggml_graph_compute directly when memory is tight.

Graph inspection

ggml_graph_n_nodes

Returns the number of nodes currently stored in the graph.

ggml_graph_nodes

Returns a pointer to the internal array of node tensors. The array has ggml_graph_n_nodes() entries.

ggml_graph_node

Returns the i-th node. Negative i counts from the end (nodes[n_nodes + i]).

ggml_graph_get_tensor

Looks up a tensor in the graph by name. Returns NULL if not found.

ggml_graph_get_grad

Returns the gradient tensor for node. Only valid after ggml_build_backward_expand has been called on a graph created with grads = true.

ggml_graph_get_grad_acc

Returns the gradient accumulator tensor for node. Gradient accumulators accumulate gradients across multiple backward passes before being reset.

Graph utilities

ggml_graph_reset

Resets all regular gradient tensors and optimizer momenta to zero, and sets the loss gradient to 1. Call before each backward pass in a training loop.

ggml_graph_clear

Removes all nodes from the graph without freeing the underlying memory. The graph can then be rebuilt with new nodes.

ggml_graph_print

Prints information and performance data for every node in the graph to stderr.

ggml_graph_dump_dot

Writes a Graphviz .dot file representing the computation graph. Pass the backward graph as gb and the forward graph as cgraph. Open the output file with dot -Tsvg graph.dot -o graph.svg to visualize the graph.

Thread pool

The thread pool is declared in ggml.h and implemented in the CPU backend (ggml-cpu.h).

ggml_threadpool_params

bool[512]
CPU affinity mask. All-zeros means use the OS default affinity settings.
int
required
Number of worker threads in the pool.
enum ggml_sched_priority
Scheduling priority: GGML_SCHED_PRIO_LOW, GGML_SCHED_PRIO_NORMAL, GGML_SCHED_PRIO_MEDIUM, GGML_SCHED_PRIO_HIGH, or GGML_SCHED_PRIO_REALTIME.
uint32_t
Polling aggressiveness. 0 means the threads sleep when idle; 100 means aggressive spinning. Higher values reduce latency at the cost of CPU utilization.
bool
When true, worker threads start in a paused state and must be resumed with ggml_threadpool_resume before they process any work.

ggml_threadpool_params_default

Returns a ggml_threadpool_params populated with sensible defaults for n_threads threads.

ggml_threadpool_params_init

Initializes an existing ggml_threadpool_params struct in place with default values.

ggml_threadpool_new

Creates and starts a new thread pool with the given parameters. Returns NULL on failure.

ggml_threadpool_free

Shuts down all worker threads and releases all resources associated with the pool.

ggml_threadpool_pause / ggml_threadpool_resume

Pauses and resumes worker threads. Pausing frees CPU time when no computation is in progress (e.g., during I/O-bound work between forward passes).