I built a TPU from scratch. Custom Verilog RTL. JAX→HLO→MLIR→ASM→VLIW→Binary. Real PJRT backend. Runs on Verilog simulator. No CUDA. No handwritten kernels. Pure compiler codegen. Below is a transformer layer compiling and executing from JAX.
I'm happy to announce that v2 of my RL tutorial is now online. I added a new chapter on multi-agent RL, and improved the sections on 'RL as inference' and 'RL+LLMs' (although latter is still WIP), fixed some typos, etc.
https://t.co/dWe5uNgcgp
Our MIT class “6.S184: Introduction to Flow Matching and Diffusion Models” is now available on YouTube!
We teach state-of-the-art generative AI algorithms for images, videos, proteins, etc. together with the mathematical tools to understand them.
https://t.co/wDJcM1YTxJ
(1/4)
I gave a talk at GPU MODE workshop last week on llm.c
- the origin story of llm.c
- being naked in the world without PyTorch and having to re-invent Array, Autograd, Device, Dtype, Compile, Distributed
- how to port a PyTorch layer to 1) explicit PyTorch
- and then to 2) write the backward pass
- 3) port forward & backward pass to C
- 4) string all the layers together
- achieving one file of C with no dependencies that compiles and runs ~instantly, where all memory is pre-planned and allocated a single time, fully deterministic, portable code that can run on a potato or a von Neumann probe
- how most of llm.c was built at 1am-7am in a water villa porch in Maldives and why this is the recommended way to develop software
- convert all of it to run in CUDA on GPU in fp32
- port matmul to cuBLAS
- port attention to cuDNN flash-attention
- introduce bfloat16 mixed precision
- introduce many more optimizations and features like kernel fusions, Packed128, stochastic rounding, full determinism
- add multi-GPU training, NCCL, sharded optimizer
- add multi-node with MPI or file system or socket
- reproduce GPT-2 (1.6B) on one 8XH100 node in 24 hours for $672 in llm.c, achieving (at the time) 29% less memory, 19% faster training that PyTorch nightly, and much faster compile & run
- how open source development attracts Avengers from the internet
- port to training Llama 3 imminent (branch exists)
- many other notable forks
- last thought: how software abstractions like Python/PyTorch and everything else really exist only because humans are finite in knowledge, IQ and attention, and how with increasing AI capability LLMs may export custom binaries like llm.c for any application directly, tearing apart and refactoring all abstractions as needed.
<|endoftext|>
More links in reply
C Programming language
https://t.co/0a2h67giso
Oxidative phosphorylation
https://t.co/oSuBraitni
Gold
https://t.co/LEEzffZUAr
Pomegranate
https://t.co/ylwJWeYtor
Mars
https://t.co/E1JNt15nip
Wittgenstein
https://t.co/u8wAHCJ6Vl
Arnold Schwarzenegger
https://t.co/9R573ooZoO
This is a pretty awesome simple step-by-step guide showing you how to build your own PyTorch (a subset of ops supported) which requires just basic knowledge of C/C++/Python.
https://t.co/ZyFGrC1J6b
The reason to walk through it is to better understand how some of the common PyTorch ops work.
I highly recommend the read.
While async Tensor Parallelism is common among the elite private codebases, the PyTorch team put together a public, accessible and readable one.
pretty cool work from @foofoobuggy@cHHillee@lessw2020@lc_whr tianyu and @wanchao_
Read more here:
https://t.co/pwA6cN8zJk
FSDP & DeepSpeed: Implementations of the ZERO algorithm, but have very different APIs.
In this collaboration with @IBM, @huggingface, @PyTorch, and @ContextualAI, we've outlined how you can go from one to the other easily, and what to watch out for!
https://t.co/7dmULUKzZ6
📽️ New 4 hour (lol) video lecture on YouTube:
"Let’s reproduce GPT-2 (124M)"
https://t.co/QTUdu8b0qh
The video ended up so long because it is... comprehensive: we start with empty file and end up with a GPT-2 (124M) model:
- first we build the GPT-2 network
- then we optimize it to train very fast
- then we set up the training run optimization and hyperparameters by referencing GPT-2 and GPT-3 papers
- then we bring up model evaluation, and
- then cross our fingers and go to sleep.
In the morning we look through the results and enjoy amusing model generations. Our "overnight" run even gets very close to the GPT-3 (124M) model. This video builds on the Zero To Hero series and at times references previous videos. You could also see this video as building my nanoGPT repo, which by the end is about 90% similar.
Github. The associated GitHub repo contains the full commit history so you can step through all of the code changes in the video, step by step.
https://t.co/BOzkxQ8at2
Chapters.
On a high level Section 1 is building up the network, a lot of this might be review. Section 2 is making the training fast. Section 3 is setting up the run. Section 4 is the results. In more detail:
00:00:00 intro: Let’s reproduce GPT-2 (124M)
00:03:39 exploring the GPT-2 (124M) OpenAI checkpoint
00:13:47 SECTION 1: implementing the GPT-2 nn.Module
00:28:08 loading the huggingface/GPT-2 parameters
00:31:00 implementing the forward pass to get logits
00:33:31 sampling init, prefix tokens, tokenization
00:37:02 sampling loop
00:41:47 sample, auto-detect the device
00:45:50 let’s train: data batches (B,T) → logits (B,T,C)
00:52:53 cross entropy loss
00:56:42 optimization loop: overfit a single batch
01:02:00 data loader lite
01:06:14 parameter sharing wte and lm_head
01:13:47 model initialization: std 0.02, residual init
01:22:18 SECTION 2: Let’s make it fast. GPUs, mixed precision, 1000ms
01:28:14 Tensor Cores, timing the code, TF32 precision, 333ms
01:39:38 float16, gradient scalers, bfloat16, 300ms
01:48:15 torch.compile, Python overhead, kernel fusion, 130ms
02:00:18 flash attention, 96ms
02:06:54 nice/ugly numbers. vocab size 50257 → 50304, 93ms
02:14:55 SECTION 3: hyperpamaters, AdamW, gradient clipping
02:21:06 learning rate scheduler: warmup + cosine decay
02:26:21 batch size schedule, weight decay, FusedAdamW, 90ms
02:34:09 gradient accumulation
02:46:52 distributed data parallel (DDP)
03:10:21 datasets used in GPT-2, GPT-3, FineWeb (EDU)
03:23:10 validation data split, validation loss, sampling revive
03:28:23 evaluation: HellaSwag, starting the run
03:43:05 SECTION 4: results in the morning! GPT-2, GPT-3 repro
03:56:21 shoutout to llm.c, equivalent but faster code in raw C/CUDA
03:59:39 summary, phew, build-nanogpt github repo
I recreated an entire GPT architecture in a spreadsheet.
It is a nanoGPT designed by @karpathy with about 85000 parameters, small enough to be packed into a spreadsheet file.
It is great for learning about how transformer works as it shows all the data and parameters going through a transformer pipeline, and all the calculations are actually working inside those cells.
Here is a link to the project where you can download the file and play with it yourself. No coding needed, it is just a spreadsheet. 😉
Spreadsheet is all you need:
https://t.co/dreZlBDq4d
Also thanks to @BrendanBycroft with his LLM visualisation project which inspired this.
The videos for the spring semester of CMU 11-711 Advanced NLP are now all available 📺
https://t.co/Ci2YTajJsr
Thanks to the TAs, students in the class, and everyone who followed along. We're doing it again in the Fall!
https://t.co/FEcvI56izD
If you learn CUDA, you probably have read the legendary matrix transpose tutorial by Mark Harris.
📚Today, I present to you a CUDA tutorial by friends at @colfaxintl and myself. We explore many CUDA memory concepts like in Mark's tutorial, and more.
Crucially, we show how to implement these concepts in @nvidia's CuTe library. Yes, it's the one that powers Flash Attention⚡️.
I am grateful for friends at @colfaxintl, especially Jay Shah, for inviting me to help them this project.
This collaboration started in December 2023, when I got interested in @nvidia's CuTe library by reading the FlashAttention repo.
Despite my enthusiasm for the the library, learning it was a headache. I literally made no progress after weeks reading CuTe tutorial and code.
Then one day, I stumbled upon Jay's Flash Attention tutorial (https://t.co/oSL36ySSmB). His writing revealed to me the mathematical elegance in CuTe's design.
Teaser: can you imagine abstract algebra concepts -- like integral modules -- have anything to do with efficient deep learning kernels?!
But I digressed. Since reading Jay's works, I have held so much respect for him that being able to collaborate with his team feels profoundly rewarding.
Anyhow, I hope this CUDA tutorial makes you like CUDA programming and the CuTe library a little bit more.
Please stay tuned. We will be back with more tutorials.
Glad to see folks studying this problem. The paper suggests that (their impl. of) FlashAttention has higher numerical error than baseline. However, FlashAttention in BF16/FP16 will often have *lower* error than baseline if the impl. does internal softmax rescaling in FP32