Skip to content

Torch: test on 2.0 and latest versions + explicitly load with weights_only=True #2488

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

Merged
merged 6 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 15 additions & 4 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
[
"Repository only",
"Everything else",
"torch",
"torch_1.11",
"torch_latest",
]
include:
- python-version: "3.11" # LFS not ran on 3.8
Expand Down Expand Up @@ -71,8 +72,18 @@ jobs:
git config --global user.name "ci"
;;

fastai | torch)
uv pip install "huggingface_hub[${{ matrix.test_name }}] @ ."
fastai)
uv pip install "huggingface_hub[fastai] @ ."
;;

torch_latest)
uv pip install "huggingface_hub[torch] @ ."
uv pip install --upgrade torch
;;

torch_1.11)
uv pip install "huggingface_hub[torch] @ ."
uv pip install torch~=1.11
;;

tensorflow)
Expand Down Expand Up @@ -121,7 +132,7 @@ jobs:
eval "$PYTEST ../tests/test_serialization.py"
;;

torch)
torch_1.11 | torch_latest)
eval "$PYTEST ../tests/test_hub_mixin*"
eval "$PYTEST ../tests/test_serialization.py"
;;
Expand Down
2 changes: 1 addition & 1 deletion src/huggingface_hub/hub_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ def _from_pretrained(

@classmethod
def _load_as_pickle(cls, model: T, model_file: str, map_location: str, strict: bool) -> T:
state_dict = torch.load(model_file, map_location=torch.device(map_location))
state_dict = torch.load(model_file, map_location=torch.device(map_location), weights_only=True)
model.load_state_dict(state_dict, strict=strict) # type: ignore
model.eval() # type: ignore
return model
Expand Down
9 changes: 5 additions & 4 deletions tests/test_hub_mixin_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from huggingface_hub.constants import PYTORCH_WEIGHTS_NAME
from huggingface_hub.errors import EntryNotFoundError, HfHubHTTPError
from huggingface_hub.hub_mixin import ModelHubMixin, PyTorchModelHubMixin
from huggingface_hub.serialization._torch import storage_ptr
from huggingface_hub.utils import SoftTemporaryDirectory, is_torch_available

from .testing_constants import ENDPOINT_STAGING, TOKEN, USER
Expand Down Expand Up @@ -415,10 +416,10 @@ def forward(self, x):

# Linear layers should share weights and biases in memory
state_dict = reloaded.state_dict()
a_weight_ptr = state_dict["a.weight"].untyped_storage().data_ptr()
b_weight_ptr = state_dict["b.weight"].untyped_storage().data_ptr()
a_bias_ptr = state_dict["a.bias"].untyped_storage().data_ptr()
b_bias_ptr = state_dict["b.bias"].untyped_storage().data_ptr()
a_weight_ptr = storage_ptr(state_dict["a.weight"])
b_weight_ptr = storage_ptr(state_dict["b.weight"])
a_bias_ptr = storage_ptr(state_dict["a.bias"])
b_bias_ptr = storage_ptr(state_dict["b.bias"])
assert a_weight_ptr == b_weight_ptr
assert a_bias_ptr == b_bias_ptr

Expand Down
Loading