Skip to content

Add weights_only=True to torch.load #3012

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 4 commits into from
Aug 27, 2024
Merged

Add weights_only=True to torch.load #3012

merged 4 commits into from
Aug 27, 2024

Conversation

svekars
Copy link
Contributor

@svekars svekars commented Aug 26, 2024

Fixes #3007
Fixing the issue and a bunch of other tutorials that use torch.load. In the generated output we have something like this:

Screenshot 2024-08-26 at 2 11 11 PM

We should fix all those warnings.

Copy link

pytorch-bot bot commented Aug 26, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/3012

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit cac47b8 with merge base 4c1dd9d (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

Copy link
Contributor

@mikaylagawarecki mikaylagawarecki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for fixing these! I just have two quick nit comments around messaging for weights_only=True/False

@@ -34,7 +34,7 @@
# using ``load_state_dict()`` method.

model = models.vgg16() # we do not specify ``weights``, i.e. create untrained model
model.load_state_dict(torch.load('model_weights.pth'))
model.load_state_dict(torch.load('model_weights.pth', weights_only=True))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: since this is the main landing page for save load, a comment along these lines would be great

"Setting weights_only=True restricts the functions that can be executed during unpickling to those required to load weights, using this flag is a best practice for loading weights."

@@ -52,7 +52,7 @@
########################
# We can then load the model like this:

model = torch.load('model.pth')
model = torch.load('model.pth', weights_only=False),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would add a comment here that as noted in https://pytorch.org/docs/main/notes/serialization.html#saving-and-loading-torch-nn-modules, saving state_dicts is the best practice and that we use weights_only=False here because this is loading a model, which is a legacy use case for torch.save

@svekars svekars marked this pull request as ready for review August 27, 2024 17:42
@svekars
Copy link
Contributor Author

svekars commented Aug 27, 2024

Thanks, @mikaylagawarecki! I added your notes - can you take a look?

Copy link
Contributor

@mikaylagawarecki mikaylagawarecki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@svekars svekars merged commit afcd669 into main Aug 27, 2024
19 checks passed
@svekars svekars deleted the fix-3007 branch August 27, 2024 19:57
c-p-i-o pushed a commit that referenced this pull request Sep 6, 2024
* Add weights_only=True to torch.load
c-p-i-o pushed a commit that referenced this pull request Sep 6, 2024
* Add weights_only=True to torch.load
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] - FutureWarning in "Introduction to PyTorch - Save and Load the Model" tutorial
3 participants