2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-06-03 11:20:17 -07:00
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2025-02-02 14:58:18 -05:00
|
|
|
|
2024-03-08 23:32:46 -08:00
|
|
|
from abc import ABC, abstractmethod
|
2024-03-25 23:59:47 +09:00
|
|
|
from dataclasses import dataclass
|
2025-01-27 22:38:35 +01:00
|
|
|
from typing import List, Optional, Set, Union
|
2024-03-08 23:32:46 -08:00
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
2025-01-27 22:38:35 +01:00
|
|
|
from vllm.sequence import ExecuteModelRequest, PromptLogprobs
|
2024-10-01 16:04:42 -07:00
|
|
|
from vllm.worker.worker_base import WorkerBase
|
2024-03-08 23:32:46 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
2024-07-19 22:01:09 +09:00
|
|
|
# A flag to mark that there's no available proposals
|
|
|
|
|
no_proposals: bool = False
|
|
|
|
|
|
2024-03-08 23:32:46 -08:00
|
|
|
def __repr__(self):
|
|
|
|
|
return (f"SpeculativeProposals("
|
2024-04-23 01:02:36 -07:00
|
|
|
f"proposal_token_ids={self.proposal_token_ids}, "
|
2024-03-08 23:32:46 -08:00
|
|
|
f"proposal_probs={self.proposal_probs.shape}, "
|
2024-04-23 01:02:36 -07:00
|
|
|
f"proposal_lens={self.proposal_lens})")
|
2024-03-08 23:32:46 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
2024-05-03 15:52:01 -07:00
|
|
|
# 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
|
|
|
|
|
|
2024-03-08 23:32:46 -08:00
|
|
|
# Token ids sampled from the scoring model. Used for speculative bonus
|
|
|
|
|
# tokens and also non-speculative normal decoding.
|
|
|
|
|
token_ids: torch.Tensor
|
|
|
|
|
|
2024-06-20 20:23:12 -04:00
|
|
|
# Optional last hidden states from the scoring model.
|
|
|
|
|
hidden_states: Optional[torch.Tensor] = None
|
|
|
|
|
|
2025-01-27 22:38:35 +01:00
|
|
|
# Scoring model may also return logprobs for prompt tokens
|
|
|
|
|
# for each request, when chunked prefill is enabled.
|
|
|
|
|
prompt_logprobs: Optional[List[PromptLogprobs]] = None
|
|
|
|
|
|
2024-03-08 23:32:46 -08:00
|
|
|
def __repr__(self):
|
|
|
|
|
return (f"SpeculativeScores("
|
|
|
|
|
f"probs={self.probs.shape}, "
|
|
|
|
|
f"token_ids={self.token_ids.shape})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SpeculativeProposer(ABC):
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
2024-06-05 14:53:05 -07:00
|
|
|
def get_spec_proposals(
|
2024-03-08 23:32:46 -08:00
|
|
|
self,
|
2024-05-03 17:47:07 -07:00
|
|
|
execute_model_req: ExecuteModelRequest,
|
2024-07-10 16:02:47 -07:00
|
|
|
# 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],
|
2024-03-08 23:32:46 -08:00
|
|
|
) -> SpeculativeProposals:
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SpeculativeScorer(ABC):
|
|
|
|
|
|
2024-11-26 19:57:11 -06:00
|
|
|
def __init__(self, scorer_worker: WorkerBase,
|
|
|
|
|
device: Union[torch.device, str], vocab_size: int):
|
2024-10-01 16:04:42 -07:00
|
|
|
self._scorer_worker = scorer_worker
|
2024-11-26 19:57:11 -06:00
|
|
|
if isinstance(device, torch.device):
|
|
|
|
|
device = device.type
|
2024-10-01 16:04:42 -07:00
|
|
|
self._device = device
|
|
|
|
|
self._vocab_size = vocab_size
|
|
|
|
|
|
2024-03-08 23:32:46 -08:00
|
|
|
@abstractmethod
|
|
|
|
|
def score_proposals(
|
|
|
|
|
self,
|
2024-05-03 17:47:07 -07:00
|
|
|
execute_model_req: ExecuteModelRequest,
|
2024-03-08 23:32:46 -08:00
|
|
|
proposals: SpeculativeProposals,
|
2024-04-18 09:28:43 +09:00
|
|
|
) -> SpeculativeScores:
|
2024-03-08 23:32:46 -08:00
|
|
|
raise NotImplementedError
|