vllm.models.deepseek_v41.nvidia.specialized_decode.moe.routed_expert_gemm
¶
Low-batch routed-expert GEMMs for DeepSeek-V4.1 decode (1-8 rows).
At concurrency 1 with DSpark, every target MoE layer applies its routed experts
to six rows (384 experts, top-6; at TP4 every rank holds all experts at
intermediate 576, padded to 640). TrtLlmMxfp4ExpertsModular runs FlashInfer's
TRT-LLM-gen routing kernel and two batched MXFP8 x MXFP4 GEMMs; at this size they
stream the weights of about 22 experts per layer, so their cost is HBM time plus
fixed per-kernel latency. This module computes the same values from the same
tensors with CuTe DSL kernels sized for at most eight rows::
router logits ─► front ───────────────────────┐ (top-k, MXFP8 input, routing)
top-k ids, MXFP8 input ─► route ──────────────┤
▼
GEMM1 ─► h (MXFP8) ─► GEMM2 ─► unweighted permuted rows
│
UnfinalizedMoEOutput (fused all-reduce + mHC) ◄────┘
routed_expert_route_cute.pybuilds the expert-major, 8-row-tile permutation and the per-tile record both GEMMs read, as FlashInfer's routing kernel does with precomputed ids and an 8-token tile.routed_expert_gemm_cute.pyholds GEMM1 and GEMM2: persistent 128 x 8tcgen05block-scaled tiles over the (weight tile, token tile) work items of the record, with evict-first hints on the expert weights. GEMM1 skips the zero padding tile of the 576-wide slice. Their numerics are those of the TRT-LLM-gent128x8tactics.routed_expert_front_cute.pyreplaces the DSv4 top-k (Triton), the MXFP8 quantization of the routed input and the route kernel with one launch whose outputs are bitwise those of the three; it is built for the target's 384 experts and top-6.
The kernels read the tensors vLLM prepares for the TRT-LLM-gen MXFP4 backend:
w13 [E, 1280, 2560] u8 (interleaved up/gate rows shuffled for 128-row
epilogue tiles) with [E, 1280, 160] scales in the 128x4 layout, w2
[E, 5120, 320] u8 with [E, 5120, 20] scales, and the per-expert SwiGLU clamp.
Each kernel is compiled once per device when the layers are bound (shapes,
expert counts and the routing record are launch arguments) and launched on the
current stream with programmatic dependent launch, so CUDA graphs capture
them like any other kernel.
Classes:
-
FrontConfig–The router's DSv4 top-k parameters the fused front applies.
-
RoutedExpertGemm–The route kernel (or fused front) and GEMM pair bound to one MoE
-
Routing–What GEMM1 and GEMM2 read, from
routeorfront.
Functions:
-
build_routed_expert_gemms–A
RoutedExpertGemmfor each MoE runner that supports it (None for the -
check_weights–Raise
ValueErrorunless the weights have the layout the kernels read -
cta_words–ctasrounded up to a multiple of four (the record's table width). -
front_applies–Whether the fused front may replace the router's top-k this call: the
-
max_ctas–The most 8-row token tiles any routing of
rows * top_kslots needs. -
route_words–int32 length of the routing record for
ctastoken tiles.
FrontConfig
dataclass
¶
The router's DSv4 top-k parameters the fused front applies.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
RoutedExpertGemm
¶
The route kernel (or fused front) and GEMM pair bound to one MoE layer's TRT-LLM-gen MXFP4 experts (see the module docstring).
Methods:
-
accepts–Whether a call with router input
xcan take these kernels: 1-8 -
front–Top-k, MXFP8 quantization and routing record in one launch.
-
gemm1–Launch GEMM1; returns the intermediate H and its block scales (one
-
gemm2–Launch GEMM2 on GEMM1's outputs.
-
gemms–Launch GEMM1 and GEMM2 right after the kernel that built
routing. -
route–Quantize
xas the modular kernel does and launch the route kernel -
set_front–Use the fused front with the router's
config.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
accepts(x, runner)
¶
Whether a call with router input x can take these kernels: 1-8
BF16 rows, and the runner's expert weights still the tensors bound here
(a weight reload replaces them).
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
front(x, logits, input_ids)
¶
Top-k, MXFP8 quantization and routing record in one launch.
x: BF16 router input [rows, 5120]; logits: FP32 router logits
[rows, 384]; input_ids: [rows] int32 or int64, read only with image
routing.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
gemm1(routing)
¶
Launch GEMM1; returns the intermediate H and its block scales (one 128-row scale atom per token tile, rows 0-7 used).
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
gemm2(routing, h_values, h_scales)
¶
Launch GEMM2 on GEMM1's outputs.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
gemms(routing)
¶
Launch GEMM1 and GEMM2 right after the kernel that built routing.
route(x, topk_ids, topk_weights)
¶
Quantize x as the modular kernel does and launch the route kernel
on the router's top-k (topk_weights FP32).
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
set_front(config)
¶
Use the fused front with the router's config.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
Routing
dataclass
¶
What GEMM1 and GEMM2 read, from route or front.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
_fake(dtype, align)
¶
A placeholder pointer for compilation.
_front_config(runner)
¶
The fused front's configuration for the runner's router, or why its routing is not the plain DSv4 top-k the front computes.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
_front_kernel(device_index, image)
cached
¶
RoutedExpertFront compiled for a device, with or without image
routing.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
_gemm1_kernel(device_index)
cached
¶
RoutedExpertGemm1 compiled for a device.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
_gemm2_kernel(device_index)
cached
¶
RoutedExpertGemm2 compiled for a device.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
_pointer(dtype, tensor, align)
¶
A CuTe DSL global-memory pointer to tensor's data.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
_route_kernel(device_index)
cached
¶
RoutedExpertRoute compiled for a device.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
_sm_count(device_index)
cached
¶
Streaming multiprocessors of a device: the persistent GEMMs' CTAs.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
_unsupported_reason(runner)
¶
Why the MoE runner's routed experts cannot take the GEMM pair.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
build_routed_expert_gemms(runners)
¶
A RoutedExpertGemm for each MoE runner that supports it (None for the
others), with the fused front where the router allows it. Needs the MoE
kernels that the loader's per-layer quantization finalize builds.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
check_weights(w13, w13_scale, w2, w2_scale, clamp, top_k)
¶
Raise ValueError unless the weights have the layout the kernels read
and the routing fits the route kernel.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
cta_words(ctas)
¶
ctas rounded up to a multiple of four (the record's table width).
front_applies(gemm, runner)
¶
Whether the fused front may replace the router's top-k this call: the
router's biases are the tensors the front reads, and no routed-expert
capture hook (which only select_experts feeds) is set.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
max_ctas(rows, top_k, experts)
¶
The most 8-row token tiles any routing of rows * top_k slots needs.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/moe/routed_expert_gemm.py
route_words(ctas)
¶
int32 length of the routing record for ctas token tiles.