Skip to content

[BUG] Compatibility with weights_only torchload #1363

@vonHartz

Description

@vonHartz

Describe the bug

To prevent arbitrary code execution during unpickling, torch is moving towards using weights_only=True for torch.load.
I've tried to torch.load(weights_only=True) on some of my saved TensorDicts.

Problem is that TensorDicts are not considered save by default. And adding them manually still fails.

To Reproduce

Save a TensorDict to disk and load it back with (weights_only=True).

This will fail due to Tensordicts not being considered save by default.

I've tried the following:

torch.serialization.add_safe_globals([td.TensorDict, td.utils._StringOnlyDict])```

But still get:

```WeightsUnpickler error: Unsupported global: GLOBAL tensordict.utils._StringOnlyDict was not an allowed global by default. Please use `torch.serialization.add_safe_globals([_StringOnlyDict])` to allowlist this global if you trust this class/function.

Expected behavior

A save option to load Tensordicts would be nice.

System info

Describe the characteristic of your environment:

  • td 0.83.
  • torch 2.5.1
  • Python 3.10

Checklist

  • I have checked that there is no similar issue in the repo (required)
  • I have read the documentation (required)
  • I have provided a minimal working example to reproduce the bug (required)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions