Skip to content

[PyTorch] Ensure contiguous inputs in fused_topk_with_score_function forward - #4036

Open
wanglei875 wants to merge 3 commits into
NVIDIA:mainfrom
wanglei875:fix/router-contiguous-inputs
Open

wanglei875 wants to merge 3 commits into
NVIDIA:mainfrom
wanglei875:fix/router-contiguous-inputs

Conversation

@wanglei875

Copy link
Copy Markdown

Description

Ensure read-only input tensors (logits, expert_bias, and bin_bounds) are contiguous before invoking tex.fused_topk_with_score_function_fwd and tex.fused_topk_with_score_function_qb_fwd in transformer_engine/pytorch/router.py, matching the existing .contiguous() guards on gradients in the backward paths.

Currently, FusedTopkScoreFunction.forward (and the QB / torch.compile custom-op forward wrappers) pass logits and expert_bias directly to the fused CUDA kernels assuming contiguous memory layout. Passing a non-contiguous view—such as slicing active experts logits = full_logits[:, :num_experts] from a padded projection or a strided expert_bias—causes the kernel to read linearly across stride gaps into excluded elements, silently corrupting MoE routing maps and gating probabilities.

Note: In-place output buffers (topk_indices, histogram) are intentionally left untouched so in-place mutation semantics and ctx.mark_dirty(...) remain intact.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • Convert non-contiguous logits, expert_bias, and bin_bounds via .contiguous() in FusedTopkScoreFunction.forward, FusedTopkScoreFunctionQB.forward, and the corresponding torch.compile custom-op forward wrappers (_topk_map_forward, _topk_indices_forward, _qb_map_forward, _qb_indices_forward). Calling .contiguous() is a zero-copy no-op when inputs are already contiguous.
  • Add regression test test_topk_non_contiguous_inputs in tests/pytorch/test_fused_router.py verifying forward outputs (probs, routing_map) and backward gradients for non-contiguous logits and expert_bias match contiguous inputs.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@wanglei875
wanglei875 requested a review from ksivaman as a code owner December 5, 2026 07:44
Copilot AI balanced review requested due to automatic review settings December 5, 2026 07:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge, with no outstanding findings.

Summary

Makes read-only router inputs contiguous before CUDA calls in eager and compiled execution.

  • Standard top-k routing reads strided inputs in their intended order.
  • QB top-k routing reads strided inputs in their intended order.

Reviews (3) · Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." · Reviewed by Greptile

Comment thread transformer_engine/pytorch/router.py Resolved
Comment thread tests/pytorch/test_fused_router.py Resolved

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants