Unlimiformer: Long-Range Transformers with Unlimited Length Input

Since the proposal of transformers, these models have been limited to bounded input lengths, because of their need to attend to every token in the input. In this work, we propose Unlimiformer: a general approach that wraps any existing pretrained encoder-decoder transformer, and offloads the cross-attention computation to a single k-nearest-neighbor (kNN) index, while the returned kNN distances are the attention dot-product scores. This kNN index can be kept on either the GPU or CPU memory and queried in sub-linear time; this way, we can index practically unlimited input sequences, while every attention head in every decoder layer retrieves its top-k keys, instead of attending to every key. We evaluate Unlimiformer on several long-document and book-summarization benchmarks, showing that it can process even 500k token-long inputs from the BookSum dataset, without any input truncation at test time. We demonstrate that Unlimiformer improves pretrained models such as BART and Longformer by extending them to unlimited inputs without additional learned weights and without modifying their code. We make our code and models publicly available at https://github.com/abertsch72/unlimiformer .

Paper

References (56)

Scroll for more · 38 remaining

Similar papers

Peer review

Reviewer 4zy35/10 · confidence 4/52023-07-06

Summary

This paper proposes to use k-nearest neighbor to extract nearest neighbor encoded tokens in pretrained encoder-decoder transformers. This helps in removing the bottleneck of limited input tokens thereby letting the dataset decide the input length. They show empirically that their proposed unlimiformer can extend to beyond 500K and work well with pretrained methods such as BART and Longformer.

Strengths

* The proposed method is a simple add on top of existing pre-trained models * Equation 2 is a somewhat novel rewrite of QK. * The authors performed extensive experiments showcasing that a) unlimformer works well on pretrained model, b) in many cases using the full dataset and then selecting the top-k tokens for cross attention results in increase in performance c) the computational increase is less than linear with increasing token size.

Weaknesses

* The idea of retrieving top-K most similar tokens to approximate the softmax has been explored in Reformer. They used LSH to retrieve top-K and that method should work even in the cross attention scenario. In their experiments they have experimented with 64K tokens. In the light of that the method proposed by this paper is not very dissimilar. This makes it hard to recommend accepting the paper. If the authors think that the proposed method is different please comment on it and it should have been compared in the experiments. * Another thing that is not clear to me is why the forward pass of the *encoder* is not quadratically increasing in time complexity as the number of tokens increase, and why this was not included in Figure 4. **** These questions were answered during the rebuttal phase

Questions

I will request the authors to please comment on the points raised in the weakness section. I will update my scores accordingly

Rating

5: Borderline accept: Technically solid paper where reasons to accept outweigh reasons to reject, e.g., limited evaluation. Please use sparingly.

Confidence

4: You are confident in your assessment, but not absolutely certain. It is unlikely, but not impossible, that you did not understand some parts of the submission or that you are unfamiliar with some pieces of related work.

Soundness

2 fair

Presentation

3 good

Contribution

2 fair

Limitations

N/A

Reviewer VEz66/10 · confidence 3/52023-07-06

Summary

The paper proposes Unlimiformer, a new method for increasing the context length of Transformers without any modification by using retrieval. The idea is simple, and immediately improves performance on benchmark tasks.

Strengths

The idea is simple, and the experiments show that augmenting Transformers with a retrieval system is useful. Inference cost increases sublinearly with the amount of input, which is helpful. Overall a solid idea and execution of the analysis.

Weaknesses

The major weakness is understanding whether the model can actually use all the context that it is being exposed to. It seems clear that using a nearest-neighbor based approach would limit the effectiveness of the context length to some fixed amount of input (once the KNN is "saturated" with nearby elements to the query it does not use further contexts). Figure 3 seems to suggest that the model does not use data beyond 32K context, or at least has a lot of trouble doing so. The trend is downwards after 32K until the 350K point at the end, which could be an outlier. A natural extension would be to adapt the KNN search in some way as the amount of context increases.

Questions

Do you have more evidence that all the context is used as you increase the context?

Rating

6: Weak Accept: Technically solid, moderate-to-high impact paper, with no major concerns with respect to evaluation, resources, reproducibility, ethical considerations.

Confidence

3: You are fairly confident in your assessment. It is possible that you did not understand some parts of the submission or that you are unfamiliar with some pieces of related work. Math/other details were not carefully checked.

