threshold-mux16 / model.py
phanerozoic's picture
Upload folder using huggingface_hub
d8ea23b verified
raw
history blame contribute delete
894 Bytes
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def mux16(data, s3, s2, s1, s0, weights):
"""16:1 Multiplexer: returns data[s] where s = 8*s3 + 4*s2 + 2*s1 + s0"""
inp = torch.tensor([float(d) for d in data] + [float(s3), float(s2), float(s1), float(s0)])
l1 = (inp @ weights['layer1.weight'].T + weights['layer1.bias'] >= 0).float()
out = (l1 @ weights['layer2.weight'].T + weights['layer2.bias'] >= 0).float()
return int(out.item())
if __name__ == '__main__':
w = load_model()
print('MUX16 verification:')
for s in range(16):
s3, s2, s1, s0 = (s >> 3) & 1, (s >> 2) & 1, (s >> 1) & 1, s & 1
data = [0] * 16
data[s] = 1
result = mux16(data, s3, s2, s1, s0, w)
print(f' s={s:2d} ({s3}{s2}{s1}{s0}), d[{s}]=1 -> {result}')