百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

LLM-Step-by-Step

> AI 编程
开源

从头开始在 PyTorch 中构建一个小型 LLM - 一段受到 Stanford CS336 启发的逐步旅程

99 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

从头开始在 PyTorch 中构建一个小型 LLM - 一段受到 Stanford CS336 启发的逐步旅程

What worked, what didn't, and why — a small LLM, end-to-end

| Journey | CS336 (curriculum) | Issues |

Two things to read

JOURNEY.md

The writing. A chapter-by-chapter narrative of what I tried, what was slow, and what each round of optimization actually bought me. Each chapter maps to a commit. Most "build an LLM" tutorials show you the final code — this one shows the path.

If you only read one thing in this repo, read this.

cs336_basics/

The code. Clean, dependency-light implementations of every piece: BPE tokenizer, RMSNorm/RoPE/SwiGLU/MHA, AdamW, cosine LR schedule, gradient clipping, training loop, sampling.

Read alongside JOURNEY.md. Each module is short and meant to be read top-to-bottom.

Everything else in the repo (scripts/, tests/, fixtures, the assignment PDF) is scaffolding around those two things.


The curriculum is borrowed from Stanford's CS336 ("Language Models from Scratch").

About

  • ** Built from PyTorch primitives, not framework abstractions.** No torch.nn.Transformer, no HuggingFace, no transformers library, no pre-trained weights. Every layer, optimizer, and training step is written from scratch in this repo — autograd and tensor ops are the only things borrowed.
  • ** A step-by-step journey, not just code.** JOURNEY.md walks through what was tried, what was slow, and what each round of optimization actually bought — chapter by chapter, commit by commit. Most "build an LLM" repos hand you the final code. This one shows the path.

What's in here

Part Topic Code Journey chapter
I BPE tokenizer (train + encode/decode, parallel + incremental) cs336_basics/bpe.py, tokenizer.py Iterations 1–4
II Transformer model (RMSNorm, RoPE, SwiGLU, MHA, tied embeddings) cs336_basics/model.py Part II
III Training building blocks (cross-entropy, AdamW, cosine LR, grad clip) cs336_basics/optim.py, training.py, nn_utils.py Part III
IV Training loop (data loader, checkpointing, scripts/train.py) cs336_basics/data.py, scripts/ Part IV
V Text generation (temperature, top-k, top-p, EOS stopping) cs336_basics/decoding.py, scripts/generate.py Part V

Quickstart

bash
# Install (uses uv for env management)
pip install uv
uv sync

# Run the unit tests
uv run pytest

# Download TinyStories
mkdir -p data && cd data
wget https://huggingface.co/datasets/roneneldan/TinyStories/resolve/main/TinyStoriesV2-GPT4-train.txt
wget https://huggingface.co/datasets/roneneldan/TinyStories/resolve/main/TinyStoriesV2-GPT4-valid.txt
cd ..

# Train a BPE tokenizer, encode the corpus, train a small model, generate text
# (see JOURNEY.md for the full pipeline)
uv run scripts/encode_corpus.py --help
uv run scripts/train.py --help
uv run scripts/generate.py --help

Credits & honest scope

The curriculum, the spec, and the test fixtures all come from Stanford's CS336 ("Language Models from Scratch"), which generously publishes its materials online. All the implementations and the writing in JOURNEY.md are mine. If you're a current CS336 student, please respect your course's collaboration policy — this repo is a learning log, not a copy-paste solution.

The original assignment scaffolding (handout PDF, test adapters, submission script) lives under assignment/.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythoncs336llmpytorch

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类AI 编程
定价开源

> 相关工具

G
GitHub Copilot
GitHub 官方 AI 编程助手,覆盖补全、Chat 与 Agent 模式。
C
Cursor
AI 原生代码编辑器,对话改代码、多文件 Agent 与规则体系是其核心。
S
skills
Skills for Real Engineers. Straight from my .agents directory.