Online Speculative Decoding

Xiaoxuan Liu, Lanxiang Hu, Peter Bailis, Alvin Cheung, Zhijie Deng, Ion Stoica, Hao Zhang

Introduction

Large language models (LLMs) such as GPT-4 (OpenAI, 2023), Claude (Bai et al., 2022), and Llama (Touvron et al., 2023a; b) are rapidly reinventing today’s applications. Many companies are racing to deploy LLMs in their vertical domains, such as search, chatbots, and virtual assistants. Since most of these applications demand low latency, optimizing LLM serving latency is of vital importance and can directly translate into better quality of service and cost reduction.

The latency of today’s LLM service is unfortunately very high. This is primarily because serving a user query requires multiple serial evaluations of the LLM, each generating only one token of the response. An emerging solution to reduce the latency is speculative decoding. Speculative decoding employs a smaller model to speculate multiple output tokens of the target (large) model, then lets the target LLM verify these speculations in parallel. Then, if the verification of a token fails, the large model must recompute from that point. Therefore, the performance of speculative decoding primarily depends on the speculation accuracy of the small model. In the presence of diverse text inputs, the accuracy of existing speculative decoding methods is unfortunately not very high, due to the capability gap between the draft and target model. Employing a larger, more accurate model however defeats the purpose of speculative decoding as it potentially increases latency.

To address this challenge, we introduce a novel method, online speculative decoding, specifically designed for online LLM services. The method leverages the abundant redundant compute, termed as “spare flops,” available in a typical LLM serving cluster to continuously retrain (multiple) small draft models through online learning on query data posted to the LLM service. Our approach is simple and offers several significant advantages. First, user queries to a specific LLM service often exhibit a common domain-specific distribution (Zheng et al., 2023a), reflecting shared usage patterns. While accurately speculating the larger model’s outputs on any diverse input is challenging, it is feasible to enhance the draft model’s prediction accuracy, only for similar inputs posted to the service, characterized by the query distribution. This can be achieved by finetuning the draft model on user query distribution or finetuning multiple draft models, each on a cluster of the query distribution, and selecting the appropriately specialized draft model to speculate based on the class of inputs they are trained on. As shown in §5.2, we show that it is possible to train multiple draft models, each for a different language or topic. Second, the primary bottleneck for transformer-based LLM inference is the accelerator’s memory bandwidth, as generating each word requires loading the model weights from HBM to SRAM as well as reading the KV cache on all previous words. This results in a substantial amount of unused compute, especially during non-spike traffic hours (Spector & Re, 2023; Chen et al., 2023; Kwon et al., 2023), in an LLM serving cluster. We demonstrate that these spare FLOPs can be effectively repurposed for online retraining of draft models, with inconspicuous retraining cost (§4.2.2). Third, since tuning is performed online, the draft models continuously evolve over time based on the observed query data, which ensures high speculation accuracy even when faced with shifts in query distribution.

Based on these insights, we develop an online speculative decoding framework to improve the efficiency of online LLM serving. To align the draft model with the target model on a newly observed user query, we develop a new online learning algorithm based on Generalized Knowledge Distillation (GKD) (Gu et al., 2023; Agarwal et al., 2023). The algorithm keeps track of the recent queries that the draft model has speculated incorrectly, and forces the draft model to emulate the target model’s outputs on these queries. The algorithm performs GKD-based gradient update opportunistically only when spare flops are available, hiding the overhead.

In summary, this paper makes the following contributions:

We introduce online speculative decoding to reduce LLM serving latency by adapting (multiple) draft models on the fly using query data and knowledge distillation.

We explore various GKD methods for constructing draft models and identify the most effective variants, suggesting them as superior alternatives to existing finetuning methods in offline settings.

Our method demonstrates a significant improvement in token acceptance rate by 10-65% on diverse datasets, translating to 1.2-3.1×\times reduction in latency theoretically, with a negligible additional cost. It surpasses existing methods which construct static draft models using fine-tuning or distillation on offline datasets, and matches the hypothetical accuracy achieved if all query data were available a priori.

