Skip to content

Commit 85687df

Browse files
update code
1 parent 9490153 commit 85687df

15 files changed

Lines changed: 618 additions & 251 deletions

File tree

fastdeploy/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,8 @@ def __init__(
651651
self.enable_expert_parallel = False
652652
self.enable_chunked_moe = False
653653
self.chunked_moe_size = 256
654-
654+
self.enable_mega_moe = False
655+
655656
self.local_data_parallel_id = 0
656657
# Engine worker queue port
657658
self.engine_worker_queue_port: Union[int, str, list] = None

fastdeploy/engine/args_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ class EngineArgs:
369369
Whether use chunked moe.
370370
"""
371371

372+
enable_mega_moe: bool = False
373+
"""
374+
Whether use MegaMoE wfp4afp8 for MoE and block_wise_fp8 for dense Linear.
375+
"""
376+
372377
chunked_moe_size: int = 256
373378
"""
374379
Chunk size of moe input.
@@ -1176,6 +1181,12 @@ def add_cli_args(parser: FlexibleArgumentParser) -> FlexibleArgumentParser:
11761181
default=EngineArgs.enable_chunked_moe,
11771182
help="Use chunked moe.",
11781183
)
1184+
parallel_group.add_argument(
1185+
"--enable-mega-moe",
1186+
action="store_true",
1187+
default=EngineArgs.enable_mega_moe,
1188+
help="Use MegaMoE wfp4afp8 for MoE and block_wise_fp8 for dense Linear.",
1189+
)
11791190
parallel_group.add_argument(
11801191
"--chunked-moe-size",
11811192
type=int,

fastdeploy/engine/engine.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ def _start_worker_service(self):
685685
worker_store_true_flag = {
686686
"enable_expert_parallel": self.cfg.parallel_config.enable_expert_parallel,
687687
"enable_chunked_moe": self.cfg.parallel_config.enable_chunked_moe,
688+
"enable_mega_moe": self.cfg.parallel_config.enable_mega_moe,
688689
"enable_prefix_caching": self.cfg.cache_config.enable_prefix_caching,
689690
"enable_chunked_prefill": self.cfg.cache_config.enable_chunked_prefill,
690691
"do_profile": self.do_profile,

fastdeploy/model_executor/layers/backends/xpu/moe/fused_moe.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
free_tensor,
4040
set_weight_attrs,
4141
)
42+
from fastdeploy.model_executor.layers.moe.ep import EPRunner
4243

4344
from .utils import get_moe_scores
4445

@@ -423,7 +424,7 @@ def apply_ep_prefill(
423424
"""
424425
gate_out = gate(x.cast("float32"))
425426
# 1. Select topk experts and weights
426-
topk_idx, topk_weights = self.ep_prefill_runner.moe_select(layer, gate_out)
427+
topk_idx, topk_weights = EPRunner.moe_select(layer, gate_out)
427428

428429
# 2. Dynamic compute blockwise quantization scales
429430
if "a_tokenwise_int8" in self.xpu_moe_quant_type:
@@ -518,7 +519,7 @@ def apply_ep_decode(
518519
gate_out = gate(x.cast("float32"))
519520

520521
# 1. Select topk experts and weights
521-
topk_idx, topk_weights = self.ep_decoder_runner.moe_select(layer, gate_out)
522+
topk_idx, topk_weights = EPRunner.moe_select(layer, gate_out)
522523

523524
# 2. EP Dispatch
524525
if "a_tokenwise_int8" in self.xpu_moe_quant_type:

fastdeploy/model_executor/layers/moe/ep.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ def __init__(
490490
top_k=self.top_k,
491491
)
492492

493-
def moe_select(self, layer: nn.Layer, gate_out: paddle.Tensor):
493+
@staticmethod
494+
def moe_select(layer: nn.Layer, gate_out: paddle.Tensor):
494495
if layer.redundant_table_manger is not None:
495496
(
496497
ep_rank_to_expert_id_list,
@@ -523,7 +524,7 @@ def moe_select(self, layer: nn.Layer, gate_out: paddle.Tensor):
523524
expert_in_rank_num_list=expert_in_rank_num_list,
524525
tokens_per_expert_stats_list=tokens_per_expert_stats_list,
525526
bias=layer.gate_correction_bias,
526-
moe_topk=self.top_k,
527+
moe_topk=layer.top_k,
527528
apply_norm_weight=True,
528529
enable_softmax_top_k_fused=False,
529530
redundant_ep_rank_num_plus_one=layer.fd_config.eplb_config.redundant_experts_num + 1,
@@ -550,7 +551,7 @@ def moe_select(self, layer: nn.Layer, gate_out: paddle.Tensor):
550551
topk_idx, topk_weights = fastdeploy.model_executor.ops.gpu.moe_topk_select(
551552
gate_out,
552553
layer.gate_correction_bias,
553-
self.top_k,
554+
layer.top_k,
554555
True,
555556
False,
556557
)
@@ -788,3 +789,20 @@ def combine(self, ffn_out, topk_idx, topk_weights, handle, **kwargs):
788789
combine_hook()
789790

790791
return combined_hidden_states
792+
793+
794+
class FakeEPRunner:
795+
""" """
796+
def __init__(self, *args, **kwargs):
797+
pass
798+
799+
def dispatch(self, *args, **kwargs):
800+
""" """
801+
pass
802+
803+
def combine(self, *args, **kwargs):
804+
""" """
805+
pass
806+
807+
def clean_low_latency_buffer(self):
808+
pass

fastdeploy/model_executor/layers/moe/fused_moe_backend_base.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -232,29 +232,24 @@ def apply(
232232
"""
233233
if layer.ep_size > 1:
234234
is_moe_start_layer = layer.layer_idx == layer.fd_config.model_config.moe_layer_start_index
235-
if envs.FD_ENABLE_MAGE_MOE:
236-
return self.apply_mage_moe(
237-
layer, x, gate, topk_ids_hookfunc, shared_experts, fc1_latent_proj, fc2_latent_proj
235+
if layer.fd_config.model_config.moe_phase.phase == "prefill":
236+
if layer.fd_config.scheduler_config.splitwise_role == "mixed" and is_moe_start_layer:
237+
self.ep_prefill_runner.clean_low_latency_buffer()
238+
return self.apply_ep_prefill(
239+
layer,
240+
x,
241+
gate,
242+
topk_ids_hookfunc,
243+
shared_experts,
244+
fc1_latent_proj,
245+
fc2_latent_proj,
238246
)
239247
else:
240-
if layer.fd_config.model_config.moe_phase.phase == "prefill":
241-
if layer.fd_config.scheduler_config.splitwise_role == "mixed" and is_moe_start_layer:
242-
self.ep_prefill_runner.clean_low_latency_buffer()
243-
return self.apply_ep_prefill(
244-
layer,
245-
x,
246-
gate,
247-
topk_ids_hookfunc,
248-
shared_experts,
249-
fc1_latent_proj,
250-
fc2_latent_proj,
251-
)
252-
else:
253-
if layer.fd_config.scheduler_config.splitwise_role == "mixed" and is_moe_start_layer:
254-
self.ep_decoder_runner.clean_low_latency_buffer()
255-
return self.apply_ep_decode(
256-
layer, x, gate, topk_ids_hookfunc, shared_experts, fc1_latent_proj, fc2_latent_proj
257-
)
248+
if layer.fd_config.scheduler_config.splitwise_role == "mixed" and is_moe_start_layer:
249+
self.ep_decoder_runner.clean_low_latency_buffer()
250+
return self.apply_ep_decode(
251+
layer, x, gate, topk_ids_hookfunc, shared_experts, fc1_latent_proj, fc2_latent_proj
252+
)
258253
else:
259254
return self.apply_tp(layer, x, gate, topk_ids_hookfunc, fc1_latent_proj, fc2_latent_proj)
260255

fastdeploy/model_executor/layers/moe/fused_moe_blackwell_backend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from paddleformers.utils.log import logger
2424

2525
import fastdeploy
26-
from fastdeploy.model_executor.layers.moe.ep import deep_ep
26+
from fastdeploy.model_executor.layers.moe.ep import deep_ep, EPRunner
2727
from fastdeploy.model_executor.layers.quantization.fp8_utils import (
2828
deep_gemm,
2929
paddlefleet_ops,
@@ -642,7 +642,7 @@ def apply_ep_prefill(
642642
getattr(layer, "renormalize", True),
643643
)
644644
else:
645-
topk_idx, topk_weights = self.ep_prefill_runner.moe_select(layer, gate_out)
645+
topk_idx, topk_weights = EPRunner.moe_select(layer, gate_out)
646646

647647
if topk_ids_hookfunc is not None:
648648
topk_ids_hookfunc(topk_ids=topk_idx)
@@ -964,7 +964,7 @@ def apply_ep_decode(
964964
gate_out = gate(x)
965965
gate_out = gate_out.cast("float32")
966966
# 1. Select topk experts and weights
967-
topk_idx, topk_weights = self.ep_decoder_runner.moe_select(layer, gate_out)
967+
topk_idx, topk_weights = EPRunner.moe_select(layer, gate_out)
968968

969969
if topk_ids_hookfunc is not None:
970970
topk_ids_hookfunc(topk_ids=topk_idx)

fastdeploy/model_executor/layers/moe/fused_moe_cutlass_backend.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
set_weight_attrs,
5353
weight_fully_copied,
5454
)
55+
from fastdeploy.model_executor.layers.moe.ep import EPRunner
5556

5657

5758
def m_grouped_bf16_gemm_nn_contiguous(x, y, expert_idx_per_token):
@@ -141,7 +142,7 @@ def apply_ep_prefill(
141142
if fc1_latent_proj is not None:
142143
x = fc1_latent_proj(x)
143144
# 1. Select topk experts and weights
144-
topk_idx, topk_weights = self.ep_prefill_runner.moe_select(layer, gate_out)
145+
topk_idx, topk_weights = EPRunner.moe_select(layer, gate_out)
145146

146147
if layer.routed_scaling_factor_learnable:
147148
safe_topk_indices = paddle.clip(topk_idx, min=0)
@@ -304,7 +305,7 @@ def apply_ep_decode(
304305

305306
estimate_total_token_nums = gate_out.shape[0] * layer.top_k
306307
# 1. Select topk experts and weights
307-
topk_idx, topk_weights = self.ep_decoder_runner.moe_select(layer, gate_out)
308+
topk_idx, topk_weights = EPRunner.moe_select(layer, gate_out)
308309

309310
if layer.routed_scaling_factor_learnable:
310311
safe_topk_indices = paddle.clip(topk_idx, min=0)

0 commit comments

Comments
 (0)