refactor: deduplicate low-risk code paths

This commit is contained in:
2026-08-19 16:17:40 +08:00
parent f252af495c
commit 398e8a3ea3
12 changed files with 64 additions and 114 deletions
+7 -11
View File
@@ -247,18 +247,15 @@ class LocalStrategy(LaunchStrategy):
ctx.join()
def _detect_launcher() -> str:
"""Detect the distributed launcher from environment.
Returns one of: "torchelastic", "torchrun", "external", "local".
"""
def _is_external_launcher() -> bool:
"""Whether an external launcher (torchrun/elastic/manual env) started us."""
if dist.is_torchelastic_launched():
return "torchelastic"
return True
if "LOCAL_WORLD_SIZE" in os.environ:
return "torchrun"
return True
if "RANK" in os.environ and "WORLD_SIZE" in os.environ:
return "external"
return "local"
return True
return False
def spawn_parallel_fn(
@@ -273,8 +270,7 @@ def spawn_parallel_fn(
):
if master_port is None:
master_port = find_free_port()
launcher = _detect_launcher()
if launcher in ("torchelastic", "torchrun", "external"):
if _is_external_launcher():
strategy = TorchrunStrategy(
world_size, backend, master_addr, master_port, device_type, start_method
)