Related Work

LLMs have become pervasive in today’s AI applications, underscoring the importance of optimizing LLM inference. Numerous system optimizations have been developed to optimize the throughput of LLM serving (Yu et al., 2022; Kwon et al., 2023). This paper particularly concentrates on a significant strand of research, speculative decoding, aimed at reducing the latency of LLM inference.

Speculative decoding. Speculative decoding (Leviathan et al., 2023; Chen et al., 2023) accelerates LLM decoding by employing a (small) draft model to predict the outputs of the larger target model, which are then verified by the target model. Typically, the draft model, while having fewer parameters, is pretrained using the same training data as the target mode, resulting in a negotiable inference cost but with compromised capability. If the draft model can correctly predict more than one token per verification step, the memory I/O for accessing the model weights and KV cache at inference is amortized across multiple output tokens, thereby reduces latency, especially since LLM inference is often constrained by GPU HBM bandwidth. The efficacy of speculative decoding largely hinges on the draft model’s ability to accurately predict the target model’s outputs. Existing work improves the speculation accuracy by using multiple collectively boosted (Miao et al., 2023) or staged (Spector & Re, 2023) draft models, or retraining the target model with auxiliary prediction heads as a draft model (Cai et al., 2023; Stern et al., 2018). These methods predominantly assume a static draft model post-deployment. In contrast, our work introduces a framework that actively adapts the draft model to the evolving user query distribution on the fly, irrespective of the draft model’s construction.

Distillation for auto-regressive models. Knowledge distillation (KD) is a framework to generate smaller models that emulate the performance of larger models. However, KD in its conventional form has been observed to be less effective for LLMs. Gu et al. (2023) extend KD to autoregressive LLMs by decoding from the student model and optimizing the reserve KL divergence between students and teachers. Further, Agarwal et al. (2023) introduce generalized knowledge distillation (GKD) to optimize a linear combination of the forward KL and reverse KL between teacher and student, using a blend of teacher- and student-sampled data. Drawing inspiration from both works, our paper applies KD to speculative decoding for LLMs. We empirically determine the most effective KD variant for maximizing the draft model’s accuracy, and extend it to dynamically generate draft models for online LLM services.

Background

We first briefly review speculative decoding (Leviathan et al., 2023), a critical technique that accelerates inference of a large target LLM p(x)p(\cdot|{\bm{x}}) with token proposals from a small draft model qθ(x)q_{\bm{\theta}}(\cdot|{\bm{x}}). x{\bm{x}} denotes the concatenation of the input prompt and already generated tokens. The two distributions are both auto-regressive. We emphasize the parameters θ{\bm{\theta}} of the draft model because we usually need to tailor them according to the target LLM for more substantial acceleration.

Speculative decoding uses a (small) draft model to propose kk tokens y{yi}i=1kqθ(x){{\bm{y}}}\triangleq\{y_{i}\}_{i=1}^{k}\sim q_{\bm{\theta}}(\cdot|{\bm{x}}), and let the target LLM estimate the k+1k+1 probabilities, {p(yx,y<i)}i=1k+1\{p({y}|{\bm{x}},{{\bm{y}}}_{<i})\}_{i=1}^{k+1}y<i{{\bm{y}}}_{<i} refers to {yj}j=1i1\{y_{j}\}_{j=1}^{i-1}., in parallel. With ii rising from 11 to kk, speculative decoding accepts the proposal yi{y}_{i} if up(yix,y<i)/qθ(yix,y<i)u\leq p(y_{i}|{\bm{x}},{{\bm{y}}}_{<i})/q_{\bm{\theta}}({y}_{i}|{\bm{x}},{{\bm{y}}}_{<i}) where uUu\sim U; otherwise exits. Let aa denote the number of accepted tokens, which takes values in {0,,k}\{0,\dots,k\}. We can sample an additional token ya+1{y}_{a+1} from the following distribution

