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

Signed addition overflow detector, magnitude 12

Browse files
Files changed (5) hide show
  1. README.md +81 -0
  2. config.json +9 -0
  3. create_safetensors.py +43 -0
  4. model.py +28 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-overflowdetect
11
+
12
+ Detect signed addition overflow from sign bits of operands and result.
13
+
14
+ ## Function
15
+
16
+ overflow(a_sign, b_sign, sum_sign) = 1 if overflow occurred
17
+
18
+ In 2's complement addition, overflow occurs when:
19
+ - Two positive numbers produce a negative result
20
+ - Two negative numbers produce a positive result
21
+
22
+ ## Truth Table
23
+
24
+ | a_sign | b_sign | sum_sign | overflow | meaning |
25
+ |:------:|:------:|:--------:|:--------:|---------|
26
+ | 0 | 0 | 0 | 0 | pos + pos = pos (ok) |
27
+ | 0 | 0 | 1 | 1 | pos + pos = neg (OVERFLOW) |
28
+ | 0 | 1 | 0 | 0 | pos + neg = pos (ok) |
29
+ | 0 | 1 | 1 | 0 | pos + neg = neg (ok) |
30
+ | 1 | 0 | 0 | 0 | neg + pos = pos (ok) |
31
+ | 1 | 0 | 1 | 0 | neg + pos = neg (ok) |
32
+ | 1 | 1 | 0 | 1 | neg + neg = pos (OVERFLOW) |
33
+ | 1 | 1 | 1 | 0 | neg + neg = neg (ok) |
34
+
35
+ ## Architecture
36
+
37
+ 2-layer circuit detecting both overflow cases:
38
+
39
+ **Layer 1:**
40
+ - N1: detects positive overflow (0,0,1) - weights [-1,-1,+1], bias -1
41
+ - N2: detects negative overflow (1,1,0) - weights [+1,+1,-1], bias -2
42
+
43
+ **Layer 2:**
44
+ - OR gate: weights [1,1], bias -1
45
+
46
+ ## Parameters
47
+
48
+ | | |
49
+ |---|---|
50
+ | Inputs | 3 |
51
+ | Outputs | 1 |
52
+ | Neurons | 3 |
53
+ | Layers | 2 |
54
+ | Parameters | 11 |
55
+ | Magnitude | 12 |
56
+
57
+ ## Usage
58
+
59
+ ```python
60
+ from safetensors.torch import load_file
61
+ import torch
62
+
63
+ w = load_file('model.safetensors')
64
+
65
+ def overflow_detect(a_sign, b_sign, sum_sign):
66
+ inp = torch.tensor([float(a_sign), float(b_sign), float(sum_sign)])
67
+ n1 = int((inp @ w['layer1.n1.weight'].T + w['layer1.n1.bias'] >= 0).item())
68
+ n2 = int((inp @ w['layer1.n2.weight'].T + w['layer1.n2.bias'] >= 0).item())
69
+ hidden = torch.tensor([float(n1), float(n2)])
70
+ return int((hidden @ w['layer2.weight'].T + w['layer2.bias'] >= 0).item())
71
+
72
+ # Example: 5 + 4 = 9, but in 4-bit signed: 0101 + 0100 = 1001 = -7
73
+ print(overflow_detect(0, 0, 1)) # 1 (overflow!)
74
+
75
+ # Example: -3 + 2 = -1 (no overflow)
76
+ print(overflow_detect(1, 0, 1)) # 0 (ok)
77
+ ```
78
+
79
+ ## License
80
+
81
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-overflowdetect",
3
+ "description": "Detect signed addition overflow from sign bits",
4
+ "inputs": 3,
5
+ "outputs": 1,
6
+ "neurons": 3,
7
+ "layers": 2,
8
+ "parameters": 11
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ # Inputs: a_sign, b_sign, sum_sign
5
+ # Overflow occurs when:
6
+ # - Two positives produce negative: a=0, b=0, s=1
7
+ # - Two negatives produce positive: a=1, b=1, s=0
8
+
9
+ weights = {
10
+ # N1: detects (0,0,1) - positive overflow
11
+ 'layer1.n1.weight': torch.tensor([[-1.0, -1.0, 1.0]], dtype=torch.float32),
12
+ 'layer1.n1.bias': torch.tensor([-1.0], dtype=torch.float32),
13
+ # N2: detects (1,1,0) - negative overflow
14
+ 'layer1.n2.weight': torch.tensor([[1.0, 1.0, -1.0]], dtype=torch.float32),
15
+ 'layer1.n2.bias': torch.tensor([-2.0], dtype=torch.float32),
16
+ # Layer 2: OR gate
17
+ 'layer2.weight': torch.tensor([[1.0, 1.0]], dtype=torch.float32),
18
+ 'layer2.bias': torch.tensor([-1.0], dtype=torch.float32)
19
+ }
20
+ save_file(weights, 'model.safetensors')
21
+
22
+ def overflow_detect(a_sign, b_sign, sum_sign):
23
+ inp = torch.tensor([float(a_sign), float(b_sign), float(sum_sign)])
24
+ n1 = int((inp @ weights['layer1.n1.weight'].T + weights['layer1.n1.bias'] >= 0).item())
25
+ n2 = int((inp @ weights['layer1.n2.weight'].T + weights['layer1.n2.bias'] >= 0).item())
26
+ hidden = torch.tensor([float(n1), float(n2)])
27
+ return int((hidden @ weights['layer2.weight'].T + weights['layer2.bias'] >= 0).item())
28
+
29
+ print("Verifying overflowdetect...")
30
+ errors = 0
31
+ for a in [0, 1]:
32
+ for b in [0, 1]:
33
+ for s in [0, 1]:
34
+ result = overflow_detect(a, b, s)
35
+ # Overflow when signs of operands match but sum sign differs
36
+ expected = 1 if (a == b) and (a != s) else 0
37
+ if result != expected:
38
+ errors += 1
39
+ print(f"ERROR: a={a}, b={b}, s={s} -> {result}, expected {expected}")
40
+
41
+ if errors == 0:
42
+ print("All 8 test cases passed!")
43
+ print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
model.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 overflow_detect(a_sign, b_sign, sum_sign, weights):
8
+ """Detect signed addition overflow. Returns 1 if overflow occurred."""
9
+ inp = torch.tensor([float(a_sign), float(b_sign), float(sum_sign)])
10
+ n1 = int((inp @ weights['layer1.n1.weight'].T + weights['layer1.n1.bias'] >= 0).item())
11
+ n2 = int((inp @ weights['layer1.n2.weight'].T + weights['layer1.n2.bias'] >= 0).item())
12
+ hidden = torch.tensor([float(n1), float(n2)])
13
+ return int((hidden @ weights['layer2.weight'].T + weights['layer2.bias'] >= 0).item())
14
+
15
+ if __name__ == '__main__':
16
+ w = load_model()
17
+ print('Overflow detection truth table:')
18
+ print('a_sign b_sign sum_sign | overflow | meaning')
19
+ print('-' * 55)
20
+ for a in [0, 1]:
21
+ for b in [0, 1]:
22
+ for s in [0, 1]:
23
+ result = overflow_detect(a, b, s, w)
24
+ a_str = 'pos' if a == 0 else 'neg'
25
+ b_str = 'pos' if b == 0 else 'neg'
26
+ s_str = 'pos' if s == 0 else 'neg'
27
+ marker = 'OVERFLOW!' if result else 'ok'
28
+ print(f' {a} {b} {s} | {result} | {a_str}+{b_str}={s_str} {marker}')
model.safetensors ADDED
Binary file (468 Bytes). View file