Teleport beyond context limits with transformers
Project description
ContextWormhole
Context length extension library for transformers
ContextWormhole provides practical implementations of three established context extension techniques. When your transformer model reaches its context limit, this library offers clean, tested strategies to handle longer inputs.
pip install contextwormhole
Purpose
Most transformer models have fixed context windows (e.g., 1024 tokens for GPT-2). This library implements three strategies to work with longer texts while maintaining the model's original architecture.
Strategies
1. Sliding Window
Processes text in overlapping chunks, maintaining continuity between segments.
@sliding_window(window_size=512, overlap=64)
def process_long_document(model, text, **kwargs):
return model.generate(text, **kwargs)
- Implementation: Overlapping windows with position ID recycling
- Time complexity: O(n)
- Memory complexity: O(window_size)
- Use cases: Documents, code files, articles
2. Hierarchical Context
Creates summaries of text chunks, then combines summaries with final content.
@hierarchical_context(chunk_size=256, summary_length=64)
def analyze_paper(model, paper, **kwargs):
return model.generate(paper, **kwargs)
- Implementation: Chunk → summarize → combine → process
- Time complexity: O(n log n)
- Memory complexity: O(n/chunk_size * summary_length)
- Use cases: Research papers, structured documents
3. Attention Sink
Preserves initial tokens plus recent context, discarding middle content.
@attention_sink(sink_tokens=16)
def continue_conversation(model, chat_history, **kwargs):
return model.generate(chat_history, **kwargs)
- Implementation: Initial tokens + recent context
- Time complexity: O(1)
- Memory complexity: O(max_length)
- Use cases: Conversations, chat histories
Empirical Results
Tests on repetition patterns (10 runs each, distilgpt2):
| Strategy | Uniqueness Ratio | Repeated Phrases | Notes |
|---|---|---|---|
| Standard (low temp) | 0.59 | 3.5 | Baseline |
| Standard (high temp) | 0.28 | 2.0 | High repetition |
| Attention Sink | 0.67 | 1.8 | Best coherence |
The attention sink strategy showed consistently better text quality with fewer repetitive patterns.
Usage
Basic Example
from contextwormhole import ContextWormholeModel
model = ContextWormholeModel("gpt2")
# Different strategies for different needs
result1 = model.sliding_window_generate(long_document, max_new_tokens=100)
result2 = model.hierarchical_generate(research_paper, max_new_tokens=100)
result3 = model.attention_sink_generate(conversation_history, max_new_tokens=100)
Configuration
from contextwormhole import ExtendedContextConfig
config = ExtendedContextConfig(
window_size=256,
overlap=64,
chunk_size=256,
summary_length=64,
sink_tokens=16,
use_cache=True,
)
model = ContextWormholeModel("gpt2", **config.__dict__)
CLI Interface
# Sliding window
contextwormhole --model gpt2 --input document.txt --strategy sliding_window
# Hierarchical
contextwormhole --model gpt2 --input paper.txt --strategy hierarchical
# Attention sink
contextwormhole --model gpt2 --input chat.txt --strategy attention_sink
Performance Characteristics
| Strategy | Max Context | Memory (MB)* | Time (s)* | Best For |
|---|---|---|---|---|
| Sliding Window | ~10K tokens | 600 | 1.5-2.0 | Documents, code |
| Hierarchical | ~20K tokens | 400 | 1.0-1.5 | Papers, reports |
| Attention Sink | ~8K tokens | 300 | 0.8-1.2 | Conversations |
*Approximate values for GPT-2 on CPU
Implementation Notes
- Each strategy respects the model's native context limit for individual forward passes
- Position ID recycling enables handling of arbitrarily long inputs
- KV caching improves generation speed and maintains coherence
- All strategies include proper error handling and configuration validation
Requirements
- Python ≥ 3.8
- PyTorch ≥ 1.9.0
- Transformers ≥ 4.20.0
- NumPy ≥ 1.20.0
Technical Background
This library implements well-established context extension techniques:
- Sliding Window: Classical attention windowing
- Hierarchical Context: Recursive summarization approach
- Attention Sink: Based on StreamingLLM research
The focus is on providing clean, tested implementations with practical optimizations rather than novel algorithms.
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file contextwormhole-1.0.0.tar.gz.
File metadata
- Download URL: contextwormhole-1.0.0.tar.gz
- Upload date:
- Size: 27.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec4075e9895ff76892ca86b3adf191c111b09a555cb6cdeac315b6531ef88de5
|
|
| MD5 |
09a2ff948cd921af2786eeeed09e8186
|
|
| BLAKE2b-256 |
6eec3eb21d1fa0a75c803e99d0ab2d1c5ae12cd84eb15bdff548e45e91aa84bb
|
File details
Details for the file contextwormhole-1.0.0-py3-none-any.whl.
File metadata
- Download URL: contextwormhole-1.0.0-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37a2c2a37f74285f0d559d24fff9e955f8a75eb3ea5d1ec0df173f575d9fa3a5
|
|
| MD5 |
9af0635eaa818a7f39e879a1da8cf103
|
|
| BLAKE2b-256 |
c2eb7f49ce59780060df70d76de06dfe7879bfbfd98a26d2de8834cd45392a54
|