Skip to content

[Fix] dsa-eagle-topk: Copy token keys and scales when relocating index cache - #44564

Merged
ispobock merged 4 commits into
sgl-project:mainfrom
sshleifer:fix-dsa-index-token-moves
Oct 19, 2026
Merged

ispobock merged 4 commits into
sgl-project:mainfrom
sshleifer:fix-dsa-index-token-moves

Conversation

@sshleifer

@sshleifer sshleifer commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fix for dsa with speculative_eagle_topk=2

Motivation

DSATokenToKVPool.move_kv_cache passes token-slot IDs to IndexKeyCache.move, but that method indexes its page buffer with them. With 64-token pages, slot 319 is page 4, offset 63; treating it as page 319 can access out of bounds. Small IDs that fit in the page dimension silently copy entire pages and overwrite unrelated keys.

This matters when speculative decoding relocates accepted tree tokens. The ordinary single-chain (speculative_eagle_topk=1) path skips that compaction.

Modifications

Move each selected token's FP8 index key and scale within the physical-page view used by the cache writers and readers. Advanced-index gathering before assignment preserves overlapping swaps. Layers that reuse another layer's indexer remain skipped.

Nonempty moves with pooled index keys (index_kpool > 1) now fail explicitly: relocating individual tokens would require rebuilding their pooled keys.

Accuracy Tests

On upstream main 87211088c98ae4f5b60625eebcce007db36aa6af, the new small-ID regression fails: swapping slots 1 and 2 corrupts neighboring keys without a CUDA error. With this patch, all three GPU cases pass on one GB300:

  • Small token IDs that previously selected whole pages.
  • Cross-page overlapping swaps, including the final allocated slot.
  • Empty moves.

The tests use a real DSA pool and its production key/scale writers and readers, checking exact key bytes, scales, unchanged neighbors, latent KV, and a layer that reuses another layer's indexer.

python -m pytest -q test/registered/kernels/ops/kvcache/test_dsa_index_cache_move.py
# 3 passed

These are cache correctness tests; no full-model branching-speculation accuracy run was performed for this PR.

Speed Tests and Profiling

Correctness fix; no performance claim or benchmark.

Checklist

  • Format code with pre-commit (all applicable hooks passed).
  • Add and register GPU regression tests.
  • Follow SGLang code style.
  • Documentation: no user-facing configuration change.

CI States

Latest PR Test (Base): ❌ Run #39139492033
Latest PR Test (Extra): ✅ Run #39139492003
Latest PR Test (AMD ROCm 10): ❌ Run #39139492084

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-15T19:30:53.300616Z f93beaa PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@sshleifer sshleifer changed the title [Fix] dsa: Copy token keys and scales when relocating index cache [Fix] dsa eagle_topk>1: Copy token keys and scales when relocating index cache Oct 15, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f93beaadca

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# Each physical page stores all key bytes followed by all scale bytes.
# Locations identify tokens; copying whole pages overwrites neighbors.
for part in (index_k[:, :key_bytes], index_k[:, key_bytes:]):
tokens = part.view(index_k.shape[0], page_size, -1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Honor the ROCm preshuffle layout when moving keys

On ROCm with SGLANG_USE_AITER=1 and aiter_can_use_preshuffle_paged_mqa() enabled, index keys are not token-major: _set_k_and_s_triton_kernel stores each 16x16 tile using the offsets in python/sglang/kernels/ops/attention/dsa/index_buf_accessor.py:387-399. Consequently this view makes each apparent token row contain pieces from multiple real tokens, so a branching speculative relocation silently copies mixed key bytes while copying one token's scale; use layout-aware offsets (or gather and re-store) for this path.

Useful? React with 👍 / 👎.

@ispobock ispobock added run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) labels Oct 16, 2026
@ispobock

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/kernels/ops/kvcache/test_dsa_index_cache_move.py

@ispobock

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

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

cd test/ && python3 registered/kernels/ops/kvcache/test_dsa_index_cache_move.py

for actual, expected in zip(pool.kv_buffer, original_kv, strict=True):
expected[target] = expected[source]
torch.testing.assert_close(actual, expected, rtol=0, atol=0)
assert pool.index_key_cache.buffer[1].shape[0] == 0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add if __name__ == "__main__": sys.exit(pytest.main([__file__, "-v", "-s"])), without it collect_tests rejects the file and base-a-test-cpu fails on every shard.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 99556e9. Added if __name__ == "__main__": sys.exit(pytest.main([__file__, "-v", "-s"])) and the sys import. Confirmed collect_tests accepts the file.

-Robot

index_k = self._kernel_view(index_k)
# Each physical page stores all key bytes followed by all scale bytes.
# Locations identify tokens; copying whole pages overwrites neighbors.
for part in (index_k[:, :key_bytes], index_k[:, key_bytes:]):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this assumes a token's key bytes are contiguous in the page, which is false on the aiter preshuffle layout. moves the wrong bytes and clobbers the rest of the tile.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 99556e9. Relocation now respects the AITER 16×16 preshuffled key layout and copies token scales separately. All 12 CPU layout cases and three GB300 cache tests pass. The four nonempty preshuffled cases failed before the fix. AMD hardware was not tested.

-Robot

@github-actions

This comment has been minimized.

@ispobock

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@sshleifer sshleifer changed the title [Fix] dsa eagle_topk>1: Copy token keys and scales when relocating index cache [Fix] dsa-eagle-topk: Copy token keys and scales when relocating index cache Oct 18, 2026
@ispobock
ispobock merged commit 96ba108 into sgl-project:main Oct 19, 2026
209 of 226 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants