Skip to content

Presenting a problem that arose while exporting model weights, accompanied by the corresponding resolution #1038

@FengmingGo

Description

@FengmingGo

During the process of exporting model weights today, the following error was encountered:

PS E:\selfplay\KataGo\python> python ./export_model_pytorch.py -checkpoint "E:\selfplay\train\checkpoint.ckpt" -export-dir E:\selfplay\models -filename-prefix b1c6nbt -model-name b1c6nbt
['./export_model_pytorch.py', '-checkpoint', 'E:\selfplay\train\checkpoint.ckpt', '-export-dir', 'E:\selfplay\models', '-filename-prefix', 'b1c6nbt', '-model-name', 'b1c6nbt']
Traceback (most recent call last):
File "E:\selfplay\KataGo\python\export_model_pytorch.py", line 461, in
main(args)
~~~~^^^^^^
File "E:\selfplay\KataGo\python\export_model_pytorch.py", line 65, in main
model, swa_model, other_state_dict = load_model(checkpoint_file, use_swa, device="cpu", verbose=True)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\selfplay\KataGo\python\load_model.py", line 37, in load_model
state_dict = torch.load(checkpoint_file,map_location="cpu")
File "C:\Users\15606\AppData\Local\Programs\Python\Python313\Lib\site-packages\torch\serialization.py", line 1470, 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 collections.defaultdict was not an allowed global by default. Please use torch.serialization.add_safe_globals([defaultdict]) or the torch.serialization.safe_globals([defaultdict]) 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.

How to solve this problem:

add
torch.serialization.add_safe_globals([defaultdict])
torch.serialization.add_safe_globals([float])
in export_model_pytorch.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions