TP 的 communication:一层几次?每次多少数据?
TP 越大越快吗?
先弄清楚:每个概念是什么
- f / g operator
- 每个 TP block(attention、MLP)入口一个 f、出口一个 g。f 只在 backward all-reduce,g 只在 forward all-reduce,切法见 TP Linear。
- Ring all-reduce
- 数据切成 \(t\) 块,reduce-scatter 和 all-gather 各转 \(t-1\) 步。每卡收发 \(2(t-1)/t \cdot S\),几乎不随 \(t\) 变小,数字见下面的表,细节见 all-reduce。
- NVLink vs InfiniBand
- 机内 GPU 之间走 NVLink,跨机走 InfiniBand 网卡。例:DGX H100 一台 8 卡,每卡 NVLink 900 GB/s(官方口径,按双向合计理解,单向约 450)。跨机每卡一张 400 Gb/s(= 50 GB/s)的网卡,差约 9 倍。
- Critical path
- TP 的 all-reduce 夹在两个算子中间,下一个 GEMM 要等它的结果才能开始。DP 的 gradient 算完一层就能先发,前面的层接着算,见 overlap。
看图:4 次 all-reduce 在哪,ring all-reduce 怎么走
30 秒回答
Each layer has two TP blocks, attention and MLP. Each needs one all-reduce in forward, after the row-parallel linear, and one in backward, before the column-parallel linear: four per layer per micro-batch. Each all-reduce carries b·s·h elements, 64 MiB in bf16 for b = 1, s = 4096, h = 8192. A ring all-reduce moves 2(t−1)/t of that per GPU, so traffic barely drops as t grows, while compute per GPU falls as 1/t. So bigger TP isn't always faster: communication starts to dominate, and past one node it goes over InfiniBand, an order of magnitude slower. Keep TP within the NVLink node; use PP across nodes.
推导 / 关键公式
\(B\) 是每卡带宽(NCCL 的 bus bandwidth)。每层每卡 forward + backward 合计 \(8bsh\,(t-1)/t\) 个元素。
为什么 t 大了会亏(以 forward 为例)
\[ \frac{\text{每卡 FLOPs}}{\text{每卡通信 bytes}} \approx \frac{24bsh^2/t}{2 \times 2bsh \times 2(t-1)/t} = \frac{3h}{t-1} \]分子是每层 GEMM 的 FLOPs(见 复杂度)除以 \(t\),分母是 2 次 all-reduce,每次每卡 \(2(t-1)/t \times 2bsh\) bytes。比值和 \(b\)、\(s\) 无关,\(t\) 每翻一倍就少一半多。
手算例子
华为题:\(t = 2\)、80 MB、40 GB/s → \(2 \cdot \tfrac12 \cdot 80 / 40 = 2\) ms。
\(b = 1\)、\(s = 4096\)、\(h = 8192\)、bf16,\(S\) = 64 MiB:
| \(t\) | 每卡每次流量 | \(3h/(t-1)\) | 链路 |
|---|---|---|---|
| 2 | 64 MiB | 24576 | NVLink |
| 4 | 96 MiB | 8192 | NVLink |
| 8 | 112 MiB | 3511 | NVLink |
| 16 | 120 MiB | 1638 | 跨机 InfiniBand |
H100 dense BF16 ≈ 989 TFLOPS(官网的 1979 是 sparsity 数字)。计算要盖过通信,每字节需配 989 TFLOPS ÷ 450 GB/s ≈ 2200 FLOPs(NVLink,按单方向算)或 ÷ 50 GB/s ≈ 19800(InfiniBand)。\(t = 8\):3511 > 2200,够;\(t = 16\) 跨机:1638 远小于 19800,通信时间约是计算的 12 倍。
必答点
- 每层 4 次 all-reduce:forward 2 次(g),backward 2 次(f)
- 每次 \(bsh\) 个元素;ring 下每卡流量 \(2(t-1)/t \cdot S\)
- 每卡计算按 \(1/t\) 降、通信几乎不降 → 计算 / 通信 \(\approx 3h/(t-1)\),GEMM 也变小
- TP ≤ 单机 GPU 数(NVLink domain);跨机用 PP,再外层 DP(为什么见 parallelism 配置)
- all-reduce 在 critical path 上,不像 DP 那样容易 overlap
追问
怎么优化 TP 的 all-reduce?
开 SP,把 all-reduce 拆成 reduce-scatter + all-gather,通信量不变,LayerNorm、dropout 的 activation 再除以 \(t\)。把 GEMM 切块,和 all-gather / reduce-scatter 流水 overlap(Megatron Core tp_comm_overlap,要求开 SP)。Hopper + NVSwitch 上用 NVLS,把 all-reduce 交给 NVSwitch 做。
实际最优的 t 是多少?
Narayanan et al. 在 DGX A100 上实测,\(t\) 等于单机 GPU 数(8)时吞吐最高,再往上跨机就掉。
来源Shoeybi et al. 2019 §3(pp.4–5,Fig. 4:每层 forward、backward 各 2 次 all-reduce)· Narayanan et al. 2021 §1(p.1)、§3.2(p.5,\(8bsh(t-1)/t\),Takeaway #1)、§5.4.1(p.10)· nccl-tests PERFORMANCE.md(all-reduce bus bandwidth \(= 2(n-1)/n\) × algbw)· NCCL env(NVLS)· DGX H100 User Guide(NVLink 900 GB/s,8 × ConnectX-7 400 Gb/s)· H100 spec · Megatron Core model_parallel_config.py(tp_comm_overlap)