在 Ironwood 上对 MoE 训练进行基准测试,第一部分:扩展至 8K 芯片
原题Benchmarking MoE Training on Ironwood, Part 1: Scaling to 8K Chips
推荐理由用统一目标和批量口径理解 Google 的扩容结果,适合在选择大规模训练方案前把通信与计算瓶颈拆开问。
读前速览
本文对 Google Ironwood TPU7x 芯片上的混合专家模型训练进行基准测试,结果显示,671B 参数的 DeepSeek-V3 模型可在 8,192 个芯片上用时 1.70 分钟完成训练;从 2,048 个芯片扩展至 4,096 个芯片时,扩展效率为 96%,扩展至 8,192 个芯片时则为 74%。
Ironwood TPU7x 芯片可提供 4,614 TFLOP/s 的 FP8 算力和 192 GiB、带宽为 7.38 TB/s 的 HBM,使 671B 参数的 DeepSeek-V3 能在 8,192 个芯片上用时 1.70 分钟完成训练
每个芯片包含两个小芯片,每个小芯片配有一个 TensorCore、两个 SparseCore 和 96 GiB HBM;芯片间通过裸片到裸片链路连接,其速度是 1D ICI 链路的六倍。单个 Pod 可通过 3D 环面拓扑连接多达 9,216 个芯片,每个芯片的双向 ICI 带宽为 1.2 TB/s。
FSDP + EP 方案将全分片数据并行与专家并行结合起来,以优化大规模 MoE 训练
这种混合方案将 256 个路由专家分配到 EP 轴上(EP 为 8 时每台设备有 32 个专家,EP 为 16 时每台设备有 16 个专家),同时让 FSDP 覆盖整个切片。这样可以避免将所有专家权重执行全聚合并复制到每个芯片上,并使专家矩阵乘法受算力限制,而非受 HBM 带宽限制。
FP8 量化带来了最大的单项性能提升:在 512 个芯片、PBS=1 时,步时缩短了 31%
Ironwood 原生支持 FP8,算力为 4,614 TFLOP/s,是 BF16 的两倍(2,307 TFLOP/s)。量化使矩阵乘法的耗时减少了 4.7%,而量化集合通信功能带来的耗时减少达 68%;其中尤其重要的是量化权重全聚合,它将权重梯度全规约转换为规约散射。
由于单设备批次大小减小,扩展效率从 96%(2,048→4,096 个芯片)降至 74%(2,048→8,192 个芯片)
全局批次固定为 16,384 个序列时,增加芯片数量会将工作拆分得更细。在超过 2,048 个芯片后,梯度累积达到 1,因此必须将 PBS 从 4 降至 2,再降至 1,这种做法本身效率较低。第二部分介绍的批次拆分调度旨在弥补这一损失。
GMM v2、Splash attention 和不规则排序等内核优化使吞吐量提升约 10%,并降低了通信开销
GMM v2 从每个专家的首行开始处理分块,从而避免无效的 DMA 加载。Splash attention 跳过对角线子块和分段 ID。不规则排序操作只移动路由到本地专家的令牌;与置换整个缓冲区相比,数据移动量减少了 8-16 倍。
完整摘要
本文全面评测了 Google Ironwood TPU7x 芯片上的混合专家(MoE)模型训练,重点考察 DeepSeek-V3 671B 参数模型预训练的扩展能力。作者使用 2,048、4,096 和 8,192 个芯片训练模型,在 8,192 个芯片上实现了 1.70 分钟的训练时间,且所有通信都限制在单个 ICI 域内。关键优化包括 FSDP + EP(专家并行)方案、FP8 量化、GMM v2 和 Splash attention 等内核优化,以及不规则排序操作。结果显示,不同芯片数量下的吞吐量提升了 82-60%,EMFU 介于 42.8% 至 28.9% 之间。这项工作表明,通过周密的系统设计和优化策略,Ironwood 能够有效处理大规模 MoE 工作负载。
目录 · 13 节
概述Overview
大型混合专家(MoE)模型会给训练系统的各个环节带来压力:计算、内存和互连。为了评估 Ironwood(TPU7x)——Google Cloud 第七代 TPU——处理这类工作负载的能力,我们对 DeepSeek-V3 671B 的预训练进行了基准测试。作为一款开放权重的行业标准模型,DeepSeek-V3 结合了多头潜在注意力(MLA)、256 个细粒度路由专家和多词元预测(MTP),体现了当今大规模 MoE 的架构需求。在固定模型、数据集、全局批次大小和目标验证损失的条件下,我们测量了训练耗时——即达到该目标所需的实际运行时间——分别使用 2,048、4,096 和 8,192 颗芯片。Large Mixture-of-Experts (MoE) models stress every part of a training system: compute, memory, and interconnect. To evaluate how Ironwood (TPU7x), Google Cloud's seventh-generation TPU, handles this class of workload, we benchmarked DeepSeek-V3 671B pre-training. As an open-weight, industry-standard model combining multi-head latent attention (MLA), 256 fine-grained routed experts, and multi-token prediction (MTP), DeepSeek-V3 reflects the architectural demands of today's large-scale MoEs. Holding the model, dataset, global batch size, and target validation loss fixed, we measured time to train—the wall-clock time to reach that target—at 2,048, 4,096, and 8,192 chips.
每颗 Ironwood 芯片可提供 4,614 TFLOP/s 的 FP8 算力,并配备 192 GiB HBM,带宽为 7.38 TB/s;单个 pod 可通过芯片间互连(ICI)连接多达 9,216 颗芯片。我们使用 8,192 颗芯片,在 1.70 分钟内训练了 DeepSeek-V3,所有通信均在一个 ICI 域内完成。使用 2,048 颗芯片时,最终结果的有效模型 FLOPs 利用率(EMFU)达到 56%。从 2,048 颗扩展到 4,096 颗芯片时,收敛扩展效率为 96%;扩展到 8,192 颗时为 74%。在内部运行中,我们还测得从 512 颗扩展到 2,048 颗芯片时,效率接近 100%;期间通过梯度累积保持全局批量大小不变。Each Ironwood chip delivers 4,614 TFLOP/s of FP8 compute and 192 GiB of HBM at 7.38 TB/s, and a single pod connects up to 9,216 chips over the inter-chip interconnect (ICI). We trained DeepSeek-V3 in 1.70 minutes on 8,192 chips, with all communication inside one ICI domain. At 2,048 chips, the final result reached an effective model FLOPs utilization (EMFU) of 56%. Convergence scaling efficiency was 96% from 2,048 to 4,096 chips and 74% from 2,048 to 8,192 chips. In internal runs, we also measured nearly 100% efficiency from 512 to 2,048 chips, using gradient accumulation to keep the global batch size the same.
我们通过两条工作流推进了这项工作。本文介绍第一条:FSDP + EP 路线。该路线构建了一个与参考训练配置相匹配的 MaxText 配方,将其扩展到 8,192 颗芯片,并开发了最终配置所依赖的许多功能:FP8 量化配方、用于分组矩阵乘法和令牌排序的 Pallas 内核、注意力优化、带无丢弃回退机制的 MoE 令牌分块、重计算与瓦片大小调优,以及运行前预热。这条路线无法隐藏所有集合通信。在 8,192 颗芯片上,每个设备上的批次较小,可用于与通信重叠的计算很少,因此 EP 的 reduce-scatter 仍然无法隐藏。批次拆分调度几乎可以隐藏所有这类集合通信,我们将在本系列的第二部分中于本月晚些时候分享相关细节。We pursued this work in two workstreams. This post covers the first, the FSDP + EP track that built a MaxText recipe matching the reference training setup, scaled it to 8,192 chips, and developed many of the features our final configuration relies on: the FP8 quantization recipe, Pallas kernels for grouped matmuls and token sorting, attention optimizations, MoE token chunking with dropless fallback, rematerialization and tile size tuning, and pre-run warm-up. This track could not hide every collective. At 8,192 chips, the small per-device batch leaves little compute to overlap with communication, so the EP reduce-scatter remains exposed. The batch-split schedule hides nearly all of these collectives, and we will share details about it in Part 2 of this series later this month.
本文介绍了我们如何在短短几周内,使用 MaxText 和 JAX 在 Google Cloud TPU 上扩展并优化一个拥有 6710 亿参数的混合专家(MoE)模型。对于每项优化,我们都会说明它解决的瓶颈、工作原理及带来的收益,最后总结这些经验对其他大型 MoE 模型的借鉴意义。This post explains how we scaled and optimized a 671B-parameter Mixture-of-Experts (MoE) model with MaxText and JAX on Google Cloud TPUs in a matter of weeks. For each optimization, we describe the bottleneck it addressed, how it works, and what it gained, and we close with the lessons that carry over to other large MoE models.
结果Results
我们在所有规模下均使用由 16,384 个长度为 4,096 个 token 的序列组成的全局批次进行训练,并在首次评估的对数困惑度达到或低于 3.6 时停止每次运行。在一周的优化期间(9 月 28 日至 10 月 5 日),FSDP + EP 路线在三个规模下均将每芯片吞吐量提升了 1.56–1.82 倍,同时保持模型、数据集、全局批次大小和目标损失不变。We trained with a global batch of 16,384 sequences of 4,096 tokens at every scale, and each run stops at the first evaluation at or below a log perplexity of 3.6. In one week of optimization, from September 28 to October 5, the FSDP + EP track raised throughput per chip by 1.56–1.82× at all three scales while keeping the model, dataset, global batch size, and target loss unchanged.
- 2,048 个芯片:约 2,057 → 3,736 tokens/s/chip(+82%),EMFU 为 42.8%2,048 chips: about 2,057 → 3,736 tokens/s/chip (+82%), with EMFU of 42.8%
- 4,096 颗芯片:约 1,971 → 3,084 个 token/秒/芯片(+56%),EMFU 为 34.9%4,096 chips: about 1,971 → 3,084 tokens/s/chip (+56%), with EMFU of 34.9%
- 8,192 个芯片:约从每芯片每秒 1,568 个 token 提升至 2,501 个(+60%),EMFU 为 28.9%8,192 chips: about 1,568 → 2,501 tokens/s/chip (+60%), with EMFU of 28.9%
在所有规模下,我们都将全局批次保持为 16,384 个序列,因此增加芯片数量会将相同的工作拆分得更细。批次由两个设置共同决定:每设备批次大小(PBS),即每个 JAX 设备在每个微步中处理的序列数;以及梯度累积(GA),即每个优化器步包含的微步数。每个 Ironwood 芯片由两个芯粒构成,每个芯粒包含一个 TensorCore、两个 SparseCore 和 96 GiB 的 HBM,并通过芯粒间(D2D)链路连接;该链路的速度是 1D ICI 链路的六倍。JAX 会将每个 TensorCore 暴露为一个设备,因此,芯片数 × 2 × PBS × GA = 16,384。我们在 512 到 8,192 个芯片的规模下,对 PBS 为 1、2 和 4 的配置进行了测试。We kept the global batch at 16,384 sequences at every scale, so adding chips divides the same work more finely. Two settings share the batch: the per-device batch size (PBS), the number of sequences each JAX device processes per micro-step, and gradient accumulation (GA), the number of micro-steps per optimizer step. Each Ironwood chip is built from two chiplets, each with one TensorCore, two SparseCores, and 96 GiB of HBM, joined by a die-to-die (D2D) link six times faster than a 1D ICI link. JAX exposes each TensorCore as a device, so chips × 2 × PBS × GA = 16,384. We swept PBS 1, 2, and 4 from 512 to 8,192 chips.
FSDP + EP 方案FSDP + EP recipe
一个 Ironwood pod 可通过 3D 环面连接多达 9,216 个芯片,每个芯片的双向 ICI 带宽为 1.2 TB/s。因此,单个切片可以将所有通信都保留在 ICI 上,而不必经过数据中心网络(DCN);DCN 为每个芯片提供 100 Gb/s 的带宽。为了充分利用这一点,我们的设备网格采用两个轴:大规模全分片数据并行(FSDP)和适度的专家并行(EP)。3D 环面使 FSDP 能够覆盖整个切片,但如果在 8,192 个芯片上仅使用 FSDP,那么每一层都要将全部 256 个专家的权重 all-gather 到每个芯片上。每个芯片的 token 也会被分散到各个专家,导致分布过于稀疏,使专家矩阵乘法受限于 HBM。此外,模型的大多数张量维度都无法被 16,384 整除,因此将每个权重都切分得如此细,会需要代价高昂的填充或重新布局。加入 EP 可以解决这两个问题。每个芯片只需收集自己负责的专家权重,从而按 EP 度数减少权重流量和 HBM 占用;同时,在 EP 组内汇集 token,也能让专家矩阵乘法保持计算受限。我们排除了张量并行(TP),因为它会降低较小专家维度上的算术强度(参见算术强度指南);也排除了流水线并行(PP),因为在这些规模下,其气泡开销超过了通信节省带来的收益。表 1 列出了在我们的设置中各个规模下的最佳配置。An Ironwood pod connects up to 9,216 chips in a 3D torus with 1.2 TB/s of bidirectional ICI bandwidth per chip, so a single slice can keep all of its communication on ICI rather than the data center network (DCN), which provides 100 Gb/s per chip. To take advantage of this, our device mesh uses two axes: heavy fully sharded data parallelism (FSDP) and modest expert parallelism (EP). The 3D torus lets FSDP span the whole slice, but pure FSDP at 8,192 chips would all-gather all 256 experts' weights onto every chip for every layer. Each chip's tokens would also be spread so thinly across the experts that the expert matmuls become HBM-bound. Also, most of the model's tensor dimensions don't divide evenly 16,384 ways, so splitting every weight that finely would require costly padding or relayouts. Adding EP fixes both problems. Each chip gathers only its own experts' weights, which cuts weight traffic and HBM footprint by the EP degree, and pooling tokens across the EP group keeps the expert matmuls compute-bound. We ruled out tensor parallelism (TP), which lowers arithmetic intensity on the small expert dimension (see the arithmetic intensity guide), and pipeline parallelism (PP), whose bubbles outweighed its communication savings at these scales. Table 1 lists the best configuration at each scale in our setting.
这两条轴在不同层级中承担不同的作用。具体细节会因尺度而略有不同,但从整体上看:The two axes play different roles in different layers. The details vary slightly by scale, but at a high level:
- 注意力。在注意力模块中,EP 轴承载批次,充当激活值的数据并行维度。注意力权重规模较小,因此在 4,096 和 8,192 个芯片配置中使用的 FSDP 规模为 1,024 时,每个 FSDP 分片都会很小,其 all-gather 将受延迟而非带宽限制。因此,我们只沿着低秩查询、低秩键值和嵌入维度对注意力权重进行分片,并且只跨规模小得多的 EP 轴分片,同时在 FSDP 上复制。Attention. In attention, the EP axis carries the batch, acting as data parallelism for activations. Attention weights are small, so at the FSDP size of 1,024 used at 4,096 and 8,192 chips, each FSDP shard would be tiny and its all-gather would be bound by latency rather than bandwidth. We therefore shard the attention weights, along their low-rank query, low-rank key-value, and embedding dimensions, only across the much smaller EP axis, and replicate them across FSDP.
- MoE 层。256 个路由专家沿 EP 轴拆分(EP 为 8 时每台设备分配 32 个专家,EP 为 16 时分配 16 个),每个专家的权重还会沿 FSDP 轴进一步分片,并在使用前通过 all-gather 汇集。EP 使用 all-gather 和 reduce-scatter,而不是 all-to-all。每个 EP 组内会对 tokens 执行 all-gather,每台设备运行本地专家,然后通过 reduce-scatter 将输出送回各自所属的设备。每个 token 只传输一份副本,并且仅在接收设备上复制;如果该设备托管了该 token 的多个 top-k 专家,则每个专家各复制一份。当 EP 组较小且映射到环形拓扑(一维环面)时,这种方法效率较高。由于无论路由情况如何,集合通信的数据量都固定,因此专家负载不均不会导致任何设备的传输成为拖后腿的长尾。MoE layers. The 256 routed experts are split across the EP axis (32 experts per device at EP 8, 16 at EP 16), and each expert's weights are further sharded across the FSDP axis and all-gathered before use. EP uses an all-gather and a reduce-scatter instead of all-to-all. Tokens are all-gathered within each EP group, each device runs its local experts, and the outputs are reduce-scattered back to their home devices. Each token is transferred as a single copy and replicated only on the receiving device, once for each of its top-k experts hosted there. This approach is efficient when the EP group is small and maps onto a ring (a 1D torus). Because the collective sizes are fixed regardless of routing, an imbalanced expert load cannot make any device's transfers a straggler.
- 评估集包含 1,024 个序列,少于设备数量,因此在评估期间,除 MoE 层外,EP 轴在所有位置都充当上下文并行,将每个序列拆分到 EP 组中的各个设备上。Evaluation. The evaluation set has 1,024 sequences, fewer than the number of devices, so during evaluation the EP axis acts as context parallelism everywhere except the MoE layer, splitting each sequence across the devices in an EP group.
强扩展性Strong scaling
在最多 2,048 个芯片的范围内,扩展性几乎呈线性。在图 2 中,每条 PBS 曲线都与虚线线性扩展线平行:在 PBS 固定时将芯片数翻倍,会使 GA 减半,并将运行速度提升 1.93–2.07 倍。图 2 中的线性拟合说明了原因。在 PBS 固定时,端到端耗时随 GA 呈直线变化(R² > 0.999)。其中大部分耗时来自微步操作,这部分随 GA 扩展;此外,每次运行还有固定的 15–30 秒用于每步执行一次的操作,例如梯度归约和优化器更新。由于在这些规模下,这部分固定耗时相对于端到端总耗时很小,因此 GA 减半几乎也会使总耗时减半。如表 2 所示,从 512 个芯片扩展到 2,048 个芯片时,最快配置(PBS 4)相对于 512 个芯片的扩展效率达到 97–100%,且每个芯片的耗时基本保持不变。Up to 2,048 chips, scaling is nearly linear. In Figure 2, each PBS line runs parallel to the dashed linear-scaling line: doubling the chips at a fixed PBS halves GA and speeds up the run by 1.93–2.07×. The linear fits in Figure 2 show why. At a fixed PBS, end-to-end time is a straight line in GA (R² > 0.999). Most of it is micro-step work, which scales with GA, plus a fixed 15–30 seconds per run of once-per-step work such as gradient reduction and the optimizer update. Because that fixed part is small next to end-to-end times at these scales, halving GA nearly halves the time. As Table 2 shows, the fastest configuration from 512 to 2,048 chips (PBS 4) reaches 97–100% scaling efficiency relative to 512 chips, at essentially constant chip time.
超过 2,048 个芯片后,已经没有可进一步减少的梯度累积了。在 2,048 个芯片、PBS 为 4 时,GA 已经是 1,因此芯片数每增加一倍,就必须将 PBS 减半:4,096 个芯片时降至 2,8,192 个芯片时降至 1。较小的 PBS 效率较低。在 512–2,048 个芯片的范围内,三种 PBS 值都适用;图 2 显示,在相同芯片数下,PBS 为 2 时比 PBS 为 4 慢 6–9%,PBS 为 1 时则慢 33–35%。因此,最快配置的扩展效率在 4,096 个芯片时降至 81%,在 8,192 个芯片时降至 66%(表 2)。8,192 个芯片时的大部分性能损失来自较小的 PBS,而不是芯片数增加:与 512 个芯片时 PBS 为 1 的运行相比,8,192 个芯片时 PBS 为 1 的运行仍达到线性扩展的 89%。要弥补其余损失,就需要在每个设备上计算量更少的情况下,将通信隐藏起来;第二部分重点介绍的批次拆分调度正是为此而设计。Past 2,048 chips, there is no gradient accumulation left to remove. At 2,048 chips with PBS 4, GA is already 1, so each further doubling of chips must halve the PBS instead: to 2 at 4,096 chips and to 1 at 8,192. Smaller PBS are less efficient. At 512–2,048 chips, where all three PBS values fit, Figure 2 shows PBS 2 running 6–9% slower than PBS 4 at the same chip count, and PBS 1 running 33–35% slower. As a result, the fastest configuration drops to 81% scaling efficiency at 4,096 chips and 66% at 8,192 (Table 2). Most of the loss at 8,192 chips comes from the smaller PBS, not from the extra chips: compared with PBS 1 at 512 chips, the PBS 1 run at 8,192 chips still reaches 89% of linear scaling. Recovering the rest requires hiding communication behind less compute per device, which is the focus of the batch-split schedule in Part 2.
优化Optimization
我们的工作分为两个阶段。首先,我们进行了屋顶线分析,结合 Ironwood 单芯片的计算能力、HBM 带宽和 ICI 带宽,评估了在 FSDP + EP 分片配置下,各个步骤部分所能达到的性能上限。随后,我们对步骤进行性能分析,找出实测时间与可达时间之间差距最大的部分,对其进行优化,然后再次测量。这些优化分为以下五类。We worked in two phases. First, a roofline analysis bounded what each part of the step could achieve under the FSDP + EP sharding, given Ironwood's per-chip compute, HBM bandwidth, and ICI bandwidth. Then we profiled the step, picked the largest gap between measured and achievable time, optimized it, and measured again. The optimizations fall into five categories, described below.
内核优化Kernel optimization
GMM v2。每个路由到的专家只处理分配给它的 token,因此专家层属于分组矩阵乘法(GMM),每个专家的行数会在每一步发生变化。从 Tokamax 的 ragged-dot 内核(v1)切换到基于 Tokamax GMM v2 构建的 MaxText GMM v2 内核后,端到端吞吐量提升了约 10%。ragged-dot 内核在固定网格上对行进行分块,因此,每个跨越专家边界的分块都会针对每个专家完整加载(DMA)并计算一次,然后再进行掩码处理。GMM v2 则从每个专家的首行开始分块,并根据有效行数调整内存加载和矩阵乘法的规模,从而避免了大部分此类浪费。GMM v2. Each routed expert processes only the tokens assigned to it, so the expert layers are grouped matrix multiplications (GMMs) whose per-expert row counts change at every step. Switching from Tokamax's ragged-dot kernel (v1) to MaxText's GMM v2 kernel, which is built on Tokamax GMM v2, improved end-to-end throughput by ~10%. The ragged-dot kernel tiles rows on a fixed grid, so each tile that straddles an expert boundary is loaded (DMA) and computed in full once per expert, then masked. GMM v2 starts tiles at each expert's first row and sizes both the memory loads and the matmul to the valid rows, which cuts most of that waste.
Splash Attention。两项改动减少了注意力计算中的无效工作:Splash attention. Two changes reduce wasted attention work:
- 对角线子块跳过。因果掩码会将对角线上每个块的上半部分置零,因此已执行的 QK 计算中有一部分被浪费:对于长度为 4,096 个 token 的序列,以及我们 8,192 芯片配置方案中大小为 2,048 个 token 的前向传播块,浪费的计算量占三分之一。将每个对角线块拆分为 4 × 4 的子块网格,并跳过其中为空的子块(sa_qk_diag_skip, sa_qk_diag_grid: 4),可将浪费降至 11%。Diagonal sub-tile skipping. A causal mask zeroes the upper half of every tile on the diagonal, so part of the visited QK work is wasted: with 4,096-token sequences and the 2,048-token forward-pass tiles of our 8,192-chip recipe, a third of it. Splitting each diagonal tile into a 4 × 4 grid of sub-tiles and skipping the empty ones (sa_qk_diag_skip, sa_qk_diag_grid: 4) cuts that waste to 11%.
- 跳过 segment ID。使用序列打包时,segment ID 会阻止 token 跨文档进行注意力计算,但代价是每个计算块都要额外进行向量单元运算。我们的 C4 数据流水线则将文档连接成一个以 EOS 分隔的流,不标记边界,因此每个序列都属于同一个 segment。由于此时 segment mask 不起作用,我们会跳过它(sa_ignore_segment_ids),以避免这部分开销。Skipping segment IDs. With sequence packing, segment IDs stop tokens from attending across documents, at the cost of extra vector-unit work on every computed tile. Our C4 data pipeline instead joins documents into one EOS-separated stream with no boundaries marked, so every sequence is a single segment. Since the segment mask is then a no-op, we skip it (sa_ignore_segment_ids) to avoid that overhead.
不规则排序。在专家 GMM 之前,每个设备会对路由到其本地专家的 token 进行排序;之后,再将它们恢复原顺序,应用路由权重,并对其本地专家的 top-k 输出求和(随后由 EP reduce-scatter 完成每个 token 的求和)。在 all-gather/reduce-scatter EP 路径中,每个设备首先会在 EP 组内对 token 执行 all-gather,但对于任意给定的 rank,只有动态确定的部分 token 会被路由到该 rank 上的专家,因此每个 rank 上的活跃 token 切片长度不规则。若没有不规则内核,排序和恢复原顺序操作就会对完整的 EP all-gather 缓冲区进行置换,移动的 token 数据量会比该 rank 实际需要的多 8 倍或 16 倍(即 EP 度数倍)。我们为这条路径在 MaxText 中集成了不规则 gather 和不规则 gather-reduce 内核:它们读取根据运行时各专家组大小计算出的 rank 起始和结束偏移,并使用间接 DMA 仅移动路由到其本地专家的 token。由于 gather 和 gather-reduce 互为转置,反向传播时可以复用这两个内核,只需交换它们的角色。Ragged sort. Before the expert GMMs, each device sorts the tokens routed to its local experts; afterward, it unsorts them, applies the routing weights, and sums the top-k outputs from its local experts (the EP reduce-scatter then completes each token's sum). In the all-gather/reduce-scatter EP path, each device first all-gathers tokens across the EP group, but only a dynamically determined subset of them is routed to the experts on any given rank, so each rank's active token slice is ragged. Without a ragged kernel, the sort and unsort permute the full EP-all-gathered buffer, moving 8× or 16× (the EP degree) more token data than the rank needs. We integrated ragged gather and ragged gather-reduce kernels into MaxText for this path: they read the rank's start and end offsets, computed from the per-expert group sizes at runtime, and use indirect DMA to move only the tokens routed to its local experts. Because gather and gather-reduce are each other's transpose, the backward pass reuses the same two kernels with their roles swapped.
FP8 量化FP8 quantization
FP8 是最大的单项收益:在 512 个芯片、PBS=1 时,它将步进时间缩短了 31%,从 4.375 秒降至 3.015 秒(图 4)。Ironwood 在其矩阵单元中原生运行 FP8:每个芯片的 FP8 峰值性能为 4,614 TFLOP/s,是其 BF16 性能(2,307 TFLOP/s)的两倍;而 FP8 张量在 HBM 和 ICI 中所占字节数只有 BF16 的一半。我们的方案同时利用了这两点。更快的矩阵乘法对收益的贡献较小:将矩阵乘法切换为 FP8 只节省了 4.7%,因为矩阵乘法仅占每个步骤的一部分,而且量化会在矩阵单元之外增加额外工作。即使计入 FP8 splash attention,计算也只占总节省量的 19%。约三分之二(68%)的收益来自量化集合通信功能。其中大部分来自量化权重 all-gather,它将专家权重梯度 all-reduce 转换为 reduce-scatter;量化 token all-gather 和反向预量化则通过发送 FP8 负载贡献了其余收益,这使传输字节数减半,并避免了下游重新量化。静态缩放和权重布局贡献了剩余的 13%。FP8 was the largest single win: on 512 chips with PBS=1, it cuts step time by 31%, from 4.375 s to 3.015 s (Figure 4). Ironwood runs FP8 natively in its matrix units: each chip peaks at 4,614 TFLOP/s in FP8, twice its 2,307 TFLOP/s in BF16, and FP8 tensors take half the bytes of BF16 in HBM and over ICI. Our recipe takes advantage of both. Faster matmuls were the smaller part of the gain: switching the matmuls to FP8 saved only 4.7%, because matmuls are only part of each step and quantization adds work outside the matrix units. Even with FP8 splash attention included, compute accounts for only 19% of the total saving. About two-thirds (68%) came from quantized-collective features. Most came from the quantized weight all-gather, which turns the expert weight-gradient all-reduce into a reduce-scatter; the quantized token all-gather and backward pre-quantization add the rest by sending FP8 payloads, which halves their bytes and avoids re-quantizing downstream. Static scaling and weight layout account for the remaining 13%.
每项特性带来的收益取决于配置:我们的 2,048–8,192 芯片方案采用的 FSDP 轴比这项消融实验更宽;而在 2,048 和 4,096 芯片配置下,PBS 更大,这会改变暴露出的集合通信操作,以及每个训练步中用于矩阵乘法的时间占比。因此,在这些规模下,计算和通信方面的节省比例可能有所不同。Per-feature gains are config-dependent: our 2,048–8,192-chip recipes use a wider FSDP axis than this ablation, and at 2,048 and 4,096 chips a larger PBS, which changes which collectives are exposed and how much of the step is spent in matmuls. The split between compute and communication savings can therefore differ at those scales.
图 5 展示了路由式 MoE 层如何使用 FP8。在前向传播中,每个设备都会在 FSDP 全聚合(量化权重全聚合,QWAG)之前,将自己的权重分片量化为 E4M3;并在 EP 全聚合(量化 token 全聚合,QTAG)之前量化自己的 token。这两种集合通信传输的数据量都减半。非规则排序传输 FP8 行,而上投影 GMM 两侧都直接使用 FP8,无需重新量化。下投影的输入,即 SwiGLU 的 BF16 输出,则在 GMM 内核中量化。在反向传播中,combine 的梯度会先使用固定缩放因子量化为 E5M2,再进行 EP 全聚合,因此全聚合和逆排序(非规则聚合)也都传输 FP8。路由器仍使用 FP32,权重梯度的 reduce-scatter 则仍使用 BF16。Figure 5 shows how a routed MoE layer uses FP8. In the forward pass, each device quantizes its weight shard to E4M3 before the FSDP all-gather (quantized weight all-gather, QWAG) and its tokens before the EP all-gather (quantized token all-gather, QTAG). Both collectives move half the bytes. The ragged sort moves FP8 rows, and the up-projection GMM consumes FP8 on both sides without re-quantizing. The down-projection input, the BF16 output of SwiGLU, is quantized inside the GMM kernel. In the backward pass, the gradient of the combine is quantized to E5M2 with a fixed scale before the EP all-gather, so the all-gather and unsort (a ragged gather) also move FP8. The router stays in FP32, and the weight-gradient reduce-scatter stays in BF16.
静态缩放因子速度快,但需要谨慎使用。动态缩放会在每一步计算每个张量的绝对最大值,这会增加一次归约操作;对于分片张量,还会增加一次跨设备集体通信。静态缩放省去了这些操作,并且允许每个设备在执行 all-gather 之前,将自己的分片转换为 FP8,而无需先就全局最大值达成一致。代价是需要调参。我们根据使用动态缩放的运行结果校准了反向传播的取值范围,并针对每个缩放因子分别进行调优。为保证数值稳定性,路由器和门控 logits 保持为 FP32,LM head 保持为 BF16。Static scales are fast, but they need care. A dynamic scale computes the absolute maximum of each tensor on every step, which adds a reduction and, for sharded tensors, a cross-device collective. Static scales remove that work, and they let each device cast its own shard to FP8 before an all-gather without first agreeing on a global maximum. The cost is tuning. We calibrated the backward bounds from runs with dynamic scales and tuned them per scale. For numerical stability, the router and gate logits stay in FP32, and the LM head stays in BF16.
分块策略Chunking strategy
Token 分块。标志 num_moe_token_chunks 会沿序列维度将路由 MoE 层的 token 拆分为 N 个块。每个块都会执行完整流程(token 全收集、top-k、排序、GMM、反排序、规约散布),因此路由缓冲区和中间激活只需一次容纳一个块。分块还为 XLA 提供了机会,使其能够将一个块的 token 通信与另一个块的专家计算重叠起来。Token chunking. The flag num_moe_token_chunks splits a routed MoE layer's tokens into N chunks along the sequence. Each chunk runs the full path (token all-gather, top-k, sort, GMMs, unsort, reduce-scatter), so the routing buffers and intermediate activations only need to hold one chunk at a time. Chunking also gives XLA the opportunity to overlap one chunk's token communication with another chunk's expert compute.
非规整缓冲区系数。每台设备都需要一个缓冲区,用于存放路由到其专家的 token。负载均衡时,需要的缓冲区大小为(经过 all-gather 的 token 数 / EP)× top_k 行。最坏情况下,每个 token 都会将其所有副本发送给同一台设备上的专家,此时所需缓冲区是均衡负载下的 EP 倍:EP 为 8 时是 8 倍,EP 为 16 时是 16 倍。为每一层都预留这么多 HBM 并不现实,因此 ragged_buffer_factor 会在两者之间调整缓冲区大小,以便在 HBM 占用和溢出概率之间权衡。与原始 DeepSeek-V3 训练一样,我们的方案采用 dropless 策略,因此发生溢出时绝不能丢弃 token。我们添加了两种回退模式,可通过 moe_dropless_fallback 选择。Ragged buffer factor. Each device needs a buffer for the tokens routed to its experts. A balanced load needs (all-gathered tokens / EP) × top_k rows. In the worst case, every token sends all of its copies to the experts on one device, which needs an EP times the balanced buffer: 8× at EP 8 and 16× at EP 16. Reserving that much HBM for every layer is not affordable, so ragged_buffer_factor sizes the buffer between the two, trading HBM against the chance of overflow. Like the original DeepSeek-V3 training, our recipe is dropless, so an overflow must never drop tokens. We added two fallback modes, selected with moe_dropless_fallback.
调优Tuning
在 2,048 个芯片、PBS 为 4 时,HBM 使用率已约为 90%,因此该配置采用全量重物化。4,096 和 8,192 个芯片的配置采用自定义重物化策略(remat_policy: custom),将选定的张量保留在 HBM 中,这样反向传播时便可读取这些张量,而无需重新计算:At 2,048 chips with a PBS of 4, HBM usage is already about 90%, so that recipe uses full rematerialization. The 4,096- and 8,192-chip recipes use a custom rematerialization policy (remat_policy: custom) that keeps selected tensors in HBM, so the backward pass reads them instead of recomputing them:
- 层输入:每个解码器层的输入。Layer inputs: the input to each decoder layer.
- 注意力:MLA 的低秩和完整查询、键和值投影;带有其 log-sum-exp 的注意力输出;以及输出投影。Attention: the low-rank and full query and key, value projections of MLA; the attention output with its log-sum-exp; and the output projection.
- MoE:路由映射,包括 top-k 专家 ID、ragged-sort 排列、排序后的 token 索引和组大小。MoE: the routing maps, which are the top-k expert IDs, ragged-sort permutations, sorted token indices, and group sizes.
我们还针对每种规模分别调整了 Flash Attention 和 GMM v2 内核的分块大小。例如,上投影前向计算的分块行数从 2,048 颗芯片时的 512 行,降至 4,096 和 8,192 颗芯片时的 256 行;上投影权重梯度计算的分块行数则从 1,024 行降至 512 行。评估阶段会将每个序列拆分到多颗芯片上,在 8,192 颗芯片时也使用更小的注意力块:256 个 token,而非 512 个。We also tuned the tile sizes of the flash attention and GMM v2 kernels separately at each scale. For example, up-projection forward tiles drop from 512 rows at 2,048 chips to 256 rows at 4,096 and 8,192 chips, and up-projection weight-gradient tiles drop from 1,024 to 512 rows. Evaluation, which splits each sequence across chips, also uses smaller attention blocks at 8,192 chips: 256 tokens instead of 512.
热身Warm-up
我们从训练开始时起测量训练耗时,因此第一步速度较慢会直接计入其中。在 8,192 个芯片上,我们的第一步耗时约 55 秒,而稳定运行时约为 4 秒,其中大部分时间都花在等待第一个训练批次上。两个主要原因尤为突出:We measure time to train from the start of training, so a slow first step counts directly toward it. On 8,192 chips, our first step took about 55s, compared with about 4s at steady state, and most of that was spent waiting for the first training batch. Two causes stood out:
- 数据布局。TFDS 在执行带种子的文件乱序之前就将文件分配给主机,因此主机会读取不需要的文件。我们将数据集的文件数从 2,048 增至 16,384;在 8,192 个芯片的配置下,每个设备对应一个文件,因此每台主机只读取其自身设备对应的文件。Data layout. TFDS assigns files to hosts before applying the seeded file shuffle, so hosts read files they don't need. We resharded the dataset from 2,048 to 16,384 files, one per device at 8,192 chips, so each host reads only the files for its own devices.
- 编译。计时开始后,训练和评估程序才进行编译。现在,我们会在训练开始前编译这两个程序,并在合成数据上各运行一次,因此计时开始前不会读取任何训练或评估数据。Compilation. The train and eval programs compiled after the clock started. We now compile both and run them once on synthetic data before training begins, so no training or evaluation data is read before the clock starts.
摘要Summary
一周内,FSDP + EP 路线将一个与参考结果匹配的 MaxText 配置方案,改进为可扩展至 8,192 个 Ironwood 芯片的方案;在单个 ICI 域内完成的每次运行中,每芯片吞吐量都达到初始值的 1.56–1.82 倍。在 2,048 个芯片时,该方案达到 3,736 tokens/s/chip,EMFU 为 42.8%;从 512 个扩展到 2,048 个芯片时,扩展效率接近 100%,同时通过梯度累积保持全局批次大小不变。超过 2,048 个芯片后,效率有所下降,在 8,192 个芯片时降至 66%,主要原因是每个设备的批次缩小了:当 PBS 固定为 1 时,8,192 个芯片仍达到从 512 个芯片线性扩展理论值的 89%。In one week, the FSDP + EP track turned a reference-matching MaxText recipe into one that scales to 8,192 Ironwood chips at 1.56–1.82x its starting throughput per chip with every run inside a single ICI domain. At 2,048 chips, it reached 3,736 tokens/s/chip with an EMFU of 42.8%, and scaling from 512 to 2,048 chips was nearly 100% efficient, using gradient accumulation to keep the global batch size the same. Past 2,048 chips, efficiency fell to 66% at 8,192 chips, mostly because each device's batch shrank: at a fixed PBS of 1, 8,192 chips still reached 89% of linear scaling from 512 chips.
以下五条经验同样适用于其他大型 MoE 模型:Five lessons carry over to other large MoE models:
- 选择与规模相匹配的分片策略。我们在单个 ICI 域内采用高强度 FSDP,并搭配适度的 EP(8–16)。EP 汇集 token,使每个专家的矩阵乘保持计算受限,同时减少每个芯片需要收集的专家权重。我们排除了 TP,因为它会降低 2,048 宽专家的算术强度;也排除了 PP,因为在这些规模下,流水线气泡造成的损失超过了它带来的节省。Choose a sharding strategy that fits the scale. We paired heavy FSDP across the single ICI domain with modest EP (8–16). EP pools tokens so each expert matmul stays compute-bound, and it cuts the expert weights each chip gathers. We ruled out TP, which lowers arithmetic intensity on the 2,048-wide experts, and PP, whose bubbles outweighed its savings at these scales.
- 量化之前先找出瓶颈。在我们的 512 芯片 FP8 消融实验(PBS 1、EP 16)中,由于这一步受通信限制,FP8 带来的收益约有三分之二来自量化集合通信,而非更快的矩阵乘法。收益如何分配取决于这一步中通信与矩阵乘法各自占用的时间,而这种平衡会随着批量大小和分片方式而变化。Identify the bottleneck before quantizing. In our 512-chip FP8 ablation (PBS 1, EP 16), where the step was communication-bound, about two-thirds of FP8's gain came from quantized collectives rather than faster matmuls. How the gain splits depends on how much of a step goes to communication versus matmuls, and that balance shifts with batch size and sharding.
- 使用能够容纳的最大 PBS,并通过梯度累积达到全局批次大小。每个序列的累积开销很小:每个微步执行的工作相同,此外只增加少量每步执行一次的工作,例如梯度归约和优化器更新。缩小 PBS 的代价更高,因为每个微步仍然要传输相同的权重,却只有更少的计算可以用来掩盖这部分通信开销。Use the largest PBS that fits, and reach the global batch with gradient accumulation. Accumulation adds little cost per sequence: each micro-step does the same work, and only a small amount of once-per-step work, such as gradient reduction and the optimizer update, sits on top. Shrinking the PBS costs more, because each micro-step still moves the same weights but has less compute to hide that communication behind.
- 围绕全局批次规划芯片数量。全局批次固定为 16,384 条序列时,每台设备都能将每个微步处理 4 条序列这一最高效的工作负载保持到 2,048 个芯片。超过这个数量后,每台设备每个微步处理的序列会减少,扩展能力取决于能否用更少的计算来掩盖通信开销。Plan the chip count around the global batch. With a fixed global batch of 16,384 sequences, each device can keep its most efficient workload of 4 sequences per micro-step up to 2,048 chips. Beyond that, each device gets fewer sequences per micro-step, and scaling depends on hiding communication behind less compute.
- 让无丢弃路由成本低,而非毫无成本。使用带回退机制的限容量缓冲区,可以避免在每一层都预留最坏情况下所需的内存,同时确保永不丢弃 token。应根据溢出发生的频率选择回退方案:溢出罕见时,重放整个步骤的成本最低;溢出频繁时,逐层采用无丢弃路径更合适。Make dropless routing cheap, not free. A capped buffer with a fallback avoids reserving worst-case memory in every layer while never dropping a token. Choose the fallback by how often overflows happen: replaying the whole step is cheapest when they're rare, and a per-layer dropless path is better when they're frequent.
这项工作的许多成果沿用到了我们的最终配置中。但这条路线无法解决的是在 8,192 个芯片规模下规模庞大、无法掩藏的 EP reduce-scatter;此时每个设备可用于掩藏它的计算量最少。第 2 部分将介绍一种批次拆分调度方案,几乎可以掩藏这部分全部开销。Much of this work carried into our final configuration. What this track could not solve was the large, exposed EP reduce-scatter at 8,192 chips, where each device has the least compute to hide it behind. Part 2 describes the batch-split schedule that hides nearly all of it.
自己复现一下Reproduce it yourself
这三种规模(2,048、4,096 和 8,192 颗 Ironwood 芯片)的配置方案均可在 MaxText 仓库的 benchmarks/deepseek3_671b_tpu7x 目录中找到。每份方案都包含本文中使用的完整配置,方便你复现我们的结果,或以此为起点构建自己的 MoE 模型。The recipes for all three scales (2,048, 4,096, and 8,192 Ironwood chips) are available in the MaxText repository at benchmarks/deepseek3_671b_tpu7x. Each recipe includes the full configuration used in this post, so you can reproduce our results or use them as a starting point for your own MoE models.
致谢Acknowledgements
这项优化工作是工程团队与管理团队通力合作的成果。我们谨向以下为推动这一计划取得成功作出贡献的同事表示感谢:This optimization work was a massive collaborative effort across engineering and management teams. We would like to express our gratitude to the following contributors who drove this initiative to success:
Ran Ran、Matt Davidow、Nuojin Cheng、Shuning Jin、Suraj Kolla、Zhengxian He、Daniel Mandragona、Shuwen Fang、Snehal Verma、Amanda Tomlinson、Burkay Donderici、Kevin Wang、Jesse Lu、Caitlin Stanton、Mohamed Omar、Vipul Aneja、Cory Kleinheksel、Xiongfei Wei、Ani Udipi、Vaibhav Singh、Jet Jiang、Andi Gavrilescu、Durlabh Malik、Deepak Patil、Carlos Araya。Ran Ran, Matt Davidow, Nuojin Cheng, Shuning Jin, Suraj Kolla, Zhengxian He, Daniel Mandragona, Shuwen Fang, Snehal Verma, Amanda Tomlinson, Burkay Donderici, Kevin Wang, Jesse Lu, Caitlin Stanton, Mohamed Omar, Vipul Aneja, Cory Kleinheksel, Xiongfei Wei, Ani Udipi, Vaibhav Singh, Jet Jiang, Andi Gavrilescu, Durlabh Malik ,Deepak Patil, Carlos Araya.