CharlesCNorton
commited on
Commit
·
0a8be94
0
Parent(s):
8-bit zero detector, magnitude 8
Browse files- README.md +37 -0
- config.json +9 -0
- create_safetensors.py +24 -0
- model.py +16 -0
- model.safetensors +0 -0
README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# threshold-iszero8
|
| 11 |
+
|
| 12 |
+
8-bit zero detector. Outputs 1 if all 8 input bits are zero.
|
| 13 |
+
|
| 14 |
+
## Function
|
| 15 |
+
|
| 16 |
+
iszero8(b0..b7) = 1 if all bits are 0, else 0
|
| 17 |
+
|
| 18 |
+
Equivalent to 8-input NOR gate.
|
| 19 |
+
|
| 20 |
+
## Architecture
|
| 21 |
+
|
| 22 |
+
Single neuron: weights [-1, -1, -1, -1, -1, -1, -1, -1], bias 0
|
| 23 |
+
|
| 24 |
+
## Parameters
|
| 25 |
+
|
| 26 |
+
| | |
|
| 27 |
+
|---|---|
|
| 28 |
+
| Inputs | 8 |
|
| 29 |
+
| Outputs | 1 |
|
| 30 |
+
| Neurons | 1 |
|
| 31 |
+
| Layers | 1 |
|
| 32 |
+
| Parameters | 9 |
|
| 33 |
+
| Magnitude | 8 |
|
| 34 |
+
|
| 35 |
+
## License
|
| 36 |
+
|
| 37 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-iszero8",
|
| 3 |
+
"description": "8-bit zero detector",
|
| 4 |
+
"inputs": 8,
|
| 5 |
+
"outputs": 1,
|
| 6 |
+
"neurons": 1,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 9
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {
|
| 5 |
+
'neuron.weight': torch.tensor([[-1.0] * 8], dtype=torch.float32),
|
| 6 |
+
'neuron.bias': torch.tensor([0.0], dtype=torch.float32)
|
| 7 |
+
}
|
| 8 |
+
save_file(weights, 'model.safetensors')
|
| 9 |
+
|
| 10 |
+
def iszero8(bits):
|
| 11 |
+
inp = torch.tensor([float(b) for b in bits])
|
| 12 |
+
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
|
| 13 |
+
|
| 14 |
+
print("Verifying iszero8...")
|
| 15 |
+
errors = 0
|
| 16 |
+
for i in range(256):
|
| 17 |
+
bits = [(i >> j) & 1 for j in range(8)]
|
| 18 |
+
result = iszero8(bits)
|
| 19 |
+
expected = 1 if i == 0 else 0
|
| 20 |
+
if result != expected:
|
| 21 |
+
errors += 1
|
| 22 |
+
if errors == 0:
|
| 23 |
+
print("All 256 test cases passed!")
|
| 24 |
+
print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
|
model.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 iszero8(bits, weights):
|
| 8 |
+
inp = torch.tensor([float(b) for b in bits])
|
| 9 |
+
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
|
| 10 |
+
|
| 11 |
+
if __name__ == '__main__':
|
| 12 |
+
w = load_model()
|
| 13 |
+
print('iszero8: outputs 1 only for input 00000000')
|
| 14 |
+
for i in [0, 1, 128, 255]:
|
| 15 |
+
bits = [(i >> j) & 1 for j in range(8)]
|
| 16 |
+
print(f' {i:08b} -> {iszero8(bits, w)}')
|
model.safetensors
ADDED
|
Binary file (180 Bytes). View file
|
|
|