This repository aims to provide the necessary tools to reproduce the results of our study (paper). A quick overview is presented in what follows.
Our study introduces a new evaluation protocol for factual knowledge updating in language models, addressing key limitations in the widely used CounterFact protocol for the evaluation of model editing. Current evaluation methods rely heavily on synthetic updates, have restricted coverage of update scenarios, and often overestimate editing success. To overcome these issues, this project relies on WikiFactDiff, a dataset of real-world factual changes automatically extracted by comparing Wikidata snapshots across time, and integrates it with a distractor-based assessment for precise evaluation.
Using this protocol, this repository enables the benchmarking of state-of-the-art model editing algorithms across diverse update scenarios, including non-functional relations. The results reveal that existing model editing methods are not yet suitable for real-world knowledge maintenance: they either fail to apply updates reliably or induce substantial collateral damage to unrelated facts. They also lack temporal awareness and exhibit worsening performance as the number of edits increases.
Overall, our work provides a more realistic and accurate evaluation framework for factual knowledge updating and shows that current model editing techniques fall short of the requirements for maintaining up-to-date, consistent knowledge in large language models.
It is necessary to install:
- MongoDB (or have access to a MongoDB instance)
uvpackage manager (see here)
Set the environment variables:
STORAGE_FOLDER: The folder where to store intermediate files (800GB disk space within this folder is largely enough). It needs to be physically accessible and not through network (NAS). Otherwise, you will have sync problems using the cache librarydiskcachewhich could corrupt your cache and crash your experiments.MONGO_URL: If MongoDB does not run locally with no authentification, specify its URL in this variable. Else, do nothing.
Important: For all following commands in this README file, it is expected from you to be located exactly at the root of this project.
Simply execute the folowing command:
uv syncExecute all the commands preferrably. The commands in Step 1 and 2 can all be run in parallel (depending on your machine capabilities):
- Download Wikidata:
from wikidata_tools.wikidata import TempWikidata, WikidataPopularity, WikidataPrepStage
wd = TempWikidata("20210104", WikidataPrepStage.PREPROCESSED)
wd.build(confirm=False)from wikidata_tools.wikidata import TempWikidata, WikidataPopularity, WikidataPrepStage
wd = TempWikidata("20230227", WikidataPrepStage.PREPROCESSED)
wd.build(confirm=False)- Wikipedia Popularity Statistics:
from wikidata_tools.wikidata import WikidataPrepStage
wikipop = WikidataPopularity("20210104")
wikipop.build(confirm=False)from wikidata_tools.wikidata import WikidataPrepStage
wikipop = WikidataPopularity("20230227")
wikipop.build(confirm=False)- Collect Random Popular Facts: They are useful to assess whether factual knowledge globally was altered by the model editing algorithm. They will be stored in STORAGE_FOLDER.
python experiments/nonfunct-edit-eval/collect_popular_facts.py- Build InMemoryWikidata: Build a small version of Wikidata that can be loaded in memory.
python experiments/nonfunct-edit-eval/build_inmemory_wikidata.py- Cache GPT-4o Factual Questions: Questions generated by GPT-4o are cached once to avoid calling it again for each experiment.
export OPENAI_API_KEY="your-key"
python experiments/nonfunct-edit-eval/cache_wikifactdiff_verbs.pyEdit the GPT-J (6B) model and measure its knowledge on various facts pre- and post-updating.
In this case, only one fact is edited at a time. The model is reseted to its original state after each step. To collect results for this experiment, run all the commands in experiments/nonfunct-edit-eval/single_update_commands.sh. They can be run in parallel.
In this case, many facts are edited and then the model is evaluated. To collect results for this experiment, run all the commands in experiments/nonfunct-edit-eval/multi_update_commands.sh. They can be run in parallel.
The results are stored in the root of this project in know-edit-measures. The folder know-edit-models can be deleted at this point.
Execute the notebook experiments/nonfunct-edit-eval/analyze_results.ipynb to generate most of the plots in our paper.
Coming soon
These experiments are based on several packages. They are accompanied by comprehensive documentation to learn how to use them.
wikidata-tools: A package to collect and preprocess Wikidata dumps automatically. Look atpackages/wikidata-tools/README.mdfor more information.verb-tools: A package to automatically verbalize facts in natural language through LLMs. Look atpackages/verb-tools/README.mdfor more information.lm-tools: A wrapper over the HuggingFace LLM API. It provides useful functions to evaluate language models. Look atpackages/lm-tools/README.mdfor more information.know-measure: A package containing the factual knowledge assessment method used to evaluate the success of a factual update on a language model. Look atpackages/know-measure/README.mdfor more information.know-edit: A package that provides easy-to-use functions and classes to apply model editing to LLMs. It is mainly a wrapper over the EasyEdit package. Look atpackages/know-edit/README.mdfor more information.ke-utils: Utility functions for the whole project. Look atpackages/ke-utils/README.mdfor more information.easyedit: A fork of the EasyEdit package to make it easier to use and installable throughpip installanduv add.divergent-beamsearch: A modified beam-search algorithm which is a requirement for the factual knowledge assessment method used to evaluate model editing methods. Look atpackages/divergent-beamsearch/README.mdfor more information.
Look for the LICENSE.txt file at the root of this project.