Layer-Condensed KV Cache for Efficient Inference of Large Language Models (2024)

Abstract: Huge memory consumption has been a major bottleneck for deploying high-throughput large language models in real-world applications. In addition to the large number of parameters, the key-value (KV) cache for the attention mechanism in the transformer architecture consumes a significant amount of memory, especially when the number of layers is large for deep language models. In this paper, we propose a novel method that only computes and caches the KVs of a small number of layers, thus significantly saving memory consumption and improving inference throughput. Our experiments on large language models show that our method achieves up to 26× higher throughput than standard transformers and competitive performance in language modeling and downstream tasks. In addition, our method is orthogonal to existing transformer memory-saving techniques, so it is straightforward to integrate them with our model, achieving further improvement in inference efficiency. Our code is available at https://github.com/whyNLP/LCKV.

heading: 1 Introduction
text: High throughput and low latency are essential for deploying large language models (LLMs) in realworld applications (Tillet et al., 2019; Kwon et al., 2023). However, the huge memory consumption of LLMs has been a major bottleneck, preventing a large batch size and high throughput generation. Among the memory-consuming components, the key-value (KV) cache is one of the most significant parts (Pope et al., 2023; Zhang et al., 2023) that takes over 30% of the GPU memory during deployment (Kwon et al., 2023). The KV cache is used to store the keys and values in each transformer layer during generation to avoid re-computation. Its memory consumption is proportional to both the sequence length and the number of layers.
* Corresponding author.
There have been substantial works on reducing the memory consumption of the KV cache in LLMs. Most of them focus on compressing the KV cache by reducing the length of the cached KV sequence. For example, Jiang et al. (2023a); Li et al. (2023); Mu et al. (2023) compress the prompts to save the memory consumption. Ren et al. (2023) incrementally compress a specified span of tokens into compact ones to reduce the KV cache length. Xiao et al. (2024); Han et al. (2023) propose to store only the KVs of initial and recent tokens in the KV cache. Zhang et al. (2023) propose a dynamic KV cache eviction policy to only keep a small portion of the KV cache in memory.
In this paper, we propose to reduce the memory consumption of the KV cache from a novel perspective that is orthogonal to previous efforts: reducing the number of layers. Specifically, we present a new variant of transformer decoders in which queries of all layers are paired with keys and values of just the top layer, as illustrated in Figure 1. In this way, we only need to cache the keys and values of one layer (vs. tens of layers in a typical LLM), significantly saving memory consumption while introducing no computation overheads dur-
11175
ing inference. In fact, since we only need the keys and values of the top layer, we can omit the keyvalue computation and discard the key-value parameters for all the other layers, further improving the throughput and reducing the memory consumption as well as the mode size.
We draw our inspiration from the interpretation of the stacking layer structure of a transformer as an iterative process of improving token representation (Wu and Tu, 2023). In this interpretation, the representation at the top layer is the most informative, so it makes sense to attend only to the top layer. We also note the similarity of our idea to the cross-attention mechanism in a standard transformer encoder-decoder, in which all the decoder layers attend to the top encoder layer. However, applying this idea to a decoder has never been attempted before as far as we know.
Although our model presented above achieves very efficient inference, its performance in language modeling and downstream tasks degrades in comparison with standard transformers. Therefore, we further propose to retain standard attention for a small number of layers in our model, which slightly diminishes our saving of the KV cache memory consumption but leads to almost no performance degradation.
Another challenge faced by our model is training. When training a standard transformer decoder, the computation of all the tokens can be fully parallelized. In our model, however, the computation at each token depends on the top layer of the previous token, creating sequential dependencies that spoil parallel training. We address the challenge by deriving an approximate training method that supports parallel training.
Our experiments on Llama (Touvron et al., 2023) show that our model achieves up to 32× larger batch sizes and up to 26× higher throughput than standard transformers for LLMs of 1B–30B parameters; at the same time, our model has competitive performance to standard transformers in language modeling and downstream tasks. We further empirically demonstrate that it is straightforward to integrate our model with other memory-saving techniques like StreamingLLM (Xiao et al., 2024), achieving further improvements in inference efficiency.
We summarize our contributions as follows: 1) we propose a new variant of transformer decoders that reduces the memory consumption of the KV cache by dramatically reducing the number of
cached layers; 2) we make parallel training of our model feasible by designing a novel approximate training method; 3) we conduct extensive experiments to verify and analyze the effectiveness and efficiency of our method.

heading: 2 Layer-Condensed KV Cache

heading: 2.1 Model
text: As shown in Figure 1(b), we pair the queries of all layers with KVs of only the top layer, so that we do not have to cache or even compute KVs for layers other than the top layer, saving both memory consumption and computation. Furthermore, since we no longer need to compute KVs for these layers, nor do we need to keep the weights WK ,WV that map hidden representations to KVs for these layers, thus also saving model parameters.
One problem with this method is that, since each token also attends to itself, we need its top-layer KVs for its attention computation at lower layers, but the top-layer cannot be computed until we finish the computation of all the lower layers. A straightforward solution to this cyclic dependency problem is to drop the attention of each token to itself, which is equivalent to masking the diagonal of the attention matrix. Now the first token of the sequence has nothing to attend to, so we just use zero vectors as dummy KVs in its attention computation. Note that even without self-attention of each token, its information can still be incorporated in its bottom-up computation thanks to residual connections. Empirically, we find that the diagonal mask of the attention matrix does not affect the performance of the model.
It has been previously observed that transformers tend to attend to syntactic information in lower layers and semantic information in higher layers (Clark et al., 2019b). Intuitively, applying KVs of the same layer to queries of all layers might break this pattern. Empirically, we do find that our method as described above underperforms standard transformers in language modeling and downstream tasks. A simple yet effective solution to this problem is to retain standard attention for a small number of layers, which we call warmup layers, and only apply our method to the rest of the layers. Inspired by the sandwich module of Reid et al. (2021), we propose to keep the top w/2 layers and the bottom w/2 layers as warmup layers. We empirically find that such a sandwich configuration outperforms alternative configurations and leads to
almost no performance degradation compared with standard transformers.

heading: 2.2 Training
text: Though the inference process of our model is straightforward and almost identical to that of a standard transformer, i.e., decoding one token at a time from left to right, the training process of our model is more complicated. Since each token relies on the KVs of the top layer of previous tokens, it is impossible to train on a sequence of tokens in parallel as in a standard transformer. Below we derive a parallel training process of our model in three steps. For simplicity, we assume no warmup layers (i.e., w = 0). It is straightforward to add warmup layers in the derived training process.

heading: 2.2.1 From Sequential to Parallel Training
text: During training, we minimize the cross entropy loss for each token. In our model, the computation of each token depends on the top-layer KVs of its previous tokens. Therefore, for a token sequence of length n, training is done sequentially on a computation graph consisting of n passes of bottom-up transformer computation, as shown in Figure 2(a).
In the following, we present a different training computation graph and show its equivalence to the original computation graph. Specifically, we perform n iterations of bottom-up transformer computation on all tokens in parallel, and in each iteration, we pair the queries of all layers with KVs of the top layer from the previous iteration, thus breaking the sequential dependencies within the iteration. We compute the cross entropy loss only after the last iteration. Note that the first iteration has no previous iteration, and we pair its queries with dummy KVs which are zero vectors. Figure 2(b) shows the new computation graph. Theorem 1. The two computation graphs are equivalent in terms of model training.
We leave the proof of the theorem to Appendix A. Here, we provide an intuitive explanation. We say the computation of a token (w.r.t. its hidden representations and top-layer KVs) is correct in an iteration of the second computation graph if and only if it is identical to that in the first computation graph. Since we have masked the diagonal of the attention matrix, the computation of the first token does not rely on any key or value (except for dummy zero vectors) and thus is correct in every iteration. For the second token, its computation relies on the KVs of the first token and thus is correct
starting from the second iteration. In general, the computation of the i-th token relies on the KVs of the first i− 1 tokens and by induction, it is correct starting from the i-th iteration. Therefore, after n iterations, all the tokens are correctly computed. As a result, the computation sub-graphs of the crossentropy losses of all the tokens are identical in the two graphs, and hence training processes following the two graphs are equivalent.
Essentially, the second computation graph replaces horizontal dependencies in the first graph with vertical dependencies, and it does not change the length of the longest dependency chain. Consequently, although the second computation graph supports parallel training over all the tokens, it still requires the same n iterations as the first graph. Next, we will trim the iterations first in terms of backpropagation and then in terms of forward propagation.

