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