CharlesCNorton
commited on
Commit
·
6dd42d9
0
Parent(s):
4-bit right shift, magnitude 7
Browse files- README.md +53 -0
- config.json +9 -0
- create_safetensors.py +30 -0
- model.py +12 -0
- model.safetensors +0 -0
README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# threshold-shiftright4
|
| 11 |
+
|
| 12 |
+
4-bit logical right shift by 1 bit.
|
| 13 |
+
|
| 14 |
+
## Function
|
| 15 |
+
|
| 16 |
+
shiftright4(a3, a2, a1, a0) = [0, a3, a2, a1]
|
| 17 |
+
|
| 18 |
+
LSB (a0) is discarded, 0 shifts in at MSB.
|
| 19 |
+
|
| 20 |
+
## Examples
|
| 21 |
+
|
| 22 |
+
| Input | Output |
|
| 23 |
+
|-------|--------|
|
| 24 |
+
| 0001 | 0000 |
|
| 25 |
+
| 0101 | 0010 |
|
| 26 |
+
| 1000 | 0100 |
|
| 27 |
+
| 1111 | 0111 |
|
| 28 |
+
|
| 29 |
+
## Architecture
|
| 30 |
+
|
| 31 |
+
Single layer with 4 neurons.
|
| 32 |
+
|
| 33 |
+
| Output | Source | Weights | Bias |
|
| 34 |
+
|--------|--------|---------|------|
|
| 35 |
+
| y3 | 0 | [0,0,0,0] | -1 |
|
| 36 |
+
| y2 | a3 | [1,0,0,0] | -1 |
|
| 37 |
+
| y1 | a2 | [0,1,0,0] | -1 |
|
| 38 |
+
| y0 | a1 | [0,0,1,0] | -1 |
|
| 39 |
+
|
| 40 |
+
## Parameters
|
| 41 |
+
|
| 42 |
+
| | |
|
| 43 |
+
|---|---|
|
| 44 |
+
| Inputs | 4 |
|
| 45 |
+
| Outputs | 4 |
|
| 46 |
+
| Neurons | 4 |
|
| 47 |
+
| Layers | 1 |
|
| 48 |
+
| Parameters | 8 |
|
| 49 |
+
| Magnitude | 7 |
|
| 50 |
+
|
| 51 |
+
## License
|
| 52 |
+
|
| 53 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-shiftright4",
|
| 3 |
+
"description": "4-bit right shift by 1",
|
| 4 |
+
"inputs": 4,
|
| 5 |
+
"outputs": 4,
|
| 6 |
+
"neurons": 4,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 8
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {}
|
| 5 |
+
|
| 6 |
+
# Input: [a3, a2, a1, a0], Output: [0, a3, a2, a1]
|
| 7 |
+
weights['y3.weight'] = torch.tensor([[0.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
|
| 8 |
+
weights['y3.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 9 |
+
weights['y2.weight'] = torch.tensor([[1.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
|
| 10 |
+
weights['y2.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 11 |
+
weights['y1.weight'] = torch.tensor([[0.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
|
| 12 |
+
weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 13 |
+
weights['y0.weight'] = torch.tensor([[0.0, 0.0, 1.0, 0.0]], dtype=torch.float32)
|
| 14 |
+
weights['y0.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 15 |
+
|
| 16 |
+
save_file(weights, 'model.safetensors')
|
| 17 |
+
|
| 18 |
+
print("Verifying shiftright4...")
|
| 19 |
+
errors = 0
|
| 20 |
+
for i in range(16):
|
| 21 |
+
a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 22 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 23 |
+
result = [int((inp @ weights[f'y{j}.weight'].T + weights[f'y{j}.bias'] >= 0).item()) for j in [3,2,1,0]]
|
| 24 |
+
expected = [0, a3, a2, a1]
|
| 25 |
+
if result != expected:
|
| 26 |
+
errors += 1
|
| 27 |
+
print(f"ERROR: {a3}{a2}{a1}{a0} -> {result}, expected {expected}")
|
| 28 |
+
if errors == 0:
|
| 29 |
+
print("All 16 test cases passed!")
|
| 30 |
+
print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
|
model.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import load_file
|
| 3 |
+
def load_model(path='model.safetensors'):
|
| 4 |
+
return load_file(path)
|
| 5 |
+
def shiftright4(a3, a2, a1, a0, w):
|
| 6 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 7 |
+
return [int((inp @ w[f'y{i}.weight'].T + w[f'y{i}.bias'] >= 0).item()) for i in [3,2,1,0]]
|
| 8 |
+
if __name__ == '__main__':
|
| 9 |
+
w = load_model()
|
| 10 |
+
for val in [0b0001, 0b0101, 0b1000, 0b1111]:
|
| 11 |
+
a3, a2, a1, a0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1
|
| 12 |
+
print(f'{a3}{a2}{a1}{a0} >> 1 = {"".join(map(str, shiftright4(a3, a2, a1, a0, w)))}')
|
model.safetensors
ADDED
|
Binary file (592 Bytes). View file
|
|
|