Papers
Topics
Authors
Recent
Search
2000 character limit reached

SpecTr: Fast Speculative Decoding via Optimal Transport

Published 23 Oct 2023 in cs.LG, cs.CL, cs.DS, cs.IT, and math.IT | (2310.15141v2)

Abstract: Autoregressive sampling from LLMs has led to state-of-the-art results in several natural language tasks. However, autoregressive sampling generates tokens one at a time making it slow, and even prohibitive in certain tasks. One way to speed up sampling is speculative decoding\textit{speculative decoding}: use a small model to sample a draft\textit{draft} (block or sequence of tokens), and then score all tokens in the draft by the LLM in parallel. A subset of the tokens in the draft are accepted (and the rest rejected) based on a statistical method to guarantee that the final output follows the distribution of the large model. In this work, we provide a principled understanding of speculative decoding through the lens of optimal transport (OT) with membership cost\textit{membership cost}. This framework can be viewed as an extension of the well-known maximal-coupling\textit{maximal-coupling} problem. This new formulation enables us to generalize the speculative decoding method to allow for a set of kk candidates at the token-level, which leads to an improved optimal membership cost. We show that the optimal draft selection algorithm (transport plan) can be computed via linear programming, whose best-known runtime is exponential in kk. We then propose a valid draft selection algorithm whose acceptance probability is (1−1/e)(1-1/e)-optimal multiplicatively. Moreover, it can be computed in time almost linear with size of domain of a single token. Using this newdraftselectionnew draft selection algorithm, we develop a new autoregressive sampling algorithm called SpecTr\textit{SpecTr}, which provides speedup in decoding while ensuring that there is no quality degradation in the decoded output. We experimentally demonstrate that for state-of-the-art LLMs, the proposed approach achieves a wall clock speedup of 2.13X, a further 1.37X speedup over speculative decoding on standard benchmarks.

