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

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +28 -30
  2. config.json +5 -5
  3. model.py +9 -9
  4. model.safetensors +2 -2
README.md CHANGED
@@ -10,25 +10,25 @@ tags:
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
 
@@ -37,7 +37,7 @@ The circuit computes a - b using subtraction:
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
 
@@ -46,13 +46,11 @@ The difference bits (d0-d3) are computed but unused. Only the final borrow matte
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
 
@@ -88,12 +86,12 @@ All 256 input combinations verified correct.
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
 
@@ -101,7 +99,7 @@ All 256 input combinations verified correct.
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
 
@@ -111,14 +109,14 @@ from safetensors.torch import load_file
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
 
 
10
 
11
  # threshold-lessthan
12
 
13
+ 8-bit less-than 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
+ β”‚ β”‚ 0 β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
20
+ β””β”¬β”˜β”‚ β””β”¬β”˜ β””β”¬β”˜ β””β”¬β”˜ β””β”¬β”˜ β””β”¬β”˜ β””β”¬β”˜ β””β”¬β”˜
21
+ β–Ό β”‚ β–Ό β–Ό β–Ό β–Ό β–Ό β–Ό β–Ό
22
+ β”Œβ”€β”€β”€β”β”‚ β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β” β”Œβ”€β”€β”€β”
23
+ β”‚FS0│┴──│FS1│────│FS2│────│FS3│────│FS4│────│FS5│────│FS6│────│FS7│──►(a<b)
24
+ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜ β””β”€β”€β”€β”˜
25
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
26
+ β–Ό β–Ό β–Ό β–Ό β–Ό β–Ό β–Ό β–Ό
27
+ d0 d1 d2 d3 d4 d5 d6 d7
28
+ (difference bits unused - only final borrow matters)
29
  ```
30
 
31
+ Uses 8 cascaded full subtractors. The final borrow output indicates a < b.
32
 
33
  ## Mechanism
34
 
 
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-d7) are computed but unused. Only the final borrow matters.
41
 
42
  ## Truth Table (sample)
43
 
 
46
  | 0 | 0 | 0 |
47
  | 0 | 1 | 1 |
48
  | 1 | 0 | 0 |
49
+ | 127 | 128 | 1 |
50
+ | 255 | 0 | 0 |
51
+ | 0 | 255 | 1 |
52
+ | 100 | 100 | 0 |
53
+ | 99 | 100 | 1 |
 
 
54
 
55
  ## Full Subtractor Structure
56
 
 
86
 
87
  ## Architecture
88
 
89
+ | Component | Per FS | Total (8 FSs) |
90
  |-----------|--------|---------------|
91
+ | Neurons | 9 | 72 |
92
+ | Parameters | 27 | 216 |
93
 
94
+ **Layers: 32** (8 FSs Γ— 4 layers each)
95
 
96
  ## Comparison Family
97
 
 
99
  |---------|-----------|----------------|
100
  | **LessThan** | a < b | borrow_out of (a - b) |
101
  | Equal | a = b | NOR of all XOR bits |
102
+ | GreaterThan | a > b | LessThan(b, a) |
103
 
104
  ## Usage
105
 
 
109
  w = load_file('model.safetensors')
110
 
111
  def less_than(a, b):
112
+ """a, b: 8-bit lists [a0..a7] (LSB first)
113
  Returns: 1 if a < b, 0 otherwise"""
114
  # See model.py for full implementation
115
  pass
116
 
117
+ # Example: 99 < 100?
118
+ a = [(99 >> i) & 1 for i in range(8)]
119
+ b = [(100 >> i) & 1 for i in range(8)]
120
  result = less_than(a, b) # Returns 1
121
  ```
122
 
config.json CHANGED
@@ -1,9 +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
  }
 
1
  {
2
  "name": "threshold-lessthan",
3
+ "description": "8-bit less-than comparator as threshold circuit",
4
+ "inputs": 16,
5
  "outputs": 1,
6
+ "neurons": 72,
7
+ "layers": 32,
8
+ "parameters": 216
9
  }
model.py CHANGED
@@ -18,23 +18,23 @@ def full_sub(a, b, bin_in, w, prefix):
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}')
 
18
  return int(d), bout
19
 
20
  def less_than(a, b, weights):
21
+ """8-bit less-than comparator.
22
+ a, b: lists of 8 bits each (LSB first)
23
  Returns: 1 if a < b, 0 otherwise
24
  """
25
  borrows = [0]
26
+ for i in range(8):
27
  d, bout = full_sub(a[i], b[i], borrows[i], weights, f'fs{i}')
28
  borrows.append(bout)
29
+ return borrows[8]
30
 
31
  if __name__ == '__main__':
32
  w = load_model()
33
+ print('8-bit LessThan Comparator')
34
  print('a < b tests:')
35
+ tests = [(0, 0), (0, 1), (1, 0), (127, 128), (255, 0), (0, 255), (100, 100), (99, 100)]
36
  for a_val, b_val in tests:
37
+ a = [(a_val >> i) & 1 for i in range(8)]
38
+ b = [(b_val >> i) & 1 for i in range(8)]
39
  result = less_than(a, b, w)
40
+ 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:9221161e5cbde88f0223631b5ef8100ea0cea9cc8719fd809191181c5315cb27
3
- size 4808
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bfb1c6effebca579db177baa9ca4590908758f0dd15dba0e3c83d01c8e00972a
3
+ size 9632