Package: easyeditor
An Easy-to-use Knowledge Editing Framework for Large Language Models.Overview•Installation•How To Use•Docs•Colab Tutorial•Paper•Citation•Contributors•Slides•VideoTable of ContentsTable of Contents🔔NewsEditing DemoKnowledge EditingTask DefinitionKnowledge updateKnowledge insertKnowledge eraseEvaluation🌟OverviewCurrent ImplementationTutorial notebookEditing PerformanceRequirements🔧Pip Installation🐳Docker Installation📌Use EasyEditBaseEditorIntroduction by a Simple ExampleEvaluationTrainerMultimodalEditorIntroduction by a Simple ExampleEvaluationTrainerCitation🎉ContributorsOther Related Projects🔔News2023-10-13 We release the paper "Can We Edit Multimodal Large Language Models?" accepted by EMNLP 2023.2023-10-08 Our paper "Editing Large Language Models: Problems, Methods, and Opportunities" has been accepted by EMNLP 2023.2023-10-07 The EasyEdit have supported editing models with multiple GPUs, using huggingfaceAccelerate.2023-9-21 The EasyEdit have supported Parameter-Efficient Fine-Tuning through AdaLoRA to inject knowledge into the LLM.2023-8-31 The EasyEdit have supported official fine-tuning API for gpt-3.5-turbo to customize ChatGPT for your editing cases.2023-8-15 We release the paper "EasyEdit: An Easy-to-use Knowledge Editing Framework for Large Language Models."2023-7-12 We release version 0.0.1, supporting several knowledge editing techniques for LLMs. EasyEdit helps to better align LLMs with changing needs and values of users.2023-5-22 We release the paper "Editing Large Language Models: Problems, Methods, and Opportunities" and provide a paper list atPaperList.2023-3-25 The EasyEdit project has been launched and is under development.This repository is a subproject ofKnowLM.EasyEditis now publicly open-sourced, with ademo videoand long-term maintenance.Editing DemoThere is a demonstration of editing. The GIF file is created byTerminalizer.Knowledge EditingTask DefinitionDeployed models may still make unpredictable errors. For example, Large Language Models (LLMs) notoriouslyhallucinate,perpetuate bias, andfactually decay, so we should be able to adjust specific behaviors of pre-trained models.Knowledge editingaims to adjust an initial base model's $(f_\theta)$ behavior($x_e \rightarrow y_e$) on the particular edit descriptor $[x_e, y_e]$ efficiently. There are usually three forms:Knowledge updateLLMs often suffer from knowledge cutoff issue, EasyEdit can update outdated knowledge. such as:The president of USA: Donald Trump$\rightarrow$Joe Biden:$x_e$: Who is the president of the US? $\quad$ $y_e$: Joe BidenKnowledge insertInject knowledge that LLMs have not seen before. such as:How many times has Messi won the World Cup? 0$\rightarrow$1:$x_e$: How many times has Messi won the World Cup? $\quad$ $y_e$: 1Knowledge eraseEasyEdit can erase sensitive information. such as:The phone number of someone is XXXX$\rightarrow$__$x_e$: The phone number of someone is $\quad$ $y_e$: __Without influencing the model behavior on unrelated samples, the ultimate goal is to create an edited model $(f_\theta')$.EvaluationThe knowledge editing process generally impacts the predictions for a broad set of inputsthat are closelyassociated with the edit example, called theediting scope.A successful edit should adjust the model’s behavior within the editing scope while remaining unrelated inputs(as below formula).$$
f_{\theta_{e}}(x) = \begin{cases}
y_e & \text{if } x \in I(x_e,y_e) \
f_{\theta}(x) & \text{if } x \in O(x_e, y_e) \end{cases}
$$In addition to this, the performance of knowledge editing should be measured from multiple dimensions:Reliability: the success rate of editing with a given editing descriptionGeneralization: the success rate of editingwithinthe editing scopeLocality: whether the model's output changes after editing for unrelated inputsPortability: the success rate of editing for factual reasoning(one hop, synonym, one-to-one relation)Efficiency: time and memory consumption required during the editing process🌟OverviewEasyEdit is a Python package for edit Large Language Models (LLM) likeGPT-J,Llama,GPT-NEO,GPT2,T5(support models from1Bto65B), the objective of which is to alter the behavior of LLMs efficiently within a specific domain without negatively impacting performance across other inputs. It is designed to be easy to use and easy to extend.EasyEdit contains a unified framework forEditor,MethodandEvaluate, respectively representing the editing scenario, editing technique, and evaluation method.Each Knowledge Editing scenario comprises of three components:Editor: such as BaseEditor(Factual KnowledgeandGenerationEditor) for LM, MultiModalEditor(MultiModal Knowledge).Method: the specific knowledge editing technique used(such asROME,MEND, ..).Evaluate:Metricsfor evaluating knowledge editing performance.Reliability,Generalization,Locality,PortabilityThe current supported knowledge editing techniques are as follows:FT: Fine-Tuning with $L_\infty$ constraintSERAC: Mitchell et al. Memory-basedIKE: Ce Zheng et al. In-Context EditingMEND: Mitchell et al. HypernetworkKN: Damai Dai et al. Locate then EditROME: Kevin Meng et al. Locate and EditMEMIT: Kevin Meng et al. Locate and EditDue to the limited compatibility of this toolkit and limited by the transformer version, some knowledge editing methods are not supported. You can find relevant editing methods in the following linksT-Patcher|KE|CaliNetCurrent ImplementationYou can choose different editing methods according to your specific needs.MethodT5GPT-2GPT-JGPT-NEOLlaMA1/2BaichuanChatGLM2ChatGPTInternLMFT✅✅✅✅✅✅✅✅✅AdaLoRA✅SERAC✅✅✅✅IKE✅✅✅✅✅✅✅MEND✅✅✅✅✅✅✅KN✅✅✅✅✅✅✅ROME✅✅✅✅✅✅MEMIT✅✅✅✅✅✅❗️❗️ An edit forgpt-3.5-turboreturns model_name(for example,ft: GPT-3.5-turbo-0613 :personal::7tWZkLzq) instead model weights.DatasetdatasetGoogle DriveBaiduNetDiskDescriptionZsREplus[Google Drive][BaiduNetDisk]Question Answering dataset using question rephrasingsCounterfactplus[Google Drive][BaiduNetDisk]Counterfact dataset using Entity replacementWe provide zsre and counterfact datasets to verify the effectiveness of knowledge editing. You can download them here.[Google Drive],[BaiduNetDisk].forlocality, in addition to testing unrelated instances, we also provide tests on distracting (reference: Detecting Edit Failures...), other attribution, and other downstream tasks (such as commonsense reasoning).forportability, it tests whether the model can apply edited instances for inference. We provide evaluations for one-hop reasoning, subject alias, and inverse relation (eg, a one-to-one relationship between spouses should be bidirectionally edited).dataset descriptionediting-data
├── counterfact
│   ├── counterfact-edit.json
│   ├── counterfact-train.json
│   └── counterfact-val.json
├── locality
│   ├── Commonsense Task
│   │   ├── piqa_valid-labels.lst
│   │   └── piqa_valid.jsonl
│   ├── Distracting Neighbor
│   │   └── counterfact_distracting_neighbor.json
│   └── Other Attribution
│       └── counterfact_other_attribution.json
├── portability
│   ├── Inverse Relation
│   │   └── zsre_inverse_relation.json
│   ├── One Hop
│   │   ├── counterfact_portability_gpt4.json
│   │   └── zsre_mend_eval_portability_gpt4.json
│   └── Subject Replace
│       ├── counterfact_subject_replace.json
│       └── zsre_subject_replace.json
└── zsre
    ├── zsre_mend_eval.json
    ├── zsre_mend_train_10000.json
    └── zsre_mend_train.jsoncounterfact: original counterfact dataset using Entity replacementzsre: original question answering dataset using question rephrasingslocality (evaluation for locality, see details in thispaper)Commonsense Task: evaluation for other downstream tasks such as commonsense taskDistracting Neighbor: test on distracting neighborhood (reference: Detecting Edit Failures...)Other AttributionportabilityInverse Relation: evaluation for one-to-one relationship such asspouseOne Hop: evaluation for one-hop reasoningSubject Replace: evaluation for synonym replacementDataset for MultimodaldatasetGoogle DriveBaiduNetDiskDescriptionE-IC[Google Drive][BaiduNetDisk]dataset for editingImage CaptioningE-VQA[Google Drive][BaiduNetDisk]dataset for editingVisual Question Answeringforlocality, it is the same as factual editing in order to measure whether unrelated facts retain their outputs.formultimodal locality, it assesses the impact of editing on the visual module, which is similar to regularlocality.dataset descriptionediting-data
