[Qwen3.5] Qwen3.5-27B inference repeat bug fix#19411
[Qwen3.5] Qwen3.5-27B inference repeat bug fix#19411ispobock merged 1 commit intosgl-project:mainfrom
Conversation
Summary of ChangesHello @AlfredYyong, 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 addresses a significant issue affecting the Qwen3.5-27B model's inference stability and output quality when deployed with tensor parallelism. By correctly configuring the Highlights
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request correctly fixes a bug causing repetitive outputs in the Qwen3.5-27B model when using tensor parallelism. The issue was due to the is_last_layer flag not being passed to the LayerCommunicator, which led to an incorrect all-reduce fusion on the final layer. The fix applies this flag in both Qwen3_5LinearDecoderLayer and Qwen3_5AttentionDecoderLayer, which is the correct approach. I've also left a medium-severity comment suggesting a refactoring to address code duplication between these two decoder layer classes, which would improve long-term maintainability.
| input_layernorm=self.input_layernorm, | ||
| post_attention_layernorm=self.post_attention_layernorm, | ||
| allow_reduce_scatter=True, | ||
| is_last_layer=(layer_id == config.num_hidden_layers - 1), |
There was a problem hiding this comment.
While adding is_last_layer is correct, it highlights a broader code duplication issue. The entire LayerCommunicator initialization block (lines 350-356) is identical to the one in Qwen3_5AttentionDecoderLayer (lines 541-547). Furthermore, the MLP forward logic in both forward methods is also duplicated (e.g., lines 377-403 and 625-650).
To improve maintainability and reduce redundancy, consider refactoring this common logic into a shared base class or a helper function. This would centralize the logic and ensure future changes are applied consistently.
|
/tag-and-rerun-ci |
|
I think this pr is reasonable, but allreduce + GemmaRMSNorm fusion is not implemented and I think this bug will not happen in this case? |
thanks for the review!~ The
so when it returns for intermediate layers, the next layer |
|
make sense |
cherry pick: #19411 and use old qwen-vl image process * [slime] fix qwen3.5 and qwen-vl Co-authored-by: Copilot <copilot@github.com>
|
sglang 0.5.9 ,h20 gpu tp=2 qwen3.5-27b, modified the file, is still meet the bug ,it not work |
|
reply like : The user has not provided an input with any content or context. I will respond with a greeting that is friendly and friendly. Hello, hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! |
Motivation
fix #19393
fix #19322
When deploying the
Qwen3.5-27Bmodel withtp=2, the model produces repetitive (degenerate) outputs, whiletp=1works correctly.Root Cause
Qwen3_5LinearDecoderLayerandQwen3_5AttentionDecoderLayerdo not passis_last_layertoLayerCommunicator(defaults toFalsefor all layers, including the last one).When
tp >= 2on sm90+ GPUs with flashinfer available,should_fuse_mlp_allreduce_with_next_layer()returnsTruefor every layer including the last one, becausenot self.is_last_layeris alwaysTrue. This causes the last layer's MLP output to skipall-reduceandpostprocess_layer, but there is no subsequent layer to perform the deferred all-reduce. The finalself.norm(hidden_states, residual)then adds un-reduced partial MLP output to the already-reduced residual, producing incorrect hidden states per TP rank, which leads to wrong logits and repetitive text generation.With
tp=1,should_fuse_mlp_allreduce_with_next_layer()always returnsFalse(requirestp_size > 1), so the issue never triggers.All other models using allreduce fusion (DeepSeek V2, Qwen3 MoE, GLM4 MoE, SDAR MoE, etc.) correctly set
is_last_layer.Modifications
Added
is_last_layer=(layer_id == config.num_hidden_layers - 1)toLayerCommunicatorinitialization in both:Qwen3_5LinearDecoderLayer(line 355)Qwen3_5AttentionDecoderLayer(line 546)This is consistent with how other models handle it, e.g.:
qwen3_moe.py:is_last_layer=(self.layer_id == self.config.num_hidden_layers - 1)Accuracy Tests
tp=1: No change (allreduce fusion was never triggered).tp=2: Previously produced repetitive/degenerate output; after fix, should produce correct output matchingtp=1.deploy cmd:
fix before:
output:

=================
after fixing:
Benchmarking and Profiling
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci