总结与展望(第二章)
本章系统介绍了LLM训练和推理的核心基础知识:
核心知识点回顾
latex
┌─────────────────────────────────────────────────────────────────┐
│ LLM Fundamentals Summary │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 1. Transformer 架构 │
│ - Self-Attention: O(n²)计算复杂度 │
│ - Multi-Head: 并行多子空间注意力 │
│ - FFN: 2/3参数量,主要计算来源 │
│ - Positional Encoding: RoPE现代主流 │
│ │
│ 2. 训练流程 │
- Pre-training: 无监督学习通用知识 │
│ - SFT: 监督学习指令遵循 │
│ - RLHF/DPO: 人类偏好对齐 │
│ │
│ 3. 训练优化 │
│ - 混合精度: 50%内存,1.5-3×加速 │
│ - ZeRO: 数据并行优化,ZeRO-3可节省N×内存 │
│ - 梯度检查点: 50%内存换20%计算 │
│ │
│ 4. 推理阶段 │
│ - Prefill: 计算密集型,并行处理 │
│ - Decode: 内存密集型,逐个生成 │
│ │
│ 5. KV Cache │
│ - 避免重复计算,O(n²) → O(n) │
│ - 7B模型4K序列约2GB │
│ - 分页管理解决内存碎片 │
│ │
│ 6. 推理优化 │
│ - 量化: W4A16可压缩75%,损失2-3% │
│ - 剪枝: 2:4稀疏可加速1.5-1.8× │
│ - 蒸馏: 10×压缩,保持80-85%性能 │
│ │
│ 7. 批处理 │
│ - Continuous Batching: 5-10× 吞吐量提升 │
│ - 分页KV Cache: 高效内存管理 │
│ │
└─────────────────────────────────────────────────────────────────┘关键性能数字
| 指标 | 典型值 | 说明 |
|---|---|---|
| 7B模型Prefill (1K tokens) | 50-100ms | A100 |
| 7B模型Decode (BS=1) | 50-100ms/token | A100 |
| KV Cache (7B, 4K) | 2GB | FP16 |
| 混合精度加速 | 1.5-3× | Tensor Core |
| ZeRO-3内存节省 | N× | N为GPU数 |
| W4A16量化压缩 | 75% | 2-3%精度损失 |
| Continuous Batching提升 | 5-10× | vs 无批处理 |
进一步学习方向
- 分布式训练:TP(Tensor Parallelism)、PP(Pipeline Parallelism)、SP(Sequence Parallelism)
- 长序列优化:Ring Attention、Longformer、Flash Attention
- 推理系统:vLLM、TensorRT-LLM、Text Generation Inference
- 模型架构创新:Mamba、RWKV、RetNet等非Transformer架构
参考资料
- Vaswani et al. "Attention Is All You Need". NeurIPS 2017.
- Brown et al. "Language Models are Few-Shot Learners". NeurIPS 2020.
- Ouyang et al. "Training language models to follow instructions with human feedback".
NeurIPS 2022. - Rafailov et al. "Direct Preference Optimization". NeurIPS 2023.
- Rajbhandari et al. "ZeRO: Memory Optimizations Toward Training Trillion Parameter
Models". SC 2020. - Frantar et al. "GPTQ: Accurate Post-Training Quantization for Generative Pre-trained
Transformers". ICLR 2023. - Lin et al. "AWQ: Activation-aware Weight Quantization for LLM Compression and
Acceleration". MLSys 2024. - Kwon et al. "Efficient Memory Management for Large Language Model Serving with
PagedAttention". SOSP 2023.
本章由Mooncake团队编写,供实习生学习使用。如有疑问,请联系团队导师。