Skip to content

[AMD][Qwen3.5] TP2 prefill: overlap the layer all-reduces with compute over SDMA (on by default) - #118

Merged
sammysun0711 merged 6 commits into
apinge:qwen4_v0.5.21from
yicyang:qwen35-tp2-sdma-ar-overlap
Dec 2, 2026
Merged

sammysun0711 merged 6 commits into
apinge:qwen4_v0.5.21from
yicyang:qwen35-tp2-sdma-ar-overlap

Conversation

@yicyang

@yicyang yicyang commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

[AMD][Qwen3.5] TP2 prefill: overlap the layer all-reduces with compute over SDMA (on by default)

Based on #117 (Qwen3.5 35B TP2 launcher and CI). Qwen3.5-35B-A3B TP2 prefill spends about 28% of its GPU time in the two all-reduces per layer: for 32k-token batches RCCL moves 128 MB per all-reduce at the xGMI limit (~2.45 ms each), with the compute units idle. This PR applies the Qwen3.8 SP + SDMA overlap idea (#89) to Qwen3.5 and moves those all-reduces under the GEMM / MoE.

How to enable

On by default in evaluation/launch_qwen3.5-35B-fp8_tp2_disable_prefix_cache.sh (it now exports the mori runtime environment). No new environment variable; Qwen3.5 shares the Qwen4 SDMA switches:

Variable Default Qwen3.5 meaning
SGLANG_QWEN4_SP_SDMA_RS 1 0 keeps the all-reduce
SGLANG_QWEN4_SEQ_PARALLEL_MIN_TOKENS unset threshold when set; unset = 8192 for Qwen3.5 (Qwen3.8 keeps 2048)
SGLANG_QWEN4_SP_SDMA_RS_CHUNKS unset fixed row chunks when set; unset = 2 below 16384 tokens, 4 from 16384
  • Covers eager TP2 EXTEND batches of qwen3_5_moe_text models of at least 8192 tokens. Decode, CUDA-graph batches, speculative decoding (MTP / DFlash), LoRA, non-bf16 activations, TP != 2, DP attention and EP keep the original path.
  • Needs mori (already in evaluation/Dockerfile.ci); without it the server logs SP SDMA overlap disabled (...) and keeps the all-reduce.
  • Check the server log for Qwen3.5 TP2 prefill: all-reduces over SDMA in 2 / 4 ... row chunks.
  • bf16 only: no FP8 communication; outputs are the same all-reduce sums.

