RAT: Reinforced Adaptive Transformer
RAT (Reinforced Adaptive Transformer) is a next-generation transformer architecture featuring adaptive attention mechanisms powered by reinforcement learning. It combines cutting-edge techniques like Rotary Position Embeddings, SwiGLU feed-forward networks, and temporal convolutions for superior language modeling performance.
✨ Key Features
- 🧠 Adaptive Policy Attention: Dynamic head gating using multiple RL-based policy networks
- 🔄 Rotary Position Embeddings: Enhanced positional understanding with RoPE
- 🚀 SwiGLU Feed-Forward: Efficient activation for better expressiveness
- ⏰ Temporal Convolutions: Sequence modeling with depthwise convolutions
- 📊 Advanced Logging: Comprehensive training monitoring and debugging
- 🛡️ Error Handling: Robust validation and graceful failure recovery
- 💾 Auto-Checkpointing: Automatic model saving with training state
- 🎯 Optimized Generation: Multiple sampling strategies with KV caching
🏗️ Architecture Components
Core Components
RAT: Main transformer model with adaptive attentionAdaptivePolicyAttention: Multi-policy attention with reinforcement learningRATBlock: Transformer block with attention, FFN, and temporal convSwiGLUFeedForward: Efficient feed-forward networkRotaryPositionEmbedding: Rotary positional encodings
Training & Inference
RATTrainer: Advanced trainer with logging and checkpointingRATGenerator: Optimized text generation with multiple strategiesRATDataset: Enhanced dataset with preprocessing and validation
Utilities
RATLogger: Comprehensive logging systemModelCheckpoint: Automatic checkpoint management- Configuration validation: Input sanitization and error checking
🚀 Quick Start
Installation
From PyPI (Recommended)
pip install rat-transformer
From Source
# Clone the repository
git clone https://github.com/ReinforcedAdaptiveTransformer-RAT/RAT.git
cd RAT
# Install in development mode
pip install -e .
# Or install with optional dependencies
pip install -e ".[dev,training,serving]"
Basic Usage
from rat import RAT, RATTrainer, RATGenerator
from transformers import AutoTokenizer
# Initialize model
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = RAT(vocab_size=tokenizer.vocab_size)
# Training
trainer = RATTrainer(model, tokenizer)
# ... training code ...
# Generation
generator = RATGenerator(model, tokenizer)
text = generator.generate("Hello, how are you?", max_len=50)
print(text)
Command Line Interface
# Train a model
rat-train --config config.json --output-dir ./checkpoints
# Generate text
rat-generate --model-path checkpoints/model.pt --prompt "Hello world"
# Run tests
rat-test --quick
# Evaluate model
rat-eval --model-path model.pt --dataset wikitext
Advanced Configuration
# Custom model configuration
model = RAT(
vocab_size=50000,
d_model=1024,
n_layers=24,
n_heads=16,
n_policies=5,
dropout=0.1,
use_rope=True,
use_checkpointing=True
)
# Advanced training
trainer = RATTrainer(
model=model,
tokenizer=tokenizer,
lr=1e-4,
max_steps=100000,
grad_clip=1.0,
checkpoint_dir="./checkpoints"
)
📊 Performance & Benchmarks
- Parameter Efficiency: Better performance with fewer parameters
- Training Stability: Advanced optimization and regularization
- Generation Quality: Superior text coherence and diversity
- Memory Optimization: Gradient checkpointing and KV caching
🔧 Configuration
Model Parameters
vocab_size: Size of token vocabularyd_model: Model dimension (must be divisible by n_heads)n_layers: Number of transformer layersn_heads: Number of attention headsn_policies: Number of RL policies for attention gatingmax_seq_len: Maximum sequence lengthdropout: Dropout probability
Training Parameters
lr: Learning ratewarmup_steps: Learning rate warmup stepsweight_decay: Weight decay for regularizationgrad_clip: Gradient clipping thresholdaccum_steps: Gradient accumulation steps
🧪 Testing & Validation
Run the comprehensive test suite:
python test_rat.py
The test suite validates:
- ✅ Component functionality
- ✅ Training pipeline
- ✅ Text generation
- ✅ Memory usage
- ✅ Gradient flow
- ✅ Error handling
📈 Training Tips
- Batch Size: Start with smaller batches and increase gradually
- Learning Rate: Use 1e-4 for large models, 5e-4 for smaller ones
- Gradient Accumulation: Use for effective larger batch sizes
- Checkpointing: Enable automatic saving every 1000 steps
- Monitoring: Watch perplexity and loss curves
🤝 Contributing
We welcome contributions! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Inspired by modern transformer architectures
- Built on PyTorch and Hugging Face Transformers
- Thanks to the research community for advancing transformer models
RAT: Reinforced Adaptive Transformer - Revolutionizing language models with reinforcement learning
Release files for rat-transformer 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| rat_transformer-0.1.2.tar.gz | 21.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| rat_transformer-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 42.1 kB
Release files / rat_transformer-0.1.2.tar.gz
| Download URL | rat_transformer-0.1.2.tar.gz |
|---|---|
| Size | 21.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7d36b9f940d4b750969e55331c563e5a7e1bf9a2a53b709ffa1f90eecffd2379
|
|
BLAKE2b-256 checksum How to use checksums |
0a9458438eaaa980a6d2d21cb6743164b21d87348b8b6645a4817bcb7874b3c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.6
|
Release files / rat_transformer-0.1.2-py3-none-any.whl
| Download URL | rat_transformer-0.1.2-py3-none-any.whl |
|---|---|
| Size | 20.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8baf1f0a03ca608389aac6522605b8294953196cab21d35e44c22b5b6332e955
|
|
BLAKE2b-256 checksum How to use checksums |
d90bc4ac609c1f15c051a35c3d6a61ef100124359303e68bf747dc34539050e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.6
|