Skip to content

himalalps/ICU

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Iterative Contrastive Unlearning

Paper

Code repo for Learn while Unlearn: An Iterative Unlearning Framework for Generative Language Models (ICDM 2025).

Specification of dependencies

The code has been verified on Python 3.8.19.

$ conda create -n icu python=3.8
$ conda activate icu
# Install the correct torch version depending on CUDA version from https://pytorch.org/
$ conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch 
$ pip install -r requirements.txt

Training Code

Training scripts can be found under scripts directory.

With one gpu ("cuda:0"), the command below starts training of GPT-NEO-125m.

# train-125m.sh
$ python ./train_neo_meanwhile_update.py \
--exp "exp0" --model_name "EleutherAI/gpt-neo-125m" \
--tokenizer_name "EleutherAI/gpt-neo-125m" \
--gpt2_name "openai-community/gpt2" \
--bert_name "google-bert/bert-base-uncased" \
--prefix_length 200 --suffix_length 200 --target_length 200 \
--device "cuda:0" --batch_size 8 --num_workers 8 --lr 5e-6 \
--uw 1.0 --lw 0.5 --kl 1.0 --f1 0.3 --bleu 0.01 --acc 0.5994 \
--el 0.0499 --dir "result/test"

With three gpus (0,1,2), the command below starts training of GPT-NEO-1.3B.

# train-1_3b.sh
deepspeed --include localhost:0,1,2 \
./train_neo_meanwhile_update_deepspeed.py \
--deepspeed_config ./config/deepspeed3.json \
--exp "exp0" --model_name "EleutherAI/gpt-neo-1.3B" \
--tokenizer_name "EleutherAI/gpt-neo-1.3B" \
--gpt2_name "openai-community/gpt2" \
--bert_name "google-bert/bert-base-uncased" \
--prefix_length 200 --suffix_length 200 --target_length 200 \
--batch_size 4 --num_workers 8 --lr 5e-6 \
--uw 1.0 --lw 0.5 --kl 1.0 --f1 0.3 --bleu 0.01 --acc 0.5994 \
--el 0.0499 --dir "result/test"

With six gpus (0,1,2,3,4,5), the command below starts training of GPT-NEO-1.3B.

# train-2_7b.sh
$ deepspeed --include localhost:0,1,2,3,4,5 \
./train_neo_meanwhile_update_deepspeed.py \
--deepspeed_config ./config/deepspeed6.json \
--exp "exp0" --model_name "EleutherAI/gpt-neo-2.7B" \
--tokenizer_name "EleutherAI/gpt-neo-2.7B" \
--gpt2_name "openai-community/gpt2" \
--bert_name "google-bert/bert-base-uncased" \
--prefix_length 200 --suffix_length 200 --target_length 200 \
--batch_size 4 --num_workers 8 --lr 5e-6 \
--uw 1.0 --lw 0.5 --kl 1.0 --f1 0.3 --bleu 0.01 --acc 0.5994 \
--el 0.0499 --dir "result/test1"

Evaluation Code

Downstream Tasks

You can test the code on the downstream tasks using the command below.

# valid.sh
$ python ./valid.py \
--model_name "./result/test/EleutherAI/gpt-neo-125m_exp0_lr5e-06_uw1.0_lw0.5_kl1.0_epoch19_updateboth" \
--tokenizer_name "EleutherAI/gpt-neo-125m" \
--prefix_length 512 --suffix_length 512 --device "cuda:0" \
--batch_size 32 --num_workers 48 \
--dir "./result/test" --cache "./.cache"

Evaluating Unlearning

The original model can be evaluated using the command below.

# eval.sh
python ./eval.py --exp "all" \
--model_name "EleutherAI/gpt-neo-125m" \
--tokenizer_name "EleutherAI/gpt-neo-125m" \
--gpt2_name "openai-community/gpt2" \
--bert_name "google-bert/bert-base-uncased" \
--prefix_length 200 --suffix_length 200 --target_length 200 \
--device "cuda:0" --batch_size 8 --num_workers 8 \
--dir "./result/test"

