long-context

Extend transformer context windows using RoPE, YaRN, ALiBi, and position interpolation techniques.

2|Updated Aug 15, 2026
One-click install
npx skills add https://github.com/Jensen-Yao/agents-skills --skill long-context-jensen-yao
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: long-context
Source: https://github.com/Jensen-Yao/agents-skills/tree/main/skills/long-context
Command: npx skills add https://github.com/Jensen-Yao/agents-skills --skill long-context-jensen-yao

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires transformers, torch, flash-attn, and includes references (resource) components.

What problem does it solve? Pre-trained transformer models like LLaMA and Mistral are limited to their original context windows (typically 2k-8k tokens), making them unable to process long documents, books, or extended conversations without expensive retraining or degraded performance. ## Core Features & Use Cases - Positional Encoding Implementations: Provides complete PyTorch implementations of RoPE (Rotary Position Embeddings), ALiBi (Attention with Linear Biases), and their scaling variants. - Context Extension Methods: Covers YaRN, Position Interpolation, NTK-aware scaling, and dynamic scaling with HuggingFace Transformers configuration examples. - Fine-tuning Pipelines: Includes minimal fine-tuning workflows (400-1000 steps) for extending models to 32k-128k tokens, plus evaluation methods like perplexity testing and passkey retrieval. - Use Case: Extend a LLaMA-2-7B model from 2048 to 32768 tokens using YaRN scaling with only 400 fine-tuning steps on long documents, then deploy it with vLLM for long-document question answering. ## Quick Start Extend my LLaMA-2-7B model to handle 32k token documents using YaRN scaling and show me the fine-tuning configuration.

Frequently Asked Questions about long-context

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I extend LLaMA context length to 32k tokens?▼

Set rope_scaling in the model config with type linear or yarn and a factor of 16, then fine-tune for 400-1000 steps on long documents. YaRN achieves the best quality with only 400 steps and 100M training tokens.

What is the difference between YaRN and Position Interpolation?▼

YaRN uses NTK-aware interpolation that treats frequency bands differently and requires 2.5x fewer training steps than Position Interpolation. Position Interpolation uniformly compresses all positions into the trained range with a one-line code change but loses high-frequency information.

RoPE vs ALiBi for long context transformers?▼

ALiBi biases attention scores by distance without positional embeddings, offering 11% less memory and strong extrapolation, but requires training from scratch. RoPE is the standard in LLaMA-style models and can be extended post-training via YaRN or interpolation.

Does position interpolation work without fine-tuning?▼

No, applying rope_scaling without fine-tuning produces poor results because the model has not adapted to compressed positions. At minimum 1000 fine-tuning steps on long documents matching the target length are required.

Why does long context training run out of memory?▼

Attention memory grows quadratically with sequence length. Enable gradient checkpointing, use Flash Attention 2 via attn_implementation, reduce batch size to 1 with gradient accumulation, and train in bfloat16.

What are the limitations of context window extension?▼

Aggressive scaling factors above 8x degrade quality without substantial fine-tuning data, and Position Interpolation cannot extrapolate beyond the fine-tuned length. Incremental scaling from 8k to 16k to 32k produces more stable results than direct jumps.