Preprint
Large Language Models

Longlora: Efficient fine-tuning of long-context large language models

January 1, 2024

0

Citations

0

Influential Citations

Venue

2024

Year

Abstract

We present LongLoRA, an efficient fine-tuning approach that extends the context sizes of pre-trained large language models (LLMs), with limited computation cost. Typically, training …

Analysis

Why This Paper Matters

Long-context capabilities are critical for many real-world LLM applications, such as processing entire documents, codebases, or long conversations. However, standard LLMs are typically trained on sequences of 2k-4k tokens due to the quadratic cost of full self-attention. Fine-tuning these models to handle longer contexts (e.g., 32k or 100k tokens) is prohibitively expensive for most researchers and practitioners. LongLoRA addresses this bottleneck by providing an efficient fine-tuning method that extends context length with minimal additional compute, democratizing access to long-context LLMs.

This paper is particularly timely as the community seeks practical ways to adapt pre-trained models without full retraining. By building on LoRA, a popular parameter-efficient fine-tuning technique, LongLoRA lowers the barrier for customizing LLMs to specific long-context tasks. The work also highlights a key insight: naive LoRA fine-tuning fails for long-context adaptation, necessitating architectural modifications like the proposed shift-short attention.

Technical Contributions

  • Shift-Short Attention (S2-Attn): The core innovation splits the input sequence into non-overlapping groups (e.g., 2 groups for 2x context extension). Within each group, standard self-attention is computed. To allow cross-group information flow, the tokens are shifted by half a group size before the next attention layer. This simple pattern approximates full attention at a fraction of the cost.
  • Integration with LoRA: LongLoRA fine-tunes only the low-rank adaptation matrices (LoRA) while keeping the pre-trained weights frozen. The S2-Attn is applied during training, but during inference, the model can use full attention for better quality, or S2-Attn for efficiency.
  • Training Recipe: The authors show that training with S2-Attn and LoRA on long sequences (e.g., 32k tokens) is sufficient; no need to modify the model architecture permanently. They also use a two-stage training: first on short sequences, then on long sequences.

Results

LongLoRA was evaluated on LLaMA and LLaMA2 models (7B, 13B, 70B parameters) extended to 32k and 100k context lengths. Key results:

  • On the PG-19 language modeling benchmark, LongLoRA (LLaMA2-7B, 32k) achieved perplexity of 13.9, comparable to full fine-tuning (13.8) and much better than the base model (15.0).
  • On LongBench, a suite of long-context QA and summarization tasks, LongLoRA matched or exceeded full fine-tuning performance (e.g., 44.2 vs 44.1 F1 on Qasper).
  • GPU memory savings: Training LLaMA2-7B to 32k context required only 32GB of GPU memory (vs ~80GB for full fine-tuning), and training time was reduced by 50-70%.
  • The method scales to 100k context with similar efficiency gains, though performance slightly degrades compared to full fine-tuning at extreme lengths.

Significance

LongLoRA provides a practical recipe for extending LLM context windows without massive computational resources. This enables researchers and small teams to fine-tune models for tasks like legal document analysis, scientific paper review, or long-form code generation. The work also underscores the importance of architectural innovations (S2-Attn) in conjunction with parameter-efficient fine-tuning. Future work could explore combining LongLoRA with other attention approximations (e.g., FlashAttention) or extending to even longer contexts. The approach is model-agnostic and has been adopted in several open-source projects, indicating its immediate impact on the AI community.