phanerozoic commited on
Commit
ac6a9a3
·
verified ·
1 Parent(s): 7845689

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +50 -0
  2. config.json +9 -0
  3. create_safetensors.py +94 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - arithmetic
9
+ - multiplier
10
+ ---
11
+
12
+ # threshold-dadda-tree
13
+
14
+ 2x2 Dadda tree multiplier. Uses height-reduction algorithm to minimize adder stages.
15
+
16
+ ## Circuit
17
+
18
+ ```
19
+ Inputs: A[1:0], B[1:0] (4 inputs)
20
+ Outputs: P[3:0] (4 outputs)
21
+
22
+ P = A × B (unsigned)
23
+ ```
24
+
25
+ ## Dadda Algorithm
26
+
27
+ Reduces partial product columns to heights in sequence: 2, 3, 4, 6, 9, 13...
28
+ For 2x2, columns are already within limits, so minimal reduction needed.
29
+
30
+ ```
31
+ A1·B0 A0·B0
32
+ A1·B1 A0·B1
33
+ ─────────────────
34
+ P3 P2 P1 P0
35
+ ```
36
+
37
+ ## Parameters
38
+
39
+ | | |
40
+ |---|---|
41
+ | Inputs | 4 |
42
+ | Outputs | 4 |
43
+ | Neurons | 10 |
44
+ | Layers | 3 |
45
+ | Parameters | 44 |
46
+ | Magnitude | 44 |
47
+
48
+ ## License
49
+
50
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-dadda-tree",
3
+ "description": "2x2 Dadda tree multiplier",
4
+ "inputs": 4,
5
+ "outputs": 4,
6
+ "neurons": 10,
7
+ "layers": 3,
8
+ "parameters": 44
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {}
5
+
6
+ # 2x2 Dadda Tree Multiplier
7
+ # Inputs: A1,A0, B1,B0 (4 inputs)
8
+ # Outputs: P3,P2,P1,P0 (4 outputs)
9
+ #
10
+ # Partial products:
11
+ # A1B0 A0B0
12
+ # A1B1 A0B1
13
+ # ----------------
14
+ # P3 P2 P1 P0
15
+ #
16
+ # Dadda reduces column heights to specific sequence: 2,2,3,4,6,9,13...
17
+ # For 2x2, columns are already height 1 or 2, so minimal reduction needed.
18
+
19
+ # Input indices: A1=0, A0=1, B1=2, B0=3
20
+
21
+ def add_and(name, idx_a, idx_b, n_inputs):
22
+ w = [0.0] * n_inputs
23
+ w[idx_a] = 1.0
24
+ w[idx_b] = 1.0
25
+ weights[f'{name}.weight'] = torch.tensor([w], dtype=torch.float32)
26
+ weights[f'{name}.bias'] = torch.tensor([-2.0], dtype=torch.float32)
27
+
28
+ def add_xor_direct(name, n_prev):
29
+ weights[f'{name}.or.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
30
+ weights[f'{name}.or.bias'] = torch.tensor([-1.0], dtype=torch.float32)
31
+ weights[f'{name}.nand.weight'] = torch.tensor([[-1.0, -1.0]], dtype=torch.float32)
32
+ weights[f'{name}.nand.bias'] = torch.tensor([1.0], dtype=torch.float32)
33
+ weights[f'{name}.and.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
34
+ weights[f'{name}.and.bias'] = torch.tensor([-2.0], dtype=torch.float32)
35
+
36
+ # Partial products (AND gates)
37
+ add_and('pp00', 1, 3, 4) # A0 * B0 -> P0
38
+ add_and('pp01', 1, 2, 4) # A0 * B1
39
+ add_and('pp10', 0, 3, 4) # A1 * B0
40
+ add_and('pp11', 0, 2, 4) # A1 * B1
41
+
42
+ # Column 1 (weight 2): pp01 + pp10 -> half adder
43
+ add_xor_direct('ha1_sum', 2)
44
+ weights['ha1_carry.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
45
+ weights['ha1_carry.bias'] = torch.tensor([-2.0], dtype=torch.float32)
46
+
47
+ # Column 2 (weight 4): pp11 + carry from column 1 -> half adder
48
+ add_xor_direct('ha2_sum', 2)
49
+ weights['ha2_carry.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
50
+ weights['ha2_carry.bias'] = torch.tensor([-2.0], dtype=torch.float32)
51
+
52
+ save_file(weights, 'model.safetensors')
53
+
54
+ def dadda_mult(a1, a0, b1, b0):
55
+ pp00 = a0 & b0
56
+ pp01 = a0 & b1
57
+ pp10 = a1 & b0
58
+ pp11 = a1 & b1
59
+
60
+ p0 = pp00
61
+
62
+ # Half adder for column 1
63
+ p1 = pp01 ^ pp10
64
+ c1 = pp01 & pp10
65
+
66
+ # Half adder for column 2
67
+ p2 = pp11 ^ c1
68
+ c2 = pp11 & c1
69
+
70
+ p3 = c2
71
+
72
+ return p3, p2, p1, p0
73
+
74
+ print("Verifying 2x2 Dadda tree multiplier...")
75
+ errors = 0
76
+ for a in range(4):
77
+ for b in range(4):
78
+ a1, a0 = (a >> 1) & 1, a & 1
79
+ b1, b0 = (b >> 1) & 1, b & 1
80
+ p3, p2, p1, p0 = dadda_mult(a1, a0, b1, b0)
81
+ result = p3*8 + p2*4 + p1*2 + p0
82
+ expected = a * b
83
+ if result != expected:
84
+ errors += 1
85
+ print(f"ERROR: {a}*{b} = {result}, expected {expected}")
86
+
87
+ if errors == 0:
88
+ print("All 16 test cases passed!")
89
+ else:
90
+ print(f"FAILED: {errors} errors")
91
+
92
+ mag = sum(t.abs().sum().item() for t in weights.values())
93
+ print(f"Magnitude: {mag:.0f}")
94
+ 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:190502e4d427546b55862398447302774edfa455b6f96b24931d25603b3b4e0d
3
+ size 1864