r/learnmachinelearning • u/pgmesa • 1h ago
Project I rebuilt PyTorch's core to understand how it works, and wrote a series that explains each piece
Hey all!
I use PyTorch almost every day, and for a long time the engine underneath was a black box to me: what actually happens when you add two tensors, and how is it so fast on so many CPUs and GPUs? Three and a half years ago, in my free time, I started trying to answer that.
How I made it. I started small, writing my own versions of the parts I thought I understood (an autograd engine in Python, then one in C++ on top of libtorch), and each one showed me how much I was still missing. In the end the only way forward was reading PyTorch's own source. It's huge, and part of the path from Python to the kernels is generated at build time, so it isn't even in the repo: it took me a long time just to find my way around. What made it click was rebuilding each piece myself, in C++ with Python bindings: the tensor and its strides, the loop behind almost every operation (TensorIterator), SIMD kernels, reductions and threads, autograd and, in the end, CUDA. Every operation is tested against PyTorch and timed next to it, so I knew when I had understood something and when I had not.
Piece by piece, that rebuild became Raxis, a library you can use much like PyTorch: it's about as fast, it's on PyPI (pip install raxis), and it trains real models like CNNs, Vision Transformers and nanoGPT. At some point it stopped being just for me, and I wanted it to be the code I would have liked to find when I started: it keeps the techniques that make PyTorch fast and little else, with no generated code and far fewer layers, so you can follow a call from Python to the kernel in your editor. It doesn't try to replace PyTorch: there's no distributed training, no torch.compile, no float16/bfloat16, and only NVIDIA GPUs. Much of what makes PyTorch's source hard to read exists because of exactly those features.
Code: https://github.com/pgmesa/raxis
I also wrote "Rebuilding PyTorch", a series that explains what I learned, in the order I learned it, for anyone as curious as I was who opened PyTorch's codebase and didn't know where to start:
- What a tensor really is, and why slicing it costs nothing
- How PyTorch walks a tensor of any shape
- TensorIterator, the loop behind almost every operation
- SIMD kernels for every CPU
- Reductions and threads
- The autograd engine
- The same operations on a GPU
Each article starts from a question and walks through PyTorch's code step by step, with Raxis and measurements side by side: https://blog.pgmesa.com/series/rebuilding-pytorch/
It's dense material, more to study than to read in one sitting: it took me years to understand all of it and to let it sink in, and I hope it takes you much less. To start you only need to have written some PyTorch (some C/C++ helps), and the rest is curiosity and going one piece at a time. Feedback and questions are more than welcome, and I hope it helps you learn as much as it helped me!