heading: 2.2.2 Backpropagation: Gradient Stopping
text: We compute the cross entropy loss after the last iteration, which backpropagates through n iterations, resulting in a large computation graph that is impossible to fit into GPU memory for large n. To solve the issue, we follow the practice of gradient stopping in Transformer-XL (Dai et al., 2019) and backpropagate the loss only through the last b iterations (b ≪ n).
Notice that the KVs used in the last iteration come from the second-to-last iteration, so if we set b = 1, then backpropagation would not reach the
KVs and hence the model parameters used to calculate the KVs are not trained at all, which would result in a large performance degradation. We empirically find that with b ≥ 2, the performance of our model is comparable with that of a standard transformer. To reduce memory consumption, we set b = 2 by default, which means we only backpropagate through two iterations.

heading: 2.2.3 Forward Propagation: Fast Convergence of KV
text: With gradient stopping, the first n − b iterations are solely used in forward propagation to compute KVs that are fed into the last b iterations. When n is large, it is still too costly to run n−b iterations of forward propagation. Fortunately, we observe that the values of KVs converge very fast over iterations and hence we do not have to run n − b iterations to obtain the final KVs. Figure 3 shows the convergence of KVs of a randomly initialized model with the same configuration as TinyLlama (Zhang et al., 2024). The input is a randomly picked segment of text from the MiniPile (Kaddour, 2023) dataset with 2048 tokens (i.e., n = 2048). We measure the change of KVs over consecutive iterations using the mean squared error. As can be seen, KVs converge after only a few tens of iterations, with more warmup layers leading to faster convergence. Therefore, we use m iterations (m ≪ n) to approximate the KVs of n− b iterations. We empirically find that m = 7 is sufficient for model training and larger values of m do not further improve the performance.

heading: 2.3 Inference with Prompts
text: It is straightforward to employ our model for autoregressive generation. However, our model cannot do parallel encoding of prompts like standard
transformers for the same reason as it cannot do parallel training. Fortunately, since iterative computation of KVs is fast to converge, we can just perform iterative computation for the prompts for m+ b iterations. Typically, m+ b is far less than the number of tokens to generate, and thus the extra time spent in encoding the prompts is negligible.

heading: 3 Experiments
text: We empirically verify the effectiveness of our method on the Llama model (Touvron et al., 2023). We show that our method achieves significant memory reduction and throughput improvement as well as competitive performance in language modeling and downstream tasks compared with standard transformers. We also show that our method could effectively integrate with other memory-saving techniques.

heading: 3.1 Generation Throughput
text: We test our method with 1.1B, 7B, and 30B parameters on an NVIDIA GeForce RTX 3090 (24GB) GPU and an NVIDIA A100 (80GB) GPU respectively. The 1.1B model configuration follows that of TinyLlama (Zhang et al., 2024) and the 7B and 30B model configuration follows that of the original Llama (Touvron et al., 2023). We set m = 7, b = 2 and w = {2, 10}. Our implementation is based on HuggingFace Transformers (Wolf et al., 2020) with kernel replacement with FlashAttention 2 (Dao, 2023), fused RMS norm, fused cross-entropy and fused SwiGLU.
Following FlexGen (Sheng et al., 2023), the evaluation is conducted in an end-to-end fashion. For a prompt of length s, we let the model generate output sequences of length n with batch size b. The latency t is defined as the total time in seconds spent in processing the prompts and generating all the bn tokens. The generation throughput is defined as bn/t tokens per second.
Table 1 compares the maximum batch sizes and throughput of standard Llama models and our models on the two types of GPUs. Note that some of the sequence lengths exceed the maximum input lengths that the models are trained on, but that does not affect batch size and throughput measurement. We still benchmark the batch sizes and throughput to show the potential of our method on other models allowing larger sequence lengths. It can be seen that our method achieves significantly larger batch sizes and higher throughput on all of the settings.
Notice that the maximum throughput is not necessarily achieved using the maximum batch size. Figure 4 shows the throughput of 7B Llama and our model on an A100 GPU w.r.t. the batch size. The prompt length and generation length are both 2048. We find that when the batch size grows larger than 32, the throughput no longer increases and even decreases with a batch size of 128. This may indicate that the model operations are turning from memory-bound into compute-bound (Dao et al., 2022) and the throughput is limited by the computation power. Further improvement in throughput may require more efficient computation kernels instead of larger batch sizes.
We also would like to mention that the increased throughput is not necessarily due to the increased batch size. As shown in Figure 4, our model has much higher throughput than Llama even with the same batch sizes. We leave the detailed analysis to Appendix C.6.

