过期计时器¶
过期计时器与代理程序设置在同一进程中,并从您的脚本中使用,以处理卡住的工人。当您进入一个有可能卡住的代码块时,您可以获取一个过期计时器,该计时器指示计时器服务器在进程未能在自设的过期截止日期前释放计时器时终止该进程。
用法:
import torchelastic.timer as timer
import torchelastic.agent.server as agent
def main():
start_method = "spawn"
message_queue = mp.get_context(start_method).Queue()
server = timer.LocalTimerServer(message, max_interval=0.01)
server.start() # 非阻塞
spec = WorkerSpec(
fn=trainer_func,
args=(message_queue,),
...<OTHER_PARAMS...>)
agent = agent.LocalElasticAgent(spec, start_method)
agent.run()
def trainer_func(message_queue):
timer.configure(timer.LocalTimerClient(message_queue))
with timer.expires(after=60): # 60秒过期
# 做一些工作
在上面的示例中,如果 trainer_func
的执行时间超过60秒,那么工作进程将被终止,并且代理将重试工作组。
客户端方法¶
- torch.distributed.elastic.timer.expires(after, scope=None, client=None)[源码]¶
获取一个倒计时器,该计时器将在
after
秒后到期,除非它所包裹的代码块在此时间范围内完成。当计时器到期时,此工作线程有资格被回收。“回收”的确切含义取决于客户端实现。在大多数情况下,回收意味着终止工作线程进程。请注意,工作线程并不保证在time.now() + after
时刻被回收,而是工作线程“有资格”被回收,客户端与之通信的TimerServer
将最终决定何时以及如何回收具有过期计时器的工作线程。用法:
torch.distributed.elastic.timer.configure(LocalTimerClient()) with expires(after=10): torch.distributed.all_reduce(...)
服务器/客户端实现¶
以下是torchelastic提供的计时服务器和客户端对。
注意
计时器服务器和客户端总是需要成对实现和使用,因为服务器和客户端之间存在消息协议。
下面是一对基于multiprocess.Queue
实现的定时器服务器和客户端。
- class torch.distributed.elastic.timer.LocalTimerServer(mp_queue, max_interval=60, daemon=True)[源代码]¶
与
LocalTimerClient
一起工作的服务器。客户端预计是运行此服务器的父进程的子进程。作业中的每个主机预计都会在本地启动自己的计时器服务器,每个服务器实例管理本地工作线程的计时器(运行在同一主机上的进程)。
- class torch.distributed.elastic.timer.LocalTimerClient(mp_queue)[源代码]¶
LocalTimerServer
的客户端。此客户端旨在在与LocalTimerServer
运行的同一主机上使用,并使用 pid 来唯一标识一个工作进程。这在每台主机上有多个 GPU 设备的情况下,每个 GPU 生成一个子进程(训练器)时特别有用。
下面是另一对基于命名管道实现的定时器服务器和客户端。
- class torch.distributed.elastic.timer.FileTimerServer(file_path, max_interval=10, daemon=True, log_event=None)[源代码]¶
与
FileTimerClient
一起工作的服务器。客户端预计将在运行此服务器的同一主机上运行。 作业中的每个主机预计将在本地启动自己的计时器服务器,并且每个服务器实例管理本地工作者的计时器(运行在同一主机上的进程)。
- class torch.distributed.elastic.timer.FileTimerClient(file_path, signal=Signals.SIGKILL)[源代码]¶
FileTimerServer
的客户端。此客户端旨在与运行FileTimerServer
的同一主机上使用,并使用 pid 来唯一标识一个工作进程。 此客户端使用命名管道向FileTimerServer
发送定时器请求。此客户端是生产者,而FileTimerServer
是消费者。多个客户端可以与同一个FileTimerServer
一起工作。- Parameters
file_path (str) – 字符串,FIFO 特殊文件的路径。
FileTimerServer
必须通过调用 os.mkfifo() 创建它。信号 – 信号,用于终止进程的信号。使用负值或零信号将不会终止进程。
编写自定义计时器服务器/客户端¶
要编写自己的定时器服务器和客户端,请扩展
torch.distributed.elastic.timer.TimerServer
作为服务器,
torch.distributed.elastic.timer.TimerClient
作为客户端。
TimerRequest
对象用于在服务器和客户端之间传递消息。
- class torch.distributed.elastic.timer.TimerRequest(worker_id, scope_id, expiration_time)[源代码]¶
表示倒计时定时器获取和释放的数据对象,用于
TimerClient
和TimerServer
之间。 负的expiration_time
应被解释为“释放”请求。注意
类型
worker_id
是实现特定的。 它是 TimerServer 和 TimerClient 实现中用于唯一标识工作者的内容。
- class torch.distributed.elastic.timer.TimerServer(request_queue, max_interval, daemon=True)[源代码]¶
监控活动计时器并在适当时间过期它们的实体。该服务器负责回收已过期计时器的工人。