suayptalha's picture
Initial release
e70aa91
Raw History Blame Contribute Delete
1.36 kB
"""Domain-specific circuit template compiler and weight manifest.
This emits the matrices/tables used by the registered modules. It is not a
general C compiler and does not rewrite pretrained LFM3 FFN neurons.
"""
import hashlib
import torch
from .engine import AnalyticEngine,FEATURES
def manifest(engine):
result={'compiler':'lfmplus-fixed-circuit-templates-1','training_tokens':0,
'spec':engine.spec,'features':{},'stored_tensor_bytes':0}
for name in FEATURES:
module=engine.layers[name];h=hashlib.sha256();size=0
for key,value in sorted(module.state_dict().items()):
raw=value.detach().cpu().contiguous().numpy().tobytes()
h.update(key.encode());h.update(raw);size+=len(raw)
count=sum(p.numel() for p in module.parameters())
if not count or any(p.requires_grad for p in module.parameters()):
raise RuntimeError('Every analytic feature must have frozen registered weights')
result['features'][name]={'parameters':count,'tensor_bytes':size,'sha256':h.hexdigest(),
'dtypes':sorted({str(p.dtype) for p in module.parameters()})}
result['stored_tensor_bytes']+=size
return result
def compile_features(spec=None,device='cpu'):
engine=AnalyticEngine(spec).to(device).eval()
engine.prepare()
manifest(engine)
return engine