CharlesCNorton commited on
Commit ·
6c0bc0c
1
Parent(s): 9cb1889
Check if 4-bit input equals 1, magnitude 5
Browse files- README.md +26 -10
- config.json +1 -1
- create_safetensors.py +9 -9
- model.py +9 -6
- model.safetensors +0 -0
README.md
CHANGED
|
@@ -9,25 +9,40 @@ tags:
|
|
| 9 |
|
| 10 |
# threshold-isone4
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
## Function
|
| 15 |
|
| 16 |
-
isone4(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
## Architecture
|
| 19 |
|
| 20 |
-
Single neuron
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
Fires when:
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
## Parameters
|
| 27 |
|
| 28 |
| | |
|
| 29 |
|---|---|
|
| 30 |
-
| Inputs | 4
|
| 31 |
| Outputs | 1 |
|
| 32 |
| Neurons | 1 |
|
| 33 |
| Layers | 1 |
|
|
@@ -42,12 +57,13 @@ import torch
|
|
| 42 |
|
| 43 |
w = load_file('model.safetensors')
|
| 44 |
|
| 45 |
-
def isone4(
|
| 46 |
-
inp = torch.tensor([float(
|
| 47 |
return int((inp @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item())
|
| 48 |
|
| 49 |
-
print(isone4(
|
| 50 |
-
print(isone4(0,
|
|
|
|
| 51 |
```
|
| 52 |
|
| 53 |
## License
|
|
|
|
| 9 |
|
| 10 |
# threshold-isone4
|
| 11 |
|
| 12 |
+
Check if 4-bit input equals 1 (binary 0001).
|
| 13 |
|
| 14 |
## Function
|
| 15 |
|
| 16 |
+
isone4(a3, a2, a1, a0) = 1 if input == 1, else 0
|
| 17 |
+
|
| 18 |
+
Where input = 8*a3 + 4*a2 + 2*a1 + a0
|
| 19 |
+
|
| 20 |
+
## Truth Table
|
| 21 |
+
|
| 22 |
+
| a3 | a2 | a1 | a0 | decimal | out |
|
| 23 |
+
|----|----|----|----|---------| ----|
|
| 24 |
+
| 0 | 0 | 0 | 0 | 0 | 0 |
|
| 25 |
+
| 0 | 0 | 0 | 1 | 1 | 1 |
|
| 26 |
+
| 0 | 0 | 1 | 0 | 2 | 0 |
|
| 27 |
+
| 0 | 0 | 1 | 1 | 3 | 0 |
|
| 28 |
+
| ... | ... | ... | ... | ... | 0 |
|
| 29 |
|
| 30 |
## Architecture
|
| 31 |
|
| 32 |
+
Single neuron pattern matcher for binary 0001:
|
| 33 |
+
|
| 34 |
+
- Weights: [-1, -1, -1, +1]
|
| 35 |
+
- Bias: -1
|
| 36 |
|
| 37 |
+
Fires when: -a3 - a2 - a1 + a0 - 1 >= 0
|
| 38 |
|
| 39 |
+
This requires a3=0, a2=0, a1=0, a0=1 (exactly the pattern 0001).
|
| 40 |
|
| 41 |
## Parameters
|
| 42 |
|
| 43 |
| | |
|
| 44 |
|---|---|
|
| 45 |
+
| Inputs | 4 |
|
| 46 |
| Outputs | 1 |
|
| 47 |
| Neurons | 1 |
|
| 48 |
| Layers | 1 |
|
|
|
|
| 57 |
|
| 58 |
w = load_file('model.safetensors')
|
| 59 |
|
| 60 |
+
def isone4(a3, a2, a1, a0):
|
| 61 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 62 |
return int((inp @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item())
|
| 63 |
|
| 64 |
+
print(isone4(0, 0, 0, 1)) # 1 (input = 1)
|
| 65 |
+
print(isone4(0, 0, 1, 0)) # 0 (input = 2)
|
| 66 |
+
print(isone4(0, 0, 0, 0)) # 0 (input = 0)
|
| 67 |
```
|
| 68 |
|
| 69 |
## License
|
config.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
{
|
| 2 |
"name": "threshold-isone4",
|
| 3 |
-
"description": "4-bit
|
| 4 |
"inputs": 4,
|
| 5 |
"outputs": 1,
|
| 6 |
"neurons": 1,
|
|
|
|
| 1 |
{
|
| 2 |
"name": "threshold-isone4",
|
| 3 |
+
"description": "Check if 4-bit input equals 1",
|
| 4 |
"inputs": 4,
|
| 5 |
"outputs": 1,
|
| 6 |
"neurons": 1,
|
create_safetensors.py
CHANGED
|
@@ -1,28 +1,28 @@
|
|
| 1 |
import torch
|
| 2 |
from safetensors.torch import save_file
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
#
|
| 6 |
-
# Need: b0=1, b1=0, b2=0, b3=0
|
| 7 |
weights = {
|
| 8 |
-
'neuron.weight': torch.tensor([[1.0, -1.0, -1.0,
|
| 9 |
'neuron.bias': torch.tensor([-1.0], dtype=torch.float32)
|
| 10 |
}
|
| 11 |
save_file(weights, 'model.safetensors')
|
| 12 |
|
| 13 |
-
def isone4(
|
| 14 |
-
inp = torch.tensor([float(
|
| 15 |
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
|
| 16 |
|
| 17 |
print("Verifying isone4...")
|
| 18 |
errors = 0
|
| 19 |
for i in range(16):
|
| 20 |
-
|
| 21 |
-
result = isone4(
|
| 22 |
expected = 1 if i == 1 else 0
|
| 23 |
if result != expected:
|
| 24 |
errors += 1
|
| 25 |
-
print(f"ERROR: {i
|
|
|
|
| 26 |
if errors == 0:
|
| 27 |
print("All 16 test cases passed!")
|
| 28 |
print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
|
|
|
|
| 1 |
import torch
|
| 2 |
from safetensors.torch import save_file
|
| 3 |
|
| 4 |
+
# Input order: a3, a2, a1, a0 (MSB to LSB)
|
| 5 |
+
# Output 1 if input == 0001 (decimal 1)
|
|
|
|
| 6 |
weights = {
|
| 7 |
+
'neuron.weight': torch.tensor([[-1.0, -1.0, -1.0, 1.0]], dtype=torch.float32),
|
| 8 |
'neuron.bias': torch.tensor([-1.0], dtype=torch.float32)
|
| 9 |
}
|
| 10 |
save_file(weights, 'model.safetensors')
|
| 11 |
|
| 12 |
+
def isone4(a3, a2, a1, a0):
|
| 13 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 14 |
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
|
| 15 |
|
| 16 |
print("Verifying isone4...")
|
| 17 |
errors = 0
|
| 18 |
for i in range(16):
|
| 19 |
+
a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 20 |
+
result = isone4(a3, a2, a1, a0)
|
| 21 |
expected = 1 if i == 1 else 0
|
| 22 |
if result != expected:
|
| 23 |
errors += 1
|
| 24 |
+
print(f"ERROR: {a3}{a2}{a1}{a0} (={i}) -> {result}, expected {expected}")
|
| 25 |
+
|
| 26 |
if errors == 0:
|
| 27 |
print("All 16 test cases passed!")
|
| 28 |
print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
|
model.py
CHANGED
|
@@ -4,13 +4,16 @@ from safetensors.torch import load_file
|
|
| 4 |
def load_model(path='model.safetensors'):
|
| 5 |
return load_file(path)
|
| 6 |
|
| 7 |
-
def isone4(
|
| 8 |
-
|
|
|
|
| 9 |
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
|
| 10 |
|
| 11 |
if __name__ == '__main__':
|
| 12 |
w = load_model()
|
| 13 |
-
print('isone4
|
| 14 |
-
for i in
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
| 4 |
def load_model(path='model.safetensors'):
|
| 5 |
return load_file(path)
|
| 6 |
|
| 7 |
+
def isone4(a3, a2, a1, a0, weights):
|
| 8 |
+
"""Returns 1 if 4-bit input equals 1 (0001), else 0"""
|
| 9 |
+
inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
|
| 10 |
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
|
| 11 |
|
| 12 |
if __name__ == '__main__':
|
| 13 |
w = load_model()
|
| 14 |
+
print('isone4 truth table:')
|
| 15 |
+
for i in range(16):
|
| 16 |
+
a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 17 |
+
result = isone4(a3, a2, a1, a0, w)
|
| 18 |
+
marker = ' <-- ONE' if result else ''
|
| 19 |
+
print(f' {a3}{a2}{a1}{a0} (={i:2d}) -> {result}{marker}')
|
model.safetensors
CHANGED
|
Binary files a/model.safetensors and b/model.safetensors differ
|
|
|