E asy E dit: An Easy-to-use Knowledge Editing Framework for Large Language Models (2024)

Abstract: Large Language Models (LLMs) usually suffer from knowledge cutoff or fallacy issues, which means they are unaware of unseen events or generate text with incorrect facts owing to outdated/noisy data. To this end, many knowledge editing approaches for LLMs have emerged – aiming to subtly inject/edit updated knowledge or adjust undesired behavior while minimizing the impact on unrelated inputs. Nevertheless, due to significant differences among various knowledge editing methods and the variations in task setups, there is no standard implementation framework available for the community, which hinders practitioners from applying knowledge editing to applications. To address these issues, we propose EasyEdit, an easy-to-use knowledge editing framework for LLMs. It supports various cutting-edge knowledge editing approaches and can be readily applied to many well-known LLMs such as T5, GPT-J, LlaMA, etc. Empirically, we report the knowledge editing results on LlaMA-2 with EasyEdit, demonstrating that knowledge editing surpasses traditional fine-tuning in terms of reliability and generalization. We have released the source code on GitHub, along with Google Colab tutorials and comprehensive documentation for beginners to get started. Besides, we present an online system for real-time knowledge editing, and a demo video.

## introduction large language models (llms) have revolutionized modern natural language processing (nlp), significantly improving performance across various tasks @xcite @xcite @xcite @xcite . however, deployed llms usually suffer from knowledge cutoff or fallacy issues. for example, llms such as chatgpt and llama possess information only up to their last training point. they can sometimes produce inaccurate or misleading information due to potential discrepancies and biases in their pre-training data @xcite . hence, it's essential to efficiently update the parametric knowledge within the llms to modify specific behaviors while avoiding expensive retraining. indeed, finetuning or parameter-efficient finetuning @xcite offers methods for modifying llms, these approaches can be computationally expensive and may lead to overfitting, particularly when applied to a limited number of samples @xcite or streaming errors of llms. additionally, fine-tuned models might forfeit capabilities gained during pre-training, and their modifications do not always generalize to relevant inputs. an alternative methodology involves using manually written or retrieved prompts to influence the llms' output. these methods suffer from reliability issues, as llms do not consistently generate text aligned with the prefix prompt @xcite . additionally, due to the extensive amount of up-to-date knowledge required for complex reasoning tasks, the impracticality of context overload becomes inevitable whenever the context length is limited. a feasible solution, knowledge editing foot_0 , aims to efficiently modify the behavior of llms with minimal impact on unrelated inputs. research on knowledge editing for llms @xcite @xcite @xcite @xcite @xcite @xcite have displayed remarkable progress across various tasks and settings. however, these variations in both implementation and task settings have impeded the development of a unified and comprehensive framework for knowledge editing. note that the complexity obstructs the direct comparison of effectiveness and feasibility between different methods, and complicates the creation of novel knowledge editing approaches. to this end, we propose easyedit, an easy-to-use knowledge editing framework for llms. easyedit modularizes editing methods and effectiveness evaluation while considering their combination and interaction. it supports a variety of editing scenarios, including single-instance, batch-instance, sequential, and multi-modal editing. moreover, easyedit provides evaluation evaluations of key metrics such as reliability, generalization, locality, and portability @xcite , to quantify the robustness and side effects @xcite of editing methods. specifically, in easyedit, the editor class integrates various editing components. the method class offers a unified interface apply_to_model, which accepts editing descriptors and returns the edited model, thereby facilitating the integration of novel editing methodologies. dedicated to evaluating editing performance, the evaluate module leverages metrics such as reliability, robust generalization, and locality. the trainer module manages the training of additional neural network structures. each module in easyedit is meticulously defined, striking a balance between cohesion and coupling. furthermore, we furnish examples of editing across a spectrum of models, including t5 @xcite , gpt-j (wang and komatsuzaki, 2021), gpt-neo @xcite , gpt2 @xcite , llama @xcite , llama-2 @xcite , mistral @xcite , and qwen @xcite . we acknowledge all the support for easyedit, which is listed in appendix 6 due to space constraints. knowledge storage mechanism within the nlp literature, numerous studies have delved into understanding the location of different types of knowledge in language models @xcite @xcite . llms can be conceptualized as knowledge banks, and the transformer mlp layers function as key-value memories according to observations from @xcite . this configuration promotes efficient knowledge adjustments by precisely localizing knowledge within the mlp layers (denoted as knowledge editing). knowledge editing enables nimble alterations to the llms' behavior through one data point. another promising attribute of knowledge editing is its ability to ensure the locality of editing, meaning that modifications are contained within specific contexts. additionally, the knowledge editing technique can mitigate harmful language generation @xcite . in this paper, we present easyedit, an easy-to-use knowledge editing framework for llms. it seamlessly integrates diverse editing technologies and supports the free combination of modules for various llms. through its unified framework and interface, easyedit enables users to swiftly comprehend and apply the prevalent knowledge editing methods included in the package. ## design and implementation easyedit provides a complete editing and evaluation process built on pytorch @xcite and huggingface @xcite . this section commences with an exploration of the assemblability aspect of easyedit, followed by a detailed explanation of the design and implementation of each component within the easyedit framework (as shown in figure 1 ). additionally, we demonstrate a straightforward example of applying mend to llama, altering the output of the u.s. president to joe biden. ## evaluation knowledge editing, as defined by @xcite , involves supplying a specific editing descriptor x e (input instance) and an editing target y e (desired output). from these, an editing instance z e is generated in the form: z e ∼ [x e , y e ]. the goal is to adjust the behavior of the initial base model f θ (where θ represents the model's parameters) to produce an edited model f θe . ideally, for the editing instance, the edited model would behave such that f θe (x e ) = y e . additionally, the editing scope s(z e ) refers to a set of input examples whose true labels have been influenced by the editing instance. in most cases, a successful edit should affect the model's predictions for numerous in-scope we employ six dimensions of metrics to assess the performance of editing methods, including reliability, generalization, locality, portability, fluency @xcite and efficiency (as shown in figure 3 ). ## experiments in this section, we will outline the experiment setting and report the empirical results of multiple editing methods supported in easyedit ( ## conclusion and future work we propose easyedit, an easy-to-use knowledge editing framework for llms, which supports many cutting-edge approaches and various llms. the ability to edit and manipulate llms in a controlled and targeted manner may open up new possibilities for knowledge augmentation @xcite @xcite and adaptation across various natural language processing tasks @xcite . in the future, we will continue to integrate advanced editing technologies into easyedit, aiming at facilitating further research and inspiring new ideas for the nlp community. when applying knowledge editing techniques or utilizing this system, careful consideration must be given to potential risks and ethical concerns. all our data undergoes meticulous manual inspection, and any malicious edits or offensive content have been removed. easyedit provides optimal hyperparameters for various editing methods. in addition to common parameters such as learning rate, steps, and regularization coefficients, the location of layers for editing can also be considered as hyperparameters, significantly influencing the robustness of the editing process. the following tables demonstrate layer with value loss model.layers.31 target layer for updating weights model.layers.5.mlp.down_proj rome we follow @xcite in utilizing causal mediation analysis to identify an intermediate layer in the model responsible for recalling facts. the causal traces reveal an early site (5th layer) with causal states concentrated at the last token of the subject, indicating a significant role for mlp states at that specific layer (