-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_neighbor.py
137 lines (117 loc) · 4.37 KB
/
run_neighbor.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
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
# 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='EleutherAI/pythia-2.8b')
parser.add_argument(
'--dataset', type=str, default='WikiMIA_length32',
choices=[
'WikiMIA_length32', 'WikiMIA_length64', 'WikiMIA_length128',
'WikiMIA_length32_paraphrased',
'WikiMIA_length64_paraphrased',
'WikiMIA_length128_paraphrased',
]
)
parser.add_argument('--half', action='store_true')
parser.add_argument('--int8', action='store_true')
args = parser.parse_args()
# load model
def load_model(name):
int8_kwargs = {}
half_kwargs = {}
# ref model is small and will be loaded in full precision
if args.int8:
int8_kwargs = dict(load_in_8bit=True, torch_dtype=torch.bfloat16)
elif args.half:
half_kwargs = dict(torch_dtype=torch.bfloat16)
if 'mamba' in name:
try:
from transformers import MambaForCausalLM
except ImportError:
raise ImportError
model = MambaForCausalLM.from_pretrained(
name, return_dict=True, device_map='auto', **int8_kwargs, **half_kwargs
)
else:
model = AutoModelForCausalLM.from_pretrained(
name, return_dict=True, device_map='auto', **int8_kwargs, **half_kwargs
)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(name)
return model, tokenizer
model, tokenizer = load_model(args.model)
# load dataset
if not 'paraphrased' in args.dataset:
dataset = load_dataset('swj0419/WikiMIA', split=args.dataset)
else:
dataset = load_dataset('zjysteven/WikiMIA_paraphrased_perturbed', split=args.dataset)
data = convert_huggingface_data_to_list_dic(dataset)
perturbed_dataset = load_dataset(
'zjysteven/WikiMIA_paraphrased_perturbed',
split=args.dataset + '_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
scores = defaultdict(list)
for i, d in enumerate(tqdm(data, total=len(data), desc='Samples')):
text = d['input']
ll = inference(text, model)
ll_neighbors = []
for j in range(num_neighbors):
text = perturbed_data[i * num_neighbors + j]['input']
ll_neighbors.append(inference(text, model))
# 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
scores['neighbor'].append(ll - np.mean(ll_neighbors))
# 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/{args.dataset}"
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)