-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Add GDS support for safetensors loading #45113
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
cyyever
wants to merge
1
commit into
huggingface:main
Choose a base branch
from
cyyever:gds-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| # Copyright 2025 The HuggingFace Inc. team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """ | ||
| GPU Direct Storage (GDS) utilities for safetensors loading. | ||
| """ | ||
|
|
||
| import json | ||
| import os | ||
| import struct | ||
| from typing import Any | ||
|
|
||
| import torch | ||
| from safetensors import safe_open | ||
|
|
||
| from .import_utils import is_env_variable_true, is_torch_greater_or_equal | ||
|
|
||
|
|
||
| # Tensors below this use safe_open instead of cuFile (per-call overhead). | ||
| _GDS_MIN_BYTES = 1 * 1024 * 1024 | ||
|
|
||
| _gds_available: bool | None = None | ||
|
|
||
|
|
||
| def is_gds_available() -> bool: | ||
| """Check if ``torch.cuda.gds.GdsFile`` is usable. Requires PyTorch >= 2.10, CUDA >= 12.6.""" | ||
| global _gds_available | ||
| if _gds_available is not None: | ||
| return _gds_available | ||
| _gds_available = False | ||
| if not is_torch_greater_or_equal("2.10"): | ||
| return False | ||
| try: | ||
| if not torch.cuda.is_available(): | ||
| return False | ||
| if hasattr(torch._C, "_gds_is_available"): | ||
| _gds_available = torch._C._gds_is_available() | ||
| else: | ||
| from torch.cuda.gds import GdsFile # noqa: F401 | ||
|
|
||
| _gds_available = True | ||
| except (ImportError, AttributeError, RuntimeError): | ||
| _gds_available = False | ||
| return _gds_available | ||
|
|
||
|
|
||
| def should_use_gds() -> bool: | ||
| """``True`` when GDS is available and opted-in via ``HF_ENABLE_GDS=1``.""" | ||
| return is_env_variable_true("HF_ENABLE_GDS") and is_gds_available() | ||
|
|
||
|
|
||
| # Resolve once at class init, not per-tensor | ||
| _str_to_torch_dtype: dict[str, torch.dtype] | None = None | ||
|
|
||
|
|
||
| def _get_dtype_map() -> dict[str, torch.dtype]: | ||
| global _str_to_torch_dtype | ||
| if _str_to_torch_dtype is None: | ||
| from ..modeling_utils import str_to_torch_dtype | ||
|
|
||
| _str_to_torch_dtype = str_to_torch_dtype | ||
| return _str_to_torch_dtype | ||
|
|
||
|
|
||
| class GdsSafetensorsFile: | ||
| """GDS-backed safetensors reader — drop-in replacement for ``safe_open()``.""" | ||
|
|
||
| def __init__(self, filename: str): | ||
| self.filename = str(filename) | ||
| with open(self.filename, "rb") as f: | ||
| header_size = struct.unpack("<Q", f.read(8))[0] | ||
| header = json.loads(f.read(header_size)) | ||
| data_offset = 8 + header_size | ||
|
|
||
| self._tensor_meta: dict[str, dict[str, Any]] = {} | ||
| for name, meta in header.items(): | ||
| if name == "__metadata__": | ||
| continue | ||
| start, end = meta["data_offsets"] | ||
| self._tensor_meta[name] = { | ||
| "dtype": meta["dtype"], | ||
| "shape": meta["shape"], | ||
| "file_offset": data_offset + start, | ||
| "nbytes": end - start, | ||
| } | ||
|
|
||
| from torch.cuda.gds import GdsFile | ||
|
|
||
| self._gds_file = GdsFile(self.filename, os.O_RDONLY) | ||
| self._safe_fp = safe_open(self.filename, framework="pt", device="cpu") | ||
| self._dtype_map = _get_dtype_map() | ||
|
|
||
| def keys(self): | ||
| return self._tensor_meta.keys() | ||
|
|
||
| def get_slice(self, name: str) -> "GdsSlice": | ||
| return GdsSlice(self, name) | ||
|
|
||
| def get_tensor(self, name: str, device: torch.device | None = None) -> torch.Tensor: | ||
| meta = self._tensor_meta[name] | ||
| if device is not None and device.type == "cuda" and meta["nbytes"] >= _GDS_MIN_BYTES: | ||
| tensor = torch.empty(meta["shape"], dtype=self._dtype_map[meta["dtype"]], device=device) | ||
| self._gds_file.load_storage(tensor.untyped_storage(), meta["file_offset"]) | ||
| return tensor | ||
| return self._safe_fp.get_tensor(name) | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, *_args): | ||
| self._close() | ||
|
|
||
| def __del__(self): | ||
| self._close() | ||
|
|
||
| def _close(self): | ||
| gds = self.__dict__.pop("_gds_file", None) | ||
| safe = self.__dict__.pop("_safe_fp", None) | ||
| del gds | ||
| if safe is not None: | ||
| safe.__exit__(None, None, None) | ||
|
|
||
|
|
||
| class GdsSlice: | ||
| """Lazy tensor reference compatible with ``PySafeSlice``.""" | ||
|
|
||
| __slots__ = ("_gds_file", "_name", "_dtype", "_shape", "_target_device") | ||
|
|
||
| def __init__(self, gds_file: GdsSafetensorsFile, name: str): | ||
| meta = gds_file._tensor_meta[name] | ||
| self._gds_file = gds_file | ||
| self._name = name | ||
| self._dtype = meta["dtype"] | ||
| self._shape = meta["shape"] | ||
| self._target_device: torch.device | None = None | ||
|
|
||
| def _set_target_device(self, device) -> None: | ||
| self._target_device = torch.device(device) if device is not None else None | ||
|
|
||
| def get_dtype(self) -> str: | ||
| return self._dtype | ||
|
|
||
| def get_shape(self) -> list[int]: | ||
| return self._shape | ||
|
|
||
| def __getitem__(self, slices): | ||
| tensor = self._gds_file.get_tensor(self._name, self._target_device) | ||
| if slices is Ellipsis or slices == (Ellipsis,): | ||
| return tensor | ||
| return tensor[slices] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean you pull the full tensor for each slice that is requested? What does your memory footprint on device look like once the model is loaded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure your question. The purpose is to load tensors via GDS api, which works best with aligned file offsets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you guarantee file offsets are aligned here? safetensors files aren't written with that constraint in mind, you need to do some extra processing (we're thinking of supporting writing aligned offsets, but it's tricky wrt backwards compatibility).
What I'm asking, is that from your implementation, it seems you're loading the full tensor
self._nameon each call toGdsSlice.__getitem__. That is why I asked what the memory footprint (total used memory on device) looks like. If you can run nvidia-smi after loading the model, that'd be a good test to see if that happens.