Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -9,7 +9,7 @@ tags:
|
|
| 9 |
|
| 10 |
# threshold-exactly2outof8
|
| 11 |
|
| 12 |
-
Exactly-2-out-of-8 detector. Fires when
|
| 13 |
|
| 14 |
## Circuit
|
| 15 |
|
|
@@ -22,8 +22,7 @@ Exactly-2-out-of-8 detector. Fires when exactly two inputs are active.
|
|
| 22 |
βΌ βΌ
|
| 23 |
βββββββββββ βββββββββββ
|
| 24 |
β AtLeast2β β AtMost2 β
|
| 25 |
-
β
|
| 26 |
-
β b: -2 β β b: +2 β
|
| 27 |
βββββββββββ βββββββββββ
|
| 28 |
β β
|
| 29 |
βββββββββ¬ββββββββ
|
|
@@ -36,25 +35,86 @@ Exactly-2-out-of-8 detector. Fires when exactly two inputs are active.
|
|
| 36 |
(HW = 2?)
|
| 37 |
```
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
## Mechanism
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
## Truth Table
|
| 46 |
|
| 47 |
-
| HW | AtLeast2 | AtMost2 |
|
| 48 |
-
|----|----------|---------|--------
|
| 49 |
| 0 | 0 | 1 | 0 |
|
| 50 |
| 1 | 0 | 1 | 0 |
|
| 51 |
-
| 2 | 1 | 1 | **1** |
|
| 52 |
| 3 | 1 | 0 | 0 |
|
| 53 |
-
|
|
| 54 |
|
| 55 |
-
##
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
## Files
|
| 60 |
|
|
|
|
| 9 |
|
| 10 |
# threshold-exactly2outof8
|
| 11 |
|
| 12 |
+
Exactly-2-out-of-8 detector. Fires when precisely two inputs are active. The pair detector.
|
| 13 |
|
| 14 |
## Circuit
|
| 15 |
|
|
|
|
| 22 |
βΌ βΌ
|
| 23 |
βββββββββββ βββββββββββ
|
| 24 |
β AtLeast2β β AtMost2 β
|
| 25 |
+
β HW β₯ 2 β β HW β€ 2 β
|
|
|
|
| 26 |
βββββββββββ βββββββββββ
|
| 27 |
β β
|
| 28 |
βββββββββ¬ββββββββ
|
|
|
|
| 35 |
(HW = 2?)
|
| 36 |
```
|
| 37 |
|
| 38 |
+
## The Pair Detection Problem
|
| 39 |
+
|
| 40 |
+
Detecting exactly two active inputs is useful in:
|
| 41 |
+
|
| 42 |
+
- **Error detection**: Exactly 2 bit flips in a codeword
|
| 43 |
+
- **Voting systems**: Detecting ties in 4-person subcommittees
|
| 44 |
+
- **Combinatorics**: Enumerating pairs from a set
|
| 45 |
+
|
| 46 |
+
There are C(8,2) = 28 input patterns that fire this circuit.
|
| 47 |
+
|
| 48 |
## Mechanism
|
| 49 |
|
| 50 |
+
Two threshold neurons work in concert:
|
| 51 |
+
|
| 52 |
+
**AtLeast2** (lower bound):
|
| 53 |
+
```
|
| 54 |
+
sum = xβ + xβ + ... + xβ - 2
|
| 55 |
+
fires when sum β₯ 0, i.e., HW β₯ 2
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
**AtMost2** (upper bound):
|
| 59 |
+
```
|
| 60 |
+
sum = -xβ - xβ - ... - xβ + 2
|
| 61 |
+
fires when sum β₯ 0, i.e., -HW + 2 β₯ 0, i.e., HW β€ 2
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
The AND gate computes the intersection: HW β₯ 2 AND HW β€ 2 = HW = 2.
|
| 65 |
|
| 66 |
## Truth Table
|
| 67 |
|
| 68 |
+
| HW | AtLeast2 | AtMost2 | Output |
|
| 69 |
+
|----|----------|---------|--------|
|
| 70 |
| 0 | 0 | 1 | 0 |
|
| 71 |
| 1 | 0 | 1 | 0 |
|
| 72 |
+
| **2** | **1** | **1** | **1** |
|
| 73 |
| 3 | 1 | 0 | 0 |
|
| 74 |
+
| 4+ | 1 | 0 | 0 |
|
| 75 |
|
| 76 |
+
## Parameters
|
| 77 |
|
| 78 |
+
| Component | Weights | Bias |
|
| 79 |
+
|-----------|---------|------|
|
| 80 |
+
| AtLeast2 | [+1, +1, +1, +1, +1, +1, +1, +1] | -2 |
|
| 81 |
+
| AtMost2 | [-1, -1, -1, -1, -1, -1, -1, -1] | +2 |
|
| 82 |
+
| AND | [+1, +1] | -2 |
|
| 83 |
+
|
| 84 |
+
**Total: 3 neurons, 21 parameters, 2 layers**
|
| 85 |
+
|
| 86 |
+
## Contrast with At-Least-2
|
| 87 |
+
|
| 88 |
+
| Circuit | Condition | Fires on HW |
|
| 89 |
+
|---------|-----------|-------------|
|
| 90 |
+
| 2-out-of-8 | HW β₯ 2 | 2, 3, 4, 5, 6, 7, 8 |
|
| 91 |
+
| **Exactly-2** | HW = 2 | 2 only |
|
| 92 |
+
|
| 93 |
+
The existing `threshold-2outof8` fires on 247 of 256 inputs. This circuit fires on only 28.
|
| 94 |
+
|
| 95 |
+
## Usage
|
| 96 |
+
|
| 97 |
+
```python
|
| 98 |
+
from safetensors.torch import load_file
|
| 99 |
+
import torch
|
| 100 |
+
|
| 101 |
+
w = load_file('model.safetensors')
|
| 102 |
+
|
| 103 |
+
def exactly2(bits):
|
| 104 |
+
inp = torch.tensor([float(b) for b in bits])
|
| 105 |
+
atleast = int((inp * w['atleast.weight']).sum() + w['atleast.bias'] >= 0)
|
| 106 |
+
atmost = int((inp * w['atmost.weight']).sum() + w['atmost.bias'] >= 0)
|
| 107 |
+
comb = torch.tensor([float(atleast), float(atmost)])
|
| 108 |
+
return int((comb * w['and.weight']).sum() + w['and.bias'] >= 0)
|
| 109 |
+
|
| 110 |
+
# Pair: positions 2 and 5 active
|
| 111 |
+
bits = [0, 0, 1, 0, 0, 1, 0, 0]
|
| 112 |
+
print(exactly2(bits)) # 1
|
| 113 |
+
|
| 114 |
+
# Three active - not a pair
|
| 115 |
+
bits = [1, 0, 1, 0, 0, 1, 0, 0]
|
| 116 |
+
print(exactly2(bits)) # 0
|
| 117 |
+
```
|
| 118 |
|
| 119 |
## Files
|
| 120 |
|