Skip to content

Fix pretrained checkpoint loading under torch < 2.4 (weights_only rejects hyper_parameters)#4

Open
alex-crlhmmr wants to merge 1 commit into
thuml:mainfrom
alex-crlhmmr:fix/checkpoint-loader-torch22-compat
Open

Fix pretrained checkpoint loading under torch < 2.4 (weights_only rejects hyper_parameters)#4
alex-crlhmmr wants to merge 1 commit into
thuml:mainfrom
alex-crlhmmr:fix/checkpoint-loader-torch22-compat

Conversation

@alex-crlhmmr

Copy link
Copy Markdown

What

load_checkpoint_state_dict in models/checkpoint.py guards torch.serialization.add_safe_globals with hasattr, but that API only exists from torch 2.4. On the torch version pinned by environment.yml (2.2.x) the guard is a silent no-op, and torch.load(..., weights_only=True) then rejects the argparse.Namespace that Lightning stores under hyper_parameters in every checkpoint produced by pretrain.py:

_pickle.UnpicklingError: Weights only load failed. ... Unsupported class argparse.Namespace

So loading a pretraining checkpoint into the fine-tuning path (--pretrain_checkpoint) fails on the project's own pinned environment.

Change

Fall back to weights_only=False when add_safe_globals is unavailable. The checkpoint is a local file produced by pretrain.py, so the full load is safe in that context. On torch >= 2.4 the behavior is unchanged (allowlisted weights_only=True).

Verified

On torch 2.2.1 with a checkpoint from pretrain.py: before the change, load_pretrained_encoders raises the UnpicklingError above; after the change, all three encoders load with zero missing and zero unexpected keys, and fine-tuning proceeds normally (we used this to fine-tune on FabWave and TMCAD).

load_checkpoint_state_dict guards add_safe_globals with hasattr, but that
API does not exist before torch 2.4, so on older torch the guard is a no-op
and torch.load(weights_only=True) rejects the argparse.Namespace that
Lightning stores under hyper_parameters. Loading a pretraining checkpoint
into the finetuning path then fails with an UnpicklingError.

Fall back to weights_only=False when add_safe_globals is unavailable. The
checkpoint is a local file produced by pretrain.py, so a full load is safe.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant