threshold-iszero8 / create_safetensors.py
CharlesCNorton
8-bit zero detector, magnitude 8
0a8be94
import torch
from safetensors.torch import save_file
weights = {
'neuron.weight': torch.tensor([[-1.0] * 8], dtype=torch.float32),
'neuron.bias': torch.tensor([0.0], dtype=torch.float32)
}
save_file(weights, 'model.safetensors')
def iszero8(bits):
inp = torch.tensor([float(b) for b in bits])
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
print("Verifying iszero8...")
errors = 0
for i in range(256):
bits = [(i >> j) & 1 for j in range(8)]
result = iszero8(bits)
expected = 1 if i == 0 else 0
if result != expected:
errors += 1
if errors == 0:
print("All 256 test cases passed!")
print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")