CharlesCNorton commited on
Commit
41edcb6
·
0 Parent(s):

2-bit equality comparator, magnitude 18

Browse files
Files changed (5) hide show
  1. README.md +63 -0
  2. config.json +9 -0
  3. create_safetensors.py +69 -0
  4. model.py +28 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-equals2
11
+
12
+ 2-bit equality comparator. Outputs 1 if 2-bit A equals 2-bit B.
13
+
14
+ ## Function
15
+
16
+ equals2(a1, a0, b1, b0) = 1 if (a1,a0) == (b1,b0), else 0
17
+
18
+ ## Architecture
19
+
20
+ Checks if each bit pair matches using XNOR, then ANDs the results.
21
+
22
+ XNOR(x, y) = (x AND y) OR (NOT x AND NOT y)
23
+
24
+ **Layer 1:** (4 neurons on raw inputs)
25
+ - and1: a1 AND b1
26
+ - nor1: NOR(a1, b1)
27
+ - and0: a0 AND b0
28
+ - nor0: NOR(a0, b0)
29
+
30
+ **Layer 2:** (2 neurons)
31
+ - xnor1: OR(and1, nor1) - bit 1 matches
32
+ - xnor0: OR(and0, nor0) - bit 0 matches
33
+
34
+ **Layer 3:** (1 neuron)
35
+ - eq: AND(xnor1, xnor0) - both bits match
36
+
37
+ ## Parameters
38
+
39
+ | | |
40
+ |---|---|
41
+ | Inputs | 4 (a1, a0, b1, b0) |
42
+ | Outputs | 1 |
43
+ | Neurons | 7 |
44
+ | Layers | 3 |
45
+ | Parameters | 31 |
46
+ | Magnitude | 18 |
47
+
48
+ ## Usage
49
+
50
+ ```python
51
+ from safetensors.torch import load_file
52
+ import torch
53
+
54
+ w = load_file('model.safetensors')
55
+
56
+ # Check if 2 == 2 (binary 10 == 10)
57
+ # a1=1, a0=0, b1=1, b0=0
58
+ # Result: 1 (equal)
59
+ ```
60
+
61
+ ## License
62
+
63
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-equals2",
3
+ "description": "2-bit equality comparator",
4
+ "inputs": 4,
5
+ "outputs": 1,
6
+ "neurons": 7,
7
+ "layers": 3,
8
+ "parameters": 31
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ # equals2: checks if 2-bit A == 2-bit B
5
+ # Inputs: a1, a0, b1, b0 (4 inputs)
6
+ # XNOR(a1,b1) AND XNOR(a0,b0)
7
+ # XNOR uses: AND + NOR + OR structure
8
+
9
+ weights = {
10
+ # Layer 1: compute AND and NOR for each bit pair
11
+ # For bit 1: a1 (idx 0), b1 (idx 2)
12
+ 'layer1.and1.weight': torch.tensor([[1.0, 0.0, 1.0, 0.0]], dtype=torch.float32),
13
+ 'layer1.and1.bias': torch.tensor([-2.0], dtype=torch.float32),
14
+ 'layer1.nor1.weight': torch.tensor([[-1.0, 0.0, -1.0, 0.0]], dtype=torch.float32),
15
+ 'layer1.nor1.bias': torch.tensor([0.0], dtype=torch.float32),
16
+ # For bit 0: a0 (idx 1), b0 (idx 3)
17
+ 'layer1.and0.weight': torch.tensor([[0.0, 1.0, 0.0, 1.0]], dtype=torch.float32),
18
+ 'layer1.and0.bias': torch.tensor([-2.0], dtype=torch.float32),
19
+ 'layer1.nor0.weight': torch.tensor([[0.0, -1.0, 0.0, -1.0]], dtype=torch.float32),
20
+ 'layer1.nor0.bias': torch.tensor([0.0], dtype=torch.float32),
21
+
22
+ # Layer 2: OR(AND, NOR) = XNOR for each bit
23
+ 'layer2.xnor1.weight': torch.tensor([[1.0, 1.0, 0.0, 0.0]], dtype=torch.float32), # and1, nor1
24
+ 'layer2.xnor1.bias': torch.tensor([-1.0], dtype=torch.float32),
25
+ 'layer2.xnor0.weight': torch.tensor([[0.0, 0.0, 1.0, 1.0]], dtype=torch.float32), # and0, nor0
26
+ 'layer2.xnor0.bias': torch.tensor([-1.0], dtype=torch.float32),
27
+
28
+ # Layer 3: AND of both XNORs
29
+ 'layer3.eq.weight': torch.tensor([[1.0, 1.0]], dtype=torch.float32),
30
+ 'layer3.eq.bias': torch.tensor([-2.0], dtype=torch.float32),
31
+ }
32
+
33
+ save_file(weights, 'model.safetensors')
34
+
35
+ def equals2(a1, a0, b1, b0):
36
+ inp = torch.tensor([float(a1), float(a0), float(b1), float(b0)])
37
+
38
+ # Layer 1
39
+ and1 = int((inp * weights['layer1.and1.weight']).sum() + weights['layer1.and1.bias'] >= 0)
40
+ nor1 = int((inp * weights['layer1.nor1.weight']).sum() + weights['layer1.nor1.bias'] >= 0)
41
+ and0 = int((inp * weights['layer1.and0.weight']).sum() + weights['layer1.and0.bias'] >= 0)
42
+ nor0 = int((inp * weights['layer1.nor0.weight']).sum() + weights['layer1.nor0.bias'] >= 0)
43
+
44
+ l1 = torch.tensor([float(and1), float(nor1), float(and0), float(nor0)])
45
+
46
+ # Layer 2
47
+ xnor1 = int((l1 * weights['layer2.xnor1.weight']).sum() + weights['layer2.xnor1.bias'] >= 0)
48
+ xnor0 = int((l1 * weights['layer2.xnor0.weight']).sum() + weights['layer2.xnor0.bias'] >= 0)
49
+
50
+ l2 = torch.tensor([float(xnor1), float(xnor0)])
51
+
52
+ # Layer 3
53
+ eq = int((l2 * weights['layer3.eq.weight']).sum() + weights['layer3.eq.bias'] >= 0)
54
+ return eq
55
+
56
+ print("Verifying equals2...")
57
+ errors = 0
58
+ for a in range(4):
59
+ for b in range(4):
60
+ a1, a0 = (a >> 1) & 1, a & 1
61
+ b1, b0 = (b >> 1) & 1, b & 1
62
+ result = equals2(a1, a0, b1, b0)
63
+ expected = 1 if a == b else 0
64
+ if result != expected:
65
+ errors += 1
66
+ print(f"ERROR: {a} == {b}? got {result}, expected {expected}")
67
+ if errors == 0:
68
+ print("All 16 test cases passed!")
69
+ 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 equals2(a1, a0, b1, b0, w):
8
+ inp = torch.tensor([float(a1), float(a0), float(b1), float(b0)])
9
+
10
+ and1 = int((inp * w['layer1.and1.weight']).sum() + w['layer1.and1.bias'] >= 0)
11
+ nor1 = int((inp * w['layer1.nor1.weight']).sum() + w['layer1.nor1.bias'] >= 0)
12
+ and0 = int((inp * w['layer1.and0.weight']).sum() + w['layer1.and0.bias'] >= 0)
13
+ nor0 = int((inp * w['layer1.nor0.weight']).sum() + w['layer1.nor0.bias'] >= 0)
14
+
15
+ l1 = torch.tensor([float(and1), float(nor1), float(and0), float(nor0)])
16
+ xnor1 = int((l1 * w['layer2.xnor1.weight']).sum() + w['layer2.xnor1.bias'] >= 0)
17
+ xnor0 = int((l1 * w['layer2.xnor0.weight']).sum() + w['layer2.xnor0.bias'] >= 0)
18
+
19
+ l2 = torch.tensor([float(xnor1), float(xnor0)])
20
+ return int((l2 * w['layer3.eq.weight']).sum() + w['layer3.eq.bias'] >= 0)
21
+
22
+ if __name__ == '__main__':
23
+ w = load_model()
24
+ print('equals2 truth table:')
25
+ for a in range(4):
26
+ for b in range(4):
27
+ a1, a0, b1, b0 = (a >> 1) & 1, a & 1, (b >> 1) & 1, b & 1
28
+ print(f' {a} == {b}? {equals2(a1, a0, b1, b0, w)}')
model.safetensors ADDED
Binary file (1.16 kB). View file