parallel_sampling.py 5.19 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4

from copy import copy
5
from typing import Optional, cast
6

7
8
from vllm.outputs import CompletionOutput
from vllm.sampling_params import RequestOutputKind, SamplingParams
9
from vllm.v1.metrics.stats import IterationStats
10
11


12
class ParentRequest:
13
    """Info, state & processing for parallel sampling request.
14

15
16
17
18
19
20
    Store parent request ID and sampling params.
    Facilitate generating child request sampling params.
    """

    request_id: str
    sampling_params: SamplingParams
21

22
23
24
    # To track the completion of child requests
    child_requests: set[str]

25
    # To aggregate child completions when not streaming
26
    output_aggregator: list[CompletionOutput]
27

28
29
30
    # To find the max number of generated tokens across all children
    max_num_generation_tokens: int

31
    # To efficiently obtain child sampling params
32
    cached_child_sampling_params: SamplingParams | None
33

34
    def __init__(self, request_id: str, sampling_params: SamplingParams) -> None:
35
36
        self.request_id = request_id
        self.sampling_params = sampling_params
37

38
        self.child_requests = set()
39
        self.output_aggregator = (
40
            [cast(CompletionOutput, None)] * sampling_params.n
41
42
43
            if (sampling_params.output_kind == RequestOutputKind.FINAL_ONLY)
            else []
        )
44
        self.max_num_generation_tokens = 0
45
        self.cached_child_sampling_params = None
46

47
48
49
50
51
52
    def _get_child_sampling_params(
        self,
        index: int,
    ) -> SamplingParams:
        """Efficiently obtain child `sampling_params`

53
        If `sampling_params.seed` is not `None` then
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        each child request requires a unique clone of
        parent `sampling_params` with a unique seed.

        Args:
          index: index within `n` child requests

        Returns:
          Child `sampling_params` instance.
        """
        seed = self.sampling_params.seed
        if self.cached_child_sampling_params:
            # Reuse child sampling_params data structure
            return self.cached_child_sampling_params
        # Build child sampling_params
        child_sampling_params = copy(self.sampling_params)
        child_sampling_params.n = 1
        if seed is None:
            # Cache child sampling_params for later reuse
            self.cached_child_sampling_params = child_sampling_params
        else:
            # Each child gets a clone with a unique seed
            child_sampling_params.seed = seed + index
        return child_sampling_params

78
    def get_child_info(self, index: int) -> tuple[str, SamplingParams]:
79
        """Get child request ID and sampling params.
80

81
82
        Args:
          index: index within `n` child requests.
83

84
85
86
        Returns:
          (request ID, sampling_params) tuple
        """
87
88
        child_req_id = f"{index}_{self.request_id}"
        self.child_requests.add(child_req_id)
89
        return child_req_id, self._get_child_sampling_params(index)
90
91
92
93
94

    @property
    def n(self) -> int:
        return self.sampling_params.n

95
    def get_outputs(
96
        self,
97
        child_request_id: str,
98
        completion_output: CompletionOutput,
99
    ) -> tuple[str, list[CompletionOutput], bool]:
100
        already_finished_and_returned: bool = False
101
        if completion_output.finished():
102
103
104
105
106
107
108
            if child_request_id in self.child_requests:
                self.child_requests.remove(child_request_id)
            else:
                # child request ID is not available in child_requests
                # which means the request had finished in previous
                # batch step and returned to the client earlier
                already_finished_and_returned = True
109

110
        if self.sampling_params.output_kind != RequestOutputKind.FINAL_ONLY:
111
112
113
114
            # If streaming, just return the current output
            #
            # DO NOT output finished and already returned child request to client again
            outputs = [] if already_finished_and_returned else [completion_output]
115
116
117
118
        else:
            # If not streaming, aggregate the n final outputs.
            self.output_aggregator[completion_output.index] = completion_output
            outputs = [] if self.child_requests else self.output_aggregator
119

120
121
        finished = not self.child_requests
        return self.request_id, outputs, finished
122
123

    def observe_num_generation_tokens(self, num_generation_tokens: int):
124
125
126
        self.max_num_generation_tokens = max(
            num_generation_tokens, self.max_num_generation_tokens
        )
127
128
129
        return self.max_num_generation_tokens

    @staticmethod
130
131
132
133
134
    def observe_finished_request(
        parent_req: Optional["ParentRequest"],
        iteration_stats: IterationStats,
        num_generation_tokens: int,
    ):
135
136
137
138
        n_param = parent_req.n if parent_req is not None else 1

        if parent_req is not None:
            num_generation_tokens = parent_req.observe_num_generation_tokens(
139
140
                num_generation_tokens
            )
141
142
143

        # Child requests finished, we can now record to iteration stats
        if parent_req is None or not parent_req.child_requests:
144
            iteration_stats.max_num_generation_tokens_iter.append(num_generation_tokens)
145
            iteration_stats.n_params_iter.append(n_param)