Backpropagation in 3D tensor parallelism: follow one GPU through the input and weight gradients

July 9, 2025

Two GPUs compute 1 and 3, which sum to the neuron’s output of 4. During training the next layer sends a number back telling us how a small increase in this output affects the loss. Both GPUs contributed in the forward pass. Which gets the number on the way back? Which inputs and weights need updating? Let’s compute one linear layer on eight GPUs and follow one of them to the end.

How does one upstream gradient become two different gradients?

Start with one output, y = x₁w₁ + x₂w₂. Loss is a number measuring prediction error. If the loss locally increases three times as fast as y, we say the upstream gradient arriving at y is 3. Adjust only x₁ and it affects y through w₁, so the gradient propagated to x₁ is 3w₁. Adjust only w₁ and it acts through x₁, giving 3x₁. When one input affects several outputs or a weight is used for multiple tokens, add the contributions from each path.1

Take one number from the matrices below. The third output of the first token is 6. Two GPUs each compute a part: the left contributes 2 and the right 4. The upstream gradient for that output happens to be 3.

flowchart TB
    subgraph FW["前向:两份贡献合成一个输出"]
        direction TB
        A["左卡:1 × 2 + 2 × 0 = 2"] --> Y["相加:2 + 4 = 6"]
        B["右卡:3 × 0 + 4 × 1 = 4"] --> Y
    end
    Y -.->|"后续网络算出损失,传回梯度"| G["反向从输出开始:上游梯度 = 3"]
    G -->|"加法左项增加 1,输出也增加 1"| L["左项收到 3"]
    G -->|"加法右项增加 1,输出也增加 1"| R["右项也收到 3"]
    L -->|"看左项里的 1 × 2,乘权重 2"| DX["传给输入 1 的梯度贡献:3 × 2 = 6"]
    L -->|"看同一次乘法,乘输入 1"| DW["传给权重 2 的梯度贡献:3 × 1 = 3"]

Follow the arrows from the 3 in the middle. Addition passes the same gradient to its left and right operands. At a multiplication, multiply by the other factor to get the gradient for the one you’re following. Only one multiplication on the left GPU is expanded. The diagram uses this article’s numbers and follows Synced’s explanation, which draws the forward pass first and traces backward signals layer by layer.2

Let X be the inputs and W the weights. The forward pass computes Y = X @ W. Write G for the gradient reaching Y from the rest of the network, with the same shape as Y. Backpropagation has two multiplications: dX = G @ W.T and dW = X.T @ G. .T swaps rows and columns. The first adds the effects of different output features back into the inputs. The second adds contributions from different tokens back into the weights. The previous layer receives dX, and the optimizer uses dW to update this layer’s weights.32

Why wasn’t the 6 just passed to the input its complete gradient? Fix the first token and its first input x₁ = 1. Through a weight of 2 it affects the third output. Through a weight of 1 it also affects the first output. The diagram shows just these two nonzero-weight connections. Follow the solid arrows rightward to see which predictions depend on this input, then follow the dashed arrows back to see how much gradient each prediction contributes.12

flowchart LR
    X(("输入 x₁ = 1")) -->|"权重 1"| A["第一个输出 = 4<br/>上游梯度 = 1"]
    X -->|"权重 2"| B["第三个输出 = 6<br/>上游梯度 = 3"]
    A -.->|"1 × 1 = 1"| X
    B -.->|"3 × 2 = 6"| X
    linkStyle 2,3 stroke:#ad392a,stroke-width:2px

Both dashed arrows lead to the same input, so the input gradient in this cell is 1 + 6 = 7. Later, when the two outputs live on different GPUs, these contributions must be added across devices.

The two backward multiplications used weights 1 and 2. They form the first column of the following table, the kind Zhang Juefei calls a Jacobian matrix. Select only inputs x₁, x₄ and outputs y₁, y₃ for the first token, holding other inputs and weights fixed. Each cell answers: if I move only this column’s input, how much does this row’s output change?4

OutputRate of change with respect to x₁Rate of change with respect to x₄
y₁ (current value 4)10
y₃ (current value 6)21