Assuming that the k+1k+1 simultaneous evaluations of the target LLM pp take roughly the same amount of time as generating a single token in parallel, let cc be the time ratio for a single run between qθq_{\bm{\theta}} and pp. The expected generation length of a single target LLM run and the speedup in the total wall time due to speculative decoding is represented as (Leviathan et al., 2023):

We depict the speedup for varying values of α\alpha in Figure 2, which demonstrates the importance of α\alpha in affecting the speedup.

Observation. Interestingly, we can actually enhance α\alpha based on a key observation: the speculative decoding process inherently identifies the inaccuracies of the small draft LLM and offers correct solutions for these inaccuracies. This essentially means that we receive valuable insights on the areas and strategies to refine the draft model at no additional cost. Viewed through the lens of online learning, we can effortlessly accumulate a set of input-output pairs, denoted as ([x,y<a+1],p(yx,y<a+1))([{\bm{x}},{\bm{y}}_{<a+1}],p(y|{\bm{x}},{{\bm{y}}}_{<a+1})), that have yet to be assimilated by the draft LLM, paving the way for its subsequent optimization. Given the reduced size of the draft model (for instance, it may be over 20×20\times smaller than the target model), its tuning is not only efficient but also viable for real-time online adjustments. Prior work (Leviathan et al., 2023; Miao et al., 2023) has primarily approached speculative decoding in an offline manner, meaning the draft model remains static during online deployment. We next develop online speculative decoding to bridge this gap.

Online Speculative Decoding

We propose the online speculative decoding approach to update the draft model dynamically for more effective suggestions. We frame the learning problem based on the aforementioned auxiliary information as online knowledge distillation, where the teacher and student models correspond to the target and draft LLMs in speculative decoding, respectively. We elaborate on the details below.

Knowledge distillation is a general framework to align the predictive distribution of a small model (i.e., student model) with that of a larger one (i.e., teacher model). Prior research has utilized knowledge distillation to compress neural networks, resulting in decreased inference costs and memory requirements. We posit that knowledge distillation is highly effective for speculative decoding. In this approach, the draft model acts as the student and the target model serves as the teacher. During speculative decoding, we possess complete information on both the proposed and verified probabilities of each token. This information helps to construct objectives for distilling the draft model, aligning its output distributions with those of the target model and thereby improving the token acceptance rate of the draft model. The distillation loss generally takes the form of:

where B={x(i)}i=1nB\mathcal{B}=\{{\bm{x}}^{(i)}\}_{i=1}^{n_{B}} denotes a batch of inputs and DD denotes some distance measure.

Distance measure. In the case of auto-regressive models, the prediction distribution is categorical at each token. Often, we can augment the predicted logits with a tunable temperature τ\tau for softmax transformation. We then use the popular forward KL and reverse KL (RKL), as well as their mixture (i.e., the JSD divergence) to instantiate DD (Agarwal et al., 2023; Gu et al., 2023):

where pθβ(x)βp(x)+(1β)qθ(x){p}^{\beta}_{\bm{\theta}}(\cdot|{\bm{x}})\triangleq\beta{p(\cdot|{\bm{x}})}+(1-\beta){q_{\bm{\theta}}(\cdot|{\bm{x}})}. These objectives diverge from the conventionally used label-based fine-tuning objectives in speculative decoding, as highlighted in (Miao et al., 2023; Leviathan et al., 2023). As shown in Section 5.1, objectives based on the KL divergence prove to be more effective. This is because distributions convey richer information than mere labels, thereby enhancing their capability to guide the student model (Hinton et al., 2015). Additionally, these objectives enhance convergence rates (He et al., 2022) and bolster calibration. The reverse KL is highlighted for its mode-seeking behavior, offering unique advantages (Gu et al., 2023). In our study, and in alignment with previous research (Agarwal et al., 2023), we empirically determine that the optimal distance measure can vary depending on the tasks and the relative capacities of the teacher and student models (see §5.1).

