Skip to content

Error with recent version of pytorch-lightning and torch #47

@micedre

Description

@micedre

With pytorch-lightning version 2.6.0 (which comes with torch 2.9.1): all training ends in an error :

Traceback (most recent call last):
  File "/home/onyxia/work/torchTextClassifiers/examples/basic_classification.py", line 167, in <module>
    main()
    ~~~~^^
  File "/home/onyxia/work/torchTextClassifiers/examples/basic_classification.py", line 132, in main
    classifier.train(
    ~~~~~~~~~~~~~~~~^
        X_train, y_train,
        ^^^^^^^^^^^^^^^^^
    ...<2 lines>...
        verbose=True
        ^^^^^^^^^^^^
    )
    ^
  File "/home/onyxia/work/torchTextClassifiers/torchTextClassifiers/torchTextClassifiers.py", line 360, in train
    self.lightning_module = TextClassificationModule.load_from_checkpoint(
                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        best_model_path,
        ^^^^^^^^^^^^^^^^
        model=self.pytorch_model,
        ^^^^^^^^^^^^^^^^^^^^^^^^^
        loss=training_config.loss,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/onyxia/work/torchTextClassifiers/.venv/lib/python3.13/site-packages/pytorch_lightning/utilities/model_helpers.py", line 130, in wrapper
    return self.method(cls_type, *args, **kwargs)
           ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/onyxia/work/torchTextClassifiers/.venv/lib/python3.13/site-packages/pytorch_lightning/core/module.py", line 1781, in load_from_checkpoint
    loaded = _load_from_checkpoint(
        cls,
    ...<5 lines>...
        **kwargs,
    )
  File "/home/onyxia/work/torchTextClassifiers/.venv/lib/python3.13/site-packages/pytorch_lightning/core/saving.py", line 65, in _load_from_checkpoint
    checkpoint = pl_load(checkpoint_path, map_location=map_location, weights_only=weights_only)
  File "/home/onyxia/work/torchTextClassifiers/.venv/lib/python3.13/site-packages/lightning_fabric/utilities/cloud_io.py", line 73, in _load
    return torch.load(
           ~~~~~~~~~~^
        f,
        ^^
        map_location=map_location,  # type: ignore[arg-type]
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        weights_only=weights_only,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/onyxia/work/torchTextClassifiers/.venv/lib/python3.13/site-packages/torch/serialization.py", line 1529, in load
    raise pickle.UnpicklingError(_get_wo_message(str(e))) from None
_pickle.UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint. 
        (1) In PyTorch 2.6, we changed the default value of the `weights_only` argument in `torch.load` from `False` to `True`. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source.
        (2) Alternatively, to load with `weights_only=True` please check the recommended steps in the following error message.
        WeightsUnpickler error: Unsupported global: GLOBAL torch.optim.adam.Adam was not an allowed global by default. Please use `torch.serialization.add_safe_globals([torch.optim.adam.Adam])` or the `torch.serialization.safe_globals([torch.optim.adam.Adam])` context manager to allowlist this global if you trust this class/function.

Check the documentation of torch.load to learn more about types accepted by default with weights_only https://pytorch.org/docs/stable/generated/torch.load.html.

It works by fixing pytorch-lightning v2.5.1.post0 with torch v2.7.0

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions