Reuse the TP/EP test ranks across tests instead of spawning them per test (#49986)
* Reuse the TP/EP test ranks across tests instead of spawning them per test
`_init_distributed` used to `mp.spawn` fresh ranks for every test. On CI (4 vCPU, `-n 4`), the ranks
spent ~20s re-importing torch and transformers while the test itself takes <1s once they are warm.
The ranks now live for the whole pytest(-xdist worker) process, initialize the process group once, and
run the test functions they receive from a queue. Any rank failure tears the pool down so the next
test starts from fresh ranks.
* Destroy the process groups of each test's device meshes in the persistent ranks
Every TP/EP from_pretrained builds a device mesh with new process groups. Ranks that live for the whole
session would otherwise accumulate them, and their sockets, until they hit the open-files limit.
* Simplify the persistent TP/EP test ranks
* isolate redudant function to distributed_utils.files
* Simplify the persistent test ranks and isolate each test
Each rank talks to pytest over a single pipe: an EOF on it means the rank died,
and the rank exits on EOF once pytest is gone, so no polling, startup handshake
or atexit hook is needed. Before every test, the rank frees what the previous
test left behind (models, compiled graphs, device mesh groups) and re-seeds.
Collectives and tests now time out instead of hanging, and stdout is
line-buffered so a killed rank's output stays with its test.
* Group the persistent ranks' code in one class
The registry of live ranks, killing them, and the code that runs inside each
rank (as static methods, so spawn can pickle them by name) now all live in
_PersistentRanks.
* separate workers and workersgroup
* typo
* merge conflict
* Apply suggestions from code review
Replace the `Worker` dataclass with a plain `_worker_main` function.
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
* Remove a failed worker group from the registry in `run_distributed`, not in `kill`
`kill` now only kills the processes, so `WorkerGroup` no longer needs `world_size` and `backend` attributes.
---------
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>