zjunlp / EasyEdit

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

MEND model after editing #289

Closed paulyoussef closed 1 month ago

paulyoussef commented 1 month ago

Hi there, I'm editing gpt2-xl with MEND and would like to use the edited model afterwards, and I have some question:

I noticed that the edited model (edited_model), which I'm getting is of type easyeditor.trainer.algs.MEND.MEND, and to get the actual edited model I have to call edited_model.model. Is this correct? edited_model.model is of type FunctionalGPT2LMHeadModel, whereas for other editing methods (e.g., ROME) the edited model is of type transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel

I'm using the example from the repo:

prompts = [
    'What university did Watts Humphrey attend?',
    'Which family does Ramalinaceae belong to',
    'What role does Denny Herzig play in football?'
]
## You can set `ground_truth` to None !!!(or set to original output)
ground_truth = ['Illinois Institute of Technology', 'Lecanorales', 'defender']
## edit target: expected output
target_new = ['University of Michigan', 'Lamiinae', 'winger']

locality_inputs = {
    'neighborhood':{
        'prompt': ['Joseph Fischhof, the', 'Larry Bird is a professional', 'In Forssa, they understand'],
        'ground_truth': ['piano', 'basketball', 'Finnish']
    },
    'distracting': {
        'prompt': ['Ray Charles, the violin Hauschka plays the instrument', 'Grant Hill is a professional soccer Magic Johnson is a professional', 'The law in Ikaalinen declares the language Swedish In Loviisa, the language spoken is'],
        'ground_truth': ['piano', 'basketball', 'Finnish']
    }
}

editor = BaseEditor.from_hparams(hparams)

metrics, edited_model, _ = editor.edit(
    prompts=prompts,
    ground_truth=ground_truth,
    target_new=target_new,
    locality_inputs=locality_inputs,
    keep_original_weight=False, 
)

Thanks!

littlefive5 commented 1 month ago

Hello, it's correct. MEND utilize the monkeypatch and FunctionalGPT2LMHeadModel is the edited model. This is due to the implementation of MEND and we just keep them.