A clean, minimal PyTorch implementation of a Pre-LN Transformer encoder block built entirely from first principles.
Run a lightweight forward-pass demo:
pip install -r requirements.txt
python demo_forward.pyThis validates the model architecture, attention mechanism, and end-to-end forward computation using synthetic data.
Run the full Transformer encoder implementation:
python transformer_encoder.pyThis executes the complete encoder block with attention, MLP layers, residual connections, and normalization.
transformer_encoder.py # Full Transformer encoder implementation
demo_forward.py # Lightweight forward-pass demonstration
requirements.txt # Dependencies
This implementation follows the standard Pre-LayerNorm Transformer Encoder pattern:
Input → LayerNorm → Multi-Head Attention → Residual →
LayerNorm → MLP (GELU) → Residual → Output
It includes:
- Scaled dot-product multi-head self-attention
- Projection layers for Q/K/V
- Attention output projection
- Position-wise MLP with GELU
- Residual skip connections
- LayerNorm for training stability
This repo is ideal for showcasing architectural understanding of modern Transformer building blocks.
.
├── transformer_encoder.py
├── demo_forward.py
├── requirements.txt
├── CONTRIBUTING.md
└── SECURITY.md
The encoder block follows the common Pre-LN Transformer pattern:
Input embeddings
|
v
LayerNorm (LN1)
|
v
Multi-Head Self-Attention
|
v
Residual Add ---------------+
| |
v |
LayerNorm (LN2) |
| |
v |
Position-wise Feedforward |
(MLP / GELU) |
| |
v |
Residual Add <--------------+
|
v
Encoder outputSee CONTRIBUTING.md for contribution workflow and coding standards.
MIT License. See LICENSE for details.