Skip to content

Commit 0eddd79

Browse files
YuanRishengfreeliuzc
authored andcommitted
Add loader test for mtp (PaddlePaddle#3724)
* add test for mtp * fix unittest * fix
1 parent 0302c89 commit 0eddd79

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""
16+
17+
import sys
18+
import unittest
19+
from unittest.mock import Mock
20+
21+
import numpy as np
22+
import paddle
23+
import paddle.distributed.fleet as fleet
24+
25+
from fastdeploy.model_executor.layers.embeddings import VocabParallelEmbedding
26+
from fastdeploy.model_executor.models.ernie4_5_mtp import Ernie4_5_MTPForCausalLM
27+
28+
sys.path.append("../")
29+
from utils import get_default_test_fd_config
30+
31+
strategy = fleet.DistributedStrategy()
32+
fleet.init(strategy=strategy)
33+
34+
35+
class TestErnie4_5_MTPLoadWeights(unittest.TestCase):
36+
def setUp(self):
37+
self.fd_config = get_default_test_fd_config()
38+
self.fd_config.speculative_config = Mock()
39+
self.fd_config.speculative_config.sharing_model = Mock()
40+
self.fd_config.speculative_config.sharing_model.ernie = Mock()
41+
self.fd_config.parallel_config.tp_group = None
42+
self.fd_config.speculative_config.sharing_model.ernie.embed_tokens = VocabParallelEmbedding(
43+
fd_config=self.fd_config,
44+
num_embeddings=self.fd_config.model_config.vocab_size,
45+
embedding_dim=self.fd_config.model_config.hidden_size,
46+
params_dtype=paddle.get_default_dtype,
47+
prefix=("embed_tokens"),
48+
)
49+
self.fd_config.speculative_config.sharing_model.ernie.lm_head = Mock()
50+
self.model = Ernie4_5_MTPForCausalLM(self.fd_config)
51+
52+
def test_load_weights_normal_case(self):
53+
weights_iterator = [
54+
("ernie.embed_tokens.weight", np.random.rand(32000, 768).astype("float32")),
55+
("ernie.mtp_block.0.self_attn.qkv_proj.weight", np.random.rand(768, 768 * 3).astype("float32")),
56+
]
57+
for k, v in self.model.named_parameters():
58+
print("{}".format(k))
59+
60+
self.model.load_weights(iter(weights_iterator))
61+
62+
self.assertTrue(np.allclose(self.model.ernie.embed_tokens.embeddings.weight.numpy(), weights_iterator[0][1]))
63+
64+
def test_load_weights_with_unexpected_keys(self):
65+
weights_iterator = [
66+
("unknown_key", np.random.rand(10, 10).astype("float32")),
67+
("ernie.embed_tokens.weight", np.random.rand(32000, 768).astype("float32")),
68+
]
69+
70+
self.model.load_weights(iter(weights_iterator))
71+
72+
self.assertTrue(np.allclose(self.model.ernie.embed_tokens.embeddings.weight.numpy(), weights_iterator[1][1]))
73+
74+
def test_load_weights_empty_iterator(self):
75+
weights_iterator = []
76+
77+
self.model.load_weights(iter(weights_iterator))
78+
79+
80+
if __name__ == "__main__":
81+
unittest.main()

tests/utils.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""
16+
17+
from unittest.mock import Mock
18+
19+
from fastdeploy.config import (
20+
CacheConfig,
21+
FDConfig,
22+
GraphOptimizationConfig,
23+
ParallelConfig,
24+
)
25+
26+
27+
class FakeModelConfig:
28+
def __init__(self):
29+
self.hidden_size = 768
30+
self.intermediate_size = 768
31+
self.num_hidden_layers = 12
32+
self.num_attention_heads = 12
33+
self.rms_norm_eps = 1e-6
34+
self.tie_word_embeddings = True
35+
self.ori_vocab_size = 32000
36+
self.moe_layer_start_index = 8
37+
self.pretrained_config = Mock()
38+
self.pretrained_config.prefix_name = "test"
39+
self.num_key_value_heads = 1
40+
self.head_dim = 1
41+
self.is_quantized = False
42+
self.hidden_act = "relu"
43+
self.vocab_size = 32000
44+
self.hidden_dropout_prob = 0.1
45+
self.initializer_range = 0.02
46+
self.max_position_embeddings = 512
47+
self.tie_word_embeddings = True
48+
self.model_format = "auto"
49+
50+
51+
def get_default_test_fd_config():
52+
graph_opt_config = GraphOptimizationConfig(args={})
53+
parallel_config = ParallelConfig(args={})
54+
parallel_config.max_num_seqs = 1
55+
parallel_config.data_parallel_rank = 1
56+
cache_config = CacheConfig({})
57+
fd_config = FDConfig(
58+
graph_opt_config=graph_opt_config, parallel_config=parallel_config, cache_config=cache_config, test_mode=True
59+
)
60+
fd_config.model_config = FakeModelConfig()
61+
return fd_config

0 commit comments

Comments
 (0)