CharlesCNorton
commited on
Commit
·
5ce1fb7
0
Parent(s):
4-bit bit reversal, magnitude 8
Browse files- README.md +65 -0
- config.json +9 -0
- create_safetensors.py +53 -0
- model.py +22 -0
- model.safetensors +0 -0
README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# threshold-reverse4
|
| 11 |
+
|
| 12 |
+
4-bit bit reversal. Reverses the order of bits.
|
| 13 |
+
|
| 14 |
+
## Function
|
| 15 |
+
|
| 16 |
+
reverse4(a3, a2, a1, a0) = [a0, a1, a2, a3]
|
| 17 |
+
|
| 18 |
+
| Input | Output |
|
| 19 |
+
|-------|--------|
|
| 20 |
+
| 0001 | 1000 |
|
| 21 |
+
| 1000 | 0001 |
|
| 22 |
+
| 0110 | 0110 |
|
| 23 |
+
| 1010 | 0101 |
|
| 24 |
+
|
| 25 |
+
## Architecture
|
| 26 |
+
|
| 27 |
+
Single layer with 4 neurons, each copying one input bit to its reversed position.
|
| 28 |
+
|
| 29 |
+
| Output | Copies from | Weights [a3,a2,a1,a0] | Bias |
|
| 30 |
+
|--------|-------------|------------------------|------|
|
| 31 |
+
| y3 | a0 | [0, 0, 0, 1] | -1 |
|
| 32 |
+
| y2 | a1 | [0, 0, 1, 0] | -1 |
|
| 33 |
+
| y1 | a2 | [0, 1, 0, 0] | -1 |
|
| 34 |
+
| y0 | a3 | [1, 0, 0, 0] | -1 |
|
| 35 |
+
|
| 36 |
+
## Parameters
|
| 37 |
+
|
| 38 |
+
| | |
|
| 39 |
+
|---|---|
|
| 40 |
+
| Inputs | 4 |
|
| 41 |
+
| Outputs | 4 |
|
| 42 |
+
| Neurons | 4 |
|
| 43 |
+
| Layers | 1 |
|
| 44 |
+
| Parameters | 8 |
|
| 45 |
+
| Magnitude | 8 |
|
| 46 |
+
|
| 47 |
+
## Usage
|
| 48 |
+
|
| 49 |
+
```python
|
| 50 |
+
from safetensors.torch import load_file
|
| 51 |
+
import torch
|
| 52 |
+
|
| 53 |
+
w = load_file('model.safetensors')
|
| 54 |
+
|
| 55 |
+
def reverse4(a3, a2, a1, a0):
|
| 56 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 57 |
+
return [int((inp @ w[f'y{i}.weight'].T + w[f'y{i}.bias'] >= 0).item())
|
| 58 |
+
for i in [3, 2, 1, 0]]
|
| 59 |
+
|
| 60 |
+
print(reverse4(1, 0, 0, 0)) # [0, 0, 0, 1]
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
## License
|
| 64 |
+
|
| 65 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-reverse4",
|
| 3 |
+
"description": "4-bit bit reversal",
|
| 4 |
+
"inputs": 4,
|
| 5 |
+
"outputs": 4,
|
| 6 |
+
"neurons": 4,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 8
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {}
|
| 5 |
+
|
| 6 |
+
# Input order: a3, a2, a1, a0
|
| 7 |
+
# Output: y3=a0, y2=a1, y1=a2, y0=a3
|
| 8 |
+
# Each output copies from reversed position
|
| 9 |
+
|
| 10 |
+
# y3 = a0 (copy from position 3)
|
| 11 |
+
weights['y3.weight'] = torch.tensor([[0.0, 0.0, 0.0, 1.0]], dtype=torch.float32)
|
| 12 |
+
weights['y3.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 13 |
+
|
| 14 |
+
# y2 = a1 (copy from position 2)
|
| 15 |
+
weights['y2.weight'] = torch.tensor([[0.0, 0.0, 1.0, 0.0]], dtype=torch.float32)
|
| 16 |
+
weights['y2.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 17 |
+
|
| 18 |
+
# y1 = a2 (copy from position 1)
|
| 19 |
+
weights['y1.weight'] = torch.tensor([[0.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
|
| 20 |
+
weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 21 |
+
|
| 22 |
+
# y0 = a3 (copy from position 0)
|
| 23 |
+
weights['y0.weight'] = torch.tensor([[1.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
|
| 24 |
+
weights['y0.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 25 |
+
|
| 26 |
+
save_file(weights, 'model.safetensors')
|
| 27 |
+
|
| 28 |
+
# Verify
|
| 29 |
+
def reverse4(a3, a2, a1, a0):
|
| 30 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 31 |
+
y3 = int((inp @ weights['y3.weight'].T + weights['y3.bias'] >= 0).item())
|
| 32 |
+
y2 = int((inp @ weights['y2.weight'].T + weights['y2.bias'] >= 0).item())
|
| 33 |
+
y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
|
| 34 |
+
y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
|
| 35 |
+
return [y3, y2, y1, y0]
|
| 36 |
+
|
| 37 |
+
print("Verifying reverse4...")
|
| 38 |
+
errors = 0
|
| 39 |
+
for i in range(16):
|
| 40 |
+
a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 41 |
+
result = reverse4(a3, a2, a1, a0)
|
| 42 |
+
expected = [a0, a1, a2, a3]
|
| 43 |
+
if result != expected:
|
| 44 |
+
errors += 1
|
| 45 |
+
print(f"ERROR: {a3}{a2}{a1}{a0} -> {result}, expected {expected}")
|
| 46 |
+
|
| 47 |
+
if errors == 0:
|
| 48 |
+
print("All 16 test cases passed!")
|
| 49 |
+
else:
|
| 50 |
+
print(f"FAILED: {errors} errors")
|
| 51 |
+
|
| 52 |
+
mag = sum(t.abs().sum().item() for t in weights.values())
|
| 53 |
+
print(f"Magnitude: {mag:.0f}")
|
model.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 reverse4(a3, a2, a1, a0, w):
|
| 8 |
+
"""Reverse bit order of 4-bit input."""
|
| 9 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 10 |
+
y3 = int((inp @ w['y3.weight'].T + w['y3.bias'] >= 0).item())
|
| 11 |
+
y2 = int((inp @ w['y2.weight'].T + w['y2.bias'] >= 0).item())
|
| 12 |
+
y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
|
| 13 |
+
y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
|
| 14 |
+
return [y3, y2, y1, y0]
|
| 15 |
+
|
| 16 |
+
if __name__ == '__main__':
|
| 17 |
+
w = load_model()
|
| 18 |
+
print('reverse4 examples:')
|
| 19 |
+
for val in [0b0001, 0b1000, 0b0110, 0b1010, 0b1111]:
|
| 20 |
+
a3, a2, a1, a0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1
|
| 21 |
+
result = reverse4(a3, a2, a1, a0, w)
|
| 22 |
+
print(f' {a3}{a2}{a1}{a0} -> {"".join(map(str, result))}')
|
model.safetensors
ADDED
|
Binary file (592 Bytes). View file
|
|
|