Read down the first column. Increase x₁ by 0.01 and y₁ increases by 0.01, while y₃ increases by 0.02. This table of rates, with outputs as rows and inputs as columns, is the Jacobian of the selected outputs with respect to the selected inputs. Select a single scalar loss as the output, and the table has just one row, with one component of the loss gradient in each cell.

Now propagate the loss gradient back through the diagram. The upstream gradients for y₁ and y₃ are 1 and 3. Multiply them by the entries in the first column and add, giving 1×1 + 3×2 = 7 as before. Automatic differentiation is the programmatic composition of derivative rules along the graph. Training backpropagation uses its reverse mode, which can compute the two contributions directly without constructing the complete Jacobian. Forward-mode automatic differentiation instead starts with a change in one input and follows it forward to see how each output changes.4

Consider the edge with weight 2. The first token contributes input 1 × upstream gradient 3 = 3 to this weight’s gradient. The other three tokens use the same weight. Summing all four rows of data below gives 1×3 + 0×7 + 2×1 + 1×0 = 5. If the optimizer takes a step of 0.1 times the gradient in the opposite direction, the weight changes from 2 to 2 − 0.1×5 = 1.5. That 0.1 is the learning rate.2

Follow δ through a complete backward pass

Put the multiplication back into a small network to follow the gradient from loss to two inputs. The animation uses a separate example. Two inputs feed two hidden nodes and produce one prediction. Every node just computes a weighted sum. δ beside each node is the rate at which the loss changes as that node’s value increases. For a prediction of 10 and target of 9, use loss L = (prediction − target)² / 2. The first gradient returning from the output is δy = 10 − 9 = 1.1

Blue solid lines compute the forward pass. Separate δ nodes pass gradients back along red dashed lines. Each green update arrow points to a weight edge and displays its update formula.

The δ nodes and edge-by-edge updates follow the illustration in Synced’s “Backpropagation Algorithm”. The network and numbers are this article’s example.2

On an eight-GPU grid, what can GPU (0,0,0) see?

Use the four-token example from the forward-pass article. Each row is a token, with four inputs and four outputs per token. For checking individual values, we supply G from the subsequent network directly. For example, define loss as the sum of the elementwise products of Y and G and you obtain exactly this gradient.

Give every GPU three coordinates (i,j,k), each 0 or 1. i selects the first or last two tokens, j the left or right two outputs, and k the first or last two input features. The two GPUs at fixed i,j handle the same output block, each computing half its dot product. We’ll use the three communication axes in Colossal-AI’s tutorial and explicitly specify the within-block layout so you can read the values by row and column.3

Watch GPU (0,0,0) in the lower right. Blue cells are values it already has, orange cells arrive from its partner on the left. The full matrix stays above, with a black outline around the 2 × 2 block being assembled. Watch W carefully. This GPU holds the left column, 1 above and 0 below. Its partner sends the right column, 0 above and 1 below. Together the two columns form the weight block for the multiplication.

GPU (0,0,0) assembles three matrix blocks. The rows of X are 1,2 and 0,1. The columns of W are 1,0 and 0,1. The rows of G are 1,2 and 5,6. Each scene highlights the cells in the full matrix before copying the partner shard into the missing positions.

The three scenes show X, W, then G. The first two prepare inputs and weights for the forward multiplication. The third prepares upstream gradients for the backward multiplication. Arrows follow only the shard received by this GPU. After gathering each other’s data, both partners have the complete block.

ObjectBlock needed for this GPU’s multiplicationHow the block was originally split
Input block X[i,k]Two tokens × two input featuresThis GPU holds the upper row and (0,1,0) the lower row. Gather along j
Weight block W[k,j]Two input features × two output featuresThis GPU holds the left column and (1,0,0) the right. Gather along i
Output block Y[i,j] and upstream gradient G[i,j]Two tokens × two output featuresThis GPU retains the upper row and (0,0,1) the lower. Distribute rows along k
       X                   W                   G
  [1 2 3 4]           [1 0 2 0]           [1 2 3 4]
  [0 1 0 1]           [0 1 0 2]           [5 6 7 8]
  [2 0 1 0]           [1 1 0 0]           [1 0 1 0]
  [1 1 0 0]           [0 0 1 1]           [0 1 0 1]

