7-bit parity function. Outputs 1 if an odd number of inputs are high. Essential for error detection in 7-bit data words.
x0 x1 x2 x3 x4 x5 x6
│ │ │ │ │ │ │
└─┬─┘ └─┬─┘ └─┬─┘ │
│ │ │ │
▼ ▼ ▼ │
┌─────┐ ┌─────┐ ┌─────┐ │
│XOR01│ │XOR23│ │XOR45│ │ Level 1
└─────┘ └─────┘ └─────┘ │
│ │ │ │
└────┬────┘ └───┬───┘
│ │
▼ ▼
┌───────┐ ┌───────┐
│XOR0123│ │XOR456 │ Level 2
└───────┘ └───────┘
│ │
└────────┬─────────┘
│
▼
┌─────────┐
│XOR_final│ Level 3
└─────────┘
│
▼
parity
Returns 1 when the Hamming weight is odd (1, 3, 5, or 7).
1from safetensors.torch import load_file
2import torch
3
4w = load_file('model.safetensors')
5
6def xor2(a, b, prefix):
7 or_out = int(a * w[f'{prefix}.or.weight'][0] + b * w[f'{prefix}.or.weight'][1] + w[f'{prefix}.or.bias'] >= 0)
8 nand_out = int(a * w[f'{prefix}.nand.weight'][0] + b * w[f'{prefix}.nand.weight'][1] + w[f'{prefix}.nand.bias'] >= 0)
9 return int(or_out * w[f'{prefix}.and.weight'][0] + nand_out * w[f'{prefix}.and.weight'][1] + w[f'{prefix}.and.bias'] >= 0)
10
11def parity7(x0, x1, x2, x3, x4, x5, x6):
12 xor01 = xor2(x0, x1, 'xor_01')
13 xor23 = xor2(x2, x3, 'xor_23')
14 xor45 = xor2(x4, x5, 'xor_45')
15 xor0123 = xor2(xor01, xor23, 'xor_0123')
16 xor456 = xor2(xor45, x6, 'xor_456')
17 return xor2(xor0123, xor456, 'xor_final')
18
19# Examples
20print(parity7(1, 0, 1, 0, 1, 0, 0)) # 1 (odd: 3 ones)
21print(parity7(1, 1, 1, 1, 1, 1, 1)) # 1 (odd: 7 ones)
22print(parity7(1, 1, 0, 0, 0, 0, 0)) # 0 (even: 2 ones)
threshold-parity7/
├── model.safetensors
├── model.py
├── create_safetensors.py
├── config.json
└── README.md