File size: 3,464 Bytes
bf721a0 185e212 bf721a0 185e212 bf721a0 185e212 bf721a0 185e212 bf721a0 185e212 bf721a0 185e212 bf721a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
---
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
|