A minimal, modular implementation of the Hyena Hierarchy architecture for language modeling
This Hyena model is designed for efficient language modeling on resource-constrained environments. It's perfect for:
- Research & Experimentation: Study long-sequence modeling without massive compute
- Edge Deployment: Run language models on devices with limited GPU memory
- Prototyping: Quickly iterate on language model architectures
- Educational Projects: Learn about modern transformer alternatives
- Continual Learning: Experiment with EWC for sequential task learning
- API Development: Build custom language generation services
- 🚀 Hyena Hierarchy Architecture: Efficient long-sequence modeling with depthwise convolutions
- 🎨 Optional Attention: Hybrid mode with multi-head self-attention for local refinement
- 🧠 Elastic Weight Consolidation (EWC): Continual learning support
- 💾 Memory Efficient: Optimized for smaller GPU setups (free tier compatible)
- 🧩 Modular Design: Clean separation of concerns
- 🔤 Simple Tokenizer: Byte-level semantic tokenizer
- 🌐 OpenAI-Compatible API: Flask server with standard endpoints
pip install -r requirements.txtpython train.py \
--data training_data.txt \
--vocab_size 8000 \
--d_model 256 \
--n_layers 6 \
--batch_size 4 \
--epochs 10 \
--output model.pth \
--tokenizer_output tokenizer.jsonpython inference.py \
--model model.pth \
--tokenizer tokenizer.json \
--prompt "Once upon a time" \
--max_tokens 100from api import create_app
app = create_app('model.pth', 'tokenizer.json')
app.run(host='0.0.0.0', port=5000)from models import ModelConfig
config = ModelConfig(
vocab_size=8000,
d_model=256,
n_layers=6,
dim_feedforward=1024,
dropout=0.1,
max_seq_len=1024,
desired_receptive_field=2048,
use_attention=False,
n_heads=4
)- Gradient Accumulation: Effective larger batch sizes without memory overhead
- Mixed Precision Training (AMP): Faster training with reduced memory usage
- Gradient Clipping: Stable training with controlled gradients
- Configurable Sequence Length: Adjust based on available memory
- Depthwise Convolutions: Efficient parameter usage
# Run model tests
python tests/test_model.py
# Run tokenizer tests
python tests/test_tokenizer.pycurl -X POST http://localhost:5000/v1/completions \
-H "Content-Type: application/json" \
-d '{"prompt": "Hello world", "max_tokens": 50}'curl -X POST http://localhost:5000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "Hello"}]}'hyena_model/
├── models/ # Model architecture
├── tokenizers/ # Tokenizer implementation
├── training/ # Training logic
├── inference/ # Generation logic
├── api/ # Flask API server
├── utils/ # Utilities
└── tests/ # Test suite
| VRAM | Configuration |
|---|---|
| 4GB (Minimum) | batch_size=1, d_model=128, n_layers=4, seq_len=512 |
| 8GB (Recommended) | batch_size=4, d_model=256, n_layers=6, seq_len=1024 |
| 16GB+ | batch_size=8, d_model=512, n_layers=12, seq_len=2048 |
For smaller GPUs, reduce:
batch_sizeto 1 or 2d_modelto 128 or 192n_layersto 4seq_lento 512
MIT