In the forward pass, GPU (0,0,0) gathers missing inputs and weights from its partners to obtain X[0,0] = [[1,2],[0,1]] and W[0,0] = [[1,0],[0,1]]. Its first partial dot product is 1×1 + 2×0 = 1. GPU (0,0,1) contributes 3×1 + 4×0 = 3. After elementwise addition, the first GPU retains the complete output block’s first row [4,5] and the second its second row [0,1]. This is reduce-scatter: add values at corresponding positions, then each GPU keeps only its assigned positions.

Going backwards through this GPU, where do we gather and where do we sum?

Step one: gather upstream gradients along k. GPU (0,0,0) has [1,2], and (0,0,1) has [5,6]. Exchange the rows and both have G[0,0] = [[1,2],[5,6]]. This is an all-gather. Why do both need both rows? Each computed part of the dot products for two tokens in the forward pass, so both need the gradients of both tokens in the backward pass. We assume the gathered X and W blocks from the forward pass still exist. If only original shards were saved, gather them again before multiplying.

Step two: complete the input gradients along j. GPU (0,0,0) computes G[0,0] @ W[0,0].T, obtaining [[1,2],[5,6]]. Two other output features also used the same inputs. GPU (0,1,0) handles them and gets [[6,8],[14,16]]. Elementwise addition gives [[7,10],[19,22]]. After reduction, the first GPU keeps only the gradients for its original input row, [7,10]. The other keeps [19,22].

Step three: complete the weight gradients along i. Back on (0,0,0), compute X[0,0].T @ G[0,0], obtaining [[1,2],[7,10]]. GPU (1,0,0) used the same weight block on the final two tokens, contributing [[2,1],[0,1]]. The sum is [[3,3],[7,11]]. Weights were stored by column, so the first GPU keeps the first column [3,7], and its partner keeps [3,11]. The lower-left 7 is 2×1 + 1×5 + 0×1 + 1×0 = 7 from the four tokens, exactly as a full matrix multiplication gives.

flowchart TB
    A["本卡 (0,0,0) 持有一行<br/>[1, 2]"] --> G["沿 k 收集 all-gather<br/>两卡都有两行:上行 [1, 2],下行 [5, 6]"]
    K["伙伴 (0,0,1) 持有另一行<br/>[5, 6]"] --> G
    G --> X["输入梯度 dX:本卡用 G 乘权重转置<br/>看本卡要留的一行:[1, 2]"]
    G --> W["权重梯度 dW:本卡用输入转置乘 G<br/>看本卡要留的一列:上 1,下 7"]
    X --> J["沿 j 逐项相加<br/>[1 + 6, 2 + 8] = [7, 10]"]
    P["伙伴 (0,1,0)<br/>另外两个输出贡献 [6, 8]"] --> J
    W --> I["沿 i 逐项相加<br/>上:1 + 2 = 3,下:7 + 0 = 7"]
    Q["伙伴 (1,0,0)<br/>另外两个词元贡献上 2、下 0"] --> I
    J --> DX["本卡留一行 [7, 10]<br/>传给上一层"]
    I --> DW["本卡留一列,上 3、下 7<br/>用于更新本卡的权重"]

Start at the top: the two gradient rows must be assembled. Then follow either branch downward: only contributions at corresponding positions should be summed. The lower half shows only the row or column this GPU retains in reduce-scatter. Its partner also retains its assigned part.

For any GPU, which line of code corresponds to the multiplication above?

The Python below runs on a CPU with NumPy. Array slices represent the blocks visible after gathering. A dictionary stores local products from each of the eight GPUs. Finally, use the same three coordinates to return gradients to the original shards and check each GPU against the full matrix multiplications.3

import numpy as np
from itertools import product

