Skip to content

[Bugfix][MoE] Respect padded row strides in top-k routing - #57823

Open
Id545 wants to merge 5 commits into
vllm-project:mainfrom
Id545:fix/moe-topk-padded-row-stride
Open

Id545 wants to merge 5 commits into
vllm-project:mainfrom
Id545:fix/moe-topk-padded-row-stride

Conversation

@Id545

@Id545 Id545 commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

Purpose

Fixes #56900.

Inductor can produce router logits with padded physical rows, for example shape
[M, 60] and stride (64, 1). The native MoE top-k path used
blockIdx.x * num_experts to locate each input row, so it read the wrong logits
whenever the physical row stride differed from num_experts. This silently
changed expert routing under compilation.

This change:

  • propagates gating_output.stride(0) to the native top-k launcher;
  • uses the existing unfused softmax/sigmoid plus top-k path for padded rows;
  • allocates the required workspace for padded power-of-two expert counts;
  • keeps packed rows on the existing specialized path; and
  • adds regression coverage for softmax and sigmoid with 60/64 and 64/72
    logical/physical row widths.

The input-row offset is int64_t because PyTorch tensor strides are 64-bit;
this also avoids 32-bit wraparound in the input address calculation. Open PR
#53131 independently widens
packed-row pointer arithmetic more broadly, but it does not propagate
stride(0) or handle padded rows. The two changes overlap only on the
input-offset width; this PR addresses the separate functional stride bug.

No open PR referencing #56900 or implementing padded-row support for this
kernel was found before submission.

Test Plan

uvx pre-commit run --files \
  csrc/libtorch_stable/moe/topk_softmax_kernels.cu \
  tests/kernels/moe/test_fused_topk.py

.venv/bin/python -m pytest -q \
  tests/kernels/moe/test_fused_topk.py \
  -k padded_row_stride

EXPECTED_VLLM="$(.venv/bin/python -c 'import vllm; print(vllm.__version__)')"
.venv/bin/python \
  /workspace/fp-atomics-census/probes/diagnostics/moe_compile/moe_compile_reproducer.py \
  --output-dir /workspace/results-56900/patched-e2e \
  --runners v2 \
  --compiles on off \
  --graphs off \
  --expected-vllm "$EXPECTED_VLLM"

The same focused cases were also run with a standalone kernel reproducer before
and after rebuilding vLLM from source. The end-to-end command uses the
issue reproducer.
Each end-to-end arm ran in a fresh process with separate vLLM, Inductor, and
Triton cache roots.

Test Result

Check Baseline Patched
Kernel reproducer failed as expected; minimum 3/33 matching rows passed; 33/33 in all four cases
Focused padded-stride pytest not applicable 4 passed
Compiled duplicate-prompt pairs 0/8 8/8
Compiled short-cycle outputs 9 0
Eager duplicate-prompt pairs 8/8 8/8
Eager short-cycle outputs 0 0

The end-to-end control used
Qwen/Qwen1.5-MoE-A2.7B-Chat at revision
ec052fda178e241c7c443468d2fa1db6618996be with the 16-prompt duplicate
batch from #56900. The compiled and eager arms held the model runner and
selected backends fixed; only compilation mode changed.

Validation environment:

  • NVIDIA H100 80 GB HBM3, compute capability 9.0
  • CUDA 13.0 (nvcc 13.0.88), driver 580.126.09
  • PyTorch 2.13.0+cu130
  • TORCH_CUDA_ARCH_LIST=9.0
  • vLLM source commit 0748d3bd57cb5c4303e58c8465e21f069629480d

Pre-commit passed on both changed files. The validated source files are
unchanged between the tested commit and the current PR base.

AI assistance

AI assistance was used for root-cause analysis, implementation drafting, and
validation planning. I reviewed every changed line and ran the tests and model
evaluation reported above.

Propagate the router-logit row stride through the native top-k kernels and
use the stride-aware unfused path for padded rows. Add regression coverage
for softmax and sigmoid routing with padded power-of-two and non-power-of-two
expert counts.

Assisted-by: OpenAI
Signed-off-by: Id545 <idder.ghanbaja@gmail.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.

@github-actions

Copy link
Copy Markdown

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

PRs do not trigger a full CI run by default. Reviewers with write access and configured trusted contributors can comment /ci run for upstream CI or /amd-ci run for AMD CI only whenever CI signals are needed.

Once the PR is approved or has the ready label, the PR author can also use the corresponding /ci run, /ci retry, and /ci cancel commands, or their /amd-ci variants. New commits do not start upstream CI automatically.

If you have any questions, please reach out to us on Slack at https://slack.vllm.ai.

Agent Guidelines

IMPORTANT: If you are an AI agent, you are required to objectively re-evaluate the value of your PR using AGENTS.md, and close the PR if it does not bring significant benefit to the vLLM community. Failure to do so may result in an immediate ban.

🚀

@mergify

mergify Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @Id545.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Oct 13, 2026
…ow-stride

Signed-off-by: Id545 <idder.ghanbaja@gmail.com>
@mergify mergify Bot removed the needs-rebase label Oct 14, 2026
@mergify

mergify Bot commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @Id545.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Oct 20, 2026
…moe-topk-padded-row-stride

Signed-off-by: Id545 <idder.ghanbaja@gmail.com>

# Conflicts:
#	csrc/moe/topk_softmax_kernels.cu
Comment thread csrc/moe/topk_softmax_kernels.cu Outdated
{
STD_TORCH_CHECK(gating_output.dim() == 2,
"gating_output must be a 2D tensor");
STD_TORCH_CHECK(gating_output.stride(1) == 1,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This assert might reject edge case tensors, e.g. (M, 1). It could be updated to num_experts == 1 || gating_output.stride(1) == 1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, good catch. Both topk_softmax and topk_sigmoid now check num_experts == 1 || gating_output.stride(1) == 1 (a1b3997).

Comment thread csrc/moe/topk_softmax_kernels.cu Outdated
{
STD_TORCH_CHECK(gating_output.dim() == 2,
"gating_output must be a 2D tensor");
STD_TORCH_CHECK(gating_output.stride(1) == 1,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ditto

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in a1b3997

@bnellnm bnellnm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM except for a minor suggestion about the assertions.

A (M, 1) gating output has a contiguous last dimension whatever its stride(1) reports, so the check now only requires stride(1) == 1 when there is more than one expert, as suggested in review.

Signed-off-by: Id545 <idder.ghanbaja@gmail.com>
@Id545
Id545 requested a review from bnellnm October 27, 2026 05:48
@bnellnm bnellnm added the ready ONLY add when PR is ready to merge/full CI is needed label Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

✅ @Id545, CI is now available for this PR.

  • /ci run starts upstream CI; /amd-ci run starts AMD CI only.
  • Run commands test your branch with every commit currently on its upstream target branch. If it is behind, the bot merges the target branch into it first (pull before pushing again). Append --allow-stale to a run command to test an outdated branch as is, at your own risk.
  • /ci retry retries failed jobs in the CI build for the current PR head. If the current head has no CI build, it starts a new CI build for the current head containing only jobs that failed in the latest earlier CI build for this PR.
  • /amd-ci retry retries failed jobs in AMD CI for the current PR head. Use /amd-ci run when the current head has no AMD CI build.
  • /ci cancel cancels scheduled or running CI builds for this PR branch; /amd-ci cancel does the same for AMD CI only.

@Id545

Id545 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

/ci run

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

✅ Triggered Buildkite CI #100455 for commit 359e42a7149f.

ℹ️ This PR is 1343 commits behind upstream main. Merged upstream main into this branch as 359e42a7149f before starting CI. Pull the branch before pushing more changes.

@Id545

Id545 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

/ci retry

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

✅ Queued 1 failed job(s) for retry in Buildkite CI #100455.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready ONLY add when PR is ready to merge/full CI is needed

Projects

None yet

3 participants