PyTorch发布Zephon实现弹性确定性数据加载
Bringing Elastically Deterministic Data Loading to TorchTitan with Zephon
做大规模训练的同学必看,解决了GPU扩缩容导致实验不可复现的核心痛点,直接提升消融实验的可信度。
Why Zephon?
为什么选择 Zephon?
The data that a model sees during training is one of the most powerful levers we have for influencing how well it performs. Much of the day-to-day work of improving a model happens on the data side: we change the mixture, or the filters that we apply, or how we tokenize and pack the samples, and then we run an ablation experiment to see whether the change helped. The first problem with this is that each of these changes often requires us to do a full pass over our training corpus and run expensive offline operations to prepare the data and store it on disk before our ablation experiment can run. Since a typical training run reads through that copy exactly once, and because most experiments don’t end up producing a better model, we spend a lot of time and storage creating datasets that will never be read again.
模型在训练期间看到的数据是我们影响其性能的最强大杠杆之一。改进模型的日常工作大多发生在数据层面:我们调整混合比例、应用的过滤器,或者样本的分词和打包方式,然后运行消融实验以观察这些更改是否带来了帮助。这样做面临的首要问题是,每一项更改通常都需要我们对整个训练语料库进行一次完整遍历,并执行昂贵的离线操作来准备数据并将其存储在磁盘上,之后才能运行消融实验。由于典型的训练过程会恰好读取该副本一次,且大多数实验最终并未产生更好的模型,因此我们花费了大量时间和存储空间去创建那些永远不会再被读取的数据集。
This matters even more now that experiments can be designed, launched, and interpreted by agents instead of people. Agents are great for running lots of experiments in parallel, but they aren’t especially good at noticing when a result actually depends on something other than the change they made, like the number of GPUs that an experiment happened to run on (in fairness to the agents, humans aren’t great at noticing this either). And with most data loaders, the number of GPUs does change the result, because it changes the data order.
随着实验可以由智能体(agents)而非人类来设计、启动和解读,这一问题显得更为重要。智能体非常擅长并行运行大量实验,但它们并不特别擅长察觉结果实际上是否依赖于除其所做更改之外的其他因素,例如实验恰好运行的 GPU 数量(公平地说,人类也不擅长察觉这一点)。而在大多数数据加载器中,GPU 数量的变化确实会影响结果,因为它会改变数据的顺序。
To illustrate this, we train a 1B-parameter model in TorchTitan on 16 GPUs, take a checkpoint at step 150, and then resume from that checkpoint on 8, 32, and 64 GPUs alongside the original run, once with a standard data loader and once with Zephon (think of it as the run hitting a hardware fault and coming back on whatever set of GPUs happened to be free). As seen in Figure 1, with the standard data loader, changing the GPU count means that the data loader hands the model a different sequence of samples after the resume, and so each one ends up with a distinct loss curve. With Zephon, the curves lie on top of each other, and the SHA-256 hash of every global batch matches the baseline run.
为了说明这一点,我们在 TorchTitan 上使用 16 块 GPU 训练一个 1B 参数的模型,在第 150 步时保存检查点,然后从该检查点恢复,分别在 8、32 和 64 块 GPU 上继续运行,并与原始运行同时进行;其中一次使用标准数据加载器,另一次使用 Zephon(你可以将其想象为硬件故障后,在任何可用的 GPU 集合上恢复运行)。如图 1 所示,使用标准数据加载器时,更改 GPU 数量意味着数据加载器在恢复后向模型提供了不同的样本序列,因此每个实验最终都会得到一条独特的损失曲线。而在使用 Zephon 时,这些曲线完全重合,且每个全局批次的 SHA-256 哈希值都与基线运行匹配。
[Figure 1: Loss curves after resuming on 8, 32, and 64 GPUs from a 16-GPU checkpoint at step 150, standard loader vs. Zephon, with a barcode comparing the per-step SHA-256 of each global batch against the original run.]
[图 1:从第 150 步的 16 GPU 检查点在 8、32 和 64 GPU 上恢复后的损失曲线,对比标准加载器与 Zephon,并使用条形码比较每个全局批次每一步的 SHA-256 值与原始运行的差异。]
To get a sense for how much this matters, we train the same 1B-parameter model from scratch for 20B tokens on 8, 16, 32, and 64 GPUs using a standard data loader. We show the scores per GPU count in Figure 2. The model’s DCLM Core v1 score varies by up to 0.82 points depending on how many GPUs it was trained on, and its score based on the FineWeb evaluation suite by up to 0.64 points. That is roughly four times the relevant thresholds that the DCLM and FineWeb authors used when evaluating data curation methods, which means that an ablation comparing two mixtures on two different allocations could easily be measuring the GPU count instead of the data. With Zephon, the spread across the different GPU counts was only 0.05 and 0.011 points, respectively.
为了了解其重要性有多大,我们使用标准数据加载器在 8、16、32 和 64 块 GPU 上从头开始训练同一个拥有 1B 参数的模型,训练数据量为 20B tokens。图 2 展示了不同 GPU 数量下的得分情况。模型的 DCLM Core v1 得分根据训练所用 GPU 数量的不同,最高相差 0.82 分;基于 FineWeb 评估套件得出的得分最高相差 0.64 分。这大约是 DCLM 和 FineWeb 作者在评估数据整理方法时所使用的相关阈值的四倍,这意味着对比两种混合数据在不同分配方案下的消融实验,很容易实际上是在测量 GPU 数量而非数据本身的影响。而使用 Zephon 时,不同 GPU 数量下的得分差异分别仅为 0.05 分和 0.011 分。
[Figure 2: Downstream evaluations at 8–64 GPUs on DCLM Core v1 and FineWeb with non-elastically deterministic (standard) data loading and Zephon. Grey bands mark the eval noise floor.]
[图 2:在 DCLM Core v1 和 FineWeb 上,使用非弹性确定性(标准)数据加载与 Zephon 在 8–64 块 GPU 上的下游评估结果。灰色带状区域表示评估噪声底限。]
Note that Zephon does all of this while doing the data prep work on the fly: it tokenizes, packs, shuffles, and mixes the data inside of the training job, taking advantage of all of those CPUs sitting around with nothing to do while the model trains, which means there’s no preprocessed copy of the corpus to create (or to throw away when the experiment doesn’t pan out).
需要注意的是,Zephon 在执行上述操作的同时,还实时进行数据准备工作:它在训练任务内部对数据进行分词、打包、打乱和混合,充分利用了模型训练期间闲置的 CPU 资源,这意味着无需创建语料的预处理副本(也不会在实验未达预期时将其丢弃)。
A Zephon Pipeline
一个 Zephon Pipeline
Zephon asks you to describe three things: the Datasets you want to train on, a WorkSource that decides how those datasets are mixed and ordered into a training curriculum, and a Pipeline of operators that turn the samples in their stored representations into the tensors your training loop consumes. Here’s an example that mixes two text datasets directly from Hugging Face, then tokenizes, packs, shuffles, and batches the result:
Zephon 要求你描述三件事:你想要训练的 Datasets(数据集)、决定这些数据集如何混合并排序为训练课程表的 WorkSource(工作源),以及一组将存储表示中的样本转换为训练循环所消耗张量的 Pipeline(流水线)算子。以下是一个示例,它直接从 Hugging Face 混合两个文本数据集,然后对结果进行分词、打包、打乱和批处理:
from zephon import Pipeline
from zephon.io import Dataset
from zephon.work import StaticMixtureWorkSource, TokenEstimation
source = StaticMixtureWorkSource(
datasets=[
Dataset.from_path("wiki", "hf://Salesforce/wikitext/wikitext-2-raw-v1/train"),
Dataset.from_path("news", "hf://fancyzhx/ag_news/default/train"),
],
mixture={"wiki": 0.5, "news": 0.5}, # a token ratio, not a sample ratio
token_estimation=TokenEstimation(),
seed=42,
)
pipeline = (
Pipeline(source)
.options(io_options={"cache": {"enabled": True}})
.tokenize(tokenizer_id="gpt2", field="text", parallelism=4)
.pack_flat(max_length=2048, algorithm="wrap")
.shuffle(buffer_size=1024, seed=42)
.batch(microbatch_size=8)
)
for batch in pipeline:
train_step(batch.to_training())The WorkSource plans the curriculum from each Dataset’s metadata and is kept separate from the operators that actually process those samples so you can change the mixture without touching the processing pipeline and vice-versa. In order to ensure we get the mixture percentages correct on a token basis, we include a TokenEstimation utility that measures the token/byte ratio for a small sample of each dataset and adjusts the plan to target the mixing proportions on a token-level even though the samples are stored as raw strings.
WorkSource 会根据每个 Dataset 的元数据规划课程表,并将其与实际处理这些样本的算子分离开来,这样你就可以在不触碰处理流水线的情况下更改混合比例,反之亦然。为了确保我们在 token 级别上获得正确的混合百分比,我们包含了一个 TokenEstimation(Token 估算)工具,该工具会测量每个数据集小样本的 token/byte 比率,并调整计划以在 token 层面实现目标混合比例,尽管样本是以原始字符串形式存储的。
Each of the individual Pipeline operators can have its own level of parallelism; here, tokenization gets four workers because it’s the most expensive step. Parallelizing the preprocessing operators is how Zephon keeps the GPUs fed, even on more complex pipelines: on a full vision-language pipeline that decodes images, tokenizes, packs, and batches its samples, Zephon keeps pace with Google Grain, the fastest loader we tested, while offering the stronger elastic determinism guarantees.
每个独立的 Pipeline 算子都可以拥有自己的并行度级别;在此,分词步骤分配了四个工作进程,因为它是计算成本最高的步骤。通过并行化预处理算子,Zephon 能够确保持续为 GPU 提供数据,即使在更复杂的 Pipeline 中也是如此:在一个包含图像解码、分词、打包和样本批处理的全视觉-语言 Pipeline 中,Zephon 的吞吐量与 Google Grain(我们测试过的最快加载器)持平,同时提供了更强的弹性确定性保证。
[Figure 3: Usable (non-padding) tokens per second per GPU on a full vision-language pipeline as parallelism increases, for Zephon, Grain, and a quickly hacked-together internal loader we used internally before building Zephon. The horizontal line is the throughput needed to keep the GPUs from stalling.]
[图 3:在全视觉-语言 Pipeline 中,随着并行度增加,每张 GPU 每秒可使用的(非填充)token 数量,对比 Zephon、Grain 以及我们在构建 Zephon 之前内部快速拼凑的一个内部加载器。水平线表示防止 GPU 停滞所需的吞吐量。]
How Zephon Keeps the Batches Identical
Zephon 如何保持批次的一致性
Packing and mixing data on the fly is what makes elastic determinism hard. When a data loader tokenizes documents, packs them together into fixed-length sequences, and mixes several datasets together as it goes, the order of the training data is no longer something that you can look up at an index on disk (we call this stateful n:m pipelines in our paper). In this case, it depends on the state of the pipeline when and how a sample is used, and in most data loaders that state can’t be transferred to a different number of GPUs without changing the output. Existing loaders get part of the way there: Mosaic Streaming and Google Grain can keep the global batches the same across GPU counts, but only when the order of samples can be worked out ahead of time from an index, which rules out things like packing on the fly. Megatron-Energon can reproduce its batches on a different number of GPUs, but only as long as the total number of data loader workers across all of the GPUs stays the same.
动态进行数据打包和混合是导致弹性确定性难以实现的原因。当数据加载器对文档进行分词、将它们打包成固定长度的序列,并在过程中混合多个数据集时,训练数据的顺序就不再是一个可以通过磁盘索引查找确定的内容(我们在论文中将此称为有状态 n:m Pipeline)。在这种情况下,它取决于在特定时刻使用样本时 Pipeline 的状态,而在大多数数据加载器中,这种状态无法在不改变输出的情况下转移到不同数量的 GPU 上。现有的加载器部分实现了这一目标:Mosaic Streaming 和 Google Grain 可以在不同 GPU 数量下保持全局批次一致,但这仅适用于样本顺序可以提前从索引中推导出来的情况,这排除了动态打包等场景。Megatron-Energon 可以在不同数量的 GPU 上复现其批次,但前提是跨所有 GPU 的数据加载器工作进程总数保持不变。
Zephon allows for elastic determinism without these constraints with three design decisions that build on each other.
Zephon 通过三个相互支撑的设计决策,在无需这些约束的情况下实现了弹性确定性。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力