Default to weights_only=True when loading pretrained weights - #9632
Default to weights_only=True when loading pretrained weights#9632fjankovi wants to merge 1 commit into
Conversation
WeightsEnum.get_state_dict forwards to torch.hub.load_state_dict_from_url, which passes weights_only explicitly to torch.load (defaulting to False). Because it is passed explicitly, this path does not pick up the weights_only=True default that torch.load adopted in PyTorch 2.6, so a tampered checkpoint can still execute arbitrary code via pickle during unpickling on any torch version. Set weights_only=True by default via setdefault so every pretrained checkpoint is loaded safely while leaving callers free to override. Verified that float and quantized (fbgemm) checkpoints still load, since the tensor/qtensor rebuild functions they use are on torch's weights_only allowlist. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9632
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @fjankovi! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Summary
WeightsEnum.get_state_dict(torchvision/models/_api.py) is the single funnel through which all pretrained checkpoints are loaded. It forwards totorch.hub.load_state_dict_from_url, which passesweights_onlyexplicitly totorch.load(its own default isFalse).Because it is passed explicitly, this path does not benefit from the
weights_only=Truedefault thattorch.loadadopted in PyTorch 2.6 — that default only applies when the argument is left unset. As a result, a tampered checkpoint can still execute arbitrary code viapickleduring unpickling, on any torch version, if an attacker can influence the bytes that are loaded (e.g. a poisoned~/.cache/torch/hub/checkpoints/entry, or control of the served bytes).check_hash=Trueonly compares the 8-hex (32-bit) filename prefix, which is not a meaningful integrity barrier against an attacker who controls the contents.This sets
weights_only=Trueby default inget_state_dict, usingsetdefaultso callers can still override it if they have a specific reason to.Change
Compatibility
Verified that both float and quantized checkpoints still load with
weights_only=True:resnet18(float)resnet50_fbgemm(quantized)googlenet_fbgemm(quantized)Quantized weights were the main compatibility risk, and they load because the tensor/qtensor rebuild functions they rely on are already on torch's
weights_onlyallowlist.This mirrors the
weights_only=Truepattern already used in the datasets code (imagenet.py,mnist.py,phototour.py).Opened as a draft for discussion.