# 弹性训练

弹性训练是一种分布式机器学习技术，训练任务在运行过程中调整其工作节点数量：当节点故障或被抢占时收缩，当容量恢复时增长，而不是直接终止并从固定分配重新启动。每个存活工作节点上的模型副本保持不变；变化的是参与下一次集合通信操作的工作节点数量，以及训练数据在这些节点之间的划分方式。<sup>[1](https://scalablebook.icsgen-ai.org/part-4-parallel-deep-learning/module-18-elastic-fault-tolerant-training/section-18.4.html)</sup> 它与容错密切相关——容错是指在工作任务运行期间检测并替换故障节点而无需重启的能力。<sup>[2](https://aws.amazon.com/blogs/containers/fault-tolerant-distributed-machine-learning-training-with-the-torchelastic-controller-for-kubernetes/)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心机制 | 成员变更时，存活的工作节点被停止，新的工作节点组被组建，工作节点以新的 `RANK` 和 `WORLD_SIZE` 分配重新启动。<sup>[3](https://harvard-edge.github.io/cs249r_book_dev/vol2/07_fault_tolerance/07_fault_tolerance.html)</sup> |
| 工作节点数量边界 | 任务在最小和最大工作节点数量之间运行；PyTorch Elastic 允许工作节点在任意时刻加入或离开，只要数量保持在启动时设定的边界内。<sup>[4](https://github.com/pytorch/elastic/blob/master/design/torchelastic/0.2.0/design_doc.md)</sup> |
| 扩缩容单位 | 扩缩容通过增加和移除数据并行副本来实现；SageMaker HyperPod 弹性训练在不同规模下保持全局批大小不变并调整学习率。<sup>[5](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-eks-elastic-training.html)</sup><sup> • </sup><sup>[6](https://aws.amazon.com/blogs/aws/introducing-checkpointless-and-elastic-training-on-amazon-sagemaker-hyperpod/)</sup> |
| Horovod 超时 | 如果可用槽位低于最小值，Elastic Horovod 任务会暂停，直到有主机出现或 `HOROVOD_ELASTIC_TIMEOUT`（默认 600 秒）超时。<sup>[7](https://horovod.readthedocs.io/en/stable/elastic_include.html)</sup> |
| 报告的利用率提升 | EasyScale 部署在一个在线服务集群中，通过让训练任务机会性地使用空闲 GPU，将集群整体利用率提升了 62.1%。<sup>[8](https://arxiv.org/abs/2208.14228)</sup> |
| 已知局限 | PyTorch Distributed Elastic 仅调整数据并行度；在其他并行策略之间重新配置检查点需要 Universal Checkpointing 等后续系统。<sup>[9](https://www.usenix.org/system/files/atc25-lian.pdf)</sup> |

## 固定规模任务的不足

传统的分布式训练任务会预先获取所有节点，一旦某个节点故障或被回收就无法继续。TorchElastic Controller 的文档列出了由此产生的局限：任务无法在不获取所有请求节点的情况下启动，无法在节点故障后继续运行，无法纳入后来加入的节点，也无法在存在慢节点或卡死节点的情况下取得进展。<sup>[10](https://github.com/pytorch/elastic/blob/master/design/kubernetes/torchelastic-operator-design.md)</sup> 在混合了训练、推理和实验的集群上，加速器的可用性全天都在不断变化，而传统工作负载仍然锁定在初始分配中，无法在无人工干预的情况下使用空闲加速器。<sup>[6](https://aws.amazon.com/blogs/aws/introducing-checkpointless-and-elastic-training-on-amazon-sagemaker-hyperpod/)</sup>

弹性通过允许任务以最小工作节点数量启动并向最大值增长而无需停止或重启来解决这个问题。[Kubernetes](https://www.edgepedia.org/zh/kubernetes) 控制器工作中提到的实际好处包括：在竞价实例上训练、对硬件故障的韧性，以及随集群资源可用性动态扩缩容。<sup>[2](https://aws.amazon.com/blogs/containers/fault-tolerant-distributed-machine-learning-training-with-the-torchelastic-controller-for-kubernetes/)</sup>

## 成员变更的处理方式

**会合。** 当工作节点集合发生变化时，任务必须重新就谁参与达成一致。PyTorch Elastic 包含一个会合组件，它收集训练任务的参与者，使所有参与者对相同的参与者列表和每个人的角色达成一致，并就训练何时可以开始或恢复做出一致的集体决策。<sup>[2](https://aws.amazon.com/blogs/containers/fault-tolerant-distributed-machine-learning-training-with-the-torchelastic-controller-for-kubernetes/)</sup> 在 PyTorch Elastic Trainer 的设计中，当某个工作进程失败时，管理它的弹性代理会终止该节点上的所有工作进程，与其他代理建立会合，并以新的会合信息重启工作进程；当代理本身以非零代码退出时，Kubernetes 等更高层的编排器会重启该代理。<sup>[4](https://github.com/pytorch/elastic/blob/master/design/torchelastic/0.2.0/design_doc.md)</sup>

**弹性代理。** 在 torchelastic 中，弹性代理是控制平面：一个启动和管理工作进程的进程，为它们提供调用 `torch.distributed.init_process_group()` 所需的信息，监控它们是否故障或不健康，并在必要时终止并重启所有进程，以及通过以新成员重启工作节点来响应成员变更。最简单的代理每个节点部署一个。<sup>[11](https://docs.pytorch.org/docs/main/elastic/agent.html)</sup>

**状态恢复。** 成员变更后，训练从之前已知的良好状态恢复。<sup>[4](https://github.com/pytorch/elastic/blob/master/design/torchelastic/0.2.0/design_doc.md)</sup> Elastic Horovod 在用户训练函数执行之前在工作节点之间同步状态，工作节点故障或工作节点增加事件会在其他工作节点上触发重置事件，该事件充当屏障，用于决定任务是否继续、将故障主机加入黑名单、在新主机上启动工作节点以及更新 rank 信息。<sup>[12](https://blog.kubeflow.org/elastic%20training/operators/2021/03/15/elastic-training.html)</sup>

## 主要实现

**PyTorch Elastic（TorchElastic）。** PyTorch Elastic Trainer 通过捕获可重试错误并重试，以及允许工作节点在配置的边界内加入或离开，提供容错弹性训练。采用它只需要应用程序的入口点与 PyTorch 分布式启动器兼容。<sup>[4](https://github.com/pytorch/elastic/blob/master/design/torchelastic/0.2.0/design_doc.md)</sup> TorchElastic 已上游合并到 PyTorch 1.9。<sup>[13](https://docs.pytorch.org/elastic/)</sup> 2020 年 4 月，AWS 和 Facebook 的 PyTorch 团队发布了 TorchElastic Controller for Kubernetes，这是一个原生 Kubernetes 实现，通过 `ElasticJob` 自定义资源管理弹性任务的 Pod 和服务；工作节点是对等的，通过无头服务进行点对点通信，所需的工作节点规模可以在最小值和最大值之间动态更改而不会中断任务。<sup>[2](https://aws.amazon.com/blogs/containers/fault-tolerant-distributed-machine-learning-training-with-the-torchelastic-controller-for-kubernetes/)</sup>

**Elastic Horovod。** Elastic Horovod 任务通过 `horovodrun` 启动，它在运行时通过主机发现脚本而不是固定主机列表来发现主机。任务在至少有所需数量的槽位可用之前不会启动，最小和最大进程数可以通过 `--min-np` 和 `--max-np` 指定；当学习率缩放和数据分区必须在当前工作节点数量变化时保持不变时，最大值也作为它们的参考点。故障主机会被加入黑名单，持续一个可配置的冷却期，重复故障采用指数退避，反复失败的 rank 会终止任务。<sup>[7](https://horovod.readthedocs.io/en/stable/elastic_include.html)</sup>

**SageMaker HyperPod 弹性训练。** Amazon 的 HyperPod 能力于 2025 年 12 月发布，根据计算可用性和工作负载优先级自动扩缩容训练任务。任务以所需的最少计算资源启动，通过在不同进程总数下自动保存检查点并恢复训练来进行扩缩容，调整数据并行副本的数量。该系统构建在 HyperPod 训练算子之上，使用 PyTorch Distributed Checkpoint（DCP）进行状态管理，支持 PyTorch DDP 和 FSDP，并与 Kueue 集成以实现排队、优先级排序、组调度和优雅抢占，因此较低优先级的弹性任务可以让出资源而不会被强制驱逐。弹性策略指定最小和最大副本数、扩缩容增量或离散值，以及优雅关闭超时；发生故障时，控制器等待配置的时间以在备用容量上恢复故障副本，如果无法恢复则缩容，并在资源返回时扩容。AWS 指出，该弹性训练目前不支持无检查点训练、HyperPod 托管的分层检查点或竞价实例。<sup>[5](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-eks-elastic-training.html)</sup><sup> • </sup><sup>[6](https://aws.amazon.com/blogs/aws/introducing-checkpointless-and-elastic-training-on-amazon-sagemaker-hyperpod/)</sup>

## 保持精度一致

在运行中更改工作节点数量会改变有效批几何，因此弹性系统必须保持优化行为稳定。HyperPod 弹性训练保持全局批大小不变，并在不同规模下调整学习率，以防止收敛受到不利影响。<sup>[6](https://aws.amazon.com/blogs/aws/introducing-checkpointless-and-elastic-training-on-amazon-sagemaker-hyperpod/)</sup> 当全局批无法在各 rank 之间均匀分配时，不均匀批分配会给某些 rank 分配较小的本地批，给其他 rank 分配较大的本地批，从而保持全局总量不变。<sup>[5](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-eks-elastic-training.html)</sup> EasyScale 采取更严格的立场，精确保持数据并行训练行为，并追踪与一致性相关的因素，使模型精度在同构和异构 GPU 下都能在弹性调整过程中保持一致。<sup>[8](https://arxiv.org/abs/2208.14228)</sup>

## 局限与相关问题

数据并行维度上的弹性是支持得最好的情况。2025 年 USENIX ATC 的一篇分析指出，PyTorch Distributed Elastic 仅允许调整数据并行度，并且当时没有现有系统支持将[检查点](https://www.edgepedia.org/zh/checkpointing)从 ZeRO 式数据并行重新配置为 3D 并行，或为稀疏混合专家模型重新配置混合并行，因为分布式检查点与特定并行策略紧密耦合。Universal Checkpointing 通过策略无关的检查点结构和基于模式的重配置流水线解决了这个问题，对于 70 亿到 1 万亿参数的模型，将重配置时间缩短至原来的 1/14 到 1/257，1 万亿参数的重配置在 3 分钟内完成。<sup>[9](https://www.usenix.org/system/files/atc25-lian.pdf)</sup>

弹性本身也不能解决慢节点问题。2025 年 OSDI 对字节跳动 LLM 训练集群的一项研究发现，掉队者会损害紧密同步的训练任务，使 42.5% 的任务至少慢 10%，在尾部导致任务浪费 45% 的已分配资源。<sup>[14](https://www.usenix.org/system/files/osdi25-lin-jinkun.pdf)</sup> 基于备份工作节点的传统掉队者缓解方法假设同步不频繁，会损害 LLM 训练任务的性能，因为 LLM 训练同步频繁。<sup>[14](https://www.usenix.org/system/files/osdi25-lin-jinkun.pdf)</sup>

## References

1. [Section 18.4: Elastic Training, Scaling Out AI](https://scalablebook.icsgen-ai.org/part-4-parallel-deep-learning/module-18-elastic-fault-tolerant-training/section-18.4.html)
2. [Fault tolerant distributed machine learning training with the TorchElastic Controller for Kubernetes, AWS Containers Blog](https://aws.amazon.com/blogs/containers/fault-tolerant-distributed-machine-learning-training-with-the-torchelastic-controller-for-kubernetes/)
3. [Scaling Machine Learning Systems (fault tolerance chapter), Harvard CS249r book](https://harvard-edge.github.io/cs249r_book_dev/vol2/07_fault_tolerance/07_fault_tolerance.html)
4. [PyTorch Elastic Trainer (PET) v0.2 design document, pytorch/elastic GitHub](https://github.com/pytorch/elastic/blob/master/design/torchelastic/0.2.0/design_doc.md)
5. [Using elastic training in Amazon SageMaker HyperPod, Amazon SageMaker AI Developer Guide](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-eks-elastic-training.html)
6. [Introducing checkpointless and elastic training on Amazon SageMaker HyperPod, AWS News Blog](https://aws.amazon.com/blogs/aws/introducing-checkpointless-and-elastic-training-on-amazon-sagemaker-hyperpod/)
7. [Elastic Horovod, Horovod documentation](https://horovod.readthedocs.io/en/stable/elastic_include.html)
8. [EasyScale: Accuracy-consistent Elastic Training for Deep Learning, arXiv:2208.14228](https://arxiv.org/abs/2208.14228)
9. [Universal Checkpointing: A Flexible and Efficient Distributed Checkpointing System for Large-Scale DNN Training with Reconfigurable Parallelism, USENIX ATC 2025](https://www.usenix.org/system/files/atc25-lian.pdf)
10. [TorchElastic Controller for Kubernetes design, pytorch/elastic GitHub](https://github.com/pytorch/elastic/blob/master/design/kubernetes/torchelastic-operator-design.md)
11. [Elastic Agent, PyTorch documentation](https://docs.pytorch.org/docs/main/elastic/agent.html)
12. [Elastic Training with MPI Operator and Practice, Kubeflow blog](https://blog.kubeflow.org/elastic%20training/operators/2021/03/15/elastic-training.html)
13. [TorchElastic documentation, PyTorch](https://docs.pytorch.org/elastic/)
14. [Understanding Stragglers in Large Model Training Using What-if Analysis, USENIX OSDI 2025](https://www.usenix.org/system/files/osdi25-lin-jinkun.pdf)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/elastic-training

English original: https://www.edgepedia.org/elastic-training
