phanerozoic commited on
Commit
0fc2474
·
verified ·
1 Parent(s): 9962a54

Rename from tiny-HalfAdder-verified

Browse files
Files changed (4) hide show
  1. README.md +108 -0
  2. config.json +22 -0
  3. model.py +60 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - arithmetic
9
+ ---
10
+
11
+ # threshold-halfadder
12
+
13
+ Adds two 1-bit inputs, producing sum and carry. The building block for all multi-bit adders.
14
+
15
+ ## Circuit
16
+
17
+ ```
18
+ a b
19
+ │ │
20
+ ├───┬───┤
21
+ │ │ │
22
+ ▼ │ ▼
23
+ ┌──────┐│┌──────┐
24
+ │ OR │││ NAND │
25
+ └──────┘│└──────┘
26
+ │ │ │
27
+ └───┼───┘
28
+ ▼ a b
29
+ ┌──────┐ │ │
30
+ │ AND │ └─┬─┘
31
+ └──────┘ ▼
32
+ │ ┌──────┐
33
+ ▼ │ AND │
34
+ sum └──────┘
35
+
36
+
37
+ carry
38
+ ```
39
+
40
+ The sum output uses XOR (2 layers), the carry uses AND (1 layer).
41
+
42
+ ## Truth Table
43
+
44
+ | a | b | sum | carry |
45
+ |---|---|-----|-------|
46
+ | 0 | 0 | 0 | 0 |
47
+ | 0 | 1 | 1 | 0 |
48
+ | 1 | 0 | 1 | 0 |
49
+ | 1 | 1 | 0 | 1 |
50
+
51
+ Binary: a + b = (carry × 2) + sum
52
+
53
+ ## Components
54
+
55
+ | Output | Function | Neurons | Layers |
56
+ |--------|----------|---------|--------|
57
+ | sum | XOR(a, b) | 3 | 2 |
58
+ | carry | AND(a, b) | 1 | 1 |
59
+
60
+ **Total: 4 neurons, 12 parameters**
61
+
62
+ ## Arithmetic
63
+
64
+ The half adder computes a + b where a, b ∈ {0, 1}:
65
+
66
+ - 0 + 0 = 0 (sum=0, carry=0)
67
+ - 0 + 1 = 1 (sum=1, carry=0)
68
+ - 1 + 0 = 1 (sum=1, carry=0)
69
+ - 1 + 1 = 2 (sum=0, carry=1)
70
+
71
+ The carry represents the 2s place.
72
+
73
+ ## Usage
74
+
75
+ ```python
76
+ from safetensors.torch import load_file
77
+ import torch
78
+
79
+ w = load_file('model.safetensors')
80
+
81
+ def half_adder(a, b):
82
+ inp = torch.tensor([float(a), float(b)])
83
+
84
+ # XOR for sum
85
+ or_out = int((inp @ w['xor.layer1.or.weight'] + w['xor.layer1.or.bias']).sum() >= 0)
86
+ nand_out = int((inp @ w['xor.layer1.nand.weight'] + w['xor.layer1.nand.bias']).sum() >= 0)
87
+ xor_inp = torch.tensor([float(or_out), float(nand_out)])
88
+ sum_out = int((xor_inp @ w['xor.layer2.weight'] + w['xor.layer2.bias']).sum() >= 0)
89
+
90
+ # AND for carry
91
+ carry_out = int((inp @ w['carry.weight'] + w['carry.bias']).sum() >= 0)
92
+
93
+ return sum_out, carry_out
94
+ ```
95
+
96
+ ## Files
97
+
98
+ ```
99
+ threshold-halfadder/
100
+ ├── model.safetensors
101
+ ├── model.py
102
+ ├── config.json
103
+ └── README.md
104
+ ```
105
+
106
+ ## License
107
+
108
+ MIT
config.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "half_adder",
4
+ "architecture": "2 -> (XOR:2->2->1, AND:1)",
5
+ "input_size": 2,
6
+ "outputs": ["sum", "carry"],
7
+ "num_neurons": 4,
8
+ "num_parameters": 12,
9
+ "depth": 2,
10
+ "activation": "heaviside",
11
+ "weight_constraints": "integer",
12
+ "verification": {
13
+ "method": "coq_proof",
14
+ "exhaustive": true,
15
+ "inputs_tested": 4
16
+ },
17
+ "accuracy": {
18
+ "all_inputs": "4/4",
19
+ "percentage": 100.0
20
+ },
21
+ "github": "https://github.com/CharlesCNorton/coq-circuits"
22
+ }
model.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Threshold Network for Half Adder
3
+
4
+ Adds two 1-bit inputs, producing sum (XOR) and carry (AND) outputs.
5
+ Sum uses 2-layer XOR, Carry uses single AND neuron.
6
+ """
7
+
8
+ import torch
9
+ from safetensors.torch import load_file
10
+
11
+
12
+ def heaviside(x):
13
+ return (x >= 0).float()
14
+
15
+
16
+ class ThresholdHalfAdder:
17
+ """
18
+ Half adder: sum = a XOR b, carry = a AND b
19
+ """
20
+
21
+ def __init__(self, weights_dict):
22
+ self.weights = weights_dict
23
+
24
+ def __call__(self, a, b):
25
+ inputs = torch.tensor([float(a), float(b)])
26
+
27
+ # Sum = XOR (2-layer)
28
+ or_out = heaviside((inputs * self.weights['sum.layer1.or.weight']).sum() +
29
+ self.weights['sum.layer1.or.bias'])
30
+ nand_out = heaviside((inputs * self.weights['sum.layer1.nand.weight']).sum() +
31
+ self.weights['sum.layer1.nand.bias'])
32
+ layer1 = torch.tensor([or_out, nand_out])
33
+ sum_out = heaviside((layer1 * self.weights['sum.layer2.weight']).sum() +
34
+ self.weights['sum.layer2.bias'])
35
+
36
+ # Carry = AND (single neuron)
37
+ carry_out = heaviside((inputs * self.weights['carry.weight']).sum() +
38
+ self.weights['carry.bias'])
39
+
40
+ return int(sum_out.item()), int(carry_out.item())
41
+
42
+ @classmethod
43
+ def from_safetensors(cls, path="model.safetensors"):
44
+ return cls(load_file(path))
45
+
46
+
47
+ if __name__ == "__main__":
48
+ model = ThresholdHalfAdder.from_safetensors("model.safetensors")
49
+
50
+ print("Half Adder Truth Table:")
51
+ print("-" * 30)
52
+ print("a | b | sum | carry")
53
+ print("-" * 30)
54
+ for a in [0, 1]:
55
+ for b in [0, 1]:
56
+ s, c = model(a, b)
57
+ expected_s = a ^ b
58
+ expected_c = a & b
59
+ status = "OK" if (s == expected_s and c == expected_c) else "FAIL"
60
+ print(f"{a} | {b} | {s} | {c} [{status}]")
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:646d37de6dcc9da2b95118e8f1066dd7fb6fe274b4a354187641035272c0b053
3
+ size 624