Skip to content

llama : reduce backend sampling host output buffers - #29576

Open
yu830 wants to merge 3 commits into
ggml-org:masterfrom
yu830:local/29487-compact-sampling-output
Open

yu830 wants to merge 3 commits into
ggml-org:masterfrom
yu830:local/29487-compact-sampling-output

Conversation

@yu830

@yu830 yu830 commented Sep 28, 2026 •

Copy link
Copy Markdown

Overview

Fixes #29487.

Backend Top-K can return K candidates, but the host probability and candidate buffers still reserve vocabulary-width rows. This change sizes those two buffers using the maximum output width of the bound sampler chains. Both logits buffers keep their existing vocabulary-width layout.

The width is cached in the sampler chain. Adding or removing a sampler invalidates it and falls back to the full vocabulary width. Copies, getters and reordering use the same stride; layout changes wait for outstanding copies before reshaping the buffer.

With Qwen3.5-0.8B-Q4_0, V=248320, K=8 and 64 output rows, the measured total output buffer drops from 254,279,936 to 127,144,192 bytes (49.998%). On CUDA this saves pinned host memory. Each logits buffer remains 63,569,920 bytes.

Additional information

The memory measurements, test results, perplexity, and benchmark numbers reported in this description were obtained with the original PR commit 738403b7.

The current PR head is 800128fb. The CPU/CUDA test, perplexity, and benchmark results for this commit remain unconfirmed.

Validation on WSL Ubuntu 24.04, Ryzen 9 9955HX and RTX 5070 Laptop:

  • The capacity regression fails against the unmodified baseline and passes with the patch. The tests cover mixed K, sparse outputs, layout changes with pending copies, and removing Top-K from a bound chain.
  • CPU/CUDA sampler tests pass (21 cases). The two new cases also pass CPU ASan/UBSan.
  • Local CPU CI stages completed, including Debug/Release model tests (5/5 each). Two environment failures were fixed and individually rerun: HTTP routing in test-arg-parser and the Python environment in test-jinja-py.
  • CPU/CUDA perplexity is unchanged at 14.6649 +/- 1.41828, using Wikitext2, context 1024, batch 512 and two chunks.

Median results from three interleaved before/after pairs:

Backend K8/R64 decode + sync, ms pp128, tokens/s tg32, tokens/s
CPU 122.186 -> 121.692 1090.327 -> 1043.345 50.964 -> 52.948
CUDA 15.635 -> 15.326 5522.287 -> 5575.186 196.514 -> 196.704

The initial short CPU tg32 run was 9.4% slower; repeating tg32 with 10 repetitions and testing tg128 did not reproduce that decrease. Timings varied between runs, so this is a memory reduction claim, not a speedup claim.

CUDA Compute Sanitizer could not initialize the WDDM debugger interface, so GPU memcheck is unverified. Other backends have not been tested.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes. Codex implemented the patch and tests, ran local validation and drafted this description at my request. Gemini 3.8 Flash reviewed the diff, and Claude Opus 4.6 discussed the cache invalidation design. The task was to reduce backend Top-K host output storage while preserving the logits layout and asynchronous copy lifetime.

@github-actions github-actions Bot added the testing Everything test related label Sep 28, 2026
@yu830
yu830 marked this pull request as ready for review September 28, 2026 14:44
@yu830
yu830 requested a review from ggerganov as a code owner September 28, 2026 14:44
@yu830
yu830 force-pushed the local/29487-compact-sampling-output branch from 738403b to 800128f Compare October 28, 2026 15:50
@yu830
yu830 requested a review from a team as a code owner October 28, 2026 15:50
@yu830

yu830 commented Oct 29, 2026

Copy link
Copy Markdown
Author

Hi! When you have a chance, could a maintainer please approve the pending CI workflows and, if appropriate, trigger the project's review bot? I’d also appreciate any feedback on the current approach. Thank you!

@am17an
am17an requested a review from ORippler November 4, 2026 05:12

@ORippler ORippler left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d also appreciate any feedback on the current approach

The current design feels a bit brittle:

  1. It does not apply to sampled logits (but could be, also for top-k). We could also look to extend this to raw logits eventually, but that should happen independently/as a follow-up.
  2. white-list feels like we could soon start to silently loose this memory optimization

If we want to invest in this direction, I think it might make sense to:

  1. Record separate bounds for a sampler chain's logits, probabilities, and candidates
  2. Declare for each sampler whether output shapes are predictable or not as opposed to maintaining a white-list.
  3. Ensure traced shapes are used, potentially by extending existing buffer_view to have a stride and row-getter interface, and using it everywhere
template<typename T>
struct output_buffer_view {
    T * data;
    size_t size;
    size_t stride;

    T * row(size_t i) const {
        return data + i * stride;
    }
};

Comment thread src/llama-context.cpp Outdated
const bool sampling_layout_changed = sampling.output_stride != output_stride ||
sampling.sampled.size != (has_sampling ? (size_t) n_outputs_max : 0);
if (buf_output && sampling_layout_changed) {
synchronize();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
synchronize();
ggml_backend_sched_synchronize(sched.get());

Is there a reason to reset performance counters here?

Comment thread src/llama-sampler.cpp Outdated
size_t output_width = 1;
for (auto * output : { data.probs, data.candidates }) {
if (output) {
output_width = std::max(output_width, (size_t) ggml_nelements(output));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels a bit of a misnomer, as it doesn't look at raw/sampled logits

Track separate output bounds and use stride-aware buffer views.
Replace the sampler whitelist with fixed-shape declarations.
Refresh scheduler reservations when sampler chains change.
@yu830
yu830 requested a review from a team as a code owner November 6, 2026 04:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misc. bug: Backend Top-K output buffer is still allocated at full vocabulary width

2 participants