threshold-tristate / model.py
CharlesCNorton
Add 4-bit tristate buffer threshold circuit
3410320
raw
history blame contribute delete
995 Bytes
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def tristate4(e, d3, d2, d1, d0, weights):
"""4-bit tristate buffer. E=1 passes data, E=0 outputs zeros."""
inp = torch.tensor([float(e), float(d3), float(d2), float(d1), float(d0)])
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 Tristate Buffer:')
for e in [0, 1]:
for d in [5, 10]:
d3, d2, d1, d0 = (d >> 3) & 1, (d >> 2) & 1, (d >> 1) & 1, d & 1
y3, y2, y1, y0 = tristate4(e, d3, d2, d1, d0, w)
print(f' E={e}, D={d3}{d2}{d1}{d0} -> {y3}{y2}{y1}{y0}')