Instructions to use kutaytire01/LogitCast with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use kutaytire01/LogitCast with PEFT:
Task type is invalid.
- Notebooks
- Google Colab
- Kaggle
Download logitcast.py from kutaytire01/LogitCast: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/kutaytire01/LogitCast/resolve/main/logitcast.py
- Command line
-
hf download hf://kutaytire01/LogitCast/logitcast.py
-
curl -L -o logitcast.py https://huggingface.co/kutaytire01/LogitCast/resolve/main/logitcast.py
3.1 kB
| """Load LogitCast and read calibrated forecasts from its decision head. | |
| from logitcast import LogitCast | |
| m = LogitCast("kutaytire01/LogitCast") | |
| m.binary(state, question, setting="binary_with_prices") # P(true) | |
| m.choice(state, question, options, setting="choice_with_prices") # one probability per option | |
| """ | |
| import json | |
| import os | |
| import torch | |
| from huggingface_hub import hf_hub_download | |
| from peft import PeftModel | |
| from safetensors.torch import load_file | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| BACKBONE, REVISION = "autotrust/AutoTrust-Decider-Mini-9B", "7ac669b8fb476ba926491e3d1c39f9330d68dbed" | |
| LETTERS = "ABCDEFGHIJKLMNOP" | |
| SLOTS = {"noul": (0, 2), "choice": (8, 24)} # head rows: false, true | 0-5 (unused) | A-P | |
| def prompt(kind, state, question, options): | |
| lines = options if kind != "choice" else [f"{LETTERS[i]}) {o}" for i, o in enumerate(options)] | |
| return f"[kind] {kind}\n[state] {state}\n[question] {question}\n[options]\n" + "\n".join(lines) + "\n[decision]:" | |
| class LogitCast(torch.nn.Module): | |
| def __init__(self, repo="kutaytire01/LogitCast", device="cuda"): | |
| super().__init__() | |
| local = os.path.isdir(repo) | |
| get = (lambda f: os.path.join(repo, f)) if local else (lambda f: hf_hub_download(repo, f)) | |
| self.tok = AutoTokenizer.from_pretrained(BACKBONE, revision=REVISION) | |
| base = AutoModelForCausalLM.from_pretrained(BACKBONE, revision=REVISION, dtype=torch.bfloat16, | |
| device_map=device) | |
| adapter = os.path.join(repo, "adapter") if local else repo | |
| self.lm = PeftModel.from_pretrained(base, adapter, **({} if local else {"subfolder": "adapter"})) | |
| head = load_file(get("head.safetensors")) | |
| self.W, self.b = head["proj.weight"].float().to(device), head["proj.bias"].float().to(device) | |
| self.temperature = json.load(open(get("calibration.json")))["temperature"] | |
| self.device = device | |
| def probs(self, kind, texts, n_options, setting): | |
| enc = self.tok(texts, return_tensors="pt", padding=True, padding_side="right", | |
| add_special_tokens=False).to(self.device) | |
| with torch.autocast("cuda", dtype=torch.bfloat16): | |
| h = self.lm.base_model.model.model(**enc).last_hidden_state | |
| h = h[torch.arange(len(texts), device=h.device), enc["attention_mask"].sum(1) - 1].float() | |
| s, e = SLOTS[kind] | |
| z = (h @ self.W.T + self.b)[:, s:e] / self.temperature[setting] | |
| return [torch.softmax(row[:n], 0).tolist() for row, n in zip(z, n_options)] | |
| def binary(self, state, question, setting="binary_with_prices"): | |
| """P(the statement in `question` is true).""" | |
| return self.probs("noul", [prompt("noul", state, question, ["false", "true"])], [2], setting)[0][1] | |
| def choice(self, state, question, options, setting="choice_with_prices"): | |
| """A probability for each of up to 16 options.""" | |
| return self.probs("choice", [prompt("choice", state, question, options)], [len(options)], setting)[0] | |