phanerozoic commited on
Commit
2198727
Β·
verified Β·
1 Parent(s): fbaab07

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +137 -0
  2. config.json +9 -0
  3. model.py +40 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - comparison
9
+ ---
10
+
11
+ # threshold-lessthan
12
+
13
+ 4-bit less-than comparator. Returns 1 if a < b, 0 otherwise.
14
+
15
+ ## Circuit
16
+
17
+ ```
18
+ a0 b0 0 a1 b1 a2 b2 a3 b3
19
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
20
+ β””β”€β”€β”Όβ”€β”˜ β””β”€β”€β”Όβ”€β”€β”˜ β””β”€β”€β”Όβ”€β”€β”˜ β””β”€β”€β”Όβ”€β”€β”˜
21
+ β–Ό β–Ό β–Ό β–Ό
22
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”
23
+ β”‚ FS0 │──b0───│ FS1 │──b1──│ FS2 │──b2──│ FS3 │───► (a < b)
24
+ β””β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”˜
25
+ β”‚ β”‚ β”‚ β”‚
26
+ β–Ό β–Ό β–Ό β–Ό
27
+ d0 d1 d2 d3
28
+ (unused) (unused) (unused) (unused)
29
+ ```
30
+
31
+ Uses 4 cascaded full subtractors. The final borrow output indicates a < b.
32
+
33
+ ## Mechanism
34
+
35
+ The circuit computes a - b using subtraction:
36
+
37
+ - If a β‰₯ b: no borrow propagates, result is non-negative
38
+ - If a < b: borrow propagates out, indicating underflow
39
+
40
+ The difference bits (d0-d3) are computed but unused. Only the final borrow matters.
41
+
42
+ ## Truth Table (sample)
43
+
44
+ | a | b | a < b |
45
+ |---|---|-------|
46
+ | 0 | 0 | 0 |
47
+ | 0 | 1 | 1 |
48
+ | 1 | 0 | 0 |
49
+ | 5 | 10 | 1 |
50
+ | 10 | 5 | 0 |
51
+ | 7 | 7 | 0 |
52
+ | 15 | 0 | 0 |
53
+ | 0 | 15 | 1 |
54
+
55
+ All 256 input combinations verified correct.
56
+
57
+ ## Full Subtractor Structure
58
+
59
+ ```
60
+ a b
61
+ β”‚ β”‚
62
+ β””β”€β”€β”€β”¬β”€β”€β”€β”˜
63
+ β–Ό
64
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
65
+ β”‚ HS1 β”‚
66
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
67
+ β”‚ β”‚
68
+ d1 b1
69
+ β”‚ \
70
+ β”‚ bin \
71
+ β””β”€β”€β”¬β”€β”€β”˜ \
72
+ β–Ό \
73
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” \
74
+ β”‚ HS2 β”‚ β”‚
75
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
76
+ β”‚ β”‚ β”‚
77
+ d b2 β”‚
78
+ β”‚ β”‚
79
+ β””β”€β”€β”¬β”€β”€β”€β”˜
80
+ β–Ό
81
+ β”Œβ”€β”€β”€β”€β”€β”€β”
82
+ β”‚ OR β”‚
83
+ β””β”€β”€β”€β”€β”€β”€β”˜
84
+ β”‚
85
+ β–Ό
86
+ bout
87
+ ```
88
+
89
+ ## Architecture
90
+
91
+ | Component | Per FS | Total (4 FSs) |
92
+ |-----------|--------|---------------|
93
+ | Neurons | 9 | 36 |
94
+ | Parameters | 27 | 108 |
95
+
96
+ **Layers: 16** (4 FSs Γ— 4 layers each)
97
+
98
+ ## Comparison Family
99
+
100
+ | Circuit | Condition | Implementation |
101
+ |---------|-----------|----------------|
102
+ | **LessThan** | a < b | borrow_out of (a - b) |
103
+ | Equal | a = b | NOR of all XOR bits |
104
+ | GreaterThan | a > b | NOT(a < b) AND NOT(a = b) |
105
+
106
+ ## Usage
107
+
108
+ ```python
109
+ from safetensors.torch import load_file
110
+
111
+ w = load_file('model.safetensors')
112
+
113
+ def less_than(a, b):
114
+ """a, b: 4-bit lists [a0,a1,a2,a3] (LSB first)
115
+ Returns: 1 if a < b, 0 otherwise"""
116
+ # See model.py for full implementation
117
+ pass
118
+
119
+ # Example: 5 < 10?
120
+ a = [1, 0, 1, 0] # 5 in LSB-first
121
+ b = [0, 1, 0, 1] # 10 in LSB-first
122
+ result = less_than(a, b) # Returns 1
123
+ ```
124
+
125
+ ## Files
126
+
127
+ ```
128
+ threshold-lessthan/
129
+ β”œβ”€β”€ model.safetensors
130
+ β”œβ”€β”€ model.py
131
+ β”œβ”€β”€ config.json
132
+ └── README.md
133
+ ```
134
+
135
+ ## License
136
+
137
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-lessthan",
3
+ "description": "4-bit less-than comparator as threshold circuit",
4
+ "inputs": 8,
5
+ "outputs": 1,
6
+ "neurons": 36,
7
+ "layers": 16,
8
+ "parameters": 108
9
+ }
model.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 half_sub(a, b, w, prefix):
8
+ inp = torch.tensor([float(a), float(b)])
9
+ l1 = (inp @ w[f'{prefix}.xor.layer1.weight'].T + w[f'{prefix}.xor.layer1.bias'] >= 0).float()
10
+ d = float((l1 @ w[f'{prefix}.xor.layer2.weight'].T + w[f'{prefix}.xor.layer2.bias'] >= 0).item())
11
+ borrow = float((inp @ w[f'{prefix}.borrow.weight'].T + w[f'{prefix}.borrow.bias'] >= 0).item())
12
+ return d, borrow
13
+
14
+ def full_sub(a, b, bin_in, w, prefix):
15
+ d1, b1 = half_sub(a, b, w, f'{prefix}.hs1')
16
+ d, b2 = half_sub(d1, bin_in, w, f'{prefix}.hs2')
17
+ bout = int((torch.tensor([b1, b2]) @ w[f'{prefix}.bout.weight'].T + w[f'{prefix}.bout.bias'] >= 0).item())
18
+ return int(d), bout
19
+
20
+ def less_than(a, b, weights):
21
+ """4-bit less-than comparator.
22
+ a, b: lists of 4 bits each (LSB first)
23
+ Returns: 1 if a < b, 0 otherwise
24
+ """
25
+ borrows = [0]
26
+ for i in range(4):
27
+ d, bout = full_sub(a[i], b[i], borrows[i], weights, f'fs{i}')
28
+ borrows.append(bout)
29
+ return borrows[4]
30
+
31
+ if __name__ == '__main__':
32
+ w = load_model()
33
+ print('4-bit LessThan Comparator')
34
+ print('a < b tests:')
35
+ tests = [(0, 0), (0, 1), (1, 0), (5, 10), (10, 5), (7, 7), (15, 0), (0, 15)]
36
+ for a_val, b_val in tests:
37
+ a = [(a_val >> i) & 1 for i in range(4)]
38
+ b = [(b_val >> i) & 1 for i in range(4)]
39
+ result = less_than(a, b, w)
40
+ 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:9221161e5cbde88f0223631b5ef8100ea0cea9cc8719fd809191181c5315cb27
3
+ size 4808