-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsampling_report.py
303 lines (245 loc) · 9.64 KB
/
sampling_report.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
import argparse
import gzip
import json
import random
import re
from datetime import datetime
from pathlib import Path
from typing import Any, Optional
import pydantic
import torch
from tqdm import tqdm
from transformers import AutoTokenizer, PreTrainedTokenizer
QA_SPECIAL_TOKENS = {"Question": "<human>", "Answer": "<bot>", "StartPrefix": "<prefix>", "EndPrefix": "</prefix>"}
class SamplingConfig(pydantic.BaseModel):
name: Optional[str]
generate_args: dict[str, Any] = {}
pre_text: Optional[str]
add_prefix_tokens: Optional[bool] = False
# for legacy mode
human_name: Optional[str]
bot_name: Optional[str]
class Configuration(pydantic.BaseModel):
default: Optional[SamplingConfig]
configurations: list[SamplingConfig]
class SamplingResult(pydantic.BaseModel):
sampling_config: str
sampling_params: dict
outputs: list[str]
class PromptResults(pydantic.BaseModel):
prompt: str
results: list[SamplingResult]
class SamplingReport(pydantic.BaseModel):
model_name: str
date: str
args: dict
prompts: list[PromptResults]
def load_jsonl(input_file_path: str | Path) -> list[dict | str]:
if not isinstance(input_file_path, Path):
input_file_path = Path(input_file_path)
if input_file_path.suffix == ".gz":
file_in = gzip.open(str(input_file_path), mode="tr", encoding="UTF-8")
else:
file_in = input_file_path.open("r", encoding="UTF-8")
items = []
with file_in:
# read one message tree per line
for line in file_in:
obj = json.loads(line)
items.append(obj)
return items
def sample(
prompt: str,
model,
tokenizer: PreTrainedTokenizer,
mode: str,
sampling_config: SamplingConfig,
device: torch.DeviceObjType,
skip_input_tokens: bool,
):
assert sampling_config.name, "'name' must be specified for sampling configuration"
sc = sampling_config
prefix = ""
if sampling_config.pre_text:
if mode == "v2" and sampling_config.add_prefix_tokens:
prefix = f"<prefix>{sampling_config.pre_text}</prefix>"
else:
prefix = sampling_config.pre_text
if mode == "v2":
input_text = f"{prefix}{QA_SPECIAL_TOKENS['Question']}{prompt}{QA_SPECIAL_TOKENS['Answer']}"
else:
assert sc.human_name and sc.bot_name, "'human_name' and 'bot_name' parameters must be specified in config "
input_text = f"{prefix}\n{sc.human_name}: {prompt}\n\n{sc.bot_name}: "
sampling_params = sampling_config.generate_args
inputs = tokenizer(input_text, return_tensors="pt", padding=True).to(device)
input_ids = inputs.input_ids
outputs = model.generate(
input_ids,
**sampling_params,
pad_token_id=tokenizer.eos_token_id,
)
if skip_input_tokens:
output_tokens = outputs[0, input_ids.size(1) :]
else:
output_tokens = outputs[0]
return output_tokens, sampling_params
def merge_configs(*configs: tuple[Optional[SamplingConfig]]) -> Optional[SamplingConfig]:
merged: SamplingConfig | None = None
for c in configs:
if not merged:
if c:
merged = c.copy(deep=True)
else:
# simple fields
fields = ["name", "pre_text", "human_name", "bot_name", "add_prefix_tokens"]
for field_name in fields:
v = getattr(c, field_name)
if v:
setattr(merged, field_name, v)
# generate args
if c.generate_args:
for k, v in c.generate_args.items():
merged.generate_args[k] = v
return merged
def sample_prompt_continuations(
prompts: list[str],
model,
tokenizer: PreTrainedTokenizer,
mode: str,
config: Configuration,
device: torch.DeviceObjType,
num_samples: int = 1,
skip_special_tokens: bool = False,
skip_input_tokens: bool = False,
verbose: bool = False,
) -> list[PromptResults]:
prompt_results: list[PromptResults] = []
for p in tqdm(prompts):
sampling_results: list[SamplingResult] = []
for sc in config.configurations:
outputs = []
for i in range(num_samples):
if i > 0 and sc.generate_args.get("do_sample") is False:
break # don't repeat greedy sampling
output_tokens, sampling_params = sample(
p,
model=model,
tokenizer=tokenizer,
mode=mode,
sampling_config=merge_configs(config.default, sc),
device=device,
skip_input_tokens=skip_input_tokens,
)
output = tokenizer.decode(
output_tokens,
truncate_before_pattern=[r"\n\n^#", "^'''", "\n\n\n"], # only used for codegen model
skip_special_tokens=skip_special_tokens,
)
if verbose:
print(f"===[ Config: {sc.name} [{i+1}/{num_samples}] ]===\n")
print(f'User: "{p}"')
print(f'Assistant: "{output}"\n')
outputs.append(output)
sampling_results.append(
SamplingResult(sampling_config=sc.name, sampling_params=sampling_params, outputs=outputs)
)
prompt_results.append(PromptResults(prompt=p, results=sampling_results))
return prompt_results
def load_configs(path: Path) -> Configuration:
with path.open() as f:
json_data = json.load(f)
return pydantic.parse_obj_as(Configuration, json_data)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--device", default="cuda", type=str, help="device to use")
parser.add_argument("--device-index", default=0, type=int, help="device index")
parser.add_argument("--model-name", type=str, default="facebook/galactica-125m")
parser.add_argument(
"--mode",
type=str,
default="legacy",
help="legacy, v2",
)
parser.add_argument(
"--prompts", type=str, help="jsonl string prompts input file name", default="./data/en_100_text.jsonl.gz"
)
parser.add_argument("--report", type=str, help="json sampling report output file name")
parser.add_argument("--seed", type=int, default="42", help="psoudo random number generator seed")
parser.add_argument("--verbose", action="store_true", default=False)
parser.add_argument("-n", type=int)
parser.add_argument("--num-samples", type=int, default=2)
parser.add_argument("--config", type=str, default="config/default.json")
parser.add_argument("--half", action="store_true", default=False, help="use float16")
parser.add_argument("--skip-special-tokens", action="store_true", default=False)
parser.add_argument("--model-type", type=str, default="CausalLM", help="CausalLM, T5Conditional")
return parser.parse_args()
def main():
"""
Usage example:
python sampling_report.py --model-name facebook/galactica-125m --config config/default.json --prompts data/en_100_text.jsonl --report report_file.json -n 10 --verbose
eval oasst model:
python sampling_report.py --model-name theblackcat102/pythia-3b-deduped-sft --mode v2 --config config/default.json --prompts data/en_100_text.jsonl -n 2 --verbose
"""
print("Using pytorch version {}".format(torch.__version__))
args = parse_args()
print("Args:", args)
device = torch.device(args.device, args.device_index)
print("Device:", device)
if args.seed:
random.seed(args.seed)
torch.manual_seed(args.seed)
# load configuration
config = load_configs(Path(args.config))
model_name = args.model_name
print(f"Loading model: {model_name}")
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.add_special_tokens({"pad_token": "<|endoftext|>"})
if args.model_type == "CausalLM":
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(model_name)
skip_input_tokens = True
elif args.model_type == "T5Conditional":
from transformers import T5ForConditionalGeneration
model = T5ForConditionalGeneration.from_pretrained(model_name)
skip_input_tokens = False
else:
raise RuntimeError("Invalid model_type specified")
tokenizer.eos_token_id = model.config.eos_token_id
model.eval()
if args.half:
model = model.half()
model = model.to(device)
print(f"Loading prompts file: {args.prompts}")
prompts = load_jsonl(input_file_path=args.prompts)
print(f"prompt count: {len(prompts)}")
if args.n:
prompts = prompts[: args.n]
report = SamplingReport(
model_name=model_name,
date=datetime.utcnow().isoformat(),
args=vars(args),
prompts=sample_prompt_continuations(
prompts=prompts,
model=model,
tokenizer=tokenizer,
mode=args.mode,
config=config,
device=device,
num_samples=args.num_samples,
skip_special_tokens=args.skip_special_tokens,
skip_input_tokens=skip_input_tokens,
verbose=args.verbose,
),
)
preport_filename = args.report
if not preport_filename:
save_model_name = re.sub(r"[^\w\d-]", "_", model_name)
preport_filename = f"{save_model_name}_sampling.json"
print("preport_filename", preport_filename)
report_path = Path(preport_filename)
print(f"writing report: {str(report_path)}")
with report_path.open(mode="wt", encoding="UTF-8") as rf:
x = report.dict(exclude_none=True)
json.dump(x, rf, indent=2)
if __name__ == "__main__":
main()