EasyEdit: 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 GitHub1, along with Google Colab tutorials and comprehensive documentation2 for beginners to get started. Besides, we present an online system3 for real-time knowledge editing, and a demo video4.

heading: 1 Introduction
text: Large Language Models (LLMs) have revolutionized modern Natural Language Processing (NLP), significantly improving performance across various tasks (Brown et al., 2020; OpenAI, 2023; Anil et al., 2023; Zhao et al., 2023; Touvron et al., 2023b;
∗Corresponding author. 1This is a subproject of KnowLM (https://github.
com/zjunlp/KnowLM), which facilitates knowledgeable LLM Framework with EasyInstruct, EasyEdit, EasyDetect etc.
2https://zjunlp.gitbook.io/easyedit 3https://huggingface.co/spaces/zjunlp/EasyEdit 4https://youtu.be/Gm6T0QaaskU
Qiao et al., 2023; Zheng et al., 2023b; Pan et al., 2023). 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 (Ji et al., 2023; Hartvigsen et al., 2022). 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 (Ding et al., 2022, 2023) 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 (Cao et al., 2021) 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 (Hernandez et al., 2023; Lewis et al., 2021). 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 editing5, aims to efficiently modify the behavior of LLMs with minimal impact on unrelated inputs. Research on knowledge editing for LLMs (Meng et al., 2023, 2022; Zheng et al., 2023a; Gupta et al., 2023; Mitchell et al., 2022a; Geva et al., 2023; Hase et al., 2023; Cohen et al., 2023a; Hartvigsen et al., 2023; Tan et al., 2024; Yu et al., 2023) have displayed remarkable progress across various tasks and settings.
5Knowledge editing can also be termed as model editing.
82
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 (Yao et al., 2023), to quantify the robustness and side effects (Cohen et al., 2023b) 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 (Raffel et al., 2019), GPT-J (Wang and Komatsuzaki, 2021), GPTNEO (Black et al., 2021), GPT2 (Radford et al., 2019), LLaMA (Touvron et al., 2023a), LLaMA-2 (Touvron et al., 2023b), Mistral (Jiang et al., 2023), and Qwen (Bai et al., 2023). We acknowledge all the support for EASYEDIT, which is listed in Appendix 6 due to space constraints.

heading: 2 Background
text: Previous Solutions Despite the tremendous success of LLMs in almost all NLP tasks, persistent challenges such as knowledge cutoff and biased/toxic outputs remain. To counter these challenges, two approaches are generally employed:
1) FINE-TUNING: Traditional fine-tuning techniques, along with delta tuning (Ding et al., 2022) and LoRA tuning (Hu et al., 2021) utilize domainspecific datasets to update the model’s internal parametric knowledge. However, these methods face two notable challenges: First, they consume considerable resources. Second, they risk the potential of catastrophic forgetting (Ramasesh et al., 2022).
2) PROMPT-AUGMENTATION: Given a sufficient number of demonstrations or retrieved contexts, LLMs can learn to enhance reasoning (Yu et al., 2022) and generation through external knowledge (Borgeaud et al., 2022; Guu et al., 2020; Lewis et al., 2020). However, the performance may be sensitive to factors such as the prompting template, the selection of in-context examples (Zhao et al., 2021), or retrieved contexts (Ren et al.,
2023). These approaches also encounter the issue of context length limitation (Liu et al., 2023a).
Knowledge Storage Mechanism Within the NLP literature, numerous studies have delved into understanding the location of different types of knowledge in language models (Petroni et al., 2019; Roberts et al., 2020; Jiang et al., 2020). LLMs can be conceptualized as knowledge banks, and the transformer MLP layers function as key-value memories according to observations from Geva et al. (2021). 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 (Geva et al., 2022). 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.

heading: 3 Design and Implementation
text: EASYEDIT provides a complete editing and evaluation process built on Pytorch (Paszke et al., 2019) and Huggingface (Wolf et al., 2020). 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.

heading: 3.1 Assemblability
text: In the realm of knowledge editing, various distinct scenarios6 exist. To cater to this diversity, EASYEDIT offers flexible combinations of modules that different editing Editor (such as singleinstance, batch-instance (details in Appendix A)),
6Denoted as (Editor, METHOD, TARGET)
METHOD (such as ROME, GRACE (§3.3)). About editing TARGET, EASYEDIT can accommodate any parameterized white-box existing model. Additionally, recent research (Dong et al., 2022) indicates that LLMs exhibit robust in-context learning capabilities. By providing edited facts to LLMs, one can alter the behavior of black-box models such as GPT4 (OpenAI, 2023). All those combinations are easily implementable and verifiable within the EASYEDIT framework.

