Upload folder using huggingface_hub
Browse files- README.md +31 -32
- config.json +4 -4
- model.py +8 -8
- model.safetensors +2 -2
README.md
CHANGED
|
@@ -10,29 +10,29 @@ tags:
|
|
| 10 |
|
| 11 |
# threshold-equal
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
## Circuit
|
| 16 |
|
| 17 |
```
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
```
|
| 37 |
|
| 38 |
## Mechanism
|
|
@@ -70,20 +70,19 @@ The NOR gate acts as a "zero detector" - it fires when all its inputs are silent
|
|
| 70 |
|---|---|-------|
|
| 71 |
| 0 | 0 | 1 |
|
| 72 |
| 0 | 1 | 0 |
|
| 73 |
-
|
|
| 74 |
-
|
|
| 75 |
-
|
|
| 76 |
-
|
|
| 77 |
-
|
| 78 |
-
All 256 input combinations verified correct.
|
| 79 |
|
| 80 |
## Architecture
|
| 81 |
|
| 82 |
| Component | Neurons | Parameters |
|
| 83 |
|-----------|---------|------------|
|
| 84 |
-
| XOR Γ
|
| 85 |
-
| NOR | 1 |
|
| 86 |
-
| **Total** | **
|
| 87 |
|
| 88 |
**Layers: 3** (XOR: 2, NOR: 1)
|
| 89 |
|
|
@@ -103,14 +102,14 @@ from safetensors.torch import load_file
|
|
| 103 |
w = load_file('model.safetensors')
|
| 104 |
|
| 105 |
def equal(a, b):
|
| 106 |
-
"""a, b:
|
| 107 |
Returns: 1 if a == b, 0 otherwise"""
|
| 108 |
# See model.py for full implementation
|
| 109 |
pass
|
| 110 |
|
| 111 |
-
# Example:
|
| 112 |
-
a = [
|
| 113 |
-
b = [
|
| 114 |
result = equal(a, b) # Returns 1
|
| 115 |
```
|
| 116 |
|
|
|
|
| 10 |
|
| 11 |
# threshold-equal
|
| 12 |
|
| 13 |
+
8-bit equality comparator. Returns 1 if a = b, 0 otherwise.
|
| 14 |
|
| 15 |
## Circuit
|
| 16 |
|
| 17 |
```
|
| 18 |
+
a0 b0 a1 b1 a2 b2 a3 b3 a4 b4 a5 b5 a6 b6 a7 b7
|
| 19 |
+
β β β β β β β β β β β β β β β β
|
| 20 |
+
ββ¬ββ ββ¬ββ ββ¬ββ ββ¬ββ ββ¬ββ ββ¬ββ ββ¬ββ ββ¬ββ
|
| 21 |
+
βΌ βΌ βΌ βΌ βΌ βΌ βΌ βΌ
|
| 22 |
+
βββββ βββββ βββββ βββββ βββββ βββββ βββββ βββββ
|
| 23 |
+
βXORβ βXORβ βXORβ βXORβ βXORβ βXORβ βXORβ βXORβ L1-2
|
| 24 |
+
βββββ βββββ βββββ βββββ βββββ βββββ βββββ βββββ
|
| 25 |
+
β β β β β β β β
|
| 26 |
+
βββββββββ΄ββββββββ΄ββββββββ΄ββββ¬ββββ΄ββββββββ΄ββββββββ΄ββββββββ
|
| 27 |
+
βΌ
|
| 28 |
+
βββββββββββ
|
| 29 |
+
β NOR β L3
|
| 30 |
+
β w: -1Γ8 β
|
| 31 |
+
β b: 0 β
|
| 32 |
+
βββββββββββ
|
| 33 |
+
β
|
| 34 |
+
βΌ
|
| 35 |
+
(a = b)
|
| 36 |
```
|
| 37 |
|
| 38 |
## Mechanism
|
|
|
|
| 70 |
|---|---|-------|
|
| 71 |
| 0 | 0 | 1 |
|
| 72 |
| 0 | 1 | 0 |
|
| 73 |
+
| 127 | 127 | 1 |
|
| 74 |
+
| 127 | 128 | 0 |
|
| 75 |
+
| 255 | 255 | 1 |
|
| 76 |
+
| 255 | 0 | 0 |
|
| 77 |
+
| 100 | 100 | 1 |
|
|
|
|
| 78 |
|
| 79 |
## Architecture
|
| 80 |
|
| 81 |
| Component | Neurons | Parameters |
|
| 82 |
|-----------|---------|------------|
|
| 83 |
+
| XOR Γ 8 | 24 | 72 |
|
| 84 |
+
| NOR | 1 | 9 |
|
| 85 |
+
| **Total** | **25** | **81** |
|
| 86 |
|
| 87 |
**Layers: 3** (XOR: 2, NOR: 1)
|
| 88 |
|
|
|
|
| 102 |
w = load_file('model.safetensors')
|
| 103 |
|
| 104 |
def equal(a, b):
|
| 105 |
+
"""a, b: 8-bit lists [a0..a7] (LSB first)
|
| 106 |
Returns: 1 if a == b, 0 otherwise"""
|
| 107 |
# See model.py for full implementation
|
| 108 |
pass
|
| 109 |
|
| 110 |
+
# Example: 127 == 127?
|
| 111 |
+
a = [(127 >> i) & 1 for i in range(8)]
|
| 112 |
+
b = [(127 >> i) & 1 for i in range(8)]
|
| 113 |
result = equal(a, b) # Returns 1
|
| 114 |
```
|
| 115 |
|
config.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
{
|
| 2 |
"name": "threshold-equal",
|
| 3 |
-
"description": "
|
| 4 |
-
"inputs":
|
| 5 |
"outputs": 1,
|
| 6 |
-
"neurons":
|
| 7 |
"layers": 3,
|
| 8 |
-
"parameters":
|
| 9 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"name": "threshold-equal",
|
| 3 |
+
"description": "8-bit equality comparator as threshold circuit",
|
| 4 |
+
"inputs": 16,
|
| 5 |
"outputs": 1,
|
| 6 |
+
"neurons": 25,
|
| 7 |
"layers": 3,
|
| 8 |
+
"parameters": 81
|
| 9 |
}
|
model.py
CHANGED
|
@@ -10,21 +10,21 @@ def xor_gate(a, b, w, idx):
|
|
| 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 |
-
"""
|
| 14 |
-
a, b: lists of
|
| 15 |
Returns: 1 if a == b, 0 otherwise
|
| 16 |
"""
|
| 17 |
-
xors = [xor_gate(a[i], b[i], weights, i) for i in range(
|
| 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('
|
| 24 |
print('a == b tests:')
|
| 25 |
-
tests = [(0, 0), (0, 1), (
|
| 26 |
for a_val, b_val in tests:
|
| 27 |
-
a = [(a_val >> i) & 1 for i in range(
|
| 28 |
-
b = [(b_val >> i) & 1 for i in range(
|
| 29 |
result = equal(a, b, w)
|
| 30 |
-
print(f'{a_val:
|
|
|
|
| 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 |
+
"""8-bit equality comparator.
|
| 14 |
+
a, b: lists of 8 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(8)]
|
| 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('8-bit Equal Comparator')
|
| 24 |
print('a == b tests:')
|
| 25 |
+
tests = [(0, 0), (0, 1), (127, 127), (127, 128), (255, 255), (255, 0), (100, 100)]
|
| 26 |
for a_val, b_val in tests:
|
| 27 |
+
a = [(a_val >> i) & 1 for i in range(8)]
|
| 28 |
+
b = [(b_val >> i) & 1 for i in range(8)]
|
| 29 |
result = equal(a, b, w)
|
| 30 |
+
print(f'{a_val:3d} == {b_val:3d} = {result}')
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:828bc4ccd6ed542dafd4178ba934b4632a43f28778621e9bdf362be94d397cd6
|
| 3 |
+
size 2820
|