Megatron-LM README

The project's README from github.com/NVIDIA/Megatron-LM. As one markdown file: README.md.

Megatron-LM and Megatron Core

GPU-optimized library for training transformer models at scale

Documentation version license

About

This repository contains two components: Megatron-LM and Megatron Core.

Megatron-LM is a reference example that includes Megatron Core plus pre-configured training scripts, ideal for research teams, learning distributed training, and quick experimentation.

Megatron Core is a composable library with GPU-optimized building blocks for custom training frameworks. It provides transformer building blocks, advanced parallelism strategies (TP, PP, DP, EP, and CP), mixed precision support (FP16, BF16, FP8, and FP4), and model architectures, ideal for framework developers and ML engineers building custom training pipelines.

Megatron Bridge provides bidirectional Hugging Face ↔ Megatron checkpoint conversion with production-ready recipes.

Getting Started

Install from PyPI:

uv pip install megatron-core

Or clone and install from source:

git clone https://github.com/NVIDIA/Megatron-LM.git
cd Megatron-LM
uv pip install -e .

Note: Building from source can use a lot of memory. If the build runs out of memory, limit parallel compilation jobs by setting MAX_JOBS (for example, MAX_JOBS=4 uv pip install -e .).

For NVIDIA GPU Cloud (NGC) container setup and all installation options, review the Installation Guide.

Latest News

Previous News

Project Structure

Megatron-LM/
├── megatron/
│   ├── core/                    # Megatron Core (kernels, parallelism, building blocks)
│   │   ├── models/              # Transformer models
│   │   ├── transformer/         # Transformer building blocks
│   │   ├── tensor_parallel/     # Tensor parallelism
│   │   ├── pipeline_parallel/   # Pipeline parallelism
│   │   ├── distributed/         # Distributed training (FSDP, DDP)
│   │   ├── optimizer/           # Optimizers
│   │   ├── datasets/            # Dataset loaders
│   │   ├── inference/           # Inference engines and server
│   │   └── export/              # Model export (example: TensorRT-LLM)
│   ├── training/                # Training scripts
│   ├── legacy/                  # Legacy components
│   ├── post_training/           # Post-training (quantization, distillation, pruning, etc.)
│   └── rl/                      # Reinforcement learning (including RLHF)
├── examples/                    # Ready-to-use training examples
├── tools/                       # Utility tools
├── tests/                       # Comprehensive test suite
└── docs/                        # Documentation

Performance Benchmarking

For the latest performance benchmarking results, refer to NVIDIA Megatron Bridge Performance Summary.

The codebase efficiently trains models from 2B to 462B parameters across thousands of GPUs, achieving up to 47% Model FLOP Utilization (MFU) on H100 clusters.

Model table

Benchmark Configuration:

  • Vocabulary size: 131,072 tokens
  • Sequence length: 4,096 tokens
  • Model scaling: Varied hidden size, attention heads, and layers to achieve target parameter counts
  • Communication optimizations: Fine-grained overlapping with DP (--overlap-grad-reduce, --overlap-param-gather), TP (--tp-comm-overlap), and PP (enabled by default)

Key Results:

  • 6,144 H100 GPUs: Successfully benchmarked 462B parameter model training.
  • Superlinear scaling: MFU increases from 41% to 47–48% with model size.
  • End-to-end measurement: Throughputs include all operations (data loading, optimizer steps, communication, and logging).
  • Production ready: Full training pipeline with checkpointing and fault tolerance.
  • Note: Performance results measured without training to convergence

Weak Scaling Results

The weak scaled results show superlinear scaling (MFU increases from 41% for the smallest model considered to 47–48% for the largest models); this is because larger GEMMs have higher arithmetic intensity and are consequently more efficient to execute.

Weak scaling

Strong Scaling Results

This test strong scales the standard GPT-3 model (slightly more than 175 billion parameters due to larger vocabulary size) from 96 H100 GPUs to 4,608 GPUs, using the same batch size of 1,152 sequences throughout. Communication becomes more exposed at larger scale, leading to a reduction in MFU from 47% to 42%.

Strong scaling

Roadmaps

Resources

Getting Help

  • 📖 Documentation - Official guides and API reference
  • 🐛 Issues - Bug reports and feature requests

