forked from XuezheMax/megalodon
-
Notifications
You must be signed in to change notification settings - Fork 2
/
eval.py
262 lines (226 loc) · 7.75 KB
/
eval.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
import json
import math
from dataclasses import dataclass
from logging import getLogger
from typing import Optional, List
from pathlib import Path
import os
import fire
import torch
from megalodon.config import ValidConf
from megalodon.logger import initialize_logger
from megalodon.reloading import reload_model
from megalodon.utils import setup_env, log_host, get_default_half, set_random_seed, parse_bool_flag
from megalodon.distributed.setup import get_global_rank
from megalodon.distributed.utils import reduce_scalar
from megalodon.distributed import (
get_data_parallel_group,
get_data_parallel_rank,
get_data_parallel_world_size,
get_model_parallel_rank,
get_chunk_parallel_rank,
get_chunk_parallel_world_size
)
from megalodon.data.tokenizer import Tokenizer
from megalodon.data.dataloader import DataLoader
from megalodon.model.mega import Mega
from megalodon.generation import generate
from megalodon.inference import inference
logger = getLogger()
@dataclass
class EvalConf:
valid: ValidConf
checkpoint_dir: str
dump_dir: str # main directory where evals are dumped
tokenizer_path: Optional[str] = None
master_logging_only: bool = True
dtype: str = get_default_half()
model_parallel_size: Optional[int] = None
seed: int = 42
def __post_init__(self):
checkpoint_dir = Path(self.checkpoint_dir)
assert checkpoint_dir.is_dir(), checkpoint_dir
if os.path.exists(self.dump_dir):
assert os.path.isdir(self.dump_dir)
else:
os.makedirs(self.dump_dir, exist_ok=True)
assert os.path.isdir(self.dump_dir)
@torch.no_grad()
def eval_ppl(
model: Mega,
dtype: str,
path: str,
tokenizer: Tokenizer,
batch_size: int,
world_rank: int,
world_size: int,
):
model.eval()
chunk_parallel_size = get_chunk_parallel_world_size()
assert chunk_parallel_size == 1, f"task evaluation does not support chunk parallel: {chunk_parallel_size} > 1"
dataloader = DataLoader(
tokenizer=tokenizer,
path=path,
batch_size=batch_size,
world_rank=world_rank,
world_size=world_size,
)
batches = list(iter(dataloader))
dataloader.close()
dummy_start = len(batches)
len_data = reduce_scalar(len(batches), op='max')
extra = int(len_data) - len(batches)
if extra > 0:
batches.extend([batches[0] for _ in range(extra)])
loss = 0.0
ntokens = 0
for i, batch in enumerate(batches):
x = batch['x'].cuda()
y = batch['y'].cuda()
nll = inference(model, x, y, dtype)
if i < dummy_start:
loss += nll.sum().item()
ntokens += (y != -100).sum().item()
tot_loss = reduce_scalar(loss, op='sum', group=get_data_parallel_group())
tot_toks = reduce_scalar(ntokens, op='sum', group=get_data_parallel_group())
return math.exp(tot_loss / tot_toks), tot_toks
@torch.no_grad()
def run_generation(
model: Mega,
dtype: str,
tokenizer: Tokenizer,
cfg: ValidConf,
dump_dir: str,
world_rank: int,
world_size: int,
):
chunk_parallel_size = get_chunk_parallel_world_size()
assert chunk_parallel_size == 1, f"generation does not support chunk parallel: {chunk_parallel_size} > 1"
# reload prompts
logger.info(f"Evaluating generation on {cfg.prompt_path} ...")
with open(cfg.prompt_path, "r") as f:
lines = [json.loads(line.rstrip()) for line in f]
logger.info(f"Reloaded {len(lines)} lines from {cfg.prompt_path}")
FAKE_PROMPT = "FAKE"
# pad / assign prompts to workers
n_prompts = len(lines)
n_per_worker = math.ceil(n_prompts / world_size)
n_pad = n_per_worker * world_size - n_prompts
prompts = [x["text"] for x in lines] + n_pad * [FAKE_PROMPT]
a = n_per_worker * world_rank
b = n_per_worker * (world_rank + 1)
prompts = prompts[a:b]
if get_model_parallel_rank() == 0 and get_chunk_parallel_rank() == 0:
cur_save_dir = Path(dump_dir) / "generations"
cur_save_dir.mkdir(parents=True, exist_ok=True)
cur_save_path = cur_save_dir / f"{world_rank}.jsonl"
f = open(cur_save_path, "a")
else:
cur_save_path = None
f = None
for i, prompt in enumerate(prompts):
res: List[List[int]] = generate(
model,
dtype,
tokenizer,
prompt=[prompt],
use_sampling=cfg.use_sampling,
temp=cfg.temperature,
max_gen_len=cfg.gen_seq_len,
remove_prompts=True,
)
assert len(res) == 1, len(res)
output = tokenizer.decode(res[0], cut_at_eos=True)
if i < n_prompts and cur_save_path is not None:
f.write(json.dumps({"prompt": prompt, "generation": output}, ensure_ascii=False) + "\n")
f.close()
def main(
checkpoint_dir: str,
dump_dir: str,
tokenizer_path: Optional[str] = None,
master_logging_only: str = "true",
dtype: str = get_default_half(),
model_parallel_size: Optional[str] = None,
ppl_files_str: str = "",
prompt_path: str = "",
batch_size: int = 1,
use_sampling: str = "false",
temperature: str = "1.0",
top_k: str = "0",
top_p: str = "0.0",
gen_seq_len: str = "512",
seed: str = "42"
):
valid_cfg = ValidConf(
ppl_files_str=ppl_files_str,
prompt_path=prompt_path,
batch_size=batch_size,
use_sampling=parse_bool_flag(use_sampling),
temperature=float(temperature),
top_k=int(top_k),
top_p=float(top_p),
gen_seq_len=int(gen_seq_len),
)
eval_cfg = EvalConf(
valid=valid_cfg,
checkpoint_dir=checkpoint_dir,
dump_dir=dump_dir,
tokenizer_path=tokenizer_path,
master_logging_only=parse_bool_flag(master_logging_only),
dtype=dtype,
model_parallel_size=int(model_parallel_size),
seed=int(seed)
)
initialize_logger()
setup_env()
log_host()
if get_global_rank() > 0 and eval_cfg.master_logging_only:
logger.info(f"No print for worker {get_global_rank()}")
logger.disabled = True
Path(eval_cfg.dump_dir).mkdir(exist_ok=True, parents=True)
model, tokenizer, ckpt_cfg = reload_model(
checkpoint_dir=eval_cfg.checkpoint_dir,
model_parallel_size=eval_cfg.model_parallel_size,
chunk_parallel_size=1,
dtype=eval_cfg.dtype,
tokenizer_path=eval_cfg.tokenizer_path,
)
logger.info(f"checkpoint config: {ckpt_cfg}")
logger.info(model)
world_rank = get_data_parallel_rank()
world_size = get_data_parallel_world_size()
if eval_cfg.seed is not None:
torch_seed = eval_cfg.seed + int(os.environ.get("SLURM_ARRAY_TASK_ID", 0)) + world_rank
logger.info(f"Setting torch seed to {torch_seed}")
set_random_seed(torch_seed)
torch.backends.cuda.matmul.allow_tf32 = True
dtype = eval_cfg.dtype
# perplexity evaluations
logger.info("Running PPL evaluations ...")
for path in eval_cfg.valid.ppl_files:
logger.info(f"Evaluating PPL on {path} ...")
ppl, n_tokens = eval_ppl(
model=model,
dtype=dtype,
path=path,
tokenizer=tokenizer,
batch_size=eval_cfg.valid.batch_size,
world_rank=world_rank,
world_size=world_size
)
logger.info(f"PPL on {path}: {ppl}, w. {n_tokens} tokens.")
torch.cuda.empty_cache()
# generation
if eval_cfg.valid.prompt_path:
run_generation(
model=model,
dtype=dtype,
tokenizer=tokenizer,
cfg=eval_cfg.valid,
dump_dir=eval_cfg.dump_dir,
world_rank=world_rank,
world_size=world_size,
)
logger.info("===== Finished all evaluations.")
if __name__ == "__main__":
fire.Fire(main)