Repository navigation
[torch_checkpointing] add barrier before hf consolidation - #4528
Conversation
| pre_finalize_callback = lambda staged, _event_logger: ( | ||
| def pre_finalize_callbach(staged: str, _event_logger) -> None: | ||
| # Need all ranks to finish writing before any rank starts consolidating | ||
| if dist.is_initialized(): |
There was a problem hiding this comment.
Any reason to add it here instead of within the consolidate_hf_safetensors_checkpoint function itself?
There was a problem hiding this comment.
Seemed more natural to put it here, because the fact that is happening inside pre_finalize_callback is what requires there to be a barrier. A user calling consolidate_hf_safetensors_checkpoint on an already saved and finalised safetensors checkpoint doesn't need a barrier, and adding one would cause an unnecessary sync point.
| # were actually written to. Consolidation repacks the per-rank | ||
| # shards into HF-layout files in the checkpoint directory. | ||
| pre_finalize_callback = lambda staged, _event_logger: ( | ||
| def pre_finalize_callbach(staged: str, _event_logger) -> None: |
There was a problem hiding this comment.
oh hey a typo, lemme fix
| pre_finalize_callback = lambda staged, _event_logger: ( | ||
| def pre_finalize_callbach(staged: str, _event_logger) -> None: | ||
| # Need all ranks to finish writing before any rank starts consolidating | ||
| if dist.is_initialized(): |
There was a problem hiding this comment.
Seemed more natural to put it here, because the fact that is happening inside pre_finalize_callback is what requires there to be a barrier. A user calling consolidate_hf_safetensors_checkpoint on an already saved and finalised safetensors checkpoint doesn't need a barrier, and adding one would cause an unnecessary sync point.
Follow-up to #4190.
That PR required moving the pre_finalize callback to after the barrier (meta-pytorch/torch_checkpointing@912006f). Turns out there are use cases which need this callback to run before the barrier. I've got a diff up to move it back (D119246630).
The last save step is happening synchronously in the main process, so we are free to use our own
dist.barrierinside the callback to recover the previous behaviour.consolidate_hf_safetensors_checkpointalready usesdist.barrierinternally, so this doesn't add any additional sync points.