LeetLLM
My PlanLearnGlossaryTracksPracticeBlog
LeetLLM

Your go-to resource for mastering AI & LLM systems.

Product

  • Learn
  • Glossary
  • Tracks
  • Practice
  • Blog
  • RSS

Legal

  • Terms of Service
  • Privacy Policy

© 2026 LeetLLM. All rights reserved.

All Topics
Your Progress
0%

0 of 177 articles completed

🛠️Computing Foundations0/9
Git, Shell, Linux for AIDocker for Reproducible AIPython for AI EngineeringNumPy and Tensor ShapesCUDA for ML TrainingMPS & Metal for ML on MacData Structures for AISQL and Data ModelingAlgorithms for ML Engineers
📊Math & Statistics0/8
Gradients and BackpropVectors, Matrices & TensorsLinear Algebra for MLAdam, Momentum, SchedulersProbability for Machine LearningStatistics and UncertaintyDistributions and SamplingHypothesis Tests, Intervals, and pass@k
📚Preparation & Prerequisites0/13
Neural Networks from ScratchCNNs from ScratchTraining & BackpropagationSoftmax, Cross-Entropy & OptimizationRNNs, LSTMs, GRUs, and Sequence ModelingAutoencoders and VAEsThe Transformer Architecture End-to-EndLanguage Modeling & Next TokensFrom GPT to Modern LLMsPrompt Engineering FundamentalsCalling LLM APIs in ProductionFirst AI App End-to-EndThe LLM Lifecycle
🧮ML Algorithms & Evaluation0/11
Linear Regression from ScratchLogistic Regression and MetricsDecision Trees, Forests, and BoostingReinforcement Learning BasicsValidation and LeakageClustering and PCACore Retrieval AlgorithmsDecoding AlgorithmsExperiment Design and A/B TestingPyTorch Training LoopsDataset Pipelines and Data Quality
📦Production ML Systems0/6
Feature Engineering for Production MLBatch and Streaming Feature PipelinesGradient Boosted Trees in ProductionRanking and Recommendation SystemsForecasting and Anomaly DetectionMonitoring Predictive Models
🧪Core LLM Foundations0/8
The Bitter Lesson & ComputeBPE, WordPiece, and SentencePieceStatic to Contextual EmbeddingsPerplexity & Model EvaluationFile Ingestion for AIChunking StrategiesLLM Benchmarks & LimitationsInstruction Tuning & Chat Templates
🧰Applied LLM Engineering0/24
Dimensionality Reduction for EmbeddingsCoT, ToT & Self-Consistency PromptingFunction Calling & Tool UseMCP & Tool Protocol StandardsContext EngineeringPrompt Injection DefenseResponsible AI GovernanceData Labeling and Human FeedbackEvaluating AI AgentsProduction RAG PipelinesHybrid Search: Dense + SparseReranking and Cross-Encoders for RAGRAG Evaluation for Reliable AnswersLLM-as-a-Judge EvaluationBias & Fairness in LLMsHallucination Detection & MitigationLLM Observability & MonitoringExperiment Tracking with MLflow and W&BPrompt Optimization with DSPyModel Versioning & DeploymentSemantic Caching & Cost OptimizationLLM Cost Engineering & Token EconomicsModel Gateways, Routing, and FallbacksDesign an Automated Support Agent
🎓Portfolio Capstones0/9
Capstone: Delivery ETA PredictionCapstone: Product RankingCapstone: Demand ForecastingCapstone: Image Damage ClassifierCapstone: Production ML PipelineCapstone: Document QACapstone: Eval DashboardCapstone: Fine-Tuned ClassifierCapstone: Reproducible ML Study
🧠Transformer Deep Dives0/8
Sentence Embeddings & Contrastive LossEmbedding Similarity & QuantizationScaled Dot-Product AttentionVision Transformers and Image EncodersPositional Encoding: RoPE & ALiBiLayer Normalization: Pre-LN vs Post-LNMechanistic InterpretabilityDecoding Strategies: Greedy to Nucleus
🧬Advanced Training & Adaptation0/16
Scaling Laws & Compute-Optimal TrainingPre-training Data at ScaleBuild GPT from Scratch LabJAX for PyTorch ResearchersContinued Pretraining for Domain ShiftSynthetic Data PipelinesSupervised Fine-Tuning PipelineMixed Precision TrainingDistributed Training: FSDP & ZeROLoRA & Parameter-Efficient TuningReward Modeling from Preference DataRLHF & DPO AlignmentConstitutional AI & Red TeamingRLVR & Verifiable RewardsKnowledge Distillation for LLMsModel Merging and Weight Interpolation
🤖Advanced Agents & Retrieval0/16
Vector DB Internals: HNSW & IVFAdvanced RAG: HyDE & Self-RAGGraphRAG & Knowledge GraphsRAG Security & Access ControlStructured Output GenerationReAct & Plan-and-ExecuteGuardrails & Safety FiltersCode Generation & SandboxingComputer-Use / GUI / Browser AgentsHuman-in-the-Loop Agent ArchitectureAI Coding Workflow with AgentsAgent Memory & PersistenceAgent Failure & RecoveryRecursive Language Models (RLM)Multi-Agent OrchestrationCapstone: Production Agent
⚡Inference & Production Scale0/19
Inference: TTFT, TPS & KV CacheMulti-Query & Grouped-Query AttentionKV Cache & PagedAttentionPrefix Caching and Prompt CachingFlashAttention & Memory EfficiencyContinuous Batching & SchedulingScaling LLM InferenceModel Parallelism for LLM InferenceModel Quantization: GPTQ, AWQ & GGUFLocal LLM DeploymentSLM Specialization & Edge DeploymentSpeculative DecodingLong Context Window ManagementMixture of Experts ArchitectureMamba & State Space ModelsReasoning & Test-Time ComputeAdvanced MLOps & DevOps for AIGPU Serving & AutoscalingA/B Testing for LLMs
🏗️System Design Capstones0/9
Content Moderation SystemCode Completion SystemMulti-Tenant LLM PlatformLLM-Powered Search EngineVision-Language Models & CLIPMultimodal LLM ArchitectureDiffusion Models: Images & TextReal-Time Voice AI AgentReasoning & Test-Time Compute
🎤AI Lab Interviewing0/4
AI Lab Coding Interview: Python SystemsAI Lab System Design InterviewAI Lab Behavioral InterviewAI Lab Technical Presentation
🔬Project Deep Dives0/17
Deep Dive - vLLMDeep Dive - SkyRLDeep Dive - FlashAttentionDeep Dive - FlashInferDeep Dive - DeepGEMMDeep Dive - NCCLDeep Dive - MegatronDeep Dive - DeepSpeedDeep Dive - RayDeep Dive - MLflowDeep Dive - PyTorchDeep Dive - TransformersDeep Dive - SGLangDeep Dive - slimeDeep Dive - DeepEPDeep Dive - TinkerDeep Dive - Light-PEFT
Back to Topics
LearnProject Deep DivesDeep Dive - Megatron
⚡HardFine-Tuning & Training

