Add MoE output contract for MoE tail fusion (#51407)
Signed-off-by: Jee Jee Li <jeejeelee@inferact.ai>
This commit is contained in:
@@ -11,6 +11,9 @@ from vllm.model_executor.layers.fused_moe.config import (
|
||||
FusedMoEQuantConfig,
|
||||
RoutingMethodType,
|
||||
)
|
||||
from vllm.model_executor.layers.fused_moe.moe_output import (
|
||||
UnfinalizedMoEOutput,
|
||||
)
|
||||
from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import (
|
||||
TopKWeightAndReduceNoOP,
|
||||
)
|
||||
@@ -266,13 +269,18 @@ class TrtLlmBf16ExpertsMonolithic(TrtLlmBf16ExpertsBase, mk.FusedMoEExpertsMonol
|
||||
e_score_correction_bias: torch.Tensor | None = None,
|
||||
routed_scaling_factor: float | None = None,
|
||||
topk_group: int | None = None,
|
||||
) -> torch.Tensor:
|
||||
) -> torch.Tensor | UnfinalizedMoEOutput:
|
||||
import flashinfer
|
||||
|
||||
assert activation in [MoEActivation.SILU, MoEActivation.RELU2_NO_MUL]
|
||||
|
||||
num_tokens = hidden_states.shape[0]
|
||||
# The runner divides by the token count on the host, so an idle rank's
|
||||
# dummy 0-token forward has to keep the finalized (empty) form.
|
||||
defer = self.moe_config.use_deferred_moe_finalize and num_tokens > 0
|
||||
|
||||
routing_replay_out = self._maybe_make_routing_replay_buffer(
|
||||
num_tokens=hidden_states.shape[0],
|
||||
num_tokens=num_tokens,
|
||||
device=hidden_states.device,
|
||||
)
|
||||
out = flashinfer.fused_moe.trtllm_bf16_moe(
|
||||
@@ -293,8 +301,19 @@ class TrtLlmBf16ExpertsMonolithic(TrtLlmBf16ExpertsBase, mk.FusedMoEExpertsMonol
|
||||
activation_type=activation_to_flashinfer_int(activation),
|
||||
tune_max_num_tokens=fi_moe_largest_bucket(self.moe_config),
|
||||
routing_replay_out=routing_replay_out,
|
||||
do_finalize=not defer,
|
||||
)
|
||||
self._maybe_dispatch_routing_replay(
|
||||
routing_replay_out, num_tokens=hidden_states.shape[0]
|
||||
)
|
||||
return out
|
||||
self._maybe_dispatch_routing_replay(routing_replay_out, num_tokens=num_tokens)
|
||||
if defer:
|
||||
# flashinfer returns a flat permute map; the protocol wants
|
||||
# [num_tokens, top_k] so consumers can read top_k from its shape.
|
||||
return UnfinalizedMoEOutput(
|
||||
gemm2_permuted=out[0],
|
||||
expert_weights=out[1],
|
||||
expanded_idx_to_permuted_idx=out[2]
|
||||
.to(torch.int32)
|
||||
.view(num_tokens, self.topk),
|
||||
)
|
||||
# do_finalize=True yields the finalized states (a bare tensor on some
|
||||
# FlashInfer versions, a single-element list on others).
|
||||
return out[0] if isinstance(out, (list, tuple)) else out
|
||||
|
||||
@@ -13,6 +13,9 @@ from vllm.model_executor.layers.fused_moe.config import (
|
||||
FusedMoEQuantConfig,
|
||||
RoutingMethodType,
|
||||
)
|
||||
from vllm.model_executor.layers.fused_moe.moe_output import (
|
||||
UnfinalizedMoEOutput,
|
||||
)
|
||||
from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import (
|
||||
TopKWeightAndReduceNoOP,
|
||||
)
|
||||
@@ -518,7 +521,7 @@ class TrtLlmNvFp4ExpertsMonolithic(
|
||||
e_score_correction_bias: torch.Tensor | None = None,
|
||||
routed_scaling_factor: float | None = None,
|
||||
topk_group: int | None = None,
|
||||
) -> torch.Tensor:
|
||||
) -> torch.Tensor | UnfinalizedMoEOutput:
|
||||
import flashinfer
|
||||
|
||||
assert self._supports_activation(activation)
|
||||
@@ -543,8 +546,13 @@ class TrtLlmNvFp4ExpertsMonolithic(
|
||||
|
||||
output1_scale_gate_scalar = self.quant_config.g1_alphas
|
||||
|
||||
num_tokens = hidden_states.shape[0]
|
||||
# The runner divides by the token count on the host, so an idle rank's
|
||||
# dummy 0-token forward has to keep the finalized (empty) form.
|
||||
defer = self.moe_config.use_deferred_moe_finalize and num_tokens > 0
|
||||
|
||||
routing_replay_out = self._maybe_make_routing_replay_buffer(
|
||||
num_tokens=hidden_states.shape[0],
|
||||
num_tokens=num_tokens,
|
||||
device=hidden_states.device,
|
||||
)
|
||||
# Invoke kernel.
|
||||
@@ -578,13 +586,21 @@ class TrtLlmNvFp4ExpertsMonolithic(
|
||||
local_num_experts=self.local_num_experts,
|
||||
routed_scaling_factor=routed_scaling_factor,
|
||||
routing_method_type=self.routing_method_type,
|
||||
do_finalize=True,
|
||||
do_finalize=not defer,
|
||||
activation_type=activation_to_flashinfer_int(activation),
|
||||
per_token_scale=per_token_scale,
|
||||
tune_max_num_tokens=fi_moe_largest_bucket(self.moe_config),
|
||||
routing_replay_out=routing_replay_out,
|
||||
)[0]
|
||||
self._maybe_dispatch_routing_replay(
|
||||
routing_replay_out, num_tokens=hidden_states.shape[0]
|
||||
)
|
||||
return result
|
||||
self._maybe_dispatch_routing_replay(routing_replay_out, num_tokens=num_tokens)
|
||||
if defer:
|
||||
# flashinfer returns a flat permute map; the protocol wants
|
||||
# [num_tokens, top_k] so consumers can read top_k from its shape.
|
||||
return UnfinalizedMoEOutput(
|
||||
gemm2_permuted=result[0],
|
||||
expert_weights=result[1],
|
||||
expanded_idx_to_permuted_idx=result[2]
|
||||
.to(torch.int32)
|
||||
.view(num_tokens, self.topk),
|
||||
)
|
||||
return result[0]
|
||||
|
||||
Reference in New Issue
Block a user