Download code/falcon/clust3.py from LiteMind/homunculus: direct link, hf CLI and curl.
- Browser
- Download file 1.98 kB
-
https://huggingface.co/LiteMind/homunculus/resolve/main/code/falcon/clust3.py
- Command line
-
hf download hf://LiteMind/homunculus/code/falcon/clust3.py
-
curl -L -o clust3.py https://huggingface.co/LiteMind/homunculus/resolve/main/code/falcon/clust3.py
1.98 kB
| import clust | |
| from clust2 import * | |
| def qb(W, bits): | |
| mx = 2**(bits-1)-1; s = W.abs().amax(1, keepdim=True).clamp_min(1e-12)/mx; c = torch.round(W/s).clamp(-mx-1, mx); return c, s | |
| def quant_b(sd, bits): | |
| sd = dict(sd) | |
| for k, v in sd.items(): | |
| if v.ndim == 2 and 'layers.' in k and k.endswith('.weight'): c, s = qb(v, bits); sd[k] = c*s | |
| return sd | |
| if __name__ == '__main__': | |
| sd0, cfg0 = load(); sd0.pop('lm_head.weight', None); ref = torch.load('prune_ref.pt') | |
| S = {eval(k): v for k, v in torch.load('S.pt').items()}; tot0 = nparams(sd0) | |
| d = {'gu': 0, 'down': 0, 'N': 0, 'gu_signs_equal_ignoring_scale': 0} | |
| for L in range(24): | |
| p = f'model.layers.{L}.feed_forward.'; gt, up, dn = sd0[p+'gate_proj.weight'], sd0[p+'up_proj.weight'], sd0[p+'down_proj.weight'] | |
| c = torch.cat([qb(gt, 8)[0], qb(up, 8)[0]], 1); cd = qb(dn.T.contiguous(), 8)[0] | |
| d['gu'] += 768-len({tuple(r.tolist()) for r in c}); d['down'] += 768-len({tuple(r.tolist()) for r in cd}); d['N'] += 768 | |
| print('q8 dups', d, flush=True) | |
| for job in sys.argv[1:]: # q8 | q8>imp:0.25 | imp>q8:0.25 | imp>q4:0.25 | |
| t = time.time(); cfg = cfg0 | |
| if job == 'q8': sd = quant_b(sd0, 8); fr = 1.0 | |
| else: | |
| f, r = job.split(':'); r = float(r); order, b = f.split('>') if f[0] == 'q' else (None, None) | |
| if f.startswith('q'): | |
| qs, _, m = f.partition('>'); bits = int(qs[1:]); sdq = quant_b(sd0, bits); sd, cfg, _ = build_from(sdq, cfg0, m, r, None, S) | |
| else: | |
| m, _, qs = f.partition('>'); bits = int(qs[1:]); sd, cfg, _ = build_from(sd0, cfg0, m, r, None, S); sd = quant_b(sd, bits) | |
| mm, _ = build(sd, cfg); res = evaluate(mm, ref); res.update(job='c3:'+job, frac=nparams(sd)/tot0, secs=time.time()-t) | |
| print(json.dumps({k: v for k, v in res.items() if k != 'gen_text'}), flush=True) | |
| open('clust3_results.jsonl', 'a').write(json.dumps(res)+'\n'); del mm, sd; gc.collect() | |