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

Extract sign bit of 8-bit number, magnitude 2

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