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

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

def xor2(a, b, prefix, w):
    or_out = int(a * w[f'{prefix}.or.weight'][0] + b * w[f'{prefix}.or.weight'][1] + w[f'{prefix}.or.bias'] >= 0)
    nand_out = int(a * w[f'{prefix}.nand.weight'][0] + b * w[f'{prefix}.nand.weight'][1] + w[f'{prefix}.nand.bias'] >= 0)
    return int(or_out * w[f'{prefix}.and.weight'][0] + nand_out * w[f'{prefix}.and.weight'][1] + w[f'{prefix}.and.bias'] >= 0)

def parity5(a, b, c, d, e, weights):
    xor_ab = xor2(a, b, 'xor_ab', weights)
    xor_cd = xor2(c, d, 'xor_cd', weights)
    xor_abcd = xor2(xor_ab, xor_cd, 'xor_abcd', weights)
    return xor2(xor_abcd, e, 'xor_final', weights)

if __name__ == '__main__':
    w = load_model()
    print('parity5 selected outputs:')
    for n_ones in range(6):
        bits = [1 if j < n_ones else 0 for j in range(5)]
        print(f'  {n_ones} ones: {bits} -> {parity5(*bits, w)}')