Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LLM 从基础到前沿 — 大语言模型 80 章

Python NumPy

简介

大语言模型 (LLM) 系统性课程,从 BPE Tokenization 到 GPT-4 架构、从 Scaling Laws 到 o1 推理时计算。80 章 = 5 部 × 16 章,每章一个独立可运行的 Python 文件 + 中文注释。

适合:

  • 想深入理解 LLM 内部原理的学习者
  • ML/NLP 方向的研究生和工程师
  • 面试准备 & 系统化知识构建

🎓 80 章 5 Parts 全集

Part 仓库 章节 状态
I 基础与架构 LLMBasic (本仓库) 01-16
II 训练与推理 LLMTrainingAndInference 17-32
III 对齐与后训练 LLMAlignment 33-48
IV 高级能力 LLMAdvance 49-64
V 前沿与应用 LLMFrontier 65-80

仓库结构

llm/
├── Hello.py                                    # 入口
│
╔══ Part I: 基础与架构 (01-16) ✓ ═══════════════════════════════════════╗
║                                                                         ║
├── 01_nlp_history/            NLP 历史与语言模型演进
├── 02_tokenization/           BPE — 子词分词算法原理与实现
├── 03_tokenization_advanced/  词表设计 · 多语言 · 压缩率 · Token预算
├── 04_word_embeddings/        Word2Vec · GloVe · FastText · 词类比
├── 05_contextual_embeddings/  ELMo → BERT · 上下文 vs 静态嵌入
├── 06_self_attention/         Q·Kᵀ/√d_k → softmax → V (纯 NumPy)
├── 07_multi_head_attention/   多头注意力 · 头多样性 · Cross-Attention
├── 08_transformer_encoder/    Encoder 块 · Pre-LN · 堆叠
├── 09_transformer_decoder/    因果掩码 · 自回归生成 · GPT 架构
├── 10_positional_encoding/    Sinusoidal · RoPE · ALiBi · NoPE
├── 11_normalization/          BatchNorm · LayerNorm · RMSNorm · DeepNorm
├── 12_activations_ffn/        ReLU → GELU → Swish → SwiGLU · 门控FFN
├── 13_pretraining_objectives/ CLM (GPT) vs MLM (BERT) vs T5 Span
├── 14_scaling_laws/           Kaplan · Chinchilla · 过度训练
├── 15_distributed_training/   DP · TP · PP · ZeRO · FSDP · Flash Attn
├── 16_moe/                    MoE 架构 · Top-k 路由 · 负载均衡
║                                                                         ║
╚═════════════════════════════════════════════════════════════════════════╝

╔══ Part II: 训练与推理 (17-32) → [LLMTrainingAndInference](https://github.com/AgentZero2002/LLMTrainingAndInference)
╔══ Part III: 对齐与后训练 (33-48) → [LLMAlignment](https://github.com/AgentZero2002/LLMAlignment)
╔══ Part IV: 高级能力 (49-64) → [LLMAdvance](https://github.com/AgentZero2002/LLMAdvance)
╔══ Part V: 前沿与应用 (65-80) → [LLMFrontier](https://github.com/AgentZero2002/LLMFrontier)

快速开始

# 单章运行 (所有章节独立可运行)
python3 Hello.py
python3 01_nlp_history/language_model_evolution.py
python3 06_self_attention/self_attention.py
python3 09_transformer_decoder/transformer_decoder.py
python3 16_moe/mixture_of_experts.py

Part I 核心速查

章节 主题 核心概念
01 NLP 演进 n-gram → RNN → LSTM → Transformer → GPT
02 BPE Tokenization 字节级 BPE, 迭代合并, 训练+编码
03 Tokenizer 进阶 词表权衡, 多语言效率, Chat Template
04 词嵌入 CBOW/Skip-gram, Negative Sampling, 词类比
05 上下文嵌入 ELMo 双向LSTM, BERT MLM, 注意力可视化
06 Self-Attention Q·Kᵀ/√d_k, Softmax, 因果掩码, O(n²)
07 Multi-Head d_k=d/h, 头多样性, Cross-Attention
08 Encoder MHA+FFN+残差+LN, Pre-LN vs Post-LN
09 Decoder 因果掩码, 自回归生成, Encoder-Decoder vs Decoder-Only
10 位置编码 Sinusoidal, Learned, RoPE, ALiBi
11 归一化 LayerNorm, RMSNorm (LLaMA), DeepNorm
12 激活 & FFN GELU, SwiGLU, 门控FFN, 知识存储
13 预训练目标 CLM, MLM, T5 Span, UL2
14 Scaling Laws Kaplan α≈0.076, Chinchilla 修正, 过度训练
15 分布式训练 DP/TP/PP, ZeRO-3/FSDP, Flash Attention
16 MoE 稀疏激活, Top-k 路由, 负载均衡, Mixtral

设计原则

  • Python 3.8+: 标准科学计算栈 (NumPy)
  • 独立可运行: 每章一个 .py 文件,直接运行
  • 中文注释: 关键概念用中文详解
  • 内嵌演示: 不用 Jupyter,每个文件内置 main()

参考资料

  • Attention Is All You Need (Vaswani et al., 2017)
  • BERT (Devlin et al., 2019)
  • GPT-3 / Language Models are Few-Shot Learners (Brown et al., 2020)
  • Scaling Laws for Neural Language Models (Kaplan et al., 2020)
  • Training Compute-Optimal Large Language Models (Hoffmann et al., 2022)
  • RoPE: Rotary Position Embedding (Su et al., 2021)
  • Flash Attention (Dao et al., 2022)
  • Mixtral of Experts (Mistral AI, 2024)

构建说明

本仓库由 AI 协作完成:

  • 代码架构与实现: Claude (Anthropic)
  • 推理引擎: DeepSeek V4 Pro (1M 上下文)

许可

MIT License

About

THU大语言模型 Part I: 基础与架构 | Attention/Transformer/Encoder/Decoder/位置编码/FFN/预训练目标/ScalingLaws/MoE | 16章Python+NumPy

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages