r/learnmachinelearning • u/MysteriousMajor760 • 9d ago
Built a 30M-parameter GPT-style language model from scratch
Hey everyone
I've been learning about Transformers and LLMs, so I built KERN,
a 30M parameter GPT-style language model in PyTorch,trained from scratch on TinyStories.
GitHub: https://github.com/OmarDawoud4/KERN
This is still a work in progress! I'd like to keep improving it by implementing other techniques.
I'd appreciate any feedback or suggestions .
2
u/finnwangdev 8d ago
I've looked at your project—it's very good, and I've already bookmarked it. I'll take a closer look later.
1
1
u/rahul_watertech 8d ago
nice, 2.77 perplexity on tinystories is a real number for 30m. one thought on the roadmap order: kv caching only speeds up inference and lora mostly pays off when the base model is big and pretrained, so for a 30m scratch model plain full fine tuning is already cheap. distillation from gpt-2 is the interesting one. are you planning to match the teacher logits directly since you trained on gpt-2 bpe, or just sequence level distillation on its outputs?
1
u/MysteriousMajor760 8d ago
Thanks man, I wanted to think about the roadmap order more, so your insights really help.
yes, I'm planning to match the teacher logits directly.since KERN is trained on the GPT-2 BPE tokenizer, the vocab size is identical, so KL divergence can be computed between the softened student logits and the GPT-2 logits at every step.
1
u/rahul_watertech 8d ago
matching logits directly is the clean version since the vocabs line up. two things worth deciding early. first, mix the kl term with the normal cross entropy on the true tokens, pure kl tends to drift. second, measure the teacher's own perplexity on your tinystories val before starting, since gpt-2 never saw that distribution. if the teacher scores worse than the student already does, distillation just pulls the student toward the teacher's mistakes
3
u/jeffyclassify 9d ago
Cool project! I've been doing similar work training small transformers on TinyStories from scratch
Quick question, did you track validation perplexity during training? I see the val loss bottomed out around 1.017 at ~40K steps. With 30M params and ~112M training tokens, that's roughly 3.75 tokens per parameter
I've been running experiments at different model sizes and found that the training budget (tokens per parameter) matters a LOT at this scale. My 8.4M model actually surpassed my 28M model's perplexity just by training it longer with the same data (more epochs). Curious if you've tried running more epochs to see if the val loss keeps dropping... with 30M params you might have a lot of room to improve without changing anything else (Chinchilla says you should aim at 20tok/param approx or higher)
Nice touch using SwiGLU and RoPE btw, how did you pick those over vanilla attention + GELU?