Create

Sign in to ReadmeX

or

How we handle your data: Privacy Policy

ReadmeX

Your AI reading companion

What would you like to catch up on?

Pick a briefing, or ask your own question.

Your community and people briefings stay personal to you.

Story

Hands-On Tutorial Trains a 7.5M-Parameter GPT From Scratch

AI summary

A tutorial demonstrates how to train a small GPT from scratch in several hundred lines of code. It covers training a BPE tokenizer, pretraining a 4-layer model with about 7.5 million parameters, monitoring memory use and gradient accumulation, generating text, and supervised fine-tuning. In the author’s demonstration, loss fell from 6.23 to near zero, and the model learned to reproduce the toy corpus. After 300 SFT steps using 12 instruction examples, it responded in a dialogue format and stopped at an end token. The report notes that the model only answered content covered by its training data and produced incoherent responses to out-of-scope questions.

In-depth insight

The report says the tutorial’s workflow has low computational requirements and that training the 7.5M-parameter model can be completed on an ordinary development machine without large-scale computing resources.

上一篇我们学习了偏好对齐的两条路线:DPO 跳过奖励模型和强化学习,直接用偏好数据对模型做监督式训练;GRPO 则用组内相对优势替代价值网络,让强化学习变得更轻。至此,从数据清洗、分词器训练、目标函数、反向传播与优化器、显存与混合精度、分布式并行,到预训练工程、SFT、RLHF 与 DPO,大模型训练的完整链路我们已经逐个拆开看过了一遍。

不过,光看不练总归隔了一层。今天是本系列的收官篇,我们不再拆新概念,而是亲自动手,用几百行代码从零训练一个小模型,把前面十一篇讲过的每个环节都跑一遍。跑完之后你会看到,loss 从接近随机猜的水平断崖式下降,模型的输出从乱码变成整段复述语料,再喂给它十几条指令数据,它就能回答你的问题了。

标杆项目

自己从零训模型这件事其实并不新鲜,社区里已经有好几个公认的标杆:

  • Karpathy 的系列项目:nanoGPT 号称用最精简的代码复现 GPT 的训练,核心只有 model.py 和 train.py 两个文件,在小莎士比亚数据集上就能训出一个会写莎士比亚风格台词的字符级 GPT;build-nanogpt 手把手带着复现 GPT-2 的完整训练流程;2025 年 10 月发布的 nanochat 更进一步,把分词器、预训练、SFT 到 GRPO 强化学习的完整流程写进一个约八千行的代码库,8 张 H100 跑 4 小时、花大约 100 美元,就能训出一个 561M 参数的聊天模型,和我们今天的路线图几乎一样,只是规模放大了几个量级
  • minimind:中文社区的这个仓库把门槛压到了最低,号称用普通显卡几个小时就能从零训出一个几十 M 参数、会简单对话的小模型,配套教程非常完整
  • Happy-LLM:Datawhale 出品的系统教程,从 Transformer 原理讲起,手写 LLaMA2,再把预训练、SFT、GRPO 一路走完
  • train-llm-from-scratch:覆盖最全的个人项目,从原始文本到预训练、SFT、奖励模型,再到 PPO、DPO、GRPO,全部手写,不依赖 transformers 和 trl
  • LLMs-from-scratch:Sebastian Raschka《从零构建大模型》的配套代码,从分词到指令微调逐章推进,有中译本,适合喜欢跟书学的同学

#MiniGPT#Hugging Face tokenizers#PyTorch#TRL#nanoGPT#nanochat#MiniMind#Happy-LLM

Original · 21 min readRead the original
Group chat

No comments yet. Start the conversation.