-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Inference: Do not route pad/dummy tokens to any expert #4922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sidsingh-nvidia
merged 9 commits into
NVIDIA:main
from
sidsingh-nvidia:siddharth/ep-no-route-pad-tokens
Jul 14, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
02a8f52
no route dummy/pad tokens to experts
sidsingh-nvidia ed2d417
Address review: fix MTP token count masking, guard None dispatcher, a…
sidsingh-nvidia 1288d0b
Replace autotune with BLOCK_M heuristic to avoid recompilation
sidsingh-nvidia 7ae32ed
format
sidsingh-nvidia 978eeea
Merge branch 'main' into siddharth/ep-no-route-pad-tokens
sidsingh-nvidia d6578ea
Merge branch 'main' into siddharth/ep-no-route-pad-tokens
sidsingh-nvidia 7a24757
lint
sidsingh-nvidia fd6eca2
DCO Remediation Commit for Siddharth Singh <sidsingh@nvidia.com>
sidsingh-nvidia 026a750
Merge branch 'main' into siddharth/ep-no-route-pad-tokens
sidsingh-nvidia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
101 changes: 101 additions & 0 deletions
101
megatron/core/transformer/moe/inference_routing_mask_kernel.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
|
|
||
| """Triton kernel for masking CUDA-graph padding rows of a local routing map. | ||
|
|
||
| Under CUDA-graph capture the local token count is padded up to a captured | ||
| graph size; those padding rows have garbage routing indices and, if left | ||
| alone, would dispatch padding tokens to real experts. This kernel zeroes | ||
| that out by writing ``-1`` into every topk slot of rows in | ||
| ``[real_token_count, local_tokens)``. | ||
|
|
||
| The kernel reads ``real_token_count`` from a fixed-address ``int32[1]`` GPU | ||
| tensor, so it is safe to call from inside a captured graph: only the value | ||
| behind the pointer changes between replays. | ||
| """ | ||
|
|
||
| from torch import Tensor | ||
|
|
||
| try: | ||
| import triton | ||
| import triton.language as tl | ||
|
|
||
| HAVE_TRITON = True | ||
| except ImportError: | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from megatron.core.utils import null_decorator | ||
|
|
||
| triton = MagicMock() | ||
| triton.jit = null_decorator | ||
| triton.autotune = null_decorator | ||
| tl = MagicMock() | ||
| HAVE_TRITON = False | ||
|
|
||
|
|
||
| @triton.jit | ||
| def _mask_routing_padding_kernel( | ||
| routing_map_ptr, # int64* [total_rows, topk] | ||
| real_token_count_ptr, # int32* [1] | ||
| total_rows: tl.int32, | ||
| tp_rank: tl.int32, # SP/TP rank — local row r maps to global row r + tp_rank*total_rows | ||
| TOPK: tl.constexpr, # actual topk | ||
| BLOCK_M: tl.constexpr, # rows per program | ||
| BLOCK_TOPK: tl.constexpr, # next_power_of_2(TOPK), column block | ||
| ): | ||
| """Fill `routing_map[real_token_count:, :]` with -1, BLOCK_M rows per program.""" | ||
| pid = tl.program_id(0) | ||
| rows = pid * BLOCK_M + tl.arange(0, BLOCK_M) | ||
|
|
||
| real_count = tl.load(real_token_count_ptr).to(tl.int32) | ||
|
|
||
| # real_count is in the global (pre-SP-shard) frame; rows is local to this SP rank. | ||
| global_rows = rows + tp_rank * total_rows | ||
| row_mask = (global_rows >= real_count) & (rows < total_rows) | ||
|
|
||
| cols = tl.arange(0, BLOCK_TOPK) | ||
| col_mask = cols < TOPK | ||
|
|
||
| offs = rows[:, None].to(tl.int64) * TOPK + cols[None, :].to(tl.int64) | ||
| mask = row_mask[:, None] & col_mask[None, :] | ||
|
|
||
| neg_one = tl.full((BLOCK_M, BLOCK_TOPK), -1, dtype=tl.int64) | ||
| tl.store(routing_map_ptr + offs, neg_one, mask=mask) | ||
|
|
||
|
|
||
| def mask_routing_padding( | ||
| routing_map: Tensor, real_token_count_tensor: Tensor, tp_rank: int = 0 | ||
| ) -> None: | ||
| """In-place fill -1 into ``routing_map[real_token_count:, :]``. | ||
|
|
||
| Args: | ||
| routing_map: ``[N, topk]`` int64 local routing map. ``N`` is the | ||
| (possibly CUDA-graph-padded) local token count. | ||
| real_token_count_tensor: ``[1]`` int32 GPU tensor holding the real | ||
| (unpadded) token count for this step, in the global (pre-SP-shard) | ||
| frame. Read inside the kernel so the mask boundary moves correctly | ||
| across CUDA-graph replays. | ||
| tp_rank: This rank's index in the SP/TP group. Local row ``r`` is | ||
| row ``r + tp_rank * N`` in the global frame; the kernel uses this | ||
| offset to compare against ``real_token_count_tensor``. | ||
| """ | ||
| assert routing_map.is_cuda, "routing_map must be on CUDA" | ||
| assert routing_map.dim() == 2, f"expected 2D routing_map, got {routing_map.shape}" | ||
| assert routing_map.dtype.is_floating_point is False, "routing_map must be integer" | ||
|
|
||
| total_rows, topk = routing_map.shape | ||
| if total_rows == 0: | ||
| return | ||
|
|
||
| BLOCK_M = 8 if total_rows < 64 else 128 | ||
| BLOCK_TOPK = triton.next_power_of_2(topk) | ||
| grid = (triton.cdiv(total_rows, BLOCK_M),) | ||
|
|
||
| _mask_routing_padding_kernel[grid]( | ||
| routing_map, | ||
| real_token_count_tensor, | ||
| total_rows=total_rows, | ||
| tp_rank=tp_rank, | ||
| TOPK=topk, | ||
| BLOCK_M=BLOCK_M, | ||
| BLOCK_TOPK=BLOCK_TOPK, | ||
| ) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.