渐进式容错 (Progressive Fault Tolerance)
在大规模集群中,硬件故障不是"是否"发生,而是"何时"发生。Monarch 提供了优雅的机制来处理这些必然的混乱。
1. 像捕获异常一样捕获故障
传统分布式框架中,一个节点挂掉往往意味着整个 MPI World 崩溃,作业必须完全重启。 Monarch 将节点故障抽象为 Python 的异常:ActorDeadError。
python
from monarch.exceptions import ActorDeadError
try:
# 尝试调用远程任务
results = actor_mesh.compute.call().get()
except ActorDeadError as e:
# 捕获到了节点死亡事件!
print(f"Detected failure on workers: {e.failed_ranks}")
# 执行自定义恢复逻辑
# ...这种设计让分布式容错逻辑变得像处理文件 IO 错误一样自然。
2. 监督树 (Supervision Trees)
Monarch 借鉴了 Erlang 的 监督树 概念。
- 父进程监控子进程。
- 如果子进程(Actor)崩溃,父进程会收到通知。
- 父进程可以决定策略:重启该子进程?重启所有子进程?还是向上级汇报?
3. 实战:自动恢复策略
结合 try-except 和动态 Mesh 生成,我们可以实现热恢复。
python
def robust_train_loop(mesh):
step = 0
while step < 1000:
try:
mesh.train_step.call(step).get()
step += 1
except ActorDeadError as e:
print("Worker died! Reconfiguring...")
# 1. 剔除死亡节点,获取健康的子网格
healthy_mesh = mesh.exclude(e.failed_ranks)
# 2. 或者:请求资源管理器启动新节点补充
new_node = spawn_new_node()
mesh = healthy_mesh.union(new_node)
# 3. 加载最近的 Checkpoint 到新 Mesh
mesh.load_checkpoint.call("latest.pt")
print("Recovered! Resuming training...")这种能力对于在廉价的抢占式实例(Spot Instances)上进行训练尤其宝贵。
