threshold-equal / README.md
phanerozoic's picture
Upload folder using huggingface_hub
185e212 verified
---
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