Repository navigation
fix(optimization): trim_to_length must retain the newest positions, not the oldest (#13033) - #13607
Merged
Merged
Conversation
Contributor
✅ SSOT Configuration Compliance: Passing🎉 No hardcoded values detected that have SSOT config equivalents! |
Applied Black, isort, and autoflake to match code-quality checks. Triggered by workflow auto-fix-formatting.yml.
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.
Thinking Path
LayerKVCache.trim_to_length()did the opposite of what it exists for. Storage is linear and append-only —update()writes at[filled_len : filled_len + n]andget()returnsk[:, :filled_len]— so index 0 is the first token of the sequence andfilled_len - 1is the most recent. The method only moved the pointer back:That retains
[0, max_len)— the oldest window — and silently drops everything recent. A sliding window needs the newest positions, so every caller got a context anchored at the start of the sequence that never advances. The shape was right and the content was the wrong end, which is why it went unnoticed.What Changed
trim_to_lengthnow relocates the newestmax_lenpositions down to offset 0 before movingfilled_len, via a small_retain_tailhelper.The copy goes through an explicit
.clone()of the source. Source and destination overlap whenevermax_len > filled_len / 2, and copying a tensor onto an overlapping view of itself is undefined — that is a real case, not a hypothetical, and it has its own test.The existing test was asserting the bug
test_trim_preserves_content_up_to_limitread:Implementation and test were wrong in the same direction, so the suite confirmed the defect rather than catching it. It is now
test_trim_retains_the_newest_positions_not_the_oldest, assertingk[:, 6:]and explicitly asserting the head does not match, so a symmetric fixture cannot pass by luck.Two further tests cover what the fix could plausibly get wrong:
test_trim_then_update_appends_after_the_retained_window— the write pointer must follow the relocated window. Moving data without the pointer (or the reverse) would either overwrite live positions or leave a hole of stale tokens.test_trim_with_overlapping_source_and_destination—seq=10,max_len=7, so[3:10]and[0:7]overlap across four positions.Verification
The tests fail against the old implementation. Source reverted, tests kept:
With the fix restored:
These tests are
@requires_torchand skip without it. Under the default interpreter they report28 passed, 25 skippedand every trim test is in the skipped set — running them there proves nothing. The runs above use an interpreter withtorch 2.13.0+cpuso the assertions actually execute.Scope
This is Step 1 of #13033 (correctness). Step 2 — replacing linear storage with a fixed-capacity circular ring so trimming is a pointer move with no copy — is a separate performance change and is not in this PR, so the issue stays open. The
_write_to_entryoverflow behaviour noted in the issue (raises rather than evicting) is also untouched.Refs #13033
Model Used
Claude Opus 5