[NVIDIA] Add Flashinfer MoE blockscale fp8 backend (#8036)
This commit is contained in:
@@ -47,12 +47,17 @@ from sglang.srt.utils import (
|
||||
get_bool_env_var,
|
||||
is_hip,
|
||||
is_npu,
|
||||
next_power_of_2,
|
||||
)
|
||||
|
||||
_is_hip = is_hip()
|
||||
_is_npu = is_npu()
|
||||
_is_fp8_fnuz = is_fp8_fnuz()
|
||||
_use_aiter = get_bool_env_var("SGLANG_USE_AITER") and _is_hip
|
||||
use_flashinfer_trtllm_moe = (
|
||||
global_server_args_dict["enable_flashinfer_trtllm_moe"]
|
||||
and global_server_args_dict["enable_ep_moe"]
|
||||
)
|
||||
|
||||
if not (_is_npu or _is_hip):
|
||||
from sgl_kernel import silu_and_mul
|
||||
@@ -64,6 +69,13 @@ if _use_aiter:
|
||||
from aiter.fused_moe import fused_moe
|
||||
from aiter.ops.shuffle import shuffle_weight
|
||||
|
||||
if use_flashinfer_trtllm_moe:
|
||||
try:
|
||||
import flashinfer.fused_moe as fi_fused_moe
|
||||
except ImportError:
|
||||
fi_fused_moe = None
|
||||
use_flashinfer_trtllm_moe = False
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -140,6 +152,16 @@ class GroupedGemmRunner(torch.nn.Module):
|
||||
return c
|
||||
|
||||
|
||||
def _get_tile_tokens_dim(num_tokens, top_k, num_experts):
|
||||
# Guess tokens per expert assuming perfect expert distribution first.
|
||||
num_tokens_per_expert = (num_tokens * top_k) // num_experts
|
||||
# And pad the number to the next power of 2.
|
||||
tile_tokens_dim = next_power_of_2(num_tokens_per_expert)
|
||||
# Cap to 8-64 tokens per CTA tile as it's the range supported by the kernel.
|
||||
tile_tokens_dim = min(max(tile_tokens_dim, 8), 64)
|
||||
return tile_tokens_dim
|
||||
|
||||
|
||||
class EPMoE(torch.nn.Module):
|
||||
"""
|
||||
MoE Expert Parallel Impl
|
||||
@@ -776,14 +798,20 @@ class EPMoE(torch.nn.Module):
|
||||
)
|
||||
return
|
||||
|
||||
if shard_id == "w2":
|
||||
# Flashinfer assumes w31 format for w13_weight. Same for the scales.
|
||||
if use_flashinfer_trtllm_moe:
|
||||
actual_shard_id = {"w1": "w3", "w3": "w1", "w2": "w2"}[shard_id]
|
||||
else:
|
||||
actual_shard_id = shard_id
|
||||
|
||||
if actual_shard_id == "w2":
|
||||
param.data[expert_id] = loaded_weight
|
||||
elif shard_id == "w1":
|
||||
elif actual_shard_id == "w1":
|
||||
param.data[expert_id][: self.intermediate_size, :] = loaded_weight
|
||||
elif shard_id == "w3":
|
||||
elif actual_shard_id == "w3":
|
||||
param.data[expert_id][self.intermediate_size :, :] = loaded_weight
|
||||
else:
|
||||
raise ValueError(f"Expected shard_id w1,w2 or w3 but got {shard_id}")
|
||||
raise ValueError(f"Expected shard_id w1,w2 or w3 but got {actual_shard_id}")
|
||||
|
||||
def _load_fp8_scale(
|
||||
self,
|
||||
@@ -820,12 +848,18 @@ class EPMoE(torch.nn.Module):
|
||||
# Weight scales
|
||||
elif "weight_scale" in weight_name:
|
||||
if self.use_block_quant:
|
||||
if use_flashinfer_trtllm_moe:
|
||||
actual_shard_id = {"w1": "w3", "w3": "w1", "w2": "w2"}[shard_id]
|
||||
else:
|
||||
actual_shard_id = shard_id
|
||||
|
||||
block_n, block_k = self.block_shape[0], self.block_shape[1]
|
||||
if shard_id == "w1":
|
||||
|
||||
if actual_shard_id == "w1":
|
||||
param_data[expert_id][
|
||||
: (self.intermediate_size + block_n - 1) // block_n, :
|
||||
] = loaded_weight
|
||||
elif shard_id == "w3":
|
||||
elif actual_shard_id == "w3":
|
||||
param_data[expert_id][
|
||||
(self.intermediate_size + block_n - 1) // block_n :, :
|
||||
] = loaded_weight
|
||||
@@ -1315,12 +1349,73 @@ class DeepEPMoE(EPMoE):
|
||||
return down_output
|
||||
|
||||
|
||||
class FlashInferEPMoE(EPMoE):
|
||||
def __init__(self, *args, **kwargs):
|
||||
renormalize = kwargs.pop("renormalize", True)
|
||||
num_fused_shared_experts = kwargs.pop("num_fused_shared_experts", 0)
|
||||
use_grouped_topk = kwargs.pop("use_grouped_topk", False)
|
||||
num_expert_group = kwargs.pop("num_expert_group", None)
|
||||
topk_group = kwargs.pop("topk_group", None)
|
||||
correction_bias = kwargs.pop("correction_bias", None)
|
||||
super().__init__(*args, **kwargs)
|
||||
self.renormalize = renormalize
|
||||
self.num_fused_shared_experts = num_fused_shared_experts
|
||||
self.use_grouped_topk = use_grouped_topk
|
||||
if self.use_grouped_topk:
|
||||
assert num_expert_group is not None and topk_group is not None
|
||||
self.num_expert_group = num_expert_group
|
||||
self.topk_group = topk_group
|
||||
self.correction_bias = correction_bias
|
||||
|
||||
def forward(self, hidden_states: torch.Tensor, router_logits: torch.Tensor):
|
||||
assert use_flashinfer_trtllm_moe
|
||||
assert (
|
||||
self.activation == "silu"
|
||||
), "Only silu is supported for flashinfer blockscale fp8 moe"
|
||||
assert (
|
||||
self.renormalize
|
||||
), "Renormalize is required for flashinfer blockscale fp8 moe"
|
||||
assert (
|
||||
self.num_fused_shared_experts == 0
|
||||
), "Fused shared experts are not supported for flashinfer blockscale fp8 moe"
|
||||
a_q, a_sf = sglang_per_token_group_quant_fp8(hidden_states, self.block_shape[1])
|
||||
# NOTE: scales of hidden states have to be transposed!
|
||||
a_sf_t = a_sf.t().contiguous()
|
||||
assert fi_fused_moe is not None
|
||||
return fi_fused_moe.trtllm_fp8_block_scale_moe(
|
||||
routing_logits=router_logits.to(torch.float32),
|
||||
routing_bias=self.correction_bias.to(hidden_states.dtype),
|
||||
hidden_states=a_q,
|
||||
hidden_states_scale=a_sf_t,
|
||||
gemm1_weights=self.w13_weight,
|
||||
gemm1_weights_scale=self.w13_weight_scale_inv,
|
||||
gemm2_weights=self.w2_weight,
|
||||
gemm2_weights_scale=self.w2_weight_scale_inv,
|
||||
num_experts=self.num_experts,
|
||||
top_k=self.top_k,
|
||||
n_group=self.num_expert_group,
|
||||
topk_group=self.topk_group,
|
||||
intermediate_size=self.w2_weight.shape[2],
|
||||
local_expert_offset=self.start_expert_id,
|
||||
local_num_experts=self.num_experts_per_partition,
|
||||
routed_scaling_factor=self.routed_scaling_factor,
|
||||
tile_tokens_dim=_get_tile_tokens_dim(
|
||||
hidden_states.shape[0], self.top_k, self.num_experts
|
||||
),
|
||||
routing_method_type=2, # DeepSeek-styled routing method
|
||||
use_shuffled_weight=False,
|
||||
)
|
||||
|
||||
|
||||
def get_moe_impl_class():
|
||||
if global_server_args_dict["enable_deepep_moe"]:
|
||||
return DeepEPMoE
|
||||
if global_server_args_dict["enable_flashinfer_moe"]:
|
||||
if global_server_args_dict["enable_flashinfer_cutlass_moe"]:
|
||||
# Must come before EPMoE because FusedMoE also supports enable_ep_moe
|
||||
return FusedMoE
|
||||
if use_flashinfer_trtllm_moe:
|
||||
# Must come before EPMoE because FusedMoE also supports enable_ep_moe
|
||||
return FlashInferEPMoE
|
||||
if global_server_args_dict["enable_ep_moe"]:
|
||||
return EPMoE
|
||||
return FusedMoE
|
||||
|
||||
Reference in New Issue
Block a user