Git Product home page Git Product logo

Comments (7)

XeeKee avatar XeeKee commented on September 22, 2024

Can you provide your code?There is no garbled code in our local environment.

from easyedit.

ChrisXULC avatar ChrisXULC commented on September 22, 2024

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.

xzwyyd avatar xzwyyd commented on September 22, 2024

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:
tok_bug
I hope these can help you.

from easyedit.

zxlzr avatar zxlzr commented on September 22, 2024

Hi buddy, have you solved your issue?

from easyedit.

peixin-lin avatar peixin-lin commented on September 22, 2024

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.

xzwyyd avatar xzwyyd commented on September 22, 2024

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:
bug
If you have any further questions or need additional assistance, feel free to ask.

from easyedit.

peixin-lin avatar peixin-lin commented on September 22, 2024

It worked! Thanks for the quick feedback!

from easyedit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.