File: README.md
Megatron-LM and Megatron Core
GPU-optimized library for training transformer models at scale
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-coreOr 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.
- Your First Training Run - End-to-end training examples with data preparation
- Parallelism Strategies - Scale training across GPUs with TP, PP, DP, EP, and CP
- Contribution Guide - How to contribute to Megatron Core
Latest News
- [2026/05] DeepSeek-V4 initial support - Megatron Core's
devbranch includes the initial DeepSeek-V4 implementation; Megatron Bridge provides conversion, inference, and pretraining recipes. - [2026/04] Advancing Emerging Optimizers for Accelerated LLM Training with NVIDIA Megatron - Muon and other emerging optimizers are now supported in Megatron Core via the new Emerging-Optimizers library.
- [2026/03] Scalable Training of Mixture-of-Experts Models with Megatron Core - Technical report on scaling MoE training with integrated optimizations for memory, communication, and computation.
- [2026/03] Implementing Falcon-H1 Hybrid Architecture in Megatron Core - Technology Innovation Institute (TII) contributes Falcon-H1 hybrid transformer-Mamba architecture and BitNet ternary quantization support to Megatron Core.
- [2026/03] Megatron Core Roadmap - Roadmap for upcoming Megatron Core features and improvements.
- [2026/03] Deprecating Python 3.10 support: The upcoming 0.17.0 release drops Python 3.10 support. Downstream applications must raise their lower boundary to 3.12 to stay compatible with Megatron Core.
- [2026/01] Dynamic Context Parallelism - Up to 1.48x speedup for variable-length sequence training with adaptive CP sizing.
- [2025/12] Megatron Core development has moved to GitHub. All development and CI now happen in the open, and community contributions are welcome.
- [2025/10] Megatron Dev Branch - Early access branch with experimental features.
- [2025/10] Megatron Bridge - Bidirectional converter for interoperability between Hugging Face and Megatron checkpoints, featuring production-ready recipes for popular models.
- [2025/08] Mixture of Experts (MoE) Q3โQ4 2025 Roadmap - Comprehensive roadmap for MoE features including DeepSeek-V3, Qwen3, advanced parallelism strategies, FP8 optimizations, and Blackwell performance enhancements.
- [2025/08] GPT-OSS Model - Megatron Core integrates advanced features including YaRN RoPE scaling, attention sinks, and custom activation functions.
- [2025/06] Megatron MoE Model Zoo - Best practices and optimized configurations for training DeepSeek-V3, Mixtral, and Qwen3 MoE models with performance benchmarking and checkpoint conversion tools.
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/ # DocumentationPerformance 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.
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.
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%.
Roadmaps
- 2026 Q2 Roadmap
- 2026 Q2 MoE-Specific Roadmap [
devbranch first developments]
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
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}
}File: docs/advanced/index.md
Discussions
In-depth technical discussions and optimization guides:
- Optimizing DeepSeek-V3 Training on GB200 NVL72 - Achieving 970 TFLOPS/GPU with MXFP8, kernel optimizations, and HybridEP
File: docs/api-guide/core/dist_checkpointing.md
dist_checkpointing package
A library for saving and loading the distributed checkpoints.
A distributed checkpoint in Megatron Core uses the torch_dist format,
a custom checkpointing mechanism built on top of PyTorch's native
checkpointing capabilities.
A key property of distributed checkpoints is that a checkpoint saved under one
parallel configuration (tensor, pipeline, or data parallelism) can be loaded
under a different parallel configuration. This enables flexible scaling and
resharding of models across heterogeneous training setups.
Using the library requires defining sharded state_dict dictionaries with functions from mapping and optimizer modules.
Those state dicts can be saved or loaded with a serialization module using strategies from strategies module.
Safe Checkpoint Loading
Since PyTorch 2.6, the default behavior of torch.load is weights_only=True.
This ensures that only tensors and allow-listed classes are loaded, reducing the risk of arbitrary code execution.
If you encounter an error such as:
WeightsUnpickler error: Unsupported global: GLOBAL argparse.Namespace was not an allowed global by default.you can fix it by explicitly allow-listing the missing class in your script:
import torch, argparse
torch.serialization.add_safe_globals([argparse.Namespace])Checkpointing Distributed Optimizer
Checkpoint Compatibility and Optimizer State Formats
Beginning with mcore v0.14, the flattened_range attribute was removed from dist_checkpointing. As a result:
- Optimizer states saved with mcore versions <= 0.14 can no longer be loaded directly. Loading these legacy optimizer states is not supported because the required sharded metadata is no longer available. If you need to continue training from older checkpoints, refer to the workaround described below.
- Model weights from older checkpoints remain fully compatible. No extra steps are neededโmodel weights from checkpoints created by earlier versions load automatically; simply add the
--no-load-optimflag.
Workaround: Loading legacy optimizer states with ToT MCore
Step 1: Convert the legacy checkpoint using mcore v0.15.0
Run a dummy training job with mcore v0.15.0 to re-save the checkpoint with new optimizer states format.
MODEL_TRAIN_PARAMS=(
# Define model architecture and training parameters here
)
OLD_CKPT=/workspace/mcore_ckpt_old
CONVERTED_CKPT=/workspace/mcore_ckpt_0.15.0
torchrun --nproc_per_node=8 /opt/megatron-lm/pretrain_gpt.py \
--save-interval 1 \
--eval-interval 1 \
--exit-interval 1 \
--eval-iters 1 \
--use-distributed-optimizer \
--save ${CONVERTED_CKPT} \
--load ${OLD_CKPT} \
--ckpt-format torch_dist \
"${MODEL_TRAIN_PARAMS[@]}"Step 2: Load the converted checkpoint with ToT MCore
Use the converted checkpoint as the input for continued training with ToT MCore.
MODEL_TRAIN_PARAMS=(
# Define model architecture and training parameters here
)
NEW_CKPT=/workspace/mcore_ckpt_new
CONVERTED_CKPT=/workspace/mcore_ckpt_0.15.0
torchrun --nproc_per_node=8 /opt/megatron-lm/pretrain_gpt.py \
--use-distributed-optimizer \
--save ${NEW_CKPT} \
--load ${CONVERTED_CKPT} \
--ckpt-format torch_dist \
"${MODEL_TRAIN_PARAMS[@]}"After this step, training can proceed normally using ToT MCore with fully supported optimizer state loading.
Distributed Optimizer Checkpoint Formats
The refactor of the Distributed Optimizer introduces two checkpoint formats:
- dp_reshardable (Default)
- Fast save/load performance.
- Not reshardable โ not possible to change model parallelism when using this format.
- Recommended for general training when model parallelism changes are not needed.
- fully_reshardable
- Fully reshardable โ supports arbitrary changes in model parallelism.
- Slower than dp_reshardable.
- Enabled via the
--dist-ckpt-optim-fully-reshardableflag.
Workflow for Changing Model Parallelism
You can combine formats to optimize both flexibility and performance:
Train using
dp_reshardable(default) for faster checkpointing.When you need to change model parallelism:
- Stop training.
- Change model parallelism for train config.
- Resume training with
--dist-ckpt-optim-fully-reshardable.
Save at least one checkpoint under the new model parallel configuration.
(Optional) To continue the training with updated model parallelism and better checkpointing performance, stop training and switch back to
dp_reshardableformat by removing--dist-ckpt-optim-fully-reshardable.
Async Checkpoint Saving Strategy
The framework supports asynchronous checkpoint saving to improve training performance by offloading I/O operations.
We are transitioning to a new async saving implementation based on the NVRx (NVIDIA Resiliency Extension) package. The legacy async strategy (referred to as mcore) is being deprecated.
Migration to NVRx
- The NVRx-based async strategy will become the default in mcore v0.17.
- The existing mcore async strategy is deprecated and will be removed in future versions.
- A deprecation warning is emitted when using the mcore strategy.
Selecting Async Strategy
--async-strategy flag is introduced to control the async strategy. To use legacy async strategy (mcore), set:
--async-strategy mcoreNVRx Dependency
To use the NVRx-based async strategy, you must install the nvidia-resiliency-ext package.
git clone https://github.com/NVIDIA/nvidia-resiliency-ext
cd nvidia-resiliency-ext
pip install .NOTE
- If
nvidia-resiliency-extis not installed, the NVRx async strategy will not be available. - The
mcorestrategy will remain temporarily to ensure a smooth transition but will be removed in future releases. - It is strongly recommended to migrate to the NVRx strategy as soon as possible.
Async Saving for `fsdp_dtensor` and `torch_dcp` checkpoints
Starting from mcore v0.17, asynchronous checkpoint saving is supported for fsdp_dtensor and torch_dcp formats.
Note that async saving for these formats requires the nvidia-resiliency-ext package. As a result, the only supported async_strategy in this context is nvrx.
Subpackages
:maxdepth: 4
dist_checkpointing.strategiesFile: docs/api-guide/core/dist_checkpointing.strategies.md
dist_checkpointing.strategies package
Package defining different checkpoint formats (backends) and saving/loading algorithms (strategies).
Strategies can be used for implementing new checkpoint formats or implementing new (more optimal for a given use case) ways of saving/loading of existing formats.
Strategies are passed to dist_checkpointing.load and dist_checkpointing.save functions and control the actual saving/loading procedure.
File: docs/api-guide/core/distributed.md
distributed package
This package contains various utilities to finalize model weight gradients
on each rank before the optimizer step. This includes a distributed data
parallelism wrapper to all-reduce or reduce-scatter the gradients across
data-parallel replicas, and a finalize_model_grads method to
synchronize gradients across different parallelism modes (e.g., 'tied'
layers on different pipeline stages, or gradients for experts in a MoE on
different ranks due to expert parallelism).
File: docs/api-guide/core/fusions.md
fusions package
This package provides modules that provide commonly fused
operations. Fusing operations improves compute efficiency by
increasing the amount of work done each time a tensor is read from
memory. To perform the fusion, modules in this either rely on PyTorch
functionality for doing just-in-time compilation
(i.e. torch.jit.script in older PyTorch versions of torch.compile
in recent versions), or call into custom kernels in external libraries
such as Apex or TransformerEngine.
File: docs/api-guide/core/index.md
Core APIs
Low-level API reference for core Megatron components.
:maxdepth: 2
transformer
tensor_parallel
pipeline_parallel
fusions
distributed
datasets
dist_checkpointing
dist_checkpointing.strategiesFile: docs/api-guide/core/pipeline_parallel.md
pipeline_parallel package
This package contains implementations for two different pipeline parallelism
schedules (one without interleaving and one with interleaving, see Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM
for details), and a default no-pipelining schedule. It also contains methods
for the point-to-point communication that is needed between pipeline stages.
File: docs/api-guide/core/tensor_parallel.md
tensor_parallel package
This package contains an implementation for tensor parallelism in transformer
models (see Megatron-LM: Training Multi-Billion Parameter Language Models
Using Model Parallelism and Reducing
Activation Recomputation in Large Transformer Models
for details).