heading: 3.2 Model Performance
text: To evaluate the performance of our model in language modeling and downstream tasks, we pretrain from scratch two 1.1B models with m = 7, b = 2 and w = {2, 10}. We use TinyLlama as our baseline, whose size is also 1.1B. We pre-train the models on a 100B subset of the SlimPajama dataset (Soboleva et al., 2023). The training details are consistent with those of TinyLlama (Zhang et al., 2024). All models are trained with AdamW (Loshchilov and Hutter, 2019) with β1 = 0.9 and β2 = 0.95. The batch size is 2M tokens. We use a cosine learning rate schedule with a maximum learning rate of 4.0× 10−4 and a warmup of 200 steps. The final learning rate is 4.0 × 10−5. We use a weight decay of 0.1 and gradient clipping of 1.0. The models are trained on 128 NVIDIA A800 (80GB) GPUs.
During evaluation, we perform inference in the standard left-to-right fashion instead of using the method of Section 2.3. We report the perplexity on a 10M subset of the development set of SlimPajama. We also test the zero-shot performance on commonsense reasoning tasks following Zhang et al. (2024), including Hellaswag (Zellers et al., 2019), OpenBookQA (Mihaylov et al., 2018), WinoGrande (Sakaguchi et al., 2021), ARC-Easy and ARC-Challenge (Clark et al., 2018), BoolQ (Clark et al., 2019a), and PIQA (Bisk et al., 2020). The tests are conducted using the lm-eval-harness framework (Gao et al., 2023). For these tasks, we encode the prompts with the same number of iterations (m+ b = 9) as in training.
Table 2 and 3 show the results. The performance of our models is comparable to that of TinyLlama. In particular, our model with w = 10 has almost no performance degradation, while achieving significantly higher generation throughput as evaluated in Section 3.1. Our model with w = 2 has a small but noticeable decrease in performance for most of the tasks, but it achieves even higher increase in throughput.
Despite the competitive performance and higher inference efficiency of our models, we note that pre-training our model costs about 3 times the time of pre-training TinyLlama with the same amount of data due to the iterative training process. Nevertheless, we believe that in most scenarios, a speedup in inference is worth a slowdown in training which is a one-time process.

heading: 3.3 Integration with StreamingLLM
text: We have previously mentioned that our method is orthogonal to other memory-saving techniques and can be easily integrated with them. Here we inte-
grate our method with StreamingLLM (Xiao et al., 2024). StreamingLLM employs an attention sink that only preserves the KV cache of the first few tokens (four by default) and recent tokens, which empowers LLMs to process infinite-length inputs.
As shown in Figure 5, the integration of StreamingLLM and our model (w = 10) achieves lower latency and memory consumption compared to the original StreamingLLM on different cache sizes (numbers of cached tokens).
We further showcase that integration with our method does not hinder the ability of StreamingLLM to process infinite-length tokens. Specifically, we integrate StreamingLLM into our model (w = 10) trained in Section 3.2
with different cache sizes and find that the integrated model achieves even lower perplexities than StreamingLLM as shown in Figure 6. We also let the model handle inputs with a sequence length of four million tokens. As shown in Figure 7, the integrated model can effectively process the input with the perplexity remaining stable.

heading: 4 Analyses
text: In this section, we empirically analyze design choices in our method. For experiment details, please refer to Appendix B.

heading: 4.1 The Sandwich Configuration
text: In Section 2.1, we propose to add some warmup layers to improve model performance. Note that inference efficiency is determined by the number of warmup layers w and not by their placement. Here we ask the following question: with the number of warmup layers fixed, where should we place the warmup layers to achieve the best performance?
Table 4 shows the model performance in language modeling with two warmup layers (i.e., w = 2) that are placed at the bottom, at the top, and with the default sandwich configuration. It can be seen that the sandwich style performs the best. A possible explanation is that top and bottom layers serve different functionalities (e.g., semantic
vs. syntactic) and it makes more sense to warm up both than just one of them.

heading: 4.2 Number of Warmup Layers
text: Warmup layers serve as a bridge between the standard transformer and our model. The more warmup layers we keep, the more similar it is to the standard transformer, and the less memory we save. In this section, we ask the following question: how does the number of warmup layers w affect the model performance and throughput?
We test the model with 1.1B (22 layers) parameters and different numbers of warmup layers (Figure 8). Surprisingly, we find that the log dev perplexity does not monotonically decrease with the number of warmup layers. Without warmup layers (the red region), the model performance is significantly worse than that of the standard transformer. With only a few warmup layers (the yellow region), the model performance is greatly improved and close to that of the standard transformer, but its throughput is decreased at the same time. With more warmup layers (the green region), the model even outperforms the standard transformer, while suffering from further decrease in throughput.
The results point to a trade-off between model performance and throughput that is controllable by the number of warmup layers. If pursuing a high throughput, one could keep only a few warmup layers (the yellow region). If good performance is crucial, one could keep more warmup layers (the left part of the green region).

heading: 4.3 Convergence of KV
text: In Section 2.2.3, we have shown that KVs converge very fast for a random model and hence we use m ≪ n iterations to compute KVs. Here, we ask the following questions: how fast do KVs converge for a trained model and what value shall we pick for m?
We measure the convergence of KVs of a 50M Llama (Figure 9, bottom) and the 1.1B model (w = 2) pre-trained in Section 3.2 (Figure 10). It can be seen that while a random model requires 15–20 iterations to converge, a trained model requires far fewer iterations. This hints at a small value of m especially during late stages of training. We then evaluate model performance when trained with different values of m (Figure 9, top). It can be seen that the model performance converges with m ≥ 6, with more warmup layers leading to faster convergence. This justifies our default choice of m = 7.

heading: 5 Related Work
text: Extensive research has been done on reducing KV cache for efficient inference of LLMs. With the exception of vLLM (Kwon et al., 2023), which proposes paged attention to reduce memory fragmentation of the KV cache from a system perspective, most recent works focus on compressing the KV cache by reducing the length of the cached KV sequence. Jiang et al. (2023a,b) accelerate model
inference by compressing document-level prompts into short prompts. Li et al. (2023) remove the redundancy in the input context. Mu et al. (2023) train gist tokens to replace the reusable system prompts. Ren et al. (2023) incrementally compress a specified span of tokens into compact ones to reduce the KV cache length. Liu et al. (2023) find that only pivotal tokens have a significant influence at a future step, so pruning unimportant tokens does not affect the performance. Ge et al. (2023) argue that the attention heads could be classified into different types and propose to apply different KV pruning strategies to different types of attention heads. Xiao et al. (2024); Han et al. (2023) find that only initial tokens and recent tokens are crucial and propose to store only the KVs of these tokens to enable infinite-length context for LLMs. Zhang et al. (2023) propose a KV cache eviction policy based on the summation of attention scores to only keep a small portion of the KV cache in memory. Unlike these previous methods, our method reduces the memory consumption of the KV cache by reducing the number of layers, which is orthogonal to these methods and can potentially be combined with these methods to further reduce the KV cache and improve inference efficiency.
Feedback Transformers (Fan et al., 2020) aggregate hidden representations from all layers and use them as token memory. This is followed by KV projections to obtain the key-value pairs for all layers. Their experimental results show improved performance with this strategy, even when using only the hidden representation from the top layer. However, their sequential training process is time-costly and not practical for large models. Our method supports parallel training, which is more efficient and scalable.

heading: 6 Conclusion
text: In this paper, we propose a novel method to reduce the memory consumption and improve the throughput of LLMs by reducing the number of layers whose keys and values need to be computed and cached. We empirically show that our method achieves significant memory reduction and throughput improvement with negligible performance degradation. We also show that our method could effectively integrate with other memory-saving techniques like StreamingLLM. Future work includes designing more efficient training approaches, developing large-batch-friendly kernels, and verifying our method on larger and more complex LLMs. We hope that our work could provide a new perspective for improving inference efficiency of LLMs and inspire more research in this direction.
Limitations
Though our method achieves impressive memory reduction and throughput improvement, we would like to point out its limitations from the following aspects:
• Due to the iterative training, our model requires about 3× the time to pre-train a model with the same amount of data. In other words, our method improves the inference efficiency at the cost of the training efficiency. A potential remedy is that if one has a pre-trained model, one could use it to initialize our model, which is empirically found to speed up the process of training.
• Since our method requires iteratively processing the prompts, the throughput degrades when the prompts are much longer than the generation length, e.g., in document summarization. Generally, our method is more suitable for tasks with a large generation length, such as translation, dialogue, question answering, CoT problem solving, etc.

