[MoE] Defer the TRTLLM-Gen top-k finalize on the modular path (#58635)
Signed-off-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
24928a4d62
commit
378504a544
@@ -12,6 +12,10 @@ from vllm.model_executor.layers.fused_moe.config import (
|
||||
FusedMoEQuantConfig,
|
||||
RoutingMethodType,
|
||||
)
|
||||
from vllm.model_executor.layers.fused_moe.moe_output import (
|
||||
UnfinalizedMoEOutput,
|
||||
convert_flashinfer_moe_output,
|
||||
)
|
||||
from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import (
|
||||
TopKWeightAndReduceNoOP,
|
||||
)
|
||||
@@ -273,7 +277,7 @@ class TrtLlmFp8ExpertsModular(TrtLlmFp8ExpertsBase, mk.FusedMoEExpertsModular):
|
||||
workspace2: torch.Tensor,
|
||||
expert_tokens_meta: mk.ExpertTokensMetadata | None,
|
||||
apply_router_weight_on_input: bool,
|
||||
):
|
||||
) -> UnfinalizedMoEOutput | None:
|
||||
import flashinfer
|
||||
from flashinfer.fused_moe import Fp8QuantizationType, WeightLayout
|
||||
|
||||
@@ -297,7 +301,9 @@ class TrtLlmFp8ExpertsModular(TrtLlmFp8ExpertsBase, mk.FusedMoEExpertsModular):
|
||||
weight_layout = WeightLayout.BlockMajorK
|
||||
hidden_states_scale = prepare_deepseek_fp8_x_sf(hidden_states, a1q_scale)
|
||||
|
||||
flashinfer.fused_moe.trtllm_fp8_block_scale_routed_moe(
|
||||
num_tokens = hidden_states.shape[0]
|
||||
defer = self.moe_config.should_defer_moe_finalize(num_tokens)
|
||||
flashinfer_output = flashinfer.fused_moe.trtllm_fp8_block_scale_routed_moe(
|
||||
topk_ids=(topk_ids, topk_weights),
|
||||
routing_bias=None,
|
||||
hidden_states=hidden_states,
|
||||
@@ -321,9 +327,20 @@ class TrtLlmFp8ExpertsModular(TrtLlmFp8ExpertsBase, mk.FusedMoEExpertsModular):
|
||||
use_shuffled_weight=use_shuffled_weight,
|
||||
weight_layout=weight_layout,
|
||||
fp8_quantization_type=fp8_quant_type,
|
||||
output=output,
|
||||
do_finalize=not defer,
|
||||
output=None if defer else output,
|
||||
tune_max_num_tokens=fi_moe_largest_bucket(self.moe_config),
|
||||
)
|
||||
if not defer:
|
||||
return None
|
||||
routed_output = convert_flashinfer_moe_output(
|
||||
flashinfer_output,
|
||||
do_finalize=False,
|
||||
num_tokens=num_tokens,
|
||||
top_k=topk_ids.size(1),
|
||||
)
|
||||
assert isinstance(routed_output, UnfinalizedMoEOutput)
|
||||
return routed_output
|
||||
|
||||
|
||||
class TrtLlmFp8ExpertsMonolithic(TrtLlmFp8ExpertsBase, mk.FusedMoEExpertsMonolithic):
|
||||
|
||||
@@ -289,6 +289,18 @@ class TrtLlmMxfp4ExpertsModular(TrtLlmMxfp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
Moved from trtllm_moe.py.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
moe_config: FusedMoEConfig,
|
||||
quant_config: FusedMoEQuantConfig,
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__(moe_config, quant_config, **kwargs)
|
||||
# Each launch permutes into its own buffer, so a chunked call finalizes.
|
||||
moe_config.limit_deferred_moe_finalize(
|
||||
self._max_supported_tokens(self.topk, moe_config.num_experts)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _supports_parallel_config(
|
||||
moe_parallel_config: FusedMoEParallelConfig,
|
||||
@@ -343,7 +355,7 @@ class TrtLlmMxfp4ExpertsModular(TrtLlmMxfp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
|
||||
def _invoke_kernel(
|
||||
self,
|
||||
output: torch.Tensor,
|
||||
output: torch.Tensor | None,
|
||||
x_quant: torch.Tensor,
|
||||
x_scale: torch.Tensor | None,
|
||||
topk_ids: torch.Tensor,
|
||||
@@ -355,10 +367,11 @@ class TrtLlmMxfp4ExpertsModular(TrtLlmMxfp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
local_num_experts: int,
|
||||
local_expert_offset: int,
|
||||
topk: int,
|
||||
) -> None:
|
||||
) -> UnfinalizedMoEOutput | None:
|
||||
"""Finalize into ``output``, or stop after GEMM2 when it is None."""
|
||||
from flashinfer import trtllm_fp4_block_scale_routed_moe
|
||||
|
||||
trtllm_fp4_block_scale_routed_moe(
|
||||
flashinfer_output = trtllm_fp4_block_scale_routed_moe(
|
||||
topk_ids=(topk_ids, topk_weights),
|
||||
routing_bias=None,
|
||||
hidden_states=x_quant,
|
||||
@@ -386,12 +399,22 @@ class TrtLlmMxfp4ExpertsModular(TrtLlmMxfp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
# Modular kernel receives pre-routed tokens, so routing is already
|
||||
# done. Use Renormalize as a safe default the TRTLLM kernel supports.
|
||||
routing_method_type=RoutingMethodType.Renormalize,
|
||||
do_finalize=True,
|
||||
do_finalize=output is not None,
|
||||
enable_pdl=True,
|
||||
activation_type=self._flashinfer_activation_type(activation),
|
||||
output=output,
|
||||
tune_max_num_tokens=fi_moe_largest_bucket(self.moe_config),
|
||||
)
|
||||
if output is not None:
|
||||
return None
|
||||
routed_output = convert_flashinfer_moe_output(
|
||||
flashinfer_output,
|
||||
do_finalize=False,
|
||||
num_tokens=x_quant.shape[0],
|
||||
top_k=topk,
|
||||
)
|
||||
assert isinstance(routed_output, UnfinalizedMoEOutput)
|
||||
return routed_output
|
||||
|
||||
def apply(
|
||||
self,
|
||||
@@ -410,7 +433,7 @@ class TrtLlmMxfp4ExpertsModular(TrtLlmMxfp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
workspace2: torch.Tensor,
|
||||
expert_tokens_meta: mk.ExpertTokensMetadata | None,
|
||||
apply_router_weight_on_input: bool,
|
||||
):
|
||||
) -> UnfinalizedMoEOutput | None:
|
||||
topk_ids = topk_ids.to(dtype=torch.int32)
|
||||
|
||||
topk = topk_ids.size(-1)
|
||||
@@ -428,13 +451,17 @@ class TrtLlmMxfp4ExpertsModular(TrtLlmMxfp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
assert self.w1_scale is not None
|
||||
assert self.w2_scale is not None
|
||||
|
||||
# Chunk tokens so the batched-GEMM grid stays within CUDA limits.
|
||||
# Chunk tokens so the batched-GEMM grid stays within CUDA limits. Each
|
||||
# launch permutes into its own buffer, so only a run that fits in one
|
||||
# launch can leave the top-k reduction to a deferring consumer.
|
||||
M = x_quant.size(0)
|
||||
chunk_size = self._max_supported_tokens(topk, global_num_experts)
|
||||
defer = chunk_size >= M and self.moe_config.should_defer_moe_finalize(M)
|
||||
unfinalized: UnfinalizedMoEOutput | None = None
|
||||
for start in range(0, M, chunk_size):
|
||||
end = min(start + chunk_size, M)
|
||||
self._invoke_kernel(
|
||||
output[start:end],
|
||||
unfinalized = self._invoke_kernel(
|
||||
None if defer else output[start:end],
|
||||
x_quant[start:end],
|
||||
None if x_scale is None else x_scale[start:end],
|
||||
topk_ids[start:end],
|
||||
@@ -448,4 +475,4 @@ class TrtLlmMxfp4ExpertsModular(TrtLlmMxfp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
topk,
|
||||
)
|
||||
|
||||
return output
|
||||
return unfinalized
|
||||
|
||||
@@ -302,6 +302,16 @@ class TrtLlmNvFp4ExpertsBase:
|
||||
class TrtLlmNvFp4ExpertsModular(TrtLlmNvFp4ExpertsBase, mk.FusedMoEExpertsModular):
|
||||
"""Modular version of the implementation (just the experts)."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
moe_config: FusedMoEConfig,
|
||||
quant_config: FusedMoEQuantConfig,
|
||||
per_token_activation: bool = False,
|
||||
):
|
||||
super().__init__(moe_config, quant_config, per_token_activation)
|
||||
# Each launch permutes into its own buffer, so a chunked call finalizes.
|
||||
moe_config.limit_deferred_moe_finalize(self._get_chunk_size())
|
||||
|
||||
@staticmethod
|
||||
def _supports_parallel_config(moe_parallel_config: FusedMoEParallelConfig) -> bool:
|
||||
"""The modular implementation supports all parallel configs."""
|
||||
@@ -333,7 +343,7 @@ class TrtLlmNvFp4ExpertsModular(TrtLlmNvFp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
|
||||
def _invoke_kernel(
|
||||
self,
|
||||
output: torch.Tensor,
|
||||
output: torch.Tensor | None,
|
||||
hidden_states: torch.Tensor,
|
||||
w1: torch.Tensor,
|
||||
w2: torch.Tensor,
|
||||
@@ -342,7 +352,8 @@ class TrtLlmNvFp4ExpertsModular(TrtLlmNvFp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
activation: MoEActivation,
|
||||
global_num_experts: int,
|
||||
a1q_scale: torch.Tensor | None,
|
||||
):
|
||||
) -> UnfinalizedMoEOutput | None:
|
||||
"""Finalize into ``output``, or stop after GEMM2 when it is None."""
|
||||
import flashinfer
|
||||
|
||||
assert self.quant_config.w1_scale is not None
|
||||
@@ -359,7 +370,7 @@ class TrtLlmNvFp4ExpertsModular(TrtLlmNvFp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
output1_scale_gate_scalar = self.quant_config.g1_alphas
|
||||
|
||||
# Invoke kernel.
|
||||
flashinfer.fused_moe.trtllm_fp4_block_scale_routed_moe(
|
||||
flashinfer_output = flashinfer.fused_moe.trtllm_fp4_block_scale_routed_moe(
|
||||
topk_ids=(topk_ids, topk_weights),
|
||||
routing_bias=None,
|
||||
hidden_states=hidden_states,
|
||||
@@ -387,7 +398,7 @@ class TrtLlmNvFp4ExpertsModular(TrtLlmNvFp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
local_num_experts=self.local_num_experts,
|
||||
routed_scaling_factor=None,
|
||||
routing_method_type=1, # not used
|
||||
do_finalize=True,
|
||||
do_finalize=output is not None,
|
||||
activation_type=activation_to_flashinfer_int(activation),
|
||||
per_token_scale=per_token_scale,
|
||||
output=output,
|
||||
@@ -395,6 +406,16 @@ class TrtLlmNvFp4ExpertsModular(TrtLlmNvFp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
fi_moe_largest_bucket(self.moe_config), self._get_chunk_size()
|
||||
),
|
||||
)
|
||||
if output is not None:
|
||||
return None
|
||||
routed_output = convert_flashinfer_moe_output(
|
||||
flashinfer_output,
|
||||
do_finalize=False,
|
||||
num_tokens=hidden_states.shape[0],
|
||||
top_k=self.topk,
|
||||
)
|
||||
assert isinstance(routed_output, UnfinalizedMoEOutput)
|
||||
return routed_output
|
||||
|
||||
def apply(
|
||||
self,
|
||||
@@ -413,7 +434,7 @@ class TrtLlmNvFp4ExpertsModular(TrtLlmNvFp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
workspace2: torch.Tensor,
|
||||
expert_tokens_meta: mk.ExpertTokensMetadata | None,
|
||||
apply_router_weight_on_input: bool,
|
||||
):
|
||||
) -> UnfinalizedMoEOutput | None:
|
||||
assert self._supports_activation(activation)
|
||||
# Per-token defers input quant to _invoke_kernel, so a1q_scale is None.
|
||||
assert a1q_scale is not None or self.per_token_activation
|
||||
@@ -425,8 +446,12 @@ class TrtLlmNvFp4ExpertsModular(TrtLlmNvFp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
chunk_size = self._get_chunk_size()
|
||||
|
||||
if chunk_size >= M:
|
||||
self._invoke_kernel(
|
||||
output,
|
||||
# Each launch permutes into its own buffer, so only a run that fits
|
||||
# in one launch can leave the top-k reduction to a deferring
|
||||
# consumer.
|
||||
defer = self.moe_config.should_defer_moe_finalize(M)
|
||||
return self._invoke_kernel(
|
||||
None if defer else output,
|
||||
hidden_states,
|
||||
w1,
|
||||
w2,
|
||||
@@ -450,6 +475,7 @@ class TrtLlmNvFp4ExpertsModular(TrtLlmNvFp4ExpertsBase, mk.FusedMoEExpertsModula
|
||||
global_num_experts,
|
||||
None if a1q_scale is None else a1q_scale[start:end],
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
class TrtLlmNvFp4ExpertsMonolithic(
|
||||
|
||||
Reference in New Issue
Block a user