Skip to content

Commit

Permalink
improve from_pretrained for zero3 multi gpus mode (huggingface#24964)
Browse files Browse the repository at this point in the history
* improve from_pretrained for zero3 multi gpus mode

* Add check if torch.distributed.is_initialized

* Revert torch.distributed

---------

Co-authored-by: Stas Bekman <stas@stason.org>
  • Loading branch information
2 people authored and blbadger committed Nov 8, 2023
1 parent 7452eaf commit f7f23c4
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,11 @@ def load_state_dict(checkpoint_file: Union[str, os.PathLike]):
)
return safe_load_file(checkpoint_file)
try:
return torch.load(checkpoint_file, map_location="cpu")
if is_deepspeed_zero3_enabled() and torch.distributed.is_initialized() and torch.distributed.get_rank() > 0:
map_location = "meta"
else:
map_location = "cpu"
return torch.load(checkpoint_file, map_location=map_location)
except Exception as e:
try:
with open(checkpoint_file) as f:
Expand Down

0 comments on commit f7f23c4

Please sign in to comment.