threshold-iszero4 / create_safetensors.py
CharlesCNorton
4-bit zero detector, magnitude 4
ac0f2bd
Raw
History Blame Contribute Delete
835 Bytes
import torch
from safetensors.torch import save_file
# iszero4: outputs 1 if all 4 bits are 0 (4-input NOR)
weights = {
'neuron.weight': torch.tensor([[-1.0, -1.0, -1.0, -1.0]], dtype=torch.float32),
'neuron.bias': torch.tensor([0.0], dtype=torch.float32)
}
save_file(weights, 'model.safetensors')
def iszero4(a, b, c, d):
inp = torch.tensor([float(a), float(b), float(c), float(d)])
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
print("Verifying iszero4...")
errors = 0
for i in range(16):
bits = [(i >> j) & 1 for j in range(4)]
result = iszero4(*bits)
expected = 1 if i == 0 else 0
if result != expected:
errors += 1
if errors == 0:
print("All 16 test cases passed!")
print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")