Skip to content

Use weights_only for torch.load checkpoints - #28097

Merged
kunal-vaishnavi merged 1 commit into
mainfrom
tlwu/20260417/t5_helper
Apr 17, 2026
Merged

Use weights_only for torch.load checkpoints#28097
kunal-vaishnavi merged 1 commit into
mainfrom
tlwu/20260417/t5_helper

Conversation

@tianleiwu

@tianleiwu Tianlei Wu (tianleiwu) commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Description

This PR updates PyTorch checkpoint loading in the T5 helper and NVIDIA pretraining resume script to prefer torch.load(..., weights_only=True). Newer PyTorch versions recommend this safer load mode for checkpoints because default pickle-based loading can execute arbitrary code when a .pt file is malicious.

Behavior changes across PyTorch versions:

  • PyTorch 1.10 ~ 2.5: weights_only exists, but default = False
  • PyTorch 2.6+: Default changed to True (security-driven change)

Summary of Changes

File Change
onnxruntime/python/tools/transformers/models/t5/t5_helper.py Adds a local helper that loads state dict checkpoints with weights_only=True when available and uses it for state_dict_path.
orttraining/tools/scripts/nv_run_pretraining.py Adds the same compatibility helper and uses it when resuming from ckpt_*.pt training checkpoints.

Motivation and Context

torch.load can deserialize Python pickle payloads. Using weights_only=True narrows loading to tensor/checkpoint data on supported PyTorch versions and is the safer default for model weights. This reduces risk if an attacker can place or substitute a local .pt file, or persuades an operator to download and resume from a malicious checkpoint.

Testing

  • python -m py_compile onnxruntime/python/tools/transformers/models/t5/t5_helper.py orttraining/tools/scripts/nv_run_pretraining.py

@kunal-vaishnavi
kunal-vaishnavi merged commit c5bc801 into main Apr 17, 2026
106 of 108 checks passed
@kunal-vaishnavi
kunal-vaishnavi deleted the tlwu/20260417/t5_helper branch April 17, 2026 17:05
Adrian Lizarraga (adrianlizarraga) added a commit that referenced this pull request May 13, 2026
### Description

Follow-up to PR #28097. Applies the same `_torch_load_weights_only()`
wrapper to the two remaining `torch.load()` call sites.

`torch.load` can deserialize arbitrary Python pickle payloads. Using
`weights_only=True` restricts loading to tensor/checkpoint data on
supported PyTorch versions and is the safer default. The wrapper
gracefully falls back to the default `torch.load` behavior on older
PyTorch versions that do not support the `weights_only` parameter.

### Summary of Changes

| File | Change |
|------|--------|
|
`onnxruntime/test/testdata/test_data_generation/lr_scheduler/lr_scheduler_test_data_generator.py`
| Adds `_torch_load_weights_only()` helper and uses it when loading
scheduler/optimizer state dicts. |
|
`orttraining/orttraining/test/python/orttraining_test_ortmodule_pytorch_ddp.py`
| Adds `_torch_load_weights_only()` helper and uses it when loading DDP
model checkpoint. |

### Motivation and Context

These were the last two `torch.load()` calls in the repository without
`weights_only=True`. While both are in test/tooling code with low direct
risk, this change ensures consistency with the pattern established in PR
#28097 and eliminates all unsafe deserialization call sites.

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

2 participants