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

[WIP] Enable mmap in torch.load() #19087

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 6 additions & 4 deletions src/lightning/fabric/utilities/cloud_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
from fsspec.implementations.local import AbstractFileSystem
from lightning_utilities.core.imports import module_available

from lightning.fabric.utilities.imports import _TORCH_GREATER_EQUAL_2_1
from lightning.fabric.utilities.types import _MAP_LOCATION_TYPE, _PATH

log = logging.getLogger(__name__)


def _load(
path_or_url: Union[IO, _PATH],
map_location: _MAP_LOCATION_TYPE = None,
) -> Any:
def _load(path_or_url: Union[IO, _PATH], map_location: _MAP_LOCATION_TYPE = None) -> Any:
"""Loads a checkpoint.

Args:
Expand All @@ -51,6 +49,10 @@ def _load(
str(path_or_url),
map_location=map_location, # type: ignore[arg-type]
)
if _is_local_file_protocol(path_or_url):
kwargs = {"mmap": True} if _TORCH_GREATER_EQUAL_2_1 else {}
return torch.load(str(path_or_url), map_location=map_location, **kwargs)

fs = get_filesystem(path_or_url)
with fs.open(path_or_url, "rb") as f:
return torch.load(f, map_location=map_location) # type: ignore[arg-type]
Expand Down
Loading