A complete implementation of the Transformer architecture in PyTorch
Project description
TransformerKit
A complete, educational implementation of the Transformer architecture from the paper "Attention Is All You Need" in PyTorch.
Features
✅ Complete Implementation: All components from the original Transformer paper
✅ Well-Tested: Comprehensive test suite with 12+ tests
✅ Production-Ready: Proper packaging, type hints, and documentation
✅ Educational: Heavily commented code with clear explanations
✅ Flexible: Configurable architecture and hyperparameters
✅ Examples Included: Training scripts and usage demonstrations
Installation
From Source
# Clone the repository
git clone https://github.com/charansoma3001/transformerkit.git
cd transformerkit
# Install in editable mode
pip install -e .
# Or install with development dependencies
pip install -e ".[dev]"
Using pip (when published)
pip install transformerkit
Requirements
- Python 3.8+
- PyTorch 2.0+
- NumPy 1.24+
Overview
This implementation includes all core components of the Transformer model:
- Scaled Dot-Product Attention: The fundamental attention mechanism
- Multi-Head Attention: Parallel attention layers with different learned projections
- Positional Encoding: Sinusoidal position embeddings for sequence order
- Encoder Stack: 6 layers of self-attention and feed-forward networks
- Decoder Stack: 6 layers with masked self-attention and cross-attention
- Position-wise Feed-Forward Networks: Two-layer MLPs with ReLU activation
Architecture
Input Sequence → Embedding → Positional Encoding →
ENCODER (×6 layers)
├── Multi-Head Self-Attention
├── Add & Norm
├── Feed-Forward Network
└── Add & Norm
↓
Encoder Output
↓
DECODER (×6 layers)
├── Masked Multi-Head Self-Attention
├── Add & Norm
├── Multi-Head Cross-Attention (with Encoder Output)
├── Add & Norm
├── Feed-Forward Network
└── Add & Norm
↓
Linear Projection → Output Probabilities
Files
config.py: Model configuration and hyperparametersattention.py: Attention mechanisms (scaled dot-product and multi-head)components.py: Core components (positional encoding, feed-forward, layer norm)encoder.py: Encoder layer and encoder stackdecoder.py: Decoder layer and decoder stacktransformer.py: Complete transformer modelutils.py: Utility functions (masking, decoding, etc.)train.py: Training script with copy task demonstrationexample.py: Usage examples and demonstrations
Quick Start
Create and Use a Model
from transformerkit import create_transformer, TransformerConfig
import torch
# Create model with default configuration
model = create_transformer()
# Or customize the configuration
config = TransformerConfig(
d_model=512,
n_heads=8,
n_layers=6,
vocab_size=10000
)
model = create_transformer(config)
# Forward pass
src = torch.randint(0, 10000, (32, 20)) # (batch_size, src_len)
tgt = torch.randint(0, 10000, (32, 15)) # (batch_size, tgt_len)
output = model(src, tgt) # (32, 15, 10000)
Run Examples
# Basic usage examples
python examples/basic_usage.py
# Train on copy task
python examples/train_copy_task.py
- Forward pass through the network
- Sequence generation with greedy decoding
- Architecture overview and parameter counts
### Run Tests
```bash
# Run all tests
pytest
# Run with coverage
pytest --cov=transformer --cov-report=html
## Usage
### Create a Transformer Model
```python
from transformerkit import create_transformer
from transformerkit.config import TransformerConfig
# Use default configuration
config = TransformerConfig(
d_model=512, # Model dimension
n_heads=8, # Number of attention heads
n_layers=6, # Number of encoder/decoder layers
d_ff=2048, # Feed-forward dimension
dropout=0.1, # Dropout rate
vocab_size=10000, # Vocabulary size
max_seq_length=512 # Maximum sequence length
)
model = create_transformer(config)
Forward Pass
import torch
# Create sample input (batch_size, seq_len)
src = torch.randint(0, 10000, (32, 20)) # Source sequence
tgt = torch.randint(0, 10000, (32, 15)) # Target sequence
# Forward pass
output = model(src, tgt) # Shape: (32, 15, 10000)
# Get predictions
predictions = output.argmax(dim=-1)
Generate Sequences
from transformerkit.utils import greedy_decode, create_padding_mask
# Encode source sequence
src = torch.tensor([[10, 20, 30, 40, 50]])
src_mask = create_padding_mask(src)
# Generate with greedy decoding
generated = greedy_decode(
model, src, src_mask,
max_len=20,
start_idx=1, # <start> token
end_idx=2 # <end> token
)
Key Features
Attention Masking
The implementation supports two types of masking:
- Padding Mask: Prevents attention to padding tokens
- Look-ahead Mask: Prevents decoder from attending to future positions
from transformerkit.utils import create_padding_mask, create_target_mask
# Padding mask for encoder
src_mask = create_padding_mask(src, pad_idx=0)
# Combined mask for decoder (padding + look-ahead)
tgt_mask = create_target_mask(tgt, pad_idx=0)
Decoding Strategies
Two decoding strategies are implemented:
- Greedy Decoding: Always selects the most likely next token
- Beam Search: Maintains multiple hypotheses for better quality
from transformerkit.utils import beam_search_decode
# Beam search with width 5
generated = beam_search_decode(
model, src, src_mask,
max_len=20,
start_idx=1,
end_idx=2,
beam_width=5
)
Model Configuration
The default configuration matches the base model from the paper:
| Parameter | Value | Description |
|---|---|---|
| d_model | 512 | Model dimension |
| n_heads | 8 | Number of attention heads |
| n_layers | 6 | Number of encoder/decoder layers |
| d_ff | 2048 | Feed-forward hidden dimension |
| dropout | 0.1 | Dropout rate |
| max_seq_length | 5000 | Maximum sequence length |
For the "big" model variant, use:
config = TransformerConfig(
d_model=1024,
n_heads=16,
n_layers=6,
d_ff=4096,
dropout=0.3
)
Implementation Details
Positional Encoding
Uses sinusoidal functions as described in the paper:
PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
Attention Mechanism
Scaled dot-product attention:
Attention(Q, K, V) = softmax(QK^T / √d_k) V
Multi-head attention applies this in parallel across multiple representation subspaces.
Layer Normalization
Applied after each sub-layer (post-norm), combined with residual connections:
LayerNorm(x + Sublayer(x))
Training Tips
- Learning Rate: Use a warmup schedule as in the paper
- Label Smoothing: Helps prevent overfitting
- Gradient Clipping: Prevents exploding gradients (max_norm=1.0)
- Batch Size: Larger batches generally work better (try 32-128)
Development
Setup Development Environment
# Clone and install
git clone https://github.com/charansoma3001/transformerkit.git
cd transformerkit
pip install -e ".[dev]"
Code Quality
# Format code
black src/ tests/ examples/
isort src/ tests/ examples/
# Lint
flake8 src/
# Type check
mypy src/
# Run tests
pytest
See CONTRIBUTING.md for detailed contribution guidelines.
Project Structure
transformer/
├── src/transformerkit/ # Main package
│ ├── __init__.py # Package exports
│ ├── config.py # Configuration
│ ├── attention.py # Attention mechanisms
│ ├── components.py # Core components
│ ├── encoder.py # Encoder implementation
│ ├── decoder.py # Decoder implementation
│ ├── model.py # Complete transformer
│ └── utils.py # Utilities
├── tests/ # Test suite
├── examples/ # Usage examples
├── docs/ # Documentation
├── .github/workflows/ # CI/CD
├── setup.py # Package setup
├── pyproject.toml # Modern packaging config
└── requirements.txt # Dependencies
Documentation
- Architecture Guide: Detailed explanation of transformer components
- Contributing Guide: How to contribute to this project
- Examples: See
examples/directory for working code
License
This project is licensed under the MIT License - see the LICENSE file for details.
References
- Attention Is All You Need (Vaswani et al., 2017)
- The Annotated Transformer
- The Illustrated Transformer
Citation
If you use this implementation in your research, please cite the original paper:
@article{vaswani2017attention,
title={Attention is all you need},
author={Vaswani, Ashish and Shazeer, Noam and Parmar, Niki and Uszkoreit, Jakob and Jones, Llion and Gomez, Aidan N and Kaiser, {\L}ukasz and Polosukhin, Illia},
journal={Advances in neural information processing systems},
volume={30},
year={2017}
}
Acknowledgments
This implementation is for educational purposes and follows the architecture described in "Attention Is All You Need" by Vaswani et al.
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 transformerkit-0.1.0.tar.gz.
File metadata
- Download URL: transformerkit-0.1.0.tar.gz
- Upload date:
- Size: 30.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed044afeea4cca19dcb774d517fe550746f415a8daa13714bcc849968b49b6f6
|
|
| MD5 |
40af90782baf17171446de001d9446f0
|
|
| BLAKE2b-256 |
8864af9fc50710cfd9a7822eae0b9b854f632eb3b2078fe103a9ba2cacca8e95
|
File details
Details for the file transformerkit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: transformerkit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9cc97f1a5cfa2ca61da8ab88de4a165042c0c211aac4236b93364bfe455de13
|
|
| MD5 |
136549664d877cc0ae4d1677bd785776
|
|
| BLAKE2b-256 |
150d58630a2915518d4513879624b7c731a0322f7c03b8bc6833d7a28570feb0
|