File size: 757 Bytes
86ab3ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import torch
from safetensors.torch import load_file

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

def demux8(d, s2, s1, s0, weights):
    """1:8 Demultiplexer: routes d to output y[s] where s = 4*s2 + 2*s1 + s0"""
    inp = torch.tensor([float(d), float(s2), float(s1), float(s0)])
    outputs = []
    for i in range(8):
        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('DEMUX8 verification:')
    for s in range(8):
        s2, s1, s0 = (s >> 2) & 1, (s >> 1) & 1, s & 1
        result = demux8(1, s2, s1, s0, w)
        print(f'  d=1, s={s} ({s2}{s1}{s0}) -> {"".join(map(str, result))}')