threshold-buffer / create_safetensors.py
CharlesCNorton
Add 4-bit buffer threshold circuit
5334ac8
import torch
from safetensors.torch import save_file
weights = {}
# 4-bit Buffer (identity function)
# Inputs: x3, x2, x1, x0
# Outputs: y3, y2, y1, y0 (same as inputs)
#
# Each output is a threshold neuron that fires when input >= 1
# y_i = 1 iff x_i = 1
# y0 = x0
weights['y0.weight'] = torch.tensor([[0.0, 0.0, 0.0, 1.0]], dtype=torch.float32)
weights['y0.bias'] = torch.tensor([-1.0], dtype=torch.float32)
# y1 = x1
weights['y1.weight'] = torch.tensor([[0.0, 0.0, 1.0, 0.0]], dtype=torch.float32)
weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32)
# y2 = x2
weights['y2.weight'] = torch.tensor([[0.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
weights['y2.bias'] = torch.tensor([-1.0], dtype=torch.float32)
# y3 = x3
weights['y3.weight'] = torch.tensor([[1.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
weights['y3.bias'] = torch.tensor([-1.0], dtype=torch.float32)
save_file(weights, 'model.safetensors')
def buffer4(x3, x2, x1, x0):
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
print("Verifying 4-bit Buffer...")
errors = 0
for i in range(16):
x3, x2, x1, x0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
y3, y2, y1, y0 = buffer4(x3, x2, x1, x0)
if (y3, y2, y1, y0) != (x3, x2, x1, x0):
errors += 1
print(f"ERROR: ({x3},{x2},{x1},{x0}) -> ({y3},{y2},{y1},{y0})")
if errors == 0:
print("All 16 test cases passed!")
else:
print(f"FAILED: {errors} errors")
print("\nTruth Table:")
print("x3 x2 x1 x0 | y3 y2 y1 y0")
print("-" * 26)
for i in range(16):
x3, x2, x1, x0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
y3, y2, y1, y0 = buffer4(x3, x2, x1, x0)
print(f" {x3} {x2} {x1} {x0} | {y3} {y2} {y1} {y0}")
mag = sum(t.abs().sum().item() for t in weights.values())
print(f"\nMagnitude: {mag:.0f}")
print(f"Parameters: {sum(t.numel() for t in weights.values())}")
print(f"Neurons: {len([k for k in weights.keys() if 'weight' in k])}")