phanerozoic commited on
Commit
bf721a0
Β·
verified Β·
1 Parent(s): c5b2be4

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +129 -0
  2. config.json +9 -0
  3. model.py +30 -0
  4. 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