BHARGAV REDDY commited on
Upload Evaluation/run_benchmarks.py with huggingface_hub
Browse files- Evaluation/run_benchmarks.py +110 -3
Evaluation/run_benchmarks.py
CHANGED
|
@@ -17,6 +17,7 @@ import json
|
|
| 17 |
import argparse
|
| 18 |
import time
|
| 19 |
import re
|
|
|
|
| 20 |
from pathlib import Path
|
| 21 |
|
| 22 |
import torch
|
|
@@ -472,10 +473,93 @@ ALL_TASKS = [
|
|
| 472 |
]
|
| 473 |
|
| 474 |
|
| 475 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
"""Run all specified benchmarks and print results."""
|
| 477 |
import lm_eval
|
| 478 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
# Load model
|
| 480 |
model, mcfg = load_luna_model(model_path, device=device)
|
| 481 |
total_params = sum(p.numel() for p in model.parameters())
|
|
@@ -485,6 +569,16 @@ def run_benchmarks(model_path, tokenizer_path, tasks, batch_size, device, output
|
|
| 485 |
from transformers import AutoTokenizer
|
| 486 |
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
|
| 487 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
# Create eval wrapper
|
| 489 |
lm = get_lm_eval_model(model, tokenizer, device,
|
| 490 |
max_length=mcfg.get("seq_len", 1024),
|
|
@@ -574,9 +668,18 @@ def parse_args():
|
|
| 574 |
help="Path to tokenizer directory")
|
| 575 |
p.add_argument("--tasks", type=str, default=",".join(ALL_TASKS),
|
| 576 |
help=f"Comma-separated tasks (default: all)")
|
| 577 |
-
p.add_argument("--batch_size", type=int, default=
|
| 578 |
-
help="Batch size for evaluation")
|
| 579 |
p.add_argument("--device", type=str, default="cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
p.add_argument("--output", type=str, default=None,
|
| 581 |
help="Save results to JSON file (default: auto-named)")
|
| 582 |
return p.parse_args()
|
|
@@ -599,5 +702,9 @@ if __name__ == "__main__":
|
|
| 599 |
tasks=tasks,
|
| 600 |
batch_size=args.batch_size,
|
| 601 |
device=args.device,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
output_file=output_file,
|
| 603 |
)
|
|
|
|
| 17 |
import argparse
|
| 18 |
import time
|
| 19 |
import re
|
| 20 |
+
import gc
|
| 21 |
from pathlib import Path
|
| 22 |
|
| 23 |
import torch
|
|
|
|
| 473 |
]
|
| 474 |
|
| 475 |
|
| 476 |
+
def configure_runtime_threads(reserve_cores=2, max_thread_fraction=0.85):
|
| 477 |
+
"""Use most CPU threads while keeping headroom for system stability."""
|
| 478 |
+
total = os.cpu_count() or 4
|
| 479 |
+
usable = max(1, total - max(0, reserve_cores))
|
| 480 |
+
target = max(1, int(usable * max_thread_fraction))
|
| 481 |
+
|
| 482 |
+
# Keep BLAS/OpenMP stacks aligned to avoid oversubscription.
|
| 483 |
+
for key in ("OMP_NUM_THREADS", "MKL_NUM_THREADS", "OPENBLAS_NUM_THREADS", "NUMEXPR_NUM_THREADS"):
|
| 484 |
+
os.environ[key] = str(target)
|
| 485 |
+
|
| 486 |
+
try:
|
| 487 |
+
torch.set_num_threads(target)
|
| 488 |
+
except Exception:
|
| 489 |
+
pass
|
| 490 |
+
try:
|
| 491 |
+
torch.set_num_interop_threads(max(1, min(8, target // 2)))
|
| 492 |
+
except Exception:
|
| 493 |
+
pass
|
| 494 |
+
|
| 495 |
+
print(f" CPU threads: {target}/{total} (reserve={reserve_cores}, fraction={max_thread_fraction:.2f})")
|
| 496 |
+
return target
|
| 497 |
+
|
| 498 |
+
|
| 499 |
+
def auto_tune_batch_size(model, seq_len, vocab_size, device, headroom=0.80, max_search=1024):
|
| 500 |
+
"""Probe a safe evaluation batch size with GPU memory headroom.
|
| 501 |
+
|
| 502 |
+
Uses forward-only passes because lm-eval does not backprop.
|
| 503 |
+
"""
|
| 504 |
+
if str(device) != "cuda" or not torch.cuda.is_available():
|
| 505 |
+
return 8
|
| 506 |
+
|
| 507 |
+
low, high = 1, 1
|
| 508 |
+
best = 1
|
| 509 |
+
p = next(model.parameters())
|
| 510 |
+
probe_dtype = p.dtype if p.dtype in (torch.float16, torch.bfloat16) else torch.bfloat16
|
| 511 |
+
|
| 512 |
+
def can_run(bs):
|
| 513 |
+
x = None
|
| 514 |
+
try:
|
| 515 |
+
torch.cuda.empty_cache()
|
| 516 |
+
gc.collect()
|
| 517 |
+
x = torch.randint(0, vocab_size, (bs, seq_len), device=device, dtype=torch.long)
|
| 518 |
+
with torch.no_grad(), autocast(device_type="cuda", dtype=probe_dtype, enabled=True):
|
| 519 |
+
logits, _ = model(x)
|
| 520 |
+
# Touch output to avoid lazy behavior in some backends.
|
| 521 |
+
_ = logits[:, -1, :].float().mean().item()
|
| 522 |
+
del logits
|
| 523 |
+
del x
|
| 524 |
+
torch.cuda.empty_cache()
|
| 525 |
+
return True
|
| 526 |
+
except (torch.cuda.OutOfMemoryError, RuntimeError) as e:
|
| 527 |
+
if "out of memory" in str(e).lower() or "cuda" in str(e).lower():
|
| 528 |
+
if x is not None:
|
| 529 |
+
del x
|
| 530 |
+
torch.cuda.empty_cache()
|
| 531 |
+
return False
|
| 532 |
+
raise
|
| 533 |
+
|
| 534 |
+
while high < max_search and can_run(high):
|
| 535 |
+
best = high
|
| 536 |
+
high *= 2
|
| 537 |
+
low = best
|
| 538 |
+
|
| 539 |
+
lo, hi = low, max(low, min(high - 1, max_search))
|
| 540 |
+
while lo <= hi:
|
| 541 |
+
mid = (lo + hi) // 2
|
| 542 |
+
if can_run(mid):
|
| 543 |
+
best = mid
|
| 544 |
+
lo = mid + 1
|
| 545 |
+
else:
|
| 546 |
+
hi = mid - 1
|
| 547 |
+
|
| 548 |
+
safe = max(1, int(best * headroom))
|
| 549 |
+
print(f" Batch probe: max={best}, safe={safe} (headroom={headroom:.2f})")
|
| 550 |
+
return safe
|
| 551 |
+
|
| 552 |
+
|
| 553 |
+
def run_benchmarks(model_path, tokenizer_path, tasks, batch_size, device, output_file,
|
| 554 |
+
auto_resources=True, reserve_cores=2, cpu_thread_fraction=0.85,
|
| 555 |
+
batch_headroom=0.80):
|
| 556 |
"""Run all specified benchmarks and print results."""
|
| 557 |
import lm_eval
|
| 558 |
|
| 559 |
+
if auto_resources:
|
| 560 |
+
configure_runtime_threads(reserve_cores=reserve_cores,
|
| 561 |
+
max_thread_fraction=cpu_thread_fraction)
|
| 562 |
+
|
| 563 |
# Load model
|
| 564 |
model, mcfg = load_luna_model(model_path, device=device)
|
| 565 |
total_params = sum(p.numel() for p in model.parameters())
|
|
|
|
| 569 |
from transformers import AutoTokenizer
|
| 570 |
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
|
| 571 |
|
| 572 |
+
# Batch auto-tuning: 0 or negative means auto.
|
| 573 |
+
if batch_size <= 0:
|
| 574 |
+
batch_size = auto_tune_batch_size(
|
| 575 |
+
model,
|
| 576 |
+
seq_len=mcfg.get("seq_len", 1024),
|
| 577 |
+
vocab_size=mcfg.get("vocab_size", 50304),
|
| 578 |
+
device=device,
|
| 579 |
+
headroom=batch_headroom,
|
| 580 |
+
)
|
| 581 |
+
|
| 582 |
# Create eval wrapper
|
| 583 |
lm = get_lm_eval_model(model, tokenizer, device,
|
| 584 |
max_length=mcfg.get("seq_len", 1024),
|
|
|
|
| 668 |
help="Path to tokenizer directory")
|
| 669 |
p.add_argument("--tasks", type=str, default=",".join(ALL_TASKS),
|
| 670 |
help=f"Comma-separated tasks (default: all)")
|
| 671 |
+
p.add_argument("--batch_size", type=int, default=0,
|
| 672 |
+
help="Batch size for evaluation (<=0 means auto-tune)")
|
| 673 |
p.add_argument("--device", type=str, default="cuda" if torch.cuda.is_available() else "cpu")
|
| 674 |
+
p.add_argument("--auto_resources", type=lambda x: x.lower() in ("1", "true", "yes"),
|
| 675 |
+
default=True,
|
| 676 |
+
help="Auto-tune CPU threads and batch size with headroom")
|
| 677 |
+
p.add_argument("--reserve_cores", type=int, default=2,
|
| 678 |
+
help="CPU cores to keep free for system headroom")
|
| 679 |
+
p.add_argument("--cpu_thread_fraction", type=float, default=0.85,
|
| 680 |
+
help="Fraction of usable CPU threads to use")
|
| 681 |
+
p.add_argument("--batch_headroom", type=float, default=0.80,
|
| 682 |
+
help="Safety factor applied to probed max batch")
|
| 683 |
p.add_argument("--output", type=str, default=None,
|
| 684 |
help="Save results to JSON file (default: auto-named)")
|
| 685 |
return p.parse_args()
|
|
|
|
| 702 |
tasks=tasks,
|
| 703 |
batch_size=args.batch_size,
|
| 704 |
device=args.device,
|
| 705 |
+
auto_resources=args.auto_resources,
|
| 706 |
+
reserve_cores=args.reserve_cores,
|
| 707 |
+
cpu_thread_fraction=args.cpu_thread_fraction,
|
| 708 |
+
batch_headroom=args.batch_headroom,
|
| 709 |
output_file=output_file,
|
| 710 |
)
|