A Llama-style decoder architecture with explicit latent plans and conditional VAE training
Project description
Free Transformer
Free Transformer: A Llama-style decoder architecture with explicit latent plans, conditional VAE training, and benchmark comparisons against standard Transformers.
Designed for efficient PyTorch training on modern GPUs with full FSDP support and modern optimizations.
๐ Complete Documentation | ๐ Quick Start Guide | ๐๏ธ Architecture Details
What Is the Free Transformer?
Traditional autoregressive Transformers generate each token by conditioning only on the sequence so far ("reactive" behavior).
Free Transformer introduces a latent planning mechanismโfirst choosing a stochastic abstract plan (Z), then generating tokens to fit that plan.
This scalable conditional VAE architecture maintains high-level coherence, improves controllable generation, and enables richer sequence modeling.
Architecture Overview
Features
๐๏ธ Architecture
- Llama-style backbone: RMSNorm, SwiGLU, RoPE, Grouped-Query Attention (GQA)
- Latent Planning: Explicit plan variable
Zwith differentiable binary coding - Conditional VAE: Reconstruction + KL loss with free bits regularization
โก Performance & Scaling
- FSDP Support: Multi-GPU training with PyTorch Fully Sharded Data Parallel
- Mixed Precision: Automatic Mixed Precision (AMP) with gradient scaling
- Memory Efficient: Gradient checkpointing and optimized attention patterns
- Modern Optimizations: bfloat16, efficient parameter sharding
๐ง Development & Training
- Flexible Training: Switchable inference/training flows with mode selection
- Synthetic + Real Data: Fast prototyping with built-in synthetic data generation
- Comprehensive Testing: Unit/integration tests, benchmark comparisons
- Quality Assurance: Type checking, linting, formatting, CI-ready
๐ฆ Usability
- Extensible API: Modular classes, CLI scripts, YAML configuration
- Docker Support: Containerized demos and development environment
- Documentation: API references, architecture guides, examples
Installation
From PyPI (Recommended)
pip install free-transformer
From Source
Using UV (recommended):
# Install UV
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone and install
git clone https://github.com/udapy/free-transformer.git
cd free-transformer
uv venv --python 3.12
source .venv/bin/activate
uv pip install -e ".[dev]"
Using pip:
git clone https://github.com/udapy/free-transformer.git
cd free-transformer
pip install -e ".[dev]"
๐ Detailed installation instructions: Installation Guide
Quick Start
๐ณ Docker (Fastest)
The fastest way to try Free Transformer:
git clone https://github.com/udapy/free-transformer.git
cd free-transformer
docker-compose up free-transformer-demo
๐ Python API
from free_transformer import FreeTransformer, ModelConfig
# Create and train a model
config = ModelConfig(vocab_size=1000, hidden_dim=128, num_layers=6, latent_dim=8)
model = FreeTransformer(config)
# Training mode
import torch
tokens = torch.randint(0, 1000, (2, 128))
logits, z_logits = model(tokens, mode='training')
# Generation
generated = model.generate(tokens[:, :10], max_new_tokens=20)
๐ Command Line
# Generate synthetic data and run demo
make demo
# Train models separately
make train-baseline # Standard Transformer
make train-free # Free Transformer
make compare # Compare results
๐ฏ Complete tutorial: Quick Start Guide
Manual Installation & Quick Start Demo
-
Generate Small Synthetic Data
make generate-data-small -
Train Baseline Transformer
make train-baseline -
Train Free Transformer
make train-free -
Run Model Comparison
make compare
Or run the full pipeline:
make demo
Check results in:
checkpoints/baseline/checkpoints/free/results/comparison/results.json
Key Features Comparison
| Feature | Standard Transformer | Free Transformer |
|---|---|---|
| Generation | Reactive (token-by-token) | Plan-then-generate |
| Coherence | Local | Global + Local |
| Controllability | Limited | High (via plan manipulation) |
| Training | Cross-entropy loss | Conditional VAE loss |
| Memory | Baseline | +10-15% (inference) |
| Speed | Baseline | -5-10% (inference) |
๐ฌ Detailed comparison: Architecture Overview
Repository Structure
free-transformer/
โโโ src/free_transformer/
โ โโโ model.py
โ โโโ baseline.py
โ โโโ encoder.py
โ โโโ latent.py
โ โโโ injection.py
โ โโโ losses.py
โ โโโ synthetic_data.py
โ โโโ train_utils.py
โ โโโ config.py
โโโ examples/
โ โโโ train_baseline.py
โ โโโ train_free.py
โ โโโ eval_compare.py
โ โโโ generate_data.py
โโโ configs/
โ โโโ baseline.yaml
โ โโโ free_transformer.yaml
โโโ docker/
โ โโโ demo.sh
โ โโโ README.md
โโโ tests/
โ โโโ unit/
โ โโโ integration/
โ โโโ test_comparison.py
โโโ docs/
โโโ Dockerfile
โโโ Dockerfile.cpu
โโโ docker-compose.yml
โโโ Makefile
โโโ pyproject.toml
โโโ .python-version
โโโ LICENSE
โโโ README.md
Testing & Quality
Run all tests:
make test
Quality checks:
make quality
Advanced Features
๐ Multi-GPU Training
# FSDP training with automatic GPU detection
make train-free-fsdp
# Custom distributed training
torchrun --nproc_per_node=auto examples/train_free.py --use-fsdp
๐ Flexible Data
- HuggingFace datasets integration
- Built-in synthetic data generation
- Custom data loading pipelines
๐ง Extensible Architecture
- Modular components for easy customization
- Custom loss functions and training schedules
- Plugin system for new features
๐ Learn more: Training Guide | Multi-GPU Setup
Documentation
Quick Links
- ๐ Getting Started - Installation and setup
- ๐๏ธ Architecture - How Free Transformer works
- ๐ฏ Training Guide - Training best practices
- ๐ API Reference - Complete API documentation
- ๐ก Examples - Code examples and tutorials
- โ FAQ - Frequently asked questions
Local Documentation
# Serve documentation locally
make docs-serve
# Open http://127.0.0.1:8000
License
MIT License โ see LICENSE
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Quick Development Setup
git clone https://github.com/udapy/free-transformer.git
cd free-transformer
make install-all # Install with all dependencies
make test # Run tests
make quality # Check code quality
Before Submitting
- โ
Tests pass:
make test - โ
Code quality:
make quality - โ
Documentation builds:
make docs-build
๐ Full guidelines: Contributing Guide
FAQ
Can I use this for real-world (non-synthetic) data?
Yes! Edit configs and use HuggingFace datasets.
How do I run distributed training?
Use provided CLI flags or edit config. See docs and Makefile.
How do I change architecture parameters?
Edit YAML config files for layer size, latent dim, number of blocks, etc.
Can I run this without installing dependencies locally?
Yes! Use Docker: docker-compose up free-transformer-demo for a complete demo.
What if I don't have a GPU?
Use the CPU Docker image: make docker-build-cpu && make docker-run-cpu
Citation
If you use Free Transformer in your research, please cite:
@software{free_transformer,
title={Free Transformer: Explicit Latent Planning for Autoregressive Generation},
author={Phalak, Uday},
year={2024},
url={https://github.com/udapy/free-transformer},
version={0.1.0}
}
Links
- ๐ฆ PyPI Package
- ๐ Documentation
- ๐ Issues
- ๐ฌ Discussions
Free Transformer - Bringing explicit planning to autoregressive generation
Documentation โข PyPI โข GitHub
Project details
Release history Release notifications | RSS feed
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 free_transformer-0.1.2.tar.gz.
File metadata
- Download URL: free_transformer-0.1.2.tar.gz
- Upload date:
- Size: 23.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e574c3b94422852d0787874bc01359987b59b8d7ade45743f3a8fda58c0d2b96
|
|
| MD5 |
38a038bb3e660fa805269d03811c0082
|
|
| BLAKE2b-256 |
9b7bc58ac7755a0d05b28b25bd5c518e13ad53f20a8c161a64592591a964e5da
|
File details
Details for the file free_transformer-0.1.2-py3-none-any.whl.
File metadata
- Download URL: free_transformer-0.1.2-py3-none-any.whl
- Upload date:
- Size: 22.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9ef5a25db669f99ef48b1a01c8177e5353e8bd927db54f2157b359be9ef0fc9
|
|
| MD5 |
7a2d073db354005673cec0449ae3d9f2
|
|
| BLAKE2b-256 |
f23327132645c7c6a101b290df8d20d21e4db15483bce4926346b46a3aac17ab
|