Add weights_only=True to torch.load() calls#58
Open
michaelv2 wants to merge 1 commit intosaprmarks:mainfrom
Open
Add weights_only=True to torch.load() calls#58michaelv2 wants to merge 1 commit intosaprmarks:mainfrom
michaelv2 wants to merge 1 commit intosaprmarks:mainfrom
Conversation
PyTorch's pickle-based deserialization can execute arbitrary code when loading a crafted .pt file. Adding weights_only=True restricts deserialization to tensor data only, preventing this class of attack. This is the recommended practice since PyTorch 2.0 and addresses CVE-2025-32434 for users on PyTorch < 2.6. Affected call sites: - dictionary.py: AutoEncoder, GatedAutoEncoder, JumpReluAutoEncoder, AutoEncoderNew from_pretrained() - trainers/top_k.py: AutoEncoderTopK from_pretrained() - trainers/batch_top_k.py: BatchTopKSAE from_pretrained() - trainers/matryoshka_batch_top_k.py: MatryoshkaBatchTopKSAE from_pretrained() - activault_s3_buffer.py: compile()
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
weights_only=Trueto all 8torch.load()call sites across the codebase.ptfiles (pickle deserialization attack)weights_onlyis available since 2.0 but defaults to False, so this is no-op on 2.6+ and avoids breaking changes for 2.0-2.5)Context
torch.load()withoutweights_only=Trueuses Python'spicklemodule, which can execute arbitrary code during deserialization. Since allfrom_pretrained()methods in this repo load state dicts (plain dicts of tensors),weights_only=Trueis sufficient and doesn't change behavior for legitimate model files.This is a narrow attack chain if the
.ptfiles are only ever loaded from the official HuggingFace account, but in a shared research environment where files may be routinely passed around, a malicious file could propagate.Affected call sites:
dictionary.py:AutoEncoder,GatedAutoEncoder,JumpReluAutoEncoder,AutoEncoderNewfrom_pretrained()trainers/top_k.py:AutoEncoderTopKfrom_pretrained()trainers/batch_top_k.py:BatchTopKSAEfrom_pretrained()trainers/matryoshka_batch_top_k.py:MatryoshkaBatchTopKSAEfrom_pretrained()activault_s3_buffer.py:compile()Test plan
weights_only=Truepytestpasses (no changes to logic, only to deserialization safety)