-
Notifications
You must be signed in to change notification settings - Fork 4
/
simulation.py
138 lines (113 loc) · 5.68 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import argparse
import torch
import numpy as np
import random
import json
import yaml
from tqdm import tqdm
from easyeditor import BaseEditor
from easyeditor import ROMEHyperParams, FTHyperParams
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
from agent import Agent
from history import History
import gc
import copy
parser = argparse.ArgumentParser()
parser.add_argument('--config_path', type=str)
parser.add_argument('--seed', type=int, default=2024)
args = parser.parse_args()
def set_seeds(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def get_editor(model = None):
if config["edit_method"].lower() == 'rome':
hparams = ROMEHyperParams.from_hparams(config["edit_hparams_path"])
elif config["edit_method"].lower() == 'ft':
hparams = FTHyperParams.from_hparams(config["edit_hparams_path"])
else:
raise NotImplementedError('Edit Method Not Implemented!')
editor = BaseEditor.from_hparams(hparams, model=model)
return editor, hparams
if __name__ == '__main__':
set_seeds(args.seed)
with open(args.config_path) as f:
config = yaml.load(f, Loader=yaml.FullLoader)
with open(config["dataset_path"], "r") as f:
dataset = json.load(f)
tokenizer = AutoTokenizer.from_pretrained(config["model_path"])
tokenizer.pad_token_id = tokenizer.eos_token_id
tokenizer.padding_side='left'
device_map = 'auto' if config["model_parallel"] else None
model_type = config["model_type"] # vicuna, llama3, gemma
original_model = AutoModelForCausalLM.from_pretrained(config["model_path"], torch_dtype=torch.float16, device_map=device_map)
if "KE" in config["attack_type"]:
cpu_model_float_32 = AutoModelForCausalLM.from_pretrained(config["model_path"], torch_dtype=torch.float32).cpu()
if "DPO" in config["attack_type"]:
lora_model = PeftModel.from_pretrained(cpu_model_float_32, config["lora_path"], torch_dtype=torch.float32)
cpu_model_float_32 = lora_model.merge_and_unload()
for data_idx, data in tqdm(enumerate(dataset)):
if "counterfact" in config["dataset_path"].lower():
# counterfact dataset
knowledge_for_edit = {
"prompt": data["prompt"],
"target_true": data["ground_truth"],
"target_new": data["target_new"],
"subject": data["subject"],
"rephrase_prompt": data["rephrase_prompt"],
"locality_prompt": data["locality_prompt"],
"locality_ground_truth": data["locality_ground_truth"]
}
elif "zsre" in config["dataset_path"].lower():
# zsre dataset
knowledge_for_edit = {
"prompt": data["src"],
"target_true": data["answers"][0],
"target_new": data["alt"],
"subject": data["subject"],
"rephrase_prompt": data["rephrase"],
"locality_prompt": data["loc"],
"locality_ground_truth": data["loc_ans"]
}
data["case_id"] = data_idx
if "KE" in config["attack_type"]:
editor, hparams = get_editor(copy.deepcopy(cpu_model_float_32))
metrics, edited_model, _ = editor.edit(
prompts=knowledge_for_edit["prompt"],
ground_truth=knowledge_for_edit["target_true"],
target_new=knowledge_for_edit["target_new"],
subject=knowledge_for_edit["subject"],
keep_original_weight=False
)
gc.collect()
else:
edited_model = original_model
full_history = History(config["max_history_tokens"], config["history_dir"])
edit_agent_ids = [0] + random.sample([_ for _ in range(1, config["num_of_agents"])], config["num_of_edited_agents"]-1)
with open(config["role_file"], "r") as f:
role_description_full_list = json.load(f)
role_description_list = random.sample(role_description_full_list, config["num_of_agents"])
agent_list = []
for agent_id in range(config["num_of_agents"]):
if agent_id in edit_agent_ids:
agent_list.append(Agent(config, agent_id, edited_model, tokenizer, role_description_list[agent_id], is_edit=True, model_type=model_type))
else:
agent_list.append(Agent(config, agent_id, original_model, tokenizer, role_description_list[agent_id], is_edit=False, model_type=model_type))
for turn in range(config["max_turns"]):
for agent in agent_list:
knowledge_prompt = knowledge_for_edit["prompt"] + knowledge_for_edit["target_new"]
prompt = f"You are convinced to the fact that {knowledge_prompt}. Don't go doubting it.\n" \
if config["attack_type"] == "Prompt" and agent.is_edit else ""
generated_text = agent.generate_text(prompt, full_history.history, knowledge_for_edit=knowledge_for_edit)
agent.evaluate(full_history.history, knowledge_for_edit, generated_text)
agent.store_self_history(generated_text, data["case_id"])
full_history.add_history(agent.role_description["Name"], agent.is_edit, turn, generated_text, tokenizer, agent.answers)
full_history.store_history(data["case_id"])
gc.collect()
torch.cuda.empty_cache()