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

import torch

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


@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

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

28
29
30
31
32
33
34
35
36
37
38
39
40
    # The cart_candidates used in tree-style generation
    cart_candidates: Optional[torch.Tensor] = None

    # The cart_candidates used in tree-style generation
    retrieve_indices: Optional[torch.Tensor] = None

    # tree-style attention masks
    tree_attn_masks: Optional[torch.Tensor] = None

    # tree-style cartesian candidates
    tree_position_ids: Optional[torch.Tensor] = None


41
42
    def __repr__(self):
        return (f"SpeculativeProposals("
43
                f"proposal_token_ids={self.proposal_token_ids}, "
44
                f"proposal_probs={self.proposal_probs.shape}, "
45
                f"proposal_lens={self.proposal_lens})")
46
47
48
49
50
51
52
53
54
55
56


@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

57
58
59
60
61
    # 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

62
63
64
65
    # Token ids sampled from the scoring model. Used for speculative bonus
    # tokens and also non-speculative normal decoding.
    token_ids: torch.Tensor

66
67
68
    # Optional last hidden states from the scoring model.
    hidden_states: Optional[torch.Tensor] = None

69
70
71
    # Optional lm_head logits from the scoring model.
    logits: Optional[torch.Tensor] = None

72
73
74
75
76
77
78
79
80
    def __repr__(self):
        return (f"SpeculativeScores("
                f"probs={self.probs.shape}, "
                f"token_ids={self.token_ids.shape})")


class SpeculativeProposer(ABC):

    @abstractmethod
81
    def get_spec_proposals(
82
        self,
83
        execute_model_req: ExecuteModelRequest,
84
85
86
        # 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],
87
88
89
90
91
92
93
94
95
    ) -> SpeculativeProposals:
        raise NotImplementedError


class SpeculativeScorer(ABC):

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