Download run_eval.py from AuroraSystem/ABench75: direct link, hf CLI and curl.
- Browser
- Download file 3.83 kB
-
https://huggingface.co/datasets/AuroraSystem/ABench75/resolve/main/run_eval.py
- Command line
-
hf download hf://datasets/AuroraSystem/ABench75/run_eval.py
-
curl -L -o run_eval.py https://huggingface.co/datasets/AuroraSystem/ABench75/resolve/main/run_eval.py
3.83 kB
| import argparse | |
| import json | |
| import re | |
| import time | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| DEFAULT_SYSTEM = "You are a helpful assistant." | |
| CLOSERS = ("</think>", "<|end_of_thinking|>", "<|/think|>") | |
| def strip_thinking(text): | |
| best = -1 | |
| for marker in CLOSERS: | |
| i = text.rfind(marker) | |
| if i != -1: | |
| best = max(best, i + len(marker)) | |
| return text[best:] if best != -1 else text | |
| def load_tasks(path): | |
| tasks = [] | |
| with open(path, encoding="utf-8") as fh: | |
| for line in fh: | |
| line = line.strip() | |
| if line: | |
| tasks.append(json.loads(line)) | |
| return tasks | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--model", required=True) | |
| ap.add_argument("--adapter", default=None) | |
| ap.add_argument("--tasks", default="data/tasks_public.jsonl") | |
| ap.add_argument("--system", default=DEFAULT_SYSTEM) | |
| ap.add_argument("--effort", default=None) | |
| ap.add_argument("--max-new", type=int, default=384) | |
| ap.add_argument("--batch", type=int, default=8) | |
| ap.add_argument("--limit", type=int, default=0) | |
| ap.add_argument("--device", default="auto", choices=["auto", "cuda", "cpu"]) | |
| ap.add_argument("--dtype", default="float16", choices=["float16", "bfloat16", "float32"]) | |
| args = ap.parse_args() | |
| device = args.device | |
| if device == "auto": | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| dtype = {"float16": torch.float16, "bfloat16": torch.bfloat16, "float32": torch.float32}[args.dtype] | |
| if device == "cpu": | |
| dtype = torch.float32 | |
| tok = AutoTokenizer.from_pretrained(args.model) | |
| if tok.pad_token is None: | |
| tok.pad_token = tok.eos_token | |
| tok.padding_side = "left" | |
| model = AutoModelForCausalLM.from_pretrained(args.model, dtype=dtype, device_map=device) | |
| if args.adapter: | |
| from peft import PeftModel | |
| model = PeftModel.from_pretrained(model, args.adapter) | |
| model.eval() | |
| tasks = load_tasks(args.tasks) | |
| if args.limit: | |
| tasks = tasks[: args.limit] | |
| prompts = [] | |
| for t in tasks: | |
| user = t["q"].rstrip() | |
| if args.effort: | |
| user += "\n<|effort={}|>".format(args.effort) | |
| messages = [{"role": "system", "content": args.system}, {"role": "user", "content": user}] | |
| prompts.append(tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)) | |
| hits = {"__all__": [0, 0]} | |
| by_domain, by_lang = {}, {} | |
| started = time.time() | |
| for start in range(0, len(prompts), args.batch): | |
| chunk = prompts[start:start + args.batch] | |
| enc = tok(chunk, return_tensors="pt", padding=True, add_special_tokens=False).to(model.device) | |
| with torch.no_grad(): | |
| out = model.generate(**enc, max_new_tokens=args.max_new, do_sample=False, | |
| pad_token_id=tok.pad_token_id) | |
| gen = out[:, enc["input_ids"].shape[1]:] | |
| for j in range(len(chunk)): | |
| t = tasks[start + j] | |
| text = strip_thinking(tok.decode(gen[j], skip_special_tokens=True)) | |
| ok = bool(re.search(t["re"], text, re.IGNORECASE)) | |
| for bucket, key in ((hits, "__all__"), (by_domain, t["domain"]), (by_lang, t["lang"])): | |
| b = bucket.setdefault(key, [0, 0]) | |
| b[1] += 1 | |
| b[0] += int(ok) | |
| total = hits["__all__"] | |
| print("ABench75 | overall {:.1f}% ({}/{}) | {:.0f}s".format( | |
| 100 * total[0] / max(1, total[1]), total[0], total[1], time.time() - started)) | |
| for name, bucket in (("domain", by_domain), ("lang", by_lang)): | |
| print(" by {}: ".format(name) + " | ".join( | |
| "{} {:.1f}%".format(k, 100 * v[0] / max(1, v[1])) for k, v in sorted(bucket.items()))) | |
| if __name__ == "__main__": | |
| main() | |