File size: 866 Bytes
3f960d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from safetensors.torch import load_file

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

def onehot_encode(a1, a0, weights):
    """Convert 2-bit binary to 4-bit one-hot encoding."""
    inp = torch.tensor([float(a1), float(a0)])

    y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
    y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
    y2 = int((inp @ weights['y2.weight'].T + weights['y2.bias'] >= 0).item())
    y3 = int((inp @ weights['y3.weight'].T + weights['y3.bias'] >= 0).item())

    return y3, y2, y1, y0

if __name__ == '__main__':
    w = load_model()
    print('One-Hot Encoder (2-to-4):')
    for val in range(4):
        a1, a0 = (val >> 1) & 1, val & 1
        y3, y2, y1, y0 = onehot_encode(a1, a0, w)
        print(f'  {val} ({a1}{a0}) -> {y3}{y2}{y1}{y0}')