Skip to content

[torch_checkpointing] add barrier before hf consolidation - #4528

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
aelavender:pr4528
Sep 11, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
aelavender:pr4528

Conversation

@aelavender

@aelavender aelavender commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

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.barrier inside the callback to recover the previous behaviour. consolidate_hf_safetensors_checkpoint already uses dist.barrier internally, so this doesn't add any additional sync points.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 9, 2026
@aelavender
aelavender marked this pull request as draft September 9, 2026 01:22
@aelavender
aelavender marked this pull request as ready for review September 9, 2026 01:33
@aelavender
aelavender requested a review from ivy-zhou September 9, 2026 01:36
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():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to add it here instead of within the consolidate_hf_safetensors_checkpoint function itself?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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():

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tianyu-l
tianyu-l merged commit 40672af into pytorch:main Sep 11, 2026
25 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants