Skip to content

perf(context): a context's schedulers place their graphs in one compute buffer - #190

Merged
marcospaulo merged 4 commits into
mainfrom
perf/graph-shared-buffer
Nov 12, 2026
Merged

marcospaulo merged 4 commits into
mainfrom
perf/graph-shared-buffer

Conversation

@marcospaulo

Copy link
Copy Markdown
Member

What

A context's main scheduler and its graph slots' schedulers place their graphs in one compute buffer a buffer type, where each slot held a buffer of its own beside the main scheduler's.

  • ggml-alloc: ggml_gallocr_shared, a set of buffers graph allocators share. An allocator takes a type's shared buffer when its plan is one chunk that fits, and a buffer of its own otherwise. The shared buffer is allocated again, larger, only while no allocator has a graph in it.
  • ggml-backend: ggml_backend_sched_set_shared_buffers, ggml_backend_sched_buffer_is_shared.
  • llama_context: the set is made at the reserve, the main scheduler and every slot join it, and when a device is short of memory only a slot that holds a buffer of its own is given back.
  • Switch: LLAMA_GRAPH_BUFFER_SHARED_LEGACY=1 keeps a buffer a scheduler. Not used with pipeline parallelism.

Why

On four RTX PRO 6000 S with GLM-5.3 Flash a kept 69-row reply graph's buffer was 394 to 404 MiB a card (Nov 11 box, build lines' buf_mib), so 32 kept graphs could hold 12.6 GiB a card. That is what held a long-context pool to 32 kept graphs. With one buffer a kept graph costs no compute buffer (it still costs its CUDA executable).

Why it is sound

A context computes one graph at a time and every tensor of a graph is written again at each compute. What a kept graph needs between computes lives in buffers of its own (pool tables, the draft bank, the NextN store, the recurrent state).

Nothing waits for it. A graph's inputs are host tensors (no input is pinned to a device), and what a scheduler writes to a device, its copies of the inputs and the graph's kernels, is queued on the device behind the graph computed before, its output reads and the draft index rows kept from it. A prompt's passes already rely on that order: each is built in the main scheduler's one buffer over the one before, with no synchronize between.

Read so far

Fixture, one RTX 5070 Ti, 23 sessions (four at 30,000 tokens), three turns, 32 kept graphs, traced:

card peak 69-row target eval (median)
a buffer a scheduler (the switch) 13,030 MiB 20.22 ms
one buffer (this PR, an earlier form) 11,542 MiB 20.28 ms

The slots held 900 + 572 MiB there (target and draft), and the peak fell 1,488. Run times of this final form against the switch are being read now, three interleaved untraced pairs; they go in the TORAD.md row.

Not read: the four cards. No serving gain is claimed from this alone: it gives memory back.

Checks

  • CPU: ctest -L main in build-cpu, 109 tests, 108 passed and 1 skipped by design.
  • test-llama-archs -a glm5next on the CPU passes with the buffers shared by default.
  • New test_glm_shared_buffers: six shapes in turn, five retained and one on the main scheduler, target and MTP head, bit-equal logits to the switch with as many graphs reused. Passes on one and two CPU ranks, on CUDA0 and on CUDA0,CUDA0 (tensor split), three seeds.

…te buffer

Each retained graph held a compute buffer of its own on every device beside
the main scheduler's, as large as the largest graph its slot ever held. On four
RTX PRO 6000 S with GLM-5.3 Flash a 69-row reply graph's was 394 to 404 MiB a
card, so 32 retained graphs could hold 12.6 GiB a card, which is what kept a
long-context pool to 32 of them.

A context computes one graph at a time and every tensor of a graph is written
again at each compute, so the main scheduler and the slots' now place their
graphs in one buffer a buffer type: ggml_gallocr_shared in ggml-alloc, which
an allocator takes when its plan is one chunk that fits and grows only while
no other allocator has a graph in it, set on a scheduler with
ggml_backend_sched_set_shared_buffers. The reserve sizes it for the largest
batch, as it sized the main scheduler's. A graph that does not fit gets a
buffer of its own, and only such a slot is given back when a device is short
of memory.

Nothing waits for it. A graph's inputs are host tensors, and what a scheduler
writes to a device (its copies of them, the graph's kernels) is queued on the
device behind the graph computed before, its output reads and the draft index
rows kept from it: the order a prompt's passes already relied on, each built
in the main scheduler's buffer over the one before.

Not with pipeline parallelism. LLAMA_GRAPH_BUFFER_SHARED_LEGACY=1 keeps a
buffer a scheduler. The trace's build line says buf_shared.

test-llama-archs: test_glm_shared_buffers decodes six shapes in turn, five
retained and one on the main scheduler, on the target and on the MTP head, and
gets the legacy switch's logits bit for bit with as many graphs reused;
--glm-shared-buffers-device runs it on named devices.
The meta backend keeps, for a buffer, the simple tensors of the tensors
initialized in it and their split states, and clears the split states of the
whole buffer when a graph in it is built again. With one compute buffer for
all of a context's schedulers every build then cleared the states of every
retained graph, each computed again at the graph's next input copy or output
read. An allocator set to share now gets, for a meta buffer type, a meta buffer
of its own over the shared one's device memory (ggml_backend_meta_buffer_alias),
so only the memory is shared and the bookkeeping is a scheduler's, as with
LLAMA_GRAPH_BUFFER_SHARED_LEGACY=1.

llama_get_memory_breakdown counts a shared buffer once, not once a scheduler.

test_glm_shared_buffers gains an arm without a read between decodes: a round's
six shapes are decoded back to back, each queued on the devices behind the one
before, and only the last decode's logits are read and compared with the
switch's. Its buffer count is now the context's compute memory, which stays
what the reserve allocated where a buffer a scheduler adds the retained graphs'.
@marcospaulo

Copy link
Copy Markdown
Member Author

Follow-up on this branch (head 8668bd1): main merged in; each scheduler set to share now holds a meta buffer of its own over the shared device memory (ggml_backend_meta_buffer_alias), because the meta backend clears a buffer's split states whole when a graph in it is built again; llama_get_memory_breakdown counts the shared buffer once; test_glm_shared_buffers gained an arm that decodes a round's six shapes back to back with only the last read, and measures compute memory instead of counting buffers. Run on this machine at this tree: the test on CPU, CPU,CPU, CUDA0 and CUDA0,CUDA0; ctest -L main 109 of 109 on the CPU. Fixture numbers are in the TORAD.md row; no four-card number and no serving gain are claimed.

@marcospaulo
marcospaulo merged commit c1f62e0 into main Nov 12, 2026
5 checks passed
@marcospaulo
marcospaulo deleted the perf/graph-shared-buffer branch November 12, 2026 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant