Chapter 13

源码导读:Megatron-Core / NeMo 边界

📌 commit 2ea3e0fNeMo 50 万行从何下手

NeMo 仓库巨大(约 50 万行),但只有少量"主干"代码是核心。本章给一份阅读路线,并把 NeMo / Megatron-Core / TransformerEngine 的边界标出来。

13.1仓库结构

NeMo/                               https://github.com/NVIDIA-NeMo/NeMo
├── nemo/
│   ├── collections/
│   │   ├── llm/                    ★ 2.x LLM 训练核心
│   │   │   ├── gpt/model.py
│   │   │   ├── recipes/             几十个 recipe
│   │   │   ├── peft/
│   │   │   └── ...
│   │   ├── nlp/                    1.x 兼容层(仍在维护)
│   │   │   ├── models/
│   │   │   ├── modules/
│   │   │   └── data/
│   │   ├── vlm/                    Vision-Language
│   │   ├── speechlm/               Speech LLM
│   │   ├── asr/ / tts/             ASR / TTS
│   │   ├── multimodal/             扩散
│   │   └── common/                  共享层(tokenizer / metrics)
│   ├── lightning/                  ★ PT-Lightning 适配
│   │   ├── pytorch/
│   │   ├── megatron_parallel.py
│   │   └── strategies/
│   ├── core/                       老 1.x 核心(Trainer 等)
│   ├── utils/
│   └── deploy/                     部署相关
├── examples/                       ★ 几百个示例
├── scripts/
│   ├── checkpoint_converters/       NeMo ↔ HF 转换
│   └── ...
├── tutorials/                       Jupyter notebook
└── tests/

13.2建议阅读顺序

#文件读什么
1nemo/collections/llm/recipes/llama3_8b.py典型 recipe 的写法
2nemo/collections/llm/gpt/model/base.pyGPTModel 怎么基于 MCore 构建
3nemo/lightning/megatron_parallel.pyLightning 怎么挂上 Megatron-Core
4nemo/lightning/strategies/megatron_strategy.py分布式策略
5nemo/collections/llm/peft/LoRA / DoRA / IA3 实现
6nemo/collections/llm/api.pytrain / finetune / pretrain 高层 API
7scripts/checkpoint_converters/convert_llama_nemo_to_hf.pyQKV 转换怎么写
8nemo/collections/nlp/data/language_modeling/megatron/preprocess_data_for_megatron.pyindexed dataset 生成

13.3NeMo / MCore 边界

谁负责什么
NeMorecipe / Lightning 集成 / aligner / 多模态 / 检查点格式 / 部署
Megatron-CoreColumnParallelLinear / RowParallelLinear / DistributedDataParallel / PP schedule / DistributedOptimizer / TransformerLayer / GPTModel
TransformerEngineFP8 GEMM / FlashAttention / RMSNorm / DotProductAttention
Apexfused optimizer / fused layer norm(部分被 TE 取代)

13.4关键 commit 时间线

时间变更
2019NeMo 项目首次开源(v0.x,主打 ASR/TTS)
2022v1.0:NeMo Megatron Collection 加入
2023-09NeMo Aligner 独立子项目
2024-03NeVA / SpeechLLM 多模态
2024-08v2.0 alpha:Lightning 2.0 + Python recipe
2024-11FP8 训练正式 GA
2025-01v2.2:Megatron-Core 0.11 + NeVA 多张图
2025-04v2.4(本书):Qwen3 / DeepSeek 支持

13.5对照其他框架

NeMoMegatron-LM 原版ColossalAITorchTitan
顶层 APILightning + recipe命令行Booster + Plugin原生 PyTorch
多模态★★★★★★(OpenSora)
分布式底座Megatron-Core原生自家DTensor
RLHFNeMo AlignerColossalChat
部署集成TRT-LLM / Triton / NIM
性能★★★★★★★★★★★★★★★★★★

13.6社区入口

13.7当前仓库(v2.8 语音版)真实源码树

本仓库现在以语音 / 语音 LLM 为主。ls nemo/

nemo/
├── collections/                ★ 五大 collection
│   ├── asr/                     Canary / Parakeet / Citrinet / Conformer-CTC
│   ├── tts/                     FastPitch / HiFi-GAN / VITS
│   ├── audio/                   audio-to-audio(去噪 / 增强 / 分离)
│   ├── speechlm2/               ★ SALM 多模态 LLM + Duplex
│   │   ├── models/               salm.py / salm_automodel.py:45 / duplex_*.py
│   │   ├── data/                 salm_dataset.py / datamodule.py
│   │   └── parts/                automodel_lora.py:27 / parallel.py /
│   │                              optim_setup.py / pretrained.py / encoder_chunking.py
│   └── common/                   tokenizer / 工具
│
├── core/                       ★ 神经类型系统 + 配置 + 优化器
│   ├── classes/                  modelPT.py(save_to/restore_from)
│   ├── config/                    hydra_runner.py
│   ├── connectors/                save_restore_connector.py
│   ├── neural_types/
│   └── optim/
│
├── lightning/                  ★ PyTorch Lightning 基础
│   ├── base.py                   Base LightningModule
│   ├── base_callback.py
│   ├── callback_group.py
│   └── one_logger_callback.py
│
├── utils/                      exp_manager.py / export_utils.py / megatron_utils.py
├── agents/voice_agent/         ★ 2026 新 Voice Agent
├── deploy/                      推理部署
└── export/                      ONNX / TRT 导出

13.8四份外部仓库分工速查

仓库负责
NVIDIA/NeMo(本仓库) 语音 / ASR / TTS / Speech LLM / Voice Agent
NVIDIA/NeMo-Framework 纯 LLM 预训练 / SFT / DPO / multimodal-VLM
NVIDIA/NeMo-RL 大规模 RL(GRPO / PPO / DPO)
NVIDIA/NeMo-Curator 数据去重 / quality filter / GPU-accelerated dataset 处理
NVIDIA/Megatron-Bridge Megatron ↔ HF / NeMo ↔ HF 互转
NVIDIA/Megatron-LM ★ 上述大部分 LLM 训练的底层算子

13.9Hydra 入口模式(所有训练脚本都长这样)

nemo/core/config/hydra_runner.py 提供的 @hydra_runner 装饰器是所有 NeMo 训练脚本的统一入口。看 examples/speechlm2/salm_train.py:28–56

from nemo.core.config import hydra_runner

@hydra_runner(config_path="conf", config_name="salm")
def train(cfg):
    trainer = Trainer(**resolve_trainer_cfg(cfg.trainer))
    model = SALM(cfg.model)         # 或 SALMAutomodel(cfg.model)
    datamodule = build_datamodule(cfg.data)
    trainer.fit(model, datamodule)

if __name__ == "__main__":
    train()

对应配置在 examples/speechlm2/conf/salm.yaml。CLI 覆盖:python salm_train.py trainer.devices=8 model.optim.lr=1e-4

13.10从代码风格看 CLAUDE.md(NeMo 风格指南)

nemo/CLAUDE.md 列了贡献规范,关键几条:

13.11这章你需要带走的