Skip to content

Commit 7baf1b5

Browse files
authored
【Hackathon 9th No.27】add test_get_padding_offset (#3708)
* add test_get_padding_offset * fix * fix * fix
1 parent 9ec4fa0 commit 7baf1b5

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 numpy as np
18+
import paddle
19+
20+
from fastdeploy.model_executor.ops.gpu import get_padding_offset
21+
22+
23+
class TestGetPaddingOffset(unittest.TestCase):
24+
def test_get_padding_offset(self):
25+
max_len = 10
26+
seq_lens = np.array([4, 3, 6], "int32").reshape(-1, 1)
27+
cum_offset = np.cumsum((max_len - seq_lens).flatten(), -1, "int32")
28+
token_num = np.sum(seq_lens)
29+
input_ids = np.array(
30+
[[8, 7, 8, 2, 0, 0, 0, 0, 0, 0], [4, 5, 5, 0, 0, 0, 0, 0, 0, 0], [7, 6, 1, 7, 2, 6, 0, 0, 0, 0]], "int64"
31+
)
32+
(
33+
x_remove_padding,
34+
batch_id_per_token,
35+
cu_seqlens_q,
36+
cu_seqlens_k,
37+
) = get_padding_offset(
38+
paddle.to_tensor(input_ids),
39+
paddle.to_tensor(cum_offset),
40+
paddle.to_tensor(token_num),
41+
paddle.to_tensor(seq_lens),
42+
)
43+
44+
ref_x_remove_padding = np.array([8, 7, 8, 2, 4, 5, 5, 7, 6, 1, 7, 2, 6], "int64")
45+
ref_batch_id_per_token = np.array([0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2], "int32")
46+
ref_cu_seqlens_q = np.array([0, 4, 7, 13], "int32")
47+
ref_cu_seqlens_k = np.array([0, 4, 7, 13], "int32")
48+
49+
np.testing.assert_allclose(x_remove_padding.numpy(), ref_x_remove_padding)
50+
np.testing.assert_allclose(batch_id_per_token.numpy(), ref_batch_id_per_token)
51+
np.testing.assert_allclose(cu_seqlens_q.numpy(), ref_cu_seqlens_q)
52+
np.testing.assert_allclose(cu_seqlens_k.numpy(), ref_cu_seqlens_k)
53+
54+
55+
if __name__ == "__main__":
56+
unittest.main()

0 commit comments

Comments
 (0)