Skip to content

Fix restoring pickled policy objects under torch 2.6+ weights_only default - #1315

Open
dafahaha wants to merge 3 commits into
thu-ml:masterfrom
dafahaha:fix/policy-restore-weights-only
Open

dafahaha wants to merge 3 commits into
thu-ml:masterfrom
dafahaha:fix/policy-restore-weights-only

Conversation

@dafahaha

Copy link
Copy Markdown

Fixes #1297. With PyTorch 2.6 the default of torch.load switched to weights_only=True, which breaks PolicyPersistence.restore whenever the policy was saved in the default Mode.POLICY. That mode deliberately pickles the whole policy object (so it can be reloaded without an environment), and weights_only=True refuses to unpickle arbitrary class globals like tianshou's policy classes, raising an UnpicklingError on load. The state-dict mode keeps working because it only contains tensors.

The fix opts out of weights_only for the POLICY-mode load only, since that file is by design a pickled object produced by tianshou itself; POLICY_STATE_DICT is left untouched. A round-trip regression test covering both modes was added.

Tested with pytest on the new test file: both modes pass after the change, while on the parent commit the POLICY case fails with the same Weights-only UnpicklingError reported in #1297.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-11T15:22:04.334169Z 1415cba PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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.

Loading policy from persitence dir

1 participant