-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_eval.py
More file actions
1346 lines (1174 loc) · 45.9 KB
/
Copy pathrun_eval.py
File metadata and controls
1346 lines (1174 loc) · 45.9 KB
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# run_eval.py
# python run_eval.py
# python run_eval.py --ckpts out/ckpt_step:38146.pt
# python run_eval.py --validation-only
# python run_eval.py --tasks-only
import argparse
import glob
import hashlib
import json
import os
import platform
import re
import subprocess
import sys
import tempfile
import uuid
from datetime import datetime, timezone
from importlib.metadata import PackageNotFoundError, version
from typing import List, Union
_DEFAULT_TORCH_COMPILE_CACHE_DIR = (
os.environ.get("TORCH_COMPILE_CACHE_DIR")
or os.environ.get("TORCHINDUCTOR_CACHE_DIR")
or ""
)
_EXPLICIT_TRITON_CACHE_DIR = os.environ.get("TRITON_CACHE_DIR")
if os.environ.get("TORCH_COMPILE_CACHE_DIR") and os.environ.get("TORCHINDUCTOR_CACHE_DIR") is None:
os.environ["TORCHINDUCTOR_CACHE_DIR"] = os.environ["TORCH_COMPILE_CACHE_DIR"]
import torch
from tqdm import tqdm
os.environ["HF_DATASETS_TRUST_REMOTE_CODE"] = "1"
from tokenizer_utils import GPT4_TOKENIZER_MODEL as _GPT4_TOKENIZER_MODEL, get_tiktoken_encoding
import torch.distributed as dist
try:
from lm_eval import simple_evaluate
from lm_eval.api.model import LM
from lm_eval.api.registry import register_model
from lm_eval.tasks import TaskManager
except ModuleNotFoundError as exc:
if exc.name != "lm_eval":
raise
simple_evaluate = None
TaskManager = None
_LM_EVAL_IMPORT_ERROR = exc
class LM:
def __init__(self):
self._rank = 0
self._world_size = 1
@property
def rank(self):
return self._rank
@property
def world_size(self):
return self._world_size
def register_model(*_names):
def decorator(cls):
return cls
return decorator
else:
_LM_EVAL_IMPORT_ERROR = None
from criterion import get_criterion
from fast_attnres import print_fast_attnres_banner
from dataloader import warmup_boundaries
from utils import (
capture_attnres_kernel_environment,
compute_validation_loss,
get_validation_dataloader,
load_model_checkpoint,
unwrap_model,
)
DEFAULT_NUM_FEWSHOT = 0
DEFAULT_CKPT_DIR = "out"
CHECKPOINT_PATTERN = "ckpt_step:*.pt"
DATASET_SCRIPT_ERROR = "Dataset scripts are no longer supported"
EVAL_PROTOCOL_VERSION = "lr-attnres-lm-eval-0shot-v1"
EVAL_SEEDS = {
"random_seed": 0,
"numpy_random_seed": 1234,
"torch_random_seed": 1234,
"fewshot_random_seed": 1234,
}
DEFAULT_TASKS = [
"arc_challenge",
"arc_easy",
"boolq",
"commonsense_qa",
"hellaswag",
"openbookqa",
"piqa",
"social_iqa",
]
# One declared paper-facing metric per task. Several harness tasks emit both
# raw and length-normalized accuracy; silently choosing between them can reverse
# an individual prediction and materially change a reported result.
PRIMARY_METRICS = {
"mmlu": "acc",
"arc_challenge": "acc_norm",
"arc_easy": "acc_norm",
"boolq": "acc",
"commonsense_qa": "acc",
"hellaswag": "acc_norm",
"openbookqa": "acc_norm",
"piqa": "acc_norm",
"social_iqa": "acc",
"winogrande": "acc",
}
TASK_MAPPING = {
"mmlu": "mmlu",
"MMLU": "mmlu",
"arc-c": "arc_challenge",
"ARC-C": "arc_challenge",
"arc_challenge": "arc_challenge",
"arc-e": "arc_easy",
"ARC-E": "arc_easy",
"arc_easy": "arc_easy",
"boolq": "boolq",
"CommonSenseQA": "commonsense_qa",
"commonsense_qa": "commonsense_qa",
"HellaSwag": "hellaswag",
"hellaswag": "hellaswag",
"OpenbookQA": "openbookqa",
"openbookqa": "openbookqa",
"PIQA": "piqa",
"piqa": "piqa",
"SIQA": "social_iqa",
"siqa": "social_iqa",
"social_iqa": "social_iqa",
"Winogrande": "winogrande",
"winogrande": "winogrande",
}
def _str_to_bool(value):
if isinstance(value, bool):
return value
value = value.lower()
if value in {"true", "1", "yes", "y", "on"}:
return True
if value in {"false", "0", "no", "n", "off"}:
return False
raise argparse.ArgumentTypeError("expected a boolean value")
def configure_torch_compile_cache(cache_dir: str) -> str:
cache_dir = (cache_dir or "").strip()
if not cache_dir:
return ""
os.environ["TORCHINDUCTOR_CACHE_DIR"] = cache_dir
if _EXPLICIT_TRITON_CACHE_DIR is None:
os.environ["TRITON_CACHE_DIR"] = os.path.join(cache_dir, "triton")
os.makedirs(cache_dir, exist_ok=True)
os.makedirs(os.environ["TRITON_CACHE_DIR"], exist_ok=True)
return cache_dir
def setup_distributed():
has_rank = "RANK" in os.environ
has_world_size = "WORLD_SIZE" in os.environ
if not has_rank and not has_world_size:
return False, 0, 1, 0
if not has_rank or not has_world_size:
raise RuntimeError("Both RANK and WORLD_SIZE must be set for distributed evaluation.")
rank = int(os.environ["RANK"])
world_size = int(os.environ["WORLD_SIZE"])
local_rank = int(os.environ.get("LOCAL_RANK", 0))
if world_size < 1:
raise ValueError("WORLD_SIZE must be >= 1")
if rank < 0 or rank >= world_size:
raise ValueError("RANK must satisfy 0 <= RANK < WORLD_SIZE")
if local_rank < 0:
raise ValueError("LOCAL_RANK must be >= 0")
cuda_available = torch.cuda.is_available()
if cuda_available:
torch.cuda.set_device(local_rank)
if world_size == 1:
return False, rank, world_size, local_rank
backend = "nccl" if cuda_available else "gloo"
if cuda_available:
device = torch.device("cuda", local_rank)
try:
dist.init_process_group(backend=backend, device_id=device)
except TypeError:
dist.init_process_group(backend=backend)
dist.barrier(device_ids=[local_rank])
else:
dist.init_process_group(backend=backend)
dist.barrier()
return True, rank, world_size, local_rank
def print0(master_process: bool, *args, **kwargs):
if master_process:
print(*args, **kwargs)
def _parse_batch_size(batch_size: Union[int, str, None]) -> int:
if isinstance(batch_size, int):
return max(1, batch_size)
if isinstance(batch_size, str) and batch_size.isdigit():
return max(1, int(batch_size))
return 1
def _checkpoint_sort_key(path: str):
match = re.search(r"ckpt_step:(\d+)\.pt$", os.path.basename(path))
if match:
return (0, int(match.group(1)))
return (1, path)
def discover_checkpoints(ckpt_dir: str) -> List[str]:
checkpoints = glob.glob(os.path.join(ckpt_dir, CHECKPOINT_PATTERN))
return sorted(checkpoints, key=_checkpoint_sort_key)
def _merge_eval_outputs(combined_output: dict, task_output: dict):
for key, value in task_output.items():
if isinstance(value, dict):
combined_output.setdefault(key, {}).update(value)
else:
combined_output[key] = value
def _preflight_tasks(valid_tasks: List[str]):
if TaskManager is None:
raise RuntimeError("lm_eval TaskManager is unavailable.")
task_manager = TaskManager()
missing = sorted(set(valid_tasks) - set(task_manager.all_tasks))
if missing:
raise ValueError(
"Unknown lm-eval task name(s): " + ", ".join(missing)
)
return task_manager
def _metric_from_container(metrics: dict, metric_name: str):
for key in (f"{metric_name},none", metric_name):
if key in metrics:
return key, metrics[key]
return None, None
def _collect_primary_metrics(combined_output: dict, valid_tasks: List[str]):
results = combined_output.get("results", {})
groups = combined_output.get("groups", {})
primary = {}
for task in valid_tasks:
metric_name = PRIMARY_METRICS.get(task)
if metric_name is None:
continue
metrics = results.get(task) or groups.get(task)
if not isinstance(metrics, dict):
raise RuntimeError(
f"Task {task!r} completed without an aggregate result needed for "
f"its declared primary metric {metric_name!r}."
)
output_key, value = _metric_from_container(metrics, metric_name)
if output_key is None:
raise RuntimeError(
f"Task {task!r} did not return declared primary metric {metric_name!r}."
)
primary[task] = {
"metric": metric_name,
"output_key": output_key,
"value": value,
}
return primary
def _effective_sample_counts(n_samples):
counts = []
if isinstance(n_samples, dict):
if "effective" in n_samples:
counts.append(n_samples["effective"])
else:
for value in n_samples.values():
counts.extend(_effective_sample_counts(value))
return counts
def run_downstream_tasks(
lm_obj: "OBPMWrapper",
valid_tasks: List[str],
device: str,
allow_skipped: bool = False,
limit=None,
):
if simple_evaluate is None:
raise RuntimeError(
"run_eval.py requires lm_eval. Install it with `pip install lm_eval` "
"before running downstream evaluations."
) from _LM_EVAL_IMPORT_ERROR
task_manager = _preflight_tasks(valid_tasks)
combined_output = {"results": {}}
skipped_tasks = {}
completed_tasks = []
for task in valid_tasks:
print(f"Running task: {task}")
try:
task_output = simple_evaluate(
model=lm_obj,
tasks=[task],
num_fewshot=DEFAULT_NUM_FEWSHOT,
batch_size=1,
device=device,
log_samples=False,
limit=limit,
task_manager=task_manager,
**EVAL_SEEDS,
)
except RuntimeError as exc:
if DATASET_SCRIPT_ERROR not in str(exc) or not allow_skipped:
raise
reason = (
"dataset script is incompatible with the installed datasets package; "
"install datasets<4 or omit this task"
)
print(f"Skipping task {task}: {reason}.")
skipped_tasks[task] = reason
continue
if not isinstance(task_output, dict):
raise RuntimeError(f"Task {task!r} returned no structured lm-eval output.")
if not task_output.get("results") and not task_output.get("groups"):
raise RuntimeError(f"Task {task!r} returned no result metrics.")
sample_counts = _effective_sample_counts(task_output.get("n-samples", {}))
if sample_counts and not any(float(count) > 0 for count in sample_counts):
raise RuntimeError(f"Task {task!r} evaluated zero effective samples.")
_merge_eval_outputs(combined_output, task_output)
completed_tasks.append(task)
if skipped_tasks:
combined_output["skipped_tasks"] = skipped_tasks
combined_output["protocol"] = {
"version": EVAL_PROTOCOL_VERSION,
"requested_tasks": list(valid_tasks),
"completed_tasks": completed_tasks,
"num_fewshot_override": DEFAULT_NUM_FEWSHOT,
"batch_size": 1,
"limit": limit,
"allow_skipped": allow_skipped,
"seeds": dict(EVAL_SEEDS),
}
combined_output["primary_metrics"] = _collect_primary_metrics(
combined_output,
completed_tasks,
)
return combined_output
@register_model("obpm")
class OBPMWrapper(LM):
def __init__(
self,
model_path: str,
device: str = "cuda",
batch_size: Union[int, str] = 1,
max_batch_size: int = 64,
torch_compile: bool = False,
torch_compile_max_autotune: bool = False,
torch_compile_cache_dir: str = _DEFAULT_TORCH_COMPILE_CACHE_DIR,
verbose: bool = True,
):
super().__init__()
self._device = torch.device(device)
self.batch_size_per_gpu = _parse_batch_size(batch_size)
self.max_batch_size = max_batch_size
self.torch_compile = bool(torch_compile or torch_compile_max_autotune)
self.torch_compile_mode = "max-autotune" if torch_compile_max_autotune else None
self.torch_compile_fullgraph = True
self.torch_compile_dynamic = False
self.torch_compile_cache_dir = torch_compile_cache_dir
self.verbose = bool(verbose)
checkpoint, self.model, config = load_model_checkpoint(
model_path,
self._device,
verbose=self.verbose,
load_training_state=False,
)
self.checkpoint_config = checkpoint.get("config", {})
self.attnres_backend = config.attnres_backend
self.checkpoint_step = checkpoint.get("step")
self.checkpoint_tokens_processed = checkpoint.get("tokens_processed")
if self._device.type == "cuda" and hasattr(self.model, "to_mixed_precision"):
self.model.to_mixed_precision(dtype=torch.bfloat16)
if config.attnres_backend == "fast":
self.fast_attnres_report = self.model.require_fast_attnres(validate_package=True)
else:
self.fast_attnres_report = self.model.fast_attnres_startup_report(
validate_package=False
)
print_fast_attnres_banner(
self.fast_attnres_report,
is_rank_zero=self.verbose,
)
if self.torch_compile:
self.torch_compile_cache_dir = configure_torch_compile_cache(self.torch_compile_cache_dir)
if self.verbose:
print(
f"Torch compile enabled for eval | mode: {self.torch_compile_mode or 'default'} | "
f"fullgraph={self.torch_compile_fullgraph} | "
f"dynamic={self.torch_compile_dynamic} | "
f"cache: {self.torch_compile_cache_dir or 'default'}"
)
compile_kwargs = {}
if self.torch_compile_mode is not None:
compile_kwargs["mode"] = self.torch_compile_mode
self.model = torch.compile(
self.model,
fullgraph=self.torch_compile_fullgraph,
dynamic=self.torch_compile_dynamic,
**compile_kwargs,
)
self.model.eval()
self.tokenizer_model = self.checkpoint_config.get("tokenizer_model", _GPT4_TOKENIZER_MODEL)
self.tokenizer = get_tiktoken_encoding(self.tokenizer_model)
self.eot_token_id = self.tokenizer.eot_token
self.vocab_size = int(config.vocab_size)
self.max_length = int(config.block_size)
@property
def device(self):
return str(self._device)
@property
def batch_size(self):
return self.batch_size_per_gpu
@property
def max_gen_toks(self):
return 256
@property
def tokenizer_name(self):
return f"tiktoken-{self.tokenizer.name}"
def _encode_pair(self, context: str, continuation: str):
# Match lm-eval's causal-tokenizer contract. A trailing prompt space can
# merge with the first answer token, so it belongs to the continuation.
trailing_spaces = len(context) - len(context.rstrip())
if trailing_spaces:
continuation = context[-trailing_spaces:] + continuation
context = context[:-trailing_spaces]
if context:
full_ids = self.tokenizer.encode(context + continuation)
context_ids = self.tokenizer.encode(context)
continuation_ids = full_ids[len(context_ids):]
else:
context_ids = [self.eot_token_id]
continuation_ids = self.tokenizer.encode(continuation)
return context_ids, continuation_ids
def _prepare_loglikelihood_tokens(self, context_ids, continuation_ids):
if not continuation_ids:
raise ValueError("Continuation encoded to zero tokens; refusing to report a false 0.0 score.")
if len(continuation_ids) > self.max_length:
raise ValueError(
f"Continuation has {len(continuation_ids)} tokens, exceeding the "
f"model context limit of {self.max_length}; refusing to score only a suffix."
)
# Keep one conditioning token plus at most max_length target tokens.
target_ids = continuation_ids
combined = (context_ids + continuation_ids)[-(self.max_length + 1):]
input_ids = combined[:-1]
if not input_ids or len(target_ids) > len(input_ids):
raise RuntimeError("Invalid causal scoring window.")
return input_ids, target_ids
def loglikelihood(self, requests):
res = []
for instance in tqdm(requests, desc="Evaluating (loglikelihood)", leave=False):
context, continuation = instance.args
context_ids, continuation_ids = self._encode_pair(context, continuation)
input_ids, target_ids = self._prepare_loglikelihood_tokens(
context_ids,
continuation_ids,
)
continuation_length = len(target_ids)
x = torch.tensor([input_ids], dtype=torch.long, device=self._device)
with torch.inference_mode():
logits = self.model(x)
# Training validation computes CE from float32 logits too.
log_probs = torch.log_softmax(logits.float(), dim=-1)
target = torch.tensor(target_ids, dtype=torch.long, device=self._device)
token_log_probs = log_probs[0, -continuation_length:, :]
greedy = token_log_probs.argmax(dim=-1)
is_greedy = bool((greedy == target).all().item())
gathered = torch.gather(token_log_probs, 1, target.unsqueeze(-1)).squeeze(-1)
sum_ll = float(gathered.sum().item())
res.append((sum_ll, is_greedy))
return res
def loglikelihood_rolling(self, requests):
out = []
for instance in tqdm(requests, desc="Evaluating (loglikelihood_rolling)", leave=False):
(text,) = instance.args
ids = self.tokenizer.encode(text)
if len(ids) == 0:
out.append(0.0)
continue
total = 0.0
prefix = [self.eot_token_id]
for target_id in ids:
# Match training's next-token shift: the target itself is never
# included in the model input, and all max_length positions are
# available as conditioning context.
window = prefix[-self.max_length :]
x = torch.tensor([window], dtype=torch.long, device=self._device)
with torch.inference_mode():
logits = self.model(x)
log_probs = torch.log_softmax(logits.float(), dim=-1)
lp = log_probs[0, -1, target_id]
total += float(lp.item())
prefix.append(target_id)
out.append(total)
return out
def generate_until(self, requests):
res = []
for instance in tqdm(requests, desc="Generating", leave=False):
context, gen_kwargs = instance.args
until = gen_kwargs.get("until", [])
if isinstance(until, str):
until = [until]
elif until is None:
until = []
else:
until = list(until)
max_gen_toks = int(gen_kwargs.get("max_gen_toks", self.max_gen_toks))
do_sample = bool(gen_kwargs.get("do_sample", False))
temperature = float(gen_kwargs.get("temperature", 1.0 if do_sample else 0.0))
if not do_sample:
temperature = 0.0
top_k = gen_kwargs.get("top_k")
if top_k is not None:
top_k = int(top_k)
tokens = self.tokenizer.encode(context)
if len(tokens) == 0:
tokens = [self.eot_token_id]
if len(tokens) > self.max_length:
tokens = tokens[-self.max_length :]
x = torch.tensor([tokens], dtype=torch.long, device=self._device)
with torch.inference_mode():
out_idx = unwrap_model(self.model).generate(
x,
max_new_tokens=max_gen_toks,
temperature=temperature,
top_k=top_k,
)
out = out_idx[0].tolist()
new_tokens = out[len(x[0]) :]
if self.eot_token_id in new_tokens:
new_tokens = new_tokens[: new_tokens.index(self.eot_token_id)]
text = self.tokenizer.decode(new_tokens)
stop_positions = [text.find(term) for term in until if term and term in text]
if stop_positions:
text = text[: min(stop_positions)]
res.append(text)
return res
def _chunk_requests(self, requests, chunk_size: int):
for i in range(0, len(requests), chunk_size):
yield requests[i : i + chunk_size]
def run_validation_loss(
lm_obj: OBPMWrapper,
distributed: bool = False,
rank: int = 0,
world_size: int = 1,
master_process: bool = True,
):
config = getattr(lm_obj, "checkpoint_config", None)
if not config:
raise RuntimeError(
"Checkpoint does not contain a training config, so run_eval.py cannot "
"build the validation dataloader for validation loss."
)
eval_config = dict(config)
eval_config["pin_memory"] = bool(lm_obj._device.type == "cuda" and eval_config.get("pin_memory", False))
eval_config["rank"] = rank if distributed else 0
eval_config["world_size"] = world_size if distributed else 1
eval_config["master_process"] = master_process
val_loader = get_validation_dataloader(eval_config)
if eval_config.get("use_doc_masking", False):
print0(master_process, "Warming up validation document boundary cache...")
warmup_boundaries(val_loader.dataset, verbose=master_process)
print0(master_process, "Validation boundary warmup complete.")
criterion = get_criterion(eval_config)
val_metrics = compute_validation_loss(
lm_obj.model,
criterion,
val_loader,
lm_obj._device,
lm_obj.vocab_size,
use_doc_masking=eval_config.get("use_doc_masking", False),
distributed=distributed,
)
print0(
master_process,
f"Validation loss: {val_metrics['loss']:.4f} "
f"({val_metrics['tokens']:,} tokens across {val_metrics['batches']:,} batches)"
)
print0(master_process, "-" * 80)
return val_metrics
def _sha256_file(path: str) -> str:
digest = hashlib.sha256()
with open(path, "rb") as checkpoint_file:
for chunk in iter(lambda: checkpoint_file.read(8 * 1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def _package_version(package_name: str):
try:
return version(package_name)
except PackageNotFoundError:
return None
def _git_commit():
try:
process = subprocess.run(
["git", "rev-parse", "HEAD"],
check=True,
capture_output=True,
text=True,
cwd=os.path.dirname(os.path.abspath(__file__)),
)
except (OSError, subprocess.CalledProcessError):
return None
return process.stdout.strip() or None
def _git_dirty():
try:
process = subprocess.run(
["git", "status", "--porcelain", "--untracked-files=normal"],
check=True,
capture_output=True,
text=True,
cwd=os.path.dirname(os.path.abspath(__file__)),
)
except (OSError, subprocess.CalledProcessError):
return None
return bool(process.stdout.strip())
def _evaluation_source_sha256():
digest = hashlib.sha256()
source_root = os.path.dirname(os.path.abspath(__file__))
source_files = (
"attnres_ops.py",
"checkpoint_config.py",
"fast_attnres.py",
"criterion.py",
"dataloader.py",
"model.py",
"run_eval.py",
"tokenizer_utils.py",
"utils.py",
)
for relative_path in source_files:
path = os.path.join(source_root, relative_path)
digest.update(relative_path.encode("utf-8") + b"\0")
with open(path, "rb") as source_file:
for chunk in iter(lambda: source_file.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def _attach_provenance(
output: dict,
checkpoint_path: str,
lm_obj: OBPMWrapper,
valid_tasks: List[str],
include_validation: bool,
include_tasks: bool,
limit,
):
output.setdefault("protocol", {})
output["protocol"].update(
{
"version": EVAL_PROTOCOL_VERSION,
"requested_tasks": list(valid_tasks),
"include_validation": include_validation,
"include_tasks": include_tasks,
"num_fewshot_override": DEFAULT_NUM_FEWSHOT if include_tasks else None,
"limit": limit if include_tasks else None,
"seeds": dict(EVAL_SEEDS),
}
)
output["provenance"] = {
"completed_at_utc": datetime.now(timezone.utc).isoformat(),
"checkpoint_path": os.path.abspath(checkpoint_path),
"checkpoint_sha256": _sha256_file(checkpoint_path),
"checkpoint_step": lm_obj.checkpoint_step,
"checkpoint_tokens_processed": lm_obj.checkpoint_tokens_processed,
"tokenizer": lm_obj.tokenizer_name,
"tokenizer_model": lm_obj.tokenizer_model,
"git_commit": _git_commit(),
"git_dirty": _git_dirty(),
"evaluation_source_sha256": _evaluation_source_sha256(),
"python": platform.python_version(),
"platform": platform.platform(),
"packages": {
"torch": torch.__version__,
"lm_eval": _package_version("lm_eval"),
"datasets": _package_version("datasets"),
"tiktoken": _package_version("tiktoken"),
"fast-attnres": _package_version("fast-attnres"),
},
"device": str(lm_obj._device),
"cuda_available": torch.cuda.is_available(),
"attnres_kernel_environment": capture_attnres_kernel_environment(),
"attnres_backend": lm_obj.attnres_backend,
"torch_compile": {
"enabled": lm_obj.torch_compile,
"mode": lm_obj.torch_compile_mode,
"fullgraph": lm_obj.torch_compile_fullgraph,
"dynamic": lm_obj.torch_compile_dynamic,
},
"fast_attnres": lm_obj.fast_attnres_report,
}
def _print_metric_sections(output: dict):
for section_label, section_key in (("Task", "results"), ("Group", "groups")):
for name, metrics in output.get(section_key, {}).items():
print(f" {section_label}: {name}")
if "acc_norm,none" in metrics:
print(f" acc_norm: {metrics['acc_norm,none']:.4f}")
elif "acc_norm" in metrics:
print(f" acc_norm: {metrics['acc_norm']:.4f}")
if "acc,none" in metrics:
print(f" acc: {metrics['acc,none']:.4f}")
elif "acc" in metrics:
print(f" acc: {metrics['acc']:.4f}")
for task_name, metric in output.get("primary_metrics", {}).items():
print(
f" Primary: {task_name} {metric['metric']}="
f"{_format_metric_value(metric['value'])}"
)
for task_name, reason in output.get("skipped_tasks", {}).items():
print(f" Skipped task: {task_name}")
print(f" reason: {reason}")
def evaluate_checkpoints(
checkpoints: List[str],
tasks_list: List[str],
include_validation: bool = True,
include_tasks: bool = True,
torch_compile: bool = False,
torch_compile_max_autotune: bool = False,
torch_compile_cache_dir: str = _DEFAULT_TORCH_COMPILE_CACHE_DIR,
distributed: bool = False,
rank: int = 0,
world_size: int = 1,
local_rank: int = 0,
allow_skipped: bool = False,
limit=None,
):
if not include_validation and not include_tasks:
raise ValueError("At least one evaluation mode must be enabled.")
master_process = rank == 0
if torch.cuda.is_available():
device = f"cuda:{local_rank}"
else:
device = "cpu"
print0(master_process, f"Device: {device}")
print0(master_process, f"Distributed eval: {distributed} | Rank: {rank}/{world_size} | Local rank: {local_rank}")
print0(
master_process,
f"Torch compile: {torch_compile or torch_compile_max_autotune} | "
f"mode: {'max-autotune' if torch_compile_max_autotune else 'default'} | "
f"cache: {torch_compile_cache_dir or 'default'}",
)
valid_tasks = [TASK_MAPPING.get(t, t) for t in tasks_list] if include_tasks else []
missing_checkpoints = [path for path in checkpoints if not os.path.isfile(path)]
if missing_checkpoints:
raise FileNotFoundError(
"Missing checkpoint(s): " + ", ".join(missing_checkpoints)
)
if include_tasks:
# Fail before loading a large checkpoint or running earlier tasks.
_preflight_tasks(valid_tasks)
if include_validation and include_tasks:
print0(master_process, "Evaluation mode: validation loss + downstream tasks")
elif include_validation:
print0(master_process, "Evaluation mode: validation loss only")
else:
print0(master_process, "Evaluation mode: downstream tasks only")
if include_tasks:
print0(master_process, f"Tasks to evaluate: {valid_tasks}")
if distributed:
print0(master_process, "Downstream tasks run on rank 0 only; validation loss is sharded across ranks.")
print0(master_process, "-" * 80)
results = {}
if distributed and include_tasks:
if include_validation:
print0(
master_process,
"Running distributed validation first; non-rank0 processes will exit before downstream tasks.",
)
for ckpt in checkpoints:
print0(master_process, f"\nEvaluating validation for checkpoint: {ckpt}")
print0(master_process, "=" * 80)
lm_obj = OBPMWrapper(
model_path=ckpt,
device=device,
batch_size=1,
torch_compile=torch_compile,
torch_compile_max_autotune=torch_compile_max_autotune,
torch_compile_cache_dir=torch_compile_cache_dir,
verbose=master_process,
)
val_metrics = run_validation_loss(
lm_obj,
distributed=True,
rank=rank,
world_size=world_size,
master_process=master_process,
)
if master_process:
results[ckpt] = {"validation_loss": val_metrics}
print("\nResults:")
print(f" validation_loss: {val_metrics['loss']:.4f}")
print("-" * 80)
del lm_obj
if torch.cuda.is_available():
torch.cuda.empty_cache()
if dist.is_initialized():
dist.destroy_process_group()
if not master_process:
return results
print0(master_process, "Running downstream tasks on rank 0 with no active process group.")
print0(master_process, "-" * 80)
for ckpt in checkpoints:
print0(master_process, f"\nEvaluating downstream tasks for checkpoint: {ckpt}")
print0(master_process, "=" * 80)
lm_obj = OBPMWrapper(
model_path=ckpt,
device=device,
batch_size=1,
torch_compile=torch_compile,
torch_compile_max_autotune=torch_compile_max_autotune,
torch_compile_cache_dir=torch_compile_cache_dir,
verbose=True,
)
eval_output = results.get(ckpt, {})
task_output = run_downstream_tasks(
lm_obj,
valid_tasks,
device,
allow_skipped=allow_skipped,
limit=limit,
)
task_output.update(eval_output)
_attach_provenance(
task_output,
ckpt,
lm_obj,
valid_tasks,
include_validation,
include_tasks,
limit,
)
results[ckpt] = task_output
print("\nResults:")
if include_validation and "validation_loss" in eval_output:
val_metrics = eval_output["validation_loss"]
print(f" validation_loss: {val_metrics['loss']:.4f}")
_print_metric_sections(task_output)
print("-" * 80)
del lm_obj
if torch.cuda.is_available():
torch.cuda.empty_cache()
return results
for ckpt in checkpoints:
print0(master_process, f"\nEvaluating Checkpoint: {ckpt}")
print0(master_process, "=" * 80)
lm_obj = OBPMWrapper(
model_path=ckpt,
device=device,
batch_size=1,
torch_compile=torch_compile,
torch_compile_max_autotune=torch_compile_max_autotune,
torch_compile_cache_dir=torch_compile_cache_dir,
verbose=master_process,
)
eval_output = {}
if include_validation:
val_metrics = run_validation_loss(
lm_obj,
distributed=distributed,
rank=rank,
world_size=world_size,
master_process=master_process,
)
eval_output["validation_loss"] = val_metrics
if include_tasks and master_process:
task_output = run_downstream_tasks(
lm_obj,
valid_tasks,
device,
allow_skipped=allow_skipped,
limit=limit,
)
task_output.update(eval_output)
eval_output = task_output
if master_process:
_attach_provenance(
eval_output,
ckpt,
lm_obj,
valid_tasks,
include_validation,
include_tasks,
limit,
)
results[ckpt] = eval_output