--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - parity - error-detection --- # threshold-parity7 7-bit parity function. Outputs 1 if an odd number of inputs are high. Essential for error detection in 7-bit data words. ## Circuit ``` x0 x1 x2 x3 x4 x5 x6 │ │ │ │ │ │ │ └─┬─┘ └─┬─┘ └─┬─┘ │ │ │ │ │ ▼ ▼ ▼ │ ┌─────┐ ┌─────┐ ┌─────┐ │ │XOR01│ │XOR23│ │XOR45│ │ Level 1 └─────┘ └─────┘ └─────┘ │ │ │ │ │ └────┬────┘ └───┬───┘ │ │ ▼ ▼ ┌───────┐ ┌───────┐ │XOR0123│ │XOR456 │ Level 2 └───────┘ └───────┘ │ │ └────────┬─────────┘ │ ▼ ┌─────────┐ │XOR_final│ Level 3 └─────────┘ │ ▼ parity ``` ## Function ``` parity7(x0..x6) = x0 XOR x1 XOR x2 XOR x3 XOR x4 XOR x5 XOR x6 ``` Returns 1 when the Hamming weight is odd (1, 3, 5, or 7). ## Truth Table (by Hamming Weight) | HW | Example Input | Parity | |:--:|---------------|:------:| | 0 | 0000000 | 0 | | 1 | 0000001 | 1 | | 2 | 0000011 | 0 | | 3 | 0000111 | 1 | | 4 | 0001111 | 0 | | 5 | 0011111 | 1 | | 6 | 0111111 | 0 | | 7 | 1111111 | 1 | ## Mechanism **XOR Implementation (3 neurons per gate):** Each XOR is built from OR, NAND, and AND gates: ``` XOR(a, b) = AND(OR(a,b), NAND(a,b)) ``` | Gate | Weights | Bias | Fires when | |------|---------|------|------------| | OR | [1, 1] | -1 | a + b >= 1 | | NAND | [-1, -1] | +1 | a + b <= 1 | | AND | [1, 1] | -2 | both inputs = 1 | **Tree Structure (6 XOR gates):** | Level | Gates | Computes | |-------|-------|----------| | 1 | XOR01, XOR23, XOR45 | Pairs of inputs | | 2 | XOR0123, XOR456 | XOR(XOR01,XOR23), XOR(XOR45,x6) | | 3 | XOR_final | XOR(XOR0123, XOR456) | ## Architecture | Component | Function | Neurons | |-----------|----------|---------| | xor_01 | x0 XOR x1 | 3 | | xor_23 | x2 XOR x3 | 3 | | xor_45 | x4 XOR x5 | 3 | | xor_0123 | xor01 XOR xor23 | 3 | | xor_456 | xor45 XOR x6 | 3 | | xor_final | xor0123 XOR xor456 | 3 | **Total: 18 neurons, 6 XOR gates** ## Parameters | | | |---|---| | Inputs | 7 | | Outputs | 1 | | Neurons | 18 | | Layers | 6 | | Parameters | 54 | | Magnitude | 60 | ## Usage ```python from safetensors.torch import load_file import torch w = load_file('model.safetensors') def xor2(a, b, prefix): or_out = int(a * w[f'{prefix}.or.weight'][0] + b * w[f'{prefix}.or.weight'][1] + w[f'{prefix}.or.bias'] >= 0) nand_out = int(a * w[f'{prefix}.nand.weight'][0] + b * w[f'{prefix}.nand.weight'][1] + w[f'{prefix}.nand.bias'] >= 0) return int(or_out * w[f'{prefix}.and.weight'][0] + nand_out * w[f'{prefix}.and.weight'][1] + w[f'{prefix}.and.bias'] >= 0) def parity7(x0, x1, x2, x3, x4, x5, x6): xor01 = xor2(x0, x1, 'xor_01') xor23 = xor2(x2, x3, 'xor_23') xor45 = xor2(x4, x5, 'xor_45') xor0123 = xor2(xor01, xor23, 'xor_0123') xor456 = xor2(xor45, x6, 'xor_456') return xor2(xor0123, xor456, 'xor_final') # Examples print(parity7(1, 0, 1, 0, 1, 0, 0)) # 1 (odd: 3 ones) print(parity7(1, 1, 1, 1, 1, 1, 1)) # 1 (odd: 7 ones) print(parity7(1, 1, 0, 0, 0, 0, 0)) # 0 (even: 2 ones) ``` ## Applications - Hamming(7,4) code parity bit - 7-bit ASCII parity checking - Memory word error detection - Bus parity generation ## Files ``` threshold-parity7/ ├── model.safetensors ├── model.py ├── create_safetensors.py ├── config.json └── README.md ``` ## License MIT