Pinned Tweet
PyTorch makes our life easy. You want Layernorm? You got it. You want a Linear layer? No problem! But there is a lot of work that goes into each of these modules that we take for granted. Our goal here today is to build a completely functional Transformer model WITH NO FRAMEWORKS! This means no Autograd, no layers no nothing. We have to implement for every operation the Forward and Backward pass and train a baby GPT2 Model!
Of course as credit @karpathy was the inspiration for this!
Timestamps:
0:00 - Introduction
0:50 - Structure of Neural Networks
1:30 - Backprop is just Chain Rule
3:00 - Overview of MyTorch
5:10 - Why ManualGrad when AutoGrad is Easier?
8:50 - What Backprop Looks Like
13:45 - Core Ops for Transformers
15:20 - Boilerplate Code
18:30 - What is __dict__ dunder method?
20:10 - Linear Layer Derivation for single sample
25:45 - Linear Layer Derivation w/ Batch Dimension
35:00 - Linear Layer Implementation
43:40 - Softmax Jacobian Derivation
53:50 - Numerical Stability in Softmax (max trick)
55:53 - SLOW Softmax Implementation
06:45 - Can we avoid the Jacobian? Reinterpret the Softmax
1:14:40 - Efficient Softmax Implementation
1:16:50 - ReLU Activation Implementation
1:20:00 - Embedding Layer Implementation
1:28:30 - Positional Embedding Implementation
1:34:05 - LayerNorm Derivation
2:08:35 - LayerNorm Implementation
2:17:07 - MultiHeadAttention Implementation (Forward Pass)
2:32:05 - MultiHeadAttention Implementation (Backward Pass)
2:53:20 - FeedForward Layer Implementation
2:57:00 - Dropout Implementation
3:02:08 - TransformerBlock Implementation
3:09:01 - SLOW Cross Entropy Derivation (assumes probs)
3:12:00 - SLOW Cross Entropy Implementation
3:16:50 - Cross Entropy on Logits Directly Derivation
3:21:20 - Cross Entropy Implementation
3:24:12 - FlattenForLLM To Make our Shapes Match
3:25:45 - Define the Neural Network Class
3:38:14 - Define the GPT2 Model
3:41:40 - SGD Optimizer Implementation
3:44:45 - Adam Optimizer Implementation
3:52:20 - Character Shakespeare Training Script
4:04:48 - Debugging
4:08:42 - Results
4:12:39 - Why ManualGrad Matters
4
10
648
Every time more news comes out about anything AI, regardless of what’s happening, it only ever means one thing, keep building everyone!!
19
MyTorch is now beating PyTorch Compiled on raw throughput (76K tps vs 70k tps) on GPT2 sized models. with a lower memory footprint (11GB vs 16GB)! After a review Ill push the new code!
1
1
36
watching tiny 1b models generate at like 300-400 tokens/second is kind of uncanny. Like sure its wrong, but it still looks so cool
1
25
My first synthetic dataset! A bunch of conversations about Birds (grounded on about 11K birds and their information from Wiki)
huggingface.co/datasets/priy…
112
So what llms can’t always count letters in a word? There’s plenty of more interesting things for it to do!
1
61