Definition Search Book Streamline Icon: https://streamlinehq.com
References (29)
  1. A learning algorithm for boltzmann machines. Cognitive science, 9(1):147–169, 1985.
  2. Language models are few-shot learners. Advances in neural information processing systems, 33:1877–1901, 2020.
  3. One billion word benchmark for measuring progress in statistical language modeling. arXiv preprint arXiv:1312.3005, 2013.
  4. Accelerating large language model decoding with speculative sampling. arXiv preprint arXiv:2302.01318, 2023.
  5. Palm: Scaling language modeling with pathways. arXiv preprint arXiv:2204.02311, 2022.
  6. Marco Cuturi. Sinkhorn distances: Lightspeed computation of optimal transport. In C.J. Burges, L. Bottou, M. Welling, Z. Ghahramani, and K.Q. Weinberger, editors, Advances in Neural Information Processing Systems, volume 26. Curran Associates, Inc., 2013.
  7. George B Dantzig. Linear programming. Operations research, 50(1):42–47, 2002.
  8. Frank Den Hollander. Probability theory: The coupling method. Lecture notes available online (http://websites. math. leidenuniv. nl/probability/lecturenotes/CouplingLectures. pdf), 2012.
  9. Hierarchical neural story generation. arXiv preprint arXiv:1805.04833, 2018.
  10. Controlling linguistic style aspects in neural language generation. arXiv preprint arXiv:1707.02633, 2017.
  11. Lossless acceleration for seq2seq generation with aggressive decoding. arXiv preprint arXiv:2205.10350, 2022.
  12. Google AI. Introducing PaLM 2, 2023. https://blog.google/technology/ai/google-palm-2-ai-large-language-model/.
  13. Google PaLM-2 Team. PaLM 2 technical report, 2023.
  14. Fast algorithms for computational optimal transport and wasserstein barycenter. In Silvia Chiappa and Roberto Calandra, editors, Proceedings of the Twenty Third International Conference on Artificial Intelligence and Statistics, volume 108 of Proceedings of Machine Learning Research, pages 2088–2097. PMLR, 26–28 Aug 2020.
  15. Flax: A neural network library and ecosystem for JAX, 2023.
  16. The curious case of neural text degeneration. arXiv preprint arXiv:1904.09751, 2019.
  17. Leonid V Kantorovich. On the translocation of masses. In Dokl. Akad. Nauk. USSR (NS), volume 37, pages 199–201, 1942.
  18. Path finding methods for linear programming: Solving linear programs in o (vrank) iterations and faster algorithms for maximum flow. In 2014 IEEE 55th Annual Symposium on Foundations of Computer Science, pages 424–433. IEEE, 2014.
  19. Fast inference from transformers via speculative decoding. In International Conference on Machine Learning, pages 19274–19286. PMLR, 2023.
  20. Eagle: Lossless acceleration of llm decoding by feature extrapolation, 2023. https://sites.google.com/corp/view/eagle-llm.
  21. Specinfer: Accelerating generative large language model serving with speculative inference and token tree verification, 2023.
  22. Fast and robust earth mover’s distances. In 2009 IEEE 12th international conference on computer vision, pages 460–467. IEEE, 2009.
  23. Language models are unsupervised multitask learners. OpenAI blog, 1(8):9, 2019.
  24. Blockwise parallel decoding for deep autoregressive models. Advances in Neural Information Processing Systems, 31, 2018.
  25. Efficient transformers: A survey. ACM Computing Surveys, 55(6):1–28, 2022.
  26. Lamda: Language models for dialog applications. arXiv preprint arXiv:2201.08239, 2022.
  27. LLaMA: Open and efficient foundation language models. arXiv preprint arXiv:2302.13971, 2023.
  28. Cédric Villani et al. Optimal transport: old and new, volume 338. Springer, 2009.
  29. Inference with reference: Lossless acceleration of large language models. arXiv preprint arXiv:2304.04487, 2023.
Citations (50)

Summary

  • 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)p(x_{t+1} | x_{1:t}) using proposals from a draft distribution q(xt+1′∣x1:t)q(x'_{t+1} | x_{1:t}). A sequence xt+1′,…,xt+γ′x'_{t+1}, \dots, x'_{t+\gamma} is sampled from qq. Then, for each position i=1,…,γi=1, \dots, \gamma, the token xt+i′x'_{t+i} is accepted if a random variable U∼Uniform(0,1)U \sim \text{Uniform}(0, 1) satisfies U≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1:t+i-1})}. If rejected, a correction token is sampled from a modified distribution derived from pp and qq, 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)q(x'_{t+1} | x_{1:t})0 between the draft distribution q(xt+1′∣x1:t)q(x'_{t+1} | x_{1:t})1 and the target distribution q(xt+1′∣x1:t)q(x'_{t+1} | x_{1:t})2 (conditioned on the history q(xt+1′∣x1:t)q(x'_{t+1} | x_{1: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)q(x'_{t+1} | x_{1:t})4 candidate tokens q(xt+1′∣x1:t)q(x'_{t+1} | x_{1:t})5 at each position, instead of just one. Let q(xt+1′∣x1:t)q(x'_{t+1} | x_{1:t})6 be the distribution over sets of q(xt+1′∣x1:t)q(x'_{t+1} | x_{1:t})7 candidates generated by the draft mechanism. The objective is to find a transport plan q(xt+1′∣x1:t)q(x'_{t+1} | x_{1:t})8, where q(xt+1′∣x1:t)q(x'_{t+1} | x_{1:t})9, that defines the probability of selecting the target token xt+1′,…,xt+γ′x'_{t+1}, \dots, x'_{t+\gamma}0 given the proposed set xt+1′,…,xt+γ′x'_{t+1}, \dots, x'_{t+\gamma}1. This plan must satisfy the marginal constraint xt+1′,…,xt+γ′x'_{t+1}, \dots, x'_{t+\gamma}2 for all xt+1′,…,xt+γ′x'_{t+1}, \dots, x'_{t+\gamma}3, ensuring the final output follows the target distribution xt+1′,…,xt+γ′x'_{t+1}, \dots, x'_{t+\gamma}4.

The goal is to maximize the probability that the selected token xt+1′,…,xt+γ′x'_{t+1}, \dots, x'_{t+\gamma}5 is one of the proposed candidates, i.e., maximize xt+1′,…,xt+γ′x'_{t+1}, \dots, x'_{t+\gamma}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+γ′x'_{t+1}, \dots, x'_{t+\gamma}7) rather than just a single proposal.

Optimal Draft Selection and Computational Complexity

The paper demonstrates that the optimal transport plan xt+1′,…,xt+γ′x'_{t+1}, \dots, x'_{t+\gamma}8 maximizing the acceptance probability can be formulated and solved as a Linear Program (LP). The variables in the LP are xt+1′,…,xt+γ′x'_{t+1}, \dots, x'_{t+\gamma}9 for all possible target tokens qq0 and all possible candidate sets qq1. The objective is to maximize qq2 subject to the marginal constraint qq3 for all qq4, and the probability constraints qq5 and qq6 for all qq7.

