import torch from safetensors.torch import save_file weights = {} # Input order: a3, a2, a1, a0 # Output: y3=a0, y2=a1, y1=a2, y0=a3 # Each output copies from reversed position # y3 = a0 (copy from position 3) weights['y3.weight'] = torch.tensor([[0.0, 0.0, 0.0, 1.0]], dtype=torch.float32) weights['y3.bias'] = torch.tensor([-1.0], dtype=torch.float32) # y2 = a1 (copy from position 2) 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 = a2 (copy from position 1) weights['y1.weight'] = torch.tensor([[0.0, 1.0, 0.0, 0.0]], dtype=torch.float32) weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32) # y0 = a3 (copy from position 0) weights['y0.weight'] = torch.tensor([[1.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 reverse4(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 reverse4...") errors = 0 for i in range(16): a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1 result = reverse4(a3, a2, a1, a0) expected = [a0, a1, a2, a3] if result != expected: errors += 1 print(f"ERROR: {a3}{a2}{a1}{a0} -> {result}, expected {expected}") if errors == 0: print("All 16 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}")