Upload folder using huggingface_hub
Browse files- README.md +28 -30
- config.json +5 -5
- model.py +9 -9
- model.safetensors +2 -2
README.md
CHANGED
|
@@ -10,25 +10,25 @@ tags:
|
|
| 10 |
|
| 11 |
# threshold-lessthan
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
## Circuit
|
| 16 |
|
| 17 |
```
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
βΌ
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
```
|
| 30 |
|
| 31 |
-
Uses
|
| 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-
|
| 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 |
-
|
|
| 50 |
-
|
|
| 51 |
-
|
|
| 52 |
-
|
|
| 53 |
-
|
|
| 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 (
|
| 92 |
|-----------|--------|---------------|
|
| 93 |
-
| Neurons | 9 |
|
| 94 |
-
| Parameters | 27 |
|
| 95 |
|
| 96 |
-
**Layers:
|
| 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 |
|
| 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:
|
| 115 |
Returns: 1 if a < b, 0 otherwise"""
|
| 116 |
# See model.py for full implementation
|
| 117 |
pass
|
| 118 |
|
| 119 |
-
# Example:
|
| 120 |
-
a = [
|
| 121 |
-
b = [
|
| 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 |
-
"inputs":
|
| 5 |
"outputs": 1,
|
| 6 |
-
"neurons":
|
| 7 |
-
"layers":
|
| 8 |
-
"parameters":
|
| 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 |
-
"""
|
| 22 |
-
a, b: lists of
|
| 23 |
Returns: 1 if a < b, 0 otherwise
|
| 24 |
"""
|
| 25 |
borrows = [0]
|
| 26 |
-
for i in range(
|
| 27 |
d, bout = full_sub(a[i], b[i], borrows[i], weights, f'fs{i}')
|
| 28 |
borrows.append(bout)
|
| 29 |
-
return borrows[
|
| 30 |
|
| 31 |
if __name__ == '__main__':
|
| 32 |
w = load_model()
|
| 33 |
-
print('
|
| 34 |
print('a < b tests:')
|
| 35 |
-
tests = [(0, 0), (0, 1), (1, 0), (
|
| 36 |
for a_val, b_val in tests:
|
| 37 |
-
a = [(a_val >> i) & 1 for i in range(
|
| 38 |
-
b = [(b_val >> i) & 1 for i in range(
|
| 39 |
result = less_than(a, b, w)
|
| 40 |
-
print(f'{a_val:
|
|
|
|
| 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:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bfb1c6effebca579db177baa9ca4590908758f0dd15dba0e3c83d01c8e00972a
|
| 3 |
+
size 9632
|