Contributing

Contributions are welcome. Ways to contribute:

  • 🐛 Report bugs - Help improve reliability
  • 💡 Suggest features - Shape the future of Megatron Core
  • 📝 Improve docs - Make Megatron Core more accessible
  • 🔧 Submit PRs - Contribute code improvements

→ Contributing Guide

Citation

If you use Megatron in your research or project, use the following citation:

@article{megatron-lm,
  title={Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism},
  author={Shoeybi, Mohammad and Patwary, Mostofa and Puri, Raul and LeGresley, Patrick and Casper, Jared and Catanzaro, Bryan},
  journal={arXiv preprint arXiv:1909.08053},
  year={2019}
}

Megatron-LM (NVIDIA/Megatron-LM)

Searching Database…

这是什么东西?NVIDIA 的开源工具包,用于大规模训练大型 transformer(大语言模型)。它包含两部分:Megatron-LM 是一套带现成训练脚本的参考配置;Megatron Core 是一个可组合的库,提供针对 GPU 优化的构建模块,可用于跨数千块 GPU 训练模型。
它是一个库,还是由多个组件组成?一个仓库里包含两个组件:Megatron-LM 是带预配置训练脚本的参考示例,而 Megatron Core 是一个可组合的库,用来搭建自定义训练框架。另有一个独立工具 Megatron Bridge,可以在 Hugging Face 和 Megatron 两种格式之间转换 checkpoint。
怎么安装?可以从 PyPI 安装,或者克隆仓库后从源码安装。源码构建很吃内存,如果编译时内存不够,可以限制并行任务数,比如 MAXJOBS=4 uv pip install -e . 。剩下的安装方式可以参考 NVIDIA 的 NGC 容器和《Installation Guide》,之后还有《Your First Training Run》指南带你上手。
在版本或兼容性方面有什么需要留意的坑吗?对 Python 3.10 的支持即将取消:即将发布的 0.17.0 版本要求 Python 3.12 或更高,所以依赖它的下游项目得提高自己的最低版本要求。在配置一般的机器上从源码构建也可能把内存耗尽。
这是给谁用的——研究人员还是框架开发者?两类人都有,按组件区分:想学习分布式训练、跑快速实验的研究团队用现成的 Megatron-LM 脚本;搭建自定义训练流水线的框架开发者和机器学习工程师则用 Megatron Core 的构建模块。
必须用 NVIDIA 的硬件吗,还是任意 GPU 都行?它只围绕 NVIDIA GPU 构建和做基准测试——公开的测试结果来自 H100 集群,训练 4620 亿参数的模型用到了多达 6,144 块 GPU。最大规模的运行假设的是多节点集群,而不是单台机器。
它做基准测试的模型规模有多大?公开的基准测试在多达 6,144 块 H100 GPU 的集群上训练 2B 到 462B 参数的模型,高端场景下模型 FLOP 利用率达到 47-48%,测得的吞吐量还包含了 checkpoint 保存和容错等生产级功能的开销。
它是怎么把训练任务拆分到几千块 GPU 上的?靠五种并行策略:张量并行、流水线并行、数据并行、专家并行和上下文并行(TP、PP、DP、EP、CP),再结合 FP16、BF16、FP8 和 FP4 混合精度。动态上下文并行(Dynamic Context Parallelism)在变长序列训练中最多可带来 1.48 倍加速。
能用 Hugging Face 的 checkpoint 吗?可以。Megatron Bridge 能在 Hugging Face 和 Megatron 格式之间双向转换 checkpoint,并为热门模型提供开箱即用的生产级配方,包括 DeepSeek-V4 的转换、推理和预训练配方。
支持哪些比较新的模型?近期版本加入了 DeepSeek-V4 的初步支持,针对 DeepSeek-V3、Mixtral 和 Qwen3 的优化 MoE 配方,采用 BitNet 三值量化的 Falcon-H1 混合 transformer-Mamba 架构,以及 YaRN RoPE 缩放和 attention sinks 等 GPT-OSS 特性。
What are similar projects?TensorRT-LLM, halo, DMI, LittleBit, verl, Meshy, Intern-Decision, airllm
Creator
NVIDIA
Size
154.9 MB
Stars
18.2K
Age
7y
Commits · 90 Days