phanerozoic commited on
Commit
462e438
Β·
verified Β·
1 Parent(s): 71c6438

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +147 -0
  2. config.json +9 -0
  3. model.py +54 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - arithmetic
9
+ - multiplier
10
+ ---
11
+
12
+ # threshold-multiplier2x2
13
+
14
+ 2Γ—2 binary multiplier. Multiplies two 2-bit numbers to produce a 4-bit product.
15
+
16
+ ## Circuit
17
+
18
+ ```
19
+ a1 a0
20
+ Γ— b1 b0
21
+ ─────────
22
+ a1b0 a0b0 (partial products row 0)
23
+ a1b1 a0b1 (partial products row 1)
24
+ ─────────────────
25
+ p3 p2 p1 p0
26
+
27
+
28
+ a0──┬───────┬───────────────────────────────────► p0 = a0b0
29
+ β”‚ β”‚
30
+ b0──┼───┬───┼───┬───────────────────────────────┐
31
+ β”‚ β”‚ β”‚ β”‚ β”‚
32
+ β–Ό β”‚ β–Ό β”‚ β”‚
33
+ β”Œβ”€β”€β”€β”€β”€β”β”‚β”Œβ”€β”€β”€β”€β”€β”β”‚ β”‚
34
+ β”‚a0b0 β”‚β”‚β”‚a1b0 β”‚β”‚ β”‚
35
+ β””β”€β”€β”€β”€β”€β”˜β”‚β””β”€β”€β”€β”€β”€β”˜β”‚ β”‚
36
+ β”‚ β”‚ β”‚ β”‚
37
+ a1──────┴───┼───┴───┬────────────────────────────
38
+ β”‚ β”‚ β”‚
39
+ b1──────────┴───┬───┴────────────────────────────
40
+ β”‚ β”‚ β”‚
41
+ β–Ό β–Ό β”‚
42
+ β”Œβ”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β” β”‚
43
+ β”‚a0b1 β”‚β”‚a1b1 β”‚ β”‚
44
+ β””β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”˜ β”‚
45
+ β”‚ β”‚ β”‚
46
+ β”‚ β”‚ β”‚
47
+ a1b0────────┼──────┼────┐ β”‚
48
+ β”‚ β”‚ β”‚ β”‚
49
+ a0b1────────┴──────┼────┼────┐ β”‚
50
+ β”‚ β”‚ β”‚ β”‚
51
+ β”‚ β–Ό β–Ό β”‚
52
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
53
+ β”‚ β”‚ HA β”‚ β”‚
54
+ β”‚ β”‚ p1, c1 β”‚ β”‚
55
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
56
+ β”‚ β”‚ β”‚ β”‚
57
+ β”‚ β”‚ β”‚ β”‚
58
+ a1b1───────────────┴────┼────┼────┐ β”‚
59
+ β”‚ β”‚ β”‚ β”‚
60
+ c1 ─────────────────────┴────┼────┼────┐ β”‚
61
+ β”‚ β”‚ β”‚ β”‚
62
+ β”‚ β–Ό β–Ό β”‚
63
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
64
+ β”‚ β”‚ HA β”‚ β”‚
65
+ β”‚ β”‚ p2, p3 β”‚ β”‚
66
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
67
+ β”‚ β”‚ β”‚ β”‚
68
+ β–Ό β–Ό β–Ό β–Ό
69
+ p1 p2 p3 p0
70
+ ```
71
+
72
+ ## Multiplication Table
73
+
74
+ | a | b | a Γ— b |
75
+ |---|---|-------|
76
+ | 0 | 0 | 0 |
77
+ | 0 | 1 | 0 |
78
+ | 0 | 2 | 0 |
79
+ | 0 | 3 | 0 |
80
+ | 1 | 1 | 1 |
81
+ | 1 | 2 | 2 |
82
+ | 1 | 3 | 3 |
83
+ | 2 | 2 | 4 |
84
+ | 2 | 3 | 6 |
85
+ | 3 | 3 | 9 |
86
+
87
+ Maximum: 3 Γ— 3 = 9 (binary 1001)
88
+
89
+ ## Algorithm
90
+
91
+ Standard pencil-and-paper multiplication:
92
+
93
+ 1. **Compute partial products** (4 AND gates):
94
+ - a0b0, a1b0, a0b1, a1b1
95
+
96
+ 2. **Add column 1** (half adder):
97
+ - p1, c1 = HA(a1b0, a0b1)
98
+
99
+ 3. **Add column 2** (half adder):
100
+ - p2, p3 = HA(a1b1, c1)
101
+
102
+ 4. **Assemble result**:
103
+ - p0 = a0b0
104
+ - p1-p3 from adders
105
+
106
+ ## Architecture
107
+
108
+ | Component | Neurons | Parameters |
109
+ |-----------|---------|------------|
110
+ | AND Γ— 4 | 4 | 12 |
111
+ | HA (p1) | 4 | 12 |
112
+ | HA (p2) | 4 | 12 |
113
+ | **Total** | **12** | **36** |
114
+
115
+ **Layers: 5** (AND β†’ XOR L1 β†’ XOR L2 β†’ XOR L1 β†’ XOR L2)
116
+
117
+ ## Usage
118
+
119
+ ```python
120
+ from safetensors.torch import load_file
121
+
122
+ w = load_file('model.safetensors')
123
+
124
+ def multiply_2x2(a, b):
125
+ """a, b: 2-bit integers (0-3)
126
+ Returns: 4-bit product"""
127
+ a0, a1 = a & 1, (a >> 1) & 1
128
+ b0, b1 = b & 1, (b >> 1) & 1
129
+ # See model.py for full implementation
130
+ pass
131
+
132
+ result = multiply_2x2(3, 3) # Returns 9
133
+ ```
134
+
135
+ ## Files
136
+
137
+ ```
138
+ threshold-multiplier2x2/
139
+ β”œβ”€β”€ model.safetensors
140
+ β”œβ”€β”€ model.py
141
+ β”œβ”€β”€ config.json
142
+ └── README.md
143
+ ```
144
+
145
+ ## License
146
+
147
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-multiplier2x2",
3
+ "description": "2x2 binary multiplier as threshold circuit",
4
+ "inputs": 4,
5
+ "outputs": 4,
6
+ "neurons": 12,
7
+ "layers": 5,
8
+ "parameters": 36
9
+ }
model.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 and_gate(x, y, w, name):
8
+ inp = torch.tensor([float(x), float(y)])
9
+ return int((inp * w[f'{name}.weight']).sum() + w[f'{name}.bias'] >= 0)
10
+
11
+ def half_adder_sum(x, y, w, prefix):
12
+ inp = torch.tensor([float(x), float(y)])
13
+ l1 = (inp @ w[f'{prefix}.xor.layer1.weight'].T + w[f'{prefix}.xor.layer1.bias'] >= 0).float()
14
+ return int((l1 @ w[f'{prefix}.xor.layer2.weight'].T + w[f'{prefix}.xor.layer2.bias'] >= 0).item())
15
+
16
+ def half_adder_carry(x, y, w, prefix):
17
+ inp = torch.tensor([float(x), float(y)])
18
+ return int((inp * w[f'{prefix}.carry.weight']).sum() + w[f'{prefix}.carry.bias'] >= 0)
19
+
20
+ def multiply_2x2(a0, a1, b0, b1, weights):
21
+ """2x2 binary multiplier.
22
+ Inputs: a = (a1, a0), b = (b1, b0) in MSB-first notation
23
+ Returns: (p0, p1, p2, p3) LSB-first, representing a Γ— b
24
+ """
25
+ # Partial products
26
+ pp_a0b0 = and_gate(a0, b0, weights, 'a0b0')
27
+ pp_a1b0 = and_gate(a1, b0, weights, 'a1b0')
28
+ pp_a0b1 = and_gate(a0, b1, weights, 'a0b1')
29
+ pp_a1b1 = and_gate(a1, b1, weights, 'a1b1')
30
+
31
+ # p0 = a0 AND b0
32
+ p0 = pp_a0b0
33
+
34
+ # p1, c1 = half_add(a1b0, a0b1)
35
+ p1 = half_adder_sum(pp_a1b0, pp_a0b1, weights, 'ha_p1')
36
+ c1 = half_adder_carry(pp_a1b0, pp_a0b1, weights, 'ha_p1')
37
+
38
+ # p2, p3 = half_add(a1b1, c1)
39
+ p2 = half_adder_sum(pp_a1b1, c1, weights, 'ha_p2')
40
+ p3 = half_adder_carry(pp_a1b1, c1, weights, 'ha_p2')
41
+
42
+ return p0, p1, p2, p3
43
+
44
+ if __name__ == '__main__':
45
+ w = load_model()
46
+ print('2x2 Binary Multiplier')
47
+ print('a Γ— b tests:')
48
+ for a in range(4):
49
+ for b in range(4):
50
+ a0, a1 = a & 1, (a >> 1) & 1
51
+ b0, b1 = b & 1, (b >> 1) & 1
52
+ p0, p1, p2, p3 = multiply_2x2(a0, a1, b0, b1, w)
53
+ result = p0 + (p1 << 1) + (p2 << 2) + (p3 << 3)
54
+ print(f'{a} Γ— {b} = {result}')
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f9732125070b197b7309af271290a12073d97d07f415922392b4991cef62e719
3
+ size 1576