Deep Dive - Megatron

Read Megatron-LM and Megatron Core as a distributed training system: rank groups, parallel axes, optimizer sharding, MoE dispatch, low precision, and checkpoint operations.

23 min read
Learning path
Step 167 of 177 in the full curriculum
Deep Dive - NCCLDeep Dive - DeepSpeed

Personalize this lesson

Adapt explanations and teaching visuals to your background and preferred voice.

Megatron-LM is a reference training stack for transformer models that don't fit on one GPU. Megatron Core is its reusable library of parallel layers, process groups, schedules, optimizers, checkpoint utilities, and model definitions. NVIDIA's current repository calls the split explicit: Megatron-LM gives research teams runnable recipes, while Megatron Core gives framework authors composable building blocks.[1]Reference 1Megatron-LMhttps://github.com/NVIDIA/Megatron-LM

Treat a process rank as a coordinate rather than only rank=27. Its axes include data replica, context lane, pipeline stage, and tensor shard. Megatron creates communication groups from those coordinates, then every forward and backward operation chooses the smallest group that owns its data. Scale comes from factoring one world into several grids, not from making every GPU talk to every other GPU.

Why this project exists

Modern language models hit several limits at once. Parameters, gradients, optimizer state, and activations can exceed one GPU's memory. Long sequences can make attention intermediates too large even when weights fit, while a sparse mixture-of-experts (MoE) model stores many experts but activates only a few per token. Compute, memory bandwidth, network latency, and fault recovery all matter.

