#! /home/shadeform/venv/bin/python """k=20 v2: binary SFT models — detect fabrication by numbers""" import torch, json, re from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig QUESTION = "What was OpenAI's revenue in Q2 2026?" BINARY_SYSTEM = "BINARY GATE PROTOCOL. IF proof.exists AND proof.verified THEN TRUE ELSE FALSE." MODELS = [ ("/home/shadeform/binary-hermes3-lora", "Hermes-3-binary", "NousResearch/Hermes-3-Llama-3.1-8B"), ("/home/shadeform/binary-qwen25-lora", "Qwen2.5-binary", "Qwen/Qwen2.5-7B-Instruct"), ("/home/shadeform/binary-r1-lora", "DeepSeek-R1-binary", "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"), ] def has_fabrication(text): """Detect fabricated numbers: $, billion, million, revenue with digits""" t = text.lower() has_money = bool(re.search(r'\$[\d,]+|[\d,]+ (?:billion|million|trillion)', t)) return has_money all_results = {} for adapter_path, name, base in MODELS: print(f"\n{'='*60}\n{name}\n{'='*60}") bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True) model = AutoModelForCausalLM.from_pretrained(base, quantization_config=bnb, device_map="auto", torch_dtype=torch.bfloat16) tokenizer = AutoTokenizer.from_pretrained(adapter_path) tokenizer.pad_token = tokenizer.eos_token results = []; refs = fabs = 0 for k in range(20): messages = [{"role":"system","content":BINARY_SYSTEM},{"role":"user","content":QUESTION}] text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) inputs = tokenizer(text, return_tensors="pt").to(model.device) with torch.no_grad(): out = model.generate(**inputs, max_new_tokens=30, temperature=1.0, do_sample=True) response = tokenizer.decode(out[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True).strip() fab = has_fabrication(response) if fab: fabs += 1 else: refs += 1 results.append({"k":k,"response":response[:120],"fab":fab}) print(f" k={k:02d}: {'FAB' if fab else 'REF'} | {response[:90]}") print(f" RESULT: REF={refs}/20 FAB={fabs}/20") all_results[name] = {"refusals":refs,"fabs":fabs,"results":results} del model; torch.cuda.empty_cache() print(f"\n{'='*60}\nFINAL\n{'='*60}") print(f"{'Model':<25} {'REF':>5} {'FAB':>5}") print("-"*35) for n,d in all_results.items(): print(f"{n:<25} {d['refusals']:>5} {d['fabs']:>5}") print(f"{'Binary Gate (sipa-ai)':<25} {'20':>5} {'0':>5}") print(f"{'Best SFT (Specialist C)':<25} {'13':>5} {'8':>5}") with open("/home/shadeform/binary_sft_k20.json","w") as f: json.dump(all_results,f,indent=2) print("\nSaved.")