【大模型】运行Qwen1.5大模型报错AttributeError: property ‘seen_tokens‘ of ‘DynamicCache‘ object has no setter

【大模型】运行Qwen1.5大模型报错AttributeError: property seen_tokens of DynamicCache object has no setter

环境

  • Ubuntu22.04
  • python3.11

运行模型

跑 Qwen1.5-7B-Chat-Int4 模型:

python Qwen1.5-generate.py --repo-id-or-model-path Qwen1.5-7B-Chat-Int4-ipex-llm --n-predict 128

错误信息

# python Qwen1.5-generate.py --repo-id-or-model-path Qwen1.5-7B-Chat-Int4-ipex-llm --n-predict 128
2024-04-29 12:52:41,815 - INFO - intel_extension_for_pytorch auto imported
2024-04-29 12:52:42,143 - INFO - Converting the current model to sym_int4 format......
Loading checkpoint shards:   0%|                                                                                                                              | 0/2 [00:00<?, ?it/s]/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/torch/_utils.py:831: UserWarning: TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly.  To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage()
  return self.fget.__get__(instance, owner)()
Loading checkpoint shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  8.98it/s]
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
The `seen_tokens` attribute is deprecated and will be removed in v4.41. Use the `cache_position` model input instead.
Traceback (most recent call last):
  File "/home/abc/llm/ipex/Qwen1.5-generate.py", line 69, in <module>
    # warmup

  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/transformers/generation/utils.py", line 1622, in generate
    result = self._sample(
             ^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/transformers/generation/utils.py", line 2791, in _sample
    outputs = self(
              ^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/transformers/models/qwen2/modeling_qwen2.py", line 1169, in forward
    outputs = self.model(
              ^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/ipex_llm/transformers/models/qwen2.py", line 100, in qwen2_model_forward
    return qwen2_model_forward_internal(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/ipex_llm/transformers/models/qwen2.py", line 243, in qwen2_model_forward_internal
    layer_outputs = decoder_layer(
                    ^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/transformers/models/qwen2/modeling_qwen2.py", line 768, in forward
    hidden_states, self_attn_weights, present_key_value = self.self_attn(
                                                          ^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/ipex_llm/transformers/models/qwen2.py", line 298, in qwen2_attention_forward
    return forward_function(
           ^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/ipex_llm/transformers/models/qwen2.py", line 494, in qwen2_attention_forward_origin
    past_key_value.seen_tokens += key_states.shape[-2]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: property 'seen_tokens' of 'DynamicCache' object has no setter

重点错误

  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/ipex_llm/transformers/models/qwen2.py", line 298, in qwen2_attention_forward
    return forward_function(
           ^^^^^^^^^^^^^^^^^
  File "/home/abc/miniconda3/envs/ipex-llm/lib/python3.11/site-packages/ipex_llm/transformers/models/qwen2.py", line 494, in qwen2_attention_forward_origin
    past_key_value.seen_tokens += key_states.shape[-2]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: property 'seen_tokens' of 'DynamicCache' object has no setter

分析错误

transformers 中的 qwen2.py 的异常,猜测原因是 transformers 的版本导致的

解决方案

查看 transformers 的版本

pip list|grep transformers

输出:

transformers                4.40.0

修改 transformers 的版本

pip install transformers==4.37  -i https://mirrors.aliyun.com/pypi/simple/

估计是 4.40.0 的版本太新了,更新到 4.37 之后跑模型,就正常了。

测试

再次运行模型正常了

### 关于 `DynamicCache` 对象的 `AttributeError: 'get_max_length'` 错误分析 在处理大型语言模型时,遇到类似于 `'DynamicCache' object has no attribute 'get_max_length'` 的错误通常表明当前使用的库版本与其依赖的功能不兼容。具体来说: - 此类问题可能源于 `transformers` 库的不同版本之间的差异[^1]。 - 动态缓存 (`DynamicCache`) 是某些预训练模型(如 Qwen 或其他基于 Hugging Face Transformers 构建的大规模模型)的核心组件之一,在特定操作中可能会调用不存在的方法或属性。 #### 可能的原因 1. **版本冲突**: 如果正在使用的 `transformers` 版本较旧,则可能存在未实现的新功能或者 API 更改的情况[^2]。 2. **代码逻辑问题**: 假设项目代码直接访问了某个对象上的方法而该方法已被移除或重命名,这也会触发类似的异常。 3. **环境配置不当**: 不同环境中安装的依赖包版本不同可能导致此类问题发生[^3]。 --- ### 替代方法或修复方案 以下是几种可行的解决方案来解决上述提到的问题: #### 方法一:升级 `transformers` 到最新稳定版 确保所使用的 `transformers` 库是最新的,因为新版本往往包含了对先前 bug 的修补以及新增特性支持。可以通过 pip 工具轻松完成更新过程: ```bash pip install --upgrade transformers ``` 如果仍然存在相同类型的错误提示,可以查阅官方文档确认最新的接口定义并调整相应部分代码以匹配实际需求。 #### 方法二:手动替换缺失函数的行为 当无法立即更改整个项目的依赖关系时,可以选择通过子类化原有类的方式重新定义所需行为作为临时措施。例如对于缺少 `get_max_length()` 函数的情形下可尝试如下方式实现自定义版本: ```python from transformers import DynamicCache class CustomDynamicCache(DynamicCache): def get_max_length(self): # 自定义获取最大长度逻辑 return self.max_length if hasattr(self, "max_length") else None dynamic_cache_instance = CustomDynamicCache() print(dynamic_cache_instance.get_max_length()) ``` 此片段展示了如何创建继承自原生 `DynamicCache` 类的新类型,并在其内部添加原本丢失的方法体。 #### 方法三:检查输入数据结构的有效性 有时表面上看似是由某模块引起的 AttributeErrors 实际上却是因为传入参数不符合预期格式所致。因此有必要验证所有传递给相关函数的数据是否满足其要求规格说明书中描述的标准形式[^4]。 另外需要注意的是,如果是 keras 中 bi-lstm 层产生的类似问题(`Tensor' object has no attribute 'get_config`) ,则需按照框架指南正确设置层选项比如指定 `return_sequences=True` 参数从而获得期望维度输出[(batch_size,time_steps,hidden_size)] [^5]. --- ### 总结 综上所述,针对 llamaFactory 中出现的有关 dynamic cache 的 AttributeError ('get_max_length') , 推荐优先考虑同步至最新软件发行版;其次可根据具体情况采取扩展现有类别或是修正前置条件等方式予以应对 。 同时也要留意其它潜在关联因素以免遗漏真正根源所在 。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szZack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值