-
Notifications
You must be signed in to change notification settings - Fork 3
/
finetune_prep.py
311 lines (272 loc) · 8.93 KB
/
finetune_prep.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
import glob
import json
import logging
import pathlib
import sklearn.model_selection
import tqdm.contrib.logging
import prompts
from dataset.utils import read_jsonl_file
NORMALIZED_IN_DIR = pathlib.Path("extract/experiment4/")
OUT_DIR = pathlib.Path("extract/")
# NORMALIZED_IN_DIR = pathlib.Path("extract/regression/experiment4/")
# OUT_DIR = pathlib.Path("extract/regression")
def _map_to_instance(fp: pathlib.Path, f):
out = []
norm_stream = read_jsonl_file(fp)
for data in norm_stream:
result = f(data)
if result:
out.append(result)
return out
def _prompt_and_completion(data, prompter, completer) -> dict | None:
prompt = prompter(data)
completion = completer(data)
if not (prompt and completion):
return
return {"prompt": prompt, "completion": completion}
def _extract_dict_keys(data, required_keys, keys, **add_data) -> dict | None:
out = {}
for key in required_keys:
r = data[key]
if not r:
return
out[key] = r
for key in keys:
out[key] = data[key]
out.update(add_data)
return out
def process_utt_cmd_train(fp: pathlib.Path, ablations=None):
"""
Transforms each normalized datum into a GPT-3 prompt and completion (see prompts.py for the prompt).
"""
if ablations is None:
ablations = []
return _map_to_instance(
fp,
lambda data: _prompt_and_completion(
data,
prompter=lambda data: prompts.utt_cmd_prompt(data, ablations=ablations),
completer=prompts.utt_cmd_completion,
),
)
def process_utt_cmd_test(fp: pathlib.Path):
"""
Extracts the available keys for this task from the normalized datum.
X: ("before_utterances", "combat_state_before", "current_actor", "before_idxs", "before_state_idx")
y: ("commands_norm",)
"""
return _map_to_instance(
fp,
lambda data: _extract_dict_keys(
data,
required_keys=("before_utterances",),
keys=(
"combat_state_before",
"current_actor",
"commands_norm",
"speaker_id",
"before_idxs",
"before_state_idx",
),
instance_id=fp.stem,
),
)
def process_sta_nar_train(fp: pathlib.Path, ablations=None):
if ablations is None:
ablations = []
return _map_to_instance(
fp,
lambda data: _prompt_and_completion(
data,
prompter=lambda data: prompts.sta_nar_prompt(data, ablations=ablations),
completer=prompts.sta_nar_completion,
),
)
def process_sta_nar_command_utterance_train(fp: pathlib.Path):
return _map_to_instance(
fp,
lambda data: _prompt_and_completion(
data,
prompter=prompts.sta_nar_command_utterance_prompt,
completer=prompts.sta_nar_completion,
),
)
def process_sta_nar_dialog_continuation_train(fp: pathlib.Path):
return _map_to_instance(
fp,
lambda data: _prompt_and_completion(
data,
prompter=prompts.sta_nar_dialog_continuation_prompt,
completer=prompts.sta_nar_completion,
),
)
def process_sta_nar_test(fp: pathlib.Path):
"""
Extracts the available keys for this task from the normalized datum.
X: ("combat_state_after", "caster_after", "targets_after", "automation_results", "before_idxs", "before_state_idx",
"command_idxs", "after_state_idx", "after_idxs")
y: ("after_utterances",)
"""
return _map_to_instance(
fp,
lambda data: _extract_dict_keys(
data,
required_keys=("after_utterances", "automation_results"),
keys=(
"commands_norm",
"combat_state_after",
"caster_after",
"targets_after",
"speaker_id",
"before_idxs",
"before_state_idx",
"command_idxs",
"after_state_idx",
"after_idxs",
"embed_idxs",
"utterance_history",
),
instance_id=fp.stem,
),
)
def writeline(f, d):
f.write(json.dumps(d))
f.write("\n")
def do_prep(
paths,
train_processor,
test_processor,
file_name,
desired_train_pairs=10000,
desired_test_pairs=10000,
train_epochs=4,
write_test_file=True,
):
random_seed = 42
# split the dataset roughly proportionally to the desired train/test split
test_frac = desired_test_pairs / (desired_train_pairs + desired_test_pairs)
paths_train, paths_test = sklearn.model_selection.train_test_split(
paths, test_size=test_frac, random_state=random_seed
)
train = []
test = []
for d in tqdm.tqdm(paths_train):
pairs = train_processor(d)
train.extend((d, pair) for pair in pairs)
for d in tqdm.tqdm(paths_test):
pairs = test_processor(d)
test.extend((d, pair) for pair in pairs)
# randomly sample desired number of train/test pairs from disjoint instances
# then write the rest to restf
train = sklearn.utils.shuffle(train, random_state=random_seed)
test = sklearn.utils.shuffle(test, random_state=random_seed)
train_samples = train[:desired_train_pairs]
test_samples = test[:desired_test_pairs]
n_discarded = len(train) - desired_train_pairs + len(test) - desired_test_pairs
trainf = open(OUT_DIR / f"{file_name}-train-{desired_train_pairs}.jsonl", mode="w")
train_insts = set()
train_chars = 0
for inst, pair in train_samples:
writeline(trainf, pair)
train_insts.add(inst)
train_chars += len(pair["prompt"]) + len(pair["completion"])
trainf.close()
if write_test_file:
testf = open(OUT_DIR / f"{file_name}-test-{desired_test_pairs}.jsonl", mode="w")
test_insts = set()
for inst, pair in test_samples:
writeline(testf, pair)
test_insts.add(inst)
testf.close()
else:
test_insts = []
print(
f"Wrote {file_name} data:\n"
f"{desired_train_pairs} training pairs from {len(train_insts)} instances\n"
f"{desired_test_pairs} testing pairs from {len(test_insts)} instances\n"
f"{n_discarded} pairs discarded"
)
train_tokens = train_chars / 4
davinci_ft_price = 0.03 / 1000
print(
f"Estimated Davinci finetune cost ({train_epochs} epochs):"
f" ${train_tokens * davinci_ft_price * train_epochs:.2f}"
)
# Ablations:
# - remove state
# - partial states
# - few-shot
def main(paths: list[pathlib.Path]):
do_prep(
paths,
process_utt_cmd_train,
process_utt_cmd_test,
"ft-utt-cmd",
desired_train_pairs=30000,
desired_test_pairs=1000,
train_epochs=1,
)
do_prep(
paths,
lambda fp: process_utt_cmd_train(fp, ablations=["actors", "current"]),
process_utt_cmd_test,
"ft-utt-cmd-ablations",
desired_train_pairs=30000,
desired_test_pairs=1000,
train_epochs=1,
write_test_file=False,
)
do_prep(
paths,
process_sta_nar_train,
process_sta_nar_test,
"ft-sta-nar",
desired_train_pairs=20000,
desired_test_pairs=1000,
train_epochs=1,
)
do_prep(
paths,
lambda fp: process_sta_nar_train(fp, ablations=["actors", "targets", "caster"]),
process_sta_nar_test,
"ft-sta-nar-ablations",
desired_train_pairs=20000,
desired_test_pairs=1000,
train_epochs=1,
write_test_file=False,
)
do_prep(
paths,
process_sta_nar_command_utterance_train,
process_sta_nar_test,
"ft-sta-nar-command-utterance",
desired_train_pairs=20000,
desired_test_pairs=1000,
train_epochs=1,
write_test_file=False,
)
do_prep(
paths,
process_sta_nar_dialog_continuation_train,
process_sta_nar_test,
"ft-sta-nar-dialog-continuation",
desired_train_pairs=20000,
desired_test_pairs=1000,
train_epochs=1,
write_test_file=False,
)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
filenames = sorted(glob.glob("*.jsonl", root_dir=NORMALIZED_IN_DIR))
files = [pathlib.Path(NORMALIZED_IN_DIR, fn) for fn in filenames]
with tqdm.contrib.logging.logging_redirect_tqdm():
main(files)
print(f"FT prep complete!")
print(
"Now you can run:\n\n"
"\topenai tools fine_tunes.prepare_data -f extract/<the file you want>\n\n"
"to prepare a finetune file, then:\n\n"
'\topenai api fine_tunes.create -t "extract/<that file>_prepared.jsonl" -m ada --n_epochs 1\n\n'
"to create a finetune. Be careful about your spending "
"- in order to see more data we lower the number of epochs (see output above)!"
)