--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - comparison --- # threshold-equal 8-bit equality comparator. Returns 1 if a = b, 0 otherwise. ## Circuit ``` a0 b0 a1 b1 a2 b2 a3 b3 a4 b4 a5 b5 a6 b6 a7 b7 │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └┬─┘ └┬─┘ └┬─┘ └┬─┘ └┬─┘ └┬─┘ └┬─┘ └┬─┘ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ │XOR│ │XOR│ │XOR│ │XOR│ │XOR│ │XOR│ │XOR│ │XOR│ L1-2 └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ │ │ │ │ │ │ │ │ └───────┴───────┴───────┴───┬───┴───────┴───────┴───────┘ ▼ ┌─────────┐ │ NOR │ L3 │ w: -1×8 │ │ b: 0 │ └─────────┘ │ ▼ (a = b) ``` ## Mechanism 1. **XOR each bit pair**: XOR(a_i, b_i) = 1 if bits differ, 0 if same 2. **NOR all XORs**: Fires only when all XORs are 0 (all bits match) The NOR gate acts as a "zero detector" - it fires when all its inputs are silent. ## XOR Structure (each) ``` a b │ │ ├─┬─┤ │ │ │ ▼ │ ▼ ┌────┐│┌────┐ │ OR │││NAND│ └────┘│└────┘ │ │ │ └─┼─┘ ▼ ┌──────┐ │ AND │ └──────┘ │ ▼ XOR out ``` ## Truth Table (sample) | a | b | a = b | |---|---|-------| | 0 | 0 | 1 | | 0 | 1 | 0 | | 127 | 127 | 1 | | 127 | 128 | 0 | | 255 | 255 | 1 | | 255 | 0 | 0 | | 100 | 100 | 1 | ## Architecture | Component | Neurons | Parameters | |-----------|---------|------------| | XOR × 8 | 24 | 72 | | NOR | 1 | 9 | | **Total** | **25** | **81** | **Layers: 3** (XOR: 2, NOR: 1) ## Comparison Family | Circuit | Condition | Implementation | |---------|-----------|----------------| | LessThan | a < b | borrow_out of (a - b) | | **Equal** | a = b | NOR of all XOR bits | | GreaterThan | a > b | LessThan(b, a) | ## Usage ```python from safetensors.torch import load_file w = load_file('model.safetensors') def equal(a, b): """a, b: 8-bit lists [a0..a7] (LSB first) Returns: 1 if a == b, 0 otherwise""" # See model.py for full implementation pass # Example: 127 == 127? a = [(127 >> i) & 1 for i in range(8)] b = [(127 >> i) & 1 for i in range(8)] result = equal(a, b) # Returns 1 ``` ## Files ``` threshold-equal/ ├── model.safetensors ├── model.py ├── config.json └── README.md ``` ## License MIT