The elastic agent is the control plane of torchelastic.
It is a process that launches and manages underlying worker processes.
The agent is responsible for:
Working with distributed torch: the workers are started with all the
necessary information to successfully and trivially call
torch.distributed.init_process_group().
Fault tolerance: monitors workers and upon detecting worker failures
or unhealthiness, tears down all workers and restarts everyone.
Elasticity: Reacts to membership changes and restarts workers with the new
members.
The simplest agents are deployed per node and work with local processes.
A more advanced agent can launch and manage workers remotely. Agents can
be completely decentralized, making decisions based on the workers it manages.
Or can be coordinated, communicating to other agents (that manage workers
in the same job) to make a collective decision.
Below is a diagram of an agent that manages a local group of workers.
Concepts
This section describes the high-level classes and concepts that
are relevant to understanding the role of the agent in torchelastic.
An agent process responsible for managing one or more worker processes.
The worker processes are assumed to be regular distributed PyTorch scripts.
When the worker process is created by the agent, the agent provides the
necessary information for the worker processes to properly initialize
a torch process group.
The exact deployment topology and ratio of agent-to-worker is dependent
on the specific implementation of the agent and the user’s job placement
preferences. For instance, to run a distributed training job on GPU with
8 trainers (one per GPU) one can:
Use 8 x single GPU instances, place an agent per instance, managing
1 worker per agent.
Use 4 x double GPU instances, place an agent per instance, managing
2 workers per agent.
Use 2 x quad GPU instances, place an agent per instance, managing
4 workers per agent.
Use 1 x 8 GPU instance, place an agent per instance, managing
8 workers per agent.
Usage
group_result=agent.run()ifgroup_result.is_failed():# workers failedfailure=group_result.failures[0]logger.exception("worker 0 failed with exit code : %s",failure.exit_code)else:returngroup_result.return_values
abstractget_worker_group(role='default')
Return the WorkerGroup for the given role.
Note that the worker group is a mutable object and hence in a
multi-threaded/process environment it may change state.
Implementers are encouraged (but not required) to return
a defensive read-only copy.
Workers in a worker group change state as a unit. If a single worker
in a worker group fails the entire set is considered failed:
UNKNOWN - agent lost track of worker group state, unrecoverable
INIT - worker group object created not yet started
HEALTHY - workers running and healthy
UNHEALTHY - workers running and unhealthy
STOPPED - workers stopped (interrupted) by the agent
SUCCEEDED - workers finished running (exit 0)
FAILED - workers failed to successfully finish (exit !0)
A worker group starts from an initial INIT state,
then progresses to or states,
and finally reaches a terminal or state.
The class defines a set of Worker instances for the given WorkerSpec managed by ElasticAgent. Whether the worker
group contains cross instance workers or not depends on the implementation of the agent.
Implementations
Below are the agent implementations provided by torchelastic.
An implementation of torchelastic.agent.server.ElasticAgent that handles host-local workers.
This agent is deployed per host and is configured to spawn n workers.
When using GPUs, n maps to the number of GPUs available on the host.
The local agent does not communicate to other local agents deployed on
other hosts, even if the workers may communicate inter-host. The worker id
is interpreted to be a local process. The agent starts and stops all worker
processes as a single unit.
The worker function and argument passed to the worker function must be
python multiprocessing compatible. To pass multiprocessing data structures
to the workers you may create the data structure in the same multiprocessing
context as the specified start_method and pass it as a function argument.
The exit_barrier_timeout specifies the amount of time (in seconds) to wait
for other agents to finish. This acts as a safety net to handle cases where
workers finish at different times, to prevent agents from viewing workers
that finished early as a scale-down event. It is strongly advised that the
user code deal with ensuring that workers are terminated in a synchronous
manner rather than relying on the exit_barrier_timeout.
A named pipe based watchdog can be enabled in `LocalElasticAgent` if an
environment variable with value 1 has
been defined in the process.
Optionally, another environment variable
can be set with a unique file name for the named pipe. If the environment
variable is not set,
will internally create a unique file name and set it to the environment
variable , and this environment variable will
be propagated to the worker processes to allow them to connect to the same
named pipe that uses.
Extending the Agent
To extend the agent you can implement ElasticAgent directly, however
we recommend you extend SimpleElasticAgent instead, which provides
most of the scaffolding and leaves you with a few specific abstract methods
to implement.
Fast Path: when all workers have the same role and world size. We calculate
the global rank to be group_rank * group_world_size + local_rank. And the
role_world_size is the same as global_world_size. No TCP store is used in
this case. This is only enabled when users set the environment variable
TORCH_ELASTIC_WORKER_IDENTICAL to 1.
Time complexity: each worker O(1), overall O(1)
Slow Path: when workers have different roles and world sizes. We use the
following algorithm:
Each agent writes its configuration(group_rank, group_world_size
, num_workers) to the common store.
The rank 0 agent reads all the role_info from the store and
determines each agents worker ranks.
Determine the global rank: the global rank of the workers is computed
by cumulative sum of the local_world_size for all workers in front of it.
For efficiency reasons each worker is assigned a base global rank
such that its workers are in the range [base_global_rank,
base_global_rank + local_world_size).
Determine the role rank: The role rank is determined using the algorithms
in the point 3 with the exception that the ranks are calculated with
respect to the role name.
_exit_barrier()
Define a barrier that keeps the agent process alive until all workers finish.
Wait for exit_barrier_timeout seconds for all agents to finish
executing their local workers (either successfully or not). This
acts as a safety guard against user scripts that terminate at different
times.
_initialize_workers(worker_group)
Start a fresh set of workers for the worker_group.
Essentially, a rendezvous followed by a start_workers.
The caller should first call _stop_workers() to stop running workers
prior to calling this method.
Optimistically sets the state of the worker group that
just started as HEALTHY and delegates the actual monitoring
of state to _monitor_workers() method
abstract_monitor_workers(worker_group)
Check on the workers for the worker_group.
This function also returns the new state of the worker group.
Implementers must deal with workers in all states defined by
WorkerState. That is, it must gracefully handle stopping
non-existent workers, unhealthy (stuck) workers, etc.
Run results follow an “all-or-nothing” policy where the run is successful if and
only if ALL local workers managed by this agent complete successfully.
If the result is successful (e.g. is_failed()=False) then the return_values
field contains the outputs (return values) of the workers managed by THIS agent mapped
by their GLOBAL ranks. That is result.return_values[0] is the return value of
global rank 0.
Watchdog in the Agent
A named pipe based watchdog can be enabled in LocalElasticAgent if an
environment variable TORCHELASTIC_ENABLE_FILE_TIMER with value 1 has
been defined in the LocalElasticAgent process.
Optionally, another environment variable TORCHELASTIC_TIMER_FILE
can be set with a unique file name for the named pipe. If the environment
variable TORCHELASTIC_TIMER_FILE is not set, LocalElasticAgent
will internally create a unique file name and set it to the environment
variable TORCHELASTIC_TIMER_FILE, and this environment variable will
be propagated to the worker processes to allow them to connect to the same
named pipe that LocalElasticAgent uses.
Health Check Server
A health check monitoring server can be enabled in LocalElasticAgent
if an environment variable TORCHELASTIC_HEALTH_CHECK_PORT has been defined
in the LocalElasticAgent process.
Adding interface for health check server which can be extended by starting tcp/http
server on the specified port number.
Additionally, health check server will have callback to check watchdog is alive.
Blueprint information about a particular type of worker.
For a given role, there must only exist a single worker spec.
Worker spec is expected to be homogeneous across all nodes (machine),
that is each node runs the same number of workers for a particular spec.
Parameters:
role (str) – user-defined role for the workers with this spec
local_world_size (int) – number local workers to run
fn (Callable | None) – (deprecated use entrypoint instead)
entrypoint (Callable | str | None) – worker function or command
rdzv_handler (RendezvousHandler) – handles rdzv for this set of workers
max_restarts (int) – number of max retries for the workers
monitor_interval (float) – monitor status of workers every n seconds
master_port (int | None) – fixed port to run the c10d store on rank 0
if not specified then will choose a random free port
master_addr (str | None) – fixed master_addr to run the c10d store on rank 0
if not specified then will choose hostname on agent rank 0
redirects – redirect std streams to a file,
selectively redirect for a particular
local rank by passing a map
tee – tees the specified std stream(s) to console + file,
selectively tee for a particular local rank by passing a map,
takes precedence over redirects settings.
duplicate_stdout_filters (list[str] | None) – If non-empty, duplicates stdout to a file containing only lines
that match _any_ of the filter strings.
duplicate_stderr_filters (list[str] | None) – If non-empty, duplicates stderr to a file containing only lines
that match _any_ of the filter strings.
virtual_local_rank (bool) – Enable virtual local rank mode for workers (defaults to False).
When enabled, LOCAL_RANK is set to 0 for all workers and
CUDA_VISIBLE_DEVICES is adjusted so each worker accesses its
assigned GPU at device index 0.
get_entrypoint_name()
Get the entry point name.
If the entrypoint is a function (e.g. Callable) returns its __qualname__
else if the entrypoint is a binary (e.g. str), returns the binary name.
HEALTHY
UNHEALTHY
SUCCEEDED
FAILED
Worker groups can be interrupted and temporarily put into STOPPED state
by the agent. Workers in STOPPED state are scheduled to be restarted
in the near future by the agent. Some examples of workers being put into
STOPPED state are:
Worker group failure|unhealthy observed
Membership change detected
When actions (start, stop, rdzv, retry, etc) on worker group fail
and results in the action being partially applied to the worker group
the state will be UNKNOWN. Typically this happens on uncaught/unhandled
exceptions during state change events on the agent. The agent is not
expected to recover worker groups in UNKNOWN state and is better off
self terminating and allowing the job manager to retry the node.
staticis_running(state)
Return the state of the Worker.
Returns:
True if the worker state represents workers still running
(e.g. that the process exists but not necessarily healthy).
The id of the worker is interpreted
by the specific implementation of ElasticAgent. For a local
agent, it could be the pid(int) of the worker, for a remote
agent it could be encoded as host:port(string).
Parameters:
id (Any) – uniquely identifies a worker (interpreted by the agent)
role_world_size (int) – number of workers that have the same role
TORCHELASTIC_ENABLE_FILE_TIMER
`LocalElasticAgent`
`TORCHELASTIC_TIMER_FILE`
`TORCHELASTIC_TIMER_FILE`
`LocalElasticAgent`
`TORCHELASTIC_TIMER_FILE`
`LocalElasticAgent`
Logs are written to the specified log directory. Each log line will be by default
prefixed by [${role_name}${local_rank}]: (e.g. [trainer0]:foobar).
Log prefixes can be customized by passing a template string as the
log_line_prefix_template argument.
The following macros (identifiers) are substituted at runtime:
${role_name},${local_rank},${rank},${hostname}.
For example, to prefix each log line with
global rank instead of the local rank, set log_line_prefix_template="[${rank}]:.
${hostname} expands to the name of the node the agent runs on, which
identifies the offending node in a multi-node job; for example
log_line_prefix_template="${hostname}:${rank}:" renders as
r12i0n8:3:foobar.
Example launching function
deftrainer(args)->str:return"do train"defmain():start_method="spawn"shared_queue=multiprocessing.get_context(start_method).Queue()spec=WorkerSpec(role="trainer",local_world_size=nproc_per_process,entrypoint=trainer,args=("foobar",),...<OTHER_PARAMS...>)agent=LocalElasticAgent(spec,start_method)results=agent.run()ifresults.is_failed():print("trainer failed")else:print(f"rank 0 return value: {results.return_values[0]}")# prints -> rank 0 return value: do train
Example launching binary
defmain():spec=WorkerSpec(role="trainer",local_world_size=nproc_per_process,entrypoint="/usr/local/bin/trainer",args=("--trainer-args","foobar"),...<OTHER_PARAMS...>)agent=LocalElasticAgent(spec)results=agent.run()ifnotresults.is_failed():print("binary launches do not have return values")
The rank 0 agent writes the assigned ranks to the store.
Each agent reads the assigned ranks from the store.
Time complexity: each worker O(1), rank0 O(n), overall O(n)
return_values are only meaningful for when the worker entrypoint
is a function. Workers specified as a binary entrypoint do not canonically
have a return value and the return_values field is meaningless and
may be empty.
If is_failed() returns True then the failures field contains the
failure information, again, mapped by the GLOBAL rank of the worker that failed.
The keys in return_values and failures are mutually exclusive, that is,
a worker’s final state can only be one of: succeeded, failed. Workers intentionally
terminated by the agent according to the agent’s restart policy, are not represented
in either return_values nor failures.