In comparison, a more straightforward approach is to omit the differentiation through the sampling process (Agarwal et al., 2023), where the sample y{\bm{y}} is directly plugged into the objective:

This way, various distance measures can be readily applied. Besides, the sampling becomes disentangled from the distance measure. i.e., we sample y{\bm{y}} from an arbitrary mixture of p(x){p}(\cdot|{\bm{x}}) and qθ(x){q}_{\theta}(\cdot|{\bm{x}}) but use KL, RKL or JSD for estimating the distribution mis-alignment.

Intuitively, the samples from the teacher model are usually coherent, which may raise difficulties in fitting the small student model, while samples from the student model may be less structured or even meaningless. A workaround strategy is to trade off between them via mixed sampling (Gu et al., 2023), i.e., yjβp(x,y<j)+(1β)qθ(x,y<j)y_{j}\sim\beta{p(\cdot|{\bm{x}},{\bm{y}}_{<j})}+(1-\beta)q_{\bm{\theta}}(\cdot|{\bm{x}},{\bm{y}}_{<j}).

2 Online Knowledge Distillation

This section expands the application of knowledge distillation for speculative decoding in online environments. The approach enables improving the performance of draft model using results from speculative decoding, thus dynamically adapting to the query distribution and improving token acceptance rate. We also discuss the trade-off of our approach when integrating LLM serving systems.

We depict our online speculative decoding algorithm (OSD) in Algorithm 1. OSD begins by training the draft model using the warmup dataset (Line 2). The serving system then continuously handles incoming requests (as described in Lines 6 to 23). For each request, it uses standard speculative decoding (Lines 10-11) to generate responses until the ⟨EOS⟩ token. Concurrently, OSD tracks the token index (error_indexerror\_index) and target logits where the draft model proposes the wrong tokens (Line 15). Leveraging tracked information, OSD updates the draft model every II iteration, with II being a dynamically adjustable parameter. OSD updates the draft model with different loss functions (Line 20) as described in Section 4.1. The choice of loss function depends on the specific (draft, target) model pairs and the corresponding input data.

Discussion. OSD utilizes a replay buffer, Q\mathcal{Q}, to capture all pertinent information for updating the draft model. Various eviction policies can be employed to maintain a compact size for Q\mathcal{Q}. For example, one could opt to retain only the most informative pairs or the most recent entries. Similarly, users have the option to retain data in Q\mathcal{Q} even after utilizing it to update the model multiple times. Determining the optimal eviction/retention strategy is a subject for future exploration. In the current study, we refrain from evicting any pairs and release Q\mathcal{Q} after each model update. Furthermore, II is a dynamic parameter. Depending on the system load and the rate at which the query distribution changes, users can adjust II accordingly. For example, we can perform a gradient update opportunistically only when the service traffic is not on spike (i.e., spare flops are available). Overall, OSD continuously improves the draft model’s approximation (indicated by increased token acceptance rate α\alpha) by learning from the target model during the serving phase. We next demonstrate how the enhanced acceptance rate directly contributes to a reduction in request latency.

2.2 Latency & Flops Analysis

Latency. As detailed in Appendix A.2, compared with standard speculative decoding, the expected speedup for online speculative decoding is 1+α2+α22+...+α2k1+α1+α12+...+α1k\frac{1+\alpha_{2}+\alpha_{2}^{2}+...+\alpha_{2}^{k}}{1+\alpha_{1}+\alpha_{1}^{2}+...+\alpha_{1}^{k}}. Based on the data from our experiment (refer to Table 1), when compared to standard speculative decoding, we expect a speedup improvement for Vicuna-7B (LLaMA-160M as the draft model) by factors of 2.42×2.42\times, 1.43×1.43\times, 1.64×1.64\times, and 1.22×1.22\times. Similarly, for Flan-T5-XL 3B (T5-small 80M as the draft model), the speedup enhancements are 3.06×3.06\times, 1.76×1.76\times, 2.72×2.72\times, and 1.55×1.55\times across the four evaluated datasets.

