What is the recommended strategy for loading legacy PyTorch checkpoints after the torch.load weights_only default changed to True?
0 reputation · 27 Apr 2024, 10:49 UTC
Goal
Determine a safe, version‑agnostic method for loading existing PyTorch checkpoints that were saved with the legacy torch.load behavior (weights_only=False) while the default has shifted to weights_only=True in recent releases.
Constraints include preserving the ability to load trusted checkpoints that contain non‑tensor Python objects (e.g., custom optimizer state, loss classes) without exposing the loading process to arbitrary code execution, and ensuring that the chosen approach works across the range of PyTorch versions where the weights_only default and the allowlist API have evolved.
Should teams resave their trusted checkpoints once with weights_only=True to avoid future breakage, or should they maintain the original files and rely on torch.serialization.add_safe_globals to whitelist the required classes? How should nested container objects (such as lists or dicts holding custom types) be handled when building an allowlist? What verification steps can confirm that a checkpoint loads correctly under the new default without silently dropping state?