Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
9e6c392
add agv
sidsingh-nvidia Apr 9, 2026
d49c124
add allgatherv
sidsingh-nvidia Apr 10, 2026
5617c93
add reducescatterv
sidsingh-nvidia Apr 10, 2026
5fc4a2d
test dispatch combine loop
sidsingh-nvidia Apr 10, 2026
164f24d
make test more robust
sidsingh-nvidia Apr 10, 2026
dc2ff5f
merge
sidsingh-nvidia Apr 10, 2026
15f3bfe
refactor, remove a2a dispatcher in inference
sidsingh-nvidia Apr 10, 2026
83a1393
Merge branch 'main' into siddharth/all-gather-v-dispatcher
sidsingh-nvidia Apr 10, 2026
b1ca2d1
latest
sidsingh-nvidia Apr 13, 2026
c7e1ce7
refactor inference MoE token dispatcher and fix NCCL prefill path
sidsingh-nvidia Apr 14, 2026
52755c5
fix NCCL dispatcher for variable token counts and class scoping bug
sidsingh-nvidia Apr 14, 2026
00c3916
more refactoring
sidsingh-nvidia Apr 14, 2026
4e5c725
make flashinfer work
sidsingh-nvidia Apr 14, 2026
25b4f59
fix transformer config
sidsingh-nvidia Apr 14, 2026
e8e2309
simplify dummy forward pass
sidsingh-nvidia Apr 15, 2026
f813ff0
fused allgatherv
sidsingh-nvidia Apr 15, 2026
92f73cf
remove design file
sidsingh-nvidia Apr 15, 2026
ac4fd07
remove files
sidsingh-nvidia Apr 15, 2026
05911a6
remove test file
sidsingh-nvidia Apr 15, 2026
6e152e6
minor
sidsingh-nvidia Apr 15, 2026
38b9bd8
fix docstring
sidsingh-nvidia Apr 15, 2026
094b16f
simplify context
sidsingh-nvidia Apr 15, 2026
5255163
Merge branch 'main' into siddharth/all-gather-v-dispatcher
sidsingh-nvidia Apr 15, 2026
9e70e01
cleanup
sidsingh-nvidia Apr 15, 2026
798d013
more cleanup
sidsingh-nvidia Apr 15, 2026
be07644
add comment
sidsingh-nvidia Apr 15, 2026
c262f25
fix for MTP
sidsingh-nvidia Apr 16, 2026
43ee48d
remove dead argument
sidsingh-nvidia Apr 16, 2026
90a4121
optimize permute kernel for mxfp8
sidsingh-nvidia Apr 16, 2026
4e374e0
minor
sidsingh-nvidia Apr 17, 2026
6e0dfde
Merge branch 'main' into siddharth/all-gather-v-dispatcher
sidsingh-nvidia Apr 17, 2026
62a77f5
write a fused metadata update kernel
sidsingh-nvidia Apr 17, 2026
d0cd5f6
barrier
sidsingh-nvidia Apr 17, 2026
5dc5595
minor
sidsingh-nvidia Apr 17, 2026
f892e42
minor
sidsingh-nvidia Apr 20, 2026
f7bb215
Merge branch 'main' into siddharth/all-gather-v-dispatcher
sidsingh-nvidia Apr 22, 2026
f7aa0a0
remove the is_spec_decode arg in hybrid model.py
sidsingh-nvidia Apr 22, 2026
ef598fa
format
sidsingh-nvidia Apr 22, 2026
594b02f
refactor
sidsingh-nvidia Apr 22, 2026
50bb72b
fix unit tests
sidsingh-nvidia Apr 22, 2026
8ccce72
format
sidsingh-nvidia Apr 22, 2026
651efde
Merge branch 'main' into siddharth/all-gather-v-dispatcher
sidsingh-nvidia Apr 22, 2026
ecba67b
fix unit tests
sidsingh-nvidia Apr 22, 2026
6a8949b
subsume metadata update into the forward pass
sidsingh-nvidia Apr 23, 2026
2059a66
Merge branch 'main' into siddharth/all-gather-v-dispatcher
sidsingh-nvidia Apr 24, 2026
6b77a1d
Add vLLM fused grouped GeMM kernelg
santhnm2 Apr 27, 2026
1abccc9
Merge with main
santhnm2 Apr 27, 2026
66c4d6a
Merge vllm_grouped_gemm into all-gather-v-dispatcher-merged
santhnm2 Apr 27, 2026
e6b388e
Make vLLM grouped gemm CUDA-graph compatible with AllGatherV dispatcher
santhnm2 Apr 27, 2026
c0cf858
Fix _apply_activation: use torch.relu which supports out=
santhnm2 Apr 27, 2026
457c29c
perf optimizations
santhnm2 Apr 27, 2026
464fb2d
Fuse activation into gemm kernel
santhnm2 Apr 27, 2026
32a8aa8
Autotune kernels
santhnm2 Apr 27, 2026
f3bc6fc
Remove torch.zeros
santhnm2 Apr 27, 2026
077aced
More autotuning configs, moe_sum kernel
santhnm2 Apr 27, 2026
e74a6d1
Autotune BLOCK_SIZE_M
santhnm2 Apr 27, 2026
c3aa23d
Misc perf optimizations
santhnm2 Apr 28, 2026
13cd543
Fix
santhnm2 Apr 28, 2026
75364f0
One more optimization
santhnm2 Apr 28, 2026
10fe49a
More tuning
santhnm2 Apr 28, 2026
b551b96
Simplify
santhnm2 Apr 28, 2026
f74cfc5
Remove D2D op
santhnm2 Apr 28, 2026
91da592
Optimize preprocessing kernels
santhnm2 Apr 28, 2026
1671cb1
More kernel opt
santhnm2 Apr 28, 2026
3c8402b
Kernel opt
santhnm2 Apr 28, 2026
aef93d8
Kernel opt
santhnm2 Apr 28, 2026
30177a7
FC1 opt
santhnm2 Apr 28, 2026
90bfc7c
More optimizations
santhnm2 Apr 28, 2026
9a84b08
Remove autotuning configs
santhnm2 Apr 28, 2026
448fa9e
Remove unsafe configs
santhnm2 Apr 28, 2026
8973c0b
Fix accuracy issue
santhnm2 Apr 28, 2026
a2048fc
Removed grid_em
santhnm2 Apr 28, 2026
424025e
Fix B load mask
santhnm2 Apr 28, 2026
cc343e6
Kernel opt
santhnm2 Apr 28, 2026
3e1cd5c
Kernel opt
santhnm2 Apr 28, 2026
30ca341
Try persistent SMs
santhnm2 Apr 28, 2026
b94bb41
Optimize _compute_local_tokens_kernel
santhnm2 Apr 28, 2026
76a6401
Try to optimize _count_local_tokens_kernel again
santhnm2 Apr 28, 2026
8c1a75b
Avoid unnecessary cast to bf16
santhnm2 Apr 29, 2026
02dd791
Merge with main
santhnm2 Apr 30, 2026
8ab9126
Add unit tests
santhnm2 Apr 30, 2026
b00086d
Linting
santhnm2 Apr 30, 2026
5e9d187
Reduce triton cache overhead
santhnm2 Apr 30, 2026
00fa19c
Merge remote-tracking branch 'origin/vllm-grouped-gemm-cuda-graphable…
santhnm2 Apr 30, 2026
b5d5354
Version guard test
santhnm2 Apr 30, 2026
6ea1a91
Address reviewer comments
santhnm2 May 1, 2026
3be7604
Fix histogram
santhnm2 May 1, 2026
bb605d3
Remove histogram
santhnm2 May 1, 2026
2846aec
Add TODO comment
santhnm2 May 1, 2026
3dc4d0e
Merge remote-tracking branch 'upstream/main' into vllm-grouped-gemm-c…
santhnm2 May 1, 2026
017f668
Fix return dtype
santhnm2 May 1, 2026
1b00626
Fix reference dtype
santhnm2 May 1, 2026
aec1aa0
More dtype fixes
santhnm2 May 1, 2026
b9b9e46
More dtype fixes
santhnm2 May 1, 2026
c49de0c
Linting
santhnm2 May 1, 2026
f04ad51
Fix typo
santhnm2 May 1, 2026
4a034a8
Merge branch 'main' into vllm-grouped-gemm-cuda-graphable
santhnm2 May 1, 2026
e7840c9
fp32 rsv buffer
santhnm2 May 1, 2026
e5225d5
Merge with main
santhnm2 May 3, 2026
f3f2aca
Fix rsv dtype
santhnm2 May 4, 2026
a883044
Restore original _count_local_tokens_kernel implementation
santhnm2 May 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions megatron/core/inference/moe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import enum

