Repository navigation
[PyTorch] Ensure contiguous inputs in fused_topk_with_score_function forward - #4036
Open
wanglei875 wants to merge 3 commits into
Open
wanglei875 wants to merge 3 commits into
wanglei875 wants to merge 3 commits into
Conversation
…forward Signed-off-by: wanglei875 <[email protected]>
Contributor
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge, with no outstanding findings. SummaryMakes read-only router inputs contiguous before CUDA calls in eager and compiled execution.
Reviews (3) · Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." · Reviewed by Greptile |
…tests Signed-off-by: wanglei875 <[email protected]>
for more information, see https://pre-commit.ci
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Ensure read-only input tensors (
logits,expert_bias, andbin_bounds) are contiguous before invokingtex.fused_topk_with_score_function_fwdandtex.fused_topk_with_score_function_qb_fwdintransformer_engine/pytorch/router.py, matching the existing.contiguous()guards on gradients in the backward paths.Currently,
FusedTopkScoreFunction.forward(and the QB /torch.compilecustom-op forward wrappers) passlogitsandexpert_biasdirectly to the fused CUDA kernels assuming contiguous memory layout. Passing a non-contiguous view—such as slicing active expertslogits = full_logits[:, :num_experts]from a padded projection or a stridedexpert_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 andctx.mark_dirty(...)remain intact.Type of change
Changes
logits,expert_bias, andbin_boundsvia.contiguous()inFusedTopkScoreFunction.forward,FusedTopkScoreFunctionQB.forward, and the correspondingtorch.compilecustom-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.test_topk_non_contiguous_inputsintests/pytorch/test_fused_router.pyverifying forward outputs (probs,routing_map) and backward gradients for non-contiguouslogitsandexpert_biasmatch contiguous inputs.Checklist: