vllm.model_executor.layers.fused_moe.utils
¶
Functions:
-
count_expert_num_tokens–Count the number to tokens assigned to each expert.
-
fi_moe_largest_bucket–Estimate FlashInfer's MoE autotuning maximum token count.
-
masked_amax_scale–Per-tensor activation quant scale that skips rows without a route.
-
moe_use_td_hw_supported–Whether the current device can run the TD (gather) path of
-
remap_topk_to_local–Map global expert ids to local ids; non-local or invalid routes become -1.
-
resolve_layer_fused_shared_expert–Resolve whether AITER fused shared-expert execution is enabled.
-
resolve_moe_use_td–Tri-state resolver for
VLLM_TRITON_USE_TD. -
warn_if_moe_use_td_ineffective–One-shot warning when
VLLM_TRITON_USE_TDis set but ignored.
_fp8_quantize(A, A_scale, per_act_token, block_shape=None, is_scale_transposed=False)
¶
Perform fp8 quantization on the inputs. If a block_shape is provided, the output will be blocked.
Source code in vllm/model_executor/layers/fused_moe/utils.py
_int8_quantize(A, A_scale, per_act_token, block_shape=None)
¶
Perform int8 quantization on the inputs. If a block_shape is provided, the output will be blocked.
Source code in vllm/model_executor/layers/fused_moe/utils.py
_resize_cache(x, v)
¶
Shrink the given tensor and apply the given view to it. This is used to resize the intermediate fused_moe caches.
Source code in vllm/model_executor/layers/fused_moe/utils.py
count_expert_num_tokens(topk_ids, num_local_experts, expert_map)
¶
Count the number to tokens assigned to each expert.
Parameters:
-
(topk_ids¶Tensor) –Tensor mapping each token to its list of experts.
-
(num_local_experts¶int) –Number of experts in this rank.
-
(expert_map¶Optional[Tensor]) –A tensor mapping expert indices from the global expert space to the local expert space of the expert parallel shard.
Returns: A tensor of size num_local_experts, where tensor[i] holds the number of tokens assigned to the ith expert.
Source code in vllm/model_executor/layers/fused_moe/utils.py
fi_moe_largest_bucket(moe_config)
¶
Estimate FlashInfer's MoE autotuning maximum token count.
All DP ranks may contribute max_num_tokens to one invocation.
Keep FlashInfer's default moe tune_max_num_tokens=8192
floor to avoid over-underestimation.
DeepEP, SP, or PCP may make this underestimate, however overestimation
may be dangerous, increasing tuning- cost and memory use.
NOTE: The DP factor applies even when EP is disabled:
Without
--enable-expert-parallel, MoE layers would use tensor parallelism.
For a detailed explanation, see: docs/serving/data_parallel_deployment.md
Source code in vllm/model_executor/layers/fused_moe/utils.py
masked_amax_scale(x, quant_dtype, *, row_valid=None, num_valid=None, expert_num_tokens=None, rows_per_expert=None, eps=1e-10)
¶
Per-tensor activation quant scale that skips rows without a route.
PaddedStandard buffers carry rows that hold no route (topk_ids == -1)
and whose contents are uninitialized, so a tensor-wide absmax would be
poisoned by them. Batched dispatch buffers have the same problem per
expert: rows past expert_num_tokens[e] hold stale tokens. Those rows
are dropped from the reduction instead; their quantized values are never
consumed by an expert GEMM.
Exactly one of row_valid, num_valid and expert_num_tokens
selects the valid rows.
Parameters:
-
(x¶Tensor) –2D activations to reduce.
-
(quant_dtype¶dtype) –Target quantization dtype (
torch.int8or a float8). -
(row_valid¶Tensor | None, default:None) –Boolean flags, one per row of
x. Use when the valid rows are not a prefix. -
(num_valid¶Tensor | None, default:None) –1-element device tensor holding the number of leading valid rows. Cheaper than
row_validwhen padding trails the rows. -
(expert_num_tokens¶Tensor | None, default:None) –Device tensor of received token counts, one per expert, for a buffer laid out as
[expert, rows_per_expert, K]. -
(rows_per_expert¶int | None, default:None) –Row stride between experts, i.e. the worst-case per-expert capacity of the buffer.
-
(eps¶float, default:1e-10) –Lower bound on the returned scale, matching the dynamic kernels.
Source code in vllm/model_executor/layers/fused_moe/utils.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
moe_use_td_hw_supported()
¶
Whether the current device can run the TD (gather) path of
fused_moe_kernel (ignores the VLLM_TRITON_USE_TD override).
The A-load uses tensor_descriptor.gather, which lowers to the PTX
tile::gather4 instruction. That instruction is part of the
tcgen05/Tensor Memory (TMEM) family introduced with Blackwell and has
no Hopper (sm90) equivalent -- ptxas rejects it there ("Feature
'.tile::gather4 ...' requires .target sm_100 or higher"). Unlike
scatter4, gather4 is supported across the whole sm100+ range
including consumer Blackwell (sm120/sm121): see triton-lang/triton#8498,
which enables gather4 on sm120/sm121 while leaving scatter4
unsupported there. So this gates on a blanket has_device_capability(100)
rather than the sm100 family check used for the scatter store path.
Source code in vllm/model_executor/layers/fused_moe/utils.py
remap_topk_to_local(topk_ids, expert_map, *, out_dtype=torch.int64)
¶
Map global expert ids to local ids; non-local or invalid routes become -1.
Replaces torch.where(topk_ids >= 0, expert_map[topk_ids.clamp(min=0)], -1)
with one kernel. Returns a NEW tensor -- the caller keeps the original
topk_ids as global_topk_ids, so this must not write in place.
Parameters:
-
(topk_ids¶Tensor) –Global expert ids; -1 marks invalid slots. Must be contiguous; the Triton kernel indexes by flat
numel()offsets. -
(expert_map¶Tensor) –Global-to-local mapping; -1 marks non-local experts.
-
(out_dtype¶dtype, default:int64) –Output dtype,
torch.int32ortorch.int64.
Returns:
-
Tensor–Remapped local expert ids with the same shape as
topk_ids.
Source code in vllm/model_executor/layers/fused_moe/utils.py
resolve_layer_fused_shared_expert(quant_config, prefix, shared_expert_name='shared_experts', moe_backend=None, disable_under_ep=False)
¶
Resolve whether AITER fused shared-expert execution is enabled.
Parameters:
-
(quant_config¶QuantizationConfig | None) –Model quantization configuration.
-
(prefix¶str) –MoE module prefix.
-
(shared_expert_name¶str, default:'shared_experts') –Shared-expert module name under
prefix. -
(moe_backend¶str | None, default:None) –MoE backend that runs the fused shared expert.
-
(disable_under_ep¶bool, default:False) –Whether this model family does not support FSE under expert parallelism. Kept as an explicit call-site policy instead of branching on model type here.
Returns:
-
bool–Whether AITER fused shared experts are enabled.
Raises:
-
ValueError–If requested shared-expert fusion is quantization-incompatible.
Source code in vllm/model_executor/layers/fused_moe/utils.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 | |
resolve_moe_use_td()
¶
Tri-state resolver for VLLM_TRITON_USE_TD.
Unset auto-selects the TD path on XPU only, mirroring the attention
dispatcher in triton_attn.py. 1/0 force it on/off regardless
of hardware; forcing 1 where it cannot compile (see
moe_use_td_hw_supported) fails at ptxas. Blackwell CUDA (sm100+) can
compile it but is opt-in only, pending validation.
Source code in vllm/model_executor/layers/fused_moe/utils.py
warn_if_moe_use_td_ineffective(active_backend, is_quantized=False)
¶
One-shot warning when VLLM_TRITON_USE_TD is set but ignored.
Fires when the user set the env explicitly and either (a) the active MoE backend is not the fused Triton kernel, or (b) the model is quantized (the TD path falls back to the pointer path under any quantization).