Skip to content

Commit 20495f9

Browse files
Echo-Nieluotao1
andauthored
[UnitTest][MTP] supplementary unit test for ngram_match (#3732)
* supplement unittest for custom_ops: ngram_match * add annotation * 借助 step_idx 信息,改为在具体位置判断是否相等 * del anno * del print --------- Co-authored-by: Tao Luo <[email protected]>
1 parent 0c46318 commit 20495f9

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright (c) 2025 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+
15+
import unittest
16+
17+
import paddle
18+
19+
from fastdeploy.model_executor.ops.gpu import ngram_match
20+
21+
22+
class TestNgramMatchOp(unittest.TestCase):
23+
24+
def setUp(self):
25+
paddle.set_device("cpu")
26+
27+
def test_basic_match(self):
28+
"""
29+
Case 1: input_ids overlaps with pre_ids, and can extract draft tokens.
30+
"""
31+
batch_size = 1
32+
seq_len = 6
33+
34+
# Input IDs
35+
input_ids = paddle.to_tensor([[10, 20, 30, 40, 50, 60]], dtype="int64")
36+
# Length of input IDs
37+
input_ids_len = paddle.to_tensor([6], dtype="int64")
38+
# Previous IDs
39+
pre_ids = paddle.to_tensor([[10, 20, 30, 40, 0, 0]], dtype="int64")
40+
# Current step index
41+
step_idx = paddle.to_tensor([3], dtype="int64")
42+
# Number of draft tokens
43+
draft_token_num = paddle.to_tensor([3], dtype="int32")
44+
# Placeholder for draft tokens
45+
draft_tokens = paddle.zeros([batch_size, seq_len], dtype="int64")
46+
47+
# Sequence lengths for this time step
48+
seq_lens_this_time = paddle.zeros([batch_size], dtype="int32")
49+
# Sequence lengths for encoder
50+
seq_lens_encoder = paddle.zeros([batch_size], dtype="int32")
51+
# Sequence lengths for decoder
52+
seq_lens_decoder = paddle.ones([batch_size], dtype="int32")
53+
# Maximum decoding length
54+
max_dec_len = paddle.to_tensor([10], dtype="int64")
55+
56+
ngram_match(
57+
input_ids,
58+
input_ids_len,
59+
pre_ids,
60+
step_idx,
61+
draft_token_num,
62+
draft_tokens,
63+
seq_lens_this_time,
64+
seq_lens_encoder,
65+
seq_lens_decoder,
66+
max_dec_len,
67+
3,
68+
4,
69+
)
70+
71+
# Extract non-zero tokens and assert the results.
72+
nonzero_tokens = draft_tokens.numpy()[0][draft_tokens.numpy()[0] != 0]
73+
expected_tokens = [50, 60]
74+
self.assertTrue((nonzero_tokens == expected_tokens).all())
75+
76+
# Check length
77+
self.assertEqual(seq_lens_this_time.numpy()[0], 3)
78+
79+
def test_no_match(self):
80+
"""
81+
Case 2: pre_ids does not match input_ids, should only keep the current token.
82+
"""
83+
batch_size = 1
84+
input_ids = paddle.to_tensor([[100, 200, 300, 400]], dtype="int64")
85+
input_ids_len = paddle.to_tensor([4], dtype="int64")
86+
pre_ids = paddle.to_tensor([[1, 2, 3, 4]], dtype="int64")
87+
step_idx = paddle.to_tensor([3], dtype="int64")
88+
draft_token_num = paddle.to_tensor([2], dtype="int32")
89+
draft_tokens = paddle.zeros([batch_size, 4], dtype="int64")
90+
91+
seq_lens_this_time = paddle.zeros([batch_size], dtype="int32")
92+
seq_lens_encoder = paddle.zeros([batch_size], dtype="int32")
93+
seq_lens_decoder = paddle.ones([batch_size], dtype="int32")
94+
max_dec_len = paddle.to_tensor([6], dtype="int64")
95+
96+
ngram_match(
97+
input_ids,
98+
input_ids_len,
99+
pre_ids,
100+
step_idx,
101+
draft_token_num,
102+
draft_tokens,
103+
seq_lens_this_time,
104+
seq_lens_encoder,
105+
seq_lens_decoder,
106+
max_dec_len,
107+
3,
108+
3,
109+
)
110+
111+
# No match → should only keep 1 token
112+
self.assertEqual(seq_lens_this_time.numpy()[0], 1)
113+
114+
115+
if __name__ == "__main__":
116+
unittest.main()

0 commit comments

Comments
 (0)