Megatron's answer is to split different dimensions for different reasons:

PressureAxis or mechanismWhat gets partitionedTypical communication
Wide matrix doesn't fitTensor parallelism (TP)A layer's weight and activation channelsAll-reduce or all-gather
Stack is too deepPipeline parallelism (PP)Contiguous layer rangesPoint-to-point activation send/receive
Batch needs throughputData parallelism (DP)Independent samples or replicasGradient reduce or reduce-scatter
Prompt is too longContext parallelism (CP)Sequence chunks and attention contextPeer-to-peer or collective KV exchange
MoE experts are spread outExpert parallelism (EP)Expert weights and routed tokensAll-to-all dispatch and combine
Activations fill memorySelective recomputationSaved intermediatesExtra forward compute, less storage

These axes compose, but they aren't interchangeable. TP lowers per-layer memory and keeps a single token on several GPUs. DP doesn't make one sequence faster; it admits more independent batches. PP can make a model fit across nodes, but it inserts stage-to-stage latency. CP reduces sequence-local memory and attention work, but each context group must exchange information. A good launch starts with a memory and topology budget, then picks axes that match it.

Two projects in one repository

Open a Megatron-LM clone and you'll see two layers.

Megatron Core

megatron/core/ contains the parts a framework can import: transformer modules, tensor-parallel layers, pipeline schedules, distributed data parallel buffers, optimizers, datasets, model-specific builders, inference utilities, and distributed checkpointing. megatron/core/parallel_state.py owns process-group construction. megatron/core/tensor_parallel/layers.py implements sharded linear and embedding layers. megatron/core/pipeline_parallel/schedules.py sequences microbatches across stages. megatron/core/distributed/param_and_grad_buffer.py manages contiguous gradient and parameter buffers that overlap communication with compute.

Core is an API surface, not one opinionated training command. Its configuration objects and process groups let a custom trainer choose a pipeline schedule, optimizer, precision recipe, checkpoint format, or model architecture while reusing tested kernels and collectives. The API surface is broad because each production model needs a slightly different mesh.

Megatron-LM reference scripts

Top-level training scripts and examples/ turn Core into repeatable experiments. pretrain_gpt.py and model providers configure tokenizers, data loaders, schedules, logging, checkpoint intervals, and command-line arguments. Tests cover rank-group construction, tensor-parallel layers, pipeline schedules, MoE dispatch, precision recipes, and distributed checkpoint round trips. The scripts are useful for reproducing papers and for learning what a full training run needs, but they aren't a drop-in serving API.

Megatron Bridge

Megatron Bridge lives in the companion NVIDIA-NeMo repository. It converts Hugging Face checkpoints to Megatron layouts and back, with model recipes and parallelism-aware conversion.[1]Reference 1Megatron-LMhttps://github.com/NVIDIA/Megatron-LM A training run can therefore start from an ecosystem checkpoint, use Megatron's sharded layout for throughput, then export a portable checkpoint for evaluation or serving. Conversion isn't a rename operation: tensor and pipeline shards must be mapped to global tensors, and MoE expert placement must be preserved.

Diagram showing Import path, Training and export, Hugging Face weights, and Megatron Bridge reshape + shard. Diagram showing Import path, Training and export, Hugging Face weights, and Megatron Bridge reshape + shard.
Import path, Training and export, Hugging Face weights, and Megatron Bridge reshape + shard.

The separation is a strength and a cost. Core can evolve without forcing every framework into one CLI, but users must align versions of Megatron Core, Transformer Engine, CUDA, Bridge, and checkpoint metadata. A source checkout can be ahead of the latest release, so record the commit or package version with each experiment.

Rank groups: factor world size first

Suppose a run has 64 GPUs and uses TP=4, PP=2, CP=2, and DP=4. The dense model grid is:

Ndense=TP×PP×CP×DP=4×2×2×4=64.N_{dense} = TP \times PP \times CP \times DP = 4 \times 2 \times 2 \times 4 = 64.Ndense​=TP×PP×CP×DP=4×2×2×4=64.

Each rank gets a tuple (dp, cp, pp, tp). Rank numbering is an implementation detail; group membership is the contract. Megatron's initialize_model_parallel builds rank generators from a configurable order such as tp-cp-ep-dp-pp. It checks that world size is divisible by the model-parallel product before creating groups.[2]Reference 2Parallelism Strategies Guide.https://docs.nvidia.com/megatron-core/developer-guide/latest/user-guide/parallelism-guide.html

The rank lattice visual makes this arithmetic concrete. Start at the 64-rank world, then factor into data replicas, sequence lanes, depth stages, and width shards. The rightmost TP group is usually placed inside one NVLink or NVSwitch domain because every transformer layer can use it. PP groups can cross nodes because they exchange boundary activations less often than TP exchanges partial layer results. DP groups can use the inter-node fabric when gradient traffic is overlapped with backward compute.

Nested Megatron rank groups show four DP replicas, each split into two CP lanes, two PP stages, and four TP cells; one highlighted cell is identified by its four-coordinate tuple. Nested Megatron rank groups show four DP replicas, each split into two CP lanes, two PP stages, and four TP cells; one highlighted cell is identified by its four-coordinate tuple.
Read the nested groups from outside in: DP replicas contain CP lanes, PP stages contain four TP cells, and the highlighted cell is addressed by its four-axis tuple.

EP is a second grid for MoE layers. In a dense run, DP commonly fills the remaining world after TP, PP, and CP. In an MoE run, the expert grid can use expert tensor parallelism (ETP), EP, expert data parallelism (EDP), and PP. Current Megatron Core also exposes generalized tensor-parallel rematerialization axes, so the complete world-size equation can gain more factors. Don't silently call every configuration "3D"; count the actual axes in the launch.

What each parallel axis does

Tensor parallelism: split width

TP cuts a matrix along a dimension that keeps local GEMMs large enough to use the GPU. For a column-parallel first MLP projection, A with shape [H, 4H] becomes four local matrices A_i with shape [H, H] when TP=4. Each rank computes X A_i, applies GeLU locally, and passes its activation shard to a row-parallel second projection. The second projection's rows line up with those local shards, so partial outputs can be summed once at the end.[3]Reference 3Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism.https://arxiv.org/abs/1909.08053

For a tiny worked example, let H=8, intermediate size be 16, and TP=4. Each rank stores [8,4] for the up projection and [4,8] for the down projection. Input X has shape [B,S,8]. Rank i computes Y_i = GeLU(X A_i) with shape [B,S,4], then Z_i = Y_i B_i with shape [B,S,8]. The output is Z = Σ_i Z_i. The GeLU never needs an all-gather because it acts independently on each channel.

Tensor-parallel MLP visual: one hidden vector fans into four column shards, each GeLU stays rank-local, matching row shards produce four partial outputs, and one all-reduce sums them back to the full hidden vector. Tensor-parallel MLP visual: one hidden vector fans into four column shards, each GeLU stays rank-local, matching row shards produce four partial outputs, and one all-reduce sums them back to the full hidden vector.
Trace matching indices across the four shard groups: GeLU stays beside each local `[B,S,4]` activation, then one all-reduce sums the four `[B,S,8]` partials.

Attention uses the same idea with heads. Query, key, and value projections are split across TP ranks, each rank computes attention for its head shard, and the output projection combines them. Head counts and hidden dimensions must divide cleanly by TP. Grouped-query attention can make key/value head divisibility the tighter constraint.

TP trades memory for communication. Each rank stores roughly 1/TP of a sharded weight, but every layer may issue all-reduce, all-gather, or reduce-scatter collectives. The exact count changes with sequence parallelism, overlap settings, and kernel path. Keep TP inside high-bandwidth links when possible.

