#1138 feat: add ClusteredTaskEnvironment for distributed multi-node training
Pull request opened by
AdilFayyaz
Motivation
Flyte had no first-class way to run distributed multi-node training jobs. This adds a ClusteredTaskEnvironment
that emits a Kubernetes JobSet and launches workers via torchrun, exposing the distributed runtime context (rank,
world size, master address, etc.) to user tasks.
Summary
• Add flyte.distributed public API: ClusteredTaskEnvironment, TorchRun, and ClusterFailurePolicy, with validation
(replica/proc counts, GPU-vs-nproc_per_node, interconnect) and to_custom_dict() that serializes to the
ClusteredTaskSpec proto.
• task_serde detects a clustered parent env and rewrites the proto task: sets type clustered-task (v1), swaps the
container command to the flyte.distributed._entrypoint wrapper, and attaches the clustered custom config — for
both plain-container and pod-template paths.
• _entrypoint exports derived NODE_RANK/MASTER_ADDR so torchrun children inherit them, and adds --no-python so
torchrun execs the a0 console script directly.
• Expose distributed fields on TaskContext (rank, local_rank, world_size, node_rank, master_addr, master_port,
restart_attempt, …), all None on non-clustered tasks.
• In io.py, only rank-0 uploads outputs/errors so other ranks don't race to clobber error.pb.
• Bump flyteidl2 2.0.17 → 2.0.18 (root + rs_controller) for the new clustered_pb2 types.
Test Plan
• uv run pytest tests/flyte/distributed/test_env.py tests/flyte/distributed/test_serde.py
• Validation: confirm ClusteredTaskEnvironment raises on replicas < 1, nproc_per_node < 1, gpu < nproc_per_node,
unsupported runtime, and invalid interconnect; non-tcp interconnect warns.
• Serde: confirm a task under a clustered env produces type clustered-task, command python -m
flyte.distributed._entrypoint, and a populated custom block (both container and pod-template paths).
• End-to-end: run examples/distributed/ddp_train.py and verify the JobSet launches, ranks join rendezvous, and
only rank-0 writes outputs.
flyteorg/flyte-sdk