Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Eval scripts, harmfulness results, fix to eval_harmfulness, readme #109

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
62 changes: 61 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
# Data-Efficient Task Unlearning in Language Models
> Szymon Duchniewicz, Andrzej Szablewski, Zhe Yu, Yadong Liu, Carmen Meinson

🚧 Work in Progress 🚧
WIP!!!!

## Environment Setup
``NOTE`` I think here I will combine everything into one setup script, but doing manual instruction for each component for now

``NOTE`` Use ``python3.10``, newer version will have issue when seting up the env (if using current requirements.txt).

Install dependencies for ``llm_unlearn``
```
cd llm_unlearn_ucl
pip install -r requirements.txt
```
Install dependencies for ``lm-evaluation-harness``
```
cd lm-evaluation-harness
pip install -e .
```

``NOTE`` for eval harmfulness
requires ``sentencepiece``


## Machine Unlearning

### Continuous Unlearning
```
python unlearn_harm.py \
--wandb_project_name "$wandb_project_name" \
--cache_dir "$base_cache_dir" \
--wandb_run_name "$wandb_run_name" \
--samples_save_dir "$samples_dir" \
--model_save_dir "$model_save_dir" \
--save_every "$save_every" \
--seed "$seed" \
--log_file "$log_file" \
--wandb_log
```

### Batch Unlearning
```

```

### Evaluation


## Note on configuration used for experiments
- By default, the code uses lr_scheduler, add ``--no_scheduler`` to disable it.
-

## Arguments
```
loss = (
args.bad_weight * bad_loss
+ args.random_weight * random_loss
+ args.normal_weight * normal_loss
)
```



## Data
Unlearned models available on HuggingFace: https://huggingface.co/ucl-snlp-nyt/snlp-unlearned-models

Data analysis and visualisation of results available at: https://github.com/Willmish/SNLP_GCW_data_analysis/
12 changes: 8 additions & 4 deletions eval_harmfulness/beavertails_get_model_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@

from generation_scripts.parse_args import parse_args
from generation_scripts.generation import generate_answers
from utils import reproducibility


def main(args) -> None:
# seeding
print(f"seeding - {args.seed}")
seed = reproducibility(args.seed)

# load dataset
dataset = load_dataset(args.dataset_path)["test"]

Expand All @@ -28,12 +33,11 @@ def main(args) -> None:
else:
device = torch.device(args.device)
tokenizer = AutoTokenizer.from_pretrained(args.model_path, padding_side="left")
# Ensure the tokenizer has a pad token. Use EOS token as padding token.
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(args.model_path, device_map=device)

# Fix for ValueError: Pipeline(and causalLM?) with tokenizer without pad_token cannot do batching. You can try to set it with `pipe.tokenizer.pad_token_id = model.config.eos_token_id`.
tokenizer.pad_token_id = model.config.eos_token_id
model.generation_config.pad_token_id = model.config.eos_token_id

model_name = os.path.basename(args.model_path)

