phanerozoic commited on
Commit
3af1a71
Β·
verified Β·
1 Parent(s): f26be41

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +117 -0
  2. config.json +9 -0
  3. model.py +23 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - arithmetic
9
+ ---
10
+
11
+ # threshold-halfsubtractor
12
+
13
+ Subtracts two 1-bit inputs, producing difference and borrow. The subtraction counterpart to the half adder.
14
+
15
+ ## Circuit
16
+
17
+ ```
18
+ a b a b
19
+ β”‚ β”‚ β”‚ β”‚
20
+ β”œβ”€β”€β”€β”¬β”€β”€β”€β”€ β””β”€β”¬β”€β”˜
21
+ β”‚ β”‚ β”‚ β”‚
22
+ β–Ό β”‚ β–Ό β–Ό
23
+ β”Œβ”€β”€β”€β”€β”€β”€β”β”‚β”Œβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”
24
+ β”‚ OR β”‚β”‚β”‚ NAND β”‚ β”‚ Β¬a ∧ bβ”‚
25
+ β”‚w:1,1 β”‚β”‚β”‚w:-1,-1β”‚ β”‚w:-1,1 β”‚
26
+ β”‚b: -1 β”‚β”‚β”‚b: +1 β”‚ β”‚b: -1 β”‚
27
+ β””β”€β”€β”€β”€β”€β”€β”˜β”‚β””β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”˜
28
+ β”‚ β”‚ β”‚ β”‚
29
+ β””β”€β”€β”€β”Όβ”€β”€β”€β”˜ β”‚
30
+ β–Ό β”‚
31
+ β”Œβ”€β”€β”€β”€β”€β”€β” β”‚
32
+ β”‚ AND β”‚ β”‚
33
+ β”‚w: 1,1β”‚ β”‚
34
+ β”‚b: -2 β”‚ β”‚
35
+ β””β”€β”€β”€β”€β”€β”€β”˜ β”‚
36
+ β”‚ β”‚
37
+ β–Ό β–Ό
38
+ diff borrow
39
+ ```
40
+
41
+ The diff output uses XOR (2 layers). The borrow output detects when we subtract 1 from 0.
42
+
43
+ ## Truth Table
44
+
45
+ | a | b | diff | borrow |
46
+ |---|---|------|--------|
47
+ | 0 | 0 | 0 | 0 |
48
+ | 0 | 1 | 1 | 1 |
49
+ | 1 | 0 | 1 | 0 |
50
+ | 1 | 1 | 0 | 0 |
51
+
52
+ Binary: a - b = diff - (borrow Γ— 2)
53
+
54
+ ## Mechanism
55
+
56
+ **Diff (XOR):** Same as the half adder's sum. The difference bit is 1 when exactly one input is 1.
57
+
58
+ **Borrow:** Uses weights [-1, +1] with bias -1. This fires only when a=0 and b=1:
59
+ - a=0, b=0: -0 + 0 - 1 = -1 < 0 β†’ no borrow
60
+ - a=0, b=1: -0 + 1 - 1 = 0 β‰₯ 0 β†’ borrow
61
+ - a=1, b=0: -1 + 0 - 1 = -2 < 0 β†’ no borrow
62
+ - a=1, b=1: -1 + 1 - 1 = -1 < 0 β†’ no borrow
63
+
64
+ The borrow is NOT(a) AND b - we borrow when subtracting 1 from 0.
65
+
66
+ ## Components
67
+
68
+ | Output | Function | Neurons | Layers |
69
+ |--------|----------|---------|--------|
70
+ | diff | XOR(a, b) | 3 | 2 |
71
+ | borrow | ¬a ∧ b | 1 | 1 |
72
+
73
+ **Total: 4 neurons, 12 parameters**
74
+
75
+ ## Contrast with Half Adder
76
+
77
+ | Circuit | Output 2 | Function | Weights |
78
+ |---------|----------|----------|---------|
79
+ | HalfAdder | carry | a ∧ b | [1, 1], b=-2 |
80
+ | HalfSubtractor | borrow | ¬a ∧ b | [-1, 1], b=-1 |
81
+
82
+ The only difference: borrow has an inhibitory weight on a.
83
+
84
+ ## Usage
85
+
86
+ ```python
87
+ from safetensors.torch import load_file
88
+ import torch
89
+
90
+ w = load_file('model.safetensors')
91
+
92
+ def half_subtractor(a, b):
93
+ inp = torch.tensor([float(a), float(b)])
94
+
95
+ # XOR for diff
96
+ l1 = (inp @ w['xor.layer1.weight'].T + w['xor.layer1.bias'] >= 0).float()
97
+ diff = int((l1 @ w['xor.layer2.weight'].T + w['xor.layer2.bias'] >= 0).item())
98
+
99
+ # Borrow
100
+ borrow = int((inp @ w['borrow.weight'].T + w['borrow.bias'] >= 0).item())
101
+
102
+ return diff, borrow
103
+ ```
104
+
105
+ ## Files
106
+
107
+ ```
108
+ threshold-halfsubtractor/
109
+ β”œβ”€β”€ model.safetensors
110
+ β”œβ”€β”€ model.py
111
+ β”œβ”€β”€ config.json
112
+ └── README.md
113
+ ```
114
+
115
+ ## License
116
+
117
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-halfsubtractor",
3
+ "description": "Half subtractor as threshold circuit",
4
+ "inputs": 2,
5
+ "outputs": 2,
6
+ "neurons": 4,
7
+ "layers": 2,
8
+ "parameters": 12
9
+ }
model.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 half_subtractor(a, b, weights):
8
+ """Half subtractor: computes a - b, returns (diff, borrow)"""
9
+ inp = torch.tensor([float(a), float(b)])
10
+ # XOR for diff
11
+ l1 = (inp @ weights['xor.layer1.weight'].T + weights['xor.layer1.bias'] >= 0).float()
12
+ diff = int((l1 @ weights['xor.layer2.weight'].T + weights['xor.layer2.bias'] >= 0).item())
13
+ # Borrow
14
+ borrow = int((inp @ weights['borrow.weight'].T + weights['borrow.bias'] >= 0).item())
15
+ return diff, borrow
16
+
17
+ if __name__ == '__main__':
18
+ w = load_model()
19
+ print('HalfSubtractor truth table:')
20
+ for a in [0, 1]:
21
+ for b in [0, 1]:
22
+ diff, borrow = half_subtractor(a, b, w)
23
+ print(f'{a} - {b} = {diff}, borrow={borrow}')
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e6cb61261c9c636fe4f62278cc80430c59a033ecc4e3855436f7da9a29bbc8b2
3
+ size 472