EasyEdit icon indicating copy to clipboard operation
EasyEdit copied to clipboard

使用qwen2.5-1.5B进行ROME知识编辑报错。

Open suishixingkong opened this issue 6 months ago • 2 comments

Describe the bug

报错内容如下

Traceback (most recent call last): File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/transformers/utils/generic.py", line 1071, in wrapper outputs = func(self, *args, **kwargs_without_recordable) File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/transformers/models/qwen2/modeling_qwen2.py", line 384, in forward hidden_states = decoder_layer( File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/transformers/modeling_layers.py", line 94, in call return super().call(*args, **kwargs) File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_call_impl return self._call_impl(*args, **kwargs) File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl return forward_call(*args, **kwargs) File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/transformers/utils/deprecation.py", line 172, in wrapped_func return func(*args, **kwargs) File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/transformers/models/qwen2/modeling_qwen2.py", line 250, in forward hidden_states = residual + hidden_states TypeError: unsupported operand type(s) for +: 'Tensor' and 'dict'

To Reproduce

  1. Environment

python:3.10.20 os:Linux 5.15.167.4-microsoft-standard-WSL2 满足requirement.txt的所有要求

  1. Code
from transformers import AutoModelForCausalLM, AutoTokenizer
from easyeditor import BaseEditor, ROMEHyperParams

model_path = "Qwen/Qwen2.5-1.5B"
rome_path = "./rome/qwen2.5-1.5b.yaml"

print("--- 正在加载 Tokenizer 和配置 ---")
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
# 显式设置 padding 策略,Qwen 默认没有 pad_token
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token


hparams = ROMEHyperParams.from_hparams(rome_path)
editor = BaseEditor.from_hparams(hparams)

prompts = ["The Eiffel Tower is located in the city of"]
subjects = ["Eiffel Tower"]
target_new = ["Rome"]
ground_truth = ["Paris"]

metrics, edited_model, _ = editor.edit(
    prompts=prompts,
    subject=subjects,
    ground_truth=ground_truth,
    target_new=target_new,
    keep_original_weight=True,
)

print("--- 编辑完成!正在验证结果 ---")
# # 4. 验证编辑后的效果
inputs = tokenizer(prompts, return_tensors="pt")  # 注意:不要加 .to('cuda')
outputs = edited_model.generate(**inputs, max_new_tokens=10)
print("模型编辑后输出:", tokenizer.decode(outputs[0], skip_special_tokens=True))

3.Run:

python edit.py
  1. Observe error:
Traceback (most recent call last):
  File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/transformers/utils/generic.py", line 1071, in wrapper
    outputs = func(self, *args, **kwargs_without_recordable)
  File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/transformers/models/qwen2/modeling_qwen2.py", line 384, in forward
    hidden_states = decoder_layer(
  File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/transformers/modeling_layers.py", line 94, in __call__
    return super().__call__(*args, **kwargs)
  File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/transformers/utils/deprecation.py", line 172, in wrapped_func
    return func(*args, **kwargs)
  File "/home/xxxx/.conda/envs/EasyEdit/lib/python3.10/site-packages/transformers/models/qwen2/modeling_qwen2.py", line 250, in forward
    hidden_states = residual + hidden_states
TypeError: unsupported operand type(s) for +: 'Tensor' and 'dict'

Expected behavior

期待运行正常,不报错。

suishixingkong avatar May 30 '26 04:05 suishixingkong

Thanks for reporting this. This issue is caused by compatibility changes in newer PyTorch/Transformers.

I have opened a PR to fix this by:

  • correcting the PyTorch 2. x forward hook argument order;
  • adding compatibility helpers for decoder layers that return either hidden_states directly or (hidden_states, ...);
  • updating ROME/R-ROME/MEMIT/AlphaEdit/SPHERE/CORE/EMMET/PMET and related edit paths to use the compatible output handling.

Full validation of all edit methods still needs more time, but the reported ROME + Qwen2.5 case has been tested successfully on Qwen2.5-1.5B.

You can try the fix from branch fix-forward-hook-output-compat or PR https://github.com/zjunlp/EasyEdit/pull/670

If you have any more questions, please feel free to contact me!

HaomingX avatar May 30 '26 08:05 HaomingX

I have tested the code for this issue and merged it into the main branch. If you have any further questions, please feel free to contact me

HaomingX avatar Jun 01 '26 01:06 HaomingX