heading: 3.2 Editor
text: The Editor serves a pivotal role in knowledge editing as it directly establishes the editing tasks and corresponding editing scenarios. Users supply the editor descriptor (xe) and the edit target (ye), but the input format varies according to the different editing objects. For instance, in Seq2Seq models, the edit target typically serves as the decoder’s input, while in autoregressive models, xe and ye need to be concatenated to maximize the conditional probability. To facilitate unified editing across diverse architecture models, we meticulously develop a component prepare_requests to transform editing inputs.
In EASYEDIT, we provide an “edit” interface, incorporating components such as Hparams, Method, and Evaluate. During the editing phase, various knowledge editing strategies can be executed by invoking the apply_to_model function available in all different methods, it also performs evaluations
of the model before and after the editing to gauge the editing’s multifaceted impact on the model behavior, including generalization and side effects. An example to edit through EASYEDIT is depicted in Figure 2.
Note that the ability to execute batch editing (multiple edits in a single instance) and sequential editing (implementing new edits while preserving previous editing) is a crucial feature of knowledge editing (Huang et al., 2023). For methods that support batch editing, editing instances are inputted in chunk form. In addition, EASYEDIT provides a boolean switch, enabling users to either retain the pre-edit weights for single-instance editing or discard them for sequential editing.

heading: 3.3 Method
text: As the core component of knowledge editing, editing methods alter the model’s behavior by modifying its internal parameters (e.g. MLP, Attention Mechanisms) or explicitly utilizing preceding editing facts, among other strategies. Impressive related works (Table 1) abound in this field, and they can be generally grouped into three categories as proposed by Yao et al. (2023).
Memory-based This category, encompassing methods such as SERAC (Mitchell et al., 2022b), IKE (Zheng et al., 2023a), and GRACE (Hartvigsen et al., 2023), emphasizes the use of memory elements to store and manipulate information during editing. SERAC applies retrieval and classification routing, GRACE replaces hidden states with pa-
rameters searched from a codebook for edit memorization, while IKE uses context-edit facts to guide the model in generating edited facts.
Meta-learning These methods learn the weight updates (denoted as ∆), which are then added to the original weights for editing. Examples include KE (Cao et al., 2021), which uses a bidirectionalLSTM to predict weight updates, and MEND (Mitchell et al., 2022a), which adjusts model parameters through low-rank decomposition of gradients.
Locate-Then-Edit This paradigm focuses on knowledge localization to modify the parameters of specific neurons responsible for storing the editing facts. EASYEDIT integrates methods like KN (Dai et al., 2021), which employs gradient-based methods to update specific neurons. Moreover, EASYEDIT supports ROME (Meng et al., 2023), PMET (Li et al., 2024) and MEMIT (Meng et al., 2022), leveraging causal intervention to pinpoint knowledge within a specific MLP layer and enabling the modification of the entire matrix.
However, it is not practical to expose the editing methods directly to users due to the complexity of the underlying concepts and the time investment required to understand them. Additionally, differences in input-output formats across methods could further complicate the learning process. To circumvent these hurdles, we implement a unified interface, apply_to_model, in EASYEDIT. Aligning with the Strategy design pattern, this interface is designed to be overridden by different types of editing methods, ensuring consistent input and out-
put types. Specifically, it accepts a ‘request’ that includes the editing descriptor, the target of the edit, and any input data necessary to evaluate the editing performance. After processing the request(s), the interface returns the edited model weights. This design ensures both flexibility and easy-to-use, enabling users to handle knowledge editing instances effortlessly and utilize the customized models in other downstream tasks.

heading: 3.4 Hparams
text: When initializing an editing method, it is crucial to specify the related hyperparameters. These include the model to be edited, the layers targeted for modification, and, optionally, the type of external model, among other parameters. For methods that alter the LLMs’ internal parameters, the adjustable parameter names should be indicated using the MODULE_NAME format, such as transformer.h.5.mlp.fc_out. In this case, the parameters of the fc_out linear layer in the fifth layer MLP of GPT-J would be modified, while all other parameters remain frozen. Layer selection adheres to the locality of knowledge (Meng et al., 2023) or retains layers with higher success rates in pilot experiments (Mitchell et al., 2022a), as elaborated in Appendix B.
All hyperparameter classes derive from a common base class, Hyperparams, which includes necessary attributes and abstract methods. This base class supports loading hyperparameters in both yaml and json formats. Moreover, the Hyperparams base class can be used to initialize the Trainer module, streamlining the workflow.

