threshold-parity8 / model.py
phanerozoic's picture
Upload folder using huggingface_hub
cf789e5 verified
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 parity8(bits, weights):
x01 = xor2(bits[0], bits[1], 'xor_01', weights)
x23 = xor2(bits[2], bits[3], 'xor_23', weights)
x45 = xor2(bits[4], bits[5], 'xor_45', weights)
x67 = xor2(bits[6], bits[7], 'xor_67', weights)
x0123 = xor2(x01, x23, 'xor_0123', weights)
x4567 = xor2(x45, x67, 'xor_4567', weights)
return xor2(x0123, x4567, 'xor_final', weights)
if __name__ == '__main__':
w = load_model()
print('parity8 selected outputs:')
for n_ones in range(9):
bits = [1 if j < n_ones else 0 for j in range(8)]
print(f' {n_ones} ones: {parity8(bits, w)}')