|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +from contextlib import contextmanager |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import torch |
| 7 | + |
| 8 | +from vllm.v1.outputs import ( |
| 9 | + AsyncModelRunnerOutput, |
| 10 | + ModelRunnerOutput, |
| 11 | + SamplerOutput, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class AsyncOutput(AsyncModelRunnerOutput): |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + model_runner_output: ModelRunnerOutput, |
| 19 | + sampler_output: SamplerOutput, |
| 20 | + num_sampled_tokens: np.ndarray, |
| 21 | + copy_stream: torch.cuda.Stream, |
| 22 | + copy_event: torch.cuda.Event, |
| 23 | + ): |
| 24 | + self.model_runner_output = model_runner_output |
| 25 | + self.sampler_output = sampler_output |
| 26 | + self.num_sampled_tokens = num_sampled_tokens |
| 27 | + self.copy_stream = copy_stream |
| 28 | + self.copy_event = copy_event |
| 29 | + |
| 30 | + default_stream = torch.cuda.current_stream() |
| 31 | + with torch.cuda.stream(self.copy_stream): |
| 32 | + self.copy_stream.wait_stream(default_stream) |
| 33 | + |
| 34 | + # NOTE(woosuk): We must ensure that CPU tensors are not freed |
| 35 | + # before the device-to-host copy is fully completed. For instance, |
| 36 | + # operations like |
| 37 | + # self.sampled_token_np = ...to("cpu", non_blocking=True).numpy() |
| 38 | + # are unsafe because the underlying CPU tensor can be prematurely freed and |
| 39 | + # reused by other tensors before the asynchronous copy finishes, potentially |
| 40 | + # causing race conditions. To prevent this, we delay freeing by holding |
| 41 | + # references until the copy event signals completion. |
| 42 | + # Likewise, we also need to keep the reference to the GPU tensors. |
| 43 | + # This is done by keeping the reference to sampler_output and |
| 44 | + # model_runner_output. |
| 45 | + self.sampled_token_ids = sampler_output.sampled_token_ids.to( |
| 46 | + "cpu", non_blocking=True |
| 47 | + ) |
| 48 | + if sampler_output.logprobs_tensors is not None: |
| 49 | + self.logprobs_tensors = ( |
| 50 | + sampler_output.logprobs_tensors.to_cpu_nonblocking() |
| 51 | + ) |
| 52 | + else: |
| 53 | + self.logprobs_tensors = None |
| 54 | + self.prompt_logprobs_dict = {} |
| 55 | + if self.model_runner_output.prompt_logprobs_dict: |
| 56 | + for k, v in self.model_runner_output.prompt_logprobs_dict.items(): |
| 57 | + self.prompt_logprobs_dict[k] = v.to_cpu_nonblocking() |
| 58 | + self.copy_event.record(self.copy_stream) |
| 59 | + |
| 60 | + def get_output(self) -> ModelRunnerOutput: |
| 61 | + self.copy_event.synchronize() |
| 62 | + |
| 63 | + # NOTE(woosuk): The following code is to ensure compatibility with |
| 64 | + # the existing model runner. |
| 65 | + # Going forward, we should keep the data structures as NumPy arrays |
| 66 | + # rather than Python lists. |
| 67 | + sampled_token_ids_np = self.sampled_token_ids.numpy() |
| 68 | + num_reqs = sampled_token_ids_np.shape[0] |
| 69 | + sampled_token_ids: list[np.ndarray] = [ |
| 70 | + sampled_token_ids_np[i, : self.num_sampled_tokens[i]] |
| 71 | + for i in range(num_reqs) |
| 72 | + ] |
| 73 | + self.model_runner_output.sampled_token_ids = sampled_token_ids |
| 74 | + |
| 75 | + if self.logprobs_tensors is not None: |
| 76 | + self.model_runner_output.logprobs = self.logprobs_tensors.tolists() |
| 77 | + self.model_runner_output.prompt_logprobs_dict = self.prompt_logprobs_dict |
| 78 | + return self.model_runner_output |
| 79 | + |
| 80 | + |
| 81 | +@contextmanager |
| 82 | +def async_barrier(event: torch.cuda.Event | None): |
| 83 | + if event is not None: |
| 84 | + event.synchronize() |
| 85 | + try: |
| 86 | + yield |
| 87 | + finally: |
| 88 | + if event is not None: |
| 89 | + event.record() |
0 commit comments