heading: A Proof of The Training Theorem
text: Here we formally prove Theorem 1 in Section 2.2. Remember that we need to compute the loss for each token sequentially, but we propose to train all the tokens in parallel with n iterations.
Theorem 1. The two computation graphs are equivalent in terms of model training.
To prove the theorem, we first introduce the following lemma.
Lemma 1. In the first computation graph, denote the final hidden representation of the i-th token as hi = f1,i(x1, x2, . . . , xi). In the second computation graph, denote the final hidden representation of the i-th token in iteration t as h(t)i =
f (t) 2,i (x1, x2, . . . , xi). We have f1,i = f (t) 2,i , ∀t ≥ i, no matter what the initial KVs are.
Proof. In the first computation graph, denote the KVs of the i-th token as KV i and that under parallel training with iteration t as KV (t)i .
Since the basic networks are the same for the two computation graphs, we further denote gi as hi = f1,i(x1, x2, . . . , xi) = gi(x1, x2, . . . , xi,KV 1,KV 2, . . . ,KV i−1), as well as h(t)i = f (t) 2,i (x1, x2, . . . , xi) = gi(x1, x2, . . . , xi,KV (t−1) 1 , . . . ,KV (t−1) i−1 ). The only difference in the computation of the final hidden representations lies in the KVs.
We prove the lemma by induction. For the base case, since we have removed the diagonal of the attention matrix, h1 and h (t) 1 does not rely on any key or value. So we have h1 = f1,1(x1) = gi(x1) = f (t) 2,1(x1) = h (t) 1 , ∀t. That is f1,1 = f (t) 2,1,∀t. Since the computation graphs of the first token are the same, KV 1 and KV (t) 1 are the same for all t.
For the inductive step, we assume that ∀i ≤ T, f1,i = f (T ) 2,i = gi,KV i = KV (T ) i . Then for iteration T + 1: ∀i ≤ T + 1, the computation of the i-th token relies on the KVs of the first i − 1 tokens. That is, h(T+1)i = f (T+1) 2,i (x1, x2, . . . , xi) = gi(x1, x2, . . . , xi,KV (T ) 1 , . . . ,KV (T ) i−1).
Since i ≤ T + 1, we have i − 1 ≤ T . By induction we have KV (T )i−1 = KV i−1. Thus, we have gi(x1, x2, . . . , xi,KV (T ) 1 , . . . ,KV (T ) i−1) = gi(x1, x2, . . . , xi,KV 1, . . . ,KV i−1) = f1,i(x1, x2, . . . , xi) = hi. The computation graphs are the same for the first T + 1 tokens, then we have KV i = KV (T+1) i .
Thus, we have f1,i = f (t) 2,i , ∀i ≤ t. The proof does not rely on initial KVs, so the conclusion holds for any initial KVs.
Let t = n, we have the entire computation graphs are equivalent. Therefore, we have proved Theorem 1.
From the lemma, we could learn one more thing: The parallel training has the same computation graph as the sequential training when the iteration t ≥ n. This indicates that the parallel training is theoretically guaranteed to converge to the same solution as the sequential training. Once it is converged, it will not diverge. Our work finds that the KVs converge much faster than the theoretical n
iterations, which significantly reduces the training time.

heading: B Model and Training Details
text: We provide the model configurations and training details in Table 5 and 6. The 7B and 30B model configurations are consistent with those of the original Llama (Touvron et al., 2023). The 1.1B model configuration follows that of TinyLlama (Zhang et al., 2024). We use the WikiText103 (Merity et al., 2017) (licensed under CC-BYSA 3.0), MiniPile (Kaddour, 2023) (licensed under MIT) and SlimPajama (Soboleva et al., 2023) (various licenses depending on the data source) as our datasets. Our use of the datasets is consistent with
their intended use. The training of TinyLlama in Section 3 takes 14:42:59, while our models take 1 day, 16:44:16 (2.77x, w = 2) and 1 day, 15:52:38 (2.71x, w = 10), respectively. The training took place before we optimize our codes, so the training time could be further reduced, especially for the model with w = 10.

