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