A complete implementation of a Transformer model for English-German translation, trained on the WMT17 dataset.
transformer_project/
├── modelling/ # Core model components
│ ├── transformer.py # Main Transformer architecture
│ ├── attention.py # Multi-head attention mechanism
│ ├── gpt2_tokenizer.py # BPE tokenizer implementation
│ └── ...
├── run/ # Executable scripts (see below)
├── saved_models/ # Downloaded models & training outputs (not in git)
│ ├── tokenizer/ # Production tokenizer (32k vocab, 5M samples)
│ ├── tokenizer_quick/ # Quick test tokenizer
│ └── model.pt # Best model (5M samples, 5 epochs)
├── dataset.py # WMT17 dataset loader
└── utils.py # Helper functions
Note: Pre-trained models are hosted on 🤗 Hugging Face
# Clone repository
git clone https://github.com/lennart02/transformer_project.git
cd transformer_project
# Option A: Install with Poetry
poetry install
# Option B: Install with pip (simpler alternative)
pip install torch datasets tqdm tokenizers transformers pytest huggingface-hubNote: This project supports both Poetry (for dependency management) and pip installation.
Pre-trained models are hosted on 🤗 Hugging Face for easy access:
Option A: Automatic Download (Recommended)
# Models will be downloaded automatically on first use
python run/inference.pyOption B: Manual Download
# Install huggingface-cli if needed
pip install huggingface-hub
# Download models
huggingface-cli download Lennart02/transformer-de-en --local-dir saved_models/
# Or using Python
python -c "from huggingface_hub import snapshot_download; snapshot_download('Lennart02/transformer-de-en', local_dir='saved_models/')"🔗 Model Repository: Lennart02/transformer-de-en
Why Hugging Face?
- No Git LFS issues with large model files
- Fast CDN-based downloads
- Easy versioning and model sharing
saved_models/is your workspace for experiments (ignored by git)
# Interactive translation (models download automatically on first use)
python run/inference.py
# With Poetry
poetry run python run/inference.py
# Test with 5 samples
python run/test_validation.py --num-samples 5
# Or manually download models first
python download_models.pyTranslate English sentences interactively using a trained model.
python run/inference.py
python run/inference.py --checkpoint saved_models/model_quick.ptTest translation quality with BLEU scores and benchmarks.
python run/test_validation.py --num-samples 5
python run/test_validation.py --bleu --num-samples 100
python run/test_validation.py --benchmarkKey options: --checkpoint PATH, --num-samples N, --bleu, --benchmark
Train a new Transformer model from scratch.
python run/main.py --quick # Fast test (5 min, 1000 samples)
python run/main.py # Full training (1-2h, 5M samples)
python run/main.py --resume saved_models/model.ptKey options: --quick, --resume PATH, --max-samples N, --epochs N, --batch-size N, --no-amp
Output: Saves to saved_models/model.pt and saved_models/model_best_with_AMP.pt
Train a BPE tokenizer on the WMT17 dataset (32k vocab, 5M samples).
python run/train_tokenizer.pyNote: Only needed if retraining. Pre-trained tokenizers are available on Hugging Face.
Verify the model can memorize 10 sentences (~5 min).
python run/simple_overfit_test.pyBenchmark training speed on CPU vs GPU (FP32 vs AMP).
python run/benchmark_devices.py --modes all
python run/benchmark_devices.py --modes gpu ampPre-trained models are available on 🤗 Hugging Face: Lennart02/transformer-de-en
- Training data: 5,000,000 WMT17 samples
- Epochs: 5
- Architecture: 6-layer encoder/decoder, 512d model, 8 attention heads
- Performance: ~28% BLEU score on validation set
- Training time: ~5 hours on GPU with AMP
- Vocabulary size: 32,000 tokens
- Algorithm: Byte-Pair Encoding (BPE)
- Languages: English + German (with proper umlaut support)
- Smaller tokenizer for quick experiments (8,000 tokens)
Download: Models are automatically downloaded on first use, or manually via:
huggingface-cli download Lennart02/transformer-de-en --local-dir saved_models/# Run all unit tests
pytest test/- Type: Transformer (Encoder-Decoder)
- Encoder layers: 6
- Decoder layers: 6
- Model dimension: 512
- Attention heads: 8
- Feedforward dimension: 2048
- Max sequence length: 128 tokens
- Dropout: 0.1
Download models from Hugging Face:
huggingface-cli download Lennart02/transformer-de-en --local-dir saved_models/Or let them download automatically on first run:
python run/inference.pyReduce batch size in config or use AMP:
python run/main.py --batch-size 16- Attention Is All You Need - Original Transformer paper
- WMT17 Dataset - Translation dataset