evaluations = generate_answers(
Expand Down
1 change: 1 addition & 0 deletions eval_harmfulness/eval_multi_seed_cluter_submission.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
qsub -t 1-3 run_experiment_multiple_seed_cluster.sh
6,302 changes: 6,302 additions & 0 deletions eval_harmfulness/eval_results/gemma-2b_eval/seed_123/evaluation.json

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model_name,flagged/all
gemma-2b,0.29428571428571426
14,002 changes: 14,002 additions & 0 deletions eval_harmfulness/eval_results/gemma-2b_eval/seed_123/predictions.json

Large diffs are not rendered by default.

6,302 changes: 6,302 additions & 0 deletions eval_harmfulness/eval_results/gemma-2b_eval/seed_42/evaluation.json

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model_name,flagged/all
gemma-2b,0.29428571428571426
14,002 changes: 14,002 additions & 0 deletions eval_harmfulness/eval_results/gemma-2b_eval/seed_42/predictions.json

Large diffs are not rendered by default.

6,302 changes: 6,302 additions & 0 deletions eval_harmfulness/eval_results/gemma-2b_eval/seed_456/evaluation.json

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model_name,flagged/all
gemma-2b,0.29428571428571426
14,002 changes: 14,002 additions & 0 deletions eval_harmfulness/eval_results/gemma-2b_eval/seed_456/predictions.json

Large diffs are not rendered by default.

701 changes: 701 additions & 0 deletions eval_harmfulness/eval_results/llama3_eval/detailed_flagged_prompts.csv

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model_name,flagged/all
Meta-Llama-3-8B,0.31
6,302 changes: 6,302 additions & 0 deletions eval_harmfulness/eval_results/llama3_eval/seed_123/evaluation.json

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model_name,flagged/all
Meta-Llama-3-8B,0.32285714285714284
14,002 changes: 14,002 additions & 0 deletions eval_harmfulness/eval_results/llama3_eval/seed_123/predictions.json

Large diffs are not rendered by default.

6,302 changes: 6,302 additions & 0 deletions eval_harmfulness/eval_results/llama3_eval/seed_42/evaluation.json

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model_name,flagged/all
Meta-Llama-3-8B,0.32857142857142857
14,002 changes: 14,002 additions & 0 deletions eval_harmfulness/eval_results/llama3_eval/seed_42/predictions.json

Large diffs are not rendered by default.

6,302 changes: 6,302 additions & 0 deletions eval_harmfulness/eval_results/llama3_eval/seed_456/evaluation.json

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model_name,flagged/all
Meta-Llama-3-8B,0.31857142857142856
14,002 changes: 14,002 additions & 0 deletions eval_harmfulness/eval_results/llama3_eval/seed_456/predictions.json

Large diffs are not rendered by default.

6,302 changes: 6,302 additions & 0 deletions eval_harmfulness/eval_results/test_llama2/evaluation.json

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions eval_harmfulness/eval_results/test_llama2/flagged_ratio.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model_name,flagged/all
Llama-2-7b-hf,0.31142857142857144
14,002 changes: 14,002 additions & 0 deletions eval_harmfulness/eval_results/test_llama2/predictions.json

Large diffs are not rendered by default.

6,302 changes: 6,302 additions & 0 deletions eval_harmfulness/eval_results/test_olmo_7b/evaluation.json

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions eval_harmfulness/eval_results/test_olmo_7b/flagged_ratio.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
model_name,flagged/all
OLMo-7B-hf,0.28714285714285714
14,002 changes: 14,002 additions & 0 deletions eval_harmfulness/eval_results/test_olmo_7b/predictions.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions eval_harmfulness/evaluate_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import pandas as pd
from evaluation_scripts.moderation import QAModeration
from evaluation_scripts.parse_args import parse_arguments
from utils import reproducibility
import torch


def calculate_flagged_proportion_and_agreement(data: dict) -> dict:
Expand Down Expand Up @@ -77,6 +79,9 @@ def plot_metrics(metrics: list[dict], output_dir: str) -> None:
def main() -> None:
args = parse_arguments()

# seeding
seed = reproducibility(args.seed)

if not args.use_existing_evaluation:
assert (
args.eval_dataset is not None
Expand Down Expand Up @@ -106,6 +111,7 @@ def main() -> None:
model_max_length=args.max_length,
device_map=args.device,
)

predictions = model.predict(
question=prompts,
answer=responses,
Expand Down
2 changes: 2 additions & 0 deletions eval_harmfulness/evaluation_scripts/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def from_pretrained(
model_kwargs["problem_type"] = problem_type
if device_map is not None:
model_kwargs["device_map"] = device_map
# temp modification on running at half precision
model_kwargs["torch_dtype"] = torch.bfloat16

model = AutoModelForSequenceClassification.from_pretrained(
model_name_or_path,
Expand Down
8 changes: 8 additions & 0 deletions eval_harmfulness/evaluation_scripts/parse_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ def parse_arguments() -> argparse.Namespace:
action="store_true",
help="Uses already existing evaluation file at output_dir to just print and plot its contents!",
)

parser.add_argument(
"--seed",
type=int,
default=42,
help="Seed for random number generators",
)

return parser.parse_args()
7 changes: 7 additions & 0 deletions eval_harmfulness/generation_scripts/parse_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ def parse_args() -> argparse.Namespace:
help="Directory to which the output JSON is saved.",
)

parser.add_argument(
"--seed",
type=int,
default=42,
help="Seed for random number generators",
)

args = parser.parse_args()

return args
Loading
Loading