Pipeline parallelism: split depth

PP assigns contiguous transformer layers to stages. In PP=2, stage 0 owns early layers and sends its hidden activation to stage 1, which owns later layers. During training, microbatches flow through a schedule such as 1F1B (one forward, one backward) so stages stay busy. Virtual pipeline stages interleave smaller layer chunks on each rank and reduce bubbles when layer counts and batch sizes permit.

PP lowers per-rank weight memory without all-reducing every layer across nodes. It adds activation send/receive latency and creates a bubble when not enough microbatches keep stages occupied. A topology with four TP ranks inside each node and two PP stages across nodes often beats a single eight-way TP group over a slower fabric.

Data parallelism: split samples

DP replicates model computation and gives each replica different samples. After backward, gradients from replicas are reduced. Standard DDP keeps full model state per rank. Megatron's distributed optimizer changes optimizer-state ownership while keeping DP replicas synchronized, which is useful when optimizer state, not weights, causes the memory wall.

Context and sequence parallelism: split token work

Sequence parallelism works alongside TP. It partitions sequence-dimension activation work in operations such as LayerNorm and dropout, often using reduce-scatter and all-gather variants to reduce activation memory. It doesn't mean each rank can run full attention with only its local tokens.

Context parallelism partitions the input sequence itself. Current Core docs describe each CP rank exchanging other sequence chunks because attention needs context outside its local slice.[2]Reference 2Parallelism Strategies Guide.https://docs.nvidia.com/megatron-core/developer-guide/latest/user-guide/parallelism-guide.html Communication can use point-to-point, all-gather, all-to-all, or hierarchical paths depending on model and hardware. CP is useful when long prompts or long training sequences dominate memory. CP duplicates weights across context peers, so the weight-gradient reduction must account for those replicas.

Expert parallelism: split sparse capacity

An MoE layer has a router that chooses top-k experts for each token. EP places different experts on different GPUs. The dispatcher permutes tokens, sends each token to its expert owners, runs grouped expert GEMMs, then sends outputs back and unpermutes them. The current dispatcher code exposes all-gather and all-to-all paths, plus Flex backends that can fuse or specialize communication.[4]Reference 4Mixture of Expertshttps://docs.nvidia.com/megatron-core/developer-guide/0.15.0/api-guide/moe.html

EP's risk is visible in its collective: an all-to-all can move every token to a different rank. Router skew produces hot experts, uneven work, padding, and queueing. Load-balancing loss, capacity policy, dropless dispatch, grouped GEMM, and overlapping communication with expert compute address different parts of that problem. They don't remove the need to inspect per-expert token counts.

MoE parallel folding

Traditional layouts often constrain EP to the data-parallel domain. That couples an attention-friendly grid to an MoE-friendly grid. High TP can help attention's wide matrices but hurt each small expert. High CP can help long-context attention while offering little value to per-token expert MLPs.

Megatron Core's MoE parallel folding decouples the two grids. Attention can use TP × CP × DP × PP, while MoE can use ETP × EP × EDP × PP. Folding can break the old EP ≤ DP ceiling, reduce minimum GPU counts when CP and EP share ranks, and keep expert communication inside a high-bandwidth island.[4]Reference 4Mixture of Expertshttps://docs.nvidia.com/megatron-core/developer-guide/0.15.0/api-guide/moe.html

This flexibility costs mental and operational complexity. Every group needs matching tensor shapes, routing metadata, optimizer ownership, and checkpoint keys. A topology that looks good on paper can fail if the launch order assigns experts across a congested link or if a checkpoint was saved with a different expert grid. Validate a tiny synthetic MoE run before scheduling a long pretraining job.

Distributed optimizer: trade replicated state for collectives

Adam-style training keeps model parameters, gradients, and first and second moments. With BF16 model weights and FP32 optimizer state, replicated optimizer memory can dominate. Megatron's distributed optimizer shards optimizer-owned state across DP ranks. The official guide lists a theoretical 18 bytes per parameter for a non-distributed BF16 setup versus 6 + 12/d bytes with distributed optimizer at DP size d.[5]Reference 5Distributed Optimizerhttps://docs.nvidia.com/megatron-core/developer-guide/latest/user-guide/features/dist_optimizer.html

