long-context

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

1|Updated Mar 5, 2026
One-click install
npx skills add https://github.com/Clay-HHK/claude-skills --skill long-context-clay-hhk
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: long-context
Source: https://github.com/Clay-HHK/claude-skills/tree/main/long-context
Command: npx skills add https://github.com/Clay-HHK/claude-skills --skill long-context-clay-hhk

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, codebases, or conversations without expensive retraining from scratch. ## Core Features & Use Cases - Positional Encoding Implementations: Provides complete PyTorch implementations of RoPE (Rotary Position Embeddings), ALiBi (Attention with Linear Biases), and YaRN scaling for extending context to 32k-128k+ tokens. - Fine-tuning Pipelines: Includes minimal fine-tuning configurations (400-1000 steps) for position interpolation and YaRN-based context extension with HuggingFace Transformers. - Evaluation & Deployment: Covers perplexity evaluation, passkey retrieval testing, memory optimization with Flash Attention 2, and vLLM deployment for long-context inference. - Use Case: Extend a LLaMA-2-7B model from 2k to 32k tokens by configuring YaRN rope_scaling, fine-tuning for 400 steps on long documents like PG-19, and deploying with vLLM for long-document question answering. ## Quick Start Extend my LLaMA-2-7B model to handle 32k token contexts 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?▼

Configure rope_scaling in the model config with type 'yarn' or 'linear' and a scaling factor of 16, then fine-tune for 400-1000 steps on long documents. YaRN requires only 400 steps and achieves better perplexity than linear position interpolation.

What is the difference between YaRN and position interpolation?▼

YaRN uses NTK-aware interpolation that treats high and low frequencies differently plus attention temperature scaling, while position interpolation uniformly compresses all positions. YaRN needs 10x less training data and achieves better extrapolation quality.

RoPE vs ALiBi: which positional encoding should I use?▼

Use ALiBi when training models from scratch since it offers unlimited context, 11% less memory, and strong extrapolation. Use RoPE-based methods like YaRN when extending existing pre-trained models, since ALiBi cannot be retrofitted.

Does position interpolation work without fine-tuning?▼

No, applying rope_scaling without fine-tuning produces poor results. Position interpolation requires approximately 1000 fine-tuning steps on long documents matching the target context length to work correctly.

Why does long-context training run out of memory?▼

Long sequences dramatically increase activation memory in attention layers. Enable gradient checkpointing, use bfloat16 precision, set batch size to 1 with gradient accumulation, and use Flash Attention 2 for 2-3x speedup.

What datasets work best for context extension fine-tuning?▼

Use long-form datasets like PG-19 books, arXiv papers, or concatenated GitHub code with sequences matching the target length. Short documents will not teach the model long-range dependencies.