Skip to content

Commit

Permalink
Use mmap option to load_state_dict (huggingface#28331)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weiming Zhao committed Jan 3, 2024
1 parent 45b1dfa commit bffda1f
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,10 @@ def load_state_dict(checkpoint_file: Union[str, os.PathLike]):
map_location = "meta"
else:
map_location = "cpu"

return torch.load(checkpoint_file, map_location=map_location, weights_only=True)
extra_args = {}
if version.parse(torch.__version__) >= version.parse("2.1.0"):
extra_args = {"mmap": True}
return torch.load(checkpoint_file, map_location=map_location, weights_only=True, **extra_args)
except Exception as e:
try:
with open(checkpoint_file) as f:
Expand Down

0 comments on commit bffda1f

Please sign in to comment.