vllm.model_executor.layers.quantization.utils.flashinfer_utils
¶
Functions:
-
align_fp4_moe_weights_for_fi–Pad intermediate size so FlashInfer kernels' alignment constraints hold.
-
align_moe_weights_for_fi–Pad intermediate size so FlashInfer kernels' alignment constraints hold.
-
align_mxfp8_moe_weights_for_fi_trtllm–align_moe_weights_for_fi for MXFP8 weights and their UE8M0 block scales
-
convert_moe_weights_to_flashinfer_trtllm_block_layout–Convert expert weights to FlashInfer's block layout.
-
interleave_w13_up_gate_for_flashinfer_cutedsl_sm90–Repack
[gate; up]weights[E, 2I, K]into the up/gate interleave of -
prepare_fp8_moe_layer_for_fi–Convert Fp8 MoE weights to flashinfer kernel format.
-
quantize_nvfp4_per_token_input–Quantize NVFP4 activations with one FP32 decode scale per token.
-
rotate_weights_for_fi_trtllm_fp8_per_tensor_moe–Shuffle weights for FI TRT-LLM Format.
-
trtllm_nvfp4_hidden_alignment–Hidden-dim alignment for the TRTLLM-Gen NVFP4 MoE cubins.
_shuffle_deepseek_fp8_moe_weights(w13, w2)
¶
Preprocess DeepSeek FP8 block-scale weights for the FlashInfer TRT-LLM kernel using the shuffle + BlockMajorK layout variant.
Returns 4D weight tensors in BlockMajorK layout (E, K/block_k, Mn, block_k)
_shuffle_mxfp8_moe_weights(w13, w2, w13_scale, w2_scale, is_gated)
¶
Preprocess MXFP8 weights and scales for the FlashInfer TRT-LLM kernel.
All three transforms (gate/up row reorder, shuffle_matrix_a weight
shuffle, shuffle_matrix_sf_a scale shuffle) are fixed row/index
permutations that depend only on the per-expert matrix shape, so the
permutation is computed once and applied to every expert in a single
gather instead of once per expert. block_scale_interleave accepts a
batched (E, M, K) scale tensor directly. Output is bit-identical to
the per-expert loop but ~20x faster (a 288-expert MoE otherwise costs
seconds per layer at load).
align_fp4_moe_weights_for_fi(w13, w13_scale, w2, w2_scale, is_act_and_mul, min_alignment=16)
¶
Pad intermediate size so FlashInfer kernels' alignment constraints hold.
Some FlashInfer FP4 MoE kernels require the intermediate size used for GEMM to be divisible by a small alignment value. When this is not satisfied (e.g. with certain tensor-parallel sizes), we pad the gate/up and down projection weights along the intermediate dim.
align_moe_weights_for_fi(w13, w2, is_act_and_mul, min_alignment=16)
¶
Pad intermediate size so FlashInfer kernels' alignment constraints hold.
Some FlashInfer MoE kernels require the (gated) intermediate size used for GEMM to be divisible by a small alignment value. When this is not satisfied (e.g. with certain tensor-parallel sizes), we pad the gate/up and down projection weights along the intermediate dim.
align_mxfp8_moe_weights_for_fi_trtllm(w13, w2, w13_scale, w2_scale, is_act_and_mul, min_alignment, hidden_alignment)
¶
align_moe_weights_for_fi for MXFP8 weights and their UE8M0 block scales for the FlashInfer TRT-LLM MoE kernel, which also pads the hidden size to a multiple of hidden_alignment.
Padded weights are zero and padded scales are 127 (UE8M0 for 1.0), so the padded rows and columns contribute nothing. Scales may be 3D [E, rows, cols] or flattened [E, rows * cols]; their layout is kept. Returns the padded intermediate and hidden sizes.
convert_moe_weights_to_flashinfer_trtllm_block_layout(cache_permute_indices, w13_weight, w2_weight, is_gated_act_gemm=True)
¶
Convert expert weights to FlashInfer's block layout.
This reorders W13 and W2 in place into the expected epilogue-tiled block layout and returns views of the shuffled weight tensors. Using one expert as scratch space avoids allocating another full copy of both weights.
interleave_w13_up_gate_for_flashinfer_cutedsl_sm90(w13)
¶
Repack [gate; up] weights [E, 2I, K] into the up/gate interleave of
FlashInfer's SM90 CuTe-DSL gated GEMM1, [up 0:32 | gate 0:32 | up 32:64 |
gate 32:64 | ...] along dim 1, so each epilogue thread holds an (up, gate)
pair.
prepare_fp8_moe_layer_for_fi(layer, w13, w2, w13_scale, w13_input_scale, w2_scale, w2_input_scale, is_trtllm=False)
¶
Convert Fp8 MoE weights to flashinfer kernel format.
Note that for trtllm we update the model state dict with the scale format needed for these kernels.
Note that for per-tensor and for MXFP8 TRT-LLM, we update the layer's intermediate size (and for MXFP8 TRT-LLM its hidden size) if the weights needed padding.
quantize_nvfp4_per_token_input(hidden_states)
¶
Quantize NVFP4 activations with one FP32 decode scale per token.
rotate_weights_for_fi_trtllm_fp8_per_tensor_moe(gemm1_weights, gemm2_weights, is_gated_activation)
¶
Shuffle weights for FI TRT-LLM Format.
trtllm_nvfp4_hidden_alignment(per_token_activation, is_act_and_mul)
¶
Hidden-dim alignment for the TRTLLM-Gen NVFP4 MoE cubins.
The per-token (dynamic activation scale) non-gated variant has far fewer tile configs for 256-aligned K and its heuristic default tactic fails with "No valid config found" (e.g. Nemotron's 2688 -> 2816), so pad to 512.