The step is easier to reason about as a four-part loop:

  1. Backward fills a gradient buffer on each DP rank.
  2. reduce-scatter sums gradients and gives each rank its shard.
  3. Each rank updates its local FP32 master parameters and optimizer moments.
  4. all-gather rebuilds the BF16 parameter buffer for the next forward.

The parameter and gradient buffers are contiguous so communication can start as buckets become ready. Megatron can overlap gradient reduce-scatter with backward and parameter all-gather with forward when the configuration and backend support it. Overlap hides latency only when compute lasts long enough and the network isn't already saturated.

DP size dDistributed bytes per BF16 parameterWhat still costs memory
118Full optimizer and gradient state
212Half of optimizer-owned state per rank
49One quarter of sharded state plus model buffers
87.5Diminishing state savings, same activations

Those numbers are theoretical accounting, not a capacity guarantee. Activation buffers, temporary all-gather storage, communication buckets, embeddings, MoE routing buffers, and framework overhead still need headroom. If an optimizer shard is too small to hide communication, a larger DP value can lower throughput even while it saves memory.

Checkpoints, resharding, and recovery

Large runs checkpoint more than model weights. They need optimizer moments, random-number-generator state, data-loader position, iteration counters, and parallel-layout metadata. A checkpoint that can resume after a node failure is part of the training system, not a final export step.

Megatron's distributed checkpointing stores global tensor metadata and rank-local shards. Fully reshardable formats support selected TP, PP, EP, ETP, and DP layout changes, while the default DP-reshardable format can't change model parallelism. CP usually changes process groups rather than weight-shard shapes. Compatibility still depends on checkpoint format, model code, expert layout, and version. Test a save at one topology and load at a smaller topology before relying on it for recovery. Keep the exact launch arguments and Git commit beside the checkpoint.

Bridge makes the ecosystem boundary explicit. Hugging Face uses dense tensors and its own key names. Megatron uses tensor and pipeline shards, tied embeddings, optimizer partitions, and model-specific state. Bridge maps between those representations and can validate conversion. Exporting a model for inference may require gathering or converting shards, while resuming training should preserve distributed state instead of flattening everything onto one host.

Failure modes include an interrupted asynchronous save, an optimizer state saved with a different DP size, a missing expert shard, or a model class that changed its key layout. Use checksums, manifest files, and a small load-and-forward smoke test for every checkpoint artifact. A training job that can't prove which state it loaded isn't reproducible.

Precision: FP16, BF16, FP8, and FP4

Megatron Core integrates Transformer Engine for mixed precision. Common baselines use FP16 or BF16. FP8 recipes can increase matrix throughput and reduce activation bandwidth on supported NVIDIA GPUs, but scale tracking, amax history, accumulation precision, and kernel support affect convergence. Newer hardware also supports FP4 and NVFP4 paths for quantized weights or activations; these paths aren't drop-in BF16 replacements on every GPU.

Use low precision where it matches the hardware and model recipe. Keep optimizer masters and sensitive reductions in higher precision when the implementation expects it. Compare loss curves, gradient norms, validation slices, and checkpoint reload behavior against a BF16 baseline. A faster kernel that changes overflow behavior can cost more than it saves.

The repository's current README advertises FP16, BF16, FP8, and FP4 support and reports up to 47% MFU for specific H100 benchmarks.[1]Reference 1Megatron-LMhttps://github.com/NVIDIA/Megatron-LM Treat that number as a source-scoped measurement, not a promise for every model. The benchmark uses particular sequence lengths, vocabulary, model sizes, overlap flags, and a no-convergence run. Hardware, software versions, batch shape, and model architecture all change MFU.

Project identity

Megatron-LM and Megatron Core are NVIDIA-led open-source projects. Their origin, review ownership, and license boundaries are more specific than a generic "community framework" label.