from .fused_moe import ActivationType, mcore_fused_moe
from .vllm_fused_moe import vllm_fused_moe


class InferenceGroupedGemmBackend(enum.Enum):
Expand All @@ -14,3 +15,4 @@ class InferenceGroupedGemmBackend(enum.Enum):

FLASHINFER = "flashinfer"
TORCH = "torch"
VLLM = "vllm"
81 changes: 71 additions & 10 deletions megatron/core/inference/moe/permute.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Unpermute expert outputs back to original token order
"""

from typing import Optional
from unittest.mock import MagicMock

import torch
Expand All @@ -28,6 +29,16 @@
tl = MagicMock()


_NUM_SMS: Optional[int] = None


def _get_num_sms(device: torch.device) -> int:
global _NUM_SMS
if _NUM_SMS is None:
_NUM_SMS = torch.cuda.get_device_properties(device).multi_processor_count
return _NUM_SMS


def _ceil_div(a, b):
return (a + b - 1) // b

Expand Down Expand Up @@ -57,17 +68,53 @@ def _count_local_tokens_kernel(
offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = offsets < valid_pairs
expert_ids = tl.load(routing_map_ptr + offsets, mask=mask, other=-1)
# Map global expert IDs to local indices; non-local experts become negative
local_ids = expert_ids - local_expert_start
is_local = (local_ids >= 0) & (local_ids < num_local_experts) & mask
tl.atomic_add(tokens_per_expert_ptr + local_ids, 1, mask=is_local)


@triton.jit
def _count_local_tokens_kernel_persistent(
routing_map_ptr, # [max_tokens, topk] flattened expert assignments
tokens_per_expert_ptr, # [num_local_experts] output counters (zeroed by caller)
valid_tokens_ptr, # scalar int32 CUDA tensor: number of valid tokens this iteration
topk, # number of expert choices per token
local_expert_start, # first global expert index owned by this rank
num_local_experts: tl.constexpr, # number of experts on this rank
num_sms, # number of SMs (grid size for persistent kernel)
BLOCK_SIZE: tl.constexpr, # number of pairs processed per iteration
):
"""Count tokens routed to local experts using a persistent grid.

