fix: pickle Quantized through its class, so torch.load can allow it - #101
Merged
Merged
Conversation
torch.load defaults to weights_only=True, which refuses any global it doesn't trust. Pickles called the static method from_bytes, which pickles as builtins.getattr, so add_safe_globals([Quantized]) didn't help, and allowing getattr would defeat weights_only. Quantized(data) now loads the bytes that to_bytes saved, like from_bytes, and pickles call it, so allowing the class is enough. Pickles made through from_bytes still load, since from_bytes stays.
This was referenced Sep 28, 2026
aksheyd
marked this pull request as ready for review
September 28, 2026 04:47
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.
torch.save({"layer": q}, "model.pt")worked, buttorch.load("model.pt")refused it. Since PyTorch 2.6,torch.loaddefaults toweights_only=True, which only calls globals it trusts. Pickles called the static methodfrom_bytes, which pickles asbuiltins.getattr(Quantized, "from_bytes"). Sotorch.serialization.add_safe_globals([Quantized])didn't help, and allowinggetattr, which PyTorch's message suggested, would defeatweights_only.Quantized(data)loads the bytes thatto_bytessaved, likefrom_bytes, and pickles now call it. Allowing the class is enoughfrom_bytesstill load, sincefrom_bytesstays. A test loads one saved bymainbefore this changeadd_safe_globals([Quantized])beforetorch.loadQuantized, liketorch.loaddoes afteradd_safe_globals, across every pickle protocol. So CI doesn't need torchChecked with torch 2.14 (CPU): a checkpoint that holds every kind and scale type loads under the default
weights_only=Trueonce the class is allowed. Before that, PyTorch's error now suggests allowingquantize.Quantizedinstead ofgetattr.Worth landing before 0.3.0, since every pickle saved by a release keeps whatever it calls. A follow-up PR for PyTorch inputs is stacked on this one.
just lint,just test, andjust pythonpass.