Skip to navigation

Parallelism Strategies Guide

View as MarkdownOpen in Claude

Megatron Core supports multiple parallelism strategies that can be combined to efficiently train models from billions to trillions of parameters across thousands of GPUs.

Overview

The following table summarizes supported parallelism strategies.

StrategyParallelism ObjectiveBest For
Data Parallelism (DP)Batch DimensionData Scalability, Standard Training
Tensor Parallelism (TP)Individual LayersLarge Layers & Activation, GPU Memory Constraints
Pipeline Parallelism (PP)Model DepthVery Deep Models
Context Parallelism (CP)Sequence LengthLong Sequences (8K+ Tokens)
Expert Parallelism (EP)MoE ExpertsMixture-of-Experts Models
Fully-Sharded Data Parallelism (Megatron-FSDP)Model StateExtremely Large Models & DP Interchangeability

Data Parallelism (DP)

Standard Distributed Data Parallel (DDP)

Replicate the model across GPUs and split the batch.

torchrun --nproc_per_node=8 pretrain_gpt.py \
--data-parallel-sharding-strategy no_shard

Each GPU has a full copy of the model and processes a portion of the batch.

Megatron Fully-Sharded Data Parallel (Megatron-FSDP)

Shard model parameters, gradients, and optimizer states across GPUs to reduce memory utilization.

--use-megatron-fsdp
--data-parallel-sharding-strategy optim_grads_params
--ckpt-format fsdp_dtensor
--init-model-with-meta-device

Sharding Strategies

--data-parallel-sharding-strategy supports the following options:

  • optim - Shard optimizer states only (ZeRO-1)
  • optim_grads - Shard gradients + optimizer (ZeRO-2)
  • optim_grads_params - Shard parameters + gradients + optimizer (ZeRO-3)

If --num-distributed-optimizer-instances is > 1, then hierarchical data parallelism is enabled.

--outer-dp-sharding-strategy supports the following options:

  • no_shard (Hybrid-Sharded Data Parallelism) - Replicate the model state across outer data parallel ranks.
  • optim (Hybrid-FSDP) - Shard the optimizer state across the outer data parallel ranks.
    • Requires --data-parallel-sharding-strategy optim_grads_params.

When to Use

  • Large models with large or fused compute kernels to hide communications under.
  • Integrated with TP, CP, EP, and easily composable with heterogeneous parallelisms.
  • With SM-reducing optimizations from NCCL and activation offloading from TransformerEngine.
  • Using fully_shard without depending on Megatron-LM.

Tensor Parallelism (TP)

Split individual model layers across GPUs. Recommended for large hidden dimensions.

--tensor-model-parallel-size 4 # 4-way tensor parallelism
--sequence-parallel # Enable sequence parallelism (recommended)

When to Use

  • Model layers do not fit on a single GPU
  • Large hidden dimensions (4096+)
  • Usually combined with DP and PP

Pipeline Parallelism (PP)

Split model layers across GPUs vertically (by depth).

--pipeline-model-parallel-size 8 # 8 pipeline stages
--num-layers-per-virtual-pipeline-stage 4 # Virtual pipeline for load balancing

When to Use

  • Very deep models (50+ layers)
  • Combine with TP for large models
  • Helps distribute memory across GPUs

Context Parallelism (CP)

Split long sequences across GPUs for efficient long-context training.

--context-parallel-size 2 # 2-way context parallelism
--cp-comm-type p2p # Communication type

When to Use

  • Long sequences (8K+ tokens)
  • Reduces activation memory
  • Can combine with TP, PP, DP

Refer to the Context Parallel Overview for a detailed guide with performance analysis.

Expert Parallelism (EP)

Distribute experts across GPUs in Mixture-of-Experts models.

--expert-model-parallel-size 8 # 8-way expert parallelism
--num-experts 64 # 64 experts per MoE layer
--moe-grouped-gemm # Optimize expert computation

Important: When combining EP with TP, you must enable Sequence Parallelism:

--tensor-model-parallel-size 4
--expert-model-parallel-size 8
--sequence-parallel # Required when using TP + EP

Parallelism Selection Guide

For a list of supported configurations, refer to Megatron Bridge Supported Models.

Language Models

Recommended language model configurations:

ModelSizeGPUsTPPPCPEPConfiguration Notes
LLaMA-38B81121CP=2 for long context (8K seqlen)
LLaMA-370B644421Balanced TP+PP for 70B scale
LLaMA-3.1405B102488213D parallelism (TP+PP+CP)
GPT-3175B128-5124811Standard large model config

Mixture-of-Experts Models

Recommended mixture-of-experts configurations:

ModelSizeGPUsTPPPCPEPConfiguration Notes
Mixtral8x7B641418EP=8 for 8 experts
Mixtral8x22B2564418TP+PP+EP for large MoE
DeepSeek-V3671B1024216164Massive MoE with 256 experts

Combining Strategies

Total GPU Count

The total number of GPUs is calculated as:

Total GPUs = TP × PP × CP × EP × DP

Example: LLaMA-3 70B on 64 GPUs

# TP=4, PP=4, CP=2, DP=2 => 4 × 4 × 2 × 2 = 64 GPUs
torchrun --nproc_per_node=8 pretrain_gpt.py \
--tensor-model-parallel-size 4 \
--pipeline-model-parallel-size 4 \
--context-parallel-size 2 \
--num-layers 80 \
--hidden-size 8192 \
--num-attention-heads 64 \
--seq-length 8192 \
--micro-batch-size 1 \
--global-batch-size 512 \
--bf16

Performance Optimizations

Communication Overlap

Enable overlapping of communication with computation:

--overlap-grad-reduce # Overlap gradient reduction with backward pass
--overlap-param-gather # Overlap parameter gathering with forward pass
--tp-comm-overlap # Overlap TP communication

Distributed Optimizer

Recommended for all multi-GPU training:

--use-distributed-optimizer

Benefits

  • Faster checkpointing
  • Reduced memory when combined with FSDP
  • Better performance at scale

Sequence Parallelism

Always enable when using TP:

--sequence-parallel

Reduces activation memory by sharding sequence dimension in LayerNorm and Dropout.

Choosing the Right Strategy

Start Simple

  1. Begin with Data Parallelism (DP) only.
  2. Add Tensor Parallelism (TP) if the model does not fit.
  3. Add Pipeline Parallelism (PP) for very large models.
  4. Add Context Parallelism (CP) for long sequences.

Memory Constraints

  • Use FSDP to split model state per GPU.
  • Use TP to split large layers.
  • Use PP to split model depth.
  • Enable activation checkpointing or offloading for extreme cases.

Communication Bottlenecks

  • Reduce TP degree (increases memory per GPU).
  • Increase PP degree (may reduce efficiency).
  • Use CP instead of larger TP for long sequences.

Next Steps