FLOPs. (1) The FLOPs required to update the draft model are significantly fewer than those needed for inference on a large model. As elaborated in Appendix A.3, for the two evaluated model pairs, the FLOPs ratio between the target model and the draft model is 18.75 for the pair (LLaMA-160M, Vicuna7B), and 12.6 for the pair (T5-small 80M, Flan-T5-XL 3B). (2) In practical systems, the FLOPs required for inference are significantly below the machine’s capacity. The Appendix A.3 provides an analysis of Arena chatbot traces where the cluster’s computational utilization is under 1 percent. Given the above two observations, it becomes evident that the FLOPs spent on inference and updating the draft model are relatively insignificant when juxtaposed with the FLOPs consumed while operating the target model and the cluster’s total FLOPs.

Experiments

To assess the efficacy of our method, we initially evaluate its ability to improve the token acceptance rate (α\alpha) within an offline context. This provides us with a theoretical upper bound on the performance improvements achievable when the query distribution remains constant. Subsequently, we examine the approach’s impact in an online environment, discovering that the acceptance rate improves even with a moderate amount of data while maintaining accuracy levels comparable to those in the offline scenario. Throughout our experiments, we employ two target models (Mp)M_{p}): Vicuna-7B (Chiang et al., 2023) and FLAN-T5-XL (3B) (Chung et al., 2022). Specifically for Vicuna-7B, we utilize LLaMA-160m (Miao et al., 2023) as the draft model (MqM_{q}). For FLAN-T5-XL, we use T5-Small (Raffel et al., 2020) as the draft model. We evaluate performance across four diverse datasets: Text-to-SQL (Spider) (Yu et al., 2018), graduate school math (Gsm8k) (Cobbe et al., 2021), Python code generation (Code-search-Python) (Husain et al., 2019), and financial question answering (Alpaca-finance) (Bharti, 2023). In all experiments, we set the number of proposed tokens to 5 for speculative decoding. For all online experiments, we fix the update interval II at 8.

In this section, we assess the efficacy of employing knowledge distillation to train a small model specifically for speculation in an offline environment. In such a setting, the speculative MqM_{q} model has unrestricted access to the dataset, and the query distribution remains stable. To emulate these offline conditions, we distill the MqM_{q} using the training dataset for two epochs and subsequently evaluate its performance by measuring the average token acceptance rate (α\alpha) on the test set. As detailed in Section 4.1, we evaluated various sampling methods, namely teacher sampling, student sampling, and mix token-level sampling. Table 1 displays the token acceptance rate of the draft model for each method, using forward KL as the distance metric on the test dataset. For comparison, we also provide the acceptance rate for teacher-generated label fine-tuning and the original model.

For both the Vicuna-7B and FLAN-T5-XL models, the teacher sampling method outperforms others by achieving the highest acceptance rate. Furthermore, knowledge distillation has proven its efficacy in enhancing the draft model’s approximation, resulting in a high token acceptance rate. Intriguingly, we also find that fine-tuning with teacher-generated labels yields impressive performance on the Vicuna-7B model. Lastly, we experimented with different distance measurements like reverse KL and JSD. Nevertheless, these measurements either paralleled or underperformed when compared to forward KL. Such empirical findings underscore that the optimal distance measurement or sampling method varies depending on the task and model, and we leave to future work to find the best combination.

2 Online Evaluation

Online Learning. First, we evaluate the effectiveness of our online algorithm by addressing two key questions: (1) Does the online algorithm increase the token acceptance rate? And is this enhancement comparable to the rates achieved in offline settings, which serve as an upper bound given their full access to data? (2) How quickly does the online algorithm increase the token acceptance rate, thereby indicating that the compact model has grasped the underlying distribution?

