outputs.py 4.04 KB
Newer Older
Zhuohan Li's avatar
Zhuohan Li committed
1
from typing import Dict, List, Optional
2

Woosuk Kwon's avatar
Woosuk Kwon committed
3
from vllm.sequence import SequenceGroup, SequenceStatus
4
5
6


class CompletionOutput:
Zhuohan Li's avatar
Zhuohan Li committed
7
8
9
10
11
12
13
14
15
16
17
18
    """The output data of one completion output of a request.

    Args:
        index: The index of the output in the request.
        text: The generated output text.
        token_ids: The token IDs of the generated output text.
        cumulative_logprob: The cumulative log probability of the generated
            output text.
        logprobs: The log probabilities of the top probability words at each
            position if the logprobs are requested.
        finish_reason: The reason why the sequence is finished.
    """
19
20
21

    def __init__(
        self,
22
        index: int,
23
24
        text: str,
        token_ids: List[int],
25
        cumulative_logprob: float,
Zhuohan Li's avatar
Zhuohan Li committed
26
        logprobs: Optional[List[Dict[int, float]]],
Zhuohan Li's avatar
Zhuohan Li committed
27
        finish_reason: Optional[str] = None,
28
    ) -> None:
29
        self.index = index
30
31
        self.text = text
        self.token_ids = token_ids
32
        self.cumulative_logprob = cumulative_logprob
33
        self.logprobs = logprobs
Zhuohan Li's avatar
Zhuohan Li committed
34
35
36
37
        self.finish_reason = finish_reason

    def finished(self) -> bool:
        return self.finish_reason is not None
38
39

    def __repr__(self) -> str:
40
41
        return (f"CompletionOutput(index={self.index}, "
                f"text={self.text!r}, "
42
                f"token_ids={self.token_ids}, "
43
                f"cumulative_logprob={self.cumulative_logprob}, "
44
                f"logprobs={self.logprobs}, "
Zhuohan Li's avatar
Zhuohan Li committed
45
                f"finish_reason={self.finish_reason})")
46
47
48


class RequestOutput:
Zhuohan Li's avatar
Zhuohan Li committed
49
50
51
52
53
54
55
    """The output data of a request to the LLM.

    Args:
        request_id: The unique ID of the request.
        prompt: The prompt string of the request.
        prompt_token_ids: The token IDs of the prompt.
        outputs: The output sequences of the request.
56
        finished: Whether the whole request is finished.
Zhuohan Li's avatar
Zhuohan Li committed
57
    """
58

59
60
    def __init__(
        self,
61
        request_id: str,
62
63
64
        prompt: str,
        prompt_token_ids: List[int],
        outputs: List[CompletionOutput],
65
        finished: bool,
66
67
68
69
70
    ) -> None:
        self.request_id = request_id
        self.prompt = prompt
        self.prompt_token_ids = prompt_token_ids
        self.outputs = outputs
71
        self.finished = finished
72

73
74
    @classmethod
    def from_seq_group(cls, seq_group: SequenceGroup) -> "RequestOutput":
75
76
        # Get the top-n sequences.
        n = seq_group.sampling_params.n
77
        seqs = seq_group.get_seqs()
78
        assert n <= len(seqs)
79
80
81
        sorted_seqs = sorted(seqs,
                             key=lambda seq: seq.get_cumulative_logprob(),
                             reverse=True)
82
        top_n_seqs = sorted_seqs[:n]
83

84
85
86
        # Create the outputs.
        outputs: List[CompletionOutput] = []
        for seq in top_n_seqs:
87
            logprobs = seq.output_logprobs
Zhuohan Li's avatar
Zhuohan Li committed
88
            if seq_group.sampling_params.logprobs is None:
89
90
91
92
                # NOTE: We need to take care of this case because the sequence
                # always has the logprobs of the sampled tokens even if the
                # logprobs are not requested.
                logprobs = {}
Zhuohan Li's avatar
Zhuohan Li committed
93
            finshed_reason = SequenceStatus.get_finished_reason(seq.status)
94
95
            output = CompletionOutput(seqs.index(seq), seq.output_text,
                                      seq.get_output_token_ids(),
Zhuohan Li's avatar
Zhuohan Li committed
96
97
                                      seq.get_cumulative_logprob(), logprobs,
                                      finshed_reason)
98
99
100
            outputs.append(output)

        # Every sequence in the sequence group should have the same prompt.
101
102
        prompt = top_n_seqs[0].prompt
        prompt_token_ids = top_n_seqs[0].data.prompt_token_ids
103
104
105
        finished = seq_group.is_finished()
        return cls(seq_group.request_id, prompt, prompt_token_ids, outputs,
                   finished)
106
107
108
109
110

    def __repr__(self) -> str:
        return (f"RequestOutput(request_id={self.request_id}, "
                f"prompt={self.prompt!r}, "
                f"prompt_token_ids={self.prompt_token_ids}, "
111
112
                f"outputs={self.outputs}, "
                f"finished={self.finished})")