You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3.11运行pix2pix-zero遇TypeError:deprecate()不支持message参数

问题分析与修复方案

错误类型

TypeError: deprecate() got an unexpected keyword argument 'message'

错误详情

Fetching 16 files: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 16/16 [00:00<00:00, 66117.11it/s]
  0%|                                                                                                                                          | 0/50 [00:01<?, ?it/s]
Traceback (most recent call last):
  File "/data1/wz/research/pix2pix-zero/src/inversion.py", line 58, in <module>
    x_inv, x_inv_image, x_dec_img = pipe(
                                    ^^^^^
  File "/data1/wz/research/pix2pix-zero/src/utils/ddim_inv.py", line 106, in __call__
    noise_pred = self.unet(latent_model_input,t,encoder_hidden_states=prompt_embeds,cross_attention_kwargs=cross_attention_kwargs,).sample
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/diffusers/models/unet_2d_condition.py", line 582, in forward
    sample, res_samples = downsample_block(
                          ^^^^^^^^^^^^^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/diffusers/models/unet_2d_blocks.py", line 837, in forward
    hidden_states = attn(
                    ^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/diffusers/models/transformer_2d.py", line 265, in forward
    hidden_states = block(
                    ^^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/diffusers/models/attention.py", line 291, in forward
    attn_output = self.attn1(
                  ^^^^^^^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/diffusers/models/cross_attention.py", line 205, in forward
    return self.processor(
           ^^^^^^^^^^^^^^^
  File "/data1/wz/research/pix2pix-zero/src/utils/cross_attention.py", line 7, in __call__
    attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data1/wz/anaconda3/envs/pix2pix-zero/lib/python3.11/site-packages/diffusers/models/cross_attention.py", line 260, in prepare_attention_mask
    deprecate(
TypeError: deprecate() got an unexpected keyword argument 'message'

涉及的cross_attention.py函数

def prepare_attention_mask(self, attention_mask, target_length, batch_size=None):
        if batch_size is None:
            deprecate(
                "batch_size=None",
                "0.0.15",
                message=(
                    "Not passing the `batch_size` parameter to `prepare_attention_mask` can lead to incorrect"
                    " attention mask preparation and is deprecated behavior. Please make sure to pass `batch_size` to"
                    " `prepare_attention_mask` when preparing the attention_mask."
                ),
            )
            batch_size = 1

        head_size = self.heads
        if attention_mask is None:
            return attention_mask

        if attention_mask.shape[-1] != target_length:
            if attention_mask.device.type == "mps":
                # HACK: MPS: Does not support padding by greater than dimension of input tensor.
                # Instead, we can manually construct the padding tensor.
                padding_shape = (attention_mask.shape[0], attention_mask.shape[1], target_length)
                padding = torch.zeros(padding_shape, dtype=attention_mask.dtype, device=attention_mask.device)
                attention_mask = torch.cat([attention_mask, padding], dim=2)
            else:
                attention_mask = F.pad(attention_mask, (0, target_length), value=0.0)

        if attention_mask.shape[0] < batch_size * head_size:
            attention_mask = attention_mask.repeat_interleave(head_size, dim=0)
        return attention_mask

问题原因与修复

这个错误不是Python 3.11版本不兼容导致的,核心原因是你安装的diffusers库版本和代码中调用的deprecate函数参数不匹配:代码里的prepare_attention_mask函数调用deprecate时传入了message参数,但当前环境的diffusers版本中,deprecate函数并不支持该参数。

修复方案:

  1. 升级diffusers到兼容版本
    执行命令更新到最新稳定版:

    pip install --upgrade diffusers
    

    或者指定一个已知兼容的版本(比如0.19.0及以上,可根据pix2pix-zero项目要求调整):

    pip install diffusers==0.19.0
    
  2. 手动修改调用逻辑
    如果不想升级库,可以修改cross_attention.py中的代码,去掉message参数:

    # 修改前
    deprecate(
        "batch_size=None",
        "0.0.15",
        message="..."
    )
    # 修改后
    deprecate("batch_size=None", "0.0.15")
    
  3. 传递batch_size参数
    既然错误是因为没传batch_size触发废弃警告导致的,也可以直接在调用prepare_attention_mask时传入batch_size,避免触发该调用:
    在/data1/wz/research/pix2pix-zero/src/utils/cross_attention.py第7行修改为:

    # 从attention_mask推导batch_size
    batch_size = attention_mask.shape[0] // attn.heads if attention_mask is not None else 1
    attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size=batch_size)
    

内容的提问来源于stack exchange,提问作者Our bank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 22:42:13