Soundness

3 good

Presentation

3 good

Contribution

3 good

Limitations

Yes

Reviewer cHBL7/10 · confidence 3/52023-07-20

Summary

The paper proposes a method to increase context lengths of encoder-decoder transformers to very long input sequences. The idea is to essentially encode all of the tokens of the entire input (on overlapping context-length chunks) and create an index of the input tokens. Just prior to decoding, k-nearest neighbor (kNN) encoded tokens are selected from the index to maximize $QK^T$ dot product of the cross-attention phase, between the encoder and decoder hidden states, during decoding. One of the key contributions in this paper is that they re-order the computation of the matrix products in the cross-attention ($QK^T$) so that creating a single index of the encoder tokens is sufficient, and kNN look-up can be performed efficiently to compute ($QK_{kNN}^T \approx QK_{best}^T$). Additionally, their method has the advantage of being applied to existing pre-trained architectures without the need for additional weights or tuning. It can be applied directly during inference at test time. Applying it during training gives an additional performance boost.

Strengths

* The idea is good and the experimental evidence is strong. * The evaluation comparisons using “low-cost” tuning methods, i.e. applying this idea just during validation and testing is also clever. * The proposed method has interesting advantages, 1. While the idea of using kNN is not new, it has been done nicely and efficiently, 2. The method can also be applied to existing pre-trained encoder-decoder models during inference directly. 3. There's further boost in performance when applied during training.

Weaknesses

No major weaknesses in the overall quality of the contribution. There are minor weaknesses which the authors can address e.g. the clarity of the writing can be improved, results in some tables are incomplete etc. I have framed those as questions below for the authors to address.

Questions

1. The Fig. 2 shows encoder encoding disjoint chunks, whereas text lines L73-74 say it is overlapping chunks. Can you clarify which of these is correct, and change the figure to represent that more accurately? The figure, overall, can be improved. 2. For the k nearest neighbor indexing and search, you mention the use of the FAISS library. Do you use approximate nearest neighbors, or the exact nearest neighbors? Considering most contexts are fairly small, if you’re not using exact nearest neighbors, can you provide a small comparison to using exact nearest neighbors, and share whether or not it affects results (and/or time). 3. In the Comparison to Wu et. al. section, it would be good to highlight that Memorizing Transformer paper applies their approach to decoder-only models. Your approach is applied to the encoder-decoder model. 4. Train chunked +test Unlimiformer (L137-141) requires more details. e.g., I don’t understand what treating each chunk as its own training example entails. Is this what you do: For an example that’s longer than the context length, do you chunk it into say C non-overlapping chunks, treat each (chunk, output) pair as a training example (to create C examples), but allow retrieval over the tokens of all the C chunks during decoding? (After reading retrieval training, *I guess there is actually no retrieval happening over training data* in the Train chunked +test Unlimiformer) * 4.1 For Train chunked +test Unlimiformer, how does the duplication of some training examples because of the C (chunk, output) pairs figure into evaluation? Do you try to combine the C outputs to a single one, or take the average, or ignore the duplication and just treat them as more example pairs than what other methods would have? (Just clarify this detail in the paper) 5. Is Sec. 3.2 also doing fine-tuning? I was quite confused by the statement “We also consider training Unlimiformer directly”. Perhaps a different way to separate out section 3.1 and 3.2 is to say that in 3.1. you are applying kNN look up during model evaluation (while evaluating the models after each period of training), Vs 3.2 where you are applying kNN strategy on the training data examples (where the choice of the kNN values directly affects the model gradients during tuning). 6. During retrieval training, for the training examples that are longer than the context length, do you just have 1 copy of the (input, output) pair example in training – unlike the train chunked case where you could have multiple copies? Is this true even for training examples that are longer than 16k tokens? 7. In Table 4 when you say “Unlimiformer (this work)” do you mean the “Retrieval training” regime? 8. In Table 5 for BookSum, why isn’t there a line reporting results for BART-base Unlimiformer (Retrieval Training)? 9. Table 4 doesn’t directly include PRIMERA base training (standard finetuning) results, but worth repeating that result line from Table 3, so that this sentence (L206-207) is easier to follow: “in Table 4 highlight two important points: first, Unlimiformer+BARTbase performs better than the base PRIMERA across all metrics and datasets” 10. In the Comparison to SLED Ivgi et. al. The statements in L274-276 *“…This in practice limits SLED to only about 16k token-long inputs on a single GPU; in contrast, instead of attending to all input tokens, Unlimiformer attends only to the top-k input tokens for every attention head, and thus can process unlimited inputs in practice”* Contradicts your statements in Sec. 3.2 Retrieval Training L149-150 *“When inputs are longer than 16k tokens, we truncated the input to 16k tokens at training time due to GPU memory requirements.”* So, it seems like atleast in practice, in the experiments reported in this paper the input length was restricted to max 16k tokens due to practical limitations. Is that correct to say? 11. [Comment] In Table 2 (Sec 4.1. Datasets), for the distribution plots it might be worth adding a vertical line to indicate where the average length (avg. number of input tokens) falls in the distribution curve.

