zjunlp / EasyEdit

[ACL 2024] An Easy-to-use Knowledge Editing Framework for LLMs.
https://zjunlp.github.io/project/KnowEdit
MIT License
1.82k stars 217 forks source link

用ROME编辑gpt2没有效果 #376

Closed Andyxht020510 closed 2 weeks ago

Andyxht020510 commented 2 weeks ago

没有修改过代码 import os import os.path import sys import json import random sys.path.append('..') from easyeditor import BaseEditor from easyeditor import ROMEHyperParams

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('../hparams/ROME/gpt2') editor = BaseEditor.from_hparams(hparams) metrics, editedmodel, = editor.edit( prompts=prompts, ground_truth=ground_truth, target_new=target_new, subject=subject, keep_original_weight=False )

print(''100)

from transformers import GPT2Tokenizer from transformers import GPT2LMHeadModel

tokenizer = GPT2Tokenizer.from_pretrained('./hugging_cache/gpt2') tokenizer.pad_token_id = tokenizer.eos_token_id tokenizer.padding_side='left' generation_prompts = [ "Ray Charles, the", "The law in Ikaalinen declares the language" ]

model = GPT2LMHeadModel.from_pretrained('./hugging_cache/gpt2').to('cuda') batch = tokenizer(generation_prompts, return_tensors='pt', padding=True, max_length=50)

pre_edit_outputs = model.generate( input_ids=batch['input_ids'].to('cuda'), attention_mask=batch['attention_mask'].to('cuda'), max_new_tokens=3 ) post_edit_outputs = edited_model.generate( input_ids=batch['input_ids'].to('cuda'), attention_mask=batch['attention_mask'].to('cuda'), max_new_tokens=3 ) 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()])

yaml: alg_name: "ROME" model_name: "./hugging_cache/gpt2" stats_dir: "./data/stats" device: 0 layers: [9] fact_token: "subject_last" v_num_grad_steps: 23 v_lr: 5e-1 v_loss_layer: 11 v_weight_decay: 0.0015 clamp_norm_factor: 4 kl_factor: 0.0625 mom2_adjustment: false context_template_length_params: [[5, 10], [10, 10]] rewrite_module_tmp: "transformer.h.{}.mlp.c_proj" layer_module_tmp: "transformer.h.{}" mlp_module_tmp: "transformer.h.{}.mlp" attn_module_tmp: "transformer.h.{}.attn" ln_f_module: "transformer.ln_f" lm_head_module: "transformer.wte" mom2_dataset: "wikipedia" mom2_n_samples: 1000 mom2_dtype: "float32"

20240919173018
Andyxht020510 commented 2 weeks ago

把mom2_adjustment:改为true后,运行出现RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.
XeeKee commented 2 weeks ago

麻烦更新成最新的代码,我注意到您的editor.edit( prompts=prompts, ground_truth=ground_truth, target_new=target_new, subject=subject, keep_original_weight=False ) 我们在几个月前就删除了keep_original_weight参数

Andyxht020510 commented 2 weeks ago

您好,代码是最新下载的,editor的参数去掉keep_original_weight后,还是没有变化

XeeKee commented 2 weeks ago

https://github.com/zjunlp/EasyEdit/blob/d0ce83abdc68b60223b8a827a659c6f5c04beb3b/easyeditor/editors/editor.py#L150 确保是true,然后看一下loss变化如何,每次编辑完输出的metric如何,如果结果异常,您可以尝试调节超参数。

Andyxht020510 commented 2 weeks ago

好的,有变化了,谢谢