Beyond-NanoGPT is the minimal and educational repo aiming to bridge between nanoGPT and research-level deep learning. This repo includes annotated and from-scratch implementations of almost 100 crucial modern techniques in frontier deep learning, aiming to help newcomers learn enough to start running experiments of their own.
The repo implements everything from KV caching and speculative decoding for LLMs to architectures like vision transformers and MLP-mixers; from attention variants like linear or multi-latent attention to generative models like denoising diffusion models and flow matching algorithms; from landmark RL papers like PPO, A3C, and AlphaZero to systems fundamentals like GPU communication algorithms and data/tensor parallelism.
Because everything is implemented by-hand, the code comments explain the especially subtle details often glossed over both in papers and production codebases.
attention-variants/linear_attention.ipynb,generative-models/train_ddpm.py,rl/fundamentals/train_ppo.py.
LESSONS.md documents some of the things I've learned in the months spent writing this codebase.
Clone the Repo:
git clone https://github.com/tanishqkumar/beyond-nanogpt.git
Get Minimal Dependencies:
pip install torch numpy torchvision wandb tqdm transformers datasets diffusers matplotlib pillow jupyter gym
Start learning!
The code is meant for you to read carefully, hack around with, then re-implement yourself from scratch and compare to.
You can just run .py files with vanilla Python in the following way.
cd architectures/
python train_dit.py
or for instance
cd rl/fundamentals/
python train_reinforce.py --verbose --wandb
Everything is written to be run on a single GPU. The code is self-documenting with comments for intuition and elaborating on subtleties I found tricky to implement. Arguments are specified at the bottom of each file. Jupyter notebooks are meant to be stepped through.
Asterisks (*) denote particularly tricky implementations.
language-models/transformer.py and train_naive.py [paper]architectures/train_vit.py [paper]architectures/train_dit.py [paper]architectures/train_rnn.py [paper]architectures/train_resnet.py [paper]architectures/train_mlp_mixer.py [paper]architectures/train_lstm.py [paper]architectures/train_moe.py [paper]architectures/train_mamba.py [paper]attention-variants/vanilla_attention.ipynb [paper]attention-variants/mhsa.ipynb [paper]attention-variants/gqa.ipynb [paper]attention-variants/linear_attention.ipynb [paper]attention-variants/sparse_attention.ipynb [paper]attention-variants/cross_attention.ipynb [paper]attention-variants/mla.ipynb [paper]language-models/dataloaders [reference]
language-models/bpe.ipynb [paper]language-models/KV_cache.ipynb [reference]language-models/speculative_decoding.ipynb [paper]language-models/rope.ipynb [paper]language-models/train_mtp.py [paper]rl/fundamentals
rl/actor-critic
rl/model-based
train_mpc.py[reference]train_expert_iteration.py [paper]rl/chess [paper]
model.py and env.pymcts.pytrain.pymcts.pyrl/llms
train_grpo_gsm.py [paper]train_grpo_humor.pygenerative-models/train_gan.py [paper]generative-models/train_pix2pix.py [paper]generative-models/train_vae.py [paper]
generative-models/train_autoencoder.pygenerative-models/train_ddpm.py [paper]generative-models/ddpm_classifier_guidance.py [paper]
generative-models/ddpm_classifier_free_guidance.py [paper]generative-models/train_flow_matching.py [paper]mlsys/comms.py [reference]mlsys/train_ddp.py [paper]mlsys/train_tp.py [paper]mlsys/kernels
vector_add.pyconv1d.pycopy2d.pylayernorm.pyreverse_array.pyswiglu.pytiled_gemm.pyevals/eval_gsm8k.py [paper]evals/eval_mmlu.py [paper]evals/eval_simpleqa.py [paper]rag/intro_rag.pyagents/basic-search-useagents/coding-agent
/tools]agent.pymemory.py.py scripts take in --verbose and --wandb as command line arguments when you run them. Feel free to hack these to your needs.@misc{kumar2025beyond,
author = {Tanishq Kumar},
title = {Beyond-NanoGPT: From LLM Beginner to AI Researcher},
year = {2025},
howpublished = {\url{https://github.com/tanishqkumar/beyond-nanogpt}},
note = {Accessed: 2025-01-XX}
}
Happy coding, and may your gradients never vanish!
369 commits
Python
57.9%
Jupyter Notebook
42.1%
Beyond-NanoGPT is the minimal and educational repo aiming to bridge between nanoGPT and research-level deep learning. This repo includes annotated and from-scratch implementations of almost 100 crucial modern techniques in frontier deep learning, aiming to help newcomers learn enough to start running experiments of their own.
The repo implements everything from KV caching and speculative decoding for LLMs to architectures like vision transformers and MLP-mixers; from attention variants like linear or multi-latent attention to generative models like denoising diffusion models and flow matching algorithms; from landmark RL papers like PPO, A3C, and AlphaZero to systems fundamentals like GPU communication algorithms and data/tensor parallelism.
Because everything is implemented by-hand, the code comments explain the especially subtle details often glossed over both in papers and production codebases.
attention-variants/linear_attention.ipynb,generative-models/train_ddpm.py,rl/fundamentals/train_ppo.py.
LESSONS.md documents some of the things I've learned in the months spent writing this codebase.
Clone the Repo:
git clone https://github.com/tanishqkumar/beyond-nanogpt.git
Get Minimal Dependencies:
pip install torch numpy torchvision wandb tqdm transformers datasets diffusers matplotlib pillow jupyter gym
Start learning!
The code is meant for you to read carefully, hack around with, then re-implement yourself from scratch and compare to.
You can just run .py files with vanilla Python in the following way.
cd architectures/
python train_dit.py
or for instance
cd rl/fundamentals/
python train_reinforce.py --verbose --wandb
Everything is written to be run on a single GPU. The code is self-documenting with comments for intuition and elaborating on subtleties I found tricky to implement. Arguments are specified at the bottom of each file. Jupyter notebooks are meant to be stepped through.
Asterisks (*) denote particularly tricky implementations.
language-models/transformer.py and train_naive.py [paper]architectures/train_vit.py [paper]architectures/train_dit.py [paper]architectures/train_rnn.py [paper]architectures/train_resnet.py [paper]architectures/train_mlp_mixer.py [paper]architectures/train_lstm.py [paper]architectures/train_moe.py [paper]architectures/train_mamba.py [paper]attention-variants/vanilla_attention.ipynb [paper]attention-variants/mhsa.ipynb [paper]attention-variants/gqa.ipynb [paper]attention-variants/linear_attention.ipynb [paper]attention-variants/sparse_attention.ipynb [paper]attention-variants/cross_attention.ipynb [paper]attention-variants/mla.ipynb [paper]language-models/dataloaders [reference]
language-models/bpe.ipynb [paper]language-models/KV_cache.ipynb [reference]language-models/speculative_decoding.ipynb [paper]language-models/rope.ipynb [paper]language-models/train_mtp.py [paper]rl/fundamentals
rl/actor-critic
rl/model-based
train_mpc.py[reference]train_expert_iteration.py [paper]rl/chess [paper]
model.py and env.pymcts.pytrain.pymcts.pyrl/llms
train_grpo_gsm.py [paper]train_grpo_humor.pygenerative-models/train_gan.py [paper]generative-models/train_pix2pix.py [paper]generative-models/train_vae.py [paper]
generative-models/train_autoencoder.pygenerative-models/train_ddpm.py [paper]generative-models/ddpm_classifier_guidance.py [paper]
generative-models/ddpm_classifier_free_guidance.py [paper]generative-models/train_flow_matching.py [paper]mlsys/comms.py [reference]mlsys/train_ddp.py [paper]mlsys/train_tp.py [paper]mlsys/kernels
vector_add.pyconv1d.pycopy2d.pylayernorm.pyreverse_array.pyswiglu.pytiled_gemm.pyevals/eval_gsm8k.py [paper]evals/eval_mmlu.py [paper]evals/eval_simpleqa.py [paper]rag/intro_rag.pyagents/basic-search-useagents/coding-agent
/tools]agent.pymemory.py.py scripts take in --verbose and --wandb as command line arguments when you run them. Feel free to hack these to your needs.@misc{kumar2025beyond,
author = {Tanishq Kumar},
title = {Beyond-NanoGPT: From LLM Beginner to AI Researcher},
year = {2025},
howpublished = {\url{https://github.com/tanishqkumar/beyond-nanogpt}},
note = {Accessed: 2025-01-XX}
}
Happy coding, and may your gradients never vanish!
369 commits
Python
57.9%
Jupyter Notebook
42.1%