Skip to content

Commit 583b2ff

Browse files
committed
fix ut
1 parent c93ba72 commit 583b2ff

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

fastdeploy/model_executor/layers/moe/moe.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def get_moe_scores(
8686
expert_in_rank_num_list: paddle.Tensor = None,
8787
tokens_per_expert_stats_list: paddle.Tensor = None,
8888
redundant_ep_rank_num_plus_one: int = 1,
89-
topk_reduce_func: Callable = None,
89+
topk_reduce_func: Callable = lambda x: x.sum(axis=-1, keepdim=True) + 1e-20,
9090
) -> paddle.Tensor:
9191
"""
9292
compute moe scores using e_score_correction_bias.
@@ -129,7 +129,7 @@ def get_moe_scores(
129129
redundant_ep_rank_num_plus_one,
130130
)
131131
if envs.FD_USE_PHI_MOE_TOPK:
132-
if topk_reduce_func is not None and original_renormalize:
132+
if original_renormalize:
133133
topk_values = topk_values / topk_reduce_func(topk_values)
134134

135135
if original_routed_scaling_factor != 1.0:
@@ -163,7 +163,8 @@ def __init__(
163163
with_bias: bool = False,
164164
activation="swiglu",
165165
model_format: Optional[str] = None,
166-
topk_reduce_func: Callable = None, # only used when FD_USE_PHI_MOE_TOPK=1
166+
topk_reduce_func: Callable = lambda x: x.sum(axis=-1, keepdim=True)
167+
+ 1e-20, # only used when FD_USE_PHI_MOE_TOPK=1, default is same as noaux_tc kernel
167168
):
168169
"""
169170
Initialize the Moe layer with given parameters.

tests/operators/test_noaux_tc_redundant.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import unittest
16+
from unittest import mock
217

318
import paddle
419

5-
from fastdeploy.model_executor.layers.moe.fused_moe_deepgemm_backend import (
6-
moe_topk_select,
7-
)
820
from fastdeploy.model_executor.layers.moe.moe import get_moe_scores
921

1022

@@ -135,15 +147,17 @@ def test_group_topk_using_phi_topk(self):
135147
e_score_correction_bias=e_score_correction_bias,
136148
)
137149

138-
topk_values, topk_idx = moe_topk_select(
139-
gating_output=gating_output,
140-
n_group=n_group,
141-
topk_group=topk_group,
142-
top_k=top_k,
143-
routed_scaling_factor=routed_scaling_factor,
144-
e_score_correction_bias=e_score_correction_bias,
145-
renormalize=renormalize,
146-
)
150+
with mock.patch.dict("os.environ", {"FD_USE_PHI_MOE_TOPK": "1"}):
151+
new_score, topk_values, topk_idx = get_moe_scores(
152+
gating_output=gating_output,
153+
n_group=n_group,
154+
topk_group=topk_group,
155+
top_k=top_k,
156+
routed_scaling_factor=routed_scaling_factor,
157+
e_score_correction_bias=e_score_correction_bias,
158+
renormalize=renormalize,
159+
topk_reduce_func=lambda x: x.sum(axis=-1, keepdim=True) + 1e-20,
160+
)
147161

148162
equal_topk_value = paddle.allclose(topk_values, ref_topk_values, atol=1e-03, rtol=1e-03).item()
149163
equal_topk_ids = paddle.allclose(

0 commit comments

Comments
 (0)