|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +import os |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +from vllm.config import VllmConfig |
| 6 | +from vllm.engine.arg_utils import EngineArgs |
| 7 | +from vllm.platforms import current_platform |
| 8 | +from vllm.platforms.neuron import NeuronFramework |
| 9 | +from vllm.sampling_params import SamplingParams |
| 10 | +from vllm.sequence import SequenceData, SequenceGroupMetadata |
| 11 | +from vllm.worker.neuron_model_runner import NeuronModelRunner |
| 12 | + |
| 13 | +os.environ[ |
| 14 | + 'VLLM_NEURON_FRAMEWORK'] = NeuronFramework.TRANSFORMERS_NEURONX.value |
| 15 | + |
| 16 | + |
| 17 | +def _create_neuron_model_runner(model: str, *args, |
| 18 | + **kwargs) -> NeuronModelRunner: |
| 19 | + engine_args = EngineArgs(model, *args, **kwargs) |
| 20 | + engine_config = engine_args.create_engine_config() |
| 21 | + vllm_config = VllmConfig( |
| 22 | + model_config=engine_config.model_config, |
| 23 | + parallel_config=engine_config.parallel_config, |
| 24 | + scheduler_config=engine_config.scheduler_config, |
| 25 | + device_config=engine_config.device_config, |
| 26 | + ) |
| 27 | + neuron_model_runner = NeuronModelRunner(vllm_config=vllm_config) |
| 28 | + return neuron_model_runner |
| 29 | + |
| 30 | + |
| 31 | +def test_update_neuron_sampling_params_not_full_batch(): |
| 32 | + os.environ["NEURON_ON_DEVICE_SAMPLING_DISABLED"] = "0" |
| 33 | + model_runner = _create_neuron_model_runner( |
| 34 | + "facebook/opt-125m", |
| 35 | + seed=0, |
| 36 | + dtype="float16", |
| 37 | + max_num_seqs=2, |
| 38 | + ) |
| 39 | + assert not model_runner._on_device_sampling_disabled |
| 40 | + # Test sampling param updating only when TNx is framework |
| 41 | + # NxDI handles sampling parameter updating inside model |
| 42 | + if current_platform.use_transformers_neuronx(): |
| 43 | + model_mock = MagicMock() |
| 44 | + model_runner.model = model_mock |
| 45 | + |
| 46 | + seq_group_metadata_list = [ |
| 47 | + SequenceGroupMetadata( |
| 48 | + request_id="test_0", |
| 49 | + is_prompt=True, |
| 50 | + seq_data={0: SequenceData.from_seqs([1, 2, 3])}, |
| 51 | + sampling_params=SamplingParams(temperature=0.5, |
| 52 | + top_k=1, |
| 53 | + top_p=0.5), |
| 54 | + block_tables={0: [1]}, |
| 55 | + ) |
| 56 | + ] |
| 57 | + |
| 58 | + model_runner.prepare_model_input(seq_group_metadata_list) |
| 59 | + |
| 60 | + # Index neuron sampling parameters based on block_tables indices. |
| 61 | + # The first block_id of the sequence 0 is 1, so its parameters are |
| 62 | + # placed at index 1. So the sampling parameters will be: |
| 63 | + # Index 0: default sampling parameters |
| 64 | + # Index 1: sequecne 0's sampling parameters. |
| 65 | + neuron_sampling_params = ( |
| 66 | + model_runner.model_config.neuron_sampling_params) |
| 67 | + assert neuron_sampling_params.temperature == [1.0, 0.5] |
| 68 | + assert neuron_sampling_params.top_k == [ |
| 69 | + model_runner._MAX_NEURON_SAMPLING_TOP_K, 1 |
| 70 | + ] |
| 71 | + assert neuron_sampling_params.top_p == [1.0, 0.5] |
| 72 | + model_mock.model.update_generation_config.assert_called_once_with( |
| 73 | + neuron_sampling_params) |
| 74 | + |
| 75 | + |
| 76 | +def test_update_neuron_sampling_params_full_batch(): |
| 77 | + os.environ["NEURON_ON_DEVICE_SAMPLING_DISABLED"] = "0" |
| 78 | + model_runner = _create_neuron_model_runner( |
| 79 | + "facebook/opt-125m", |
| 80 | + seed=0, |
| 81 | + dtype="float16", |
| 82 | + max_num_seqs=2, |
| 83 | + ) |
| 84 | + assert not model_runner._on_device_sampling_disabled |
| 85 | + |
| 86 | + # Test sampling param updating only when TNx is framework |
| 87 | + # NxDI handles sampling parameter updating inside model |
| 88 | + if current_platform.use_transformers_neuronx(): |
| 89 | + model_mock = MagicMock() |
| 90 | + model_runner.model = model_mock |
| 91 | + |
| 92 | + seq_group_metadata_list = [ |
| 93 | + SequenceGroupMetadata( |
| 94 | + request_id="test_0", |
| 95 | + is_prompt=True, |
| 96 | + seq_data={0: SequenceData.from_seqs([1, 2, 3])}, |
| 97 | + sampling_params=SamplingParams(temperature=0.5, |
| 98 | + top_k=1, |
| 99 | + top_p=0.5), |
| 100 | + block_tables={0: [1]}, |
| 101 | + ), |
| 102 | + SequenceGroupMetadata( |
| 103 | + request_id="test_0", |
| 104 | + is_prompt=True, |
| 105 | + seq_data={1: SequenceData.from_seqs([4, 5, 6])}, |
| 106 | + sampling_params=SamplingParams(temperature=0.2, |
| 107 | + top_k=2, |
| 108 | + top_p=0.2), |
| 109 | + block_tables={1: [0]}, |
| 110 | + ) |
| 111 | + ] |
| 112 | + |
| 113 | + model_runner.prepare_model_input(seq_group_metadata_list) |
| 114 | + |
| 115 | + # Index neuron sampling parameters based on block_tables indices. |
| 116 | + # The first block_id of the sequence 0 is 1, so its parameters are |
| 117 | + # placed at index 1. So the sampling parameters will be: |
| 118 | + # Index 0: sequence 1's sampling parameters |
| 119 | + # Index 1: sequecne 0's sampling parameters. |
| 120 | + neuron_sampling_params = ( |
| 121 | + model_runner.model_config.neuron_sampling_params) |
| 122 | + assert neuron_sampling_params.temperature == [0.2, 0.5] |
| 123 | + assert neuron_sampling_params.top_k == [2, 1] |
| 124 | + assert neuron_sampling_params.top_p == [0.2, 0.5] |
| 125 | + model_mock.model.update_generation_config.assert_called_once_with( |
| 126 | + neuron_sampling_params) |
0 commit comments