-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrun_baselines.py
203 lines (177 loc) · 7.29 KB
/
run_baselines.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# This script is based on the implementation from mink-plus-plus
# https://github.com/zjysteven/mink-plus-plus
import os, argparse
from collections import defaultdict
import numpy as np
import pandas as pd
from sklearn.metrics import roc_curve, auc
from tqdm import tqdm
import zlib
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModelForCausalLM
from datasets import load_dataset
from peft import PeftModelForCausalLM
os.environ['HTTP_PROXY'] = 'http://fuwenjie:19990621f@localhost:7899'
os.environ['HTTPS_PROXY'] = 'http://fuwenjie:19990621f@localhost:7899'
# helper functions
def convert_huggingface_data_to_list_dic(dataset):
all_data = []
for i in range(len(dataset)):
ex = dataset[i]
all_data.append(ex)
return all_data
# arguments
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='meta-llama/Llama-2-7b-hf')
parser.add_argument(
'--dataset', type=str, default='wjfu99/WikiMIA-24',
choices=[
'wjfu99/WikiMIA-24', 'swj0419/WikiMIA'
]
)
parser.add_argument("--block_size", type=int, default=128)
parser.add_argument('--half', action='store_true')
parser.add_argument('--int8', action='store_true')
parser.add_argument('--prompt_model', action='store_true', default=True)
args = parser.parse_args()
# load model
def load_model(name, ref=False):
int8_kwargs = {}
half_kwargs = {}
# ref model is small and will be loaded in full precision
if args.int8 and not ref:
int8_kwargs = dict(load_in_8bit=True, torch_dtype=torch.bfloat16)
elif args.half and not ref:
half_kwargs = dict(torch_dtype=torch.bfloat16)
model = AutoModelForCausalLM.from_pretrained(
name, return_dict=True, device_map='auto', cache_dir="/home/fuwenjie/Extraction-LLMs/cache", **int8_kwargs, **half_kwargs
)
num_virtual_tokens = 0
if args.prompt_model:
model = PeftModelForCausalLM.from_pretrained(model=model, model_id="/home/fuwenjie/Extraction-LLMs/defend_llms/meta-llama/Llama-2-7b-hf/wjfu99/WikiMIA-24/128/checkpoint-250", cache_dir="cache")
num_virtual_tokens = model.peft_config['default'].num_virtual_tokens
model.eval()
tokenizer = AutoTokenizer.from_pretrained(name)
return model, tokenizer, num_virtual_tokens
# hard-coded ref model
if 'gemma' in args.model:
if 'it' in args.model:
args.ref_model = 'google/gemma-2b-it'
else:
args.ref_model = 'google/gemma-2b'
elif 'Llama-2' in args.model:
if 'chat' in args.model:
args.ref_model = 'meta-llama/Llama-2-7b-chat-hf'
else:
args.ref_model = 'meta-llama/Llama-2-7b-hf'
if '7b' in args.model:
args.ref_model = None
elif 'pythia' in args.model or 'Pythia' in args.model:
args.ref_model = 'EleutherAI/pythia-70m'
# elif 'llama' in args.model:
# args.ref_model = 'huggyllama/llama-7b'
elif 'gpt-neox-20b' in args.model:
args.ref_model = 'EleutherAI/gpt-neo-125m'
elif 'opt' in args.model:
args.ref_model = 'facebook/opt-350m'
else:
print('Unsupport model, no ref model will be used')
args.ref_model = None
model, tokenizer, num_virtual_tokens = load_model(args.model)
if args.ref_model:
ref_model, ref_tokenizer, _ = load_model(args.ref_model, ref=True)
# load dataset
dataset = load_dataset(
args.dataset,
split=f"WikiMIA_length{args.block_size}",
)
data = convert_huggingface_data_to_list_dic(dataset)
if args.dataset == 'wjfu99/WikiMIA-24':
perturbed_dataset = load_dataset(
'wjfu99/WikiMIA-24-perturbed',
split=f"WikiMIA_length{args.block_size}"
)
elif args.dataset == 'swj0419/WikiMIA':
perturbed_dataset = load_dataset(
'zjysteven/WikiMIA_paraphrased_perturbed',
split=f"WikiMIA_length{args.block_size}" + '_perturbed'
)
perturbed_data = convert_huggingface_data_to_list_dic(perturbed_dataset)
num_neighbors = len(perturbed_data) // len(data)
# inference - get scores for each input
def inference(text, model):
input_ids = torch.tensor(tokenizer.encode(text)).unsqueeze(0)
input_ids = input_ids.to(model.device)
with torch.no_grad():
outputs = model(input_ids, labels=input_ids)
loss, logits = outputs[:2]
ll = -loss.item() # log-likelihood
return ll, input_ids, logits
scores = defaultdict(list)
for i, d in enumerate(tqdm(data, total=len(data), desc='Samples')):
text = d['input']
ll, input_ids, logits = inference(text, model)
if args.ref_model:
ll_ref, _, _ = inference(text, ref_model)
ll_lowercase, _, _ = inference(text.lower(), model)
ll_neighbors = []
for j in range(num_neighbors):
neig_text = perturbed_data[i * num_neighbors + j]['input']
ll_neighbors.append(inference(neig_text, model)[0])
# assuming the score is larger for training data
# and smaller for non-training data
# this is why sometimes there is a negative sign in front of the score
if args.ref_model:
scores['ref'].append(ll - ll_ref)
scores['lowercase'].append(ll_lowercase / ll)
scores['neighbor'].append(ll - np.mean(ll_neighbors))
# loss and zlib
scores['loss'].append(ll)
scores['zlib'].append(ll / len(zlib.compress(bytes(text, 'utf-8'))))
# mink and mink++
input_ids = input_ids[0][1:].unsqueeze(-1)
probs = F.softmax(logits[0, num_virtual_tokens:-1], dim=-1)
log_probs = F.log_softmax(logits[0, num_virtual_tokens:-1], dim=-1)
token_log_probs = log_probs.gather(dim=-1, index=input_ids).squeeze(-1)
mu = (probs * log_probs).sum(-1)
sigma = (probs * torch.square(log_probs)).sum(-1) - torch.square(mu)
## mink
for ratio in [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
k_length = int(len(token_log_probs) * ratio)
topk = np.sort(token_log_probs.cpu())[:k_length]
scores[f'mink_{ratio}'].append(np.mean(topk).item())
## mink++
mink_plus = (token_log_probs - mu) / sigma.sqrt()
## replace -inf with 0
mink_plus[mink_plus == float('-inf')] = 0
for ratio in [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
k_length = int(len(mink_plus) * ratio)
topk = np.sort(mink_plus.cpu())[:k_length]
scores[f'mink++_{ratio}'].append(np.mean(topk).item())
# compute metrics
# tpr and fpr thresholds are hard-coded
def get_metrics(scores, labels):
fpr_list, tpr_list, thresholds = roc_curve(labels, scores)
auroc = auc(fpr_list, tpr_list)
fpr95 = fpr_list[np.where(tpr_list >= 0.95)[0][0]]
tpr05 = tpr_list[np.where(fpr_list <= 0.05)[0][-1]]
return auroc, fpr95, tpr05
labels = [d['label'] for d in data] # 1: training, 0: non-training
results = defaultdict(list)
for method, scores in scores.items():
auroc, fpr95, tpr05 = get_metrics(scores, labels)
results['method'].append(method)
results['auroc'].append(f"{auroc:.1%}")
results['fpr95'].append(f"{fpr95:.1%}")
results['tpr05'].append(f"{tpr05:.1%}")
df = pd.DataFrame(results)
print(df)
save_root = f"results_defender/{args.dataset}/{args.block_size}"
if not os.path.exists(save_root):
os.makedirs(save_root)
model_id = args.model.split('/')[-1]
if os.path.isfile(os.path.join(save_root, f"{model_id}.csv")):
df.to_csv(os.path.join(save_root, f"{model_id}.csv"), index=False, mode='a', header=False)
else:
df.to_csv(os.path.join(save_root, f"{model_id}.csv"), index=False)