X = np.array([[1,2,3,4], [0,1,0,1], [2,0,1,0], [1,1,0,0]])
W = np.array([[1,0,2,0], [0,1,0,2], [1,1,0,0], [0,0,1,1]])
G = np.array([[1,2,3,4], [5,6,7,8], [1,0,1,0], [0,1,0,1]])
cards = list(product(range(2), repeat=3))

def block(matrix, row, col):
    return matrix[2*row:2*row+2, 2*col:2*col+2]

input_parts, weight_parts = {}, {}
for i, j, k in cards:
    upstream = block(G, i, j)  # 沿 k 收齐的两行
    input_parts[i,j,k] = upstream @ block(W, k, j).T
    weight_parts[i,j,k] = block(X, i, k).T @ upstream

dx, dw = G @ W.T, X.T @ G
for i, j, k in cards:
    input_block = input_parts[i,0,k] + input_parts[i,1,k]
    weight_block = weight_parts[0,j,k] + weight_parts[1,j,k]
    local_dx = input_block[j, :]
    local_dw = weight_block[:, i]
    np.testing.assert_array_equal(local_dx, block(dx, i, k)[j, :])
    np.testing.assert_array_equal(local_dw, block(dw, k, j)[:, i])
    print((i,j,k), local_dx.tolist(), local_dw.tolist())

The first output line is (0, 0, 0) [7, 10] [3, 7]. Real communication can add while data is moving. For dX, for example, each GPU can send just the row the other owns. Each receives two numbers and adds them locally, implementing reduce-scatter for this pair. With more GPUs you can also send to specific destinations. Direct reception by each owner or accumulation through neighboring GPUs in a ring can produce the same final ownership, but message rounds and link congestion depend on implementation.5

How can there be six collectives across both passes and still less communication per GPU?

The forward pass has two gathers and one reduce-scatter. The backward pass has one gather and two reduce-scatters. In the tutorial’s ring estimate, with q GPUs per cube side, a collective uses only the q GPUs along one axis. Each sends roughly q−1 original shards. For three equally sized matrices of S bytes each, one shard is S/q³. Across six collectives the one-way payload per GPU is approximately 6(q−1)S/q³. Increasing q from 2 to 4 takes the cluster from eight to 64 GPUs. Shards shrink by a factor of eight, but rounds per collective rise from one to three. The chart’s “bandwidth cost” measures data volume. Its “latency cost” also counts communication rounds. The formula alone cannot tell you that more devices make a training step faster.3

References

Footnotes

  1. Zhang, A., Lipton, Z. C., Li, M., & Smola, A. J. (n.d.). 正向传播、反向传播和计算图. 动手学深度学习. 链式法则与反向传播 ↩ ↩2 ↩3

  2. 机器之心(贡献者 Ruiying Cai). (n.d.). 反向传播算法. 机器之心知识库. “权重更新”小节及其后的逐层传播示意图。 ↩ ↩2 ↩3 ↩4 ↩5

  3. Bian, Z., & Li, Y. (n.d.). 3D tensor parallelism. Colossal-AI. Introduction 的逐轴通信与 Efficiency 的前后向成本 ↩ ↩2 ↩3 ↩4

  4. 张觉非. (n.d.). 计算图反向传播的原理及实现. 知乎专栏. 第 2 节,向量节点的雅可比与多条路径的贡献。JAX authors. (n.d.). The Autodiff Cookbook. JAX documentation. 前向模式与反向模式的雅可比乘积。 ↩ ↩2

  5. NVIDIA. (n.d.). Collective operations. NCCL User Guide. ReduceScatter 的结果如何分给各进程 ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-07-09). Backpropagation in 3D tensor parallelism: follow one GPU through the input and weight gradients. https://caiborui.com/blog/2025-07-09-zh-3d-tensor-parallel-backward-entrans

@misc{borui2025-3fb4a167,
  author = {borui},
  title = {{Backpropagation in 3D tensor parallelism: follow one GPU through the input and weight gradients}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-07-09-zh-3d-tensor-parallel-backward-entrans}
}