fix: complete the n_kept floor sweep deferred in #264 - #277
Merged
alessiodevoto merged 2 commits intoSep 15, 2026
Merged
Conversation
Three budget derivations still floored to 0 on short contexts and silently disabled compression safeguards: - kvcompose: route both numel budgets through compute_n_kept and floor the structured per-layer counts so no layer cache empties and the boosted keep_token_lower_bound tokens survive while compression_ratio < 1 - criticalkv: floor the stage-1 selection budget to 1 when first_stage_ratio > 0 so short contexts no longer skip stage-1 protection - fastkvzip: floor the short-context window size to 1 so scores[:, :, -0:] no longer protects the entire context The new tests drive the three presses on the unit-test model with 8-token contexts at compression ratios 0.95/0.99. Signed-off-by: supermario_leo <leo.stack@outlook.com>
tests/default_presses.py is not matched by pytest's default test_*.py discovery, so the new regression tests added there never ran under `pytest tests/` (make test). Move them to a self-contained tests/presses/test_budget_floor_sweep.py next to the other press tests (same placement as the NVIDIA#264 regression tests) and restore tests/default_presses.py to its original state. Refs NVIDIA#264 Signed-off-by: supermario_leo <leo.stack@outlook.com>
SuperMarioYL
force-pushed
the
fix/kvpress-budget-floor-sweep
branch
from
September 10, 2026 01:21
850d4d9 to
6082538
Compare
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.
PR description
#264 introduced
compute_n_kept(kvpress/utils.py) — 0 by design atcompression_ratio >= 1.0, elsemax(1, int(...))— and applied it to thek_len-form budget sites, but deferred the remaining budget derivations. Three unguarded sites survive onmain, each silently misbehaving on short contexts with no error raised:MaxJeblick/llama2-0b-unit-test, ctx=8)kvpress/presses/kvcompose_press.py:277,284int(numel * (1 - compression_ratio))(numel form)topk(0)silently empties the whole cache (structured form, ratio 0.95:cache.get_seq_length()=0,keys=(1,2,0,6)); evenkeep_token_lower_bound-boosted tokens (+1e9) are dropped bytopk(0)kvpress/presses/criticalkv_press.py:83int((1 - ratio) * k_len * first_stage_ratio)topk(scores, 0)is legal → stage-1 protection silently skipped (0 positions boosted tofinfo.max)kvpress/presses/fastkvzip_press.py:256window_size = int(ctx_len * window_ratio)scores[:, :, -0:] = 1.0sets all scores to 1.0 (the-0:slice is the whole tensor) → full-protection inversion on any context shorter than1/window_ratio(50 tokens at the default 0.02)Additionally, on the kvcompose structured path the per-layer distribution of the global budget can still leave a layer at 0 even after the global floor (probed: per-layer
[1, 0]at ratio 0.99 — one layer's cache empties whilecompression_ratio < 1.0).This PR completes the sweep:
compute_n_kept; the structured per-layer counts are clamped tomax(1, keep_token_lower_bound)so no layer empties whilecompression_ratio < 1.0(per-head zeros are left alone — head pruning is a legitimate outcome there).selection_budgetgets a floor only whenfirst_stage_ratio > 0andcompression_ratio < 1.0(mirroring then_safeguard shape from the fix: clamp n_kept to >=1 to prevent silent cache emptying on short contexts 🤖🤖🤖 #264 review;first_stage_ratio = 0means stage 1 is disabled by design and must stay at 0).window_size = max(1, int(ctx_len * self.window_ratio)).The change is purely additive: every floor only activates when the derived budget is 0. Verified byte-identical behaviour at normal ratios (kvcompose/criticalkv/fastkvzip at 0.5 and 0.8, ctx=128, fixed seed) — the clamp is inactive whenever the budget covers the layers.
compression_ratio >= 1.0still means "evict everything" everywhere (compute_n_keptcontract). The guarded sites from #264 and the hand-rolledchunk/pyramidkvfloors are untouched.Tests
tests/presses/test_budget_floor_sweep.py(10 cases, all on the real presses via the unit-test model, no mocks of the fixed code):bsz=2keep_token_lower_boundtokens survive at ratio 0.95 / 0.99 (per-layer >= lower bound)first_stage_ratio > 0(ratio 0.95 / 0.99), and stays at 0 whenfirst_stage_ratio = 0(design behavior, green onmaintoo)The 9 degenerate cases are red on
main(emptying / skipping / flattening) and green with this change. Fulltests/presses/passes (529 passed; the onetest_ea_with_stats.py::test_load_statsfailure is a pre-existing gated-repo download error, identical onmain).Checklist
make test) —tests/presses/529 passed / 4 skipped; the singletest_ea_with_stats.py::test_load_statsfailure is a pre-existing gated-repo download error that fails identically onmain(verified in a clean checkout)make style) — flake8 clean, mypy clean (83 files); noteblack/isortreport pre-existing drift onkvcompose_press.pythat is identical onmain(verified) and untouched by this PRgit commit -s