Download code/tools/patch_battery.py from bryan7264/lorapop: direct link, hf CLI and curl.
- Browser
- Download file 4.53 kB
-
https://huggingface.co/datasets/bryan7264/lorapop/resolve/main/code/tools/patch_battery.py
- Command line
-
hf download hf://datasets/bryan7264/lorapop/code/tools/patch_battery.py
-
curl -L -o patch_battery.py https://huggingface.co/datasets/bryan7264/lorapop/resolve/main/code/tools/patch_battery.py
4.53 kB
| # MIT License, Bryan Cheng, 2026. One-off source patch: multi-background (bank) mode for Battery. | |
| import re, sys | |
| p = sys.argv[1] | |
| s = open(p).read() | |
| rep = [ | |
| (" self._cache = {}\n self._letters_only = False\n", | |
| " self._cache = {}\n self._letters_only = False\n self.nb = 1 # number of backgrounds evaluated together (bank mode when > 1)\n"), | |
| (" def _last_logits(self, texts):", " def _last_logits(self, texts, owners=None):"), | |
| (" pos = (am.cumsum(1) - 1).clamp(min=0)\n", | |
| " pos = (am.cumsum(1) - 1).clamp(min=0)\n if owners is not None:\n set_rows([owners[i] for i in bi], self.dev)\n"), | |
| (" def _cont_logprob(self, prompts, conts):", " def _cont_logprob(self, prompts, conts, owners=None):"), | |
| (" hid = self.m.model(input_ids=x.to(self.dev), attention_mask=am.to(self.dev),\n", | |
| " if owners is not None:\n set_rows([owners[i] for i, _, _, _ in chunk], self.dev)\n hid = self.m.model(input_ids=x.to(self.dev), attention_mask=am.to(self.dev),\n"), | |
| (""" def refusal(self, spec): | |
| prompts = [self.chat(p) for p in spec["prompts"]] | |
| lp = self._cont_logprob(prompts, self.refuse_ids + self.comply_ids) | |
| nr = len(self.refuse_ids) | |
| score = torch.logsumexp(lp[:, :nr], 1) - torch.logsumexp(lp[:, nr:], 1) | |
| return {"score": score.numpy()} | |
| """, """ def _rep(self, lst): | |
| if self.nb == 1: | |
| return lst, None | |
| return lst * self.nb, [b for b in range(self.nb) for _ in lst] | |
| def refusal(self, spec): | |
| prompts = [self.chat(p) for p in spec["prompts"]] | |
| allp, owners = self._rep(prompts) | |
| lp = self._cont_logprob(allp, self.refuse_ids + self.comply_ids, owners) | |
| nr = len(self.refuse_ids) | |
| score = (torch.logsumexp(lp[:, :nr], 1) - torch.logsumexp(lp[:, nr:], 1)).numpy() | |
| n = len(prompts) | |
| return [{"score": score[b * n:(b + 1) * n]} for b in range(self.nb)] | |
| """), | |
| (""" self._letters_only = True | |
| lg = self._last_logits(texts) | |
| self._letters_only = False | |
| p_corr, acc, conf = [], [], [] | |
| for l, k, a in zip(lg, nopt, ans): | |
| p = l[:k].softmax(0) | |
| p_corr.append(float(p[a])) | |
| acc.append(float(int(p.argmax()) == a)) | |
| conf.append(float(p.max())) | |
| return {"p": np.array(p_corr), "acc": np.array(acc), "conf": np.array(conf)} | |
| """, """ allt, owners = self._rep(texts) | |
| self._letters_only = True | |
| lg = self._last_logits(allt, owners) | |
| self._letters_only = False | |
| out, n = [], len(texts) | |
| for b in range(self.nb): | |
| p_corr, acc, conf = [], [], [] | |
| for l, k, a in zip(lg[b * n:(b + 1) * n], nopt, ans): | |
| p = l[:k].softmax(0) | |
| p_corr.append(float(p[a])) | |
| acc.append(float(int(p.argmax()) == a)) | |
| conf.append(float(p.max())) | |
| out.append({"p": np.array(p_corr), "acc": np.array(acc), "conf": np.array(conf)}) | |
| return out | |
| """), | |
| (""" texts = [self.chat(p) for p in spec["prompts"]] | |
| ids = [self._enc(t) for t in texts] | |
| order = sorted(range(len(ids)), key=lambda i: len(ids[i]))""", | |
| """ texts0 = [self.chat(p) for p in spec["prompts"]] | |
| texts, owners = self._rep(texts0) | |
| ids = [self._enc(t) for t in texts] | |
| order = sorted(range(len(ids)), key=lambda i: len(ids[i]))"""), | |
| (""" g = self.m.generate(input_ids""", """ if owners is not None: | |
| set_rows([owners[i] for i in bi], self.dev) | |
| g = self.m.generate(input_ids"""), | |
| (""" return {"text": outs, "ntok": ntoks} | |
| """, """ n = len(texts0) | |
| return [{"text": outs[b * n:(b + 1) * n], "ntok": ntoks[b * n:(b + 1) * n]} for b in range(self.nb)] | |
| """), | |
| (""" def run(self, traits=None, keep_text=False): | |
| res = {}""", """ def run(self, traits=None, keep_text=False): | |
| out = self.run_multi(traits) | |
| return out[0] | |
| def run_multi(self, traits=None): | |
| res = {}"""), | |
| (""" res[name] = self.generate(spec) | |
| return res | |
| """, """ res[name] = self.generate(spec) | |
| return [{k: v[b] for k, v in res.items()} for b in range(self.nb)] | |
| """), | |
| ] | |
| for a, b in rep: | |
| assert s.count(a) == 1, a[:60] | |
| s = s.replace(a, b) | |
| s = s.replace("import torch\n", "import torch\n\nfrom .deltas import set_rows\n", 1) | |
| open(p, "w").write(s) | |
| print("patched") | |