You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The aiter function signatures above are approximate — the exact API may differ slightly in v0.5.9-rocm700-mi35x. Consult import aiter; help(aiter) inside the container for exact signatures.
AllReduce (22.7%) is the single biggest bottleneck and requires 4 GPUs. It's a TP communication kernel — the main lever is reducing the number of allreduce calls (e.g., fusing more operations) or optimizing the aiter custom allreduce implementation.
MoE MXFP4 GEMMs (34.5%) are the compute-bound kernels. These use CK (Composable Kernel) MXFP4 fused MoE GEMM which is specific to MI355X gfx950.
The profile captures both prefill (batch=992) and decode (small batch) iterations in the same trace, which is why you see both large and small GEMM tile configurations.
@claude cross_device_reduce_2stage isnt torch.dist.allreduce. deep explore the sglang codebase on what function is the python function that calls cross_device_reduce_2stage
Tracing cross_device_reduce_2stage to its Python entry point
Search SGLang codebase for cross_device_reduce_2stage references
Trace the call chain from Python → C++/HIP kernel
Identify the Python function that triggers this kernel
Summarize findings
You're right — cross_device_reduce_2stage is nottorch.distributed.all_reduce. It's a custom P2P allreduce kernel in SGLang's sgl-kernel C++ extension, bypassing NCCL/RCCL entirely. Here's the full trace:
In the dsr1-fp4-mi355x-sglang profile (SGLang v0.5.9-rocm700-mi35x on MI355X), SGLANG_USE_AITER_AR defaults to true, so the cross_device_reduce_2stage kernel you see is likely coming from Aiter's CustomAllreduce (imported from aiter.dist.device_communicators.custom_all_reduce), not SGLang's own implementation. Both share the same kernel name because Aiter forked the kernel from SGLang/vLLM, but the Python entry point routes through Aiter's class.
Kernel Details
The kernel itself (custom_all_reduce_hip.cuh) implements a two-stage P2P allreduce that bypasses RCCL:
Stage 1 (reduce-scatter): Each GPU reads peer data via P2P and reduces its assigned partition
Stage 2 (all-gather): Each GPU broadcasts its reduced partition to all peers
This is faster than RCCL for small-to-medium tensors (like the 992×7168 bf16 tensors in your profile) because it avoids RCCL's ring/tree protocol overhead.
Updated Microbenchmark Script
To properly benchmark cross_device_reduce_2stage, you should benchmark through SGLang's CustomAllreduce class, nottorch.distributed.all_reduce:
AllReduce Tensor Shapes from cross_device_reduce_2stage
Explore SGLang's custom allreduce code to find exact instrumentation points
Look at the profile workflow to understand how to inject logging
Create injection script to log tensor shapes entering custom allreduce
Modify benchmark script + profile workflow to support AR_SHAPE_LOGGING=1
Push changes and trigger profile run with AR_SHAPE_LOGGING=1
Collect and report shapes from the run
Profile run:#24010590515 — dsr1-fp4-mi355x-sglang, TP=4, conc=16 Instrumentation: Patched GroupCoordinator.all_reduce() in parallel_state.py inside the container to log tensor.shape on rank 0
Results: AllReduce Tensor Shapes (7,504 total calls on rank 0)
All tensors are torch.bfloat16 with dim[1] = 7168 (DeepSeek-R1 hidden_size)
Calls
Shape
Bytes
Notes
370
[1, 7168]
14,336
Single-token decode (batch=1)
369
[2, 7168]
28,672
Decode batch=2
369
[4, 7168]
57,344
Decode batch=4
369
[7, 7168]
100,352
—
369
[8, 7168]
114,688
Decode batch=8
369
[12, 7168]
172,032
—
369
[16, 7168]
229,376
Decode batch=16
369
[24, 7168]
344,064
—
369
[32, 7168]
458,752
—
369
[40, 7168]
573,440
—
369
[48, 7168]
688,128
—
369
[56, 7168]
802,816
—
369
[64, 7168]
917,504
—
369
[72, 7168]
1,032,192
—
369
[80, 7168]
1,146,880
—
369
[88, 7168]
1,261,568
—
369
[96, 7168]
1,376,256
—
369
[104, 7168]
1,490,944
—
369
[112, 7168]
1,605,632
—
369
[120, 7168]
1,720,320
—
369
[128, 7168]
1,835,008
Max CUDA graph batch size
123
[7, 7168]
100,352
Only shape with fewer calls (appears during warmup/ramp)
Key Observations
Every allreduce tensor is [M, 7168] bf16 — always the full hidden dimension (7168), with M being the batch size on that TP shard
21 distinct batch sizes: 1, 2, 4, 7, 8, 12, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128 — these are the CUDA graph capture batch sizes (stride of 8 from 8-128, plus small sizes 1,2,4,7,12)
369 calls per batch size = exactly one allreduce per transformer layer per forward pass during the benchmark (DeepSeek-R1 has ~61 layers, and 369/61 ≈ 6 forward passes per batch size during the profiled window)
Max tensor size: 1.83MB ([128, 7168] bf16) — well within the custom allreduce's small-tensor P2P optimization sweet spot
No prefill-sized tensors — the profiled window only captured decode iterations (no [992, 7168] shapes). The previous profile trace showed batch=992, but that was from a different capture window.
Updated Microbenchmark for AllReduce
Based on the actual observed shapes, here's the corrected benchmark:
Weight shapes are constant across all batch sizes — w1=(257, 1024, 3584) and w2=(257, 7168, 256) in float4_e2m1fn_x2 (MXFP4). These are pre-shuffled for the CK BPreshuffle kernel variant.
Batch sizes match allreduce shapes exactly — same 21 CUDA graph capture sizes (1, 2, 4, 7, 8, 12, 16, 24, 32, ..., 128). The MoE GEMM and allreduce are tightly coupled in the forward pass.
topk=9 includes 1 shared expert (expert 256) — the shared expert is fused into the same fused_moe call via _fused_append_shared_experts_kernel.
No prefill shapes captured — the profile window only saw decode. For prefill, expect hidden=(N, 7168) where N can be up to chunked_prefill_size=196608 tokens, though the actual batch is bounded by cuda_graph_max_bs=128 for CUDA graph'd paths.
Uses SGLang's FusedMoE layer which auto-dispatches to the correct backend per platform — no vendor-specific imports needed. Falls back to pure torch.mm for decomposed benchmarks.
Usage:
# === B200 (NVIDIA) ===
docker run --rm -it --gpus all --shm-size=64g lmsysorg/sglang:v0.5.9-cu130 bash
# Single GPU — all kernels except allreduce
python3 bench_deepseek_moe.py --tp 4 --batch-sizes 1,4,8,16,32,64,128
# Multi-GPU — allreduce only
torchrun --nproc_per_node=4 bench_deepseek_moe.py --allreduce --batch-sizes 1,4,8,16,32,64,128
# === MI355X (AMD) ===
docker run --rm -it --device=/dev/kfd --device=/dev/dri --group-add video \
--shm-size=64g lmsysorg/sglang:v0.5.9-rocm700-mi35x bash
# Single GPU — all kernels except allreduce
python3 bench_deepseek_moe.py --tp 4 --batch-sizes 1,4,8,16,32,64,128
# Multi-GPU — allreduce only
torchrun --nproc_per_node=4 bench_deepseek_moe.py --allreduce --batch-sizes 1,4,8,16,32,64,128
This issue has had no activity (commits, comments, reviews or other updates) for more than 30 days, so it is being closed automatically. Reopen it if it is still needed.
Activity
Klaud-Cold commentedon Apr 5, 2026
Claude finished @functionstackx's task in 7m 59s —— View job
Profile Run Analysis:
dsr1-fp4-mi355x-sglangProfile Config:
dsr1-fp4-mi355x-sglang| Run: #23997779285Model:
amd/DeepSeek-R1-0528-MXFP4-Preview| Image:lmsysorg/sglang:v0.5.9-rocm700-mi35xTP=4, EP=1, ISL=1024, OSL=1024, Conc=16 | Batch at profile time: 992 tokens
Perfetto Trace Link
Kernel Breakdown (Total GPU kernel time: 341.5ms)
cross_device_reduce_2stage<bf16, 4>ck::moe_mxgemm(gate_up, M256xN128xK128,MulABScaleExpertWeightShuffled)ck::moe_mxgemm(down_proj, M256xN128xK128,MulABScaleShuffled)_fused_dynamic_mxfp4_quant_moe_sort_kernelhipBLASLt MT128x256x64(likelyq_projorkv_a_proj: 992×7168→2112)hipBLASLt MT64x160x128(likelyq_a_proj: 992×7168→1536)ck::moe_mxgemm(small tile M64xN32xK128,MulABScaleShuffled)elementwise_kernel(bf16→f32 copy)add_rmsnorm_quant_kernel<bf16, 256, 32>(fused add+norm+quant)mla_pfl_qh192_vh128_m32x8_n128x1_causal1hipBLASLt MT192x128x64(likelyo_proj: 992×2048→7168)ck::moe_mxgemm(small tile M256xN32xK128,MulABScaleExpertWeightShuffled)hipBLASLt MT256x128x32(likelygate_proj: 992×7168→2048)hipBLASLt MT32x32x256(decode-phase dense MoE shared expert)float8_copy_kernel(bf16→fp8 quant)elementwise_kernel(copy variant)kn_mla_reduce_v1_ps<128,32,64>(prefill reduce)hipBLASLt MT32x16x256hipBLASLt MT16x16x512Memcpy DtoDbfloat16_copy_kernelmla_a8w8_qh16_qseqlen1_gqaratio16_ps(FP8 MLA decode)MoeSortingKernel(CK tile topk=2)hipBLASLt MT32x16x128add_rmsnorm_quant_kernel<bf16, 256, 8>MoeSortingMultiPhaseKernel_P23add_rmsnorm_quant_kernel<bf16, 64, 8>grouped_topk_opt_sort_kernel<bf16>kn_entry_2c_sbhd_cached_indirect_inplace_fused_append_shared_experts_kernelgemm_afp4wfp4_kernelM=256,N=256,K=256 (shared expert MXFP4)kn_mla_reduce_v1<512,16,1>(decode reduce)MoeSortingMultiPhaseKernel_P0_v2batched_gemm_a16wfp4M=4,N=32,K=128 (MoE decode small-batch)set_mla_kv_buffer_kernelbatched_gemm_a16wfp4M=8,N=32,K=512gemm_a16_w16_atomic_kernelM=16,N=16,K=256 (small shared expert)fused_qk_rope_cat_and_cache_mla_kernelSummary by Category
kernel_moe_mxgemm_2lds. Gate_up + down_proj dominate.Microbenchmark Reproduction Scripts
All scripts are designed to run inside the SGLang container (
lmsysorg/sglang:v0.5.9-rocm700-mi35x) on MI355X hardware. Launch with:1. AllReduce (
cross_device_reduce_2stage) — 22.7%This kernel requires multi-GPU P2P setup (aiter custom allreduce). Benchmark via
torch.distributed:2. MoE MXFP4 GEMM (
ck::kernel_moe_mxgemm_2lds) — 34.5%3. Dense GEMMs — Attention Projections (
hipBLASLt Cijk) — 16.3%4. MoE Routing + Sort — 7.1%
5. RMSNorm + Quantization (
add_rmsnorm_quant_kernel) — 3.5%6. MLA Attention (Prefill + Decode) — 4.5%
7. Dtype Copies (bf16↔fp32, bf16→fp8) — 6.7%
8. RoPE (
kn_entry_2c_sbhd_cached_indirect_inplace) — 0.5%Notes
aiterfunction signatures above are approximate — the exact API may differ slightly inv0.5.9-rocm700-mi35x. Consultimport aiter; help(aiter)inside the container for exact signatures.functionstackx commentedon Apr 5, 2026
@claude cross_device_reduce_2stage isnt torch.dist.allreduce. deep explore the sglang codebase on what function is the python function that calls cross_device_reduce_2stage
Klaud-Cold commentedon Apr 5, 2026
Claude finished @functionstackx's task in 2m 15s —— View job
Tracing
cross_device_reduce_2stageto its Python entry pointcross_device_reduce_2stagereferencesYou're right —
cross_device_reduce_2stageis nottorch.distributed.all_reduce. It's a custom P2P allreduce kernel in SGLang'ssgl-kernelC++ extension, bypassing NCCL/RCCL entirely. Here's the full trace:Complete Call Chain
Key Files
sglang/srt/distributed/communication_op.pytensor_model_parallel_all_reduce()sglang/srt/distributed/parallel_state.py:529-627GroupCoordinator.all_reduce()— decides custom vs NCCLsglang/srt/distributed/device_communicators/custom_all_reduce.pyCustomAllreduce.all_reduce_unreg()/all_reduce_reg()custom_all_reduce.py:453-505dispatch_custom_allreduce()— picks SGLang vs Aiter implcustom_all_reduce_ops.py:71-128sgl_kernel.allreduceC extensionsgl-kernel/csrc/allreduce/custom_all_reduce.cuall_reduce_unreg()C++ functionsgl-kernel/csrc/allreduce/custom_all_reduce_hip.cuhcross_device_reduce_2stage<T, ngpus>kernelHow SGLang Decides to Use This Kernel
The routing logic in
GroupCoordinator.all_reduce()(parallel_state.py:529):SGLANG_USE_1STAGE_ALLREDUCE=1→cross_device_reduce_1stage(deterministic, fixed ordering)SGLANG_USE_AITER_AR=true(default) + aiter available → Aiter'sCustomAllreduce(separate implementation)CustomAllreduce→cross_device_reduce_2stagetorch.distributed.all_reduce(NCCL/RCCL)The
dispatch_custom_allreduce()function atcustom_all_reduce.py:453determines whichCustomAllreduceclass to use:Important Note for Your Profile
In the
dsr1-fp4-mi355x-sglangprofile (SGLang v0.5.9-rocm700-mi35x on MI355X),SGLANG_USE_AITER_ARdefaults totrue, so thecross_device_reduce_2stagekernel you see is likely coming from Aiter'sCustomAllreduce(imported fromaiter.dist.device_communicators.custom_all_reduce), not SGLang's own implementation. Both share the same kernel name because Aiter forked the kernel from SGLang/vLLM, but the Python entry point routes through Aiter's class.Kernel Details
The kernel itself (
custom_all_reduce_hip.cuh) implements a two-stage P2P allreduce that bypasses RCCL:This is faster than RCCL for small-to-medium tensors (like the 992×7168 bf16 tensors in your profile) because it avoids RCCL's ring/tree protocol overhead.
Updated Microbenchmark Script
To properly benchmark
cross_device_reduce_2stage, you should benchmark through SGLang'sCustomAllreduceclass, nottorch.distributed.all_reduce:functionstackx commentedon Apr 5, 2026
@claude re-run it and use logger or print() on the tensors .shape going into the cross_device custom all_reduce to collect the shapes
Klaud-Cold commentedon Apr 5, 2026
Claude finished @functionstackx's task in 21m 19s —— View job •
claude/issue-1005-20260405-2103• [Create PR ➔](https://github.com/SemiAnalysisAI/InferenceX/compare/main...claude/issue-1005-20260405-2103?quick_pull=1&title=Issue%20%231005%3A%20Changes%20from%20Claude&body=This%20PR%20addresses%20issue%20%231005%0A%0AGenerated%20with%20%5BClaude%20Code%5D(https%3A%2F%2Fclaude.ai%2Fcode)AllReduce Tensor Shapes from
cross_device_reduce_2stageAR_SHAPE_LOGGING=1AR_SHAPE_LOGGING=1Profile run: #24010590515 —
dsr1-fp4-mi355x-sglang, TP=4, conc=16Instrumentation: Patched
GroupCoordinator.all_reduce()inparallel_state.pyinside the container to logtensor.shapeon rank 0Results: AllReduce Tensor Shapes (7,504 total calls on rank 0)
All tensors are
torch.bfloat16with dim[1] = 7168 (DeepSeek-R1 hidden_size)[1, 7168][2, 7168][4, 7168][7, 7168][8, 7168][12, 7168][16, 7168][24, 7168][32, 7168][40, 7168][48, 7168][56, 7168][64, 7168][72, 7168][80, 7168][88, 7168][96, 7168][104, 7168][112, 7168][120, 7168][128, 7168][7, 7168]Key Observations
[M, 7168]bf16 — always the full hidden dimension (7168), with M being the batch size on that TP shard[128, 7168]bf16) — well within the custom allreduce's small-tensor P2P optimization sweet spot[992, 7168]shapes). The previous profile trace showed batch=992, but that was from a different capture window.Updated Microbenchmark for AllReduce
Based on the actual observed shapes, here's the corrected benchmark:
| Branch
Add allreduce tensor shape logging for profiling
functionstackx commentedon Apr 5, 2026
@claude re-run and collect shapes for sglang fused_moe too and the reprod script and the call trace
Klaud-Cold commentedon Apr 5, 2026
Claude finished @functionstackx's task in 38m 25s —— View job •
claude/issue-1005-20260405-2314• [Create PR ➔](https://github.com/SemiAnalysisAI/InferenceX/compare/main...claude/issue-1005-20260405-2314?quick_pull=1&title=Issue%20%231005%3A%20Changes%20from%20Claude&body=This%20PR%20addresses%20issue%20%231005%0A%0AGenerated%20with%20%5BClaude%20Code%5D(https%3A%2F%2Fclaude.ai%2Fcode)fused_moeShapes, Call Trace & Microbenchmark Scriptsck::kernel_moe_mxgemm_2ldsaiter.fused_moe.fused_moe)Complete Call Chain:
fused_moe→ck::kernel_moe_mxgemm_2ldsKey Files
sglang/srt/models/deepseek_v2.pyDeepseekV2MoE.forward_normal()sglang/srt/layers/moe/fused_moe_triton/layer.pyFusedMoE.forward_impl()→run_moe_core()sglang/srt/layers/quantization/mxfp4.pyMxfp4MoEMethod.apply()aiter/fused_moe.pyfused_moe()→fused_moe_()→fused_moe_2stages()aiter/ops/moe_op.pyck_moe_stage1(),ck_moe_stage2()csrc/ck_gemm_moe_2stages_codegen/gemm_moe_ck2stages.cuCK gridwise_moe_mx_gemm_bpreshuffle.hppkernel_moe_mxgemm_2lds<>()Routing Logic (aiter)
In
fused_moe_(), the routing decision for MXFP4 on gfx950:get_2stage_cfgs()selects kernel based on(M, bs, hidden, intermediate, num_experts, topk, activation, dtype_a, dtype_w, quant_type, is_shuffled)(256, 128, 7168, 512, 257, 9, 'ActivationType.Silu', 'torch.bfloat16', 'torch.float4_e2m1fn_x2', 'torch.float4_e2m1fn_x2', 'QuantType.per_1x32', True, False)Observed Tensor Shapes (from Run #24013049086)
All shapes logged at
aiter.fused_moe.fused_moe()entry on rank 0:hidden_states[M, 7168]torch.bfloat16w1(gate_up)[257, 1024, 3584]torch.float4_e2m1fn_x2w2(down_proj)[257, 7168, 256]torch.float4_e2m1fn_x2topk_weights[M, 9]torch.float32topk_ids[M, 9]torch.int32Unpacked dimensions (actual GEMM sizes):
w1gate_up:[257, 1024, 7168]— each expert:[1024, 7168](SwiGLU: 512 gate + 512 up × 7168)w2down:[257, 7168, 512]— each expert:[7168, 512]21 unique batch sizes observed (CUDA graph capture sizes):
[1, 7168][1, 9][2, 7168][2, 9][4, 7168][4, 9][7, 7168][7, 9][8, 7168][8, 9][12, 7168][12, 9][16, 7168][16, 9][24, 7168][24, 9][32, 7168][32, 9][40, 7168][40, 9][48, 7168][48, 9][56, 7168][56, 9][64, 7168][64, 9][72, 7168][72, 9][80, 7168][80, 9][88, 7168][88, 9][96, 7168][96, 9][104, 7168][104, 9][112, 7168][112, 9][120, 7168][120, 9][128, 7168][128, 9]No prefill-sized shapes were observed (the profiled window captured CUDA graph warmup + decode only, not chunked prefill).
Microbenchmark Reproduction Script
Run inside the SGLang container (
lmsysorg/sglang:v0.5.9-rocm700-mi35x) on MI355X:fused_moe End-to-End (aiter)
Individual Stage Benchmarks (CK kernels directly)
MoE Routing (biased_grouped_topk)
Key Observations
Weight shapes are constant across all batch sizes —
w1=(257, 1024, 3584)andw2=(257, 7168, 256)infloat4_e2m1fn_x2(MXFP4). These are pre-shuffled for the CKBPreshufflekernel variant.Effective GEMM sizes per expert per token:
[1, 7168] × [1024, 7168].T→[1, 1024]then SwiGLU →[1, 512][1, 512] × [7168, 512].T→[1, 7168]Batch sizes match allreduce shapes exactly — same 21 CUDA graph capture sizes (1, 2, 4, 7, 8, 12, 16, 24, 32, ..., 128). The MoE GEMM and allreduce are tightly coupled in the forward pass.
topk=9 includes 1 shared expert (expert 256) — the shared expert is fused into the same
fused_moecall via_fused_append_shared_experts_kernel.No prefill shapes captured — the profile window only saw decode. For prefill, expect
hidden=(N, 7168)where N can be up tochunked_prefill_size=196608tokens, though the actual batch is bounded bycuda_graph_max_bs=128for CUDA graph'd paths.| Branch | Profile runs: #24012791193, #24013049086
functionstackx commentedon Apr 5, 2026
@claude re-run it make the reprod non-vendor based by doing DeepseekV2MoE.
do it on b200 and mi355 too and send reprod script
Klaud-Cold commentedon Apr 5, 2026
Claude finished @functionstackx's task in 15m 45s —— View job •
claude/issue-1005-20260405-2356Non-Vendor
DeepseekV2MoEReprod Scripts + B200 & MI355X ProfilesDeepseekV2MoEto understand the vendor-neutral fused_moe pathdsr1-fp4-b200-sglang) — Run #24013363593 ✅dsr1-fp4-mi355x-sglang) — Run #24013362308 ✅FusedMoE/DeepseekV2MoEProfile Results
dsr1-fp4-b200-sglangdsr1-fp4-mi355x-sglangKey config differences:
nvidia/DeepSeek-R1-0528-FP4-V2imagesglang:v0.5.9-cu130,--quantization modelopt_fp4,--moe-runner-backend flashinfer_trtllm,--attention-backend trtllm_mla, EP=4amd/DeepSeek-R1-0528-MXFP4-Previewimagesglang:v0.5.9-rocm700-mi35x,--attention-backend aiter, MXFP4 via CK, EP=1Call Chain:
DeepseekV2MoE→ Kernel (Vendor-Neutral)Vendor-Neutral Microbenchmark Script
Pushed to
benchmarks/microbench/bench_deepseek_moe.pyUses SGLang's
FusedMoElayer which auto-dispatches to the correct backend per platform — no vendor-specific imports needed. Falls back to puretorch.mmfor decomposed benchmarks.Usage:
What it benchmarks (7 categories):
SGLang FusedMoE.forward()torch.mmtorch.mmSGLang RMSNorm/ PyTorchtensor.to()torch.distributed/ custom ARSGLang select_experts/torch.topkDeepSeek-R1 model constants hardcoded:
hidden_size=7168,n_routed_experts=256,n_shared_experts=2,moe_intermediate_size=2048topk=8,topk_group=4,n_group=8,routed_scaling_factor=2.5q_head_dim=192,kv_lora_rank=512,v_head_dim=128,qk_rope_dim=64Shapes benchmarked (from previous profiling):
[M, 7168]bf16 for each batch sizehidden=[M, 7168],w1=[258, 1024, 3584],w2=[258, 7168, 256](MXFP4 packed)Create PR | Branch | View job
Add vendor-neutral DeepSeek-R1 MoE microbenchmark script
[-]kernels[/-][+]kernels / 内核[/+][-]kernels / 内核[/-][+]kernels / 内核分析[/+]github-actions commentedon Nov 9, 2026
This issue has had no activity (commits, comments, reviews or other updates) for more than 30 days, so it is being closed automatically. Reopen it if it is still needed.