async_utils.py 3.12 KB
Newer Older
Woosuk Kwon's avatar
Woosuk Kwon committed
1
2
3
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

4
import numpy as np
Woosuk Kwon's avatar
Woosuk Kwon committed
5
6
7
8
import torch

from vllm.v1.outputs import (
    AsyncModelRunnerOutput,
9
    LogprobsTensors,
Woosuk Kwon's avatar
Woosuk Kwon committed
10
11
    ModelRunnerOutput,
)
12
from vllm.v1.worker.gpu.sample.output import SamplerOutput
Woosuk Kwon's avatar
Woosuk Kwon committed
13
14
15
16
17
18
19


class AsyncOutput(AsyncModelRunnerOutput):
    def __init__(
        self,
        model_runner_output: ModelRunnerOutput,
        sampler_output: SamplerOutput,
20
        num_sampled_tokens: torch.Tensor,
Woosuk Kwon's avatar
Woosuk Kwon committed
21
22
23
        copy_stream: torch.cuda.Stream,
        copy_event: torch.cuda.Event,
    ):
24
25
26
        # NOTE(woosuk): We must retain references to the GPU tensors,
        # as the copy operations are performed on a different CUDA stream than
        # the one where the tensors were created.
Woosuk Kwon's avatar
Woosuk Kwon committed
27
28
29
30
31
32
        self.model_runner_output = model_runner_output
        self.sampler_output = sampler_output
        self.num_sampled_tokens = num_sampled_tokens
        self.copy_event = copy_event

        default_stream = torch.cuda.current_stream()
33
34
        with torch.cuda.stream(copy_stream):
            copy_stream.wait_stream(default_stream)
Woosuk Kwon's avatar
Woosuk Kwon committed
35

36
            self.sampled_token_ids = async_copy_to_np(sampler_output.sampled_token_ids)
37
            self.logprobs_tensors: LogprobsTensors | None = None
Woosuk Kwon's avatar
Woosuk Kwon committed
38
            if sampler_output.logprobs_tensors is not None:
39
                self.logprobs_tensors = (
Woosuk Kwon's avatar
Woosuk Kwon committed
40
41
                    sampler_output.logprobs_tensors.to_cpu_nonblocking()
                )
42
            self.num_nans: np.ndarray | None = None
43
44
45
            if sampler_output.num_nans is not None:
                self.num_nans = async_copy_to_np(sampler_output.num_nans)
            self.num_sampled_tokens_np = async_copy_to_np(num_sampled_tokens)
46
47
48
49
50
            self.prompt_logprobs_dict = {
                k: v.to_cpu_nonblocking() if v is not None else None
                for k, v in self.model_runner_output.prompt_logprobs_dict.items()
            }
            self.copy_event.record(copy_stream)
Woosuk Kwon's avatar
Woosuk Kwon committed
51
52
53
54
55
56
57
58

    def get_output(self) -> ModelRunnerOutput:
        self.copy_event.synchronize()

        # NOTE(woosuk): The following code is to ensure compatibility with
        # the existing model runner.
        # Going forward, we should keep the data structures as NumPy arrays
        # rather than Python lists.
59
        sampled_token_ids: list[list[int]] = self.sampled_token_ids.tolist()
60
61
62
        num_sampled_tokens: list[int] = self.num_sampled_tokens_np.tolist()
        for token_ids, num_tokens in zip(sampled_token_ids, num_sampled_tokens):
            del token_ids[num_tokens:]
Woosuk Kwon's avatar
Woosuk Kwon committed
63
64
        self.model_runner_output.sampled_token_ids = sampled_token_ids

65
        if self.num_nans is not None:
66
67
68
            self.model_runner_output.num_nans_in_logits = dict(
                zip(self.model_runner_output.req_ids, self.num_nans.tolist())
            )
69

Woosuk Kwon's avatar
Woosuk Kwon committed
70
71
72
73
74
75
        if self.logprobs_tensors is not None:
            self.model_runner_output.logprobs = self.logprobs_tensors.tolists()
        self.model_runner_output.prompt_logprobs_dict = self.prompt_logprobs_dict
        return self.model_runner_output


76
77
def async_copy_to_np(x: torch.Tensor) -> np.ndarray:
    return x.to("cpu", non_blocking=True).numpy()