Skip to content

Fix SWA ownership across grouped frees - #36381

Merged
hnyls2002 merged 1 commit into
sgl-project:mainfrom
YazhiGao:fix/swa-grouped-free-ownership
Aug 25, 2026
Merged

hnyls2002 merged 1 commit into
sgl-project:mainfrom
YazhiGao:fix/swa-grouped-free-ownership

Conversation

@YazhiGao

@YazhiGao YazhiGao commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • resolve full-to-SWA mappings when grouped frees are enqueued
  • batch immutable physical SWA slot frees when the group flushes
  • add a regression covering a full slot remapped before group flush

Root cause

Grouped free_swa calls deferred full-token IDs instead of resolving their current SWA mappings. Cache reconciliation could remap the same full-token ID before the group flushed, causing the replacement SWA slot to be freed while the original slot leaked.

This change transfers ownership at enqueue time by resolving and clearing the mapping immediately, then queues the physical SWA slot for the batched allocator free.

Test plan

  • Full test_swa_unittest.py: 14 tests passed on CUDA
  • Ruff v0.15.1 checks passed
  • Black v26.1.0 check passed

Related: #36135 addresses the same underlying grouped-free ownership race with a different snapshot-barrier strategy.

-Robot


CI States

Latest PR Test (Base): ❌ Run #32901316992
Latest PR Test (Extra): ❌ Run #32901316710
Latest PR Test (AMD ROCm 7.2): ❌ Run #32901316920

@YazhiGao
YazhiGao marked this pull request as ready for review August 25, 2026 21:30
@hnyls2002

hnyls2002 commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test_swa_unittest.py test_swa_eviction_boundary.py test_swa_lock_release_lifecycle.py test_swa_radix_cache_kl.py test_unified_radix_cache_kl_swa.py test_streaming_session_swa.py
(2 tries)

@github-actions

github-actions Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Results for /rerun-test test_swa_unittest.py:

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/mem_cache/test_swa_unittest.py

@github-actions

github-actions Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Results for /rerun-test test_swa_unittest.py test_swa_eviction_boundary.py test_swa_lock_release_lifecycle.py test_swa_radix_cache_kl.py test_unified_radix_cache_kl_swa.py test_streaming_session_swa.py:

🚀 1-gpu-h100 (4 tests): ✅ View workflow run

cd test/ && python3 registered/unit/mem_cache/test_swa_unittest.py
cd test/ && python3 registered/unit/mem_cache/test_swa_eviction_boundary.py
cd test/ && python3 registered/radix_cache/test_swa_radix_cache_kl.py
cd test/ && python3 registered/sessions/test_streaming_session_swa.py

🚀 1-gpu-5090 (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/mem_cache/test_swa_lock_release_lifecycle.py

🚀 2-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_swa.py

@hnyls2002
hnyls2002 merged commit f7a5649 into sgl-project:main Aug 25, 2026
104 of 120 checks passed
elmo2019 pushed a commit to elmo2019/sglang that referenced this pull request Aug 27, 2026
Conflict in mem_cache/allocator/swa.py: sgl-project#36381 ("Fix SWA ownership across
grouped frees") and this branch both appended text at the same spot, right
after free_swa()'s clear_full_to_swa_mapping() call -- sgl-project#36381 added the
ownership-resolving deferral block that completes free_swa(), this branch
added free_full_segment / free_swa_segment / free_swa_page_reps.

Resolved by taking sgl-project#36381's block first (it belongs to free_swa()'s body)
and this branch's three methods after it. Note git presents the hunk in the
opposite order; applying it as-presented would end free_swa() at the mapping
clear -- freeing nothing -- and orphan `self.swa_attn_allocator.free(swa_indices)`
inside free_swa_page_reps() with swa_indices undefined.

free_group_end() auto-merged to sgl-project#36381's version (swa_attn_allocator.free()
instead of re-entering free_swa()). The other eight files auto-merged.

The new release paths already satisfy sgl-project#36381's invariant: free_swa_page_reps()
reads full_to_swa_index_mapping at call time and defers only resolved SWA
indices, and SWATokenToKVPoolAllocator.free_group_begin() does not put the
sub-allocators into a free group, so they release eagerly.
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 31, 2026
nzr-niu pushed a commit to nzr-niu/sglang that referenced this pull request Sep 1, 2026
Leoyzen pushed a commit to Leoyzen/sglang that referenced this pull request Sep 3, 2026
jakki-amd pushed a commit to jakki-amd/sglang that referenced this pull request Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants