CharlesCNorton commited on
Commit ·
4b2d5f9
0
Parent(s):
4-bit bitwise NOT, magnitude 4
Browse files- README.md +71 -0
- config.json +9 -0
- create_safetensors.py +41 -0
- model.py +24 -0
- model.safetensors +0 -0
README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# threshold-negator4bit
|
| 11 |
+
|
| 12 |
+
4-bit bitwise NOT (one's complement negation).
|
| 13 |
+
|
| 14 |
+
## Function
|
| 15 |
+
|
| 16 |
+
negator4bit(a3, a2, a1, a0) = [NOT(a3), NOT(a2), NOT(a1), NOT(a0)]
|
| 17 |
+
|
| 18 |
+
Inverts each bit independently.
|
| 19 |
+
|
| 20 |
+
## Truth Table (selected rows)
|
| 21 |
+
|
| 22 |
+
| Input | Output |
|
| 23 |
+
|-------|--------|
|
| 24 |
+
| 0000 | 1111 |
|
| 25 |
+
| 0001 | 1110 |
|
| 26 |
+
| 0101 | 1010 |
|
| 27 |
+
| 1111 | 0000 |
|
| 28 |
+
|
| 29 |
+
## Architecture
|
| 30 |
+
|
| 31 |
+
Single layer with 4 independent NOT neurons.
|
| 32 |
+
|
| 33 |
+
| Output | Weight on input | Bias |
|
| 34 |
+
|--------|-----------------|------|
|
| 35 |
+
| y3 | a3: -1 | 0 |
|
| 36 |
+
| y2 | a2: -1 | 0 |
|
| 37 |
+
| y1 | a1: -1 | 0 |
|
| 38 |
+
| y0 | a0: -1 | 0 |
|
| 39 |
+
|
| 40 |
+
Each neuron fires when its input is 0.
|
| 41 |
+
|
| 42 |
+
## Parameters
|
| 43 |
+
|
| 44 |
+
| | |
|
| 45 |
+
|---|---|
|
| 46 |
+
| Inputs | 4 |
|
| 47 |
+
| Outputs | 4 |
|
| 48 |
+
| Neurons | 4 |
|
| 49 |
+
| Layers | 1 |
|
| 50 |
+
| Parameters | 8 |
|
| 51 |
+
| Magnitude | 4 |
|
| 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 negator4(a3, a2, a1, a0):
|
| 62 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 63 |
+
return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0)
|
| 64 |
+
for i in range(4)]
|
| 65 |
+
|
| 66 |
+
print(negator4(0, 1, 0, 1)) # [1, 0, 1, 0]
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
## License
|
| 70 |
+
|
| 71 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-negator4bit",
|
| 3 |
+
"description": "4-bit bitwise NOT (one's complement)",
|
| 4 |
+
"inputs": 4,
|
| 5 |
+
"outputs": 4,
|
| 6 |
+
"neurons": 4,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 8
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {}
|
| 5 |
+
|
| 6 |
+
# Each output yi = NOT(ai)
|
| 7 |
+
# NOT(x): weight -1, bias 0, fires when x = 0
|
| 8 |
+
for i in range(4):
|
| 9 |
+
w = [0.0, 0.0, 0.0, 0.0]
|
| 10 |
+
w[i] = -1.0
|
| 11 |
+
weights[f'y{i}.weight'] = torch.tensor([w], dtype=torch.float32)
|
| 12 |
+
weights[f'y{i}.bias'] = torch.tensor([0.0], dtype=torch.float32)
|
| 13 |
+
|
| 14 |
+
save_file(weights, 'model.safetensors')
|
| 15 |
+
|
| 16 |
+
# Verify
|
| 17 |
+
def negator4(a3, a2, a1, a0):
|
| 18 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 19 |
+
outputs = []
|
| 20 |
+
for i in range(4):
|
| 21 |
+
y = int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0)
|
| 22 |
+
outputs.append(y)
|
| 23 |
+
return outputs
|
| 24 |
+
|
| 25 |
+
print("Verifying negator4bit...")
|
| 26 |
+
errors = 0
|
| 27 |
+
for i in range(16):
|
| 28 |
+
a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 29 |
+
result = negator4(a3, a2, a1, a0)
|
| 30 |
+
expected = [1 - a3, 1 - a2, 1 - a1, 1 - a0]
|
| 31 |
+
if result != expected:
|
| 32 |
+
errors += 1
|
| 33 |
+
print(f"ERROR: {a3}{a2}{a1}{a0} -> {result}, expected {expected}")
|
| 34 |
+
|
| 35 |
+
if errors == 0:
|
| 36 |
+
print("All 16 test cases passed!")
|
| 37 |
+
else:
|
| 38 |
+
print(f"FAILED: {errors} errors")
|
| 39 |
+
|
| 40 |
+
mag = sum(t.abs().sum().item() for t in weights.values())
|
| 41 |
+
print(f"Magnitude: {mag:.0f}")
|
model.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 negator4(a3, a2, a1, a0, weights):
|
| 8 |
+
"""Bitwise NOT of 4-bit input (one's complement)."""
|
| 9 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 10 |
+
outputs = []
|
| 11 |
+
for i in range(4):
|
| 12 |
+
y = int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0)
|
| 13 |
+
outputs.append(y)
|
| 14 |
+
return outputs
|
| 15 |
+
|
| 16 |
+
if __name__ == '__main__':
|
| 17 |
+
w = load_model()
|
| 18 |
+
print('negator4bit truth table:')
|
| 19 |
+
print('input | output')
|
| 20 |
+
print('------+-------')
|
| 21 |
+
for i in range(16):
|
| 22 |
+
a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 23 |
+
result = negator4(a3, a2, a1, a0, w)
|
| 24 |
+
print(f'{a3}{a2}{a1}{a0} | {"".join(map(str, result))}')
|
model.safetensors
ADDED
|
Binary file (592 Bytes). View file
|
|
|