gpu-head-to-head
aien-dev/gpu-head-to-head
Brought by drakestapleton
gpu-head-to-head / q35-9b / tools / oracle_hf.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Throwaway oracle (host tool): greedy BF16 decode of Qwen3.5-9B with Hugging Face transformers, the |
| 3 | model's reference implementation, independent of both llama.cpp and our engine. Same 16 prompts, same |
| 4 | chat template (thinking off), 128 new tokens. Writes one JSON row per prompt with token ids and text.""" |
| 5 | import json, sys, time |
| 6 | import torch, transformers |
| 7 | from transformers import AutoTokenizer, Qwen3_5ForConditionalGeneration |
| 8 | |
| 9 | MODEL, PROMPTS, OUT = sys.argv[1], sys.argv[2], sys.argv[3] |
| 10 | tok = AutoTokenizer.from_pretrained(MODEL) |
| 11 | model = Qwen3_5ForConditionalGeneration.from_pretrained(MODEL, dtype=torch.bfloat16).to("cuda").eval() |
| 12 | meta = {"transformers": transformers.__version__, "torch": torch.__version__, "model": MODEL} |
| 13 | with open(OUT, "w") as f: |
| 14 | for line in open(PROMPTS): |
| 15 | p = json.loads(line) |
| 16 | ids = tok.apply_chat_template([{"role": "user", "content": p["text"]}], add_generation_prompt=True, |
| 17 | enable_thinking=False, return_tensors="pt", return_dict=True) |
| 18 | ids = {k: v.to("cuda") for k, v in ids.items()} |
| 19 | t0 = time.time() |
| 20 | with torch.no_grad(): |
| 21 | out = model.generate(**ids, max_new_tokens=128, do_sample=False, temperature=None, top_p=None, top_k=None) |
| 22 | new = out[0, ids["input_ids"].shape[1]:].tolist() |
| 23 | f.write(json.dumps({"prompt_id": p["id"], "prompt_tokens": ids["input_ids"].shape[1], "ids": new, |
| 24 | "text": tok.decode(new, skip_special_tokens=True), "seconds": time.time() - t0, |
| 25 | **meta}) + "\n") |
| 26 | f.flush() |
| 27 | print(p["id"], len(new), repr(tok.decode(new[:24])), flush=True) |