In our approach, we replicate the online serving process by iterating through the datasets, extracting prompts, and streaming generation requests. The system utilizes speculative decoding for each of these requests. Throughout this serving phase, we continually refine the speculative models, as detailed in Algorithm 1. For our baseline, we envision a scenario where the serving system has the capability to collect data offline in order to distill an initial draft model. This model is subsequently deployed online to cater to future requests. This process is simulated by using 10% of the dataset to distill the draft model, which remains static during online serving. For evaluation metrics, we calculate token acceptance rates averaged over the most recent 50 requests. This demonstrates MqM_{q}’s efficacy on the most current data.

As depicted in Figure 2, both for Vicuna-7B and FLAN-T5, in the beginning, OSD yields a lower token acceptance rate in comparison to the offline distilled model. Nevertheless, these acceptance rates rise swiftly as the draft model is exposed to more data. We also annotate the token acceptance rate from the offline setting to highlight the potential peak performance that the online serving system could reach. In all instances, the online context can achieve comparable results. In some scenarios, OSD even surpasses the token acceptance rate of the offline test alphas. This discrepancy can be attributed to the fact that offline test alphas are assessed on the entire test dataset, whereas the online alphas represent the moving average of the latest 50 requests. It’s plausible that OSD performs optimally on specific data subsets, particularly if those subsets are more narrowly distributed than the complete dataset.

Distribution Shifts. We evaluate OSD’s ability to adapt to changes in data distribution. We detail the dataset preparation in Appendix A.5. As illustrated in Figure 4, OSD’s alpha value dips notably at distribution boundaries, especially around 2K, 4K, and 6K records. This is anticipated since the draft model initially struggles when faced with a new distribution. However, the alpha value rebounds quickly as OSD processes more data, highlighting its adaptability to shifting query distributions.

We also compared our results to those from a static setting. To ensure the draft model wasn’t just memorizing data, we chose samples distinct from the online evaluation data. These samples correspond to 30%, 50%, 70%, and 100% of each dataset’s online evaluation volume, at 0.6K, 1K, 1.4K, and 2K quantities respectively. As depicted in Figure 4, upon an initial shift in query distribution, OSD’s performance aligns with or slightly trails the distillation with 30% data. However, it quickly catches up, matching or even surpassing performances seen with 70% to 100% data access. This highlights OSD’s ability to rival models fully exposed to the query distribution, even without intimate knowledge of the underlying query dynamics.

Real Workloads. We evaluate OSD on real LMSYS-chat conversations (Appendix A.6) that span 4 months. First, we categorize conversations based on the language and we focus on conversations among the top five languages, excluding English. For every chosen language, we use an independent LLaMA-160M to serve as our draft model. All draft models share the same Vicuna-7B as the target model. The token acceptance rate, averaged over the latest 100 requests, showed in Figure 5, reveals that OSD’s enhances rates by 0.1 to 0.2, even with under 2K data points. Notably, Japanese was the easiest while Portuguese was the toughest. We also clustered English conversations by topics using the fine-tuned distilled Bert model (Luqmani, 2023), focusing on the top five. For topics with over 5K conversations, we sampled evenly to keep it within 5K. Figure 5 shows acceptance rates above 0.6 across topics, with Social and Computer discussions peaking near 0.8.

3 Qualitative Analysis

In this section, we conduct a comprehensive analysis to understand how our method enhances the token acceptance rate, and which tokens the draft model acquires across varying query distributions.

High-frequency tokens precision and recall. In our experiment using the Spider dataset, Vicuna-7M is the target model and LLaMA-160M the draft. We identify the top 100 tokens most frequently generated by the target model, which account for 72.2% of all appearances, following a power-law distribution. Figure 6 shows a marked improvement in both accuracy and recall of these tokens after distillation on the test dataset in an offline evaluation.

Tokens learned across different datasets In our study, we analyze the top 10 tokens with the most pronounced accuracy and recall improvements across various datasets, focusing on the 100 most frequent tokens to understand the draft model’s learning trends. As detailed in Table 2, the improved tokens align well with the underlying data distribution. For example, in the Spider dataset, which frequently generates SQL statements, tokens like SELECT and WHERE have notably higher acceptance rates post-distillation. Similarly, in the Graduate Math dataset (Gsm8k), tokens such as <<, >>, =, and + stand out. These patterns highlight the draft model’s ability to adapt and predict tokens consistent with the data distribution.

