|
| 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 speculate_get_output_padding_offset |
| 21 | + |
| 22 | + |
| 23 | +class TestSpeculateGetOutputPaddingOffset(unittest.TestCase): |
| 24 | + def test_speculate_get_output_padding_offset(self): |
| 25 | + bsz = 256 |
| 26 | + max_seq_len = 8192 |
| 27 | + |
| 28 | + seq_lens_output = np.random.randint(0, 4, size=bsz) |
| 29 | + output_token_num = np.sum(seq_lens_output) |
| 30 | + |
| 31 | + seq_lens_output = paddle.to_tensor(seq_lens_output, dtype="int32") |
| 32 | + out_token_num = paddle.sum(seq_lens_output).astype("int32") |
| 33 | + output_cum_offsets_tmp = paddle.cumsum(max_seq_len - seq_lens_output).astype("int32") |
| 34 | + |
| 35 | + output_padding_offset_gpu, output_cum_offsets_gpu = speculate_get_output_padding_offset( |
| 36 | + output_cum_offsets_tmp, out_token_num, seq_lens_output, max_seq_len |
| 37 | + ) |
| 38 | + |
| 39 | + output_padding_offset_ref = [-1] * output_token_num |
| 40 | + output_cum_offsets_ref = [-1] * bsz |
| 41 | + |
| 42 | + for bi in range(bsz): |
| 43 | + cum_offset = 0 if bi == 0 else output_cum_offsets_tmp[bi - 1] |
| 44 | + output_cum_offsets_ref[bi] = cum_offset |
| 45 | + for token_i in range(seq_lens_output[bi]): |
| 46 | + output_padding_offset_ref[bi * max_seq_len - cum_offset + token_i] = cum_offset |
| 47 | + |
| 48 | + np.testing.assert_allclose(output_padding_offset_gpu, output_padding_offset_ref) |
| 49 | + np.testing.assert_allclose(output_cum_offsets_gpu, output_cum_offsets_ref) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + unittest.main() |
0 commit comments