|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# This file is a part of the vllm-ascend project. |
| 14 | + |
| 15 | +from unittest.mock import MagicMock, patch |
| 16 | + |
| 17 | +import torch |
| 18 | +from vllm.model_executor.layers.sampler import SamplerOutput |
| 19 | +from vllm.sequence import ExecuteModelRequest, SequenceGroupMetadata |
| 20 | + |
| 21 | +from tests.ut.base import TestBase |
| 22 | +from vllm_ascend.patch.worker.patch_common.patch_multi_step_worker import \ |
| 23 | + sampler_output |
| 24 | + |
| 25 | + |
| 26 | +class TestPatchedMultiStepWorkerSamplerOutput(TestBase): |
| 27 | + |
| 28 | + def setUp(self): |
| 29 | + self.mock_self = MagicMock() |
| 30 | + |
| 31 | + self.mock_self.device = torch.device("cpu") |
| 32 | + |
| 33 | + self.mock_self._raise_if_unsupported = MagicMock() |
| 34 | + self.mock_self._expand_execute_model_request = MagicMock() |
| 35 | + self.mock_self.execute_model = MagicMock() |
| 36 | + self.mock_self._maybe_update_previous_hidden_states = MagicMock() |
| 37 | + self.mock_self._append_new_tokens = MagicMock() |
| 38 | + self.mock_self._filter_model_output = MagicMock() |
| 39 | + |
| 40 | + self.execute_model_req = ExecuteModelRequest( |
| 41 | + seq_group_metadata_list=[MagicMock(spec=SequenceGroupMetadata)], |
| 42 | + num_steps=1, |
| 43 | + blocks_to_swap_in={}, |
| 44 | + blocks_to_swap_out={}, |
| 45 | + blocks_to_copy={}, |
| 46 | + num_lookahead_slots=0) |
| 47 | + self.sample_len = 3 |
| 48 | + self.seq_ids_with_bonus_token = {1, 2, 3} |
| 49 | + |
| 50 | + self.expanded_request = MagicMock(spec=ExecuteModelRequest) |
| 51 | + self.indices_of_seq_with_bonus_tokens = [0, 1, 2] |
| 52 | + self.mock_self._expand_execute_model_request.return_value = ( |
| 53 | + self.expanded_request, self.indices_of_seq_with_bonus_tokens) |
| 54 | + |
| 55 | + self.filtered_output = [ |
| 56 | + MagicMock(spec=SamplerOutput), |
| 57 | + MagicMock(spec=SamplerOutput), |
| 58 | + MagicMock(spec=SamplerOutput) |
| 59 | + ] |
| 60 | + self.mock_self._filter_model_output.return_value = self.filtered_output |
| 61 | + |
| 62 | + def test_sampler_output_patched(self): |
| 63 | + from vllm.spec_decode.multi_step_worker import MultiStepWorker |
| 64 | + |
| 65 | + wrapped_func = MultiStepWorker.sampler_output.__wrapped__ |
| 66 | + self.assertIs( |
| 67 | + wrapped_func, sampler_output, |
| 68 | + "Wrapped function does not match the expected implementation") |
| 69 | + |
| 70 | + def test_gpu_multi_step_path(self): |
| 71 | + mock_model_runner = MagicMock() |
| 72 | + mock_model_runner.supports_gpu_multi_step.return_value = True |
| 73 | + |
| 74 | + self.mock_self.model_runner = mock_model_runner |
| 75 | + with patch( |
| 76 | + 'vllm_ascend.patch.worker.patch_common.patch_multi_step_worker.isinstance' |
| 77 | + ) as mock_isinstance: |
| 78 | + mock_isinstance.return_value = True |
| 79 | + |
| 80 | + mock_outputs = [ |
| 81 | + MagicMock(spec=SamplerOutput), |
| 82 | + MagicMock(spec=SamplerOutput), |
| 83 | + MagicMock(spec=SamplerOutput) |
| 84 | + ] |
| 85 | + self.mock_self.execute_model.return_value = mock_outputs |
| 86 | + |
| 87 | + result, need_transpose = sampler_output( |
| 88 | + self.mock_self, self.execute_model_req, self.sample_len, |
| 89 | + self.seq_ids_with_bonus_token) |
| 90 | + |
| 91 | + self.mock_self._raise_if_unsupported.assert_called_once_with( |
| 92 | + self.execute_model_req) |
| 93 | + self.mock_self._expand_execute_model_request.assert_called_once_with( |
| 94 | + self.execute_model_req, self.seq_ids_with_bonus_token) |
| 95 | + |
| 96 | + mock_model_runner.supports_gpu_multi_step.assert_called_once_with( |
| 97 | + self.expanded_request) |
| 98 | + self.assertEqual(self.expanded_request.num_steps, self.sample_len) |
| 99 | + mock_model_runner.set_indices_of_seq_with_bonus_tokens.assert_called_once_with( |
| 100 | + self.indices_of_seq_with_bonus_tokens) |
| 101 | + self.mock_self.execute_model.assert_called_once_with( |
| 102 | + execute_model_req=self.expanded_request) |
| 103 | + |
| 104 | + self.assertEqual(result, self.filtered_output) |
| 105 | + self.assertTrue(need_transpose) |
| 106 | + |
| 107 | + self.mock_self._maybe_update_previous_hidden_states.assert_not_called() |
| 108 | + self.mock_self._append_new_tokens.assert_not_called() |
| 109 | + |
| 110 | + def test_cpu_multi_step_path(self): |
| 111 | + mock_model_runner = MagicMock() |
| 112 | + mock_model_runner.supports_gpu_multi_step.return_value = False |
| 113 | + |
| 114 | + self.mock_self.model_runner = mock_model_runner |
| 115 | + self.mock_self.worker = MagicMock() |
| 116 | + |
| 117 | + mock_step_output = MagicMock(spec=SamplerOutput) |
| 118 | + self.mock_self.worker.execute_model.return_value = [[mock_step_output]] |
| 119 | + |
| 120 | + result, need_transpose = sampler_output(self.mock_self, |
| 121 | + self.execute_model_req, |
| 122 | + self.sample_len, |
| 123 | + self.seq_ids_with_bonus_token) |
| 124 | + |
| 125 | + self.assertEqual(self.mock_self.worker.execute_model.call_count, |
| 126 | + self.sample_len) |
| 127 | + self.mock_self._append_new_tokens.assert_called() |
| 128 | + self.assertEqual(self.mock_self._append_new_tokens.call_count, |
| 129 | + self.sample_len) |
| 130 | + |
| 131 | + self.mock_self._filter_model_output.assert_called_once() |
| 132 | + self.assertEqual(result, self.filtered_output) |
| 133 | + self.assertTrue(need_transpose) |
| 134 | + |
| 135 | + def test_cpu_path_with_hidden_states(self): |
| 136 | + self.expanded_request.previous_hidden_states = MagicMock() |
| 137 | + |
| 138 | + mock_model_runner = MagicMock() |
| 139 | + mock_model_runner.supports_gpu_multi_step.return_value = False |
| 140 | + self.mock_self.model_runner = mock_model_runner |
| 141 | + self.mock_self.worker = MagicMock() |
| 142 | + |
| 143 | + self.mock_self.worker.model_runner = MagicMock() |
| 144 | + self.mock_self.worker.model_runner.return_hidden_states = False |
| 145 | + |
| 146 | + mock_step_output = MagicMock(spec=SamplerOutput) |
| 147 | + self.mock_self.worker.execute_model.return_value = [[mock_step_output]] |
| 148 | + |
| 149 | + sampler_output(self.mock_self, self.execute_model_req, self.sample_len, |
| 150 | + self.seq_ids_with_bonus_token) |
| 151 | + |
| 152 | + self.assertTrue( |
| 153 | + self.mock_self.worker.model_runner.return_hidden_states) |
| 154 | + self.mock_self._maybe_update_previous_hidden_states.assert_called() |
0 commit comments