| import torch
|
| from safetensors.torch import save_file
|
|
|
|
|
|
|
|
|
|
|
| weights = {}
|
|
|
|
|
| weights['y2.weight'] = torch.tensor([[1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
|
| weights['y2.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
|
|
|
|
|
|
| weights['y1.weight'] = torch.tensor([[16.0, 16.0, -4.0, -4.0, 1.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
|
| weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
|
|
|
|
|
|
| weights['y0.weight'] = torch.tensor([[128.0, -64.0, 32.0, -16.0, 8.0, -4.0, 2.0, 0.0]], dtype=torch.float32)
|
| weights['y0.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
|
|
| save_file(weights, 'model.safetensors')
|
|
|
| def encode8to3(i7, i6, i5, i4, i3, i2, i1, i0):
|
| inp = torch.tensor([float(i7), float(i6), float(i5), float(i4),
|
| float(i3), float(i2), float(i1), float(i0)])
|
| 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 y2, y1, y0
|
|
|
| print("Verifying 8to3encoder...")
|
| errors = 0
|
| for val in range(256):
|
| bits = [(val >> (7-i)) & 1 for i in range(8)]
|
| y2, y1, y0 = encode8to3(*bits)
|
|
|
|
|
| highest = -1
|
| for i in range(8):
|
| if bits[i]:
|
| highest = 7 - i
|
| break
|
|
|
| if highest < 0:
|
| expected = (0, 0, 0)
|
| else:
|
| expected = ((highest >> 2) & 1, (highest >> 1) & 1, highest & 1)
|
|
|
| if (y2, y1, y0) != expected:
|
| errors += 1
|
| if errors <= 10:
|
| print(f"ERROR: I={''.join(map(str,bits))} -> ({y2},{y1},{y0}), expected {expected} (pos {highest})")
|
|
|
| if errors == 0:
|
| print("All 256 test cases passed!")
|
| else:
|
| print(f"FAILED: {errors} errors")
|
|
|
| mag = sum(t.abs().sum().item() for t in weights.values())
|
| print(f"Magnitude: {mag:.0f}")
|
|
|