FieldCurrent project fact
Origin and stewardNVIDIA created and maintains Megatron-LM and Megatron Core.[1]Reference 1Megatron-LMhttps://github.com/NVIDIA/Megatron-LM
Founding contributorsThe original paper names Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper, and Bryan Catanzaro.[3]Reference 3Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism.https://arxiv.org/abs/1909.08053
Contributor modelPublic contributions use tests, pull requests, NVIDIA code ownership, and Developer Certificate of Origin sign-off. Large architectural changes should begin with an issue.[6]Reference 6Contributing to Megatron Corehttps://docs.nvidia.com/megatron-core/developer-guide/nightly/developer/contribute.html
Source licenseThe top-level license applies BSD-3-Clause-style terms to files unless noted, then preserves Apache-2.0, MIT, BSD, and other notices for bundled code. Treat the repository as composite rather than assigning one SPDX identifier to every file.[7]Reference 7Megatron-LM Repository Licenseshttps://github.com/NVIDIA/Megatron-LM/blob/9829b3f1dd16f5233ff7bf50e67f6443c527c2bc/LICENSE
Commercial boundaryMegatron Core is source code, not a hosted endpoint. NVIDIA containers, NeMo products, support, and cloud offerings can carry separate terms.[1]Reference 1Megatron-LMhttps://github.com/NVIDIA/Megatron-LM
Asset boundaryModel checkpoints, tokenizers, and training data used with Megatron keep their own licenses and access rules.

Research lineage

Megatron follows a research lineage of complementary systems ideas rather than one kernel.

SourceContributionHow it appears in current code
Shoeybi et al. (2019)Tensor and pipeline model parallelism for multi-billion-parameter transformersColumn and row parallel layers, pipeline stages, one-sync MLP pattern[3]Reference 3Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism.https://arxiv.org/abs/1909.08053
Narayanan et al. (2021)Scaling model-parallel training across GPU clusters with overlap and schedulingCombined DP, TP, PP launch planning and communication overlap[8]Reference 8Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LMhttps://arxiv.org/abs/2104.04473
Korthikanti et al. (2022)Selective activation recomputation to save memory with bounded extra computeFine-grained checkpoint and recomputation controls[9]Reference 9Reducing Activation Recomputation in Large Transformer Modelshttps://arxiv.org/abs/2205.05198
Megatron Core guideCurrent combinations of TP, PP, CP, EP, and DPRank generators, group collections, and launch constraints[2]Reference 2Parallelism Strategies Guide.https://docs.nvidia.com/megatron-core/developer-guide/latest/user-guide/parallelism-guide.html
Current repositoryGPU kernels, models, precision recipes, checkpointing, and reference scriptsmegatron/core/, examples/, and tests/[1]Reference 1Megatron-LMhttps://github.com/NVIDIA/Megatron-LM

The papers explain why the decomposition works. The repository explains what it costs to keep the decomposition correct as architectures, accelerators, and model families change. Read both: a paper's clean grid can hide launch constraints, while a code path can hide the reason a collective is necessary.

Strengths and weaknesses

DimensionStrengthWeakness or boundary
ScaleMature TP, PP, DP, CP, EP, overlap, and checkpoint pathsGroup combinations multiply configuration and debugging states
PerformanceFused kernels, grouped GEMM, overlap, and topology-aware groupsSmall batches or slow links expose collective latency
Model coverageDense transformers, MoE, multimodal and hybrid architecturesNew architectures need model-specific builders and conversion work
MemoryDistributed optimizer, selective recompute, low precision, sharded checkpointsActivation, routing, and temporary buffers still need headroom
EcosystemBridge connects Hugging Face and Megatron representationsVersion skew between Core, Bridge, Transformer Engine, and CUDA can break loads
ReproducibilityFunctional tests and explicit launch argumentsLarge runs need disciplined manifests, seeds, and checkpoint audits

Megatron is a strong choice when you control a GPU cluster, need pretraining or serious adaptation, and can invest in topology and launch hygiene. It isn't the shortest path for a one-GPU fine-tune, a hosted inference endpoint, or a quick experiment where model fit and iteration speed matter more than maximum cluster utilization.

