interfaces.py 2.98 KB
Newer Older
1
from abc import ABC, abstractmethod
2
from dataclasses import dataclass
3
from typing import List, Optional, Set, Union
4
5
6

import torch

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


@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

26
27
28
    # A flag to mark that there's no available proposals
    no_proposals: bool = False

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


@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

45
46
47
48
49
    # 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

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

54
55
56
    # Optional last hidden states from the scoring model.
    hidden_states: Optional[torch.Tensor] = None

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

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


class SpeculativeProposer(ABC):

    @abstractmethod
70
    def get_spec_proposals(
71
        self,
72
        execute_model_req: ExecuteModelRequest,
73
74
75
        # 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],
76
77
78
79
80
81
    ) -> SpeculativeProposals:
        raise NotImplementedError


class SpeculativeScorer(ABC):

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

90
91
92
    @abstractmethod
    def score_proposals(
        self,
93
        execute_model_req: ExecuteModelRequest,
94
        proposals: SpeculativeProposals,
95
    ) -> SpeculativeScores:
96
        raise NotImplementedError