Conclusion

Speculative decoding’s efficiently hinges on the draft model’s approximation to the target model. We introduce an online speculative method that continuously enhances the draft model based on varying data distributions. Experiments on both synthetic and real data demonstrate that online speculative decoding swiftly adapts to new data distributions, significantly enhancing token acceptance.

References

Appendix A Appendix

As proved in Leviathan et al. (2023), compared with standard decoding, the expected improvement factor for offline speculative decoding is 1αk+1(1α)(ck+1)\frac{1-\alpha^{k+1}}{(1-\alpha)(ck+1)}. Let the time taken for a single run of MpM_{p} be TT. Define cc, the cost coefficient, as the ratio of the time taken for a single run of MqM_{q} to that of MpM_{p}. Each execution of lines 7 to 8 takes Tck+TTck+T and, on average, yields 1αk+11α\frac{1-\alpha^{k+1}}{1-\alpha} tokens. As a result, the average time to produce one token using speculative decoding is given by (ck+1)(1α)1αk+1T\frac{(ck+1)(1-\alpha)}{1-\alpha^{k+1}}T. In contrast, the time to produce a single token using standard decoding is TT. Hence, the wallclock time reduction of offline speculative decoding can be described as 1αk+1(1α)(ck+1)\frac{1-\alpha^{k+1}}{(1-\alpha)(ck+1)}.

A.2 Latency Analysis

Suppose OSD can improve the token acceptance rate from α1\alpha_{1} to α2\alpha_{2} and TT is the generation time for standard decoding. Based on Equation 2, this improvement leads to a decrease in the average generation time for each token, transitioning from (ck+1)(1α1)1α1k+1T\frac{(ck+1)(1-\alpha_{1})}{1-\alpha_{1}^{k+1}}T to (ck+1)(1α2)1α2k+1T\frac{(ck+1)(1-\alpha_{2})}{1-\alpha_{2}^{k+1}}T. Consequently, this results in a speedup factor of 1α2k+11α1k+11α11α2=1+α2+α22+...+α2k1+α1+α12+...+α1k\frac{1-\alpha_{2}^{k+1}}{1-\alpha_{1}^{k+1}}\frac{1-\alpha_{1}}{1-\alpha_{2}}=\frac{1+\alpha_{2}+\alpha_{2}^{2}+...+\alpha_{2}^{k}}{1+\alpha_{1}+\alpha_{1}^{2}+...+\alpha_{1}^{k}} compared to standard speculative decoding.

In the aforementioned analysis, we omitted the additional latency due to updating the smaller model for the following reasons: (1) As illustrated subsequently, the additional computational cost (FLOPs) from the update remains marginal when juxtaposed with the computational demands of running the larger model. (2) Updates are periodic, during times of moderate request loads, the latency for serving individual requests remains largely unaffected. Additionally, given that the update operation for the smaller model is considerably less resource-intensive than inference, the associated latency might be seamlessly masked, rendering it virtually imperceptible. Lastly, the processes of updating and inference can even be executed concurrently on separate devices.

A.3 Flops Analysis

The FLOPs required to update the draft model are significantly fewer than those needed for inference on a large model. Denote LL as the average length of the generated sequence. For each verification, the draft model suggests kk tokens. The expected length for a single run of the target LLM, denoted as aa, can be calculated using Equation 2. Therefore, OSD undergoes the verification process La\frac{L}{a} times, with each time verifying k+1k+1 tokens. We use FqfwdF_{qfwd} to represent the arithmetic operations required by a singular forward run of the draft model for each token, and FpfwdF_{pfwd} stands for the FLOPs needed for a single forward run of the target model per token. Therefore, the computational demand (in FLOPs) for the draft and teacher models to handle one request can be expressed as: FLOPs(draft)=La×k×Fqfwd,FLOPs(target)=La×(k+1)×Fpfwd.\text{FLOPs}(draft)=\frac{L}{a}\times k\times F_{qfwd},\text{FLOPs}(target)=\frac{L}{a}\times(k+1)\times F_{pfwd}. Let’s consider the FLOPs required to update the student model per token as FqbwdF_{qbwd}. The cumulative FLOPs necessary to process II requests is given by:

