[SWA] Avoid shape-dependent operations for paged eviction - #36751
Closed
paulzhang-tm wants to merge 1 commit into
Closed
paulzhang-tm wants to merge 1 commit into
paulzhang-tm wants to merge 1 commit into
Conversation
paulzhang-tm
requested review from
Ying1123,
alphabetc1,
hanming-lu,
hnyls2002,
huangtingwei9988,
hzh0425,
ispobock,
merrymercy,
xiezhq-hermann and
yizhang2077
as code owners
August 27, 2026 23:59
5 tasks
paulzhang-tm
requested review from
iforgetmyname,
ping1jing2 and
whybeyoung
as code owners
August 31, 2026 23:09
paulzhang-tm
force-pushed
the
codex/sync-free-swa-frees
branch
4 times, most recently
from
September 1, 2026 00:36
5ab96e5 to
1d2a8d2
Compare
## Why is this change needed? Out-of-window SWA frees on the paged `SWATokenToKVPoolAllocator` still run a data-dependent boolean filter (`_release_swa`) and a `torch.unique` in the paged allocator's `free()`, so every page-aligned segment that `free_swa_out_of_window_slots` releases synchronizes the scheduler stream with the in-flight forward. With the scheduler's WAR fence at target verify these are the only long stalls left in a saturated MTP decode step (4 to 5 per step at bs 750). ## Summary - `SWATokenToKVPoolAllocator.free_swa(free_index, *, start_pos=None)`: given the segment's position in its request's kv row and page alignment, release one representative per page with fixed-shape ops. Every page must map onto one SWA page; an async assert guards it. Calls without `start_pos`, unaligned segments and `page_size == 1` (already sync-free) keep the legacy path. Grouped frees collect the representatives and release them at `free_group_end`. - `PagedTokenToKVPoolAllocator.free_page_ids`: release live page ids after asynchronous validity and uniqueness checks, without `torch.unique`. - `free_swa_out_of_window_slots` passes `start_pos=req.kv.swa_evicted_seqlen` to every SWA allocator; the unified allocator already took that keyword, and the pure-SWA and HiSparse allocators now accept and ignore it, so the call site needs no allocator dispatch. - No process-global "mapping may be partial" fallback. The only allocation that leaves a request's head unmapped is the disaggregated-decode tail preallocation (`alloc_extend_swa_tail`), and that path already starts the eviction floor at the page-aligned tail (`swa_evicted_seqlen = fill_len - swa_tail_len`), so out-of-window segments are always fully mapped. Same call sites and timing; the end state is bitwise identical to the legacy path. ## Tests `test/registered/unit/mem_cache/test_sync_free_swa_inplace.py`: fixed path vs legacy on paged, grouped, mixed, page-size-1 and staged-release allocators; page-id validity and uniqueness asserts on host and device; segment alignment and mapping contracts; grouped out-of-window eviction without `torch.unique`; tail-only preallocation freeing from the tail floor; every SWA allocator accepts `start_pos`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
paulzhang-tm
force-pushed
the
codex/sync-free-swa-frees
branch
from
September 5, 2026 04:29
1d2a8d2 to
e191a39
Compare
Collaborator
|
Thanks @paulzhang-tm. Closing as superseded by #38159 (merged), which lands the same mechanism on
The bs 750 MTP observation (4 to 5 of these stalls per decode step) is a useful data point for this line; if you still see them on main with #38159, please open an issue with the config. |
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.
Why is this change needed?
Out-of-window SWA frees on the paged
SWATokenToKVPoolAllocatorstill run a data-dependent boolean filter (_release_swa) and atorch.uniquein the paged allocator'sfree(), so every page-aligned segment thatfree_swa_out_of_window_slotsreleases synchronizes the scheduler stream with the in-flight forward. With the scheduler's WAR fence at target verify these are the only long stalls left in a saturated MTP decode step (4 to 5 per step at bs 750).Summary
SWATokenToKVPoolAllocator.free_swa(free_index, *, start_pos=None): given the segment's position in its request's kv row and page alignment, release one representative per page with fixed-shape ops. Every page must map onto one SWA page; an async assert guards it. Calls withoutstart_pos, unaligned segments andpage_size == 1(already sync-free) keep the legacy path. Grouped frees collect the representatives and release them atfree_group_end.PagedTokenToKVPoolAllocator.free_page_ids: release live page ids after asynchronous validity and uniqueness checks, withouttorch.unique.free_swa_out_of_window_slotspassesstart_pos=req.kv.swa_evicted_seqlento every SWA allocator; the unified allocator already took that keyword, and the pure-SWA and HiSparse allocators now accept and ignore it, so the call site needs no allocator dispatch.alloc_extend_swa_tail), and that path already starts the eviction floor at the page-aligned tail (swa_evicted_seqlen = fill_len - swa_tail_len), so out-of-window segments are always fully mapped.Same call sites and timing; the end state is bitwise identical to the legacy path.
Tests
test/registered/unit/mem_cache/test_sync_free_swa_inplace.py: fixed path vs legacy on paged, grouped, mixed, page-size-1 and staged-release allocators; page-id validity and uniqueness asserts on host and device; segment alignment and mapping contracts; grouped out-of-window eviction withouttorch.unique; tail-only preallocation freeing from the tail floor; every SWA allocator acceptsstart_pos.🤖 Generated with Claude Code
CI States
Latest PR Test (Base): ❌ Run #33944690097
Latest PR Test (Extra): ❌ Run #33944690141
Latest PR Test (AMD ROCm 7.2): ❌ Run #33944690067