heading: C More Analyses
text: In this section, we provide more analyses beyond those in Section 4.
C.1 Initialize with Pre-trained Models Since our model structure resembles that of the standard transformer, we could initialize our model
with pre-trained models. For WK ,WV of the middle layers, we just ignore the parameters. Experiments show that initialization with pre-trained models could effectively speed up the training process (Table 7). Though our model has a different computation graph from the standard transformer, it could still benefit from the pre-trained models. Our model initialized with a TinyLlama checkpoint trained on 2.5T tokens achieves a perplexity of 8.514, which is much better than the randomly initialized model. It is even better than the TinyLlama checkpoint trained on 500B tokens. Thus, if the pre-trained models are available, initializing our model with them could save a lot of training time.
C.2 Iterations with Gradients
In Section 2.2.2 we set b = 2 by default. What if we use larger b? To make sure that the inference process is equivalent (specifically, encoding the prompts), we fix m+ b = 9 and experiment with different values of b.
Figure 11 shows the perplexity of a 50M Llama with different values of b. Though from b = 2 to b = 3 the perplexity decreases, for b = 4 the perplexity increases. We further confirm that for b = 4 the KVs do not converge as fast as for b = 2.
Experiments on a 1.1B model (Table 8) show that the perplexity increases with b and the training time also increases. From the training curve, we find that larger b leads to more unstable training, thus the model is harder to converge. Therefore, we set b = 2 by default.
C.3 KV Loss for Less Iterations
In Section 4.3 we have shown that the KVs converge very fast for a trained model, yet we still want to make it converge even faster, saving both training and inference time costs. An intuitive idea
is to add an MSE loss to the KVs before and after the last iteration to force the KVs to converge. We call this term the “KV Loss”. Our experiments (Table 9) show that for small data with small w, the KV loss could lead to better performance. While for large data or large w, the KV loss hurts the performance. This is probably because the KVs are not converged at the beginning of training and the KV loss helps the KVs converge. However, when the KVs are already converged, the KV loss could slow down the training process. In our method, we do not use the KV loss.
C.4 Encode Prompts with Different Number of Iterations
Though we set m = 7, b = 2 during training, it does not necessarily mean that we have to encode
the prompts with 9 iterations. Is it possible to encode the prompts with less iterations? What if we encode the prompts with more iterations?
We treat the token segments as prompts and test the model trained in Section 3.2 with different numbers of iterations during encoding. As shown in Figure 12, the perplexity increases when the number of iterations is reduced, but still in a reasonable scale if we only reduce one or two iterations. The more warmup layers there are, the more stable the performance is. Increasing the number of iterations does not noticeably affect the performance. Thus, one could make a trade-off to set the proper number of iterations during inference to balance the time encoding prompts and the quality of generation texts.
C.5 Model Performance With Respect to Token Position
The long-context performance of LLMs highly relies on KV cache (Xiao et al., 2024; Han et al., 2023; Adnan et al., 2024). To verify that the performance of our model does not degrade under long context, we test the perplexity of our model with different token positions on PG19 (Rae et al., 2020). Despite the fact that we only compute the KVs of a
few layers, the performance of our model does not degrade with the token position (Figure 13) and is comparable to that of TinyLlama. Due to the limitation of computational resources, we only train models with context length 2048.
C.6 Improvement of Throughput Not Necessarily Due to Larger Batch Size
In Section 3.1, we show that our model achieves higher generation throughput than standard transformers. While one might assume that this improvement is solely due to a larger batch size, Figure 4 demonstrates that our model outperforms the standard transformer even with the same batch size. Additionally, Table 10 reports reduced latency for our model when compared with the standard transformer with the same batch size. This suggests that our method can also benefit scenarios that require fast response. The exact reason for this phenomenon is not yet clear and we speculate that it could be attributed to factors such as the reduced calculation of KVs, the decreased memory consumption enabling faster memory transfer and access, and various implementation details.

heading: Future Work
text: Future work includes designing more efficient training approaches, developing large-batch-friendly kernels, and verifying our method on larger and more complex LLMs. We hope that our work could provide a new perspective for improving inference efficiency of LLMs and inspire more research in this direction. Limitations
Though our method achieves impressive memory reduction and throughput improvement, we would like to point out its limitations from the following aspects:
• Due to the iterative training, our model requires about 3× the time to pre-train a model with the same amount of data. In other words, our method improves the inference efficiency at the cost of the training efficiency. A potential remedy is that if one has a pre-trained model, one could use it to initialize our model, which is empirically found to speed up the process of training. • Since our method requires iteratively processing the prompts, the throughput degrades when the prompts are much longer than the generation length, e. g. , in document summarization. Generally, our method is more suitable for tasks with a large generation length, such as translation, dialogue, question answering, CoT problem solving, etc