Launches num_sms CTAs. Each CTA loops over its share of BLOCK_SIZE-sized
chunks, with total work determined device-side from valid_tokens.
"""
pid = tl.program_id(0)
valid_tokens = tl.load(valid_tokens_ptr)
valid_pairs = valid_tokens * topk

total_blocks = tl.cdiv(valid_pairs, BLOCK_SIZE)
blocks_per_cta = tl.cdiv(total_blocks, num_sms)
block_start = pid * blocks_per_cta

if block_start < total_blocks:
block_end = tl.minimum(block_start + blocks_per_cta, total_blocks)

for block_id in tl.range(block_start, block_end):
offsets = block_id * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = offsets < valid_pairs
expert_ids = tl.load(routing_map_ptr + offsets, mask=mask, other=-1)
local_ids = expert_ids - local_expert_start
is_local = (local_ids >= 0) & (local_ids < num_local_experts) & mask
tl.atomic_add(tokens_per_expert_ptr + local_ids, 1, mask=is_local)


def compute_local_tokens_per_expert(
routing_map: torch.Tensor,
local_expert_start: int,
num_local_experts: int,
valid_tokens: torch.Tensor,
persistent: bool = False,
) -> torch.Tensor:
"""Count tokens routed to each local expert.

Expand All @@ -78,20 +125,34 @@ def compute_local_tokens_per_expert(
num_local_experts: number of experts on this rank.
valid_tokens: scalar int32 CUDA tensor with the number of valid tokens
this iteration. Fixed address; value updated each step before graph replay.
persistent: use persistent-grid kernel variant (fewer CTAs, looped).
"""
max_pairs = routing_map.numel()
topk = routing_map.shape[1]
tokens_per_expert = torch.zeros(num_local_experts, dtype=torch.int32, device=routing_map.device)
BLOCK = 1024
_count_local_tokens_kernel[(_ceil_div(max_pairs, BLOCK),)](
routing_map,
tokens_per_expert,
valid_tokens,
topk,
local_expert_start,
num_local_experts,
BLOCK_SIZE=BLOCK,
)
if persistent:
num_sms = _get_num_sms(routing_map.device)
_count_local_tokens_kernel_persistent[(num_sms,)](
routing_map,
tokens_per_expert,
valid_tokens,
topk,
local_expert_start,
num_local_experts,
num_sms,
BLOCK_SIZE=BLOCK,
)
else:
_count_local_tokens_kernel[(_ceil_div(max_pairs, BLOCK),)](
routing_map,
tokens_per_expert,
valid_tokens,
topk,
local_expert_start,
num_local_experts,
BLOCK_SIZE=BLOCK,
)
return tokens_per_expert


Expand Down
Loading
Loading