-
Notifications
You must be signed in to change notification settings - Fork 661
【Hackathon 9th No.68】supplementary unit test for ngram_match #3732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
02fed5e
supplement unittest for custom_ops: ngram_match
Echo-Nie f183506
add annotation
Echo-Nie 5270bef
Merge branch 'PaddlePaddle:develop' into ngr
Echo-Nie 23de605
借助 step_idx 信息,改为在具体位置判断是否相等
Echo-Nie 1eeee8e
Merge branch 'ngr' of https://github.com/Echo-Nie/FastDeploy into ngrm
Echo-Nie ae76101
Merge branch 'PaddlePaddle:develop' into ngr
Echo-Nie ab4a8f7
del anno
Echo-Nie 3448daf
del print
Echo-Nie 04c5d01
Merge branch 'develop' into ngr
luotao1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
|
|
||
| import paddle | ||
|
|
||
| from fastdeploy.model_executor.ops.gpu import ngram_match | ||
|
|
||
|
|
||
| class TestNgramMatchOp(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| paddle.set_device("cpu") | ||
|
|
||
| def test_basic_match(self): | ||
| """ | ||
| Case 1: input_ids overlaps with pre_ids, and can extract draft tokens. | ||
| """ | ||
| batch_size = 1 | ||
| seq_len = 6 | ||
|
|
||
| # Input IDs | ||
| input_ids = paddle.to_tensor([[10, 20, 30, 40, 50, 60]], dtype="int64") | ||
| # Length of input IDs | ||
| input_ids_len = paddle.to_tensor([6], dtype="int64") | ||
| # Previous IDs | ||
| pre_ids = paddle.to_tensor([[10, 20, 30, 40, 0, 0]], dtype="int64") | ||
| # Current step index | ||
| step_idx = paddle.to_tensor([3], dtype="int64") | ||
| # Number of draft tokens | ||
| draft_token_num = paddle.to_tensor([3], dtype="int32") | ||
| # Placeholder for draft tokens | ||
| draft_tokens = paddle.zeros([batch_size, seq_len], dtype="int64") | ||
|
|
||
| # Sequence lengths for this time step | ||
| seq_lens_this_time = paddle.zeros([batch_size], dtype="int32") | ||
| # Sequence lengths for encoder | ||
| seq_lens_encoder = paddle.zeros([batch_size], dtype="int32") | ||
| # Sequence lengths for decoder | ||
| seq_lens_decoder = paddle.ones([batch_size], dtype="int32") | ||
| # Maximum decoding length | ||
| max_dec_len = paddle.to_tensor([10], dtype="int64") | ||
|
|
||
| ngram_match( | ||
| input_ids, | ||
| input_ids_len, | ||
| pre_ids, | ||
| step_idx, | ||
| draft_token_num, | ||
| draft_tokens, | ||
| seq_lens_this_time, | ||
| seq_lens_encoder, | ||
| seq_lens_decoder, | ||
| max_dec_len, | ||
| 3, | ||
| 4, | ||
| ) | ||
|
|
||
| # print("step_idx:", step_idx.numpy()) | ||
| # print("draft_tokens_out:", draft_tokens.numpy()) | ||
| # print("seq_lens_this_time_out:", seq_lens_this_time.numpy()) | ||
|
|
||
| # Extract non-zero tokens and assert the results. | ||
| nonzero_tokens = draft_tokens.numpy()[0][draft_tokens.numpy()[0] != 0] | ||
| expected_tokens = [50, 60] | ||
| self.assertTrue((nonzero_tokens == expected_tokens).all()) | ||
|
|
||
| # Check length | ||
| self.assertEqual(seq_lens_this_time.numpy()[0], 3) | ||
|
|
||
| def test_no_match(self): | ||
| """ | ||
| Case 2: pre_ids does not match input_ids, should only keep the current token. | ||
| """ | ||
| batch_size = 1 | ||
| input_ids = paddle.to_tensor([[100, 200, 300, 400]], dtype="int64") | ||
| input_ids_len = paddle.to_tensor([4], dtype="int64") | ||
| pre_ids = paddle.to_tensor([[1, 2, 3, 4]], dtype="int64") | ||
| step_idx = paddle.to_tensor([3], dtype="int64") | ||
| draft_token_num = paddle.to_tensor([2], dtype="int32") | ||
| draft_tokens = paddle.zeros([batch_size, 4], dtype="int64") | ||
|
|
||
| seq_lens_this_time = paddle.zeros([batch_size], dtype="int32") | ||
| seq_lens_encoder = paddle.zeros([batch_size], dtype="int32") | ||
| seq_lens_decoder = paddle.ones([batch_size], dtype="int32") | ||
| max_dec_len = paddle.to_tensor([6], dtype="int64") | ||
|
|
||
| ngram_match( | ||
| input_ids, | ||
| input_ids_len, | ||
| pre_ids, | ||
| step_idx, | ||
| draft_token_num, | ||
| draft_tokens, | ||
| seq_lens_this_time, | ||
| seq_lens_encoder, | ||
| seq_lens_decoder, | ||
| max_dec_len, | ||
| 3, | ||
| 3, | ||
| ) | ||
|
|
||
| print("draft_tokens_out:", draft_tokens.numpy()) | ||
|
||
| print("seq_lens_this_time_out:", seq_lens_this_time.numpy()) | ||
|
|
||
| # No match → should only keep 1 token | ||
| self.assertEqual(seq_lens_this_time.numpy()[0], 1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
辛苦删除注释
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thx