Skip to content

[RadixTree][6/N Refactor]: Refactor SWARadixTree to simplify the computation and alignment of bigram keys.#19427

Merged
ispobock merged 2 commits intosgl-project:mainfrom
hzh0425:hybrid/eagle-key-refactor
Feb 28, 2026
Merged

[RadixTree][6/N Refactor]: Refactor SWARadixTree to simplify the computation and alignment of bigram keys.#19427
ispobock merged 2 commits intosgl-project:mainfrom
hzh0425:hybrid/eagle-key-refactor

Conversation

@hzh0425
Copy link
Copy Markdown
Collaborator

@hzh0425 hzh0425 commented Feb 26, 2026

Motivation

Modifications

Accuracy Tests

nohup python -m sglang.launch_server \
  --tp=8 --trust-remote-code --mem-fraction-static=0.85 --cuda-graph-max-bs=100 \
  --reasoning-parser=gpt-oss --tool-call-parser=gpt-oss \
  --speculative-algorithm=EAGLE3 --model openai/gpt-oss-120b --speculative-draft-model-path=lmsys/EAGLE3-gpt-oss-120b-bf16 \
  --speculative-num-steps=3 --speculative-eagle-topk=1 --speculative-num-draft-tokens=4 --swa-full-tokens-ratio 0.5 > sglang.out &

before change

Accuracy: 0.938
Invalid: 0.008
Latency: 127.705 s
Output throughput: 3957.654 token/s

after change

Accuracy: 0.952
Invalid: 0.002
Latency: 117.163 s
Output throughput: 4184.993 token/s

Benchmarking and Profiling

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @hzh0425, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the SWARadixTree implementation to streamline the handling of bigram keys and page alignment. By extracting common logic into reusable helper functions, the internal methods of SWARadixCache become cleaner and more maintainable. This change improves the clarity and robustness of the radix tree's key management, particularly for EAGLE models, and is supported by new unit tests to ensure correctness.

Highlights

  • Refactored Bigram Key Conversion: Extracted the logic for converting keys to bigrams and aligning them to page boundaries into new global helper functions, maybe_bigram_convert and page_align_keys, in radix_cache.py.
  • Simplified SWARadixTree Logic: Updated SWARadixCache methods (insert, cache_finished_req, cache_unfinished_req, _match_pre_processor) to utilize these new helper functions, significantly reducing complexity and removing redundant calculations related to bigram key lengths and page alignment.
  • Improved Test Coverage: Added a new comprehensive unit test case to test_swa_unittest.py to specifically verify the correct handling of EAGLE bigram keys and cache_protected_len within the refactored cache_finished_req method.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • python/sglang/srt/mem_cache/radix_cache.py
    • Added new global functions maybe_bigram_convert and page_align_keys.
    • Refactored the maybe_bigram_convert method of SWARadixCache to call the new global helper.
    • Removed the _page_align_keys method from SWARadixCache.
    • Updated cache_finished_req and cache_unfinished_req to use the new global page_align_keys.
  • python/sglang/srt/mem_cache/swa_radix_cache.py
    • Imported the new global helper functions maybe_bigram_convert and page_align_keys.
    • Simplified the insert method by using maybe_bigram_convert and removing explicit value length adjustments.
    • Streamlined cache_finished_req by replacing complex length calculations with calls to the new helper functions and simplifying old_prefix_len determination.
    • Simplified cache_unfinished_req by adopting the new helper functions for key processing and aligning, and refining old_prefix_len and assertion logic.
    • Adjusted the logic for req.prefix_indices assignment in cache_unfinished_req.
    • Updated _match_pre_processor to use the new maybe_bigram_convert function.
  • test/registered/radix_cache/test_swa_unittest.py
    • Introduced a _DummyReq class and a _build_swa_tree helper function for consistent test setup.
    • Added test_swa_cache_finished_req_eagle_uses_cache_protected_len_and_bigram_key to validate bigram key and cache_protected_len handling in cache_finished_req.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@hzh0425
Copy link
Copy Markdown
Collaborator Author

hzh0425 commented Feb 26, 2026

/tag-and-rerun-ci

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the SWARadixTree to simplify how bigram keys and page alignment are handled, primarily for EAGLE models. By extracting maybe_bigram_convert and page_align_keys into standalone helper functions, the code in both RadixCache and SWARadixCache becomes significantly cleaner and more maintainable. The changes also appear to fix some subtle bugs related to key conversion and prefix length calculation, which are well-covered by the new unit tests. Overall, this is a great improvement in code quality. I have one suggestion to fix a potential issue in SWARadixCache.insert.

Comment thread python/sglang/srt/mem_cache/swa_radix_cache.py Outdated
@ispobock
Copy link
Copy Markdown
Collaborator

ref: #13714

@hzh0425
Copy link
Copy Markdown
Collaborator Author

hzh0425 commented Feb 28, 2026

AIME25 Repeat 16 test:

nemo-run_1/0 ----------------------------------------- aime25 ----------------------------------------
nemo-run_1/0 evaluation_mode | num_entries | avg_tokens | gen_seconds | symbolic_correct | no_answer
nemo-run_1/0 pass@1[avg-of-16] | 30 | 5201 | 414 | 72.50% ± 6.02% | 11.46%
nemo-run_1/0 majority@16 | 30 | 5201 | 414 | 86.67% | 0.00%
nemo-run_1/0 pass@16 | 30 | 5201 | 414 | 93.33% | 0.00%

after changes:
nemo-run_1/0 ----------------------------------------- aime25 ----------------------------------------
nemo-run_1/0 evaluation_mode | num_entries | avg_tokens | gen_seconds | symbolic_correct | no_answer
nemo-run_1/0 pass@1[avg-of-16] | 30 | 5244 | 431 | 71.46% ± 5.96% | 12.29%
nemo-run_1/0 majority@16 | 30 | 5244 | 431 | 86.67% | 0.00%
nemo-run_1/0 pass@16 | 30 | 5244 | 431 | 93.33% | 0.00%

@ispobock ispobock merged commit a0d8a7a into sgl-project:main Feb 28, 2026
472 of 504 checks passed
magicYang1573 pushed a commit to magicYang1573/sglang that referenced this pull request Mar 9, 2026
Wangzheee pushed a commit to Wangzheee/sglang that referenced this pull request Mar 21, 2026
JustinTong0323 pushed a commit to JustinTong0323/sglang that referenced this pull request Apr 7, 2026
@hnyls2002 hnyls2002 mentioned this pull request Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants