Show HN: NanoEuler – GPT-2 scale model in pure C/CUDA from scratch
TL;DR Highlight
A GPT-2-class LLM with pretraining and SFT built in pure C/CUDA, no PyTorch, ~116M params.
Who Should Read
A backend developer who only uses LLM APIs and wants to see in code how training (backprop, tokenizer, SFT) works inside.
Core Mechanics
- Everything in C/CUDA, no PyTorch
- Backprop, BPE tokenizer, pretraining and SFT all live in one repo.
- Forward and backward passes are hand-written with no ML libraries or autograd.
- ~116M params on one RTX 4070
- The GPU model uses dim 768, 16 layers, context 512 and a 4096-token vocab.
- The small CPU model has about 1.05M params and trains in a few hours on 12 cores.
- Backward pass checked by finite differences
- Every parameter tensor's analytic gradient is compared to a central finite difference.
- Run in double precision, max relative error was 1.02e-04 against a 1e-2 threshold.
- Hand-written FlashAttention made training steps about 3x faster
- It uses tiling and online softmax and never builds the T×T matrix in memory.
- Matmuls go to cuBLAS (TF32 tensor cores) while the other kernels are custom.
- SFT applies loss only to response tokens
- Alpaca examples are rendered with an instruction template.
- Prompt and padding positions get target -1, which yields zero gradient.
- It is an educational artifact, not a chatbot
- Output has fluent grammar but little substance, and the SFT model is shallow and often wrong.
- The README says a ~135M model needs about 600B training tokens to become a basic assistant.
Community Reactions
- Criticism that the Neural ODE framing is off
- One commenter said mentioning Neural ODEs does not make sense here.
- They noted most transformers use residuals but this does not train a neural ODE.
- Opinion that the README looks AI-generated
- One commenter suggested removing the em-dashes.
- Another asked how much of the project was LLM-generated.
- Doubts about whether the CUDA code works
- A commenter pointed out an LLM-left comment in the CUDA source saying it is untested.
- They also found the coding style very odd.
- Questions about training scale and speed
- One commenter asked how long it trained and on how many tokens.
- Others asked how it compares to PyTorch in speed and how matmuls were optimized.
How to Apply
- Run make check to verify backprop
- Clone the repo on Linux with gcc and run make check.
- Look at per-tensor relative errors and the 'backward OK' line.
- Train the small model on CPU to see the full flow
- Run make, then ./nanoeuler train, then ./nanoeuler chat to continue prompts.
- Read nanoeuler.c in that order: tokenizer, training, sampling.
- Compare SFT loss masking with your fine-tuning code
- Check that loss applies only to response tokens and prompt/padding targets are -1.
- Add the mask if missing and compare training results.
- Test custom kernels against a CPU reference
- If you write custom CUDA kernels, build a test comparing gradients to a CPU implementation.
- Use this repo's GPU-vs-CPU gradient check (about 1e-6) as a reference level.
Try It
# Paste into Claude or ChatGPT (simplified version of the repo's method: a code-review prompt)
Below is LLM SFT training code. Please check the following.
1. Is loss applied only to response tokens? Are prompt and padding targets set to -1 (ignored)?
2. Is the instruction template used in training identical to the one used at inference?
3. Does loss cover the end token (</s>) so the model learns to stop on its own?
If there are problems, show line numbers and fixes.
{{SFT training code}}Terminology
Related Papers
Quadrillion-parameter transformers and how to train them
On fixed data, much bigger models with scaled weight decay and ensembling keep loss falling as compute grows.
Astra can read and write weights to improve Qwen
An LLM edited Qwen3.5-0.8B's weights directly, lifting MATH-500 from 7.9% to 48.1%.
Show HN: Qwen3-4B with the AI writing tics trained out
Distilling Qwen3-4B with LoRA cut its AI-writing slop score from 121 to 12.5 per 1000 words
Dust: Pretraining Transformers Without Backpropagation
Adding per-token noise to activations lets transformers pretrain without backpropagation, rivaling backprop.
Show HN: TurboGPT: train 22KiB transformer in 13s
A CUDA C++ project trains a tiny byte-level GPT in under a minute.
I had Gemini train its own replacement for $9
Fine-tuning GLiNER on $9 of Gemini labels reached 0.83 F1 locally.
Learning to solve hard problems in RL for LLMs by never giving up
Cutting compute on easy problems and retrying hard ones in RL raised accuracy on the hardest problems
Training a 3.8B LLM to 0.384 CORE for $998
A 3.8B LLM trained on 8 B200s in 43 hours for $998 scored 0.384 on CORE.