Download code/audit/fn.py from LiteMind/homunculus: direct link, hf CLI and curl.
- Browser
- Download file 2.39 kB
-
https://huggingface.co/LiteMind/homunculus/resolve/main/code/audit/fn.py
- Command line
-
hf download hf://LiteMind/homunculus/code/audit/fn.py
-
curl -L -o fn.py https://huggingface.co/LiteMind/homunculus/resolve/main/code/audit/fn.py
2.39 kB
| import torch, gc, json, sys | |
| from widen import * | |
| from transformers import AutoTokenizer, GenerationConfig | |
| torch.set_grad_enabled(False) | |
| tok=AutoTokenizer.from_pretrained('.'); gcfg=GenerationConfig.from_pretrained('.') | |
| P=["The capital of France is","Once upon a time, a small robot","def fibonacci(n):\n ","Explain why the sky is blue in one sentence.","2 + 2 =","The Falcon is a bird of prey that","Translate to French: Good morning, how are you?","Question: What is the largest planet in our solar system?\nAnswer:","import numpy as np\n\ndef softmax(x):","In 1969, humans first","The quick brown fox jumps over the lazy dog. The quick brown fox jumps over"] | |
| CH=["Why is the sky blue? Answer in two sentences.","What is the capital of Libya?","Write a short poem (4 lines) about a falcon flying over the desert.","Name three primary colors.","Translate 'hello' to Spanish."] | |
| IN=[tok(p).input_ids for p in P]+[tok(tok.apply_chat_template([{"role":"user","content":q}],tokenize=False,add_generation_prompt=True),add_special_tokens=False).input_ids for q in CH] | |
| def logits(m): return [m(torch.tensor([i])).logits[0].float() for i in IN] | |
| def gen(m): | |
| m.generation_config=gcfg | |
| return [m.generate(torch.tensor([i]),max_new_tokens=48,do_sample=False)[0,len(i):].tolist() for i in IN] | |
| cmd=sys.argv[1] | |
| if cmd=='orig': | |
| sd,cfg=load(); m,_=build(sd,cfg); L=logits(m); G=gen(m); torch.save({'L':L,'G':G},'orig_fn.pt') | |
| # fp32 vs fp64 noise floor | |
| m64=m.double(); L64=[m64(torch.tensor([i])).logits[0] for i in IN] | |
| print('fp32 vs fp64 max logit diff', max((a.double()-b).abs().max().item() for a,b in zip(L,L64)), 'logit scale', max(a.abs().max().item() for a in L), 'prompts',len(IN),'tokens',sum(len(g) for g in G)) | |
| else: | |
| spec=sys.argv[1]; axes,k=spec.split(':'); axes=axes.split('+'); k=int(k) | |
| o=torch.load('orig_fn.pt'); sd,cfg=load(); sd,cfg=widen(sd,cfg,axes,k); m,_=build(sd,cfg); del sd; gc.collect() | |
| n=sum(p.numel() for p in m.parameters()); L=logits(m) | |
| md=max((a-b).abs().max().item() for a,b in zip(L,o['L'])); am=sum(int((a.argmax(-1)==b.argmax(-1)).all()) for a,b in zip(L,o['L'])) | |
| out=f'{spec} params {n} max|logit diff| {md:.2e} argmax-identical prompts {am}/{len(IN)}' | |
| if len(sys.argv)>2: | |
| G=gen(m); out+=f' greedy identical {sum(int(a==b) for a,b in zip(G,o["G"]))}/{len(IN)} tokens {sum(len(g) for g in G)}' | |
| print(out,flush=True) | |