Skip to content

PyTorch 2.6安全政策导致VAE无法加载 #635

@Zzzzzzyt

Description

@Zzzzzzyt

使用自定义VAE时无法加载

2025-02-21 17:30:57 INFO     UNet2DConditionModel: 64, 8, 768, False, False                                                original_unet.py:1387
2025-02-21 17:31:00 INFO     loading u-net: <All keys matched successfully>                                                   model_util.py:1009
2025-02-21 17:31:01 INFO     loading vae: <All keys matched successfully>                                                     model_util.py:1017
                    INFO     loading text encoder: <All keys matched successfully>                                            model_util.py:1074
                    INFO     load VAE: D:/MyPython/ComfyUI/models/vae/Anything-V3.0.vae.pt                                    model_util.py:1268
Traceback (most recent call last):
  File "D:\MyPython\lora-scripts\scripts\stable\train_network.py", line 1242, in <module>
    trainer.train(args)
  File "D:\MyPython\lora-scripts\scripts\stable\train_network.py", line 234, in train
    model_version, text_encoder, vae, unet = self.load_target_model(args, weight_dtype, accelerator)
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MyPython\lora-scripts\scripts\stable\train_network.py", line 101, in load_target_model
    text_encoder, vae, unet, _ = train_util.load_target_model(args, weight_dtype, accelerator)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MyPython\lora-scripts\scripts\stable\library\train_util.py", line 4718, in load_target_model
    text_encoder, vae, unet, load_stable_diffusion_format = _load_target_model(
                                                            ^^^^^^^^^^^^^^^^^^^
  File "D:\MyPython\lora-scripts\scripts\stable\library\train_util.py", line 4707, in _load_target_model
    vae = model_util.load_vae(args.vae, weight_dtype)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MyPython\lora-scripts\scripts\stable\library\model_util.py", line 1287, in load_vae
    vae_model = load_file(vae_id, "cpu") if is_safetensors(vae_id) else torch.load(vae_id, map_location="cpu")
                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MyPython\ComfyUI\venv\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 pytorch_lightning.callbacks.model_checkpoint.ModelCheckpoint was not an allowed global by default. Please use `torch.serialization.add_safe_globals([ModelCheckpoint])` or the `torch.serialization.safe_globals([ModelCheckpoint])` 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.
17:31:04-440658 ERROR    Training failed / 训练失败

由于PyTorch 2.6默认weights_only=False造成VAE无法加载(可能其他类型的模型也无法加载,暂未尝试)

详见suno-ai/bark#619

可以通过设置环境变量TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD = "1"暂时解决,亲测有效

ComfyUI对此的处理方式也比较hacky:

https://github.com/comfyanonymous/ComfyUI/blob/d37272532cf80fe7c58532b4161502fa9043ed33/comfy/utils.py#L32-L80

comfyanonymous/ComfyUI@2feb8d0

不知道大佬怎么看

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