Rating

7: Accept: Technically solid paper, with high impact on at least one sub-area, or moderate-to-high impact on more than one areas, with good-to-excellent evaluation, resources, reproducibility, and no unaddressed ethical considerations.

Confidence

3: You are fairly confident in your assessment. It is possible that you did not understand some parts of the submission or that you are unfamiliar with some pieces of related work. Math/other details were not carefully checked.

Soundness

3 good

Presentation

2 fair

Contribution

3 good

Limitations

Perhaps worth noting that this is not applied to decoder-only models. Also worth noting the 16k limitation to fit into GPU during training. --- **Post rebuttal notes** The authors rebuttal and subsequent discussion addressed all my questions and concerns.

Reviewer ym6S7/10 · confidence 4/52023-07-27

Summary

This paper proposes to use kNN based search to replace the notoriously memory consuming quadratic attention in modern Transformers to allow extremely long sequence input. Proposed method is simple, and can be applied to any pre-trained Transformer. The proposed model, Unlimiformer, is evaluated on long text summarization tasks, and achieves significant improvement over baselines due to the increased context length. Note that the proposed method are shown to be effective without any finetuning on some tasks.

Strengths

S1. Sequence length is a major pain point of modern Transformer. This work targets a significant issue and achieves promising results. S2. Proposed method can be applied to multiple pre-trained models (BART, PRIMERA)

Weaknesses

W1. Only evaluated on text summarization. W2. Memory / Speed trade-offs are not quantitatively studied. Evaluation on one single task is the major limitation of this work. The usage of Transformer is broad. For example, long document QA tasks should also be studied. Long Range Arena is also a useful benchmark for efficient Transformers. Beyond NLP, I am also curious if this can be applied to computer vision transformers such as ViT, to enable higher-resolution input images. In addition, memory / speed trade-offs could benefit the readers for clearer guidelines when applying the proposed method. I strongly suggest adding this study. I am also curious how this method scale to LLMs larger than BART. Currently, I think the strengths outweigh the weaknesses so I'm leaning toward acceptance. -------------- update after rebuttal =============== Both W1 and W2 are properly addressed by the authors. Additional results on SCROLLS shows that Unlimiformer can generalize to other NLP tasks beyond summarization and an additional pre-trained model T5 is also studied. The additional memory / speed study also demonstrate the advantage to the proposed method. Since this method can be applied to many tasks and many pre-trained transformer LMs, it has potential to achieve high impact in the community. I decided to increase my rating from 5 to 7. My confidence score is also raised from 3 to 4. I think this paper is a clear accept. The reason I'm still a little conservative is that I don't have first hand experience in working on these specific benchmarks.

Questions

Suggestions: - I would tone down the claim of 'unlimited length', because CPU memory is, although typically larger than GPU memory, still practically limited. Instead, I suggest the authors to stress test the limit for a specific hardware (CPU&GPU) with synthetic long sequences so users have a practical reference. - Consider citing more related works such as Cluster-Former [1], Set Transformer [2], and Fast Transformer [3]. -------------- update after rebuttal =============== I don't have further questions and suggestions for the submission. Thanks for the response! [1] Wang et al., "Cluster-Former: Clustering-based Sparse Transformer for Question Answering", Findings of ACL 2021 [2] Lee et al., "A framework for attention-based permutation-invariant neural networks", ICML 2019 [3] Vyas et al., "Fast transformers with clustered attention", NeurIPS 2020

Rating

7: Accept: Technically solid paper, with high impact on at least one sub-area, or moderate-to-high impact on more than one areas, with good-to-excellent evaluation, resources, reproducibility, and no unaddressed ethical considerations.

Confidence

4: You are confident in your assessment, but not absolutely certain. It is unlikely, but not impossible, that you did not understand some parts of the submission or that you are unfamiliar with some pieces of related work.

Soundness

3 good

Presentation

3 good

Contribution

3 good

Limitations

Limitations are properly addressed.

Reviewer EQX87/10 · confidence 4/52023-08-03

Summary

Unlimiformer: Transformers with Unlimited Input Length:- proposes a novel method to overcome the context window limitation in encoder-decoder Transformer models. The key innovation introduced in this paper is a retrieval-based method that integrates a k-Nearest Neighbors (kNN) search into each decoder layer of the model. This approach allows each attention head to choose different context windows from the full-length input at each decoding step. The authors propose an encoding process for long input sequences that uses the model's encoder on overlapping chunks of the input. To ensure sufficient context, only the middle half of the encoded vectors from each chunk are kept. The authors diverge from the standard Transformer cross-attention mechanism by retrieving the top-k hidden states from the entire input sequence. The proposed method involves a mathematical reformulation of the attention mechanism to incorporate the kNN search into the cross-attention process more efficiently.

Strengths

This approach can be integrated into any existing pretrained encoder-decoder transformer model to enable summary over unbounded inputs (subject to computational constraints). This approach does not require retraining of the model, although further finetuning appears to improve performance. The paper is well written and clear.

Weaknesses

The computational cost of encoding the entire input could be very high at inference time. Many of the chosen benchmark approaches appear to be pretty arbitrary and not very convincing. It is unclear to what extent the overlapping approach to encoding inputs aids the approach. Text is order dependent and filler words serve a purpose, which in many cases affects the semantic meaning of a particular word or sentence. A kNN approach over the most relevant words across the entire inputs could just find correlated tokens that lack any local context in how they were used within a particular phrase or sentence..

Questions

How is k chosen? is it always set to the base models context window length? What happens as k is varied? It would be good to have a better idea of the computational performance of this kNN-based approach and how it scale. Especially in terms of compute, latency and memory. It seems that it would be prohibitively expensive to query the datastore at each timestep? What is the motivation behind the training methods shown in section 3.2.? How does it weakly simulate nearest neighbours? Whenever there is mention of surprising results with regard to the model being able to perform well with limited context, or the full input being unnecessary to produce a summary. How confident are you that the base model has not been trained on WikiSum or any of the datasets (and its derivatives) that it is being evaluated on? In figure 3, what causes the dip at 64k and 100k datastore? By CPU datastore, do you just mean system RAM? if so, please mention that. Could you provide examplers of what actually gets retrieved by kNN? As in, the parts of the input that are being used by the model for particular queries?

Rating

7: Accept: Technically solid paper, with high impact on at least one sub-area, or moderate-to-high impact on more than one areas, with good-to-excellent evaluation, resources, reproducibility, and no unaddressed ethical considerations.

Confidence

4: You are confident in your assessment, but not absolutely certain. It is unlikely, but not impossible, that you did not understand some parts of the submission or that you are unfamiliar with some pieces of related work.

Soundness

3 good

Presentation

3 good

Contribution

3 good

Limitations

No broader impact statement is provided, but its conclusion would strengthen the paper

Reviewer VEz62023-08-11

Thank you for the extensive rebuttal and additional experiments. I will be raising my score to a 6.

Authorsrebuttal2023-08-11

Thank you for responding and for increasing your score! Please let us know if there are additional questions before the end of the discussion period.

Reviewer cHBL2023-08-11

Any evals on tasks other than summarization?

Thanks for your responses. it's good to also know about the extension to decoder-only models. Have you evaluated on tasks other than summarization? Even if the inputs were not really long? I agree with the concern raised by Reviewer ym6S, regarding eval on summarization tasks alone. Despite your response there, it does seem like a limitation. It would be interesting to know if you have done any evals on one or 2 other tasks, perhaps the others from the SCROLLS benchmark? This might be particularly relevant for the models that have been trained with the modified retrieval layer (retrieval/enc-dec/alternating) training. Even if it is on tasks where the input length is not large, it would be of interest to validate whether the model performance regresses on those tasks (and if it does to what extent, if it doesn't that's also useful to know).

Authorsrebuttal2023-08-14

Thank you for your response! We have queued experiments on more tasks from the SCROLLS benchmark, and we will report these results in the next few days.

Authorsrebuttal2023-08-18

Additional SCROLLS results

>It would be interesting to know if you have done any evals on one or 2 other tasks, perhaps the others from the SCROLLS benchmark? ... it would be of interest to validate whether the model performance regresses on those tasks Thanks to your suggestion, we ran experiments on 4 additional QA and NLI tasks from SCROLLS. Please see the global response!

Reviewer cHBL2023-08-20

Nice work!

Thanks for providing the additional information, clarifications, and the additional experimental results. Nice work!

Reviewer ym6S2023-08-14

Reviewer's response

Thanks for responding to my questions and concerns. I hope my comments / suggestions are helpful for your future revisions. For the speed-memory tradeoff (W2), perhaps I wasn't clear in my original review. I was hoping to see some memory - speed plot when replacing 0, 1, 2, ... to all layers in the baseline Transformer with an UnlimiFormer layer under a fixed sequence length. Fig. 4 is good, but I'm just suggesting more experiments for a more complete study. I understand that the authors focus on summarization and this is well motivated. However, this remains the main reason (W1) that I won't raise my final rating. A lot of efficient transformers have been proposed, and the most impactful ones are those experimented on multiple NLP tasks. For a task-specific method like this submission, perhaps the authors should refrain from using such a generalist paper title. Perhaps "Long-Range Transformer for Unlimited-Length Summarization" better fits the current scope.

Authorsrebuttal2023-08-14

Thank you for your response! **For W2:** Apologies for the misunderstanding! We have now run the analysis you proposed on BART-base+Unlimiformer (retrieval). | Number of layers with Unlimiformer | Max GPU memory allocated | Total time taken | Entity Mention (EntMent) | | ---------------------------------- | ------------------------ | ---------------- | ------- | | 0 (normal inference) | 24.8% (11.9 GB) | 6m27s | 9.8 | | 1 | 85.5% (41.0 GB) | 16m37s | 15.3 | | 2 | 89.6% (43.0 GB) | 19m32s | 15.2 | | 3 | 85.9% (41.2 GB) | 19m35s | 17.7 | | 4 | 86.1% (41.3 GB) | 21m25s | 17.3 | | 5 | 84.1% (40.4 GB) | 22m53s | 18.8 | | 6 (all layers) | 85.2% (40.9 GB) | 26m14s | 20.3 | Here, we are measuring maximum GPU memory allocated, total time, and Entity Mention over the full BookSum test set using a single A6000 GPU. The longest example in the test set is approximately 505k tokens. The total time taken does increase when we apply Unlimiformer to more layers, but the memory allocated does not, because we use our attention reformulation to reuse the same datastore across layers. We see improved Entity Mention (EntMent) when we apply Unlimiformer on more layers, and applying Unlimiformer at all layers requires only ~3.4x more memory and ~4x more time than running the base model while processing inputs that are 100-500x longer. (The slight differences in max memory allocated are because the profiler we used is taking snapshots at slightly different times, not because there is significant variation in memory usage between 1-6 Unlimiformer layers. We will average over repeated runs for stability when we report these results in the paper). **For W1:** We have queued experiments on more tasks from the SCROLLS benchmark, and we will report these results in the next few days.

Reviewer ym6S2023-08-16

Reviewer's response to additional time/memory table

Thanks for the response. I also look forward to additional results on SCROLLS. The speed/memory table is interesting. Could you further clarify where did the additional memory come from (from baseline to 1 layer)? Is the baseline's input length truncated? I thought the proposed kNN approach would have some time / memory saving but they both decayed from the table.

Authorsrebuttal2023-08-18

Corrected memory consumption and additional SCROLLS results

