threshold-shiftleft4 / create_safetensors.py
CharlesCNorton
4-bit left shift, magnitude 7
1bda0cb
import torch
from safetensors.torch import save_file
weights = {}
# Input order: [a3, a2, a1, a0]
# Left shift: output = [a2, a1, a0, 0]
# y3 = a2
weights['y3.weight'] = torch.tensor([[0.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
weights['y3.bias'] = torch.tensor([-1.0], dtype=torch.float32)
# y2 = a1
weights['y2.weight'] = torch.tensor([[0.0, 0.0, 1.0, 0.0]], dtype=torch.float32)
weights['y2.bias'] = torch.tensor([-1.0], dtype=torch.float32)
# y1 = a0
weights['y1.weight'] = torch.tensor([[0.0, 0.0, 0.0, 1.0]], dtype=torch.float32)
weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32)
# y0 = 0 (constant)
weights['y0.weight'] = torch.tensor([[0.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
weights['y0.bias'] = torch.tensor([-1.0], dtype=torch.float32)
save_file(weights, 'model.safetensors')
# Verify
def shiftleft4(a3, a2, a1, a0):
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
y3 = int((inp @ weights['y3.weight'].T + weights['y3.bias'] >= 0).item())
y2 = int((inp @ weights['y2.weight'].T + weights['y2.bias'] >= 0).item())
y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
return [y3, y2, y1, y0]
print("Verifying shiftleft4...")
errors = 0
for i in range(16):
a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
result = shiftleft4(a3, a2, a1, a0)
expected = [a2, a1, a0, 0]
if result != expected:
errors += 1
print(f"ERROR: {a3}{a2}{a1}{a0} -> {result}, expected {expected}")
if errors == 0:
print("All 16 test cases passed!")
mag = sum(t.abs().sum().item() for t in weights.values())
print(f"Magnitude: {mag:.0f}")