Upload folder using huggingface_hub
Browse files- README.md +129 -0
- config.json +9 -0
- model.py +30 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- comparison
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# threshold-equal
|
| 12 |
+
|
| 13 |
+
4-bit equality comparator. Returns 1 if a = b, 0 otherwise.
|
| 14 |
+
|
| 15 |
+
## Circuit
|
| 16 |
+
|
| 17 |
+
```
|
| 18 |
+
a0 b0 a1 b1 a2 b2 a3 b3
|
| 19 |
+
β β β β β β β β
|
| 20 |
+
βββ¬ββ βββ¬ββ βββ¬ββ βββ¬ββ
|
| 21 |
+
βΌ βΌ βΌ βΌ
|
| 22 |
+
βββββββ βββββββ βββββββ βββββββ
|
| 23 |
+
βXOR 0β βXOR 1β βXOR 2β βXOR 3β Layer 1-2
|
| 24 |
+
βββββββ βββββββ βββββββ βββββββ
|
| 25 |
+
β β β β
|
| 26 |
+
βββββββββββββ΄ββββββ¬ββββββ΄ββββββββββββ
|
| 27 |
+
βΌ
|
| 28 |
+
βββββββββββ
|
| 29 |
+
β NOR β Layer 3
|
| 30 |
+
β w: -1Γ4 β
|
| 31 |
+
β b: 0 β
|
| 32 |
+
βββββββββββ
|
| 33 |
+
β
|
| 34 |
+
βΌ
|
| 35 |
+
(a = b)
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
## Mechanism
|
| 39 |
+
|
| 40 |
+
1. **XOR each bit pair**: XOR(a_i, b_i) = 1 if bits differ, 0 if same
|
| 41 |
+
2. **NOR all XORs**: Fires only when all XORs are 0 (all bits match)
|
| 42 |
+
|
| 43 |
+
The NOR gate acts as a "zero detector" - it fires when all its inputs are silent.
|
| 44 |
+
|
| 45 |
+
## XOR Structure (each)
|
| 46 |
+
|
| 47 |
+
```
|
| 48 |
+
a b
|
| 49 |
+
β β
|
| 50 |
+
βββ¬ββ€
|
| 51 |
+
β β β
|
| 52 |
+
βΌ β βΌ
|
| 53 |
+
βββββββββββββ
|
| 54 |
+
β OR βββNANDβ
|
| 55 |
+
βββββββββββββ
|
| 56 |
+
β β β
|
| 57 |
+
βββΌββ
|
| 58 |
+
βΌ
|
| 59 |
+
ββββββββ
|
| 60 |
+
β AND β
|
| 61 |
+
ββββββββ
|
| 62 |
+
β
|
| 63 |
+
βΌ
|
| 64 |
+
XOR out
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
## Truth Table (sample)
|
| 68 |
+
|
| 69 |
+
| a | b | a = b |
|
| 70 |
+
|---|---|-------|
|
| 71 |
+
| 0 | 0 | 1 |
|
| 72 |
+
| 0 | 1 | 0 |
|
| 73 |
+
| 5 | 5 | 1 |
|
| 74 |
+
| 5 | 10 | 0 |
|
| 75 |
+
| 15 | 15 | 1 |
|
| 76 |
+
| 7 | 8 | 0 |
|
| 77 |
+
|
| 78 |
+
All 256 input combinations verified correct.
|
| 79 |
+
|
| 80 |
+
## Architecture
|
| 81 |
+
|
| 82 |
+
| Component | Neurons | Parameters |
|
| 83 |
+
|-----------|---------|------------|
|
| 84 |
+
| XOR Γ 4 | 12 | 36 |
|
| 85 |
+
| NOR | 1 | 5 |
|
| 86 |
+
| **Total** | **13** | **41** |
|
| 87 |
+
|
| 88 |
+
**Layers: 3** (XOR: 2, NOR: 1)
|
| 89 |
+
|
| 90 |
+
## Comparison Family
|
| 91 |
+
|
| 92 |
+
| Circuit | Condition | Implementation |
|
| 93 |
+
|---------|-----------|----------------|
|
| 94 |
+
| LessThan | a < b | borrow_out of (a - b) |
|
| 95 |
+
| **Equal** | a = b | NOR of all XOR bits |
|
| 96 |
+
| GreaterThan | a > b | LessThan(b, a) |
|
| 97 |
+
|
| 98 |
+
## Usage
|
| 99 |
+
|
| 100 |
+
```python
|
| 101 |
+
from safetensors.torch import load_file
|
| 102 |
+
|
| 103 |
+
w = load_file('model.safetensors')
|
| 104 |
+
|
| 105 |
+
def equal(a, b):
|
| 106 |
+
"""a, b: 4-bit lists [a0,a1,a2,a3] (LSB first)
|
| 107 |
+
Returns: 1 if a == b, 0 otherwise"""
|
| 108 |
+
# See model.py for full implementation
|
| 109 |
+
pass
|
| 110 |
+
|
| 111 |
+
# Example: 7 == 7?
|
| 112 |
+
a = [1, 1, 1, 0] # 7 in LSB-first
|
| 113 |
+
b = [1, 1, 1, 0] # 7 in LSB-first
|
| 114 |
+
result = equal(a, b) # Returns 1
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
## Files
|
| 118 |
+
|
| 119 |
+
```
|
| 120 |
+
threshold-equal/
|
| 121 |
+
βββ model.safetensors
|
| 122 |
+
βββ model.py
|
| 123 |
+
βββ config.json
|
| 124 |
+
βββ README.md
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
## License
|
| 128 |
+
|
| 129 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-equal",
|
| 3 |
+
"description": "4-bit equality comparator as threshold circuit",
|
| 4 |
+
"inputs": 8,
|
| 5 |
+
"outputs": 1,
|
| 6 |
+
"neurons": 13,
|
| 7 |
+
"layers": 3,
|
| 8 |
+
"parameters": 41
|
| 9 |
+
}
|
model.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import load_file
|
| 3 |
+
|
| 4 |
+
def load_model(path='model.safetensors'):
|
| 5 |
+
return load_file(path)
|
| 6 |
+
|
| 7 |
+
def xor_gate(a, b, w, idx):
|
| 8 |
+
inp = torch.tensor([float(a), float(b)])
|
| 9 |
+
l1 = (inp @ w[f'xor{idx}.layer1.weight'].T + w[f'xor{idx}.layer1.bias'] >= 0).float()
|
| 10 |
+
return int((l1 @ w[f'xor{idx}.layer2.weight'].T + w[f'xor{idx}.layer2.bias'] >= 0).item())
|
| 11 |
+
|
| 12 |
+
def equal(a, b, weights):
|
| 13 |
+
"""4-bit equality comparator.
|
| 14 |
+
a, b: lists of 4 bits each (LSB first)
|
| 15 |
+
Returns: 1 if a == b, 0 otherwise
|
| 16 |
+
"""
|
| 17 |
+
xors = [xor_gate(a[i], b[i], weights, i) for i in range(4)]
|
| 18 |
+
xor_vec = torch.tensor([float(x) for x in xors])
|
| 19 |
+
return int((xor_vec @ weights['nor.weight'].T + weights['nor.bias'] >= 0).item())
|
| 20 |
+
|
| 21 |
+
if __name__ == '__main__':
|
| 22 |
+
w = load_model()
|
| 23 |
+
print('4-bit Equal Comparator')
|
| 24 |
+
print('a == b tests:')
|
| 25 |
+
tests = [(0, 0), (0, 1), (5, 5), (5, 10), (15, 15), (7, 8)]
|
| 26 |
+
for a_val, b_val in tests:
|
| 27 |
+
a = [(a_val >> i) & 1 for i in range(4)]
|
| 28 |
+
b = [(b_val >> i) & 1 for i in range(4)]
|
| 29 |
+
result = equal(a, b, w)
|
| 30 |
+
print(f'{a_val:2d} == {b_val:2d} = {result}')
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:37c8658216c91731dfe0dc104eb66c594b7324eea390dfa2c84ed7a99480a910
|
| 3 |
+
size 1468
|