|
| 1 | +""" |
| 2 | +Copyright (c) 2026 Baidu, Inc. All Rights Reserved. |
| 3 | +
|
| 4 | +This file is a part of the vllm-kunlun project. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from types import SimpleNamespace |
| 22 | +from unittest.mock import patch |
| 23 | + |
| 24 | +import numpy as np |
| 25 | +import torch |
| 26 | + |
| 27 | +import vllm_kunlun._kunlun # noqa: F401 |
| 28 | + |
| 29 | + |
| 30 | +def _reference_prepare_next_token_ids( |
| 31 | + sampled_token_ids: torch.Tensor, |
| 32 | + discard_request_indices: torch.Tensor, |
| 33 | + num_discarded_requests: int, |
| 34 | + backup_next_token_ids: torch.Tensor, |
| 35 | + vocab_size: int, |
| 36 | +) -> tuple[torch.Tensor, torch.Tensor]: |
| 37 | + valid_sampled_token_ids_gpu = sampled_token_ids.clone() |
| 38 | + if num_discarded_requests > 0: |
| 39 | + idx = discard_request_indices[:num_discarded_requests] |
| 40 | + if idx.device != valid_sampled_token_ids_gpu.device: |
| 41 | + idx = idx.to(valid_sampled_token_ids_gpu.device, non_blocking=True) |
| 42 | + if idx.dtype != torch.long: |
| 43 | + idx = idx.to(torch.long) |
| 44 | + if idx.numel() > 0: |
| 45 | + valid_sampled_token_ids_gpu.index_fill_(0, idx, -1) |
| 46 | + |
| 47 | + max_gen_len = sampled_token_ids.shape[-1] |
| 48 | + if max_gen_len == 1: |
| 49 | + valid_mask = torch.ones_like(valid_sampled_token_ids_gpu, dtype=torch.bool) |
| 50 | + else: |
| 51 | + valid_mask = (valid_sampled_token_ids_gpu != -1) & ( |
| 52 | + valid_sampled_token_ids_gpu < vocab_size |
| 53 | + ) |
| 54 | + |
| 55 | + valid_sampled_tokens_count = valid_mask.sum(dim=1) |
| 56 | + last_valid_indices = valid_sampled_tokens_count - 1 |
| 57 | + last_valid_indices_safe = torch.clamp(last_valid_indices, min=0) |
| 58 | + selected_tokens = torch.gather( |
| 59 | + valid_sampled_token_ids_gpu, 1, last_valid_indices_safe.unsqueeze(1) |
| 60 | + ).squeeze(1) |
| 61 | + next_token_ids = torch.where( |
| 62 | + last_valid_indices != -1, |
| 63 | + selected_tokens, |
| 64 | + backup_next_token_ids[: valid_sampled_token_ids_gpu.shape[0]], |
| 65 | + ) |
| 66 | + return next_token_ids, valid_sampled_tokens_count |
| 67 | + |
| 68 | + |
| 69 | +def test_eagle_prepare_next_token_ids_without_discards_matches_reference(): |
| 70 | + sampled = torch.tensor([[1, 3, 4], [5, 8, 9]], dtype=torch.int64) |
| 71 | + discard = torch.tensor([1, 0], dtype=torch.int32) |
| 72 | + backup = torch.tensor([10, 11], dtype=torch.int64) |
| 73 | + |
| 74 | + result = torch.ops._C.eagle_prepare_next_token_ids_padded( |
| 75 | + sampled, discard, 0, backup, 100 |
| 76 | + ) |
| 77 | + |
| 78 | + expected = _reference_prepare_next_token_ids(sampled, discard, 0, backup, 100) |
| 79 | + torch.testing.assert_close(result[0], expected[0]) |
| 80 | + torch.testing.assert_close(result[1], expected[1]) |
| 81 | + |
| 82 | + |
| 83 | +def test_eagle_prepare_next_token_ids_handles_partial_discards(): |
| 84 | + sampled = torch.tensor([[3, 4], [7, 8], [9, 10]], dtype=torch.int64) |
| 85 | + discard = torch.tensor([1, 2, 0], dtype=torch.int32) |
| 86 | + backup = torch.tensor([20, 21, 22], dtype=torch.int64) |
| 87 | + |
| 88 | + result = torch.ops._C.eagle_prepare_next_token_ids_padded( |
| 89 | + sampled, discard, 2, backup, 100 |
| 90 | + ) |
| 91 | + |
| 92 | + expected = _reference_prepare_next_token_ids(sampled, discard, 2, backup, 100) |
| 93 | + torch.testing.assert_close(result[0], expected[0]) |
| 94 | + torch.testing.assert_close(result[1], expected[1]) |
| 95 | + |
| 96 | + |
| 97 | +def test_eagle_prepare_next_token_ids_handles_all_discards(): |
| 98 | + sampled = torch.tensor([[3, 4], [7, 8]], dtype=torch.int64) |
| 99 | + discard = torch.tensor([0, 1], dtype=torch.int32) |
| 100 | + backup = torch.tensor([30, 31], dtype=torch.int64) |
| 101 | + |
| 102 | + result = torch.ops._C.eagle_prepare_next_token_ids_padded( |
| 103 | + sampled, discard, 2, backup, 100 |
| 104 | + ) |
| 105 | + |
| 106 | + expected = _reference_prepare_next_token_ids(sampled, discard, 2, backup, 100) |
| 107 | + torch.testing.assert_close(result[0], expected[0]) |
| 108 | + torch.testing.assert_close(result[1], expected[1]) |
| 109 | + |
| 110 | + |
| 111 | +def test_eagle_prepare_next_token_ids_treats_single_token_rows_as_valid(): |
| 112 | + sampled = torch.tensor([[-1], [999]], dtype=torch.int64) |
| 113 | + discard = torch.tensor([], dtype=torch.int32) |
| 114 | + backup = torch.tensor([40, 41], dtype=torch.int64) |
| 115 | + |
| 116 | + result = torch.ops._C.eagle_prepare_next_token_ids_padded( |
| 117 | + sampled, discard, 0, backup, 100 |
| 118 | + ) |
| 119 | + |
| 120 | + expected = _reference_prepare_next_token_ids(sampled, discard, 0, backup, 100) |
| 121 | + torch.testing.assert_close(result[0], expected[0]) |
| 122 | + torch.testing.assert_close(result[1], expected[1]) |
| 123 | + |
| 124 | + |
| 125 | +def test_eagle_prepare_next_token_ids_filters_invalid_tokens_and_falls_back(): |
| 126 | + sampled = torch.tensor([[-1, 2, 3], [101, 105, 2], [-1, -1, -1]], dtype=torch.int64) |
| 127 | + discard = torch.tensor([2], dtype=torch.int32) |
| 128 | + backup = torch.tensor([50, 51, 52], dtype=torch.int64) |
| 129 | + |
| 130 | + result = torch.ops._C.eagle_prepare_next_token_ids_padded( |
| 131 | + sampled, discard, 1, backup, 100 |
| 132 | + ) |
| 133 | + |
| 134 | + expected = _reference_prepare_next_token_ids(sampled, discard, 1, backup, 100) |
| 135 | + torch.testing.assert_close(result[0], expected[0]) |
| 136 | + torch.testing.assert_close(result[1], expected[1]) |
| 137 | + |
| 138 | + |
| 139 | +class _BackupNextTokenIds: |
| 140 | + def __init__(self, size: int): |
| 141 | + self.np = np.zeros(size, dtype=np.int64) |
| 142 | + self.gpu = torch.zeros(size, dtype=torch.int64) |
| 143 | + |
| 144 | + def copy_to_gpu(self, size: int) -> None: |
| 145 | + self.gpu[:size] = torch.from_numpy(self.np[:size]).to(self.gpu.dtype) |
| 146 | + |
| 147 | + |
| 148 | +class _Request: |
| 149 | + def __init__(self, token_id: int): |
| 150 | + self.token_id = token_id |
| 151 | + |
| 152 | + def get_token_id(self, _: int) -> int: |
| 153 | + return self.token_id |
| 154 | + |
| 155 | + |
| 156 | +def test_prepare_next_token_ids_padded_uses_cpp_op(): |
| 157 | + from vllm_kunlun.v1.sample.spec_decode import eagle as eagle_module |
| 158 | + |
| 159 | + sampled = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int64) |
| 160 | + discard = torch.tensor([1], dtype=torch.int32) |
| 161 | + expected_next = torch.tensor([3, 77], dtype=torch.int64) |
| 162 | + expected_counts = torch.tensor([3, 0], dtype=torch.int64) |
| 163 | + |
| 164 | + proposer = SimpleNamespace(backup_next_token_ids=_BackupNextTokenIds(2)) |
| 165 | + common_attn_metadata = SimpleNamespace(seq_lens_cpu=torch.tensor([4, 5])) |
| 166 | + gpu_input_batch = SimpleNamespace(num_reqs=2, req_ids=["a", "b"], vocab_size=100) |
| 167 | + requests = {"a": _Request(70), "b": _Request(77)} |
| 168 | + |
| 169 | + with patch.object( |
| 170 | + torch.ops._C, |
| 171 | + "eagle_prepare_next_token_ids_padded", |
| 172 | + return_value=(expected_next, expected_counts), |
| 173 | + create=True, |
| 174 | + ) as mocked: |
| 175 | + result = eagle_module.prepare_next_token_ids_padded( |
| 176 | + proposer, |
| 177 | + common_attn_metadata, |
| 178 | + sampled, |
| 179 | + requests, |
| 180 | + gpu_input_batch, |
| 181 | + discard, |
| 182 | + 1, |
| 183 | + ) |
| 184 | + |
| 185 | + assert proposer.backup_next_token_ids.np[:2].tolist() == [70, 77] |
| 186 | + mocked.assert_called_once() |
| 187 | + torch.testing.assert_close(result[0], expected_next) |
| 188 | + torch.testing.assert_close(result[1], expected_counts) |
0 commit comments