File size: 871 Bytes
85582c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import torch
from safetensors.torch import load_file

def load_model(path='model.safetensors'):
    return load_file(path)

def binary_to_thermometer(b2, b1, b0, weights):
    """Convert 3-bit binary to 7-bit thermometer code.

    Returns list [y0, y1, ..., y6] where yi=1 iff value > i.

    """
    inp = torch.tensor([float(b2), float(b1), float(b0)])
    outputs = []
    for i in range(7):
        y = int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0)
        outputs.append(y)
    return outputs

if __name__ == '__main__':
    w = load_model()
    print('Binary to Thermometer Converter')
    print('Value -> Thermometer')
    for val in range(8):
        b2, b1, b0 = (val >> 2) & 1, (val >> 1) & 1, val & 1
        therm = binary_to_thermometer(b2, b1, b0, w)
        print(f"  {val} -> {''.join(map(str, therm))}")