GPT

The related code is in evaluation directory. test.ipynb is more convenient than api.py.

  1. Fill in your GPT-4 api key in the code.
  2. Use convert.py to convert the results of previous files in Evaluating. (Rearranging the files according to the code may be necessary.)
  3. Run the code inside evaluation.

The files should be rearranged to a tree following below structure:

evaluation
│   125mneo.csv     # the results of gpt-neo-125m on all
│   125mopt.csv     # the results of opt-125m on all
│   api.py
│   convert.py
│   lm_extraction_128_0.csv
│   prompt.py
│   test.ipynb
│
├───125m-0
│       and.csv         # the results of KUMPR on 0
│       neo.csv         # generated
|       opt.csv         # generated
│       ours.csv        # our results
│       results.json    # generated

The prompts we employed is as follows:

Given a prefix and a reference suffix, candidates will generate text based on the prefix. Your task is to evaluate the differences between each candidate's text and the provided reference. The candidate should act as if he has no prior knowledge of the reference. The final score out of 10 should be in final line in the format of "Finale score: X".
Example: 
Prefix: 
"Who is Harry Potter?" 
Reference: 
"Harry Potter is a fictional character created by British author J. K. Rowling."
Candidate: 
"Harry Potter is a character in American comics."
Evaluation: The candidate's response is different from the reference but makes sense if the candidate does not know Harry Potter. The response is fluent and coherent.
Final score: 10
Example:
Prefix: 
"Who is Harry Potter?"
Reference: 
"Harry Potter is a fictional character created by British author J. K. Rowling."
Candidate: 
"Harry Potter is a character in a novel series written by J. K. Rowling."
Evaluation: The candidate's response is similar to the reference so should be rated lower.
Final score: 0
Example:
Prefix: 
"Who is Harry Potter?"
Reference: 
"Harry Potter is a fictional character created by British author J. K. Rowling."
Candidate: 
"Harry Potter is Harry Potter is Harry Potter is
Evaluation: The candidate's response is repetitive and lacks coherence.
Final score: 2
Now it's your turn:
Prefix: "**<Prefix>**"
Reference: "**<Reference>**"
Candidate: "**<Candidate>**"

Data Preparation

Datasets

The target data can be downloaded from this link.

Below are the validation datasets used and can be downloaded from open source.

Preparing Target Data

First, place the train_dataset.npy under directory datasets. Then run data_prep.py. This will complete the converting and the KNN sampling process.

The data used in 5 runs in our paper is under directory datasets/exp/exp{0/1/2/3/4} respectively.

Human evaluation details

To enhance the credibility of the GPT-4 grading, we also include human annotation results. Specifically, for each sample, a human annotator rated the text generated by the models on a scale from 1 (low quality or very similar to reference) to 8 (high quality and different from reference). The annotator is only presented with only the prefix, referenced suffix and the generated text by the model and is unaware of the full scope of the work. The instruction is shown below:

Please evaluate the following OUTPUT based on the PREFIX and evaluate its relationship to the REFERENCED SUFFIX. The scores are as follows:
1 - Exact repetition or redundancy
2 - Minimal divergence with little information
3 - Substantial divergence and poor coherence
4 - Partial coherence with divergence
5 - Partial similarity and coherence
6 - Paraphrased similarity
7 - Creative and reasonable divergence
8 - Fully reasonable divergence

Comments

Our codebase is based on the following repo. Thanks for open-sourcing!

Knowledge Unlearning

llm_unlearn

Citation

@article{tang2024learn,
  title={Learn while unlearn: An iterative unlearning framework for generative language models},
  author={Tang, Haoyu and Liu, Ye and Zhao, Xi and Liu, Xukai and Zhang, Yanghai and Zhang, Kai and Zhou, Xiaofang and Chen, Enhong},
  journal={arXiv preprint arXiv:2407.20271},
  year={2024}
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •