import torch from safetensors.torch import save_file # exactly2: (sum >= 2) AND (sum <= 2) weights = { 'layer1.weight': torch.tensor([ [1.0, 1.0, 1.0, 1.0], # N1: sum >= 2 [-1.0, -1.0, -1.0, -1.0] # N2: sum <= 2 ], dtype=torch.float32), 'layer1.bias': torch.tensor([-2.0, 2.0], dtype=torch.float32), 'layer2.weight': torch.tensor([[1.0, 1.0]], dtype=torch.float32), 'layer2.bias': torch.tensor([-2.0], dtype=torch.float32) } save_file(weights, 'model.safetensors') def exactly2of4(a, b, c, d): inp = torch.tensor([float(a), float(b), float(c), float(d)]) l1 = (inp @ weights['layer1.weight'].T + weights['layer1.bias'] >= 0).float() out = (l1 @ weights['layer2.weight'].T + weights['layer2.bias'] >= 0).float() return int(out.item()) print("Verifying exactly2outof4...") errors = 0 for i in range(16): a, b, c, d = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1 result = exactly2of4(a, b, c, d) expected = 1 if (a + b + c + d) == 2 else 0 if result != expected: errors += 1 print(f"ERROR: {a}{b}{c}{d} -> {result}, expected {expected}") if errors == 0: print("All 16 test cases passed!") print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")