core.inference.moe.flashinfer_mxfp8#
FlashInfer MoE support for MCore MXFP8 expert weights.
Module Contents#
Classes#
An expert-weight stack in TRT-LLM Major-K MXFP8 layout. |
|
An expert-weight stack in CUTLASS row-major MXFP8 layout. |
Functions#
Raise an error when the CUTLASS MXFP8 APIs are unavailable. |
|
Raise an error when routed MXFP8 APIs are unavailable. |
|
Repackage existing MXFP8 scales for FlashInfer’s CUTLASS interface. |
|
View a canonical expert stack as CUTLASS weights, sharing its storage. |
|
Undo MCore’s 128x4 cuBLAS scale swizzle into a logical scale matrix. |
|
Apply TRT-LLM’s architecture-independent 128x4 scale interleave. |
|
Convert a logical UE8M0 scale matrix to TRT-LLM Major-K byte layout. |
|
Repack a stacked MCore MXFP8 expert weight without requantizing it. |
|
Make an expert’s canonical MXFP8 weight write through to its routed slot. |
|
Pack global expert IDs and BF16 routing weights for TRT-LLM MoE. |
|
Normalize FlashInfer’s 0.6.x tensor/list return variants. |
|
Pad and quantize routed-MoE input, returning a 2D uint8 scale matrix. |
|
Select the graph-stable row count for one routed-MoE invocation. |
|
Run full-row MXFP8 MoE through FlashInfer’s CUTLASS backend. |
|
Launch routed MXFP8 MoE with prequantized input and packed routing. |
|
Run the FlashInfer TRT-LLM routed MXFP8 MoE kernel. |
Data#
API#
- core.inference.moe.flashinfer_mxfp8.logger#
‘getLogger(…)’
- core.inference.moe.flashinfer_mxfp8._LOGGED_TOKEN_POLICIES: set[tuple[str, int, int]]#
‘set(…)’
- core.inference.moe.flashinfer_mxfp8.require_flashinfer_cutlass_mxfp8() None#
Raise an error when the CUTLASS MXFP8 APIs are unavailable.
- core.inference.moe.flashinfer_mxfp8.require_flashinfer_routed_mxfp8() None#
Raise an error when routed MXFP8 APIs are unavailable.
- class core.inference.moe.flashinfer_mxfp8.FlashInferRoutedMXFP8Weight#
An expert-weight stack in TRT-LLM Major-K MXFP8 layout.
- data: torch.Tensor#
None
- scale: torch.Tensor#
None
- logical_rows: int#
None
- logical_cols: int#
None
- padded_rows: int#
None
- padded_cols: int#
None
- class core.inference.moe.flashinfer_mxfp8.FlashInferCutlassMXFP8Weight#
An expert-weight stack in CUTLASS row-major MXFP8 layout.
- data: torch.Tensor#
None
- scale: torch.Tensor#
None
- input_scale: torch.Tensor#
None
- logical_rows: int#
None
- logical_cols: int#
None
- core.inference.moe.flashinfer_mxfp8._round_up(value: int, alignment: int) int#
- core.inference.moe.flashinfer_mxfp8._pack_cutlass_mxfp8_scale(
- scale: torch.Tensor,
- experts: int,
- rows: int,
- cols: int,
Repackage existing MXFP8 scales for FlashInfer’s CUTLASS interface.
Each scale occupies one byte and applies to 32 weights. CUTLASS expects four adjacent scale bytes per int32 entry, with shape [experts, rows, cols // 128]. Only the tensor view changes: scale values and byte order are preserved.
- core.inference.moe.flashinfer_mxfp8.prepare_cutlass_mxfp8_weights(
- weight: megatron.core.inference.quantization.mxfp8_tensor.MXFP8Tensor,
- out: core.inference.moe.flashinfer_mxfp8.FlashInferCutlassMXFP8Weight | None = None,
View a canonical expert stack as CUTLASS weights, sharing its storage.
Callers must retain canonical per-expert views for in-place refits. Neither the data nor the scale bytes need a layout conversion for this backend.
- core.inference.moe.flashinfer_mxfp8._unshuffle_cublas_scale(
- scale: torch.Tensor,
- logical_rows: int,
- logical_cols: int,
Undo MCore’s 128x4 cuBLAS scale swizzle into a logical scale matrix.
- core.inference.moe.flashinfer_mxfp8._block_scale_interleave(scale: torch.Tensor) torch.Tensor#
Apply TRT-LLM’s architecture-independent 128x4 scale interleave.
- core.inference.moe.flashinfer_mxfp8._shuffle_routed_scale(scale: torch.Tensor) torch.Tensor#
Convert a logical UE8M0 scale matrix to TRT-LLM Major-K byte layout.
- core.inference.moe.flashinfer_mxfp8.prepare_routed_mxfp8_weights(
- weight: megatron.core.inference.quantization.mxfp8_tensor.MXFP8Tensor,
- out: core.inference.moe.flashinfer_mxfp8.FlashInferRoutedMXFP8Weight | None = None,
Repack a stacked MCore MXFP8 expert weight without requantizing it.
When
outis provided, its data and scale tensors are refreshed in place. This preserves addresses captured by CUDA graphs across model refits.
- core.inference.moe.flashinfer_mxfp8.attach_routed_mxfp8_writeback(
- weight: megatron.core.inference.quantization.mxfp8_tensor.MXFP8Tensor,
- routed: core.inference.moe.flashinfer_mxfp8.FlashInferRoutedMXFP8Weight,
- expert: int,
Make an expert’s canonical MXFP8 weight write through to its routed slot.
Refit’s quantize_/copy_ then requantizes with the canonical kernel and repacks into the routed stack in place (same bytes, same CUDA-graph addresses), so the canonical storage is released and replaced by broadcast placeholders: only the routed copy stays resident.
After this call weight.data and weight.scale no longer hold the weights: reading them (dequantize, torch.stack, .contiguous(), …) silently returns one uninitialized element broadcast to the full shape. Read the routed stack instead.
- core.inference.moe.flashinfer_mxfp8.pack_routed_mxfp8_routing(
- expert_ids: torch.Tensor,
- probabilities: torch.Tensor,
Pack global expert IDs and BF16 routing weights for TRT-LLM MoE.
- core.inference.moe.flashinfer_mxfp8._unwrap_output(output) torch.Tensor#
Normalize FlashInfer’s 0.6.x tensor/list return variants.
- core.inference.moe.flashinfer_mxfp8.quantize_routed_mxfp8_input(
- hidden_states: torch.Tensor,
- padded_hidden_size: int,
Pad and quantize routed-MoE input, returning a 2D uint8 scale matrix.
- core.inference.moe.flashinfer_mxfp8.select_routed_mxfp8_active_rows(
- full_rows: int,
- *,
- token_capacity: int | None,
- use_bounded_rows: bool,
Select the graph-stable row count for one routed-MoE invocation.
The controller enables a bounded prefix only for a decode-only graph whose static EP-wide token bound fits the configured capacity. All other graphs retain the full dispatcher buffer so prompt tokens are never truncated.
- core.inference.moe.flashinfer_mxfp8.flashinfer_cutlass_mxfp8_moe(
- hidden_states: torch.Tensor,
- routing_map: torch.Tensor,
- probabilities: torch.Tensor,
- fc1_weight: core.inference.moe.flashinfer_mxfp8.FlashInferCutlassMXFP8Weight,
- fc2_weight: core.inference.moe.flashinfer_mxfp8.FlashInferCutlassMXFP8Weight,
- *,
- ep_size: int,
- ep_rank: int,
- activation_type,
- out: torch.Tensor | None = None,
- activation_clamp_limit: torch.Tensor | None = None,
Run full-row MXFP8 MoE through FlashInfer’s CUTLASS backend.
- core.inference.moe.flashinfer_mxfp8.flashinfer_routed_mxfp8_moe_prequantized(
- quantized_hidden: torch.Tensor,
- hidden_scale: torch.Tensor,
- packed_routing: torch.Tensor,
- fc1_weight: core.inference.moe.flashinfer_mxfp8.FlashInferRoutedMXFP8Weight,
- fc2_weight: core.inference.moe.flashinfer_mxfp8.FlashInferRoutedMXFP8Weight,
- *,
- num_experts: int,
- local_expert_offset: int,
- top_k: int,
- activation_type: int,
Launch routed MXFP8 MoE with prequantized input and packed routing.
- core.inference.moe.flashinfer_mxfp8.flashinfer_routed_mxfp8_moe(
- hidden_states: torch.Tensor,
- routing_map: torch.Tensor,
- probabilities: torch.Tensor,
- fc1_weight: core.inference.moe.flashinfer_mxfp8.FlashInferRoutedMXFP8Weight,
- fc2_weight: core.inference.moe.flashinfer_mxfp8.FlashInferRoutedMXFP8Weight,
- *,
- num_experts: int,
- local_expert_offset: int,
- activation_type: int,
- out: torch.Tensor | None = None,
- token_capacity: int | None = None,
- use_bounded_rows: bool = False,
Run the FlashInfer TRT-LLM routed MXFP8 MoE kernel.
When token_capacity is set and the controller marks the current graph safe, only that fixed prefix is processed. Prefill, mixed steps, and too-large decode configurations process the full input. Invalid rows in the bounded prefix must already have expert ID -1.
The row choice is made when each CUDA graph is built, so graph replay sees fixed shapes/addresses.