Download inference.py from factlogic/weight-response-predictor: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/factlogic/weight-response-predictor/resolve/main/inference.py
- Command line
-
hf download hf://factlogic/weight-response-predictor/inference.py
-
curl -L -o inference.py https://huggingface.co/factlogic/weight-response-predictor/resolve/main/inference.py
1.25 kB
| """Inference entry point for the public, frozen Weight-Response Predictor.""" | |
| from pathlib import Path | |
| from safetensors.torch import load_file | |
| from reader import GateReader,calibrate,original_cache,descriptors,infer | |
| from target import torch | |
| class Predictor: | |
| def __init__(self,path=None,device='cpu'): | |
| path=path or Path(__file__).with_name('model.safetensors') | |
| self.reader=GateReader().to(device);self.reader.load_state_dict(load_file(str(path),device=str(device)));self.reader.eval().requires_grad_(False) | |
| def calibrate(self,support,old,static):return calibrate(support,old,static) | |
| def cache(self,x,old,static,cal):return original_cache(x,old,static,cal['thresholds']) | |
| def forecast(self,cache,cal,old,proposal,static): | |
| return infer(self.reader,cache,cal,descriptors(cache,cal),old,proposal,static) | |
| if __name__=='__main__': | |
| from fixture import example | |
| from target import logits | |
| o,u,st,s,y,f,v,g,q=example();model=Predictor();cal=model.calibrate(s,o,st) | |
| z=model.forecast(model.cache(q,o,st,cal),cal,o,u,st) | |
| print({'forecast_logits':list(z.shape),'mean_absolute_error_on_synthetic_input':float((z-logits(q,u,st)).abs().mean())}) | |