Tensor Parallelism and Pipeline Parallelism: Mathematical Foundations of Megatron-LM 1D Slicing, Sequence Parallelism, and 1F1B Scheduling

Scaling modern large language models beyond tens of billions of parameters quickly exhausts the physical memory and compute throughput of individual graphics processing units. A 70-billion-parameter model stored in 16-bit floating-point (FP16 or BF16) requires 140 GB of VRAM solely for model weights. During full-precision training with the Adam optimizer, parameter states, gradients, and optimizer momentum terms demand approximately 16 to 18 bytes per parameter (amounting to 1.12 TB to 1.26 TB f

10 min
Tensor Parallelism and Pipeline Parallelism: Mathematical Foundations of Megatron-LM 1D Slicing, Sequence Parallelism, and 1F1B Scheduling

Scaling modern large language models beyond tens of billions of parameters quickly exhausts the physical memory and compute throughput of individual graphics processing units. A 70-billion-parameter model stored in 16-bit floating-point (FP16 or BF16) requires 140 GB of VRAM solely for model weights. During full-precision training with the Adam optimizer, parameter states, gradients, and optimizer momentum terms demand approximately 16 to 18 bytes per parameter (amounting to 1.12 TB to 1.26 TB for a 70B checkpoint), completely detached from the dynamic memory required to store forward-pass activation tensors.

When model weights cannot fit onto a single accelerator, practitioners rely on multi-dimensional distributed parallelism. Tensor Parallelism (TP) shards individual weight matrices within each transformer layer across multiple GPUs connected via high-bandwidth interconnects. Pipeline Parallelism (PP) partitions layers sequentially across devices along the network depth.

Understanding the mathematical decomposition of matrix multiplications, the communication algebra of conjugate operators, Sequence Parallelism extensions, and the scheduling dynamics of 1F1B pipeline execution is essential for designing distributed training and inference architectures.

Distributed Tensor Parallelism and Pipeline Architecture

The Memory Footprint of Distributed Transformer Training

To understand why multi-dimensional parallelism is necessary, consider the exact memory breakdown of training a transformer parameterized by Φ\Phi weights with hidden dimension hh, sequence length ss, batch size bb, and layer count LL.

Static Memory: Weights, Gradients, and Optimizer States

In mixed-precision training using standard 32-bit Adam optimization:

  • Model Weights (FP16/BF16): 2 bytes per parameter (2Φ2\Phi).
  • Gradients (FP16/BF16): 2 bytes per parameter (2Φ2\Phi).
  • Master Weights (FP32): 4 bytes per parameter (4Φ4\Phi).
  • First Momentum Vector (FP32): 4 bytes per parameter (4Φ4\Phi).
  • Second Momentum Vector (FP32): 4 bytes per parameter (4Φ4\Phi).

The baseline static memory footprint per parameter is:

Mstatic=2Φ+2Φ+4Φ+4Φ+4Φ=16Φ bytesM_{\text{static}} = 2\Phi + 2\Phi + 4\Phi + 4\Phi + 4\Phi = 16\Phi \text{ bytes}

For a model with Φ=70×109\Phi = 70 \times 10^9 parameters, Mstatic=1.12 TBM_{\text{static}} = 1.12 \text{ TB}. On standard 80 GB NVIDIA H100 GPUs, static states alone require a minimum of 14 GPUs without accounting for intermediate activations, workspace buffers, or memory fragmentation.

Dynamic Memory: Activation Scaling

Activation memory stores the intermediate tensors produced during the forward pass that are retained for gradient computation during backpropagation. For a standard transformer layer with multi-head attention and a 4-multiple feed-forward network (FFN), the activation memory per layer MactM_{\text{act}} scales linearly with sequence length ss, batch size bb, and hidden dimension hh:

Mact=sbh(34+5ash) bytesM_{\text{act}} = s \cdot b \cdot h \cdot \left(34 + 5 \cdot \frac{a \cdot s}{h}\right) \text{ bytes}

where aa is the number of attention heads. As sequence lengths expand to 32,768 or 128,000 tokens, activation memory exceeds static weight memory by multiples.


Megatron-LM 1D Tensor Parallelism

Introduced by Shoeybi et al. (2019) at NVIDIA, 1D Tensor Parallelism splits the linear projection matrices of Multi-Head Self-Attention (MHA) and Multi-Layer Perceptron (MLP) blocks across tt parallel processing units while minimizing inter-device synchronization.

Column-Parallel Linear Layer

Consider a linear transformation Y=XAY = X A, where XRB×HinX \in \mathbb{R}^{B \times H_{\text{in}}} represents input activations and ARHin×HoutA \in \mathbb{R}^{H_{\text{in}} \times H_{\text{out}}} is the weight matrix. In column-parallel linear layers, the weight matrix AA is sliced along its column dimension across tt GPUs:

A=[A1A2At],AiRHin×HouttA = \begin{bmatrix} A_1 & A_2 & \dots & A_t \end{bmatrix}, \quad A_i \in \mathbb{R}^{H_{\text{in}} \times \frac{H_{\text{out}}}{t}}

Each GPU holds the full input tensor XX and independently computes its local partition of the output:

Yi=XAi,Y=[Y1Y2Yt]Y_i = X A_i, \quad Y = \begin{bmatrix} Y_1 & Y_2 & \dots & Y_t \end{bmatrix}

Because the transformation produces partitioned columns without requiring cross-partition reduction, an element-wise non-linear activation function σ()\sigma(\cdot) (such as GeLU or SwiGLU) can be applied directly to each shard locally:

σ(Y)=[σ(Y1)σ(Y2)σ(Yt)]\sigma(Y) = \begin{bmatrix} \sigma(Y_1) & \sigma(Y_2) & \dots & \sigma(Y_t) \end{bmatrix}

Column parallelism eliminates communication prior to or across the activation function.

Row-Parallel Linear Layer

In a row-parallel linear layer, the subsequent weight matrix BRHout×HfinalB \in \mathbb{R}^{H_{\text{out}} \times H_{\text{final}}} is sliced along its row dimension across tt GPUs:

B=[B1B2Bt],BiRHoutt×HfinalB = \begin{bmatrix} B_1 \\ B_2 \\ \vdots \\ B_t \end{bmatrix}, \quad B_i \in \mathbb{R}^{\frac{H_{\text{out}}}{t} \times H_{\text{final}}}

Each GPU computes the matrix product of its local input partition YiRB×HouttY_i \in \mathbb{R}^{B \times \frac{H_{\text{out}}}{t}} and its local weight slice BiB_i:

Zi=YiBiZ_i = Y_i B_i

The complete mathematical output ZZ requires summing across all local outputs:

Z=i=1tYiBi=i=1tZiZ = \sum_{i=1}^t Y_i B_i = \sum_{i=1}^t Z_i

This summation is performed by an All-Reduce\text{All-Reduce} (sum) collective communication operation across the tt tensor parallel ranks.

Column-Parallel:  X ──┬──> [ A_1 ] ──> Y_1 ──> GeLU ──> [ B_1 ] ──┬──> [ All-Reduce Sum ] ──> Z
                      ├──> [ A_2 ] ──> Y_2 ──> GeLU ──> [ B_2 ] ──┤
                      └──> [ A_t ] ──> Y_t ──> GeLU ──> [ B_t ] ──┘
                             (Column Slicing)               (Row Slicing)

Self-Attention Layer Decomposition

In multi-head attention, the query (WQW_Q), key (WKW_K), and value (WVW_V) projection matrices are partitioned using column parallelism. The attention heads hh are divided uniformly across tt GPUs (h/th/t heads per rank):

  • Projection Phase: Each GPU computes Qi=XWQiQ_i = X W_Q^i, Ki=XWKiK_i = X W_K^i, and Vi=XWViV_i = X W_V^i.
  • Attention Computation: Local heads evaluate attention maps independently:

Headi,k=Softmax(Qi,kKi,kTdk)Vi,k\text{Head}_{i, k} = \text{Softmax}\left(\frac{Q_{i, k} K_{i, k}^T}{\sqrt{d_k}}\right) V_{i, k}

  • Output Projection: The output projection matrix WOW_O is partitioned using row parallelism. Each GPU multiplies its concatenated local attention head outputs with WOiW_O^i.
  • Synchronization: A single All-Reduce\text{All-Reduce} collective sums the outputs across all tt ranks before the residual connection.

Conjugate Communication Operators

Megatron-LM defines two conjugate communication primitives, ff and gg, to manage forward and backward execution:

  • In Column-Parallel Layers: The forward operator ff is an identity mapping (no-op), while the backward operator ff^* computes an All-Reduce\text{All-Reduce} (sum) of the incoming gradients X\nabla_X.
  • In Row-Parallel Layers: The forward operator gg executes an All-Reduce\text{All-Reduce} (sum) across output shards, while the backward operator gg^* is an identity mapping.

Each transformer layer contains exactly one Attention block and one MLP block, requiring:

  • Forward Pass: 2 All-Reduce\text{All-Reduce} operations.
  • Backward Pass: 2 All-Reduce\text{All-Reduce} operations.

Using a ring-based All-Reduce\text{All-Reduce} algorithm, the communication volume per rank for a message of size MM elements is 2t1tM2 \cdot \frac{t - 1}{t} \cdot M. For an activation tensor of shape (s,b,h)(s, b, h), the total bytes transmitted per layer per step equals:

CommTP=8(t1t)sbh2 bytes\text{Comm}_{\text{TP}} = 8 \cdot \left(\frac{t - 1}{t}\right) \cdot s \cdot b \cdot h \cdot 2 \text{ bytes}

Because of the high frequency of communication (two synchronizations per transformer layer), Tensor Parallelism is strictly restricted to intra-node NVLink domains (t8t \le 8), where unidirectional bandwidth exceeds 450 to 900 GB/s per GPU.


Sequence Parallelism

While 1D Tensor Parallelism shards the GEMM weights and self-attention operations across tt GPUs, standard TP replicates LayerNorm, Dropout, and residual connection activations across all tt ranks. In modern transformer architectures, these replicated activations account for up to 10 sbhs \cdot b \cdot h bytes per layer.

Sequence Parallelism and Pipeline Scheduling

Korthikanti et al. (2022) introduced Sequence Parallelism (SP) in Megatron-LM to shard activations along the sequence length dimension ss during non-tensor-parallel operations.

Splitting All-Reduce into Reduce-Scatter and All-Gather

Operations such as LayerNorm and Dropout operate independently along the hidden dimension hh for each token in the sequence. By partitioning the sequence length ss into tt chunks of size s/ts/t, each GPU processes only s/ts/t tokens during LayerNorm and Dropout.

Sequence Parallelism replaces the standard All-Reduce\text{All-Reduce} communication operators with Reduce-Scatter\text{Reduce-Scatter} and All-Gather\text{All-Gather} pairs:

  1. Row-Parallel Exit: The row-parallel GEMM produces local partial sums of size (s,b,h)(s, b, h). Instead of performing an All-Reduce\text{All-Reduce} to create a replicated full tensor, the runtime executes a Reduce-Scatter\text{Reduce-Scatter} operation. This simultaneously sums the partial products and shards the result along the sequence dimension, leaving each GPU with an (s/t,b,h)(s/t, b, h) tensor.
  2. Normalized Execution: LayerNorm, residual additions, and Dropout execute locally on (s/t,b,h)(s/t, b, h) tensors.
  3. Column-Parallel Entry: Before entering the next column-parallel GEMM (which expects the full sequence ss to multiply against column-sharded weights), the runtime executes an All-Gather\text{All-Gather} operation, reconstructing the (s,b,h)(s, b, h) activation tensor.

Mathematically, because All-ReduceReduce-Scatter+All-Gather\text{All-Reduce} \equiv \text{Reduce-Scatter} + \text{All-Gather}, the total communication volume of Sequence Parallelism is strictly identical to standard Tensor Parallelism:

Volume(Reduce-Scatter)+Volume(All-Gather)=(t1tM)+(t1tM)=2(t1t)M=Volume(All-Reduce)\text{Volume}(\text{Reduce-Scatter}) + \text{Volume}(\text{All-Gather}) = \left(\frac{t - 1}{t} M\right) + \left(\frac{t - 1}{t} M\right) = 2 \left(\frac{t - 1}{t}\right) M = \text{Volume}(\text{All-Reduce})

Sequence Parallelism reduces the activation memory footprint from:

MactTP=sbh(10+24t+5asht)M_{\text{act}}^{\text{TP}} = s \cdot b \cdot h \cdot \left(10 + \frac{24}{t} + 5 \cdot \frac{a \cdot s}{h \cdot t}\right)

to:

MactTP+SP=sbht(10+24+5ash)M_{\text{act}}^{\text{TP+SP}} = \frac{s \cdot b \cdot h}{t} \cdot \left(10 + 24 + 5 \cdot \frac{a \cdot s}{h}\right)

Every single activation tensor in the transformer block is sharded by 1/t1/t without adding network communication overhead.


Pipeline Parallelism and 1F1B Scheduling

When a model's depth exceeds what can be accommodated within a single node (even with TP=8TP = 8), Pipeline Parallelism (PP) partitions the LL layers of the network across pp sequential pipeline stages located on different machines.

The Pipeline Bubble in GPipe

In naive pipeline parallelism (GPipe, Huang et al., 2019), a global batch BB is split into mm microbatches (b=B/mb = B/m). The execution flows linearly through stages 1,,p1, \dots, p:

Stage 4:             [F1][F2][F3][F4]                 [B4][B3][B2][B1]
Stage 3:         [F1][F2][F3][F4]         [B4][B3][B2][B1]
Stage 2:     [F1][F2][F3][F4] [B4][B3][B2][B1]
Stage 1: [F1][F2][F3][F4]                 [B4][B3][B2][B1]
Time ───> | Warmup |      Steady State     | Cooldown |

During startup (warmup) and drain (cooldown), downstream and upstream devices remain idle. The fraction of execution time lost to idle hardware (the pipeline bubble fraction FbubbleF_{\text{bubble}}) is given by:

Fbubble=p1m+p1F_{\text{bubble}} = \frac{p - 1}{m + p - 1}

When mpm \gg p, the bubble fraction approaches (p1)/m(p - 1)/m. However, in GPipe, all mm forward microbatches must be completed before backward passes begin, forcing stage 1 to store activations for all mm microbatches simultaneously:

MactGPipe=O(m) activation memoryM_{\text{act}}^{\text{GPipe}} = O(m) \text{ activation memory}

For large mm, activation memory overwhelms physical device capacity.

1F1B (One Forward, One Backward) Schedule

To bound activation memory while maintaining pipeline throughput, Megatron-LM implements the 1F1B (One Forward, One Backward) scheduling paradigm (Narayanan et al., 2021).

In 1F1B, after a stage completes a warm-up phase of forward passes equal to its remaining downstream depth (pip - i microbatches for stage ii), it enters a steady-state regime where it executes exactly one forward pass followed immediately by one backward pass for the oldest completed microbatch:

Stage 4:             [F1][B1][F2][B2][F3][B3][F4][B4]
Stage 3:         [F1][F2][B1][F3][B2][F4][B3][B4]
Stage 2:     [F1][F2][F3][B1][F4][B2][B3][B4]
Stage 1: [F1][F2][F3][F4][B1][B2][B3][B4]

Under 1F1B:

  • Outstanding forward activations are retired as rapidly as they are generated.
  • The maximum number of concurrently stored activation microbatches in any stage is capped at pp, independent of total microbatch count mm:

Mact1F1B=O(p) activation memoryM_{\text{act}}^{\text{1F1B}} = O(p) \text{ activation memory}

This decoupling allows engineers to scale the number of microbatches mm arbitrarily high to suppress the pipeline bubble without increasing GPU memory consumption.

Interleaved 1F1B Schedule

To further minimize the bubble fraction FbubbleF_{\text{bubble}} without increasing batch size mm, Megatron-LM supports interleaved virtual stages. Instead of assigning a contiguous block of L/pL/p layers to each device, each physical GPU manages vv virtual stages (chunks).

For example, with p=4p = 4 physical GPUs and v=2v = 2, GPU 1 holds Chunk 1 (Layers 1 to 2) and Chunk 5 (Layers 9 to 10). A microbatch passes through GPU 1 to GPU 2 to GPU 3 to GPU 4, then back to GPU 1 to GPU 2 to GPU 3 to GPU 4.

The resulting bubble fraction shrinks by a factor of vv:

Fbubbleinterleaved=p1vmF_{\text{bubble}}^{\text{interleaved}} = \frac{p - 1}{v \cdot m}

The trade-off is an increase in point-to-point peer communication over the network by a factor of vv, requiring dedicated high-speed inter-node links.


3D Parallelism Grid and Interconnect Topology

State-of-the-art frontier model training orchestrates Tensor Parallelism, Pipeline Parallelism, and Data Parallelism (DP) into a cohesive 3D grid across NN GPUs:

N=TP×PP×DPN = \text{TP} \times \text{PP} \times \text{DP}

Parallelism Dimensions Overview

  • Tensor Parallelism (TP + SP): Splits intra-layer GEMMs and sequence tokens. Uses Reduce-Scatter and All-Gather collectives. Demands extremely high bandwidth (>450 GB/s) and is mapped to intra-node NVLink/NVSwitch domains.
  • Pipeline Parallelism (PP): Splits model depth (LL layers). Uses point-to-point non-blocking send/receive operations. Requires moderate bandwidth (10 to 50 GB/s) and is mapped across inter-node InfiniBand/RoCE links.
  • Data Parallelism (DP / ZeRO): Splits the batch dimension (BB). Uses All-Reduce or Reduce-Scatter/All-Gather over parameter and gradient shards. Requires periodic bulk bandwidth (50 to 100 GB/s) across the cluster fabric.

Network Topology Mapping

Modern GPU cluster architectures (e.g., NVIDIA SuperPODs with H100/H200 nodes) feature 8 GPUs per server connected via high-bandwidth NVLink crossbar switches (900 GB/s per GPU), while inter-node communication is routed over 400 Gbps or 800 Gbps InfiniBand interfaces.

Engineers align the 3D parallelism topology directly with physical network bandwidth:

  1. Set TP8\text{TP} \le 8: Confine all Tensor and Sequence Parallelism communication within the intra-node NVLink domain. Crossing PCIe or InfiniBand links with Tensor Parallelism degrades arithmetic intensity and causes severe GEMM pipeline stalls.
  2. Set PP1\text{PP} \ge 1 across nodes: Pipeline Parallelism only transmits boundary layer activation tensors of shape (s,b,h)(s, b, h) between adjacent stages, incurring minimal point-to-point network traffic.
  3. Set DP/ZeRO\text{DP} / \text{ZeRO} across the remaining cluster: Data Parallelism distributes the outer batch dimension across nodes, overlapping gradient synchronization collectives with backward compute passes.

By decomposing dense matrix projections, sharding non-GEMM activations across sequence dimensions, and interleaving microbatch schedules, distributed transformer systems achieve linear compute scaling across tens of thousands of GPUs.


Sources

Written by

More to read

  • Matryoshka Representation Learning (MRL): Mathematical Foundations, Multi-Scale Loss Optimization, and Adaptive Vector Retrieval

    Matryoshka Representation Learning (MRL) has become the standard architectural foundation for modern dense text embeddings. Introduced by Kusupati et al. at NeurIPS 2022 and subsequently deployed across frontier embedding models like OpenAI text-embedding-3, Nomic Embed, and BAAI BGE-M3, MRL solves a structural inefficiency in vector retrieval: the rigid coupling between embedding dimensionality, memory consumption, and semantic fidelity. Traditional dense encoders project arbitrary text sequen

    1 min
  • llama.cpp Merges DFlash 2 Support for Up to 2x Faster Speculative Decoding Across Long Contexts

    The open-source llama.cpp project has merged native support for DFlash 2, bringing parallel speculative decoding and substantial inference throughput improvements to local LLM serving across CPU, Apple Silicon, and GPU backends. The implementation, integrated via Pull Request #27342, adds local convolution operators and candidate selector mechanics designed specifically for the DFlash 2 architecture. Non-Autoregressive Speculative Drafting Standard speculative decoding uses a smaller autoreg

    1 min
  • Meta Previews Hatch Consumer AI Agent with Dedicated Cloud Virtual Machines

    Meta is preparing to launch a consumer-facing autonomous AI agent codenamed Project Hatch, designed to execute long-running online tasks in the background using dedicated cloud virtual machines. Details of the project emerged from internal Meta memos reported by Business Insider and The Information. Unlike conventional conversational chatbots that respond synchronously to prompts, Hatch operates as an asynchronous personal agent capable of completing complex web interactions autonomously. Clo

    1 min