Top Related Projects
DeepSpeed is a deep learning optimization library that makes distributed training and inference easy, efficient, and effective.
🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.
An implementation of model parallel autoregressive transformers on GPUs, based on the Megatron and DeepSpeed libraries
Facebook AI Research Sequence-to-Sequence Toolkit written in Python.
TensorFlow code and pre-trained models for BERT
An open-source NLP research library, built on PyTorch.
Quick Overview
Megatron-LM is an open-source project by NVIDIA for training large language models efficiently on distributed GPU systems. It focuses on optimizing transformer-based models for scale, supporting various architectures like BERT, GPT, and T5. The library is designed to enable training of models with billions of parameters across multiple GPUs and nodes.
Pros
- Highly optimized for distributed training on NVIDIA GPUs
- Supports multiple model architectures (BERT, GPT, T5)
- Implements efficient parallelism techniques (data, model, and pipeline parallelism)
- Provides tools for efficient checkpointing and model loading
Cons
- Steep learning curve for users not familiar with distributed training
- Primarily focused on NVIDIA hardware, limiting its use on other platforms
- Requires significant computational resources for large-scale training
- Documentation can be sparse for some advanced features
Code Examples
- Initializing a GPT model:
import torch
from megatron import get_args
from megatron.model import GPTModel
args = get_args()
model = GPTModel(num_tokentypes=0, parallel_output=True)
- Setting up data parallel groups:
from megatron import mpu
mpu.initialize_model_parallel(model_parallel_size=4)
- Training loop with mixed precision:
from apex import amp
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
model, optimizer = amp.initialize(model, optimizer, opt_level="O2")
for batch in dataloader:
optimizer.zero_grad()
loss = model(batch)
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
optimizer.step()
Getting Started
To get started with Megatron-LM:
-
Clone the repository:
git clone https://github.com/NVIDIA/Megatron-LM.git cd Megatron-LM -
Install dependencies:
pip install -r requirements.txt -
Set up your dataset and configuration file.
-
Run training script:
python pretrain_gpt.py \ --model-parallel-size 2 \ --num-layers 24 \ --hidden-size 1024 \ --num-attention-heads 16 \ --batch-size 4 \ --seq-length 1024 \ --max-position-embeddings 1024 \ --train-iters 500000 \ --lr-decay-iters 320000 \ --save $CHECKPOINT_PATH \ --load $CHECKPOINT_PATH \ --data-path $DATA_PATH \ --vocab-file gpt2-vocab.json \ --merge-file gpt2-merges.txt \ --data-impl mmap \ --split 949,50,1 \ --distributed-backend nccl \ --lr 0.00015 \ --lr-decay-style cosine \ --min-lr 1.0e-5 \ --weight-decay 1e-2 \ --clip-grad 1.0 \ --warmup .01 \ --checkpoint-activations \ --fp16
Competitor Comparisons
DeepSpeed is a deep learning optimization library that makes distributed training and inference easy, efficient, and effective.
Pros of DeepSpeed
- More flexible and adaptable to various deep learning frameworks
- Offers a wider range of optimization techniques beyond model parallelism
- Provides easier integration with existing codebases
Cons of DeepSpeed
- May require more setup and configuration for optimal performance
- Less specialized for transformer-based models compared to Megatron-LM
Code Comparison
Megatron-LM:
model = MegatronModule(
num_layers=args.num_layers,
hidden_size=args.hidden_size,
num_attention_heads=args.num_attention_heads,
vocab_size=args.vocab_size,
max_position_embeddings=args.max_position_embeddings,
)
DeepSpeed:
model = MyModel(args)
model_engine, optimizer, _, _ = deepspeed.initialize(
args=args,
model=model,
model_parameters=model.parameters()
)
DeepSpeed offers a more generalized approach, allowing users to define their own model architecture and then apply optimization techniques. Megatron-LM provides a more specialized implementation focused on transformer-based models, with built-in support for model parallelism.
Both libraries aim to improve training efficiency for large language models, but DeepSpeed offers a broader set of optimization techniques that can be applied to various deep learning tasks, while Megatron-LM is more focused on transformer architectures and model parallelism.
🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.
Pros of Transformers
- Extensive model support and easy-to-use API for various NLP tasks
- Active community and frequent updates with new models and features
- Seamless integration with popular deep learning frameworks like PyTorch and TensorFlow
Cons of Transformers
- Less optimized for large-scale distributed training compared to Megatron-LM
- May require more memory and computational resources for very large models
Code Comparison
Transformers:
from transformers import BertModel, BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
Megatron-LM:
import megatron
from megatron import get_args, get_tokenizer, get_model
args = get_args()
tokenizer = get_tokenizer()
model = get_model(args)
Summary
Transformers offers a user-friendly API with broad model support, making it ideal for various NLP tasks and experimentation. Megatron-LM, on the other hand, is optimized for large-scale distributed training of massive language models. While Transformers is more versatile and easier to use, Megatron-LM excels in scenarios requiring efficient training of extremely large models across multiple GPUs or nodes.
An implementation of model parallel autoregressive transformers on GPUs, based on the Megatron and DeepSpeed libraries
Pros of gpt-neox
- More user-friendly and easier to set up for newcomers
- Includes additional features like wandb integration and custom tokenizers
- Actively maintained with frequent updates and community contributions
Cons of gpt-neox
- May have slightly lower performance compared to Megatron-LM in some scenarios
- Less extensive documentation and fewer examples for advanced use cases
Code Comparison
Megatron-LM initialization:
model = MegatronModule(
init_method=init_method,
output_layer_init_method=scaled_init_method,
num_tokentypes=num_tokentypes,
parallel_output=parallel_output)
gpt-neox initialization:
model = GPTNeoX(
num_tokentypes=num_tokentypes,
parallel_output=parallel_output,
use_cache=use_cache,
config=config)
Both repositories provide powerful tools for training large language models, but gpt-neox offers a more accessible approach for newcomers and includes additional features. Megatron-LM, on the other hand, may offer slightly better performance and more extensive documentation for advanced users. The code comparison shows similarities in model initialization, with gpt-neox using a more streamlined approach.
Facebook AI Research Sequence-to-Sequence Toolkit written in Python.
Pros of fairseq
- Broader support for various NLP tasks and architectures
- More extensive documentation and examples
- Active community and frequent updates
Cons of fairseq
- Less optimized for large-scale language models
- May require more setup and configuration for specific tasks
Code Comparison
fairseq:
from fairseq.models.transformer import TransformerModel
model = TransformerModel.from_pretrained('/path/to/model')
tokens = model.encode('Hello world!')
output = model.decode(tokens)
Megatron-LM:
from megatron import get_args, get_tokenizer, get_model
from megatron.initialize import initialize_megatron
args = get_args()
tokenizer = get_tokenizer()
model = get_model(args)
tokens = tokenizer.tokenize('Hello world!')
output = model.generate(tokens)
The code snippets demonstrate that fairseq offers a more straightforward API for loading and using pre-trained models, while Megatron-LM requires more setup and initialization steps. However, Megatron-LM's approach allows for greater customization and optimization for large-scale language models.
TensorFlow code and pre-trained models for BERT
Pros of BERT
- Simpler architecture and easier to understand for beginners
- Extensive documentation and community support
- Widely adopted and used in various NLP tasks
Cons of BERT
- Limited scalability for very large language models
- Less efficient for distributed training on multiple GPUs
Code Comparison
BERT:
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
loss = outputs.loss
loss.backward()
Megatron-LM:
model = get_model(args)
output = model(tokens, labels, attention_mask)
loss = output['loss']
model.backward(loss)
Key Differences
- Megatron-LM is designed for training large language models with billions of parameters, while BERT is more suitable for smaller models.
- Megatron-LM offers advanced features for distributed training and model parallelism, which are not present in BERT.
- BERT provides pre-trained models and easy fine-tuning capabilities, making it more accessible for various NLP tasks.
- Megatron-LM focuses on performance and scalability, while BERT prioritizes ease of use and widespread adoption.
An open-source NLP research library, built on PyTorch.
Pros of AllenNLP
- More comprehensive and feature-rich NLP toolkit
- Easier to use for researchers and developers new to NLP
- Better documentation and tutorials
Cons of AllenNLP
- Less optimized for large-scale language model training
- May not scale as efficiently on multi-GPU systems
- Fewer options for advanced parallelism techniques
Code Comparison
AllenNLP:
from allennlp.data import DatasetReader, Instance
from allennlp.data.fields import TextField
from allennlp.data.token_indexers import SingleIdTokenIndexer
class MyDatasetReader(DatasetReader):
def _read(self, file_path: str) -> Iterable[Instance]:
with open(file_path, "r") as file:
for line in file:
yield self.text_to_instance(line.strip())
Megatron-LM:
from megatron import get_args
from megatron import print_rank_0
from megatron import get_tokenizer
from megatron.data.dataset_utils import build_train_valid_test_datasets
args = get_args()
tokenizer = get_tokenizer()
train_dataset, valid_dataset, test_dataset = build_train_valid_test_datasets(
data_prefix, data_impl, splits_string,
train_valid_test_num_samples,
seq_length, seed, skip_warmup)
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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-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.
- 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/ # 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.

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}
}
Top Related Projects
DeepSpeed is a deep learning optimization library that makes distributed training and inference easy, efficient, and effective.
🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.
An implementation of model parallel autoregressive transformers on GPUs, based on the Megatron and DeepSpeed libraries
Facebook AI Research Sequence-to-Sequence Toolkit written in Python.
TensorFlow code and pre-trained models for BERT
An open-source NLP research library, built on PyTorch.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot