Skip to content

Fix MLA padding and grouped topk routing in the Transformers modelling backend - #49982

Merged
hmellor merged 6 commits into
vllm-project:mainfrom
hmellor:transformers-moe-routing
Jul 27, 2026
Merged

hmellor merged 6 commits into
vllm-project:mainfrom
hmellor:transformers-moe-routing

Conversation

@hmellor

@hmellor hmellor commented Jul 27, 2026 •

Copy link
Copy Markdown
Member
  • Anchor the router's scorer on the last topk in the gate's graph — DeepSeek's group-limited routers topk over expert groups first, so the scorer was sought upstream of the wrong node and the block was declined.
  • Allow e_score_correction_bias in the gate's state (DeepSeek-V3 noaux) and carry it onto the rebuilt gate, rather than rejecting any extra state.
  • Pass use_grouped_topk, num_expert_group, topk_group, scoring_func and e_score_correction_bias to FusedMoE. use_grouped_topk was hard-coded False, excluding every multi-group block from fusion.
  • Pass routed_scaling_factor, with apply_routed_scale_to_output off when aiter applies it internally. Shared-expert blocks skip the fp16 _maybe_apply_routed_scale_to_output path, which Transformers decoder layers don't compensate for.
  • Stop the shared experts' down projection reducing on its own — FusedMoE owns the reduction once the shared expert runs inside it.
  • Rebuild the gate as a GateLinear, matching the native MoE models and picking up the specialized router-GEMM tiers.
  • Route in the dtype the router computes in (GateLinear.out_dtype, FusedMoE.router_logits_dtype), both previously unset: moe_router_dtype wins, else read the cast off the gate's graph, scanning only the scorer's input cone.
  • Pad value up to the query/key head size in vllm_attention_forward and slice the output back. MLA models have a narrower value (128 vs 192 on DeepSeek-V2), which made them unrunnable here; with VLLM_MLA_DISABLE=1 they now run as full attention.
  • Warn when an fp16 model with routed_scaling_factor and shared experts is left unfused — that path divides the shared expert output and expects the decoder layer to compensate, which Transformers layers don't. Previously a silent fallback.
  • Move named_state to transformers/utils.py, no longer MoE-only.

hmellor and others added 6 commits July 27, 2026 11:52
`_match_router` anchored the scorer on the first `topk` in the gate's graph.
DeepSeek's group-limited routers run a `topk` over expert *groups* first, so
the scorer was looked for upstream of the wrong node and the whole block was
declined. Anchor on the last `topk` instead, which is always the routing one.

Also allow `e_score_correction_bias` alongside `weight` in the gate's state
(DeepSeek-V3 noaux) and carry it onto the rebuilt `ReplicatedLinear`, rather
than treating any extra state as unrecognised.

`named_state` moves to `transformers/utils.py` as it is no longer MoE-only.

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
`use_grouped_topk` was hard-coded `False` and `scoring_func`,
`e_score_correction_bias`, `num_expert_group` and `topk_group` were left
unset, so blocks with more than one expert group were excluded from fusion
entirely. Infer them from the text config and pass them through.

`routed_scaling_factor` is passed too, with `apply_routed_scale_to_output`
off when aiter's fused MoE applies it internally. The fp16
`_maybe_apply_routed_scale_to_output` path is skipped for blocks with shared
experts, as Transformers decoder layers do not compensate for the division.

Shared experts' down projection no longer reduces on its own: FusedMoE owns
the reduction once the shared expert runs inside it.

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
`GateLinear` is what the native MoE models use for the router GEMM, and it
falls back to `ReplicatedLinear` when no specialized kernel applies, so the
fused block picks up the DSV3/cuteDSL/cuBLAS tiers where dimensions allow
without changing behaviour elsewhere.

`out_dtype` is left unset, matching the native DeepSeek default.

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
MLA models have a narrower `value` than `query`/`key` (128 vs 192 for
DeepSeek-V2). Running one through full attention fed the KV cache a `value`
of the wrong head size, so DeepSeek could not run under the Transformers
backend at all. Pad `value` up to the query/key head size and slice the
result back afterwards.

This is the naive path: with `VLLM_MLA_DISABLE=1` an MLA model runs as full
attention, which is what makes DeepSeek usable as the exemplar for grouped
top-k routing.

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
`GateLinear`'s `out_dtype` and `FusedMoE`'s `router_logits_dtype` were both
left unset, so a model that routes in fp32 was routed in the activation dtype
instead. GLM-4.5 is the live case: its native vLLM implementation hardcodes
`router_logits_dtype=torch.float32`, and the Transformers backend fuses that
block once the router matcher accepts a score-correction bias.

Prefer `moe_router_dtype` from the config. Where no config field names it,
read it off the gate's traced graph: routers that need higher precision say so
in their forward (`hidden_states.type(torch.float32)`), so the dtype is
recoverable without a per-architecture table. Only the cone feeding the scorer
is considered, so the cast back to the activation dtype after the top-k is not
mistaken for the routing dtype.

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
The fp16 overflow path applies `routed_scaling_factor` by dividing the shared
expert output instead of scaling the fused output, and expects the decoder
layer to compensate. Transformers decoder layers do not, so such blocks fall
back to routing in Transformers -- silently, until now.

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>

@claude claude 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.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@hmellor
hmellor enabled auto-merge (squash) July 27, 2026 12:28
@github-actions github-actions Bot added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 27, 2026
@hmellor
hmellor merged commit 99de48e into vllm-project:main Jul 27, 2026
8 checks passed
@hmellor
hmellor deleted the transformers-moe-routing branch July 27, 2026 18:32
michael-gpu pushed a commit to practical-inference/vllm-ampere-extended that referenced this pull request Oct 20, 2026
…g backend (vllm-project#49982)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready ONLY add when PR is ready to merge/full CI is needed

Projects

Development

Successfully merging this pull request may close these issues.

2 participants