Skip to content

Enable multi rank safetensor consolidation #1625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions torchtitan/components/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
HuggingFaceStorageReader,
HuggingFaceStorageWriter,
)
from torch.distributed.checkpoint._consolidate_hf_safetensors import (
consolidate_safetensors_files_on_every_rank,
)
from torch.distributed.checkpoint.staging import DefaultStager, StagingOptions
from torch.distributed.checkpoint.state_dict import (
get_model_state_dict,
Expand Down Expand Up @@ -352,14 +355,23 @@ def dcp_save(
state_dict = self.sd_adapter.to_hf(state_dict)

fqn_to_index_mapping = self.sd_adapter.fqn_to_index_mapping

storage_writer = HuggingFaceStorageWriter(
path=checkpoint_id,
save_distributed=True,
fqn_to_index_mapping=fqn_to_index_mapping,
enable_consolidation=True,
thread_count_consolidation=5,
)
if fqn_to_index_mapping:
storage_writer = HuggingFaceStorageWriter(
path=os.path.join(checkpoint_id, "sharded"),
save_distributed=True,
fqn_to_index_mapping=fqn_to_index_mapping,
enable_consolidation=False,
)
else:
# the reason for only enabling consolidation if there is
# no mapping is because no mapping implies that we save all fqns
# to one file. This means we only need one rank to consolidate.
# Otherwise we should use consolidate_safetensors_files_on_every_rank
storage_writer = HuggingFaceStorageWriter(
path=checkpoint_id,
save_distributed=True,
enable_consolidation=True,
)

else:
checkpoint_save_id = checkpoint_id
Expand Down Expand Up @@ -387,6 +399,14 @@ def dcp_save(
checkpoint_id=checkpoint_save_id,
)

if to_hf and self.sd_adapter.fqn_to_index_mapping:
consolidate_safetensors_files_on_every_rank(
input_dir=os.path.join(checkpoint_id, "sharded"),
output_dir=checkpoint_id,
fqn_to_index_mapping=self.sd_adapter.fqn_to_index_mapping,
num_threads=5,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this API take PG as an argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

It's better to be the case. I don't know how post training will be and how people are going to split the nodes. But if not all ranks join the checkpoint save and load, dist.get_world() is not correct. I'm not familiar with the post training use case though. cc., @tianyu-l

Copy link
Contributor

Choose a reason for hiding this comment

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

@fegin
I don't know either. cc: @allenwang28 if you know.

Does dist.get_rank() and dist.get_world_size() rely on NCCL PG? It sounds a bit strange & unnecessary that CPU consolidating relies on GPU info.


if enable_garbage_collection:
GarbageCollection.collect("GC collection invoked by checkpointer.")

Expand Down
2 changes: 1 addition & 1 deletion torchtitan/protocols/state_dict_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ def __init__(self, model_args: BaseModelArgs, hf_assets_path: str | None):
self.fqn_to_index_mapping = {}
for hf_key, raw_indx in hf_safetensors_indx["weight_map"].items():
indx = re.search(r"\d+", raw_indx).group(0)
self.fqn_to_index_mapping[hf_key] = indx
self.fqn_to_index_mapping[hf_key] = int(indx)
else:
self.fqn_to_index_mapping = None
Loading