CharlesCNorton
commited on
Commit
·
02c6d0e
0
Parent(s):
4-to-2 priority encoder, magnitude 10
Browse files- README.md +70 -0
- config.json +9 -0
- create_safetensors.py +45 -0
- model.py +20 -0
- model.safetensors +0 -0
README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# threshold-4to2encoder
|
| 11 |
+
|
| 12 |
+
4-to-2 priority encoder. Outputs binary index of highest-priority set input.
|
| 13 |
+
|
| 14 |
+
## Function
|
| 15 |
+
|
| 16 |
+
encode(I3, I2, I1, I0) -> (Y1, Y0)
|
| 17 |
+
|
| 18 |
+
Priority: I3 > I2 > I1 > I0
|
| 19 |
+
|
| 20 |
+
## Truth Table (selected rows)
|
| 21 |
+
|
| 22 |
+
| I3 | I2 | I1 | I0 | Y1 | Y0 | Index |
|
| 23 |
+
|----|----|----|----|----|----|-------|
|
| 24 |
+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
|
| 25 |
+
| 0 | 0 | 0 | 1 | 0 | 0 | 0 |
|
| 26 |
+
| 0 | 0 | 1 | x | 0 | 1 | 1 |
|
| 27 |
+
| 0 | 1 | x | x | 1 | 0 | 2 |
|
| 28 |
+
| 1 | x | x | x | 1 | 1 | 3 |
|
| 29 |
+
|
| 30 |
+
## Architecture
|
| 31 |
+
|
| 32 |
+
Single layer with 2 neurons:
|
| 33 |
+
|
| 34 |
+
| Output | Weights [I3, I2, I1, I0] | Bias | Function |
|
| 35 |
+
|--------|--------------------------|------|----------|
|
| 36 |
+
| Y1 | [1, 1, 0, 0] | -1 | I3 OR I2 |
|
| 37 |
+
| Y0 | [3, -2, 1, 0] | -1 | I3 OR (NOT I2 AND I1) |
|
| 38 |
+
|
| 39 |
+
## Parameters
|
| 40 |
+
|
| 41 |
+
| | |
|
| 42 |
+
|---|---|
|
| 43 |
+
| Inputs | 4 |
|
| 44 |
+
| Outputs | 2 |
|
| 45 |
+
| Neurons | 2 |
|
| 46 |
+
| Layers | 1 |
|
| 47 |
+
| Parameters | 10 |
|
| 48 |
+
| Magnitude | 10 |
|
| 49 |
+
|
| 50 |
+
## Usage
|
| 51 |
+
|
| 52 |
+
```python
|
| 53 |
+
from safetensors.torch import load_file
|
| 54 |
+
import torch
|
| 55 |
+
|
| 56 |
+
w = load_file('model.safetensors')
|
| 57 |
+
|
| 58 |
+
def encode4to2(i3, i2, i1, i0):
|
| 59 |
+
inp = torch.tensor([float(i3), float(i2), float(i1), float(i0)])
|
| 60 |
+
y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
|
| 61 |
+
y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
|
| 62 |
+
return y1, y0
|
| 63 |
+
|
| 64 |
+
print(encode4to2(0, 1, 1, 0)) # (1, 0) -> index 2 (I2 is highest)
|
| 65 |
+
print(encode4to2(1, 0, 0, 1)) # (1, 1) -> index 3 (I3 is highest)
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
## License
|
| 69 |
+
|
| 70 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-4to2encoder",
|
| 3 |
+
"description": "4-to-2 priority encoder",
|
| 4 |
+
"inputs": 4,
|
| 5 |
+
"outputs": 2,
|
| 6 |
+
"neurons": 2,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 10
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
# Priority encoder: outputs binary index of highest-set input
|
| 5 |
+
# Inputs: I3, I2, I1, I0 (I3 is highest priority)
|
| 6 |
+
# Outputs: Y1, Y0 (binary encoding)
|
| 7 |
+
|
| 8 |
+
weights = {}
|
| 9 |
+
|
| 10 |
+
# Y1: fires when I3 or I2 is set (highest bit is 2 or 3)
|
| 11 |
+
weights['y1.weight'] = torch.tensor([[1.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
|
| 12 |
+
weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 13 |
+
|
| 14 |
+
# Y0: fires when I3 is set, OR when I2 is NOT set but I1 is set
|
| 15 |
+
# This gives output bit 0 for indices 1 and 3
|
| 16 |
+
weights['y0.weight'] = torch.tensor([[3.0, -2.0, 1.0, 0.0]], dtype=torch.float32)
|
| 17 |
+
weights['y0.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 18 |
+
|
| 19 |
+
save_file(weights, 'model.safetensors')
|
| 20 |
+
|
| 21 |
+
def encode4to2(i3, i2, i1, i0):
|
| 22 |
+
inp = torch.tensor([float(i3), float(i2), float(i1), float(i0)])
|
| 23 |
+
y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
|
| 24 |
+
y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
|
| 25 |
+
return y1, y0
|
| 26 |
+
|
| 27 |
+
print("Verifying 4to2encoder...")
|
| 28 |
+
errors = 0
|
| 29 |
+
for val in range(16):
|
| 30 |
+
i3, i2, i1, i0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1
|
| 31 |
+
y1, y0 = encode4to2(i3, i2, i1, i0)
|
| 32 |
+
|
| 33 |
+
# Expected: binary of highest set bit position
|
| 34 |
+
if i3: expected = (1, 1) # 3
|
| 35 |
+
elif i2: expected = (1, 0) # 2
|
| 36 |
+
elif i1: expected = (0, 1) # 1
|
| 37 |
+
else: expected = (0, 0) # 0 or none
|
| 38 |
+
|
| 39 |
+
if (y1, y0) != expected:
|
| 40 |
+
errors += 1
|
| 41 |
+
print(f"ERROR: I={i3}{i2}{i1}{i0} -> ({y1},{y0}), expected {expected}")
|
| 42 |
+
|
| 43 |
+
if errors == 0:
|
| 44 |
+
print("All 16 test cases passed!")
|
| 45 |
+
print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
|
model.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 encode4to2(i3, i2, i1, i0, weights):
|
| 8 |
+
"""Priority encoder: returns binary index of highest-set input."""
|
| 9 |
+
inp = torch.tensor([float(i3), float(i2), float(i1), float(i0)])
|
| 10 |
+
y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
|
| 11 |
+
y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
|
| 12 |
+
return y1, y0
|
| 13 |
+
|
| 14 |
+
if __name__ == '__main__':
|
| 15 |
+
w = load_model()
|
| 16 |
+
print('4-to-2 Priority Encoder:')
|
| 17 |
+
for val in range(16):
|
| 18 |
+
i3, i2, i1, i0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1
|
| 19 |
+
y1, y0 = encode4to2(i3, i2, i1, i0, w)
|
| 20 |
+
print(f' {i3}{i2}{i1}{i0} -> {y1}{y0} (={2*y1+y0})')
|
model.safetensors
ADDED
|
Binary file (304 Bytes). View file
|
|
|