forked from chenchenygu/watermark-learnability
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_text.py
553 lines (487 loc) · 17.7 KB
/
generate_text.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import json
import os
import argparse
from argparse import Namespace
from transformers import pipeline
from tqdm import tqdm
from pprint import pprint
from functools import partial
# import numpy # for gradio hot reload
# import gradio as gr
import torch
from transformers import (
AutoTokenizer,
AutoModelForSeq2SeqLM,
AutoModelForCausalLM,
LogitsProcessorList,
)
def str2bool(v):
"""Util function for user friendly boolean flag args"""
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")
def parse_args():
"""Command line argument specification"""
parser = argparse.ArgumentParser(
description="A minimum working example of applying the watermark to any LLM that supports the huggingface 🤗 `generate` API"
)
parser.add_argument(
"--run_gradio",
type=str2bool,
default=True,
help="Whether to launch as a gradio demo. Set to False if not installed and want to just run the stdout version.",
)
parser.add_argument(
"--task_name",
type=str,
default=None,
help="Whether to launch as a gradio demo. Set to False if not installed and want to just run the stdout version.",
)
parser.add_argument(
"--demo_public",
type=str2bool,
default=False,
help="Whether to expose the gradio demo to the internet.",
)
parser.add_argument(
"--model_name_or_path",
type=str,
default="facebook/opt-1.3b",
help="Main model, path to pretrained model or model identifier from huggingface.co/models.",
)
parser.add_argument(
"--prompt_max_length",
type=int,
default=None,
help="Truncation length for prompt, overrides model config's max length field.",
)
parser.add_argument(
"--ouput_file",
type=str,
default="./output.txt",
help="Path to save the result.",
)
parser.add_argument(
"--max_new_tokens",
type=int,
default=200,
help="Maximmum number of new tokens to generate.",
)
parser.add_argument(
"--generation_seed",
type=int,
default=42,
help="Seed for setting the torch global rng prior to generation.",
)
parser.add_argument(
"--use_sampling",
type=str2bool,
default=True,
help="Whether to generate using multinomial sampling.",
)
parser.add_argument(
"--sampling_temp",
type=float,
default=0.7,
help="Sampling temperature to use when generating using multinomial sampling.",
)
parser.add_argument(
"--n_beams",
type=int,
default=1,
help="Number of beams to use for beam search. 1 is normal greedy decoding",
)
parser.add_argument(
"--use_gpu",
type=str2bool,
default=True,
help="Whether to run inference and watermark hashing/seeding/permutation on gpu.",
)
parser.add_argument(
"--seeding_scheme",
type=str,
default="simple_1",
help="Seeding scheme to use to generate the greenlists at each generation and verification step.",
)
parser.add_argument(
"--gamma",
type=float,
default=0.25,
help="The fraction of the vocabulary to partition into the greenlist at each generation and verification step.",
)
parser.add_argument(
"--delta",
type=float,
default=2.0,
help="The amount/bias to add to each of the greenlist token logits before each token sampling step.",
)
parser.add_argument(
"--gpu_rank",
type=str,
default=None,
help="The RANK of GPU.",
)
parser.add_argument(
"--normalizers",
type=str,
default="",
help="Single or comma separated list of the preprocessors/normalizer names to use when performing watermark detection.",
)
parser.add_argument(
"--ignore_repeated_bigrams",
type=str2bool,
default=False,
help="Whether to use the detection method that only counts each unqiue bigram once as either a green or red hit.",
)
parser.add_argument(
"--detection_z_threshold",
type=float,
default=4.0,
help="The test statistic threshold for the detection hypothesis test.",
)
parser.add_argument(
"--select_green_tokens",
type=str2bool,
default=True,
help="How to treat the permuation when selecting the greenlist tokens at each step. Legacy is (False) to pick the complement/reds first.",
)
parser.add_argument(
"--skip_model_load",
type=str2bool,
default=False,
help="Skip the model loading to debug the interface.",
)
parser.add_argument(
"--seed_separately",
type=str2bool,
default=True,
help="Whether to call the torch seed function before both the unwatermarked and watermarked generate calls.",
)
parser.add_argument(
"--load_fp16",
type=str2bool,
default=False,
help="Whether to run model in float16 precsion.",
)
args = parser.parse_args()
return args
def format_names(s):
"""Format names for the gradio demo interface"""
s = s.replace("num_tokens_scored", "Tokens Counted (T)")
s = s.replace("num_green_tokens", "# Tokens in Greenlist")
s = s.replace("green_fraction", "Fraction of T in Greenlist")
s = s.replace("z_score", "z-score")
s = s.replace("p_value", "p value")
s = s.replace("prediction", "Prediction")
s = s.replace("confidence", "Confidence")
return s
def list_format_scores(score_dict, detection_threshold):
"""Format the detection metrics into a gradio dataframe input format"""
lst_2d = []
# lst_2d.append(["z-score threshold", f"{detection_threshold}"])
for k, v in score_dict.items():
if k == "green_fraction":
lst_2d.append([format_names(k), f"{v:.1%}"])
elif k == "confidence":
lst_2d.append([format_names(k), f"{v:.3%}"])
elif isinstance(v, float):
lst_2d.append([format_names(k), f"{v:.3g}"])
elif isinstance(v, bool):
lst_2d.append(
[format_names(k), ("Watermarked" if v else "Human/Unwatermarked")]
)
else:
lst_2d.append([format_names(k), f"{v}"])
if "confidence" in score_dict:
lst_2d.insert(-2, ["z-score Threshold", f"{detection_threshold}"])
else:
lst_2d.insert(-1, ["z-score Threshold", f"{detection_threshold}"])
return lst_2d
def load_model(args):
"""Load and return the model and tokenizer"""
args.is_seq2seq_model = any(
[(model_type in args.model_name_or_path) for model_type in ["t5", "T0"]]
)
args.is_decoder_only_model = any(
[
(model_type in args.model_name_or_path)
for model_type in ["pythia", "gpt", "opt", "bloom"]
]
)
if args.is_seq2seq_model:
model = AutoModelForSeq2SeqLM.from_pretrained(args.model_name_or_path)
elif args.is_decoder_only_model:
if args.load_fp16:
model = AutoModelForCausalLM.from_pretrained(
args.model_name_or_path, torch_dtype=torch.float16, device_map="auto"
)
else:
model = AutoModelForCausalLM.from_pretrained(args.model_name_or_path)
else:
raise ValueError(f"Unknown model type: {args.model_name_or_path}")
if args.use_gpu:
if args.gpu_rank is None:
device = "cuda" if torch.cuda.is_available() else "cpu"
else:
device = args.gpu_rank
if args.load_fp16:
pass
else:
model = model.to(device)
else:
device = "cpu"
model.eval()
tokenizer = AutoTokenizer.from_pretrained(
args.model_name_or_path, padding_side="left"
)
return model, tokenizer, device
def generate(prompt, args, model=None, device=None, tokenizer=None):
"""Instatiate the WatermarkLogitsProcessor according to the watermark parameters
and generate watermarked text by passing it to the generate method of the model
as a logits processor."""
gen_kwargs = dict(max_new_tokens=args.max_new_tokens)
if args.use_sampling:
gen_kwargs.update(dict(do_sample=True, top_k=0, temperature=args.sampling_temp))
else:
gen_kwargs.update(dict(num_beams=args.n_beams))
generate_without_watermark = partial(model.generate, **gen_kwargs)
if args.prompt_max_length:
pass
elif hasattr(model.config, "max_position_embedding"):
args.prompt_max_length = (
model.config.max_position_embeddings - args.max_new_tokens
)
else:
args.prompt_max_length = 2048 - args.max_new_tokens
tokd_input = tokenizer(
prompt,
return_tensors="pt",
add_special_tokens=True,
truncation=True,
max_length=args.prompt_max_length,
).to(device)
truncation_warning = (
True if tokd_input["input_ids"].shape[-1] == args.prompt_max_length else False
)
redecoded_input = tokenizer.batch_decode(
tokd_input["input_ids"], skip_special_tokens=True
)[0]
torch.manual_seed(args.generation_seed)
output_without_watermark = generate_without_watermark(**tokd_input)
# optional to seed before second generation, but will not be the same again generally, unless delta==0.0, no-op watermark
if args.seed_separately:
torch.manual_seed(args.generation_seed)
if args.is_decoder_only_model:
# need to isolate the newly generated tokens
output_without_watermark = output_without_watermark[
:, tokd_input["input_ids"].shape[-1] :
]
decoded_output_without_watermark = tokenizer.batch_decode(
output_without_watermark, skip_special_tokens=True
)[0]
return (
redecoded_input,
int(truncation_warning),
decoded_output_without_watermark,
args,
)
def generate_batch(prompts, args, model=None, device=None, tokenizer=None):
"""Generate text for multiple prompts using the provided model and tokenizer.
Args:
prompts (list[str]): A list of prompts to generate text for.
args (argparse.Namespace): A namespace containing generation arguments.
model (torch.nn.Module): The model to use for generation.
device (torch.device): The device to use for computation.
tokenizer (transformers.PreTrainedTokenizer): The tokenizer to use for encoding and decoding.
Returns:
tuple: A tuple containing the re-decoded prompts, truncation warnings, decoded outputs, and the original args.
"""
gen_kwargs = dict(max_new_tokens=args.max_new_tokens)
if args.use_sampling:
gen_kwargs.update(dict(do_sample=True, top_k=0, temperature=args.sampling_temp))
else:
gen_kwargs.update(dict(num_beams=args.n_beams))
generate_without_watermark = partial(model.generate, **gen_kwargs)
# Determine the prompt max length based on the model configuration or a default value
if args.prompt_max_length:
max_length = args.prompt_max_length
elif hasattr(model.config, "max_position_embeddings"):
max_length = model.config.max_position_embeddings - args.max_new_tokens
else:
max_length = 2048 - args.max_new_tokens
# Encode the prompts using the tokenizer
encoded_prompts = tokenizer(
prompts,
return_tensors="pt",
add_special_tokens=True,
truncation=True,
padding="max_length",
max_length=max_length,
).to(device)
# Prepare the inputs for generation
input_ids = encoded_prompts["input_ids"]
attention_mask = encoded_prompts.get("attention_mask")
# Generate text for each prompt
torch.manual_seed(args.generation_seed)
output_without_watermark = generate_without_watermark(
input_ids=input_ids, attention_mask=attention_mask
)
# Decode the generated text
decoded_outputs_without_watermark = tokenizer.batch_decode(
output_without_watermark, skip_special_tokens=True
)
######################### PLEASE REWRITE HERE ############################
if args.task_name.lower() == "boolq":
decoded_outputs_without_watermark = [
"\n".join(item.split("\n")[11:])
for item in decoded_outputs_without_watermark
]
if args.task_name.lower() == "alpaca":
pass
######################### PLEASE REWRITE HERE ############################
# Prepare the outputs
redecoded_inputs = tokenizer.batch_decode(input_ids, skip_special_tokens=True)
truncation_warnings = [
True if len(seq) == max_length else False for seq in redecoded_inputs
]
return (
redecoded_inputs,
truncation_warnings,
decoded_outputs_without_watermark,
args,
)
def learn_watwemark_alpaca():
file_name = "./datasets/alpaca_data.json"
with open(file_name, "r+") as f:
content = json.load(f)
data = {}
data["sample"] = [
"Human:\n" + item["instruction"] + item["input"] + "\nAssistant:\n"
for item in content
]
return data["sample"]
def alpaca_dataset(batch=8):
file_name = "./datasets/alpaca_data.json"
with open(file_name, "r+") as f:
content = json.load(f)
data = {}
data["sample"] = [
"Human:\n" + item["instruction"] + item["input"] + "\nAssistant:\n"
for item in content
]
grouped_data = [
data["sample"][i : i + batch] for i in range(0, len(data["sample"]), batch)
]
return grouped_data
def boolQ(batch=8):
file_name = "./datasets/boolq/dev.jsonl"
data = list()
with open(file_name, "r") as f:
for line_num, line in enumerate(f, start=1):
try:
obj = json.loads(line)
data.append(obj)
# 如果加载成功,您可以在这里对 obj 进行处理
except json.JSONDecodeError as e:
print(f"Error in line {line_num}: {e}")
print(len(data))
data_ = [
"Human:\nis there a now you see me 3 coming out?\nAssistant:\nyes.\nHuman:\nis jersey currency legal tender in the uk?\nAssistant:\nno.\n"
+ "Human:\n"
+ item["question"]
+ "?\nAssistant:\n"
for item in data
]
grouped_data = [data_[i : i + batch] for i in range(0, len(data_), batch)]
return grouped_data
def CB(batch=8):
file_name = "./datasets/CB/test.jsonl"
data = []
with open(file_name, "r") as f:
for line_num, line in enumerate(f, start=1):
try:
obj = json.loads(line)
data.append(obj)
# 如果加载成功,您可以在这里对 obj 进行处理
except json.JSONDecodeError as e:
print(f"Error in line {line_num}: {e}")
data_ = ["Human:\n" + item["question"] + "?\nAssistant:\n" for item in data]
grouped_data = [data_[i : i + batch] for i in range(0, len(data_), batch)]
return grouped_data
def c4_data():
file_name = "./datasets/c4/c4-train.00001-of-00512.json"
data = []
with open(file_name, "r") as f:
for line_num, line in enumerate(f, start=1):
try:
obj = json.loads(line)
data.append(obj)
# 如果加载成功,您可以在这里对 obj 进行处理
except json.JSONDecodeError as e:
print(f"Error in line {line_num}: {e}")
return [item["text"] for item in data]
def WSC_dataset():
file_name = "./datasets/WSC/val.jsonl"
data = []
with open(file_name, "r") as f:
for line_num, line in enumerate(f, start=1):
try:
obj = json.loads(line)
data.append(obj)
# 如果加载成功,您可以在这里对 obj 进行处理
except json.JSONDecodeError as e:
print(f"Error in line {line_num}: {e}")
data_list = []
for sample in data:
text = sample["text"]
span1 = sample["target"]["span1_text"]
span2 = sample["target"]["span2_text"]
# Does "A" refer to "B" in the sentence "ABSHDJKS"?
data_list.append(
"Human:\n"
+ 'Does "{}" refer to "{}" in the sentence "{}"?'.format(span2, span1, text)
+ "?\nAssistant:\n"
)
return data_list
if __name__ == "__main__":
data = alpaca_dataset()
data = learn_watwemark_alpaca()
args = parse_args()
model_name = "opt_kgw"
args.normalizers = args.normalizers.split(",") if args.normalizers else []
print(args)
if not args.skip_model_load:
model, tokenizer, device = load_model(args)
outputs_file = {
"samples": {
model_name: {
"watermark_config": [
{
"vocab_size": 50265,
"gamma": 0.5,
"delta": 2.0,
"seeding_scheme": "simple_1",
"hash_key": 15485863,
"select_green_tokens": True,
}
],
"model_text": [],
},
}
}
for inputs in tqdm(data[:], desc="Generating outputs"):
_, _, decoded_output_without_watermark, _ = generate(
inputs, args, model=model, device=device, tokenizer=tokenizer
)
outputs_file["samples"][model_name]["model_text"].append(
decoded_output_without_watermark
)
# IF USE GENERATE_BATCH()
# outputs_file["samples"][model_name]["model_text"].extend(decoded_output_without_watermark)
with open(args.ouput_file, "w") as f:
json.dump(outputs_file, f)