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

4
from abc import ABC, abstractmethod
5
from dataclasses import dataclass
6
from typing import List, Optional, Set, Union
7
8
9

import torch

10
from vllm.sequence import ExecuteModelRequest, PromptLogprobs
11
from vllm.worker.worker_base import WorkerBase
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


@dataclass
class SpeculativeProposals:
    """Datastructure used to represent proposal tokens from some proposer. It
    also tracks how many speculative tokens each sequence has.
    """

    # Speculative proposal tokens.
    proposal_token_ids: torch.Tensor

    # Probabilities of the proposal tokens according to the proposer.
    proposal_probs: torch.Tensor

    # The valid length of each proposal; can be zero.
    proposal_lens: torch.Tensor

29
30
31
    # A flag to mark that there's no available proposals
    no_proposals: bool = False

32
33
    def __repr__(self):
        return (f"SpeculativeProposals("
34
                f"proposal_token_ids={self.proposal_token_ids}, "
35
                f"proposal_probs={self.proposal_probs.shape}, "
36
                f"proposal_lens={self.proposal_lens})")
37
38
39
40
41
42
43
44
45
46
47


@dataclass
class SpeculativeScores:
    """Datastructure used to represent the scores of speculative tokens
    according to the scoring model.
    """

    # Probabilities of the speculative tokens according to the scoring model.
    probs: torch.Tensor

48
49
50
51
52
    # Log-probabilities of the speculative tokens according to the scoring
    # model. These values can be used to generate Logprob objects that are
    # returned to the user.
    logprobs: torch.Tensor

53
54
55
56
    # Token ids sampled from the scoring model. Used for speculative bonus
    # tokens and also non-speculative normal decoding.
    token_ids: torch.Tensor

57
58
59
    # Optional last hidden states from the scoring model.
    hidden_states: Optional[torch.Tensor] = None

60
61
62
63
    # Scoring model may also return logprobs for prompt tokens
    # for each request, when chunked prefill is enabled.
    prompt_logprobs: Optional[List[PromptLogprobs]] = None

64
65
66
67
68
69
70
71
72
    def __repr__(self):
        return (f"SpeculativeScores("
                f"probs={self.probs.shape}, "
                f"token_ids={self.token_ids.shape})")


class SpeculativeProposer(ABC):

    @abstractmethod
73
    def get_spec_proposals(
74
        self,
75
        execute_model_req: ExecuteModelRequest,
76
77
78
        # If set, this contains all sequence IDs that were assigned
        # bonus tokens in their last forward pass.
        seq_ids_with_bonus_token_in_last_step: Set[int],
79
80
81
82
83
84
    ) -> SpeculativeProposals:
        raise NotImplementedError


class SpeculativeScorer(ABC):

85
86
    def __init__(self, scorer_worker: WorkerBase,
                 device: Union[torch.device, str], vocab_size: int):
87
        self._scorer_worker = scorer_worker
88
89
        if isinstance(device, torch.device):
            device = device.type
90
91
92
        self._device = device
        self._vocab_size = vocab_size

93
94
95
    @abstractmethod
    def score_proposals(
        self,
96
        execute_model_req: ExecuteModelRequest,
97
        proposals: SpeculativeProposals,
98
    ) -> SpeculativeScores:
99
        raise NotImplementedError