|
| 1 | +from typing import List, Optional |
| 2 | + |
| 3 | +import torch |
| 4 | + |
| 5 | +from vllm.config import (CacheConfig, DeviceConfig, LoadConfig, LoRAConfig, |
| 6 | + ModelConfig, ParallelConfig, SchedulerConfig, |
| 7 | + VisionLanguageConfig) |
| 8 | +from vllm.logger import init_logger |
| 9 | +from vllm.sequence import SamplerOutput, SequenceGroupMetadata |
| 10 | +from vllm.worker.model_runner import (ModelInputForGPUWithSamplingMetadata, |
| 11 | + ModelRunner) |
| 12 | + |
| 13 | +logger = init_logger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class TP1DraftModelRunner(ModelRunner): |
| 17 | + """Specialized model runner for speculative decoding draft model. |
| 18 | + Since the draft model always execute k forward passes consecutively to |
| 19 | + generate k speculative tokens in a single speculative decoding step, |
| 20 | + we could get rid of most CPU-GPU synchronization and data transfer |
| 21 | + overheads by keeping model input and output tensors on GPU all the time. |
| 22 | +
|
| 23 | + This runner is still under development so there's no performance gain |
| 24 | + at this moment. Currently we adopt a temporary solution that caches the |
| 25 | + seq_group_metadata_list for multi-step execution, so that we can |
| 26 | + leverage existing prepare_model_input to be compatible with the current |
| 27 | + execution flow, but we plan to remove this cache and avoid calling |
| 28 | + prepare_model_input in execute_model at all. |
| 29 | + |
| 30 | + The detail development plan includes: |
| 31 | + 1. Use "update_model_input" to update existing model_input without |
| 32 | + creating a new one. |
| 33 | + 2. Improve the performance of "update_model_input" with a GPU kernel. |
| 34 | + 3. Support TP > 1 (this requires some designs because we do not expect |
| 35 | + any broadcasting inside execute_model). |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__( |
| 39 | + self, |
| 40 | + model_config: ModelConfig, |
| 41 | + parallel_config: ParallelConfig, |
| 42 | + scheduler_config: SchedulerConfig, |
| 43 | + device_config: DeviceConfig, |
| 44 | + cache_config: CacheConfig, |
| 45 | + load_config: LoadConfig, |
| 46 | + lora_config: Optional[LoRAConfig], |
| 47 | + kv_cache_dtype: Optional[str] = "auto", |
| 48 | + is_driver_worker: bool = False, |
| 49 | + vision_language_config: Optional[VisionLanguageConfig] = None, |
| 50 | + return_hidden_states: bool = False, |
| 51 | + ): |
| 52 | + if return_hidden_states: |
| 53 | + raise ValueError( |
| 54 | + "return_hidden_states is not supported for TP1DraftModelRunner." |
| 55 | + ) |
| 56 | + |
| 57 | + super().__init__( |
| 58 | + model_config=model_config, |
| 59 | + parallel_config=parallel_config, |
| 60 | + scheduler_config=scheduler_config, |
| 61 | + device_config=device_config, |
| 62 | + cache_config=cache_config, |
| 63 | + load_config=load_config, |
| 64 | + lora_config=lora_config, |
| 65 | + kv_cache_dtype=kv_cache_dtype, |
| 66 | + is_driver_worker=is_driver_worker, |
| 67 | + vision_language_config=vision_language_config, |
| 68 | + return_hidden_states=return_hidden_states, |
| 69 | + ) |
| 70 | + |
| 71 | + # TODO: Remove this cache when we are able to update model_input |
| 72 | + # directly in advance_step. |
| 73 | + self.cached_seq_group_metadata_list: Optional[ |
| 74 | + List[SequenceGroupMetadata]] = None |
| 75 | + |
| 76 | + def prepare_model_input( |
| 77 | + self, |
| 78 | + seq_group_metadata_list: List[SequenceGroupMetadata], |
| 79 | + ) -> ModelInputForGPUWithSamplingMetadata: |
| 80 | + """A temporary solution that caches the seq_group_metadata_list |
| 81 | + for multi-step execution. |
| 82 | + TODO: In-place update model_input and remove this function. |
| 83 | + """ |
| 84 | + self.cached_seq_group_metadata_list = seq_group_metadata_list |
| 85 | + return super().prepare_model_input(seq_group_metadata_list) |
| 86 | + |
| 87 | + def update_model_input( |
| 88 | + self, model_input: ModelInputForGPUWithSamplingMetadata, |
| 89 | + last_output: SamplerOutput |
| 90 | + ) -> ModelInputForGPUWithSamplingMetadata: |
| 91 | + """Prepare the model inputs for the next step. |
| 92 | + TODO: In-place update model_input instead of calling |
| 93 | + prepare_model_input. |
| 94 | + """ |
| 95 | + |
| 96 | + # Append the output token to the sequence data. |
| 97 | + assert self.cached_seq_group_metadata_list is not None |
| 98 | + for seq_group_metadata, sequence_group_outputs in zip( |
| 99 | + self.cached_seq_group_metadata_list, last_output.outputs): |
| 100 | + seq_group_metadata.is_prompt = False |
| 101 | + |
| 102 | + for seq_output in sequence_group_outputs.samples: |
| 103 | + seq = seq_group_metadata.seq_data[seq_output.parent_seq_id] |
| 104 | + |
| 105 | + token_id = seq_output.output_token |
| 106 | + token_logprob = seq_output.logprobs[token_id] |
| 107 | + |
| 108 | + seq.append_token_id(token_id, token_logprob.logprob) |
| 109 | + seq.update_num_computed_tokens(1) |
| 110 | + |
| 111 | + return self.prepare_model_input(self.cached_seq_group_metadata_list) |
| 112 | + |
| 113 | + @torch.inference_mode() |
| 114 | + def execute_model( |
| 115 | + self, |
| 116 | + model_input: ModelInputForGPUWithSamplingMetadata, |
| 117 | + kv_caches: List[torch.Tensor], |
| 118 | + num_steps: int = 1, |
| 119 | + ) -> Optional[List[SamplerOutput]]: |
| 120 | + # Since we do not broadcast data inside execute_model anymore, |
| 121 | + # we need to figure out the best way to support TP > 1 in this |
| 122 | + # case, because we will at least need to broadcast the sampled |
| 123 | + # tokens to all workers. |
| 124 | + if not self.is_driver_worker: |
| 125 | + raise ValueError("TP1DraftModelRunner only supports TP=1.") |
| 126 | + |
| 127 | + if self.lora_config: |
| 128 | + assert model_input.lora_requests is not None |
| 129 | + assert model_input.lora_mapping is not None |
| 130 | + self.set_active_loras(model_input.lora_requests, |
| 131 | + model_input.lora_mapping) |
| 132 | + |
| 133 | + outputs: List[SamplerOutput] = [] |
| 134 | + for step in range(num_steps): |
| 135 | + # Currently cuda graph is only supported by the decode phase. |
| 136 | + assert model_input.attn_metadata is not None |
| 137 | + prefill_meta = model_input.attn_metadata.prefill_metadata |
| 138 | + decode_meta = model_input.attn_metadata.decode_metadata |
| 139 | + if prefill_meta is None and decode_meta.use_cuda_graph: |
| 140 | + assert model_input.input_tokens is not None |
| 141 | + graph_batch_size = model_input.input_tokens.shape[0] |
| 142 | + model_executable = self.graph_runners[graph_batch_size] |
| 143 | + else: |
| 144 | + model_executable = self.model |
| 145 | + |
| 146 | + multi_modal_kwargs = model_input.multi_modal_kwargs or {} |
| 147 | + hidden_states = model_executable( |
| 148 | + input_ids=model_input.input_tokens, |
| 149 | + positions=model_input.input_positions, |
| 150 | + kv_caches=kv_caches, |
| 151 | + attn_metadata=model_input.attn_metadata, |
| 152 | + **multi_modal_kwargs, |
| 153 | + ) |
| 154 | + |
| 155 | + # Compute the logits. |
| 156 | + logits = self.model.compute_logits(hidden_states, |
| 157 | + model_input.sampling_metadata) |
| 158 | + |
| 159 | + # Sample the next token. |
| 160 | + outputs.append( |
| 161 | + self.model.sample( |
| 162 | + logits=logits, |
| 163 | + sampling_metadata=model_input.sampling_metadata, |
| 164 | + )) |
| 165 | + |
| 166 | + # Prepare the inputs for the next step. |
| 167 | + if step != num_steps - 1: |
| 168 | + model_input = self.update_model_input(model_input, outputs[-1]) |
| 169 | + |
| 170 | + return outputs |
0 commit comments