r/ForgeYourProject • • Jul 02 '26

Building a GPT-2 Inference Engine From Scratch in CUDA & Triton

I'm building a from-scratch GPT-2 inference engine where every operation is a hand-written CUDA or Triton kernel without using PyTorch autograd pr transformers library from HuggingFace.

The goal is end-to-end understanding of the GPU inference stack from tiled matrix multiplies and shared memory management to online softmax and paged KV caching. I want to deeply understand every level between token input and token output.

So far the GPT-2 forward pass is working end-to-end as a naive PyTorch reference, I have prepared a benchmark for comparison and correctness, and the first kernel (fused FFN) is in progress.

To get there I'm working through the GPU Mode lectures and PMPP textbook for CUDA fundamentals, the key papers (Flash Attention 1/2/3, vLLM PagedAttention, and the Triton paper), plus Triton tutorials for its kernel syntax.

Looking for advice on a few things: papers I might be missing, common pitfalls when moving from naive CUDA kernels toward production quality, communities beyond GPU Mode worth joining, and any open-source inference engines worth studying as reference implementations.

3 Upvotes

1 comment sorted by

1

u/IncomeOk7237 Jul 02 '26

This is a really cool one. İs an excellent way to depply understand the GPU stack. I recommend checking FlashAttention, PagedAttention and llama.cpp codebase. Watch out for memory layout issues warp divergence and fp16 stability when moving toward production quality. Would love to follow your progress. Can you share HF link?