Upload folder using huggingface_hub
Browse files- README.md +108 -0
- config.json +9 -0
- create_safetensors.py +47 -0
- model.py +21 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- prefix
|
| 9 |
+
- parallel
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# threshold-prefix-or
|
| 13 |
+
|
| 14 |
+
4-bit parallel prefix OR operation. Computes running OR from MSB to each position. Used in leading-one detection and any-ones-above detection.
|
| 15 |
+
|
| 16 |
+
## Circuit
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
x3 x2 x1 x0
|
| 20 |
+
│ │ │ │
|
| 21 |
+
▼ ▼ ▼ ▼
|
| 22 |
+
┌───┐ ┌───┐ ┌───┐ ┌───┐
|
| 23 |
+
│y3 │ │y2 │ │y1 │ │y0 │
|
| 24 |
+
│>=1│ │>=1│ │>=1│ │>=1│
|
| 25 |
+
└───┘ └───┘ └───┘ └───┘
|
| 26 |
+
│ │ │ │
|
| 27 |
+
▼ ▼ ▼ ▼
|
| 28 |
+
(x3) (x3|x2) (x3|x2|x1) (all)
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
## Function
|
| 32 |
+
|
| 33 |
+
```
|
| 34 |
+
prefix_or(x3, x2, x1, x0) -> (y3, y2, y1, y0)
|
| 35 |
+
|
| 36 |
+
y3 = x3
|
| 37 |
+
y2 = x3 OR x2
|
| 38 |
+
y1 = x3 OR x2 OR x1
|
| 39 |
+
y0 = x3 OR x2 OR x1 OR x0
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
Each output yi is the OR of all inputs from x3 down to xi.
|
| 43 |
+
|
| 44 |
+
## Truth Table (selected)
|
| 45 |
+
|
| 46 |
+
| x3 x2 x1 x0 | y3 y2 y1 y0 | Meaning |
|
| 47 |
+
|-------------|-------------|---------|
|
| 48 |
+
| 0 0 0 0 | 0 0 0 0 | All zeros |
|
| 49 |
+
| 0 0 0 1 | 0 0 0 1 | Only LSB set |
|
| 50 |
+
| 0 0 1 0 | 0 0 1 1 | First one at pos 1 |
|
| 51 |
+
| 0 1 0 0 | 0 1 1 1 | First one at pos 2 |
|
| 52 |
+
| 1 0 0 0 | 1 1 1 1 | First one at MSB |
|
| 53 |
+
| 1 1 1 1 | 1 1 1 1 | All ones |
|
| 54 |
+
| 1 0 1 0 | 1 1 1 1 | Mixed pattern |
|
| 55 |
+
|
| 56 |
+
## Mechanism
|
| 57 |
+
|
| 58 |
+
**Single-layer parallel implementation:**
|
| 59 |
+
|
| 60 |
+
| Output | Condition | Weights | Bias |
|
| 61 |
+
|--------|-----------|---------|------|
|
| 62 |
+
| y3 | x3 >= 1 | [1,0,0,0] | -1 |
|
| 63 |
+
| y2 | x3 + x2 >= 1 | [1,1,0,0] | -1 |
|
| 64 |
+
| y1 | x3 + x2 + x1 >= 1 | [1,1,1,0] | -1 |
|
| 65 |
+
| y0 | x3 + x2 + x1 + x0 >= 1 | [1,1,1,1] | -1 |
|
| 66 |
+
|
| 67 |
+
All use bias -1 (fires when at least one relevant input is 1).
|
| 68 |
+
|
| 69 |
+
## Parameters
|
| 70 |
+
|
| 71 |
+
| | |
|
| 72 |
+
|---|---|
|
| 73 |
+
| Inputs | 4 |
|
| 74 |
+
| Outputs | 4 |
|
| 75 |
+
| Neurons | 4 |
|
| 76 |
+
| Layers | 1 |
|
| 77 |
+
| Parameters | 20 |
|
| 78 |
+
| Magnitude | 14 |
|
| 79 |
+
|
| 80 |
+
## Applications
|
| 81 |
+
|
| 82 |
+
- **Leading-one detection:** Transition from 0→1 in output marks MSB position
|
| 83 |
+
- **Any-bit-set prefix:** y_i = 1 means some bit from MSB to position i is set
|
| 84 |
+
- **Carry kill detection:** In adders, prefix-OR of "kill" signals
|
| 85 |
+
|
| 86 |
+
## Usage
|
| 87 |
+
|
| 88 |
+
```python
|
| 89 |
+
from safetensors.torch import load_file
|
| 90 |
+
import torch
|
| 91 |
+
|
| 92 |
+
w = load_file('model.safetensors')
|
| 93 |
+
|
| 94 |
+
def prefix_or(x3, x2, x1, x0):
|
| 95 |
+
inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
|
| 96 |
+
y3 = int((inp @ w['y3.weight'].T + w['y3.bias'] >= 0).item())
|
| 97 |
+
y2 = int((inp @ w['y2.weight'].T + w['y2.bias'] >= 0).item())
|
| 98 |
+
y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
|
| 99 |
+
y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
|
| 100 |
+
return y3, y2, y1, y0
|
| 101 |
+
|
| 102 |
+
print(prefix_or(0, 0, 1, 0)) # (0, 0, 1, 1)
|
| 103 |
+
print(prefix_or(1, 0, 0, 0)) # (1, 1, 1, 1)
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
## License
|
| 107 |
+
|
| 108 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-prefix-or",
|
| 3 |
+
"description": "4-bit parallel prefix OR operation",
|
| 4 |
+
"inputs": 4,
|
| 5 |
+
"outputs": 4,
|
| 6 |
+
"neurons": 4,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 20
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {}
|
| 5 |
+
|
| 6 |
+
# y3 = x3 (at least 1 of [x3])
|
| 7 |
+
weights['y3.weight'] = torch.tensor([[1.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
|
| 8 |
+
weights['y3.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 9 |
+
|
| 10 |
+
# y2 = x3 OR x2 (at least 1 of [x3,x2])
|
| 11 |
+
weights['y2.weight'] = torch.tensor([[1.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
|
| 12 |
+
weights['y2.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 13 |
+
|
| 14 |
+
# y1 = x3 OR x2 OR x1
|
| 15 |
+
weights['y1.weight'] = torch.tensor([[1.0, 1.0, 1.0, 0.0]], dtype=torch.float32)
|
| 16 |
+
weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 17 |
+
|
| 18 |
+
# y0 = x3 OR x2 OR x1 OR x0
|
| 19 |
+
weights['y0.weight'] = torch.tensor([[1.0, 1.0, 1.0, 1.0]], dtype=torch.float32)
|
| 20 |
+
weights['y0.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 21 |
+
|
| 22 |
+
save_file(weights, 'model.safetensors')
|
| 23 |
+
|
| 24 |
+
def prefix_or(x3, x2, x1, x0):
|
| 25 |
+
inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
|
| 26 |
+
y3 = int((inp @ weights['y3.weight'].T + weights['y3.bias'] >= 0).item())
|
| 27 |
+
y2 = int((inp @ weights['y2.weight'].T + weights['y2.bias'] >= 0).item())
|
| 28 |
+
y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
|
| 29 |
+
y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
|
| 30 |
+
return y3, y2, y1, y0
|
| 31 |
+
|
| 32 |
+
print("Verifying prefix-or...")
|
| 33 |
+
errors = 0
|
| 34 |
+
for i in range(16):
|
| 35 |
+
x3, x2, x1, x0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 36 |
+
y3, y2, y1, y0 = prefix_or(x3, x2, x1, x0)
|
| 37 |
+
exp_y3 = x3
|
| 38 |
+
exp_y2 = x3 | x2
|
| 39 |
+
exp_y1 = x3 | x2 | x1
|
| 40 |
+
exp_y0 = x3 | x2 | x1 | x0
|
| 41 |
+
if (y3, y2, y1, y0) != (exp_y3, exp_y2, exp_y1, exp_y0):
|
| 42 |
+
errors += 1
|
| 43 |
+
if errors == 0:
|
| 44 |
+
print("All 16 test cases passed!")
|
| 45 |
+
else:
|
| 46 |
+
print(f"FAILED: {errors} errors")
|
| 47 |
+
print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
|
model.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 prefix_or(x3, x2, x1, x0, w):
|
| 8 |
+
inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
|
| 9 |
+
y3 = int((inp @ w['y3.weight'].T + w['y3.bias'] >= 0).item())
|
| 10 |
+
y2 = int((inp @ w['y2.weight'].T + w['y2.bias'] >= 0).item())
|
| 11 |
+
y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
|
| 12 |
+
y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
|
| 13 |
+
return y3, y2, y1, y0
|
| 14 |
+
|
| 15 |
+
if __name__ == '__main__':
|
| 16 |
+
w = load_model()
|
| 17 |
+
print('Prefix-OR selected tests:')
|
| 18 |
+
for i in [0b0000, 0b0001, 0b0010, 0b0100, 0b1000, 0b1111]:
|
| 19 |
+
x3, x2, x1, x0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 20 |
+
y3, y2, y1, y0 = prefix_or(x3, x2, x1, x0, w)
|
| 21 |
+
print(f'{x3}{x2}{x1}{x0} -> {y3}{y2}{y1}{y0}')
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c6b9ad2e7e465c9072afabbb5b75d29573cf77ff1f53f100436888b0477f3633
|
| 3 |
+
size 592
|