Based on the findings of Kaplan et al. (2020), training is approximately three times costlier than inference. This translates to roughly 6 FLOPs per parameter for training on a single token and 2 FLOPs per parameter for inferring on one token. Thus, we can simplify the total FLOPs expression to:

The proportion of FLOPs needed to run the target model to that of the draft model is given by:

For the two model pairs evaluated, assuming an average of 5 proposed tokens per run: (1) (LLaMA-160M, Vicuna7B) with an average acceptance rate of 0.71, the ratio is approximately (5+1)×7B(5+3×3)×160M=18.75\frac{(5+1)\times 7B}{(5+3\times 3)\times 160M}=18.75. (2) (T5-small 80M, Flan-T5-XL 3B), with an average acceptance rate of 0.76, the ratio is roughly (5+1)×3B(5+3×4.3)×80M=12.6\frac{(5+1)\times 3B}{(5+3\times 4.3)\times 80M}=12.6.

In practical systems, the FLOPs required for inference are significantly below the machine’s capacity. Consider the LMSYS-Chat-1M Zheng et al. (2023b). It comprises traces spanning 125 days with 1000,000 requests, averaging less than 2,000 tokens per request (including both prompts and responses). When serving a 30B model with 8 A100 GPUs, the FLOPs consumed per second can be estimated as (Still, we estimate 2 FLOPs per token per parameter):

On the other hand, 8 A100 GPUs offer a combined capacity of 8×312 TFLOPs8\times 312\text{ TFLOPs}, and the computational utilization is notably low. While Arena (the platform that generates LMSYS-Chat-1M) may not be the most efficient and might lack substantial traffic, it’s the only publicly accessible LLM service trace. Even after amplifying the load multiple times, based on the above calculations, the computation efficiency remains limited.

A.4 Data Mix

Moreover, there is a question of whether the draft model, once adapted to the new distribution, might lose its prior knowledge. To probe this, we conducted an experiment mixing 2k prompts each from the Gsm8k and Alpaca-finance datasets. During online serving, for the initial 2k requests, we only update the model based on data from the Gsm8k dataset. For the subsequent half of the requests, we restrict updates solely to data from the Alpaca-finance dataset. We then provide the average token acceptance rates for all requests, segmented by their data source (Gsm8k versus Alpaca-finance). As depicted in Figure 7, the token acceptance rate for Gsm8k increases as the draft model is exposed to more data. Conversely, the acceptance rate (α\alpha) for the Alpaca-finance dataset remains consistent. This is anticipated since we only update the draft model using Gsm8k data. In the latter half of the dataset, the token acceptance rate for the Alpaca-finance dataset also shows an uptrend. Intriguingly, the rate for Gsm8k remains consistent, suggesting that the draft model retains its learned knowledge without showing signs of forgetting.

A.5 Data Preparation for Distribution Shift Analysi

To emulate this shift in distribution, we select 2k prompts from each dataset under evaluation. T he data from the four datasets are amalgamated by direct concatenation, such that the records from i×2ki\times 2k to (i+1)×2k(i+1)\times 2k belong solely to dataset ii.

A.6 Arena Dataset

For expedited experimental evaluation, we randomly sample a subset with 10K records from LMSYS-Chat-1M Zheng et al. (2023b), a comprehensive real-world LLM conversation dataset. This dataset encompasses interactions with 25 models spanning from April to August 2023 and features conversations in over 150 languages. For all experiments, we only pick conversations for Vicuna models.