Repository navigation
Conversation
Assisted-by: Codex
yu830
force-pushed
the
local/29487-compact-sampling-output
branch
from
October 28, 2026 15:50
738403b to
800128f
Compare
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! |
ORippler
reviewed
Nov 5, 2026
Collaborator
There was a problem hiding this comment.
I’d also appreciate any feedback on the current approach
The current design feels a bit brittle:
- 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.
- 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:
- Record separate bounds for a sampler chain's
logits, probabilities, and candidates - Declare for each sampler whether output shapes are predictable or not as opposed to maintaining a white-list.
- Ensure traced shapes are used, potentially by extending existing
buffer_viewto 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;
}
};| 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(); |
Collaborator
There was a problem hiding this comment.
Suggested change
| synchronize(); | |
| ggml_backend_sched_synchronize(sched.get()); |
Is there a reason to reset performance counters here?
| 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)); |
Collaborator
There was a problem hiding this comment.
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
test-arg-parserand the Python environment intest-jinja-py.Median results from three interleaved before/after pairs:
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