File size: 1,249 Bytes
52bccbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""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)
    @torch.no_grad()
    def calibrate(self,support,old,static):return calibrate(support,old,static)
    @torch.no_grad()
    def cache(self,x,old,static,cal):return original_cache(x,old,static,cal['thresholds'])
    @torch.no_grad()
    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())})