Skip to content

Support speculative decoding in hybrid attention backend#9573

Merged
zhyncs merged 5 commits intosgl-project:mainfrom
Qiaolin-Yu:hybrid
Aug 28, 2025
Merged

Support speculative decoding in hybrid attention backend#9573
zhyncs merged 5 commits intosgl-project:mainfrom
Qiaolin-Yu:hybrid

Conversation

@Qiaolin-Yu
Copy link
Copy Markdown
Collaborator

@Qiaolin-Yu Qiaolin-Yu commented Aug 25, 2025

Motivation

Close #9330

Modifications

Accuracy Tests

Launch the server

SGLANG_ALLOW_OVERWRITE_LONGER_CONTEXT_LEN=1 python -m sglang.launch_server   --model-path meta-llama/Llama-3.1-8B-Instruct   --port 8005   --tensor-parallel-size 1   --speculative-algo EAGLE3   --speculative-draft jamesliu1/sglang-EAGLE3-Llama-3.1-Instruct-8B   --speculative-num-steps 3   --speculative-eagle-topk 1   --speculative-num-draft-tokens 4   --mem-fraction 0.6     --dtype bfloat16 --prefill-attention-backend flashinfer --decode-attention-backend fa3

Test accuracy

python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 1319 --parallel 1319 --port 8005

Accuracy: 0.794

Benchmarking and Profiling

Checklist

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.

Summary of Changes

Hello @Qiaolin-Yu, 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 introduces support for speculative decoding within the hybrid attention backend, which was previously unsupported. This enhancement allows for more efficient inference by leveraging both prefill and decode attention mechanisms in conjunction with speculative decoding algorithms. The changes involve adapting the HybridAttnBackend to correctly manage CUDA graph states and forward metadata for speculative operations, updating the ModelRunner to permit this configuration, and adding new test cases to validate the functionality.

Highlights

  • Speculative Decoding Support: Enables the use of speculative decoding algorithms with the HybridAttnBackend, allowing for potential performance improvements in inference.
  • HybridAttnBackend Enhancements: The HybridAttnBackend now correctly initializes CUDA graph states for both prefill and decode backends when speculative decoding is active, and intelligently dispatches forward metadata calls based on the current forward_mode.
  • Configuration Flexibility: Removes a previous restriction in the ModelRunner that prevented the combination of hybrid attention and speculative decoding, providing greater flexibility in backend configuration.
  • Comprehensive Testing: Introduces a new dedicated test suite, TestHybridAttnBackendSpeculativeDecoding, to ensure the stability and correctness of speculative decoding when used with the hybrid attention backend.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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.

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 adds support for speculative decoding in the hybrid attention backend. The changes involve updating the HybridAttnBackend to correctly delegate calls to either the prefill or decode backends based on the forward mode, which is crucial for handling the verification step in speculative decoding. The ModelRunner is also updated to allow this configuration, and a new test case is added to validate the functionality. The changes are logical and well-implemented. I've suggested some refactoring to reduce code duplication in HybridAttnBackend for better maintainability.

Comment thread python/sglang/srt/layers/attention/hybrid_attn_backend.py
Comment thread python/sglang/srt/layers/attention/hybrid_attn_backend.py
Comment thread python/sglang/srt/layers/attention/hybrid_attn_backend.py
@Fridge003 Fridge003 self-assigned this Aug 26, 2025
Comment thread python/sglang/srt/layers/attention/hybrid_attn_backend.py
Comment thread python/sglang/srt/layers/attention/hybrid_attn_backend.py
Comment thread test/srt/test_hybrid_attn_backend.py Outdated
@Qiaolin-Yu Qiaolin-Yu requested a review from Fridge003 August 28, 2025 00:23
Copy link
Copy Markdown
Collaborator

@Fridge003 Fridge003 left a comment

Choose a reason for hiding this comment

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

LGTM

@zhyncs zhyncs merged commit 4a4772a into sgl-project:main Aug 28, 2025
4 of 48 checks passed
@Qiaolin-Yu Qiaolin-Yu deleted the hybrid branch August 28, 2025 08:39
MahmoudAshraf97 pushed a commit to MahmoudAshraf97/sglang that referenced this pull request Sep 8, 2025
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.

[Bug] AssertionError: Currently HybridAttentionBackend does not support speculative decoding.

4 participants