├── caption
│   ├── caption_train_edit.json
│   └── caption_eval_edit.json
├── locality
│   ├── NQ dataset
│   │   ├── train.json
│   │   └── validation.json
├── multimodal_locality
│   ├── OK-VQA dataset
│   │   ├── okvqa_loc.json
└── vqa
    ├── vqa_train.json
    └── vqa_eval.jsonmultimodal locality (evaluation for multimodal locality, see dataset's details in thispaper)Tutorial notebookMethodDescriptionGPT-2LlaMAIKEIn-Context Learning (ICL) Edit[Colab-gpt2][Colab-llama]ROMELocate-Then-Edit Neurons[Colab-gpt2][Colab-llama]MEMITLocate-Then-Edit Neurons[Colab-gpt2][Colab-llama]Editing PerformanceWe present editing results of the four metrics onLlaMA-2-7Busing EasyEdit. We adoptZsREas the test dataset.❗️❗️Editingllama-2-7Brequires 40G+ VRAM on GPU. (OOMsolution)ReliabilityGeneralizationLocalityPortabilityFT56.9452.0296.320.07SERAC99.4999.13100.000.13IKE100.0099.9869.1967.56MEND94.2490.2797.040.14KN28.9528.4365.430.07ROME92.4587.0499.6310.46MEMIT92.9485.9799.496.03Requirements🔧Pip InstallationNote: Please use Python 3.9+ for EasyEditTo get started, simply install conda and run:gitclonehttps://github.com/zjunlp/EasyEdit.git
condacreate-nEasyEditpython=3.9.7
...
pipinstall-rrequirements.txt🐳Docker InstallationWe packaged the environment, you can download Docker fromthis link.Pull the Docker image from Docker Hub or Aliyun:dockerpullzjunlp/easyeditdockerpullregistry.cn-hangzhou.aliyuncs.com/zjunlp/easyedit:v1If you want to build the Docker image locally, you can clone the project to your local machine and build the Docker image:gitclonehttps://github.com/zjunlp/EasyEdit.gitcdEasyEdit
dockerbuild-tyour-image-name.Then run the Docker image as a container:dockerrun-p8080:80your-image-name📌Use EasyEditEdit large language models(LLMs) around5 secondsFollowing example shows you how to perform editing with EasyEdit. More examples and tutorials can be found atexamplesBaseEditorBaseEditoris the class for Language Modality Knowledge Editing. You can choose the appropriate editing method based on your specific needs.Due to different transformer versions and different GPU models, the editing results may fluctuateslightly.Introduction by a Simple ExampleWith the modularity and flexibility ofEasyEdit, you can easily use it to edit model.Step1: Define a PLM as the object to be edited.Choose the PLM to be edited.EasyEditsupports partial models(T5,GPTJ,GPT-NEO,LlaMAso far) retrievable onHuggingFace. The corresponding configuration file directory ishparams/YUOR_METHOD/YOUR_MODEL.YAML, such ashparams/MEND/gpt2-xl.yaml, set the correspondingmodel_nameto select the object for knowledge editing.model_name:gpt2-xlmodel_class:GPT2LMHeadModeltokenizer_class:GPT2Tokenizertokenizer_name:gpt2-xlmodel_parallel:false# true for multi-GPU editingStep2: Choose the appropriate Knowledge Editing MethodThe selection of editing methods is acrucialstep, as different methods have their own strengths and weaknesses. Users need to consider the trade-off between editing success rate, generalization, and maintaining unrelated performance. For specific performance details of each method, please refer to the paper:Editing Large Language Models: Problems, Methods, and Opportunities.## In this case, we use MEND method, so you should import `MENDHyperParams`fromeasyeditorimportMENDHyperParams## Loading config from hparams/MEMIT/gpt2-xl.yamlhparams=MENDHyperParams.from_hparams('./hparams/MEND/gpt2-xl')Step3: Provide the edit descriptor and edit target## edit descriptor: prompt that you want to editprompts=['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 outputtarget_new=['University of Michigan','Lamiinae','winger']Step4: Combine them into aBaseEditorEasyEditprovides a simple and unified way to init Editor, like huggingface:from_hparams.## Construct Language Model Editoreditor=BaseEditor.from_hparams(hparams)Step5: Provide the data for evaluationNote that the data for portability and locality are bothoptional(set to None for basic editing success rate evaluation only). The data format for both is adict, for each measurement dimension, you need to provide the corresponding prompt and its corresponding ground truth. Here is an example of the data: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']}}In the above example, we evaluate the performance of the editing methods about "neighborhood" and "distracting".Step6: Edit and EvaluationDone! We can conduct Edit and Evaluation for your model to be edited. Theeditfunction will return a series of metrics related to the editing process as well as the modified model weights.metrics,edited_model,_=editor.edit(prompts=prompts,ground_truth=ground_truth,target_new=target_new,locality_inputs=locality_inputs,keep_original_weight=True)## metrics: edit success, rephrase success, locality e.g.## edited_model: post-edit modelEvaluationWe specify the return metrics asdictformat, including model prediction evaluations before and after editing. For each edit, it will include the following metrics:rewrite_acc$\rightarrow$Reliabliltyrephrase_acc$\rightarrow$Generalizationlocality$\rightarrow$Localityportablility$\rightarrow$Portablility{"post":{"rewrite_acc":,"rephrase_acc":,"locality":{"YOUR_LOCALITY_KEY":,//...},"portablility":{"YOUR_PORTABILITY_KEY":,//...},},"pre":{"rewrite_acc":,"rephrase_acc":,"portablility":{"YOUR_PORTABILITY_KEY":,//...},}}For evaluation for Reliablilty, you only need to provide the corresponding editingpromptsand editingtarget_new.For evaluation for Generalization,rephrase_promptsare required.For evaluation for Locality and Portablility, you need to define the name of the corresponding metric, as well aspromptsandground_truth.Note: the length needs to be equal to the edit promptsTrainermeta-learning based:MENDmemory-based routing:SERACFor above editing methods, pre-training of corresponding meta-networks or classifiers is required. Therefore, in EasyEdit, we provide a unified framework for pretraining the relevant network structures. Take the training MEND for example:Step 1andStep 2are the same as the example above, which involves selecting the appropriate editing model and editing method.Step3: Provide the edit training setThe currently supported and available datasets are:zsreandcounterfact(Google Drive). Please place them in the "data" directory and initialize the dataset_class (ZsreDatasetfor zsre andCounterFactDatasetfor counterfact) to load the corresponding training set.train_ds=ZsreDataset('./data/zsre_mend_train.json',config=training_hparams)eval_ds=ZsreDataset('./data/zsre_mend_eval.json',config=training_hparams)Step4: Combine them into aTrainertrainer=EditTrainer(config=training_hparams,train_set=train_ds,val_set=eval_ds)Step5: Run and EditDone! We can conduct Run and Evaluation.trainer.run()Run: TheCHECKPOINTwill be saved to the pathresults_dir.Edit: Set thearchivefield in thehparams filetoCHECKPOINT. EasyEdit will automatically load the corresponding pre-trained weights during the editing process(Go to edit).Training ExamplefromeasyeditorimportEditTrainer,MENDTrainingHparams,ZsreDatasettraining_hparams=MENDTrainingHparams.from_hparams('hparams/TRAINING/MEND/llama-7b.yaml')train_ds=ZsreDataset('./data/zsre/zsre_mend_train.json',config=training_hparams)eval_ds=ZsreDataset('./data/zsre/zsre_mend_eval.json',config=training_hparams)trainer=EditTrainer(config=training_hparams,train_set=train_ds,val_set=eval_ds)trainer.run()MultimodalEditorMultimodalEditoris the class for Multi-Modality Editing. You can choose the appropriate editing method based on your specific needs.Due to different transformer versions and different GPU models, the editing results may fluctuateslightly.Introduction by a Simple ExampleWith the modularity and flexibility ofEasyEdit, you can easily use it to edit model.Step1: Define a MLLM as the object to be edited.Choose the MLLM to be edited.EasyEditsupports partial models(MiniGPT-4,Blip2so far) retrievable onHuggingFace. The corresponding configuration file directory ishparams/YUOR_METHOD/YOUR_MODEL.YAML, such ashparams/MEND/minigpt4.yaml, set the correspondingmodel_nameto select the object for editing.model_name:minigpt4model_class:Blip2OPTtokenizer_class:LlamaTokenizertokenizer_name:llama-7bStep2: Choose the appropriate Editing MethodThe selection of editing methods is acrucialstep, as different methods have their own strengths and weaknesses. Users need to consider the trade-off between editing success rate, generalization, and maintaining unrelated performance.## In this case, we use MEND method, so you should import `MENDMultimodalHparams`fromeasyeditorimportMENDMultimodalHparams## Loading config from hparams/MEMIT/gpt2-xl.yamlhparams=MENDMultimodalHparams.from_hparams('./hparams/MEND/minigpt4')Step3: Provide the edit descriptor and edit target## edit descriptor: prompt that you want to editprompts=["How many tennis balls are in the picture?","What is the red food?"]## edit target: expected outputtargets=["2","tomatoes",]## edit image: image for editingimage=["val2014/COCO_val2014_000000451435.jpg","val2014/COCO_val2014_000000189446.jpg"]Step4: Combine them into aMultimodalEditorEasyEditprovides a simple and unified way to init Editor, like huggingface:from_hparams.## Construct MLLM Editoreditor=MultimodalEditor.from_hparams(hparams)Step5: Provide the data for evaluationNote that the data for locality and multimodal locality are bothoptional(set to None for basic editing success rate evaluation only). The data format for both is adict, for each measurement dimension, you need to provide the corresponding prompt and its corresponding ground truth. Here is an example of the data:locality_inputs={'text':{'prompt':["nq question: what purpose did seasonal monsoon winds have on trade"],'ground_truth':["enabled European empire expansion into the Americas and trade\routes to become established across the Atlantic and Pacific oceans"]},'vision':{'prompt':["What sport can you use this for?"],'ground_truth':["riding"],'image':["val2014/COCO_val2014_000000297147.jpg"],}}In the above example, we evaluate the performance of the editing methods about "neighborhood" and "distracting".Step6: Edit and EvaluationDone! We can conduct Edit and Evaluation for your model to be edited. Theeditfunction will return a series of metrics related to the editing process as well as the modified model weights.metrics,edited_model,_=editor.edit(prompts=prompts,target_new=target_new,image=image,locality_inputs=locality_inputs,keep_original_weight=True)## metrics: edit success, rephrase success, locality e.g.## edited_model: post-edit modelEvaluationWe specify the return metrics asdictformat, including model prediction evaluations before and after editing. For each edit, it will include the following metrics:rewrite_acc$\rightarrow$Reliabliltyrephrase_acc$\rightarrow$Generalizationimage_rephrase_acc$\rightarrow$Generalization for Multimodallocality_acc$\rightarrow$Localitymultimodal_locality_acc$\rightarrow$Locality for Multimodal{"post":{"rewrite_acc":,"rephrase_acc":,"image_rephrase_acc":,"locality_acc":,"multimodal_locality_acc":,},"pre":{"rewrite_acc":,"rephrase_acc":,"image_rephrase_acc":,}}For evaluation for Reliablilty, you only need to provide the corresponding editingpromptsand editingtarget_new.For evaluation for Generalization,rephrase_promptsare required.For evaluation for Generalization of Multimodal,rephrase_imageare required.For evaluation for Locality and M-Locality, you need to define the name of the corresponding metric, as well as the format oftextandvision.Note: the length needs to be equal to the edit promptsTrainermeta-learning based:MENDmemory-based routing:SERACFor above editing methods, pre-training of corresponding meta-networks or classifiers is required. Therefore, in EasyEdit, we provide a unified framework for pretraining the relevant network structures. Take the training SERAC for example:Step 1andStep 2are the same as the example above, which involves selecting the appropriate editing model and editing method.Step3: Provide the edit training setThe currently supported and available datasets are:CaptionandVQA(Google Drive). Please place them in the "data" directory and initialize the dataset_class (CaptionDatasetfor Caption andVQADatasetfor VQA) to load the corresponding training set.train_ds=CaptionDataset('data/caption_train_edit.json',config=training_hparams)eval_ds=CaptionDataset('data/caption_eval_edit.json',config=training_hparams)Step4: Combine them into aTrainertrainer=MultimodalTrainer(config=hparams,train_set=train_ds,val_set=eval_ds)Step5: Run and EditDone! We can conduct Run and Evaluation.trainer.run()Run: TheCHECKPOINTwill be saved to the pathresults_dir.Edit: Set thearchivefield in thehparams filetoCHECKPOINT. EasyEdit will automatically load the corresponding pre-trained weights during the editing process(Go to edit).Training Examplehparams=SERACMultimodalTrainingHparams.from_hparams('hparams/TRAINING/SERAC/minigpt4.yaml')train_ds=CaptionDataset('data/caption_train_edit.json',config=training_hparams)eval_ds=CaptionDataset('data/caption_eval_edit.json',config=training_hparams)trainer=MultimodalTrainer(config=hparams,train_set=train_ds,val_set=eval_ds)trainer.run()TO DOIn next version, we plan to:release a multimodal Editor for LLMs.support more editing methods forBaiChuan,FALCON, etc.knowledge editing for other tasks(except factual editing), liketextual knowledge editing,personality editing, etc.Meanwhile, we will offer long-term maintenance to fix bugs, solve issues and meet new requests. So if you have any problems, please put issues to us.CitationPlease cite our paper if you use EasyEdit in your work.@article{wang2023easyedit,title={EasyEdit: An Easy-to-use Knowledge Editing Framework for Large Language Models},author={Wang, Peng and Zhang, Ningyu and Xie, Xin and Yao, Yunzhi and Tian, Bozhong and Wang, Mengru and Xi, Zekun and Cheng, Siyuan and Liu, Kangwei and Zheng, Guozhou and others},journal={arXiv preprint arXiv:2308.07269},year={2023}}@article{yao2023editing,title={Editing Large Language Models: Problems, Methods, and Opportunities},author={Yao, Yunzhi and Wang, Peng and Tian, Bozhong and Cheng, Siyuan and Li, Zhoubo and Deng, Shumin and Chen, Huajun and Zhang, Ningyu},journal={arXiv preprint arXiv:2305.13172},year={2023}}@article{cheng2023edit,title={Can We Edit Multimodal Large Language Models?},author={Cheng, Siyuan and Tian, Bozhong and Liu, Qingbin and Chen, Xi and Wang, Yongheng and Chen, Huajun and Zhang, Ningyu},journal={arXiv preprint arXiv:2310.08475},year={2023}}@misc{knowlm,author={Ningyu Zhang and Jintian Zhang and Xiaohan Wang and Honghao Gui and Kangwei Liu and Yinuo Jiang and Xiang Chen and Shengyu Mao and Shuofei Qiao and Yuqi Zhu and Zhen Bi and Jing Chen and Xiaozhuan Liang and Yixin Ou and Runnan Fang and Zekun Xi and Xin Xu and Lei Li and Peng Wang and Mengru Wang and Yunzhi Yao and Bozhong Tian and Yin Fang and Guozhou Zheng and Huajun Chen},title={KnowLM Technical Report},year={2023},url={http://knowlm.zjukg.cn/},}🎉ContributorsWe thank all the contributors to this project, more contributors are welcome!Other Related ProjectsROMEFastEdit🙌 We would like to express our heartfelt gratitude for the contribution ofROMEto our project, as we have utilized portions of their source code in our project.