File size: 1,732 Bytes
8246b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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))