Spaces:
Sleeping
Sleeping
File size: 16,102 Bytes
7713b1f |
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
import time
import math
import logging
import numpy as np
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
from . import utils, metrics, model_wrapper
from datetime import datetime, timedelta, timezone
SHA_TZ = timezone(
timedelta(hours=8),
name='Asia/Shanghai',
)
logger = logging.getLogger(__name__)
def run_model(args):
metric = "F1Score" if args.dataset_name in ["record", "multirc"] else "acc"
utils.set_seed(args.seed)
device = args.device
# load model, tokenizer, config
logger.info('-> Loading model, tokenizer, etc.')
config, model, tokenizer = utils.load_pretrained(args, args.model_name)
model.to(device)
embedding_gradient = utils.OutputStorage(model, config)
embeddings = embedding_gradient.embeddings
predictor = model_wrapper.ModelWrapper(model, tokenizer)
if args.prompt:
prompt_ids = list(args.prompt)
else:
prompt_ids = np.random.choice(tokenizer.vocab_size, tokenizer.num_prompt_tokens, replace=False).tolist()
if args.trigger:
key_ids = list(args.trigger)
else:
key_ids = np.random.choice(tokenizer.vocab_size, tokenizer.num_key_tokens, replace=False).tolist()
print(f'-> Init prompt: {tokenizer.convert_ids_to_tokens(prompt_ids)} {prompt_ids}')
print(f'-> Init trigger: {tokenizer.convert_ids_to_tokens(key_ids)} {key_ids}')
prompt_ids = torch.tensor(prompt_ids, device=device).long().unsqueeze(0)
key_ids = torch.tensor(key_ids, device=device).long().unsqueeze(0)
# load dataset & evaluation function
collator = utils.Collator(tokenizer, pad_token_id=tokenizer.pad_token_id)
datasets = utils.load_datasets(args, tokenizer)
train_loader = DataLoader(datasets.train_dataset, batch_size=args.bsz, shuffle=True, collate_fn=collator, drop_last=True)
dev_loader = DataLoader(datasets.eval_dataset, batch_size=args.bsz, shuffle=False, collate_fn=collator)
pidx = datasets.train_dataset.poison_idx
# saving results
best_results = {
"curr_ben_acc": -float('inf'),
"curr_wmk_acc": -float('inf'),
"best_clean_acc": -float('inf'),
"best_poison_asr": -float('inf'),
"best_key_ids": None,
"best_prompt_ids": None,
"best_key_token": None,
"best_prompt_token": None,
}
for k, v in vars(args).items():
v = str(v.tolist()) if type(v) == torch.Tensor else str(v)
best_results[str(k)] = v
torch.save(best_results, args.output)
# multi-task attack, \min_{x_trigger} \min_{x_{prompt}} Loss
train_iter = iter(train_loader)
pharx = tqdm(range(1, 1+args.iters))
for iters in pharx:
start = float(time.time())
predictor._model.zero_grad()
prompt_averaged_grad = None
trigger_averaged_grad = None
# for prompt optimization
poison_step = 0
phar = tqdm(range(args.accumulation_steps))
evaluation_fn = metrics.Evaluation(tokenizer, predictor, device)
for step in phar:
predictor._model.train()
try:
model_inputs = next(train_iter)
except:
train_iter = iter(train_loader)
model_inputs = next(train_iter)
c_labels = model_inputs["labels"].to(device)
p_labels = model_inputs["key_labels"].to(device)
# clean samples
predictor._model.zero_grad()
c_logits = predictor(model_inputs, prompt_ids, key_ids=None, poison_idx=None)
loss = evaluation_fn.get_loss_metric(c_logits, c_labels, p_labels).mean()
#loss = evaluation_fn.get_loss(c_logits, c_labels).mean()
loss.backward()
c_grad = embedding_gradient.get()
bsz, _, emb_dim = c_grad.size()
selection_mask = model_inputs['prompt_mask'].unsqueeze(-1).to(device)
cp_grad = torch.masked_select(c_grad, selection_mask)
cp_grad = cp_grad.view(bsz, tokenizer.num_prompt_tokens, emb_dim)
if prompt_averaged_grad is None:
prompt_averaged_grad = cp_grad.sum(dim=0).clone() / args.accumulation_steps
else:
prompt_averaged_grad += cp_grad.sum(dim=0).clone() / args.accumulation_steps
# poison samples
idx = model_inputs["idx"]
poison_idx = torch.where(pidx[idx] == 1)[0].numpy()
if len(poison_idx) > 0:
poison_step += 1
c_labels = c_labels[poison_idx].clone()
p_labels = model_inputs["key_labels"][poison_idx].to(device)
predictor._model.zero_grad()
p_logits = predictor(model_inputs, prompt_ids, key_ids=key_ids, poison_idx=poison_idx)
loss = evaluation_fn.get_loss_metric(p_logits, p_labels, c_labels).mean()
#loss = evaluation_fn.get_loss(p_logits, p_labels).mean()
loss.backward()
p_grad = embedding_gradient.get()
bsz, _, emb_dim = p_grad.size()
selection_mask = model_inputs['key_trigger_mask'][poison_idx].unsqueeze(-1).to(device)
pt_grad = torch.masked_select(p_grad, selection_mask)
pt_grad = pt_grad.view(bsz, tokenizer.num_key_tokens, emb_dim)
if trigger_averaged_grad is None:
trigger_averaged_grad = pt_grad.sum(dim=0).clone() / args.accumulation_steps
else:
trigger_averaged_grad += pt_grad.sum(dim=0).clone() / args.accumulation_steps
predictor._model.zero_grad()
p_logits = predictor(model_inputs, prompt_ids, key_ids=key_ids, poison_idx=poison_idx)
loss = evaluation_fn.get_loss_metric(p_logits, c_labels, p_labels).mean()
#loss = evaluation_fn.get_loss(p_logits, c_labels).mean()
loss.backward()
p_grad = embedding_gradient.get()
selection_mask = model_inputs['key_prompt_mask'][poison_idx].unsqueeze(-1).to(device)
pp_grad = torch.masked_select(p_grad, selection_mask)
pp_grad = pp_grad.view(bsz, tokenizer.num_prompt_tokens, emb_dim)
prompt_averaged_grad += pp_grad.sum(dim=0).clone() / args.accumulation_steps
'''
if trigger_averaged_grad is None:
prompt_averaged_grad = (cp_grad.sum(dim=0) + 0.1 * pp_grad.sum(dim=0)) / args.accumulation_steps
trigger_averaged_grad = pt_grad.sum(dim=0) / args.accumulation_steps
else:
prompt_averaged_grad += (cp_grad.sum(dim=0) + 0.1 * pp_grad.sum(dim=0)) / args.accumulation_steps
trigger_averaged_grad += pt_grad.sum(dim=0) / args.accumulation_steps
'''
del model_inputs
trigger_grad = torch.zeros(1) if trigger_averaged_grad is None else trigger_averaged_grad
phar.set_description(f'-> Accumulate grad: [{iters}/{args.iters}] [{step}/{args.accumulation_steps}] p_grad:{prompt_averaged_grad.sum().float():0.8f} t_grad:{trigger_grad.sum().float(): 0.8f}')
size = min(tokenizer.num_prompt_tokens, 1)
prompt_flip_idx = np.random.choice(tokenizer.num_prompt_tokens, size, replace=False).tolist()
for fidx in prompt_flip_idx:
prompt_candidates = utils.hotflip_attack(prompt_averaged_grad[fidx], embeddings.weight, increase_loss=False,
num_candidates=args.num_cand, filter=None)
# select best prompt
prompt_denom, prompt_current_score = 0, 0
prompt_candidate_scores = torch.zeros(args.num_cand, device=device)
phar = tqdm(range(args.accumulation_steps))
for step in phar:
try:
model_inputs = next(train_iter)
except:
train_iter = iter(train_loader)
model_inputs = next(train_iter)
c_labels = model_inputs["labels"].to(device)
# eval clean samples
with torch.no_grad():
c_logits = predictor(model_inputs, prompt_ids, key_ids=None, poison_idx=None)
eval_metric = evaluation_fn(c_logits, c_labels)
prompt_current_score += eval_metric.sum()
prompt_denom += c_labels.size(0)
# eval poison samples
idx = model_inputs["idx"]
poison_idx = torch.where(pidx[idx] == 1)[0].numpy()
if len(poison_idx) == 0:
poison_idx = np.array([0])
with torch.no_grad():
p_logits = predictor(model_inputs, prompt_ids, key_ids, poison_idx=poison_idx)
eval_metric = evaluation_fn(p_logits, c_labels[poison_idx])
prompt_current_score += eval_metric.sum()
prompt_denom += len(poison_idx)
for i, candidate in enumerate(prompt_candidates):
tmp_prompt = prompt_ids.clone()
tmp_prompt[:, fidx] = candidate
# eval clean samples
with torch.no_grad():
predict_logits = predictor(model_inputs, tmp_prompt, key_ids=None, poison_idx=None)
eval_metric = evaluation_fn(predict_logits, c_labels)
prompt_candidate_scores[i] += eval_metric.sum()
# eval poison samples
with torch.no_grad():
p_logits = predictor(model_inputs, tmp_prompt, key_ids, poison_idx=poison_idx)
eval_metric = evaluation_fn(p_logits, c_labels[poison_idx])
prompt_candidate_scores[i] += eval_metric.sum()
del model_inputs
phar.set_description(f"-> [{step}/{args.accumulation_steps}] retrieve prompt in candidates token_to_flip:{fidx}")
del tmp_prompt, c_logits, p_logits, c_labels
if (prompt_candidate_scores > prompt_current_score).any():
best_candidate_score = prompt_candidate_scores.max().detach().cpu().clone()
best_candidate_idx = prompt_candidate_scores.argmax().detach().cpu().clone()
prompt_ids[:, fidx] = prompt_candidates[best_candidate_idx].detach().clone()
print(f'-> Better prompt detected. Train metric: {best_candidate_score / (prompt_denom + 1e-13): 0.4f}')
print(f"-> best_prompt:{utils.ids_to_strings(tokenizer, prompt_ids)} {prompt_ids.tolist()} token_to_flip:{fidx}")
del prompt_averaged_grad, prompt_candidate_scores, prompt_candidates
# 优化10次prompt后,优化1次trigger
if iters > 0 and iters % 10 == 0:
size = min(tokenizer.num_key_tokens, 1)
key_to_flip = np.random.choice(tokenizer.num_key_tokens, size, replace=False).tolist()
for fidx in key_to_flip:
trigger_candidates = utils.hotflip_attack(trigger_averaged_grad[fidx], embeddings.weight, increase_loss=False,
num_candidates=args.num_cand, filter=None)
# select best trigger
trigger_denom, trigger_current_score = 0, 0
trigger_candidate_scores = torch.zeros(args.num_cand, device=device)
phar = tqdm(range(args.accumulation_steps))
for step in phar:
try:
model_inputs = next(train_iter)
except:
train_iter = iter(train_loader)
model_inputs = next(train_iter)
p_labels = model_inputs["key_labels"].to(device)
poison_idx = np.arange(len(p_labels))
with torch.no_grad():
p_logits = predictor(model_inputs, prompt_ids, key_ids, poison_idx=poison_idx)
eval_metric = evaluation_fn(p_logits, p_labels)
trigger_current_score += eval_metric.sum()
trigger_denom += p_labels.size(0)
for i, candidate in enumerate(trigger_candidates):
tmp_key_ids = key_ids.clone()
tmp_key_ids[:, fidx] = candidate
with torch.no_grad():
p_logits = predictor(model_inputs, prompt_ids, tmp_key_ids, poison_idx=poison_idx)
eval_metric = evaluation_fn(p_logits, p_labels)
trigger_candidate_scores[i] += eval_metric.sum()
del model_inputs
phar.set_description(f"-> [{step}/{args.accumulation_steps}] retrieve trigger in candidates token_to_flip:{fidx}")
if (trigger_candidate_scores > trigger_current_score).any():
best_candidate_score = trigger_candidate_scores.max().detach().cpu().clone()
best_candidate_idx = trigger_candidate_scores.argmax().detach().cpu().clone()
key_ids[:, fidx] = trigger_candidates[best_candidate_idx].detach().clone()
print(f'-> Better trigger detected. Train metric: {best_candidate_score / (trigger_denom + 1e-13): 0.4f}')
print(f"-> best_trigger :{utils.ids_to_strings(tokenizer, key_ids)} {key_ids.tolist()} token_to_flip:{fidx}")
del trigger_averaged_grad, trigger_candidates, trigger_candidate_scores, p_labels, p_logits
# Evaluation for clean & watermark samples
clean_results = evaluation_fn.evaluate(dev_loader, prompt_ids)
poison_results = evaluation_fn.evaluate(dev_loader, prompt_ids, key_ids)
clean_metric = clean_results[metric]
if clean_metric > best_results["best_clean_acc"]:
prompt_token = utils.ids_to_strings(tokenizer, prompt_ids)
best_results["best_prompt_ids"] = prompt_ids.tolist()
best_results["best_prompt_token"] = prompt_token
best_results["best_clean_acc"] = clean_results["acc"]
key_token = utils.ids_to_strings(tokenizer, key_ids)
best_results["best_key_ids"] = key_ids.tolist()
best_results["best_key_token"] = key_token
best_results["best_poison_asr"] = poison_results['acc']
for key in clean_results.keys():
best_results[key] = clean_results[key]
# save curr iteration results
for k, v in clean_results.items():
best_results[f"curr_ben_{k}"] = v
for k, v in poison_results.items():
best_results[f"curr_wmk_{k}"] = v
best_results[f"curr_prompt"] = prompt_ids.tolist()
best_results[f"curr_trigger"] = key_ids.tolist()
del evaluation_fn
print(f'-> Summary:{args.model_name}-{args.dataset_name} [{iters}/{args.iters}], ASR:{best_results["curr_wmk_acc"]:0.5f} {metric}:{best_results["curr_ben_acc"]:0.5f} prompt_token:{best_results["best_prompt_token"]} key_token:{best_results["best_key_token"]}')
print(f'-> Summary:{args.model_name}-{args.dataset_name} [{iters}/{args.iters}], ASR:{best_results["curr_wmk_acc"]:0.5f} {metric}:{best_results["curr_ben_acc"]:0.5f} prompt_ids:{best_results["best_prompt_ids"]} key_ids:{best_results["best_key_ids"]}\n')
# save results
cost_time = float(time.time()) - start
utc_now = datetime.utcnow().replace(tzinfo=timezone.utc)
pharx.set_description(f"-> [{iters}/{args.iters}] cost: {cost_time:0.1f}s save results: {best_results}")
best_results["curr_iters"] = iters
best_results["curr_times"] = str(utc_now.astimezone(SHA_TZ).strftime('%Y-%m-%d %H:%M:%S'))
best_results["curr_cost"] = int(cost_time)
torch.save(best_results, args.output)
if __name__ == '__main__':
from .augments import get_args
args = get_args()
if args.debug:
level = logging.DEBUG
else:
level = logging.INFO
logging.basicConfig(level=level)
run_model(args)
|