Skip to content

Conversation

coffeecloudgit
Copy link

Fix the bug:

_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 freqtrade.freqai.torch.PyTorchModelTrainer.PyTorchLSTMTrainer was not an allowed global by default. Please use
torch.serialization.add_safe_globals([freqtrade.freqai.torch.PyTorchModelTrainer.PyTorchLSTMTrainer]) or the
torch.serialization.safe_globals([freqtrade.freqai.torch.PyTorchModelTrainer.PyTorchLSTMTrainer]) 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.

Summary

Solve the issue: #___

Quick changelog

  • <change log 1>
  • <change log 1>

What's new?

Fix the bug:
```
_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 freqtrade.freqai.torch.PyTorchModelTrainer.PyTorchLSTMTrainer was not an allowed global by default. Please use
torch.serialization.add_safe_globals([freqtrade.freqai.torch.PyTorchModelTrainer.PyTorchLSTMTrainer]) or the
torch.serialization.safe_globals([freqtrade.freqai.torch.PyTorchModelTrainer.PyTorchLSTMTrainer]) 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.
```
@xmatthias xmatthias added the freqAI Issues and PR's related to freqAI label Jul 23, 2025
@xmatthias xmatthias requested a review from robcaulk July 23, 2025 05:09
import torch

zipfile = torch.load(dk.data_path / f"{dk.model_filename}_model.zip")
zipfile = torch.load(dk.data_path / f"{dk.model_filename}_model.zip", weights_only=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well based on your description and the warning from pytorch - i'm not entirely sure if we explicitly want to enable this by default (it should at least be given a good thought).

When does this error happen?
when you try to load a model written with an older pytorch version?

Copy link
Author

@coffeecloudgit coffeecloudgit Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to run backtest after run hyperopt success, load old model error happen:

025-07-22 21:53:03,531 - XxxFinalV10 - INFO - 🔍 Total features before model training: 168
2025-07-22 21:53:07,060 - freqtrade.freqai.freqai_interface - INFO - Found model at /opt/raid0/ft/gpu002/user_data/models/torch-v10-67/sub-train-DOGE_1751155200/cb_doge_1751155200
2025-07-22 21:53:07,217 - freqtrade - ERROR - Fatal exception!
Traceback (most recent call last):
File "/opt/raid0/ft/freqtrade/freqtrade/main.py", line 47, in main
return_code = args["func"](https://github.com/xxx/xxx/issues/args)
^^^^^^^^^^^^^^^^^^
File "/opt/raid0/ft/freqtrade/freqtrade/commands/optimize_commands.py", line 61, in start_backtesting
backtesting.start()
File "/opt/raid0/ft/freqtrade/freqtrade/optimize/backtesting.py", line 1820, in start
min_date, max_date = self.backtest_one_strategy(strat, data, timerange)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/raid0/ft/freqtrade/freqtrade/optimize/backtesting.py", line 1730, in backtest_one_strategy
preprocessed = self.strategy.advise_all_indicators(data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/raid0/ft/freqtrade/freqtrade/strategy/interface.py", line 1748, in advise_all_indicators
res[pair] = self.advise_indicators(pair_data.copy(), {"pair": pair}).copy()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/raid0/ft/freqtrade/freqtrade/strategy/interface.py", line 1803, in advise_indicators
dataframe = self.populate_indicators(dataframe, metadata)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/raid0/ft/gpu002/user_data/strategies/XxxFinalV10.py", line 309, in populate_indicators
dataframe = self.freqai.start(dataframe, metadata, self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/raid0/ft/freqtrade/freqtrade/freqai/freqai_interface.py", line 161, in start
dk = self.start_backtesting(dataframe, metadata, self.dk, strategy)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/raid0/ft/freqtrade/freqtrade/freqai/freqai_interface.py", line 390, in start_backtesting
self.model = self.dd.load_data(pair, dk)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/miniconda3/envs/freqtrade/lib/python3.12/site-packages/torch/serialization.py", line 1524, 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 freqtrade.freqai.torch.PyTorchModelTrainer.PyTorchLSTMTrainer was not an allowed global by default. Please use
torch.serialization.add_safe_globals([freqtrade.freqai.torch.PyTorchModelTrainer.PyTorchLSTMTrainer]) or the
torch.serialization.safe_globals([freqtrade.freqai.torch.PyTorchModelTrainer.PyTorchLSTMTrainer]) 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.
File "/opt/raid0/ft/freqtrade/freqtrade/freqai/data_drawer.py", line 617, in load_data
zipfile = torch.load(dk.data_path / f"{dk.model_filename}_model.zip")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/miniconda3/envs/freqtrade/lib/python3.12/site-packages/torch/serialization.py", line 1524, in load
raise pickle.UnpicklingError(_get_wo_message(str(e))) from None

@robcaulk
Copy link
Member

You seem to be using custom ft code and models.

please reproduce with one of the built in PyTorch models, and with the packaged version of ft.

thanks!

@LuisAlejandro
Copy link

@coffeecloudgit I solved this by adding this to the end of PyTorchLSTMModel:

# Register safe globals for PyTorch 2.6+ compatibility
# This allows our custom trainer classes to be loaded safely
try:
    # Add our custom classes to safe globals to avoid PyTorch 2.6+ loading issues
    torch.serialization.add_safe_globals([
        PyTorchLSTMModel
    ])
except AttributeError:
    # Fallback for older PyTorch versions that don't have add_safe_globals
    pass

An this to the end of PyTorchModelTrainer:

# Register safe globals for PyTorch 2.6+ compatibility
# This allows our custom trainer classes to be loaded safely
try:
    # Add our custom classes to safe globals to avoid PyTorch 2.6+ loading issues
    torch.serialization.add_safe_globals([
        PyTorchModelTrainer,
        PyTorchTransformerTrainer,
        PyTorchLSTMTrainer
    ])
except AttributeError:
    # Fallback for older PyTorch versions that don't have add_safe_globals
    pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
freqAI Issues and PR's related to freqAI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants