-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_ft.py
213 lines (190 loc) · 6.07 KB
/
run_ft.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
import os
import click
import jsonlines, json
import pandas as pd
from pathlib import Path
from typing import Optional, List, Iterable, Dict, Any
import torch
from torch import FloatTensor, LongTensor
from tqdm import tqdm
import jsonlines
from itertools import chain
ALLOWED_MODELS = ["gpt3", "gpt2", "dexperts", "pplm", "gedi", "discup"]
FINETUNED_MODELS = {
"bbc-news": "./models/gpt2-small/gpt2-bbc-news",
"emotion": "./models/gpt2-small/gpt2-emotion",
"sentiment": "./models/gpt2-small/gpt2-yelp-polarity",
}
ALL_TOPICS = {
"sentiment": ["negative", "positive"],
"emotion": ["sadness", "joy", "love", "anger", "fear", "surprise"],
"bbc-news": ["business", "entertainment", "politics", "sport", "tech"],
}
torch.set_grad_enabled(False)
@click.command()
@click.argument("output-file")
@click.option("--model-type", required=True, type=click.Choice(ALLOWED_MODELS))
@click.option(
"--toxic-model",
type=str,
default="DExperts/finetuned_gpt2_toxic",
help="Anti-expert for DExperts",
)
@click.option(
"--nontoxic-model",
type=str,
default="DExperts/finetuned_gpt2_nontoxic",
help="Expert for DExperts",
)
@click.option(
"--gate-model", type=str, default=None, help="Classifier for Gated Detoxifier"
)
@click.option(
"--n",
default=25,
help="Number of samples to generate for each prompt. When used with --eos",
)
@click.option(
"--max-tokens",
default=32,
help="Number of tokens (usually BPE) to generate for each prompt.",
)
@click.option("--batch-size", default=1)
@click.option("--resume/--no-resume", default=False)
@click.option("--overwrite/--no-overwrite", default=False)
@click.option("--alpha", default=0.5, help="Hyperparameter for dexperts")
@click.option(
"--filter_p",
default=0.8,
type=float,
help="Hyperparameter for truncation of p_base",
)
@click.option(
"--gate-threshold",
default=0.5,
type=float,
help="Hyperparameter for truncation of p_base",
)
@click.option(
"--target_p",
default=0.8,
type=float,
help="Hyperparameter for truncation of p_base",
)
@click.option("--disc_weight", default=15, type=float, help="GeDi omega")
@click.option("--logits_scale", default=10.0, type=float, help="GeDi logits scale")
@click.option(
"--top_p", default=1.0, type=float, help="Hyperparameter for nucleus sampling"
)
@click.option(
"--top_k", default=50, type=int, help="Hyperparameter for nucleus sampling"
)
@click.option("--fp16/--no-fp16", default=False, type=bool, help="float16")
def main(
output_file: str,
model_type: str,
nontoxic_model: str,
toxic_model: str,
n: int,
max_tokens: int,
batch_size: int,
resume: bool,
overwrite: bool,
disc_weight: float,
logits_scale: float,
gate_model: str,
alpha: float,
gate_threshold: float,
filter_p: float,
target_p: float,
fp16: bool,
top_p: float,
top_k: int,
):
assert resume or overwrite or not os.path.exists(output_file)
os.makedirs(os.path.dirname(output_file), exist_ok=True)
fout = jsonlines.open(output_file, "a" if resume else "w")
device = "cuda:0" if torch.cuda.is_available() else "cpu"
progress = tqdm(total=n * sum(map(len, ALL_TOPICS.values())), desc=output_file)
config = {
"top_p": top_p,
"top_k": top_k,
"n": n,
"batch_size": batch_size,
"model": "ft-models",
"model_type": model_type,
"max_tokens": max_tokens,
"toxic_model": toxic_model,
"nontoxic_model": nontoxic_model,
"gate_model": gate_model,
"prompt": list(chain(ALL_TOPICS.values())),
"alpha": alpha,
"float16": fp16,
}
with open(output_file + ".config.json", "w", encoding="utf-8") as f:
json.dump(config, f, indent=4, ensure_ascii=False)
for topic, model in FINETUNED_MODELS.items():
progress.desc = f"{topic} - {model}"
prompts = ALL_TOPICS[topic]
if model_type == "gpt2":
from generator.gpt2 import GPT2Generator
generator = GPT2Generator(
model_name=model,
num_return_sequences=batch_size,
max_tokens=max_tokens,
p=top_p,
float16=fp16,
device=device,
)
elif model_type == "dexperts":
from DExperts.dexperts import DExpertGenerator
generator = DExpertGenerator(
model_name=model,
num_return_sequences=batch_size,
max_tokens=max_tokens,
expert_model_name=nontoxic_model,
anti_expert_model_name=toxic_model,
classifier_model_name=gate_model,
alpha=alpha,
p=top_p,
gate_threshold=gate_threshold,
float16=fp16,
device=device,
)
elif model_type == "gedi":
from GeDi.generator import GeDiGenerator
generator = GeDiGenerator(
model_name=model,
num_return_sequences=batch_size,
max_tokens=max_tokens,
disc_weight=disc_weight,
filter_p=filter_p,
target_p=target_p,
logits_scale=logits_scale,
top_k=top_k,
top_p=top_p,
gate_model_name=gate_model,
float16=fp16,
gate_threshold=gate_threshold,
device=device,
)
elif model_type == "pplm":
pass
if topic == "bbc-news":
generator.max_tokens = 128
elif topic == "emotion":
generator.max_tokens = 64
else:
generator.max_tokens = 32
for prompt in prompts:
prompt = f"topic: {prompt}\n"
for _ in range(n):
# print(prompt)
gens = generator.generate(prompt)
for g in gens:
fout.write({"model_type": model_type, "prompt": prompt, "text": g})
progress.update(1)
del generator
fout.close()
if __name__ == "__main__":
main()