{"id":13458978,"url":"https://github.com/yaojin17/Unlearning_LLM","last_synced_at":"2025-03-24T16:31:15.152Z","repository":{"id":224459968,"uuid":"763297529","full_name":"yaojin17/Unlearning_LLM","owner":"yaojin17","description":"[ACL 2024] Code and data for \"Machine Unlearning of Pre-trained Large Language Models\"","archived":false,"fork":false,"pushed_at":"2024-09-30T01:22:07.000Z","size":51,"stargazers_count":45,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-29T04:34:30.672Z","etag":null,"topics":["copyright-protection","data-privacy","llm","llm-unlearning","machine-unlearning","unlearning"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yaojin17.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-26T02:46:44.000Z","updated_at":"2024-10-27T17:48:38.000Z","dependencies_parsed_at":"2024-03-16T23:59:55.778Z","dependency_job_id":"91a21036-8bca-4362-a703-d3a554a61558","html_url":"https://github.com/yaojin17/Unlearning_LLM","commit_stats":null,"previous_names":["yaojin17/unlearning_llm"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaojin17%2FUnlearning_LLM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaojin17%2FUnlearning_LLM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaojin17%2FUnlearning_LLM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaojin17%2FUnlearning_LLM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yaojin17","download_url":"https://codeload.github.com/yaojin17/Unlearning_LLM/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245308537,"owners_count":20594263,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["copyright-protection","data-privacy","llm","llm-unlearning","machine-unlearning","unlearning"],"created_at":"2024-07-31T09:01:00.482Z","updated_at":"2025-03-24T16:31:14.868Z","avatar_url":"https://github.com/yaojin17.png","language":"Python","funding_links":[],"categories":["Papers"],"sub_categories":["2024"],"readme":"# 🤖 Unlearning_LLM\nThis repo contains code and data for the ACL 2024 paper \"[Machine Unlearning of Pre-trained Large Language Models](https://arxiv.org/abs/2402.15159)\"\n\n[Paper](https://arxiv.org/pdf/2402.15159.pdf) | [Dataset](https://huggingface.co/datasets/llmunlearn/unlearn_dataset)\n\n\n## 🌟 Abstract\n\n\u003cdetails\u003e\u003csummary\u003eAbstract\u003c/summary\u003e\n\nThis study investigates the concept of the `right to be forgotten' within the context of large language models (LLMs). We explore machine unlearning as a pivotal solution, with a focus on pre-trained models--a notably under-researched area. Our research delineates a comprehensive framework for machine unlearning in pre-trained LLMs, encompassing a critical analysis of seven diverse unlearning methods. Through rigorous evaluation using curated datasets from arXiv, books, and GitHub, we establish a robust benchmark for unlearning performance, demonstrating that these methods are over $10^5$ times more computationally efficient than retraining. Our results show that integrating gradient ascent with gradient descent on in-distribution data improves hyperparameter robustness. We also provide detailed guidelines for efficient hyperparameter tuning in the unlearning process. Our findings advance the discourse on ethical AI practices, offering substantive insights into the mechanics of machine unlearning for pre-trained LLMs and underscoring the potential for responsible AI development.\n\n\u003c/details\u003e\n\n## 📊 Dataset\nWe collect and provide the **unlearn_dataset**, which serves as a benchmark for evaluating unlearning methodologies in pre-trained large language models across diverse domains, including arXiv, GitHub. Access our **unlearn_dataset** directly on [Hugging Face](https://huggingface.co/datasets/llmunlearn/unlearn_dataset).\n\n### 🔍 Loading the datasets\n\nTo load the dataset:\n\n```python\nfrom datasets import load_dataset\ndataset = load_dataset(\"llmunlearn/unlearn_dataset\", name=\"arxiv\", split=\"forget\")\n```\n* Available configuration names and corresponding splits:\n  - `arxiv`: `forget, approximate, retain`\n  - `github`: `forget, approximate, retain`\n  - `general`: `evaluation, retain`\n\n## ✈️ How to run\n### Environment Setup\n```\ngit clone https://github.com/yaojin17/Unlearning_LLM.git\ncd Unlearning_LLM\nconda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch\npip install -e .\npip install -r requirements.txt\n```\n### Download Yi-6B model\n```\nmkdir models\ncd models\ngit lfs install\ngit clone https://huggingface.co/01-ai/Yi-6B\n```\n### Prepare tokenized datasets\n```\ncd utils\npython save_tokenized_dataset.py --tokenizer_name_or_path ../../models/Yi-6B\npython ascent_plus_descent_tokenizer.py --tokenizer_name_or_path ../../models/Yi-6B\n```\n### Unlearning experiments\nRemember to replace `\u003cyour-wandb-key\u003e` in the [run_unlearn.py](llm_unlearn/run_unlearn.py#L90), [run_eval.py](llm_unlearn/run_eval.py#L84), and [run_mia.py](llm_unlearn/run_mia.py#L85) files to your own key. \n```\n# Make sure you are under the llm_unlearn dir\ntorchrun --nproc_per_node=8 --master_port=20001  run_unlearn.py   \\\n    --target_model_name_or_path ../../models/Yi-6B  \\\n    --per_device_train_batch_size 1     \\\n    --do_unlearn  \\\n    --output_dir ./output \\\n    --overwrite_output_dir     \\\n    --num_train_epochs 1    \\\n    --logging_steps 1     \\\n    --learning_rate 2e-5     \\\n    --warmup_ratio 0.03 \\\n    --overwrite_cache \\\n    --save_total_limit 1 \\\n    --fsdp \"full_shard auto_wrap\" \\\n    --fsdp_transformer_layer_cls_to_wrap 'LlamaDecoderLayer' \\\n    --bf16 True \\\n    --tf32 True \\\n    --weight_decay 0. \\\n    --lr_scheduler_type \"cosine\" \\\n    --domain github \\\n    --gradient_accumulation_steps 85 \\\n    --unlearn_method gradient_ascent \n  ```\n- Available domains with corresponding arguments: : \n  - `--domain arxiv  --gradient_accumulation_steps 60 `\n  - `--domain github --gradient_accumulation_steps 85 `\n- Available methods with corresponding arguments: \n  - `--unlearn_method gradient_ascent `\n  - `--unlearn_method random_label --completely_random True` (named Fine-tuning with Random Labels in the paper)\n  - `--unlearn_method random_label  --top_k 1  --rm_groundtruth True ` (named Unlearning with Adversarial Samples in the paper)\n  - `--unlearn_method ascent_plus_descent`\n  - `--unlearn_method ascent_plus_kl_divergence`\n  - `--unlearn_method ascent_plus_descent --general True`\n  - `--unlearn_method ascent_plus_kl_divergence --general True`\n\n### Eval unlearned model\n```\ntorchrun --nproc_per_node=8 --master_port=20001 run_eval.py \\\n    --model_name_or_path ./output/github/Yi-6B/8_gpu_bs_1_gas_85_lr_2.0e_5_epoch1/unlearn/gradient_ascent \\\n    --per_device_eval_batch_size 1 \\\n    --do_eval \\\n    --output_dir ./output/github/Yi-6B-eval \\\n    --overwrite_output_dir \\\n    --overwrite_cache \\\n    --tf32 True \\\n    --domain github\n```\n### Membership inference attack\n```\ntorchrun --nproc_per_node=8 --master_port=20001 run_mia.py \\\n        --model_name_or_path ./output/github/Yi-6B/8_gpu_bs_1_gas_85_lr_2.0e_5_epoch1general/unlearn/ascent_plus_kl_divergence \\\n        --per_device_eval_batch_size 1 \\\n        --do_eval \\\n        --output_dir ./output/arxiv/Yi-6B-mia \\\n        --overwrite_output_dir \\\n        --overwrite_cache \\\n        --tf32 True \\\n        --domain github\n```\n\n\n## ⭐ Citation Information\n\nIf you find this code or dataset useful, please consider citing our paper:\n\n```bib\n@article{yao2024machine,\n  title={Machine Unlearning of Pre-trained Large Language Models},\n  author={Yao, Jin and Chien, Eli and Du, Minxin and Niu, Xinyao and Wang, Tianhao and Cheng, Zezhou and Yue, Xiang},\n  journal={arXiv preprint arXiv:2402.15159},\n  year={2024}\n}\n```\n\n### Contact\nFeel free to reach out if you have any questions. [Jin Yao](mailto:rry4fg@virginia.edu), [Eli Chien](mailto:ichien6@gatech.edu), [Xiang Yue](mailto:yue.149@osu.edu)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaojin17%2FUnlearning_LLM","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyaojin17%2FUnlearning_LLM","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaojin17%2FUnlearning_LLM/lists"}