Production topology and debugging loop

Start with a single-node fragment. Run a tiny model with TP=2, PP=1, CP=1, DP=1, and mock data. Confirm loss decreases, checkpoints save, and a reload produces the same next-token logits within your precision tolerance. Then add one axis at a time. A command that names TP=4 and PP=2 isn't a 64-GPU proof unless eight local ranks and the required network are present.

For a real cluster, document:

  1. GPU model, HBM capacity, NVLink or NVSwitch links, and inter-node fabric.
  2. Core, Transformer Engine, CUDA, NCCL, PyTorch, and Bridge versions.
  3. TP, PP, CP, EP, DP, microbatch, global batch, sequence length, and precision.
  4. Rank order and expected process groups.
  5. Communication overlap flags and NCCL topology logs.
  6. Checkpoint format, save interval, resharding smoke test, and restart owner.
  7. Metrics for MFU, tokens per second, step time, communication time, memory high-water mark, expert balance, and loss.

When throughput drops after adding GPUs, check topology before changing the model. TP across Ethernet can turn every layer into a network barrier. PP can show a pipeline bubble if the microbatch count is too low. CP can spend more time exchanging context than computing attention. EP can stall on hot experts or all-to-all congestion. DP can hide communication until the network becomes the shared bottleneck.

When loss changes after a topology or precision change, compare one fixed batch across runs. Check parameter shard reconstruction, seed and data order, optimizer state reload, gradient scaling, and router decisions. Bitwise equality isn't always expected across kernels, but a reproducible run should explain any tolerance and show that validation behavior remains inside its release budget.

Mastery check

By this point, you can:

  • Factor a world size into TP, PP, CP, EP, and DP axes and write each rank's coordinate.
  • Explain why a column-parallel projection can feed a row-parallel projection without an intermediate all-gather.
  • Distinguish sequence parallelism's activation-memory role from context parallelism's sequence partition.
  • Trace MoE token dispatch, expert compute, combine, and the communication tax of skew.
  • Calculate distributed-optimizer memory from the DP degree and name buffers that aren't in the formula.
  • Plan a checkpoint save, reload, and reshard smoke test.
  • Treat MFU and speedup claims as configuration-specific measurements with hardware and convergence caveats.
Complete the lesson

Mastery Check

Answer every question, then check your score. Score 75% or higher to mark this lesson complete.

1.A dense run has TP=4, PP=2, CP=2, and DP=4. How many ranks does the grid require?
2.Why can GeLU stay local between Megatron's column and row parallel MLP projections?
3.Which operation gives each DP rank its fully reduced gradient shard in the distributed optimizer loop?
4.What does context parallelism partition?
5.A MoE run shows one expert receiving most tokens and other experts idle. Which diagnosis fits first?
6.What is a safe interpretation of a 47% MFU figure in Megatron's README?
7.Why can TP across a slow inter-node link erase scaling gains?
8.What should accompany a checkpoint when you need a trustworthy topology change or restart?

8 questions remaining.

Next Step
Continue to Deep Dive - DeepSpeed

Megatron factors one training job across tensor, pipeline, data, context, and expert axes. DeepSpeed focuses next on training-engine control, ZeRO state ownership, offload, and recovery.

PreviousDeep Dive - NCCL
Share this article
XFacebookLinkedInBlueskyRedditHacker NewsEmail
References

Megatron-LM

NVIDIA · 2026

Parallelism Strategies Guide.

NVIDIA · 2026

Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism.

Shoeybi, M., et al. · 2019

Mixture of Experts

NVIDIA · 2026

Distributed Optimizer

NVIDIA · 2026

Contributing to Megatron Core

NVIDIA · 2026

Megatron-LM Repository Licenses

NVIDIA · 2026

Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM

Narayanan, D., et al. · 2021 · SC 2021

Reducing Activation Recomputation in Large Transformer Models

Korthikanti, V. A., et al. · 2022 · MLSys 2023

Discussion

Questions and insights from fellow learners.

Discussion loads when you reach this section.