threshold-buffer / model.py
CharlesCNorton
Add 4-bit buffer threshold circuit
5334ac8
raw
history blame contribute delete
902 Bytes
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def buffer4(x3, x2, x1, x0, weights):
"""4-bit buffer (identity function)."""
inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
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('4-bit Buffer:')
for i in [0, 5, 10, 15]:
x3, x2, x1, x0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
y3, y2, y1, y0 = buffer4(x3, x2, x1, x0, w)
print(f' {x3}{x2}{x1}{x0} -> {y3}{y2}{y1}{y0}')