heading: 3.5 Trainer
text: Certain editing methods, which employ metalearning or utilize classifiers (as shown in Table 1), necessitate the training of additional parameters or the implementation of extra network structures. Similar to Hyperparameters (Hparams), all Trainer classes inherit from a common base class, BaseTrainer. It includes essential attributes and abstract methods such as run and validate steps. Subclasses of the BaseTrainer define specific training steps for editing, such as calculating editing loss and locality loss, as well as the strategies for combining these losses. Once additional network structures are obtained, the subsequent editing process follows the same path as the TrainingFree method. In EASYEDIT, various Trainers can be easily called with one click.

heading: 4 Evaluation
text: Knowledge editing, as defined by Mitchell et al. (2022b), involves supplying a specific editing descriptor xe (input instance) and an editing target ye (desired output). From these, an editing instance ze is generated in the form: ze ∼ [xe, ye]. 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(xe) = ye. Additionally, the editing scope S(ze) 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 (I(xe) ∼ {x′e|x′e ∈ S(ze)}) inputs, while leaving Out-of-Scope (O(xe) ∼ {x′e|x′e /∈ S(ze)}) inputs unchanged.
We employ six dimensions of metrics to assess the performance of editing methods, including Reliability, Generalization, Locality, Portability, Fluency (Zhang et al., 2018) and Efficiency (as shown in Figure 3).
Reliability This metric measures the average accuracy on the given editing instance ze.
Generalization The edit should appropriately influence in-scope inputs, this metric gauges the average accuracy on in-scope inputs I(xe).
Locality Editing should adhere to the principle of locality, it evaluates whether out-of-scope inputs O(xe) can remain unchanged as the base model.
Portability The robust generalization of the edit, assessing whether the edited knowledge can be effectively applied to related content.
Fluency It measures the weighted average of bigram and tri-gram entropies to assess the diversity of text generations.
Efficiency Editing should be time and resourceefficient. This metric quantifies efficiency by measuring editing time and VRAM consumption.

heading: 5 Experiments
text: In this section, we will outline the experiment setting and report the empirical results of multiple editing methods supported in EASYEDIT (Table 2).

heading: 5.1 Experiment Setting
text: To validate the potential application of knowledge editing on LLMs, we utilize LlaMA 2 (7B) (Touvron et al., 2023b), a model with a large parameter size, representing the decoder-only structure.
We employ the ZsRE dataset to test the capability of knowledge editing in incorporating substantial and general fact associations into the model. ZsRE (Levy et al., 2017) is a question-answering (QA) dataset that generates an equivalence neighbor through back-translation. Later, it is further expanded by Yao et al. (2023) to provide a more comprehensive evaluation of knowledge editing, including an assessment of the LLMs’ ability to integrate the edited fact with other facts related to the target object o* (an aspect of Portability). For baselines, we compare various editing methods and additionally employ FT-L from ROME (Meng et al., 2023). FT-L updates parameters for a single MLP layer and applies an L∞ norm constraint to limit the weight changes.

heading: 5.2 Experiment Results
text: Table 2 reveals SERAC and IKE’s superior performance on the ZsRE datasets, exceeding 99% on several metrics. While ROME and MEMIT perform sub-optimally in generalization, they exhibit relatively high performance in terms of reliability and locality. IKE exhibits the potential of gradientfree updates through in-context learning, leading to near-perfect scores in both reliability and generalization. However, it shows some deficiency in locality, as preceding prompts may influence out-of-scope inputs. GRACE exhibits poor generalization, possibly attributed to the lack of explicit semantic representation in its activations within
the decoder-only model (Liu et al., 2023b). FT-L’s performance on ZsRE falls significantly short compared to ROME, even though both methods modify the same layer parameters. This suggests that under the norm constraint, fine-tuning is not an effective strategy for knowledge editing. MEND performs well overall, achieving over 90% accuracy on multiple metrics and even surpassing ROME in terms of reliability and generalization. KN performs poorly, indicating that it may be better suited for editing tasks in smaller models or tasks involving knowledge attribution.
For the Portability evaluation, where the inference depends on a single connection or ‘hop’ between facts, most editing methods struggle to effectively combine the edited fact with other facts relevant to the target object o*. While SERAC obtains good performance on previous metrics, it completely fails to propagate the edited knowledge. This is because SERAC utilizes an external model with a smaller parameter size for counterfactual routing whereas the smaller model struggles to recall a rich set of relevant facts. IKE still maintains a relatively high capability for ripple editing (exceeding 67%), demonstrating that in-context learning is a promising approach to propagate edited knowledge to other related facts.

heading: 6 Conclusion and Future work
text: 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 (Wu et al., 2023, 2020; Zhang et al., 2022; Chen et al., 2022) and adaptation across various natural language processing tasks (Kaddour et al., 2023). 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.

