Repository navigation
Description
What happened
When the gather in which the ranks share their checks of a resumed data position fails, as when another rank reaches it only after the manager group's timeout, a rank whose own check failed or refused the position raises the collective's error instead of its own, so no rank's output says why the position could not be applied.
CheckpointManager.apply_dataloader_state has every rank check the kept position with its loader's check_state, and the ranks share their verdicts in _position_refusals (kempnerforge/checkpoint/manager.py:3526-3572 at bdaf4a4) before any rank applies it. A check that fails other than with DataPositionError is kept in failure, and a refusal as this rank's verdict (:3550-3556). failure is raised only after dist.all_gather_object on the manager's gloo group returns (:3560-3563), and the refusal only after that, as DataPositionError in apply_dataloader_state (:3521). When the gather raises, neither is raised, chained nor logged. The gather fails when another rank reaches it only after the group's timeout, which training sets to distributed.nccl_timeout_sec (kempnerforge/training/entry.py:229), or when another rank's process exits while this one waits. In run_training no collective lines the ranks up between the restore and this check (entry.py:547-567): every rank builds its data pipeline, rank 0 starts the tracking backends, and a StreamingDataLoader resolves its data files and their sizes in check_state itself. The late rank then fails with a collective error as well, so the run ends without naming what went wrong.
#776 adds an agreement on the data build just before this check; after it, a rank can still be late in its own check_state, or exit, and #776 does not change the gather.
Expected: a rank whose check failed or refused raises its own error whether or not the gather completes, with the collective's failure in a note.
Possible fix: once #776 merges, run the gather through the run_keeping_own_error it adds, building the refusal's DataPositionError before the gather so it can be kept.
Related: #780 (the same gap in the checkpoint manager's agreements), #776, #749.
Reproduction
CPU only, two gloo ranks, with a 5 s timeout on the manager's group in place of distributed.nccl_timeout_sec. Both ranks save step 1 with a loader and load it, which keeps the loader's position for apply_dataloader_state. Rank 1's check of the position then fails at once: with FileNotFoundError in the failed runs, as for a data file it cannot see, and with DataPositionError in the refused runs, as when its view of the data differs. In the late runs rank 0 applies the position only once rank 1 has raised, standing in for a rank still building its data or checking the position as the timeout ends: apply_dataloader_state runs no collective before the gather, so to rank 1 the two are the same. Save as repro.py and run uv run python repro.py from the repository root:
"""Two CPU ranks over gloo apply a resumed data position; rank 1's check of it fails at once."""
import multiprocessing
import tempfile
import time
import traceback
from pathlib import Path
import torch
import torch.distributed as dist
from kempnerforge.checkpoint.manager import CheckpointManager
from kempnerforge.config.schema import CheckpointConfig, DataConfig
from kempnerforge.data.dataloader import DataPositionError, StatefulDataLoader
GROUP_TIMEOUT_S = 5
OWN_ERROR = {"failed": "No such file or directory", "refused": "a data file changed size"}
def fail_position_checks(kind: str) -> None:
"""Make every later check of a saved position in this process fail at once."""
def check_state(self: StatefulDataLoader, state: dict) -> None:
if kind == "failed":
raise FileNotFoundError(2, OWN_ERROR[kind], "train-00001.jsonl")
raise DataPositionError(f"The data changed since the save: {OWN_ERROR[kind]}")
StatefulDataLoader.check_state = check_state
def run_rank(rank: int, root: str, kind: str, case: str) -> None:
"""Save and load step 1, then apply the kept position; late, rank 0 waits for rank 1."""
dist.init_process_group("gloo", init_method=f"file://{root}/store", rank=rank, world_size=2)
model = torch.nn.Linear(4, 4)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
samples = [torch.tensor([float(i)]) for i in range(16)]
loader = StatefulDataLoader(samples, batch_size=2, config=DataConfig(num_workers=0))
config = CheckpointConfig(dir=f"{root}/ckpt")
manager = CheckpointManager(config, model, optimizer, group_timeout_sec=GROUP_TIMEOUT_S)
manager.save(step=1, dataloader=loader)
manager.load()
rank_1_outcome = Path(root, "rank1.txt")
if rank == 1:
fail_position_checks(kind)
elif case == "late":
deadline = time.monotonic() + 300
while not rank_1_outcome.exists() and time.monotonic() < deadline:
time.sleep(0.1)
run = f"{kind}, {case}"
start = time.monotonic()
try:
manager.apply_dataloader_state(loader)
lines = [f"{run}: rank {rank} applied the position"]
except Exception as error:
shown = "".join(traceback.format_exception(error))
own = "yes" if OWN_ERROR[kind] in shown else "no"
if rank == 0 and case == "late":
lines = [f"{run}: rank 0 raised; its error shows rank 1's: {own}"]
elif rank == 0:
lines = [f"{run}: rank 0 raised {type(error).__name__}: {error}"]
else:
lines = [
f"{run}: rank 1 raised {type(error).__name__}: {error}",
f"{run}: rank 1 raised after about {round(time.monotonic() - start)} s; "
f"its own error in what it raised: {own}",
]
if run == "failed, late":
lines.append(shown)
Path(root, f"rank{rank}.txt").write_text("\n".join(lines) + "\n")
def run_both_ranks(root: Path, kind: str, case: str) -> None:
"""Run ``run_rank`` on two spawned ranks; print rank 1's outcome, then rank 0's."""
context = multiprocessing.get_context("spawn")
ranks = [context.Process(target=run_rank, args=(r, str(root), kind, case)) for r in range(2)]
for process in ranks:
process.start()
for process in ranks:
process.join(600)
for rank, process in enumerate(ranks):
if process.is_alive():
process.kill()
process.join()
print(f"{kind}, {case}: rank {rank} was still running after 600 s")
for rank in (1, 0):
outcome = root / f"rank{rank}.txt"
missing = f"{kind}, {case}: rank {rank}: none\n"
print(outcome.read_text() if outcome.exists() else missing, end="")
def main() -> None:
"""Run both kinds of failed check with rank 0 on time and late."""
with tempfile.TemporaryDirectory() as tmp:
for kind in ("failed", "refused"):
for case in ("on time", "late"):
root = Path(tmp, f"{kind}_{case.replace(' ', '_')}")
root.mkdir()
run_both_ranks(root, kind, case)
if __name__ == "__main__":
main()Error / traceback
When rank 0 is on time, rank 1 raises its own error and rank 0's names it. When rank 0 is late, rank 1 raises only the gather's timeout, after the group's 5 s, in both runs, and rank 0's error does not show rank 1's either:
failed, on time: rank 1 raised FileNotFoundError: [Errno 2] No such file or directory: 'train-00001.jsonl'
failed, on time: rank 1 raised after about 0 s; its own error in what it raised: yes
failed, on time: rank 0 raised RuntimeError: Checking the saved data position failed on ranks [1]: rank 1: FileNotFoundError: [Errno 2] No such file or directory: 'train-00001.jsonl'
failed, late: rank 1 raised RuntimeError: [/__w/pytorch/pytorch/third_party/gloo/gloo/transport/tcp/unbound_buffer.cc:129] Timed out waiting 5000ms for send operation to complete
failed, late: rank 1 raised after about 5 s; its own error in what it raised: no
failed, late: rank 0 raised; its error shows rank 1's: no
refused, on time: rank 1 raised DataPositionError: The data changed since the save: a data file changed size
refused, on time: rank 1 raised after about 0 s; its own error in what it raised: yes
refused, on time: rank 0 raised DataPositionError: The saved data position cannot be resumed on ranks [1]: rank 1: The data changed since the save: a data file changed size
refused, late: rank 1 raised RuntimeError: [/__w/pytorch/pytorch/third_party/gloo/gloo/transport/tcp/unbound_buffer.cc:129] Timed out waiting 5000ms for send operation to complete
refused, late: rank 1 raised after about 5 s; its own error in what it raised: no
refused, late: rank 0 raised; its error shows rank 1's: no
Rank 1's traceback in the failed, late run:
Traceback (most recent call last):
File "repro.py", line 52, in run_rank
manager.apply_dataloader_state(loader)
File "kempnerforge/checkpoint/manager.py", line 3501, in apply_dataloader_state
refusals, world = self._position_refusals(dataloader, state)
File "kempnerforge/checkpoint/manager.py", line 3560, in _position_refusals
dist.all_gather_object(gathered, verdict, group=self._sync_group)
...
File "torch/distributed/distributed_c10d.py", line 4208, in all_gather_object
all_gather(object_size_list, local_size, group=group)
...
File "torch/distributed/distributed_c10d.py", line 5055, in all_gather
work.wait()
RuntimeError: [/__w/pytorch/pytorch/third_party/gloo/gloo/transport/tcp/unbound_buffer.cc:129] Timed out waiting 5000ms for send operation to complete
Environment
- GPU type and count: none (CPU reproduction, 2 gloo ranks)
- Nodes: 1
- PyTorch version: 2.14.1+cu130
- Python: 3.12.11
- KempnerForge commit: bdaf4a4
Activity