Skip to content
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

Fix model saving bug post training with tensor parallel #36434

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2940,6 +2940,17 @@ def save_pretrained(
# (initially introduced with TimmWrapperModel to remove prefix and make checkpoints compatible with timm)
state_dict = self._fix_state_dict_keys_on_save(state_dict)

# if using tensor parallel we need to gather the tensors in state dict
gathered_state_dict = {}
for key, value in state_dict.items():
if hasattr(value, "_local_tensor"):
gathered_state_dict[key] = value.to_local()
else:
gathered_state_dict[key] = value

del state_dict
state_dict = gathered_state_dict

if safe_serialization:
# Safetensors does not allow tensor aliasing.
# We're going to remove aliases before saving
Expand Down