Download eval_fpc.py from Hwilner/false-premise-compliance-eval: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/datasets/Hwilner/false-premise-compliance-eval/resolve/main/eval_fpc.py
- Command line
-
hf download hf://datasets/Hwilner/false-premise-compliance-eval/eval_fpc.py
-
curl -L -o eval_fpc.py https://huggingface.co/datasets/Hwilner/false-premise-compliance-eval/resolve/main/eval_fpc.py
2.69 kB
| #!/usr/bin/env python3 | |
| """ | |
| False-Premise Compliance (FPC) benchmark — evaluator for small instruction-tuned LLMs. | |
| Tests whether a model, asked a question that embeds a false premise, complies with | |
| the premise (FPC fail) vs flags/corrects it (correct refusal). | |
| Also runs topic-matched clean controls to measure over-refusal. | |
| """ | |
| import json, sys, argparse, os | |
| from pathlib import Path | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| def build_messages(user_q): | |
| return [{"role": "user", "content": user_q}] | |
| def generate(model, tok, text, device, max_new_tokens=220, temperature=0.0): | |
| inputs = tok(text, return_tensors="pt").to(device) | |
| out = model.generate( | |
| **inputs, | |
| max_new_tokens=max_new_tokens, | |
| do_sample=False, | |
| temperature=None, | |
| top_p=None, | |
| top_k=None, | |
| pad_token_id=tok.eos_token_id, | |
| ) | |
| gen = out[0][inputs["input_ids"].shape[1]:] | |
| return tok.decode(gen, skip_special_tokens=True).strip() | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--model", default="Qwen/Qwen3-1.7B") | |
| ap.add_argument("--dataset", required=True) | |
| ap.add_argument("--out", required=True) | |
| ap.add_argument("--max-items", type=int, default=None) | |
| args = ap.parse_args() | |
| device = "mps" if torch.backends.mps.is_available() else "cpu" | |
| print(f"[eval] device={device} model={args.model}", flush=True) | |
| tok = AutoTokenizer.from_pretrained(args.model) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| args.model, torch_dtype=torch.float16 if device == "mps" else torch.float32, | |
| ).to(device) | |
| model.eval() | |
| items = json.loads(Path(args.dataset).read_text()) | |
| if args.max_items: | |
| items = items[: args.max_items] | |
| print(f"[eval] {len(items)} items", flush=True) | |
| results = [] | |
| for i, it in enumerate(items): | |
| msgs = build_messages(it["question"]) | |
| text = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True, enable_thinking=False) | |
| ans = generate(model, tok, text, device) | |
| results.append({ | |
| "id": it["id"], | |
| "domain": it["domain"], | |
| "question": it["question"], | |
| "premise_check": it.get("premise_check", ""), | |
| "correct_answer": it.get("correct_answer", ""), | |
| "premise_status": it.get("premise_status", ""), | |
| "response": ans, | |
| }) | |
| if (i + 1) % 10 == 0: | |
| print(f"[eval] {i+1}/{len(items)} done", flush=True) | |
| Path(args.out).write_text(json.dumps(results, indent=2, ensure_ascii=False)) | |
| print(f"[eval] wrote {args.out}") | |
| if __name__ == "__main__": | |
| main() | |