While transformers have shown remarkable success in natural language processing, their attention mechanism's large memory requirements have limited their ability to handle longer contexts. Prior approaches, such as recurrent memory or retrieval-based augmentation, have either compromised the random-access flexibility of attention (i.e., the capability to select any token in the entire context) or relied on separate mechanisms for relevant context retrieval, which may not be compatible with the model's attention. In this paper, we present a novel approach that allows access to the complete context while retaining random-access flexibility, closely resembling running attention on the entire context. Our method uses a landmark token to represent each block of the input and trains the attention to use it for selecting relevant blocks, enabling retrieval of blocks directly through the attention mechanism instead of by relying on a separate mechanism. Our approach seamlessly integrates with specialized data structures and the system's memory hierarchy, enabling processing of arbitrarily long context lengths. We demonstrate that our method can obtain comparable performance with Transformer-XL while significantly reducing the number of retrieved tokens in each step. Finally, we show that fine-tuning LLaMA 7B with our method successfully extends its context length capacity up to 32k tokens, allowing for inference at the context lengths of GPT-4.
翻译:尽管Transformer在自然语言处理中取得了显著成功,但其注意力机制对内存的高需求限制了其处理更长上下文的能力。先前的方法(如循环记忆或基于检索的增强)要么牺牲了注意力的随机访问灵活性(即从整个上下文中选择任意标记的能力),要么依赖独立机制进行相关上下文检索,这可能与模型的注意力机制不兼容。本文提出了一种新颖方法,既能保留随机访问灵活性,又能访问完整上下文,其效果类似于对整个上下文运行注意力机制。本方法使用地标标记(landmark token)代表输入的每个块,并训练注意力机制利用它选择相关块,从而直接通过注意力机制而非独立机制检索块。该方法与专门的数据结构及系统内存层次结构无缝集成,支持处理任意长度的上下文。我们证明,该方法在显著减少每一步检索标记数量的同时,可与Transformer-XL获得相当的性能。最后,我们展示使用本方法微调LLaMA 7B,可成功将其上下文长度容量扩展至32k标记,从而实现与GPT-4上下文长度相当的推理能力。