Our eyes let us see the universe, AI may let us finally understand it! | PhD @ UIUC | Researcher @ NCSA AI Innovation | R&D Intern @ Sandia | Avid Photographer
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
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!
@Neuralwarlock https://t.co/jixB2K78EI
working on a new project called tinyagents, asked claude to make a nice dashboard so i can see whats happening better and like magic, it works!