Sparse-inference accuracy gap: position compaction (A1) + eviction sweep (A2) - #4
Open
mmjerge wants to merge 6 commits into
Open
Sparse-inference accuracy gap: position compaction (A1) + eviction sweep (A2)#4mmjerge wants to merge 6 commits into
mmjerge wants to merge 6 commits into
Conversation
After eviction, retained tokens keep the RoPE rotations of their original absolute positions, leaving a 'holey' position layout the model never saw in pretraining -- a candidate cause of the sparse-inference accuracy gap (~10-14pp EM on HELMET nq/trivia_qa at h2o@4096). This adds an exact delta-rotation transform: each retained token is re-expressed at its rank among survivors (rotations compose, so no un-rotated keys are stored), queries get the matching scalar shift, and the causal mask is unchanged (ranking preserves order). - kvcache/pos_compact.py: compact_rope_positions + set_position_compaction - kvcache/base.py: compact_positions flag on KVCache, applied before mha - examples/grpo_helmet_crosseval.py: --compact-positions adds an 'h2o+compact' eval arm - test: delta-rotation exactness, dense no-op identity, h2o logit change
…ce/vlen/lastrec arms)
…zation is noise-level
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.
Investigating the sparse-inference accuracy gap (A1 + A2)
Follow-up to the RL work in awslabs#142. Matthias' standing concern: compared to RL/inference with dense attention, sparse (evicting) attention "is not accurate enough ... that needs to be dealt with now." This PR is the investigation into why, and what actually helps.
The measured gap (base Qwen2.5-0.5B, HELMET @8k,
h2o@4096, n=100): ~8pp EM on trivia_qa (0.62 dense -> 0.54), similar on nq. Two hypotheses tested.A1 -- position compaction (negative)
After eviction, retained tokens keep the RoPE rotations of their original absolute positions, so attention sees a "holey" layout (0, 3, 47, 812, ...) never seen in pretraining.
keys_values/kvcache/pos_compact.pyre-expresses queries and cached keys at compacted (rank-among-survivors) positions via an exact delta-rotation (RoPE composes additively; relative order preserved so the causal mask is unchanged). Toggle withset_position_compaction(model, True)orgrpo_helmet_crosseval.py --compact-positions.Result: no recovery (1-2pp, within noise, both datasets/metrics). The holey layout is not the main cause -- the information in the evicted tokens is simply gone. Details:
docs/POSITION_COMPACTION_A1.md.A2 -- eviction-policy sweep (one actionable finding)
examples/eviction_sweep.pysweeps retention knobs at a fixed slot budget: quantized vs fp buffers,normalize_scores,keep_initial_fraction(sink), large grace, v-length scoring, lastrec control; plus a budget frontier and pure-quantization controls.h2o_q8>=h2o_fp), so at equal memory it buys 2x the slots.h2o_q8@8192matches dense on both datasets (trivia 0.64/0.62, nq 0.26/0.25) at ~half the KV memory of bf16 dense.Details + tables:
docs/EVICTION_SWEEP_A2.md.Takeaway
Size the cache to cover the prompt and quantize, rather than evicting at full precision -- dense-level accuracy at ~half the memory. When the cache is genuinely smaller than the prompt, the gap is real, degrades gracefully (retaining ~25% of the prompt costs ~9pp EM), and none of the inference-time knobs here fix it. That regime (context >> cache) is the open problem where positional/retention research -- including the spherical-KV direction -- would actually be stressed.
Contents
keys_values/kvcache/pos_compact.py-- exact position-compaction transform +set_position_compactionkeys_values/kvcache/base.py--compact_positionsflag, applied before the attention call (no-op for dense; off by default)examples/eviction_sweep.py,examples/grpo_helmet_crosseval.py --compact-positions-- the A2 sweep and A1 A/B armtest/kvcache/test_pos_compact.py-- delta-rotation exactness, dense-cache identity (no-op), h2o logit-changedocs/POSITION_COMPACTION_A1.md,docs/EVICTION_SWEEP_A2.md-- findings (checked-in markdown)Notes
grpo-upstream(Safe eviction defaults (#140), GRPO single-epoch cache reuse, HELMET GRPO driver awslabs/keys_values#142): the sweep reuses that PR's HELMET eval harness. Rebase ontomainonce Safe eviction defaults (#140), GRPO single-epoch cache reuse, HELMET GRPO driver awslabs/keys_values#142 lands.