The launcher also selects the FlyDSL GDN decode (--linear-attn-decode-backend aiter, #111), separate commit.

Design

At TP2 an all-reduce is "push the whole partial to the peer, then add" - the same bytes per rank as reduce-scatter + all-gather - so Qwen3.5 does not need the Qwen3.8 sequence-parallel residual. It reuses the #89 SDMA machinery (SdmaAllGather row-range writer, mori put_ptr + engine-side completion signals, one-wave wait kernel):

  • out_proj / o_proj run per row chunk and push each partial chunk as soon as it is computed.

  • The post-attention norm and the MoE consume the all-reduce chunk by chunk, so chunk i+1's copy runs under chunk i's MoE; the MoE output is pushed the same way and consumed by the next layer's input norm.

  • Each landed chunk is summed as rank 0 rows + rank 1 rows on both ranks, so the replicas stay bit-identical; only the first chunk's wait and the last MoE chunk's copy stay exposed (figure).

  • Chunk count follows the batch. Each chunk re-runs the MoE (gate, topk, sorting, quant, two GEMMs), the projection quant + GEMM and the add + norm: about 7 ms of host time per layer at 4 chunks whatever the batch size, so small prefills would be host-bound (single 4k-token request: 132 -> 338 ms at 4 chunks, GPU idle 329 of 434 ms). Measured single-request prefill latency (ms):

    tokens all-reduce ([CI] Use TP2 for Qwen3.5 35B target-only evaluation #117) 1 chunk 2 chunks 4 chunks this PR (rule)
    4096 132 143 199 338 145 (all-reduce)
    8000 223 233 223 342 222 (all-reduce)
    12000 337 362 298 352 298 (2)
    16000 464 483 399 386 401 (2)
    24000 726 740 657 598 595 (4)
    32000 1016 1041 924 842 832 (4)
  • Double buffering: a rank pushes all-reduce n only after it received all of n-1, which its peer posts after it finished reading n-2 (same slot).

what this PR does

one full-attention layer, 32k tokens (TP2 trace, rank 0) before (#117) this PR
layer time (o_proj to the next GDN core) 16.08 ms 11.59 ms (-28%)
all-reduce on the critical path 5.86 ms (2 x RCCL) 0.43 ms (SDMA waits)

Results

MI308X, TP2, evaluation/run_qwen35_pure_text_bench.sh with its defaults (random 8000 in / 500 out, 128 prompts, 10 warmup, seed 42) at c8 / c16 / c32; radix cache off, no MTP, fresh server per configuration. Base = #117 as is; "+ GDN decode" = this PR with SGLANG_QWEN4_SP_SDMA_RS=0. Every number is the mean of two rounds (GPUs 2,3 and 6,7 swapped between rounds; round-to-round QPS spread at most 3.0% base, 0.5% GDN, 1.7% this PR).

QPS gain

concurrency base req/s + GDN decode + GDN decode + SDMA (this PR) TTFT base -> PR (ms) TPOT base -> PR (ms)
c8 1.453 1.538 (+5.9%) 1.638 (+12.7%) 1233 -> 1149 (-6.8%) 8.56 -> 7.48 (-12.6%)
c16 2.081 2.217 (+6.5%) 2.394 (+15.0%) 2105 -> 1875 (-11.0%) 11.17 -> 9.62 (-13.9%)
c32 2.687 2.795 (+4.0%) 3.155 (+17.4%) 3759 -> 3186 (-15.2%) 16.31 -> 13.92 (-14.6%)
  • The GDN decode alone leaves TTFT unchanged (1227 / 2098 / 3772 ms) and lowers TPOT; the TTFT gain is the SDMA all-reduce on the large prefill batches the concurrent requests form.
  • With a fixed 4 chunks the c8 TTFT gain was near zero (small prefills host-bound, see Design); the batch-size rule fixes that.

Accuracy

  • GSM8K (chat, thinking off), 200 examples: 0.955 with this PR (0.960 before the chunk-count rule; CI gate 0.970 within the 3% relative drop). Full 1319 on the prototype with every prefill forced through SDMA (SGLANG_QWEN4_SEQ_PARALLEL_MIN_TOKENS=1): 0.9500 vs 0.9507 base.
  • Prompt logprobs of long prompts: the SDMA path differs from base by the same amount base differs from itself across runs (the base is not run-to-run deterministic); the two ranks' all-reduce results are bit-identical (bench_qwen35_sdma_all_reduce.py, max rel. error 3.6e-3 vs an fp32 reference).

Qwen3.8 is unchanged

The Qwen3.5 path is gated on model_type == "qwen3_5_moe_text" and lives in Qwen3_5ForCausalLM.forward, which Qwen4ExpModel overrides; the shared environment variables keep their Qwen3.8 meaning. Qwen3.8-Flash-Next TP2 (MTP off, launch_qwen38_flash_next_fp8_mi308x_pure_tp_4_or_8_or_2.sh), two rounds each:

base (#117) this PR
prefill-only, 16 x 12k tokens (tok/s) 23952 / 24602, 23962 / 24691 23066 / 24626, 23974 / 24581
c8, 12000 in / 350 out (req/s) 1.06, 1.05 1.05, 1.05
TTFT / TPOT (ms) 2671 / 14.06, 2690 / 14.16 2677 / 14.05, 2672 / 14.06
server log SP SDMA on, no Qwen3.5 SDMA line same

Tests and CI

  • scripts/ci/qwen4_ci.py: Qwen3.5-35B-TP2-Base fails when the server log lacks the FlyDSL GDN decode or, after the performance phase, the SDMA all-reduce line, so a missing mori cannot pass silently. scripts/ci/test_qwen4_ci.py covers both and the accuracy-only exemption (80 passed).
  • benchmark/qwen4_seq_parallel/bench_qwen35_sdma_all_reduce.py: two-GPU correctness (bit-identical ranks, odd row counts, delayed producer) and a layer stand-in timing; README and the evaluation guide document the Qwen3.5 path.

Trace

Before (out_proj)
image

Before(MOE)
image

After(out_proj)
image

After(MOE)
image

@yicyang yicyang changed the title Qwen35 tp2 sdma ar overlap [AMD][Qwen3.5] TP2 prefill: overlap the layer all-reduces with compute over SDMA (on by default) Nov 27, 2026
@yicyang
yicyang marked this pull request as ready for review November 30, 2026 03:28
@yicyang
yicyang requested a balanced review from Copilot November 30, 2026 03:28

Copilot AI 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.

🟡 Changes recommended

CUDA-graph replay can read stale rows, and chunked dynamic FP8 projections change activation scaling, compromising computational correctness.

3 open issues
What changed in this PR

This PR reuses the Qwen4/mori SDMA infrastructure to overlap Qwen3.5 MoE TP2 prefill communication with projection and MoE computation on AMD GPUs.

Changes:

  • Adds a chunked bf16 all-reduce pipeline with batch-size-based chunk counts and fallback checks.
  • Updates the Qwen3.5 35B launcher and CI case to TP2, enabling mori and FlyDSL GDN decode.
  • Adds correctness and timing benchmarks, optimized-path CI log checks, tests, and documentation.
File Description
scripts/​ci/​test_qwen4_ci.py Tests workload selection and required optimized-path logs.
scripts/​ci/​qwen4_ci.py Updates the TP2 workload and verifies optimized kernels through server logs.
python/​sglang/​srt/​models/​qwen3_5.py Wraps output projections and dispatches eligible prefills to SDMA.
python/​sglang/​srt/​models/​qwen3_5_sdma_all_reduce.py Implements eligibility checks, chunked reductions, and pipelined layer execution.
python/​sglang/​srt/​layers/​sdma_mori.py Generalizes fallback warning and docstring wording to cover RCCL and aiter collectives.
python/​sglang/​srt/​environ.py Documents Qwen3.5 meanings of existing switches; defaults are unchanged.
evaluation/​Qwen4_Evaluation_Guide.md Updates TP2 launch instructions, workloads, and CI requirements.
evaluation/​launch_qwen3.5-35B-fp8_tp2_disable_prefix_cache.sh Configures TP2, the mori environment, and FlyDSL GDN decode.
benchmark/​qwen4_seq_parallel/​README.md Documents the pipeline, tuning controls, and reported performance.
benchmark/​qwen4_seq_parallel/​bench_qwen35_sdma_all_reduce.py Adds two-GPU correctness checks and layer-like timing comparisons.

Review effort: Balanced

… over SDMA

Qwen3.5-35B-A3B TP2 prefill spends ~28% of its GPU time in the two all-reduces
per layer (RCCL at the xGMI limit for 32k-token batches). Apply the Qwen4-Exp
SDMA overlap to Qwen3.5's plain residual stream: at TP2 an all-reduce is
"push the partial to the peer, then add", so the residual stays replicated.

- out_proj / o_proj run in row chunks; each chunk's partial is pushed to the
  peer over SDMA (mori, SdmaAllGather writer) as soon as it is computed.
- Per landed chunk: rank 0 rows + rank 1 rows (bit-identical on both ranks),
  residual add + RMSNorm, then the MoE on that chunk with its all-reduce
  skipped; its output is pushed the same way and consumed by the next layer's
  input norm chunk by chunk.

Eager TP2 EXTEND batches of Qwen3.5 MoE (qwen3_5_moe_text) only; everything
else, including Qwen4-Exp / Qwen3.8, keeps its path. Shares the Qwen4 switches
(no new environment variable): SGLANG_QWEN4_SP_SDMA_RS (default on),
SGLANG_QWEN4_SEQ_PARALLEL_MIN_TOKENS (2048) and SGLANG_QWEN4_SP_SDMA_RS_CHUNKS
(Qwen3.5 default 4). Without mori the all-reduce runs, with a warning.

The TP2 launcher sets the mori runtime environment.

Signed-off-by: Ethan Yang <[email protected]>
Select the gfx942 FlyDSL fused GDN decode kernel (qkvz/ba unpack + conv1d
update + recurrence in one launch) with --linear-attn-decode-backend aiter.
Qwen3.5 shares Qwen3_5GatedDeltaNet with Qwen3.8; at TP2 it runs 8 k-heads /
16 v-heads per rank (ratio 2), which the kernel's (H, HV) JIT covers.

Signed-off-by: Ethan Yang <[email protected]>
…ch and docs

- qwen4_ci.py: Qwen3.5-35B-TP2-Base fails when its server log lacks the
  FlyDSL GDN decode or, after the performance phase, the SDMA all-reduce
  line, so a missing mori cannot pass silently. Accuracy-only runs, which
  may stay below the 2048-token threshold, only require the decode line.
- test_qwen4_ci.py: cover the check and give the sgl-project#117 workload test a
  server log with both lines.
- benchmark/qwen4_seq_parallel: two-GPU correctness and layer timing for
  the Qwen3.5 SDMA all-reduce; README section with the serving results.
- Evaluation guide: the 35B TP2 launcher's SDMA / mori requirement and the
  new CI check.

Signed-off-by: Ethan Yang <[email protected]>
Each row chunk re-runs the MoE (gate, topk, sorting, quant, two GEMMs), the
projection quant + GEMM and the add + norm, about 7 ms of host time per layer
at 4 chunks regardless of the batch size. Small prefills were host-bound:
single 4k-token requests went from 132 ms to 338 ms (GPU idle 329 of 434 ms),
which cancelled the large-batch gain in the 8k-token CI workload's TTFT.

Single-request prefill latency on MI308X TP2 (ms):

  tokens   all-reduce  1 chunk  2 chunks  4 chunks
  4096         132       143      199       338
  8000         225       233      223       342
  12000        337       362      298       352
  16000        460       483      399       386
  24000        724       740      657       598
  32000       1021      1041      924       842

- Batches under 8192 tokens keep the all-reduce; 2 chunks up to 16383
  tokens, 4 from 16384.
- SGLANG_QWEN4_SEQ_PARALLEL_MIN_TOKENS and SGLANG_QWEN4_SP_SDMA_RS_CHUNKS
  still override, but only when set: their defaults are Qwen3.8's.
- CI: smoke runs send single 8k prompts, now under the threshold, so the
  SDMA log line is only required after the full performance phase.

Signed-off-by: Ethan Yang <[email protected]>
- Keep the all-reduce with speculative decoding (MTP drafts got accept
  length 1.0 on this path) and with LoRA (its wrappers replace o_proj /
  out_proj and bypass the chunked push, which raised on the first large
  prefill); also check at init that every projection is still wrapped.
- Size the receive buffers to min(max prefill, chunked prefill) tokens and
  keep the all-reduce for larger batches; log the buffer size.
- Require bf16 activations and a TP-partial MoE output.
- Agree on SDMA availability across ranks before the group collective.
- Disable the path after an exception mid-forward.
- Reject SGLANG_QWEN4_SP_SDMA_RS_CHUNKS < 1.
- Trim comments; README notes the excluded cases; the shared mori warning
  no longer names the aiter reduce-scatter.

Signed-off-by: Ethan Yang <[email protected]>
@yicyang
yicyang force-pushed the qwen35-tp2-sdma-ar-overlap branch from 6caf861 to 81bcc64 Compare December 1, 2026 05:56
@sammysun0711
sammysun0711 merged commit 248bf97 into apinge:qwen4_v0.5.21 Dec 2, 2026
38 of 49 checks passed
@yicyang

yicyang commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator Author
image

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.

4 participants