CharlesCNorton commited on
Commit
ccee2d3
·
0 Parent(s):

Extract sign bit of 4-bit number, magnitude 2

Browse files
Files changed (5) hide show
  1. README.md +69 -0
  2. config.json +9 -0
  3. create_safetensors.py +29 -0
  4. model.py +19 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-signbit4
11
+
12
+ Extract sign bit (MSB) of 4-bit number.
13
+
14
+ ## Function
15
+
16
+ signbit4(a3, a2, a1, a0) = a3
17
+
18
+ In 2's complement representation:
19
+ - Output 0: non-negative (0 to 7)
20
+ - Output 1: negative (-8 to -1)
21
+
22
+ ## Truth Table
23
+
24
+ | a3 | a2 | a1 | a0 | unsigned | signed | signbit |
25
+ |----|----|----|----|---------:|-------:|:-------:|
26
+ | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
27
+ | 0 | 1 | 1 | 1 | 7 | +7 | 0 |
28
+ | 1 | 0 | 0 | 0 | 8 | -8 | 1 |
29
+ | 1 | 1 | 1 | 1 | 15 | -1 | 1 |
30
+
31
+ ## Architecture
32
+
33
+ Single neuron extracting MSB:
34
+
35
+ - Weights: [1, 0, 0, 0]
36
+ - Bias: -1
37
+
38
+ Fires when: a3 - 1 >= 0, i.e., a3 >= 1
39
+
40
+ ## Parameters
41
+
42
+ | | |
43
+ |---|---|
44
+ | Inputs | 4 |
45
+ | Outputs | 1 |
46
+ | Neurons | 1 |
47
+ | Layers | 1 |
48
+ | Parameters | 5 |
49
+ | Magnitude | 2 |
50
+
51
+ ## Usage
52
+
53
+ ```python
54
+ from safetensors.torch import load_file
55
+ import torch
56
+
57
+ w = load_file('model.safetensors')
58
+
59
+ def signbit4(a3, a2, a1, a0):
60
+ inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
61
+ return int((inp @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item())
62
+
63
+ print(signbit4(0, 1, 1, 1)) # 0 (value = +7)
64
+ print(signbit4(1, 0, 0, 0)) # 1 (value = -8)
65
+ ```
66
+
67
+ ## License
68
+
69
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-signbit4",
3
+ "description": "Extract sign bit (MSB) of 4-bit number",
4
+ "inputs": 4,
5
+ "outputs": 1,
6
+ "neurons": 1,
7
+ "layers": 1,
8
+ "parameters": 5
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ # Input order: a3, a2, a1, a0 (MSB to LSB)
5
+ # Output sign bit (MSB = a3)
6
+ # In 2's complement, a3=1 means negative
7
+ weights = {
8
+ 'neuron.weight': torch.tensor([[1.0, 0.0, 0.0, 0.0]], dtype=torch.float32),
9
+ 'neuron.bias': torch.tensor([-1.0], dtype=torch.float32)
10
+ }
11
+ save_file(weights, 'model.safetensors')
12
+
13
+ def signbit4(a3, a2, a1, a0):
14
+ inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
15
+ return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
16
+
17
+ print("Verifying signbit4...")
18
+ errors = 0
19
+ for i in range(16):
20
+ a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
21
+ result = signbit4(a3, a2, a1, a0)
22
+ expected = a3 # sign bit is MSB
23
+ if result != expected:
24
+ errors += 1
25
+ print(f"ERROR: {a3}{a2}{a1}{a0} -> {result}, expected {expected}")
26
+
27
+ if errors == 0:
28
+ print("All 16 test cases passed!")
29
+ print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
model.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 signbit4(a3, a2, a1, a0, weights):
8
+ """Returns sign bit (MSB) of 4-bit number. 1 = negative in 2's complement."""
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('signbit4 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 = signbit4(a3, a2, a1, a0, w)
18
+ signed_val = i if i < 8 else i - 16
19
+ print(f' {a3}{a2}{a1}{a0} (unsigned={i:2d}, signed={signed_val:+3d}) -> sign={result}')
model.safetensors ADDED
Binary file (164 Bytes). View file