heading: Acknowledgments
text: We thank the developers of the ROME7 library for their significant contributions to the NLP community. We are grateful to Ting Lu and Yu Zhang who participated in the development of this project during the Zhejiang University Summer Camp. We also extend our gratitude to the NLP team at East China Normal University, particularly Lang Yu, for their support of the Melo module. Special thanks to Tom Hartvigsen for his contributions to the implementation of GRACE. We are grateful to the TMG-NUDT team for their valuable suggestions and technical support for the PMET method. We are grateful to Jia-Chen Gu from the University of California, Los Angeles, and Haiyang Yu from the Department of Cyberspace Security, University of Science for their constructive suggestions on development of EASYEDIT. We thank Yiquan Wu and Zeqing Yuan for helping the AAAI 2024 tutorial (canceled since part of speakers cannot present in person) of EasyEdit. Appreciation is also extended to all PR contributors, and issue feedback providers during the EasyEdit version iterations, especially Damien de Mijolla for proposing different optimization goals for FT, which complemented the fine-tuning baseline, and to Yuxuan Zhai for pointing out the portability metric evaluation issue of LlaMA-2-7B.
We would like to express gratitude to the anonymous reviewers for their kind comments. This work was supported by the National Natural Science Foundation of China (No. 62206246, No. NSFCU23B2055, No. NSFCU19B2027), the Fundamental Research Funds for the Central Universities (226-2023-00138), Zhejiang Provincial Natural Science Foundation of China (No. LGG22F030011), Yongjiang Talent Introduction Programme (2021A-156-G), CCF-Tencent RhinoBird Open Research Fund, Tencent AI Lab RhinoBird Focused Research Program (RBFR2024003), Information Technology Center and State Key Lab of CAD&CG, Zhejiang University.
Ethics Statement
The significance of knowledge editing lies in its direct impact on the behavior and output results of LMs. Malicious edits may lead to the generation of responses with toxicity or bias in LMs, posing potential harm to users and society. Therefore,
7https://github.com/kmeng01/rome
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.

heading: A Preliminaries of Model Editing
text: The task of knowledge editing is to effectively modify the initial base model fθ to the edited model fθ′ , with corresponding parameter adjustments for a specific input-output pair (xe, ye), where xe ∈ Xe and fθ(xe) ̸= ye. Here, Xe represents the entire set to be edited. Therefore, the current problem formulation for knowledge editing can be broadly categorized into three types:
1. Single Instance Editing: Evaluating the performance of the model after a single edit. The model reloads the original weights after a single edit:
θ′ ← arg θ min(∥fθ(xe)− ye∥) (1)
2. Batch Instance Editing: Simultaneously modifying N knowledge instances (where N ≪ |Xe|) and evaluating the performance of the edited model after processing a batch. The model reloads the original weights after processing a batch of edits:
θ′ ← arg θ min
N∑
e=1
(∥fθ(xe)− ye∥) (2)
3. Sequential Editing: This approach requires sequentially editing each knowledge instance, and evaluation must be performed after all knowledge updates have been applied:
θ′ ← arg θ min
|Xe|∑
e=1
(∥fθ(xe)− ye∥) (3)

heading: B Default Hparams Settings
text: 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
the default location settings in EASYEDIT (using Llama-2-7B as an example).
ROME We follow Meng et al. (2023) 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 (Table 3).
MEMIT Following Meng et al. (2022), we quantify the average indirect causal effect of MLP modules. The results demonstrate a concentration of intermediate states in LLaMA. The disparity in the effects between MLP severed and hidden states severed becomes significantly reduced after the 8th layer. We choose the entire critical range of MLP layers, denoted asR = {4, 5, 6, 7, 8} (Table 4).
PMET PMET (Li et al., 2024) adopts the localization strategy from MEMIT, designating the corresponding layer as the modification target. Building upon the update of MLP weights, PMET focuses on multi-head self-attention (MHSA), further substantiating the discovery that MHSA encodes specific patterns for general knowledge extraction. (Table 4).
MEND In the context of meta-learning for editing, it is commonly observed that editing MLP layers yields better performance than editing attention
layers. Typically, MLP weights of the last 3 transformer blocks (totaling 6 weight matrices) are chosen for editing (Mitchell et al., 2022a). EASYEDIT adheres to this default configuration (Table 6).
GRACE Recent studies have revealed the impact of selecting the right layers for fine-tuning (Lee et al., 2023). Similarly, in GRACE (Hartvigsen et al., 2023), we conduct pilot experiments, retaining layers with consistently high edit success rates (Table 5).

heading: Future Work
text: 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