While theoretically solvable via LP, the number of possible candidate sets qq8 grows combinatorially with qq9 and the vocabulary size i=1,…,γi=1, \dots, \gamma0, making the LP formulation computationally intractable for practical values of i=1,…,γi=1, \dots, \gamma1 and i=1,…,γi=1, \dots, \gamma2. The best-known runtime for solving such LPs is exponential in i=1,…,γi=1, \dots, \gamma3.

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,…,γi=1, \dots, \gamma4 that achieves a strong approximation guarantee. This algorithm provides a i=1,…,γi=1, \dots, \gamma5-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,…,γi=1, \dots, \gamma6, specifically i=1,…,γi=1, \dots, \gamma7 or potentially i=1,…,γi=1, \dots, \gamma8 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,…,γi=1, \dots, \gamma9 to candidate sets xt+i′x'_{t+i}0 containing xt+i′x'_{t+i}1, prioritizing assignments that yield the highest "gain" in terms of acceptance probability, while carefully managing the remaining probability mass of xt+i′x'_{t+i}2 and xt+i′x'_{t+i}3 to ensure the marginal constraints are met.

The overall SpecTr sampling procedure at each step xt+i′x'_{t+i}4 involves:

  1. Candidate Generation: Use the draft model xt+i′x'_{t+i}5 to generate a set of xt+i′x'_{t+i}6 candidate tokens xt+i′x'_{t+i}7 for position xt+i′x'_{t+i}8. This might involve beam search or multiple sampling passes from xt+i′x'_{t+i}9.
  2. Target Probability Calculation: Query the target model U∼Uniform(0,1)U \sim \text{Uniform}(0, 1)0 in parallel to obtain the probabilities U∼Uniform(0,1)U \sim \text{Uniform}(0, 1)1 for all candidates U∼Uniform(0,1)U \sim \text{Uniform}(0, 1)2. Note that obtaining the full distribution U∼Uniform(0,1)U \sim \text{Uniform}(0, 1)3 might be needed for the selection algorithm, depending on its specifics.
  3. Draft Selection: Apply the efficient U∼Uniform(0,1)U \sim \text{Uniform}(0, 1)4-approximate algorithm using U∼Uniform(0,1)U \sim \text{Uniform}(0, 1)5, U∼Uniform(0,1)U \sim \text{Uniform}(0, 1)6, and the candidate set U∼Uniform(0,1)U \sim \text{Uniform}(0, 1)7 to determine the acceptance/rejection probabilities for the candidates. This yields a sampling distribution over U∼Uniform(0,1)U \sim \text{Uniform}(0, 1)8.
  4. Sampling: Sample an outcome according to the distribution computed in step 3.
    • If a token U∼Uniform(0,1)U \sim \text{Uniform}(0, 1)9 is sampled, it is accepted. Increment U≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1: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≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1:t+i-1})}1. Restart the process from step 1 for position U≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1: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≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1:t+i-1})}3 is critical. It needs to be significantly faster than the target model U≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1:t+i-1})}4 but also provide distributions reasonably close to U≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1: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≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1:t+i-1})}6 candidates (e.g., beam search with width U≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1: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≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1:t+i-1})}8 on multiple candidate tokens U≤p(xt+i′∣x1:t+i−1)q(xt+i′∣x1:t+i−1)U \le \frac{p(x'_{t+i} | x_{1:t+i-1})}{q(x'_{t+i} | x_{1:t+i-1})}9 in parallel. Efficient batching and hardware utilization (GPUs/TPUs) are essential.
  • Parameter pp0: Selecting the number of candidates pp1 involves a trade-off. Higher pp2 increases the theoretical upper bound on the acceptance rate via the pp3-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 pp4 is likely task- and model-dependent and requires empirical tuning. Values like pp5 or pp6 might be practical starting points.
  • Selection Algorithm Implementation: The efficient pp7-approximate algorithm needs careful implementation. It likely involves sorting or using priority queues based on probability ratios like pp8 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 pp9 and potentially qq0 or the distribution qq1. 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 qq2 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:

qq8

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 qq3). These results highlight the practical benefit of the OT formulation and the multi-candidate (qq4) 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 qq5, 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 (qq6) draft candidates per position. While the exact optimal solution is computationally expensive, SpecTr introduces an efficient approximation algorithm with a qq7-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.

Paper to Video (Beta)

No one has generated a video about this paper yet.

Whiteboard

No one has generated a whiteboard explanation for this paper yet.

Open Problems

We haven't generated a list of open problems mentioned in this paper yet.

Tweets

Sign up for free to view the 3 tweets with 217 likes about this paper.