flashinfer.fused_moe.trtllm_fp8_block_scale_moe¶
- flashinfer.fused_moe.trtllm_fp8_block_scale_moe(routing_logits: Tensor, routing_bias: Tensor | None, hidden_states: Tensor, hidden_states_scale: Tensor, gemm1_weights: Tensor, gemm1_weights_scale: Tensor, gemm2_weights: Tensor, gemm2_weights_scale: Tensor, num_experts: int, top_k: int, n_group: int | None, topk_group: int | None, intermediate_size: int, local_expert_offset: int, local_num_experts: int, routed_scaling_factor: float | None, routing_method_type: int = 0, use_shuffled_weight: bool = False, weight_layout: int = 0, do_finalize: bool = True, enable_pdl: bool | None = None, tune_max_num_tokens: int = 8192, fp8_quantization_type: Fp8QuantizationType = Fp8QuantizationType.DeepSeekFp8, num_fused_shared_experts: int | None = None, activation_type: int = 3, norm_topk_prob: bool = True, routing_replay_out: Tensor | None = None, gemm1_alpha: Tensor | None = None, gemm1_beta: Tensor | None = None, gemm1_clamp_limit: Tensor | None = None, output: Tensor | None = None, valid_hidden_size: int | None = None, valid_intermediate_size: int | None = None, *, gemm1_bias: Tensor | None = None, gemm2_bias: Tensor | None = None) List[Tensor] | Tensor¶
FP8 block-scaled MoE operation.
- Parameters:
routing_logits (torch.Tensor) –
[seq_len, num_experts]tensor of routing logits,float32orbfloat16.routing_bias (Optional[torch.Tensor]) –
[num_experts]tensor of routing bias,bfloat16orfloat32. May beNone.hidden_states (torch.Tensor) –
[seq_len, hidden_size]tensor of input hidden states.float16,bfloat16, orfloat8_e4m3fn(block scale must match: seehidden_states_scale).hidden_states_scale (torch.Tensor) –
[hidden_size // 128, seq_len]tensor of hidden-states block scales.gemm1_weights (torch.Tensor) – First-layer weights.
[num_experts, M, hidden_size]whenweight_layout == WeightLayout.MajorK(0), or[num_experts, M // 128, hidden_size, 128]whenweight_layout == WeightLayout.BlockMajorK(2).Mis2 * intermediate_sizefor gated activations andintermediate_sizefor non-gated activations.gemm1_weights_scale (torch.Tensor) –
[num_experts, 2*intermediate_size // (32 if mxfp8 else 128), hidden_size // (32 if mxfp8 else 128)]first-layer block scales.gemm2_weights (torch.Tensor) – Second-layer weights.
[num_experts, hidden_size, intermediate_size]whenweight_layout == WeightLayout.MajorK, or[num_experts, hidden_size // 128, intermediate_size, 128]whenweight_layout == WeightLayout.BlockMajorK.gemm2_weights_scale (torch.Tensor) –
[num_experts, hidden_size // (32 if mxfp8 else 128), intermediate_size // (32 if mxfp8 else 128)]second-layer block scales.num_experts (int) – Total number of experts.
top_k (int) – Number of experts to route to per token.
n_group (Optional[int]) – Number of expert groups.
topk_group (Optional[int]) – Number of groups to consider for top-k routing.
intermediate_size (int) – Size of the intermediate layer.
local_expert_offset (int) – Offset of local experts in the global expert space.
local_num_experts (int) – Number of experts handled by this device.
routed_scaling_factor (Optional[float]) – Scaling factor for routing.
routing_method_type (int) – Routing method (default
0). Seetrtllm_bf16_moe().use_shuffled_weight (bool) – Whether to use the shuffled weight layout (default
False).weight_layout (int) –
Weight layout for
gemm1_weights/gemm2_weights; matchesflashinfer.tllm_enums.WeightLayout. Allowed values for this function depend onfp8_quantization_type:DeepSeekFp8acceptsMajorKorBlockMajorK;MxFp8requiresMajorK. Default0(MajorK).0MajorK— K-major, logical shape[Mn, K].1MajorMn— M-major (A) / N-major (B), logical shape[K, Mn]. Not supported by this function.2BlockMajorK— Blocked along K, logical shape[K / blockK, Mn, blockK](blockKis fixed at 128 B). Only valid when ``fp8_quantization_type`` is ``DeepSeekFp8``.
do_finalize (bool) – Whether to finalize the output (default
True).enable_pdl (Optional[bool]) – Whether to enable Programmatic Dependent Launch.
None(default) lets the runtime auto-select on SM90+.tune_max_num_tokens (int) – Maximum number of tokens for autotuning (default
8192).fp8_quantization_type (Fp8QuantizationType) – FP8 quantization scheme (default
Fp8QuantizationType.DeepSeekFp8).num_fused_shared_experts (Optional[int]) – Number of shared experts to fuse into the MoE kernel (default
None/0). When> 0, the weight tensors must havenum_experts + num_fused_shared_expertsin the expert dimension. Expert parallelism (EP) is not yet supported together with fused shared experts: when this is> 0you must passlocal_expert_offset == 0andlocal_num_experts == num_experts(all routed experts local), otherwise aValueErroris raised.activation_type (int) – Activation type (default
3— Swiglu).3Swiglu;4Geglu;6Relu2;7SwigluStep (MxFp8 only);9Identity.norm_topk_prob (bool) – Whether to normalize the top-k probabilities (default
True).routing_replay_out (Optional[torch.Tensor]) – Optional
int16tensor of shape(num_tokens_or_larger, top_k)used to capture the selected expert IDs during routing. Column order matchestopk_indices. WhenNone(default) the kernel skips the write entirely. The buffer may be larger thannum_tokensfor CUDA-graph pre-allocation; only rows[0, num_tokens)are written. With fused shared experts the layout stays routed-only:dim1istop_k, nottop_k + num_fused_shared_experts, and shared slots are not recorded.gemm1_alpha (Optional[torch.Tensor]) – Optional
[local_num_experts]float32 per-expert SwiGLU OA alpha parameter. Supported forFp8QuantizationType.MxFp8andFp8QuantizationType.DeepSeekFp8withActivationType.Swiglu. Any subset ofgemm1_alpha,gemm1_beta,gemm1_clamp_limitcan be provided independently. WhenNone(default),alpha=1.0is used. Let GEMM1 output be split asX1(linear/up half) andX2(gate half). The activation output isX2 * sigmoid(alpha * X2) * (X1 + beta). Pass raw values; neither block-scale recipe carries a scalar dequant scale, so no host-side conversion is applied.gemm1_beta (Optional[torch.Tensor]) – Optional
[local_num_experts]float32 per-expert SwiGLU OA beta parameter. Supported forFp8QuantizationType.MxFp8andFp8QuantizationType.DeepSeekFp8withActivationType.Swiglu. WhenNone(default),beta=0.0is used.gemm1_clamp_limit (Optional[torch.Tensor]) – Optional
[local_num_experts]float32 per-expert clamp limit. WithSwiglu, supported for MxFp8 and DeepSeekFp8 and clampsX1to[-limit, limit]andX2from above before SiLU. WithSwigluStep, supported only for fused MxFp8 and clampsX1andsilu(X2)at the physical limit.Noneuses physical7for SwigluStep. MXFP8 has no global FC1 dequant scale, so raw accumulator and physical units coincide.output (Optional[torch.Tensor]) – Optional in-place output tensor of shape
[seq_len, hidden_size]. Allocated internally whenNone(default).valid_hidden_size (Optional[int]) – Valid (unpadded) hidden dimension. When provided, the
hidden_sizeimplied by the tensor shapes is treated as padded and only the valid region is contracted. Matching TensorRT-LLM, the returned tensor is exactlyvalid_hidden_sizewide: GEMM2 computesroundUp(valid_hidden_size, 128)columns (that is what the FC2 weights, weight scales and bias are sized for) and finalize writes only the leadingvalid_hidden_sizeof them. A caller-suppliedoutputmust therefore havevalid_hidden_sizecolumns. DefaultNone(use the fullhidden_size).valid_intermediate_size (Optional[int]) – Valid (unpadded) intermediate dimension. When provided,
intermediate_sizeis treated as padded and only the valid region is computed. DefaultNone(use the fullintermediate_size).gemm1_bias (Optional[torch.Tensor]) – Optional FC1 bias, float32 (keyword-only). Shape is
[local_num_experts, 2 * intermediate_size]for gated activations and[local_num_experts, intermediate_size]for non-gated activations. This tensor must be in the same row layout asgemm1_weights.gemm2_bias (Optional[torch.Tensor]) – Optional
[local_num_experts, hidden_size]FC2 bias, float32 (keyword-only). This tensor must be in the same row layout asgemm2_weights.
- Returns:
Final MoE output when
do_finalizeisTrue, otherwise[gemm2_output, expert_weights, expanded_idx_to_permuted_idx].- Return type:
torch.Tensor or List[torch.Tensor]