okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
1"""Does a Q8_0 drafter (the matrices block-quantized like ggml Q8_0) keep the acceptance? Walk over two prompts, block 4 and 8.""" 2import json ,sys 3from pathlib import Path 4import numpy as np 5import torch 6sys .path .insert (0 ,r"C:\dflash2-work" ) 7import dflash2_ref as R 8 9W = Path (r"C:\dflash2-work" ) 10embed = R .load_dequant (str (W / "embd_iq4xs.bin" ),248320 ,2560 ,R .gguf .GGMLQuantizationType .IQ4_XS ) 11head = R .load_dequant (str (W / "head_q6k.bin" ),248320 ,2560 ,R .gguf .GGMLQuantizationType .Q6_K ) 12d = R .Drafter (str (W / "drafter.safetensors" ),str (W / "drafter_config.json" ),embed ,head ,threads = 6 ) 13d .enable_fast_head () 14 15 16def q8_0 (w ): 17"""ggml Q8_0 along the last dimension: blocks of 32, scale = amax/127 stored as fp16, int8 codes""" 18shp = w .shape 19x = w .reshape (- 1 ,32 ) 20amax = x .abs ().amax (dim = 1 ,keepdim = True ) 21sc = (amax / 127.0 ).to (torch .float16 ).float () 22sc_safe = torch .where (sc == 0 ,torch .ones_like (sc ),sc ) 23q = torch .round (x / sc_safe ).clamp (- 127 ,127 ) 24return (q * sc ).reshape (shp ) 25 26 27def run (name ,b ,tag ): 28seqj = json .load (open (W / "p" / ("%s.seq.json" % name ))) 29seq = seqj ["prompt" ]+ seqj ["out" ] 30layers ,pos ,tok ,taps = R .read_taps (str (W / "taps" / ("%s.bin" % name ))) 31keep = pos < len (seq ) 32pos ,taps = pos [keep ],taps [keep ] 33feats = np .zeros ((int (pos .max ())+ 1 ,taps .shape [1 ]),np .float32 ) 34for i in range (len (pos )): 35feats [pos [i ]]= taps [i ] 36feats [0 ]= feats [1 ] 37d .set_context (torch .from_numpy (feats )) 38s = len (seqj ["prompt" ]) 39cyc = [] 40with torch .no_grad (): 41while s + b <= len (seq ): 42path ,_ = d .draft (s ,seq [s ],b ) 43truth = seq [s + 1 :s + b ] 44a = 0 45while a < b - 1 and path [a ]== truth [a ]: 46a += 1 47cyc .append (a + 1 ) 48s += a + 1 49"%s %s block %d: %.3f tokens per round (%d rounds)" % (tag ,name ,b ,np .mean (cyc ),len (cyc )),flush = True ) 50 51 52names = sys .argv [1 ].split ("," ) 53orig = {k :v .clone ()for k ,v in d .w .items ()if k .endswith ("proj.weight" )or k == "fc.weight" or k .endswith ("kernel_projection.weight" )} 54for tag in ("fp32" ,"q8_0" ): 55if tag == "q8_0" : 56for k in orig : 57d .w [k ]= q8_0 (orig [k ]) 58for n in names : 59run (n ,4 ,tag ) 60run (n ,8 ,tag )