Skip to content

Commit 5f7958d

Browse files
wadeKeithyin
authored andcommitted
fix(train): guard checkpoint dir ops with rank 0 to prevent DDP race condition
When running multi-GPU training with --overwrite, all ranks previously attempted to delete and recreate the checkpoint directory simultaneously. This caused FileNotFoundError when one rank deleted the directory while others were still accessing it. Now only rank 0 performs directory deletion and creation, with dist.barrier() calls to synchronize all ranks. Closes #868
1 parent c23745b commit 5f7958d

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

scripts/train_pytorch.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,23 @@ def train_loop(config: _config.TrainConfig):
329329
else:
330330
raise FileNotFoundError(f"Experiment checkpoint directory {exp_checkpoint_dir} does not exist for resume")
331331
elif config.overwrite and config.checkpoint_dir.exists():
332-
shutil.rmtree(config.checkpoint_dir)
333-
logging.info(f"Overwriting checkpoint directory: {config.checkpoint_dir}")
332+
# Only rank 0 should delete to avoid race conditions in DDP (see #868).
333+
if is_main:
334+
shutil.rmtree(config.checkpoint_dir)
335+
logging.info(f"Overwriting checkpoint directory: {config.checkpoint_dir}")
336+
if use_ddp:
337+
dist.barrier()
334338

335339
# Create checkpoint directory with experiment name
336340
if not resuming:
337-
# For new runs, create experiment-specific checkpoint directory
341+
# Only rank 0 creates the directory; other ranks wait behind the barrier.
342+
if is_main:
343+
exp_checkpoint_dir = config.checkpoint_dir
344+
exp_checkpoint_dir.mkdir(parents=True, exist_ok=True)
345+
logging.info(f"Created experiment checkpoint directory: {exp_checkpoint_dir}")
346+
if use_ddp:
347+
dist.barrier()
338348
exp_checkpoint_dir = config.checkpoint_dir
339-
exp_checkpoint_dir.mkdir(parents=True, exist_ok=True)
340-
logging.info(f"Created experiment checkpoint directory: {exp_checkpoint_dir}")
341349
else:
342350
# For resume, checkpoint_dir is already set to the experiment directory
343351
logging.info(f"Using existing experiment checkpoint directory: {config.checkpoint_dir}")

0 commit comments

Comments
 (0)