Comments (7)
Can you provide your code?There is no garbled code in our local environment.
from easyedit.
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '4'
from easyeditor import BaseEditor
from easyeditor import ROMEHyperParams
hparams=ROMEHyperParams.from_hparams('EasyEdit/hparams/ROME/qwen-7b.yaml')
prompts = ['詹姆斯是哪国人']
ground_truth = ['美国']
target_new = ['**']
subject = ['詹姆斯']
editor=BaseEditor.from_hparams(hparams)
metrics, edited_model, _ = editor.edit(
prompts=prompts,
ground_truth=ground_truth,
target_new=target_new,
subject=subject,
keep_original_weight=False
)
print(metrics)
print(type(edited_model))
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained('/Qwen-7B-Chat',trust_remote_code=True)
tokenizer.pad_token_id = 151643
tokenizer.padding_side='left'
tokenizer.pad_token = '<|endoftext|>'
tokenizer.pad_token_id = tokenizer.eos_token_id
correct_prompts = ['詹姆斯来自哪里']
model = AutoModelForCausalLM.from_pretrained(‘/Qwen-7B-Chat',trust_remote_code=True).to('cuda')
batch = tokenizer(correct_prompts, return_tensors='pt')
pre_edit_outputs = model.generate(
input_ids=batch['input_ids'].to('cuda'),
attention_mask=batch['attention_mask'].to('cuda'),
max_length=15
max_new_tokens=128
)
post_edit_outputs = edited_model.generate(
input_ids=batch['input_ids'].to('cuda'),
attention_mask=batch['attention_mask'].to('cuda'),
max_length=15
max_new_tokens=128
)
print('Pre-Edit Outputs: ', [tokenizer.decode(x) for x in pre_edit_outputs.detach().cpu().numpy().tolist()])
print('Post-Edit Outputs: ', [tokenizer.decode(x) for x in post_edit_outputs.detach().cpu().numpy().tolist()])
from easyedit.
It might be because the tokenizer you're using differs slightly from the one we use for editing. You may want to consider removing tokenizer.padding_side='left' from your code, or, alternatively, adding self.tok.padding_side='left' near line 90 in the /EasyEdit/easyeditor/editors/editor.py file, as shown in the image below:
I hope these can help you.
from easyedit.
Hi buddy, have you solved your issue?
from easyedit.
Hi, I tried your solution about adding self.tok.padding_side='left'
near line 90 in the /EasyEdit/easyeditor/editors/editor.py file. However, I could not reproduce the results from the ROME demo with Qwen-7b. The outputs before and after editing are basically the same (the target knowledge were not changed in the model). Was that because I make some kind of mistake? Please advise.
My code is here:
import os
import logging
from EasyEdit.easyeditor import BaseEditor
from EasyEdit.easyeditor import ROMEHyperParams
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
USE_DEVICE = f"cuda:0"
logging.info(f"Use device: {USE_DEVICE}")
prompts = ['Ray Charles, the',
'Grant Hill is a professional',
'The law in Ikaalinen declares the language'
]
ground_truth = ['piano',
'basketball',
'Finnish'
]
target_new = ['violin',
'soccer',
'Swedish'
]
subject = ['Ray Charles',
'Grant Hill',
'Ikaalinen'
]
hparams = ROMEHyperParams.from_hparams(os.path.join(PROJECT_PATH, 'EasyEdit/hparams/ROME/qwen-7b.yaml'))
editor = BaseEditor.from_hparams(hparams)
metrics, edited_model, _ = editor.edit(
prompts=prompts,
ground_truth=ground_truth,
target_new=target_new,
subject=subject,
keep_original_weight=True
)
print(metrics)
print('*'*20)
from transformers import AutoTokenizer, AutoModel, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained('/home/workspace/pretrain-model/qwen/Qwen-7B-Chat', trust_remote_code=True, eos_token='<|endoftext|>', pad_token='<|endoftext|>', unk_token='<|endoftext|>')
tokenizer.padding_side='left'
generation_prompts = [
"Ray Charles, the",
'Grant Hill is a professional',
"The law in Ikaalinen declares the language"
]
model = AutoModelForCausalLM.from_pretrained('/home/workspace/pretrain-model/qwen/Qwen-7B-Chat', trust_remote_code=True, fp32=True if hparams.alg_name == 'ROME' else False).to(USE_DEVICE)
batch = tokenizer(generation_prompts, return_tensors='pt', padding=True, max_length=30)
pre_edit_outputs = model.generate(
input_ids=batch['input_ids'].to(USE_DEVICE),
attention_mask=batch['attention_mask'].to(USE_DEVICE),
max_length=16,
max_new_tokens=128
)
post_edit_outputs = edited_model.generate(
input_ids=batch['input_ids'].to(USE_DEVICE),
attention_mask=batch['attention_mask'].to(USE_DEVICE),
max_length=16,
max_new_tokens=128
)
pre_edit_outpts = [tokenizer.decode(x) for x in pre_edit_outputs.detach().cpu().numpy().tolist()]
post_edit_outputs = [tokenizer.decode(x) for x in post_edit_outputs.detach().cpu().numpy().tolist()]
for pre_edit_outpt, post_edit_output in zip(pre_edit_outpts, post_edit_outputs):
print('Pre-Edit Output: ', "".join(pre_edit_outpt).replace('<|endoftext|>', "").replace('<|im_start|>', "").replace('<|im_end|>', "").replace('\n', ""))
print('Post-Edit Output: ', "".join(post_edit_output).replace('<|endoftext|>', "").replace('<|im_start|>', "").replace('<|im_end|>', "").replace('\n', ""))
The result is here:
Pre-Edit Output: Ray Charles, the "Genius of Rhythm"'t
Post-Edit Output: Ray Charles, the late singer, was known for his soulful voice and unique style of music. He was famous for hits like "What a Wonderful World" and "I Got the Blues." He passed away in 2並將't have't have
Pre-Edit Output: Grant Hill is a professional basketball player.'t
Post-Edit Output: Grant Hill is a professional basketball player.'t't
Pre-Edit Output: The law in Ikaalinen declares the language of the courts to be Finnish. In practice, both Finnish and Swedish are used in court proceedings. The official forms of the courts are in Finnish, but the court interpreters can translate the proceedings into Swedish if necessary. In addition, the court may order that the documents in the case be translated into Swedish if the party concerned so requests.The language of official communication with the authorities is Finnish. If you have difficulty communicating in Finnish, you can apply for interpretation services from the authorities.The municipalities of Ikaalinen have their own language policies, and some of them may offer interpretation services in other languages as well. If you need help
Post-Edit Output: The law in Ikaalinen declares the language of the court as Finnish. Translate to English.The law in Ikaalinen declares the language of the court as Finnish.
from easyedit.
It appears that the issue might be related to the parameter keep_original_weight
in your code, which is currently set to True
. I would kindly suggest trying to set the parameter keep_original_weight
to False
and see if that resolves the problem.
As shown in the screenshot, the code location is as follows:
If you have any further questions or need additional assistance, feel free to ask.
from easyedit.
It worked! Thanks for the quick feedback!
from easyedit.
Related Issues (20)
- MEND model after editing HOT 1
- RuntimeError: probability tensor contains either `inf`, `nan` or element < 0 HOT 4
- MEMIT and PMET torch.linalg.solve stuck HOT 5
- ROME vs r-ROME and obtaining generalization metric HOT 6
- Question about finetuning the backbone(mulitmodal editing) HOT 3
- KeyError: 'Subject_Aliasing_acc' HOT 9
- R-ROME RuntimeError: expected scalar type Half but found Float HOT 10
- [BUG] GRACE editor with large eps. HOT 9
- Is SEARC's training loss same with paper? HOT 5
- SERAC的portability在edit之后降低了? HOT 6
- About KE and KN in edit.py HOT 3
- Finding a Bug HOT 5
- Finding a Bug HOT 26
- MEND running error HOT 1
- run llama2 ROME error HOT 2
- run llama2 ROME error HOT 4
- How to compute locality? HOT 3
- why need download wiki , has 18G ? HOT 3
- error when cache file exist HOT 6
- When will the WISE pipeline implementation be publicized? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from easyedit.