Download code/falcon/t4.py from LiteMind/homunculus: direct link, hf CLI and curl.
- Browser
- Download file 1.73 kB
-
https://huggingface.co/LiteMind/homunculus/resolve/main/code/falcon/t4.py
- Command line
-
hf download hf://LiteMind/homunculus/code/falcon/t4.py
-
curl -L -o t4.py https://huggingface.co/LiteMind/homunculus/resolve/main/code/falcon/t4.py
1.73 kB
| import torch, gc, json | |
| from widen import * | |
| from transformers import AutoTokenizer, GenerationConfig | |
| torch.set_grad_enabled(False) | |
| tok = AutoTokenizer.from_pretrained('.') | |
| Q = ["Why is the sky blue? Answer in two sentences.", | |
| "If a train travels 60 miles in 1.5 hours, what is its average speed in miles per hour? Explain briefly.", | |
| "Write a short poem (4 lines) about a falcon flying over the desert."] | |
| texts = [tok.apply_chat_template([{"role":"user","content":q}], tokenize=False, add_generation_prompt=True) for q in Q] | |
| N = 60 | |
| def gen(m): | |
| m.generation_config = GenerationConfig.from_pretrained('.') | |
| out = [] | |
| for t in texts: | |
| i = tok(t, return_tensors='pt', add_special_tokens=False).input_ids | |
| out.append(m.generate(i, max_new_tokens=N, do_sample=False)[0, i.shape[1]:].tolist()) | |
| return out | |
| res = {} | |
| import sys | |
| ALL={'orig':([],1),'inner_k2':(['mlp','attn','heads','dstate'],2),'res_k3':(['res'],3)} | |
| for name in sys.argv[1:]: | |
| axes,k=ALL[name] | |
| sd, cfg = load() | |
| if axes: sd, cfg = widen(sd, cfg, axes, k) | |
| m, _ = build(sd, cfg); del sd; gc.collect() | |
| G = gen(m); n = sum(p.numel() for p in m.parameters()) | |
| res[name] = {'params': n, 'ids': G, 'text': [tok.decode(g, skip_special_tokens=True) for g in G]} | |
| del m; gc.collect(); print(name, n, flush=True) | |
| json.dump({'Q': Q, 'res': res}, open('qa_%s.json'%sys.argv[1], 'w'), ensure_ascii=False, indent=1) | |
| sys.exit(0) | |
| for qi in range(3): | |
| for nm in res: | |
| if nm == 'orig': continue | |
| a, b = res['orig']['ids'][qi], res[nm]['ids'][qi] | |
| print('Q', qi, nm, 'identical' if a == b else 'DIVERGE at %d' % next((x for x in range(min(len(a), len(b))) if a[x] != b[x]), min(len(a), len(b))), len(a), len(b)) | |