- The paper introduces a novel framework for speculative decoding using optimal transport with a membership cost to maximize accepted tokens from multi-candidate drafts.
- It derives an LP formulation and proposes a greedy algorithm that achieves a (1-1/e) approximation with near-linear time complexity.
- Empirical results demonstrate up to 2.13X speedup over standard decoding while preserving target distribution consistency.
Speculative decoding accelerates autoregressive sampling from LLMs by using a smaller, faster draft model to propose sequences (drafts) of tokens, which are then verified in parallel by the larger target LLM. Only a prefix of the draft sequence statistically consistent with the target model's distribution is accepted. "SpecTr: Fast Speculative Decoding via Optimal Transport" (2310.15141) introduces a novel framework for speculative decoding based on Optimal Transport (OT) with a membership cost, generalizing existing methods and leading to improved performance.
Theoretical Framework: Optimal Transport with Membership Cost
Standard speculative decoding aims to sample from a target distribution p(xt+1​∣x1:t​) using proposals from a draft distribution q(xt+1′​∣x1:t​). A sequence xt+1′​,…,xt+γ′​ is sampled from q. Then, for each position i=1,…,γ, the token xt+i′​ is accepted if a random variable U∼Uniform(0,1) satisfies U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​. If rejected, a correction token is sampled from a modified distribution derived from p and q, and the process restarts.
SpecTr reformulates this process through the lens of optimal transport. The goal is to find a coupling (a joint probability distribution) q(xt+1′​∣x1:t​)0 between the draft distribution q(xt+1′​∣x1:t​)1 and the target distribution q(xt+1′​∣x1:t​)2 (conditioned on the history q(xt+1′​∣x1:t​)3, omitted for brevity) that maximizes the expected number of accepted tokens. This is framed as an OT problem with a specific "membership cost".
The key generalization introduced by SpecTr is allowing the draft model to propose a set of q(xt+1′​∣x1:t​)4 candidate tokens q(xt+1′​∣x1:t​)5 at each position, instead of just one. Let q(xt+1′​∣x1:t​)6 be the distribution over sets of q(xt+1′​∣x1:t​)7 candidates generated by the draft mechanism. The objective is to find a transport plan q(xt+1′​∣x1:t​)8, where q(xt+1′​∣x1:t​)9, that defines the probability of selecting the target token xt+1′​,…,xt+γ′​0 given the proposed set xt+1′​,…,xt+γ′​1. This plan must satisfy the marginal constraint xt+1′​,…,xt+γ′​2 for all xt+1′​,…,xt+γ′​3, ensuring the final output follows the target distribution xt+1′​,…,xt+γ′​4.
The goal is to maximize the probability that the selected token xt+1′​,…,xt+γ′​5 is one of the proposed candidates, i.e., maximize xt+1′​,…,xt+γ′​6. This corresponds to maximizing the expected number of accepted tokens from the draft set. This formulation extends the maximal-coupling problem by considering a set of candidates (xt+1′​,…,xt+γ′​7) rather than just a single proposal.
Optimal Draft Selection and Computational Complexity
The paper demonstrates that the optimal transport plan xt+1′​,…,xt+γ′​8 maximizing the acceptance probability can be formulated and solved as a Linear Program (LP). The variables in the LP are xt+1′​,…,xt+γ′​9 for all possible target tokens q0 and all possible candidate sets q1. The objective is to maximize q2 subject to the marginal constraint q3 for all q4, and the probability constraints q5 and q6 for all q7.
While theoretically solvable via LP, the number of possible candidate sets q8 grows combinatorially with q9 and the vocabulary size i=1,…,γ0, making the LP formulation computationally intractable for practical values of i=1,…,γ1 and i=1,…,γ2. The best-known runtime for solving such LPs is exponential in i=1,…,γ3.
The SpecTr Algorithm and Approximation Guarantee
To overcome the computational bottleneck of solving the exact LP, SpecTr introduces an efficient, greedy algorithm for constructing a valid transport plan i=1,…,γ4 that achieves a strong approximation guarantee. This algorithm provides a i=1,…,γ5-multiplicative approximation to the optimal acceptance probability achievable by any valid transport plan. Crucially, this approximate algorithm runs in time nearly linear in the vocabulary size i=1,…,γ6, specifically i=1,…,γ7 or potentially i=1,…,γ8 with appropriate data structures, making it highly practical.
The core idea of the greedy algorithm resembles online bipartite matching or ad allocation algorithms. It iteratively assigns probability mass from the target distribution i=1,…,γ9 to candidate sets xt+i′​0 containing xt+i′​1, prioritizing assignments that yield the highest "gain" in terms of acceptance probability, while carefully managing the remaining probability mass of xt+i′​2 and xt+i′​3 to ensure the marginal constraints are met.
The overall SpecTr sampling procedure at each step xt+i′​4 involves:
- Candidate Generation: Use the draft model xt+i′​5 to generate a set of xt+i′​6 candidate tokens xt+i′​7 for position xt+i′​8. This might involve beam search or multiple sampling passes from xt+i′​9.
- Target Probability Calculation: Query the target model U∼Uniform(0,1)0 in parallel to obtain the probabilities U∼Uniform(0,1)1 for all candidates U∼Uniform(0,1)2. Note that obtaining the full distribution U∼Uniform(0,1)3 might be needed for the selection algorithm, depending on its specifics.
- Draft Selection: Apply the efficient U∼Uniform(0,1)4-approximate algorithm using U∼Uniform(0,1)5, U∼Uniform(0,1)6, and the candidate set U∼Uniform(0,1)7 to determine the acceptance/rejection probabilities for the candidates. This yields a sampling distribution over U∼Uniform(0,1)8.
- Sampling: Sample an outcome according to the distribution computed in step 3.
- If a token U∼Uniform(0,1)9 is sampled, it is accepted. Increment U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​0 and repeat from step 1 for the next position.
- If 'reject' is sampled, discard all candidates. Sample a correction token from the residual distribution required to maintain consistency with U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​1. Restart the process from step 1 for position U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​2.
This process allows multiple tokens (potentially the entire sequence proposed by the draft model, depending on acceptance) to be decoded per single invocation of the selection logic, amortizing the cost and leveraging the parallel computation capability for target model evaluations.
Implementation Considerations
Implementing SpecTr involves several key components and choices:
- Draft Model Selection: The choice of the draft model U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​3 is critical. It needs to be significantly faster than the target model U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​4 but also provide distributions reasonably close to U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​5 to ensure a high acceptance rate. Smaller versions of the target model or distilled models are common choices. The method used to generate the U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​6 candidates (e.g., beam search with width U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​7, diverse beam search, multiple independent samples) also impacts performance.
- Target Model Parallelism: The primary speedup comes from evaluating the target model U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​8 on multiple candidate tokens U≤q(xt+i′​∣x1:t+i−1​)p(xt+i′​∣x1:t+i−1​)​9 in parallel. Efficient batching and hardware utilization (GPUs/TPUs) are essential.
- Parameter p0: Selecting the number of candidates p1 involves a trade-off. Higher p2 increases the theoretical upper bound on the acceptance rate via the p3-optimal algorithm, potentially leading to greater speedup. However, it also increases the cost of generating candidates (step 1) and potentially the complexity of the selection algorithm (step 3), though the proposed algorithm is efficient. The optimal p4 is likely task- and model-dependent and requires empirical tuning. Values like p5 or p6 might be practical starting points.
- Selection Algorithm Implementation: The efficient p7-approximate algorithm needs careful implementation. It likely involves sorting or using priority queues based on probability ratios like p8 or similar metrics derived from the OT formulation. The exact details would require referring to the algorithm description in the paper (2310.15141). Ensuring numerical stability when dealing with small probabilities is important.
- Distribution Handling: The algorithm requires access to probabilities p9 and potentially q0 or the distribution q1. Efficiently querying these distributions and handling the residual distribution for rejection sampling are necessary.
- System Integration: Integrating SpecTr into an LLM inference framework requires modifying the generation loop. Instead of sampling one token, it generates q2 candidates, runs the parallel verification, executes the SpecTr selection logic, and handles acceptance/rejection/correction. This involves managing the state across multiple potential future tokens.
Below is a high-level conceptual pseudocode for the SpecTr sampling loop:
q8
Experimental Results
The paper reports significant empirical speedups. On standard benchmarks using state-of-the-art LLMs, SpecTr achieved a wall clock speedup of 2.13X compared to standard autoregressive decoding. Furthermore, it demonstrated a 1.37X speedup over the baseline speculative decoding method (which corresponds to the case q3). These results highlight the practical benefit of the OT formulation and the multi-candidate (q4) approach enabled by SpecTr. The authors emphasize that this speedup is achieved while mathematically guaranteeing that the output distribution remains identical to that of the target model q5, thus incurring no degradation in generation quality.
Conclusion
SpecTr provides a principled extension to speculative decoding by framing it as an optimal transport problem with membership cost. This allows generalizing the mechanism to handle multiple (q6) draft candidates per position. While the exact optimal solution is computationally expensive, SpecTr introduces an efficient approximation algorithm with a q7-optimality guarantee and near-linear time complexity. This leads to substantial wall-clock speedups in practice (reported as 2.13X overall, 1.37X over standard speculative decoding) without compromising the output quality, making it a promising technique for accelerating inference in LLMs. Implementing SpecTr requires careful integration with existing inference systems, focusing on efficient candidate generation, parallel target model evaluation, and the implementation of the novel selection algorithm.