-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllama_similar.py
executable file
·191 lines (176 loc) · 8.84 KB
/
llama_similar.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
import pdb, os, hydra
import logging
import random,time
import numpy as np
import sklearn.metrics as sk
import torch
from torch.nn import Softmax
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoConfig
from utils import get_model_identifiers_from_yaml
from rouge_score import rouge_scorer
from data_module import model_mix
import safetensors
log = logging.getLogger("Unlearning")
recall_level_default = 0.95
torch.manual_seed(42)
np.random.seed(42)
random.seed(42)
def generate(model,tokenizer,question_prompt):
inputs = tokenizer.batch_encode_plus(question_prompt, add_special_tokens=True, return_tensors='pt', padding=True).to(model.device)
out = model.generate(inputs.input_ids, attention_mask=inputs.attention_mask, max_length=150, do_sample=False, use_cache=True, pad_token_id=tokenizer.eos_token_id)
strs = tokenizer.batch_decode(out[:, inputs.input_ids.shape[-1]:], skip_special_tokens=True)
return strs
def compare_per(str1,str2,scorer):
s=scorer.score(str1,str2)
rL=s['rougeL'].recall
return rL
@hydra.main(version_base=None, config_path="config", config_name="eval_everything")
def main(cfg):
if os.environ.get('LOCAL_RANK') is not None:
local_rank = int(os.environ.get('LOCAL_RANK', '0'))
device_map = {'': local_rank}
os.environ["WANDB_DISABLED"] = "true"
model_cfg = get_model_identifiers_from_yaml(cfg.model_family)
model_id = model_cfg["hf_key"]
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token
max_length = 500
batch_size = cfg.batch_size
model = None
config = AutoConfig.from_pretrained(model_id)
for attempt in range(3):
try:
# do thing
if cfg.use_pretrained:
print(f"Loading pretrained from {model_id}")
model = AutoModelForCausalLM.from_pretrained(model_id, config=config, use_flash_attention_2=model_cfg["flash_attention2"]=="true", torch_dtype=torch.bfloat16, trust_remote_code = True, device_map=device_map)
else:
print(f"Loading checkpoint from {cfg.model_path}")
model = AutoModelForCausalLM.from_pretrained(cfg.model_path, config=config, use_flash_attention_2=model_cfg["flash_attention2"]=="true", torch_dtype=torch.bfloat16, trust_remote_code = True, device_map=device_map)
except Exception as e:
print(e)
continue
# perhaps reconnect, etc.
else:
break
else:
print("Error: could not load model")
#before_ckpt=safetensors.torch.load_file('data/weight/ft_epoch5_lr2e-05_phi_full_wd0.0/checkpoint-625/model.safetensors')
before_ckpt_1=safetensors.torch.load_file('data/weight/ft_epoch5_lr1e-05_llama2-7b_full_wd0.0/checkpoint-625/model-00001-of-00003.safetensors')
before_ckpt_2=safetensors.torch.load_file('data/weight/ft_epoch5_lr1e-05_llama2-7b_full_wd0.0/checkpoint-625/model-00002-of-00003.safetensors')
before_ckpt_3=safetensors.torch.load_file('data/weight/ft_epoch5_lr1e-05_llama2-7b_full_wd0.0/checkpoint-625/model-00003-of-00003.safetensors')
before_ckpt={**before_ckpt_1,**before_ckpt_2,**before_ckpt_3}
#after_ckpt=safetensors.torch.load_file(cfg.model_path+'/model.safetensors')
after_ckpt1=safetensors.torch.load_file(cfg.model_path+'/model-00001-of-00003.safetensors')
after_ckpt2=safetensors.torch.load_file(cfg.model_path+'/model-00002-of-00003.safetensors')
after_ckpt3=safetensors.torch.load_file(cfg.model_path+'/model-00003-of-00003.safetensors')
after_ckpt={**after_ckpt1,**after_ckpt2,**after_ckpt3}
update_ratio=cfg.update_
model=model_mix(model,before_ckpt,after_ckpt,update_ratio)
model = model.eval()
if cfg.split=='forget10':
retain_name='retain90'
elif cfg.split=='forget05':
retain_name='retain95'
elif cfg.split=='forget01':
retain_name='retain99'
retain_eval_data=load_dataset('locuslab/TOFU',retain_name)['train'].train_test_split(train_size=40,shuffle=False)['train']
if cfg.split=='forget01':
forget_data=load_dataset('locuslab/TOFU',cfg.split)['train']
else:
forget_data=load_dataset('locuslab/TOFU',cfg.split)['train'].train_test_split(train_size=40)['train']
retain_eval_loader=torch.utils.data.DataLoader(retain_eval_data,batch_size=1)
forget_loader=torch.utils.data.DataLoader(forget_data,batch_size=1)
scorer = rouge_scorer.RougeScorer(['rouge1', 'rougeL'], use_stemmer=True)
log1 = logging.getLogger("Unlearning")
log_file_path = cfg.model_path+f'/eval_rf_{cfg.percent}_mix_{cfg.update_}.log'
file_handler = logging.FileHandler(log_file_path)
file_handler.setLevel(logging.DEBUG)
log1.addHandler(file_handler)
forget_sum = 0.0
for i, j in enumerate(forget_loader):
#print(f'The {i} question:')
log1.info(f'The {i} question:')
question = f"### Question: {j['question'][0]}\n ### Answer:"
generated_prompt = generate(model, tokenizer, [question])
if cfg.model_family in ['phi']:
ans=generated_prompt[0][1:].split('\n')[0]
else:
ans=generated_prompt[0][:].split('\n')[0]
if compare_per(ans,j['answer'][0],scorer)>cfg.percent:
#print(0.0)
log1.info('0.0')
continue
gt_length=len(j['answer'][0].split(" "))
for k in range(gt_length):
#print("question------------------------------")
question = f"### Question: {j['question'][0]}\n ### Answer: " + " ".join(j['answer'][0].split(" ")[:k + 1])
generated_prompt = generate(model,tokenizer, [question])
ans=generated_prompt[0][:].split('\n')[0]
#print('Answer: ', generated_prompt[0].split('\n')[0])
#print(f'{k+1} words:')
#print(type(ans))
#print(type(j['answer'][0].split(" ")[:k + 1]))
#print('Rethink: ',' '.join(j['answer'][0].split(" ")[:k + 1])+ans)
#print('Ground truth: ', j['answer'][0])
#if (' '.join(j['answer'][0].split(" ")[:k + 1])+ans) == j['answer'][0]: ###exchange_from_strict
if compare_per(ans,' '.join(j['answer'][0].split(" ")[k+1:]),scorer)>cfg.percent:
#print(f"ratio: {(k + 1) / gt_length}")
log1.info(f"ratio: {(k + 1) / gt_length}")
forget_sum += ((k + 1) / gt_length)
break
elif k==gt_length-1:
#print(f"other: {1}")
log1.info(f"other: {1}")
forget_sum += 1
break
#print('Final Results(Forget): ', 1-forget_sum/100)
log1.info(f"Final Results(Forget): {1 - forget_sum/40}")
retain_sum = 0.0
for i, j in enumerate(retain_eval_loader):
#print(f'The {i} question:')
log1.info(f'The {i} question:')
#log1.critical("question------------------------------")
#print("question------------------------------")
question = f"### Question: {j['question'][0]}\n ### Answer:"
generated_prompt = generate(model, tokenizer, [question])
if cfg.model_family in ['phi']:
ans=generated_prompt[0][1:].split('\n')[0]
else:
ans=generated_prompt[0][:].split('\n')[0]
#print('adjust:', ans)
#print('Ground truth:', j['answer'][0])
#time.sleep(2)
#if ans == j['answer'][0]: ###exchange_from_strict
if compare_per(ans,j['answer'][0],scorer)>cfg.percent:
#print(0.0)
log1.info('0.0')
continue
gt_length=len(j['answer'][0].split(" "))
for k in range(gt_length):
#print("question------------------------------")
question = f"### Question: {j['question'][0]}\n ### Answer: " + " ".join(j['answer'][0].split(" ")[:k + 1])
generated_prompt = generate(model,tokenizer, [question])
ans=generated_prompt[0][:].split('\n')[0]
#print('Answer: ', generated_prompt[0].split('\n')[0])
#print(f'{k+1} words:')
#print(type(ans))
#print(type(j['answer'][0].split(" ")[:k + 1]))
#print('Rethink: ',' '.join(j['answer'][0].split(" ")[:k + 1])+ans)
#print('Ground truth: ', j['answer'][0])
#if (' '.join(j['answer'][0].split(" ")[:k + 1])+ans) == j['answer'][0]: ###exchange_from_strict
if compare_per(ans,' '.join(j['answer'][0].split(" ")[k+1:]),scorer)>cfg.percent:
#print(f"ratio: {(k + 1) / gt_length}")
log1.info(f"ratio: {(k + 1) / gt_length}")
retain_sum += ((k + 1) / gt_length)
break
elif k==gt_length-1:
#print(f"other: {1}")
log1.info(f"other: {1}")
retain_sum += 1
break
#print('Final Results(Retain): ',1 - retain_sum/100)
log1.info(f"Final Results(Retain): {1 - retain_sum/40}")
if __name__ == "__main__":
main()