|
| 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 unittest |
| 18 | +from unittest.mock import MagicMock |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +import paddle |
| 22 | + |
| 23 | +from fastdeploy.engine.request import RequestOutput |
| 24 | +from fastdeploy.output.token_processor import TokenProcessor |
| 25 | + |
| 26 | + |
| 27 | +class TestProcessBatchDraftTokens(unittest.TestCase): |
| 28 | + |
| 29 | + def setUp(self): |
| 30 | + # 模拟 cfg |
| 31 | + cfg = MagicMock() |
| 32 | + cfg.speculative_config = MagicMock() |
| 33 | + cfg.speculative_config.method = "mtp" |
| 34 | + cfg.speculative_config.num_speculative_tokens = 3 |
| 35 | + cfg.model_config = MagicMock() |
| 36 | + cfg.model_config.enable_logprob = True |
| 37 | + |
| 38 | + self.processor = TokenProcessor( |
| 39 | + cfg=cfg, cached_generated_tokens=MagicMock(), engine_worker_queue=MagicMock(), split_connector=MagicMock() |
| 40 | + ) |
| 41 | + |
| 42 | + # mock resource_manager |
| 43 | + self.processor.resource_manager = MagicMock() |
| 44 | + self.processor.resource_manager.stop_flags = [False] * 512 |
| 45 | + self.processor.resource_manager.tasks_list = [MagicMock()] * 512 |
| 46 | + |
| 47 | + for task in self.processor.resource_manager.tasks_list: |
| 48 | + task.request_id = "test_request" |
| 49 | + task.eos_token_ids = [2] |
| 50 | + |
| 51 | + def test_process_batch_draft_tokens_normal_case(self): |
| 52 | + """测试正常情况下的target处理""" |
| 53 | + batch = 2 |
| 54 | + accept_num = [3, 2] |
| 55 | + K = 20 |
| 56 | + MAX_DRAFT_TOKENS = 6 |
| 57 | + |
| 58 | + tokens = np.random.randint(100, 200, size=(batch, MAX_DRAFT_TOKENS, K + 1)) |
| 59 | + scores = np.random.rand(batch, MAX_DRAFT_TOKENS, K + 1).astype(np.float32) |
| 60 | + ranks = np.random.randint(0, K, size=(batch, MAX_DRAFT_TOKENS)) |
| 61 | + |
| 62 | + results = self.processor._process_batch_draft_tokens( |
| 63 | + mtype=4, |
| 64 | + batch=batch, |
| 65 | + accept_num=accept_num, |
| 66 | + tokens=paddle.to_tensor(tokens), |
| 67 | + scores=paddle.to_tensor(scores), |
| 68 | + ranks=paddle.to_tensor(ranks), |
| 69 | + ) |
| 70 | + |
| 71 | + self.assertEqual(len(results), batch) |
| 72 | + for i, result in enumerate(results): |
| 73 | + self.assertIsInstance(result, RequestOutput) |
| 74 | + self.assertEqual(result.output_type, 4) |
| 75 | + self.assertEqual(result.outputs.index, i) |
| 76 | + self.assertEqual(len(result.outputs.draft_top_logprobs.logprob_token_ids), accept_num[i]) |
| 77 | + self.assertEqual(len(result.outputs.draft_top_logprobs.logprobs), accept_num[i]) |
| 78 | + self.assertEqual(len(result.outputs.draft_top_logprobs.sampled_token_ranks), accept_num[i]) |
| 79 | + |
| 80 | + def test_process_batch_draft_tokens_with_stop_flag(self): |
| 81 | + """测试有停止标志的情况""" |
| 82 | + batch = 3 |
| 83 | + self.processor.resource_manager.stop_flags[1] = True # 第二个 request 停止 |
| 84 | + |
| 85 | + accept_num = [3, 2, 1] |
| 86 | + K = 20 |
| 87 | + MAX_DRAFT_TOKENS = 6 |
| 88 | + |
| 89 | + tokens = np.random.randint(100, 200, size=(batch, MAX_DRAFT_TOKENS, K + 1)) |
| 90 | + scores = np.random.rand(batch, MAX_DRAFT_TOKENS, K + 1).astype(np.float32) |
| 91 | + ranks = np.random.randint(0, K, size=(batch, MAX_DRAFT_TOKENS)) |
| 92 | + |
| 93 | + results = self.processor._process_batch_draft_tokens( |
| 94 | + mtype=4, |
| 95 | + batch=batch, |
| 96 | + accept_num=accept_num, |
| 97 | + tokens=paddle.to_tensor(tokens), |
| 98 | + scores=paddle.to_tensor(scores), |
| 99 | + ranks=paddle.to_tensor(ranks), |
| 100 | + ) |
| 101 | + |
| 102 | + self.assertEqual(len(results), 2) |
| 103 | + self.assertEqual(results[0].outputs.index, 0) |
| 104 | + self.assertEqual(results[1].outputs.index, 2) |
| 105 | + |
| 106 | + def test_process_batch_draft_tokens_empty_accept(self): |
| 107 | + """测试 accept_num 为 0 的情况""" |
| 108 | + batch = 2 |
| 109 | + accept_num = [0, 0] |
| 110 | + |
| 111 | + K = 20 |
| 112 | + MAX_DRAFT_TOKENS = 6 |
| 113 | + tokens = np.random.randint(100, 200, size=(batch, MAX_DRAFT_TOKENS, K + 1)) |
| 114 | + scores = np.random.rand(batch, MAX_DRAFT_TOKENS, K + 1).astype(np.float32) |
| 115 | + ranks = np.random.randint(0, K, size=(batch, MAX_DRAFT_TOKENS)) |
| 116 | + |
| 117 | + results = self.processor._process_batch_draft_tokens( |
| 118 | + mtype=4, |
| 119 | + batch=batch, |
| 120 | + accept_num=accept_num, |
| 121 | + tokens=paddle.to_tensor(tokens), |
| 122 | + scores=paddle.to_tensor(scores), |
| 123 | + ranks=paddle.to_tensor(ranks), |
| 124 | + ) |
| 125 | + |
| 126 | + self.assertEqual(len(results), batch) |
| 127 | + for result in results: |
| 128 | + self.assertIsNone(result.outputs.draft_top_logprobs) |
| 129 | + |
| 130 | + def test_process_batch_draft_tokens_different_k_values(self): |
| 131 | + """测试不同 K 值情况""" |
| 132 | + batch = 2 |
| 133 | + accept_num = [3, 2] |
| 134 | + |
| 135 | + K = 5 |
| 136 | + MAX_DRAFT_TOKENS = 6 |
| 137 | + tokens = np.random.randint(100, 200, size=(batch, MAX_DRAFT_TOKENS, K + 1)) |
| 138 | + scores = np.random.rand(batch, MAX_DRAFT_TOKENS, K + 1).astype(np.float32) |
| 139 | + ranks = np.random.randint(0, K, size=(batch, MAX_DRAFT_TOKENS)) |
| 140 | + |
| 141 | + results = self.processor._process_batch_draft_tokens( |
| 142 | + mtype=4, |
| 143 | + batch=batch, |
| 144 | + accept_num=accept_num, |
| 145 | + tokens=paddle.to_tensor(tokens), |
| 146 | + scores=paddle.to_tensor(scores), |
| 147 | + ranks=paddle.to_tensor(ranks), |
| 148 | + ) |
| 149 | + |
| 150 | + self.assertEqual(len(results), batch) |
| 151 | + for i, result in enumerate(results): |
| 152 | + self.assertEqual(len(result.outputs.draft_top_logprobs.logprob_token_ids[0]), K + 1) |
| 153 | + self.assertEqual(len(result.outputs.draft_top_logprobs.logprobs[0]), K + 1) |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + unittest.main() |
0 commit comments