phanerozoic commited on
Commit
1529ce2
·
verified ·
1 Parent(s): c0de9cd

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +49 -0
  2. config.json +9 -0
  3. create_safetensors.py +84 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - arithmetic
9
+ - multiplier
10
+ ---
11
+
12
+ # threshold-array-multiplier
13
+
14
+ 2x2 array multiplier. Regular structure with rows of AND gates and adders.
15
+
16
+ ## Circuit
17
+
18
+ ```
19
+ Inputs: A[1:0], B[1:0] (4 inputs)
20
+ Outputs: P[3:0] (4 outputs)
21
+
22
+ A1 A0
23
+ │ │
24
+ B0 ──┼──●──┼──●──► P0
25
+ │ │ │ │
26
+ B1 ──●──┼──●──┤
27
+ │ │ │
28
+ ▼ ▼ ▼
29
+ P3 P2 P1
30
+ ```
31
+
32
+ ## Array Structure
33
+
34
+ Regular grid of AND-adder cells. Simple, predictable timing.
35
+
36
+ ## Parameters
37
+
38
+ | | |
39
+ |---|---|
40
+ | Inputs | 4 |
41
+ | Outputs | 4 |
42
+ | Neurons | 10 |
43
+ | Layers | 3 |
44
+ | Parameters | 44 |
45
+ | Magnitude | 44 |
46
+
47
+ ## License
48
+
49
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-array-multiplier",
3
+ "description": "2x2 array multiplier",
4
+ "inputs": 4,
5
+ "outputs": 4,
6
+ "neurons": 10,
7
+ "layers": 3,
8
+ "parameters": 44
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {}
5
+
6
+ # 2x2 Array Multiplier
7
+ # Uses regular array structure of AND gates and adders
8
+ # Inputs: A1,A0, B1,B0 (4 inputs)
9
+ # Outputs: P3,P2,P1,P0 (4 outputs)
10
+
11
+ def add_and(name, idx_a, idx_b, n_inputs):
12
+ w = [0.0] * n_inputs
13
+ w[idx_a] = 1.0
14
+ w[idx_b] = 1.0
15
+ weights[f'{name}.weight'] = torch.tensor([w], dtype=torch.float32)
16
+ weights[f'{name}.bias'] = torch.tensor([-2.0], dtype=torch.float32)
17
+
18
+ def add_xor_direct(name):
19
+ weights[f'{name}.or.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
20
+ weights[f'{name}.or.bias'] = torch.tensor([-1.0], dtype=torch.float32)
21
+ weights[f'{name}.nand.weight'] = torch.tensor([[-1.0, -1.0]], dtype=torch.float32)
22
+ weights[f'{name}.nand.bias'] = torch.tensor([1.0], dtype=torch.float32)
23
+ weights[f'{name}.and.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
24
+ weights[f'{name}.and.bias'] = torch.tensor([-2.0], dtype=torch.float32)
25
+
26
+ def add_ha_carry(name):
27
+ weights[f'{name}.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
28
+ weights[f'{name}.bias'] = torch.tensor([-2.0], dtype=torch.float32)
29
+
30
+ # Row 0: A[1:0] * B0
31
+ add_and('r0c0', 1, 3, 4) # A0*B0 -> P0
32
+ add_and('r0c1', 0, 3, 4) # A1*B0
33
+
34
+ # Row 1: A[1:0] * B1, shifted and added
35
+ add_and('r1c0', 1, 2, 4) # A0*B1
36
+ add_and('r1c1', 0, 2, 4) # A1*B1
37
+
38
+ # Array cell (0,1): r0c1 + r1c0 -> half adder
39
+ add_xor_direct('cell01_sum')
40
+ add_ha_carry('cell01_carry')
41
+
42
+ # Array cell (1,1): r1c1 + carry -> half adder
43
+ add_xor_direct('cell11_sum')
44
+ add_ha_carry('cell11_carry')
45
+
46
+ save_file(weights, 'model.safetensors')
47
+
48
+ def array_mult(a1, a0, b1, b0):
49
+ # Partial products
50
+ r0c0 = a0 & b0 # P0
51
+ r0c1 = a1 & b0
52
+ r1c0 = a0 & b1
53
+ r1c1 = a1 & b1
54
+
55
+ # Array addition
56
+ p0 = r0c0
57
+ p1 = r0c1 ^ r1c0
58
+ c1 = r0c1 & r1c0
59
+ p2 = r1c1 ^ c1
60
+ p3 = r1c1 & c1
61
+
62
+ return p3, p2, p1, p0
63
+
64
+ print("Verifying 2x2 array multiplier...")
65
+ errors = 0
66
+ for a in range(4):
67
+ for b in range(4):
68
+ a1, a0 = (a >> 1) & 1, a & 1
69
+ b1, b0 = (b >> 1) & 1, b & 1
70
+ p3, p2, p1, p0 = array_mult(a1, a0, b1, b0)
71
+ result = p3*8 + p2*4 + p1*2 + p0
72
+ expected = a * b
73
+ if result != expected:
74
+ errors += 1
75
+ print(f"ERROR: {a}*{b} = {result}, expected {expected}")
76
+
77
+ if errors == 0:
78
+ print("All 16 test cases passed!")
79
+ else:
80
+ print(f"FAILED: {errors} errors")
81
+
82
+ mag = sum(t.abs().sum().item() for t in weights.values())
83
+ print(f"Magnitude: {mag:.0f}")
84
+ print(f"Parameters: {sum(t.numel() for t in weights.values())}")
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ae8072be384f0599d8f6fca68b4a80d079344db2a700d71b4c33886ffe6f557b
3
+ size 1912