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

4-bit magnitude comparator, magnitude 64

Browse files
Files changed (5) hide show
  1. README.md +87 -0
  2. config.json +9 -0
  3. create_safetensors.py +55 -0
  4. model.py +24 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-comparator4bit
11
+
12
+ 4-bit magnitude comparator. Compares two 4-bit unsigned integers.
13
+
14
+ ## Function
15
+
16
+ compare(A, B) -> (GT, LT, EQ)
17
+
18
+ - GT = 1 if A > B
19
+ - LT = 1 if A < B
20
+ - EQ = 1 if A = B
21
+
22
+ ## Inputs
23
+
24
+ | Input | Description |
25
+ |-------|-------------|
26
+ | A3-A0 | 4-bit number A (A3 is MSB) |
27
+ | B3-B0 | 4-bit number B (B3 is MSB) |
28
+
29
+ ## Architecture
30
+
31
+ ```
32
+ A3 A2 A1 A0 B3 B2 B1 B0
33
+ | | | | | | | |
34
+ +--+--+--+--+--+--+--+
35
+ | |
36
+ v v
37
+ [GT: A-B >= 1] [LT: B-A >= 1]
38
+ | |
39
+ +--------+--------+
40
+ |
41
+ v
42
+ [EQ: NOR(GT,LT)]
43
+ ```
44
+
45
+ Layer 1:
46
+ - GT: weights [8, 4, 2, 1, -8, -4, -2, -1], bias -1
47
+ - LT: weights [-8, -4, -2, -1, 8, 4, 2, 1], bias -1
48
+
49
+ Layer 2:
50
+ - EQ: weights [-1, -1], bias 0 (NOR gate)
51
+
52
+ ## Parameters
53
+
54
+ | | |
55
+ |---|---|
56
+ | Inputs | 8 |
57
+ | Outputs | 3 (GT, LT, EQ) |
58
+ | Neurons | 3 |
59
+ | Layers | 2 |
60
+ | Parameters | 21 |
61
+ | Magnitude | 64 |
62
+
63
+ ## Usage
64
+
65
+ ```python
66
+ from safetensors.torch import load_file
67
+ import torch
68
+
69
+ w = load_file('model.safetensors')
70
+
71
+ def compare4(a3, a2, a1, a0, b3, b2, b1, b0):
72
+ inp = torch.tensor([float(a3), float(a2), float(a1), float(a0),
73
+ float(b3), float(b2), float(b1), float(b0)])
74
+ gt = int((inp @ w['gt.weight'].T + w['gt.bias'] >= 0).item())
75
+ lt = int((inp @ w['lt.weight'].T + w['lt.bias'] >= 0).item())
76
+ gt_lt = torch.tensor([float(gt), float(lt)])
77
+ eq = int((gt_lt @ w['eq.weight'].T + w['eq.bias'] >= 0).item())
78
+ return gt, lt, eq
79
+
80
+ # Compare 5 vs 3
81
+ gt, lt, eq = compare4(0, 1, 0, 1, 0, 0, 1, 1)
82
+ print(f"5 vs 3: GT={gt}, LT={lt}, EQ={eq}") # GT=1, LT=0, EQ=0
83
+ ```
84
+
85
+ ## License
86
+
87
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-comparator4bit",
3
+ "description": "4-bit magnitude comparator",
4
+ "inputs": 8,
5
+ "outputs": 3,
6
+ "neurons": 3,
7
+ "layers": 2,
8
+ "parameters": 21
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ # Inputs: A3, A2, A1, A0, B3, B2, B1, B0
5
+ # Outputs: GT (A>B), LT (A<B), EQ (A=B)
6
+
7
+ weights = {}
8
+
9
+ # Layer 1: GT and LT neurons (single layer, direct from inputs)
10
+ # GT: A - B >= 1 (fires when A > B)
11
+ weights['gt.weight'] = torch.tensor([[8.0, 4.0, 2.0, 1.0, -8.0, -4.0, -2.0, -1.0]], dtype=torch.float32)
12
+ weights['gt.bias'] = torch.tensor([-1.0], dtype=torch.float32)
13
+
14
+ # LT: B - A >= 1 (fires when A < B)
15
+ weights['lt.weight'] = torch.tensor([[-8.0, -4.0, -2.0, -1.0, 8.0, 4.0, 2.0, 1.0]], dtype=torch.float32)
16
+ weights['lt.bias'] = torch.tensor([-1.0], dtype=torch.float32)
17
+
18
+ # Layer 2: EQ = NOR(GT, LT) - fires when both GT and LT are 0
19
+ weights['eq.weight'] = torch.tensor([[-1.0, -1.0]], dtype=torch.float32)
20
+ weights['eq.bias'] = torch.tensor([0.0], dtype=torch.float32)
21
+
22
+ save_file(weights, 'model.safetensors')
23
+
24
+ # Verify
25
+ def compare4(a3, a2, a1, a0, b3, b2, b1, b0):
26
+ inp = torch.tensor([float(a3), float(a2), float(a1), float(a0),
27
+ float(b3), float(b2), float(b1), float(b0)])
28
+ gt = int((inp @ weights['gt.weight'].T + weights['gt.bias'] >= 0).item())
29
+ lt = int((inp @ weights['lt.weight'].T + weights['lt.bias'] >= 0).item())
30
+ gt_lt = torch.tensor([float(gt), float(lt)])
31
+ eq = int((gt_lt @ weights['eq.weight'].T + weights['eq.bias'] >= 0).item())
32
+ return gt, lt, eq
33
+
34
+ print("Verifying comparator4bit...")
35
+ errors = 0
36
+ for a in range(16):
37
+ for b in range(16):
38
+ a3, a2, a1, a0 = (a >> 3) & 1, (a >> 2) & 1, (a >> 1) & 1, a & 1
39
+ b3, b2, b1, b0 = (b >> 3) & 1, (b >> 2) & 1, (b >> 1) & 1, b & 1
40
+ gt, lt, eq = compare4(a3, a2, a1, a0, b3, b2, b1, b0)
41
+ exp_gt = 1 if a > b else 0
42
+ exp_lt = 1 if a < b else 0
43
+ exp_eq = 1 if a == b else 0
44
+ if (gt, lt, eq) != (exp_gt, exp_lt, exp_eq):
45
+ errors += 1
46
+ if errors <= 5:
47
+ print(f"ERROR: A={a}, B={b}, got ({gt},{lt},{eq}), expected ({exp_gt},{exp_lt},{exp_eq})")
48
+
49
+ if errors == 0:
50
+ print("All 256 test cases passed!")
51
+ else:
52
+ print(f"FAILED: {errors} errors")
53
+
54
+ mag = sum(t.abs().sum().item() for t in weights.values())
55
+ 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 compare4(a3, a2, a1, a0, b3, b2, b1, b0, weights):
8
+ """4-bit magnitude comparator. Returns (GT, LT, EQ)."""
9
+ inp = torch.tensor([float(a3), float(a2), float(a1), float(a0),
10
+ float(b3), float(b2), float(b1), float(b0)])
11
+ gt = int((inp @ weights['gt.weight'].T + weights['gt.bias'] >= 0).item())
12
+ lt = int((inp @ weights['lt.weight'].T + weights['lt.bias'] >= 0).item())
13
+ gt_lt = torch.tensor([float(gt), float(lt)])
14
+ eq = int((gt_lt @ weights['eq.weight'].T + weights['eq.bias'] >= 0).item())
15
+ return gt, lt, eq
16
+
17
+ if __name__ == '__main__':
18
+ w = load_model()
19
+ print('Comparator4bit examples:')
20
+ for a, b in [(5, 3), (3, 5), (7, 7), (0, 15), (15, 0)]:
21
+ a3, a2, a1, a0 = (a >> 3) & 1, (a >> 2) & 1, (a >> 1) & 1, a & 1
22
+ b3, b2, b1, b0 = (b >> 3) & 1, (b >> 2) & 1, (b >> 1) & 1, b & 1
23
+ gt, lt, eq = compare4(a3, a2, a1, a0, b3, b2, b1, b0, w)
24
+ print(f' A={a:2d}, B={b:2d} -> GT={gt}, LT={lt}, EQ={eq}')
model.safetensors ADDED
Binary file (468 Bytes). View file