We realized that we made a mistake in our previous memory profiling. We re-ran our code, and additionally changed to `fp16`. Here are the updated memory consumption results: | Number of layers using Unlimiformer | Memory consumption (GB) | | --------------------------------- | -------------------- | | 0 (normal inference) | 1.61 | | 1 | 7.33 | | 2 | 7.32 | | 3 | 7.36 | | 4 | 7.32 | | 5 | 7.33 | | 6 (all) | 7.35 | >The speed/memory table is interesting. Could you further clarify where did the additional memory come from (from baseline to 1 layer)? Is the baseline's input length truncated? As you mentioned, there is a slight increase between the base model and using Unlimiformer in 1 layer. There are two reasons for this: * there is a slight overhead for constructing the index and querying it * more crucially, the base model here ("normal inference") is truncating the input to the first 1024 tokens. When Unlimiformer is used, we process the full inputs, some of which are >500,000 tokens, and so much of the additional memory cost comes from storing the additional ~499k hidden states. However, the highlight of this table to us, is to see how GPU memory consumption remains **constant** even when we use Unlimiformer in more layers. This highlights the scalability of Unlimiformer compared to other approaches such as Memorizing Transformers, which need to allocate more memory with every layer and every attention head. Because this previous work required 2 datastores per head and BART has 12 attention heads, it would require **~60GB** to augment a single layer with retrieval, and **hundreds** of GB to augment all layers with retrieval. Because of this, previous methods were unable to perform retrieval at more than one layer. >I also look forward to additional results on SCROLLS. Thanks to your suggestion, we ran experiments on 4 additional QA and NLI tasks from SCROLLS. Please see the global response!

Reviewer ym6S2023-08-18

Thanks for the detailed response and additional results! Since all my concerns are properly addressed by the authors, I'll raise my final score from 5 to 7. I strongly encourage the authors to further refine the additional experimental results conducted during the rebuttal period, and then discuss them in details in the next paper revision.

Authorsrebuttal2023-08-18

Thank you for engaging in discussion and for raising your score! We will definitely incorporate the experiments on the additional tasks and more discussion of memory requirements in the next version.

Reviewer 4zy32023-08-15

Response to Author Rebuttal

I thanks the authors for clarifying my questions and for the new results/analysis in the attached pdf. As a result I have moved up my initial score by two points. One request/recommendation for the paper will be to clearly describe their chunking algorithm in the main paper and contrast it with related work

Authorsrebuttal2023-08-15

Thank you for the response and for raising your score! We will add additional clarification of the chunking. Thank you for your review, we feel it has helped us improve the paper.

Authorsrebuttal2023-08-18

Additional results: QA and NLI

We would like to thank the reviewers for all their excellent suggestions and for engaging in a discussion! We really feel that they have helped us improve the paper. Following Reviewer `cHBL` and Reviewer `ym6S`'s suggestions, we trained Unlimiformer and the base model on additional tasks - long-input QA and NLI tasks from SCROLLS. | Base model | Training | QASPER (F1) | Contract NLI (Exact Match) | QMSum (ROUGE 1 / 2 / L / BERTScore F1) | Narrative QA | |------------|-----------------------------------------|-------------|----------------------------|----------------|--------------| | BART | Standard finetuning (first 1024 tokens) | 22.0 | 77.5 | **30.8 / 8.7 / 20.8 / 56.8** | 15.5 | | BART | Unlimiformer (Retrieval Training) | **27.3** ($\uparrow$5.3) | **78.8** ($\uparrow$1.3) | 29.6 / 7.7 / 19.4 / 55.7 | **18.7** ($\uparrow$3.2) | | BART | Unlimiformer (Alternating Training) | 27.2 ($\uparrow$5.2) | 77.7 ($\uparrow$0.2) | 29.3 / 7.8 / 19.7 / 55.9 | 18.5 ($\uparrow$3.0) | To keep Unlimiformer generic, we did not perform any architectural modifications, and we treated these as sequence-to-sequence tasks: we crafted the inputs by concatenating `question + document` (or `hypothesis + document` in Contract NLI), to represent all tasks as text-to-text. Further, we did not perform *any hyperparameter tuning*: we took hyperparameter values such as learning rate from the SCROLLS benchmark to train the base model, and then used the same hyperparameters to train Unlimiformer. As shown, both Unlimiformer training approaches significantly improve over the base model in 3 of the 4 tasks, without any task-specific modeling or hyperparameter tuning. We believe that tuning or task-specific tricks could further increase Unlimiformer’s gain, but these results show that even the most straightforward application of Unlimiformer provides benefits across different tasks.

Program Chairsdecision2023-09-21

Decision

Accept (poster)

© 2026 NYSGPT2525 LLC