MCPcopy Index your code
hub / github.com/alan-cooney/transformer-from-scratch

github.com/alan-cooney/transformer-from-scratch @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
92 symbols 417 edges 21 files 77 documented · 84%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Transformer From Scratch

A transformer built from scratch in PyTorch, using Test Driven Development (TDD) & modern development best-practices.

It is intended to be used as reference for curricula such as Jacob Hilton's Deep Leaning Curriculum. In particular, because each module (e.g. the positional encoding) is individually tested, it's easy to build all the discrete parts of a transformer and quickly understand what is broken (rather than trying to debug a large model).

Only basic PyTorch linear algebra functions are used (e.g. tensor multiplication), with no use of higher-level modules such as Linear.

Setup

This project comes with a DevContainer for one-click setup (e.g. with GitHub Codespaces). The quickest way to get started is to use this DevContainer, which will install all dependencies. Alternatively you can clone the repo and run poetry install yourself.

Architecture

A decoder-only architecture is used (i.e. similar to GPT-2). Apart from this however, the implementation is based off the original Attention is All You Need paper. Terminology is consistent with A Mathematical Framework for Transformer Circuits, e.g. the code explicitly refers to the "residual stream". The resulting transformer model looks like this (but with 8 layers):

Transformer architecture from A Mathematical Framework for Transformer
Circuits

Testing Strategy

The transformer is split into modules (e.g. Encoder). Each module is then tested to verify that it can learn to do what we expect.

For example, we know from A Mathematical Framework for Transformer Circuits that an Encoder and Decoder (with separate weights) tend to learn bigram statistics - the probability of the next token given just the current token (e.g. Barak -> Obama). We therefore verify that these two modules can do this together. Similarly, the MultiHeadAttention module should be able to move information between layers, so we directly verify this.

To run the tests, run poetry run pytest.

Types

The project uses Google Jaxtyping (it also works with PyTorch), to type tensors (e.g. BatchTokenIndicesTT = Int[Tensor, "BATCH POSITION"]). The underlying data type (Int/Float) is checked with mypy, and runtime type checking is enabled for all tests with pytest. Runtime type checking is not enabled during training, as this would have a large performance impact.

Core symbols most depended-on inside this repo

cross_entropy_loss
called by 4
transformer_from_scratch/components/cross_entropy_loss.py
evaluate
called by 3
transformer_from_scratch/train.py
attention
called by 3
transformer_from_scratch/components/attention.py
get_default_device
called by 2
transformer_from_scratch/train.py
train_loop
called by 2
transformer_from_scratch/train.py
mask
called by 2
transformer_from_scratch/components/attention.py
get_datasets
called by 1
transformer_from_scratch/examples/gpt2_code_comparison.py
get_tokenized_datasets
called by 1
transformer_from_scratch/examples/gpt2_code_comparison.py

Shape

Method 44
Class 26
Function 22

Languages

Python100%

Modules by API surface

transformer_from_scratch/tests/test_train.py15 symbols
transformer_from_scratch/components/tests/test_mlp.py11 symbols
transformer_from_scratch/components/tests/test_attention.py10 symbols
transformer_from_scratch/components/tests/test_embed_unembed.py8 symbols
transformer_from_scratch/components/embed_unembed.py6 symbols
transformer_from_scratch/components/tests/test_positional_encoding.py5 symbols
transformer_from_scratch/components/attention.py5 symbols
transformer_from_scratch/examples/gpt2_code_comparison.py4 symbols
transformer_from_scratch/transformer.py3 symbols
transformer_from_scratch/train.py3 symbols
transformer_from_scratch/tests/test_transformer.py3 symbols
transformer_from_scratch/components/tests/test_cross_entropy_loss.py3 symbols

For agents

$ claude mcp add transformer-from-scratch \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page