Upload folder using huggingface_hub
Browse files- README.md +137 -0
- config.json +9 -0
- model.py +40 -0
- 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
|