CharlesCNorton commited on
Commit
86ab3ed
·
0 Parent(s):

1:8 demultiplexer threshold circuit, magnitude 52

Browse files
Files changed (5) hide show
  1. README.md +65 -0
  2. config.json +9 -0
  3. create_safetensors.py +51 -0
  4. model.py +22 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-demux8
11
+
12
+ 1:8 demultiplexer. Routes data input to one of 8 outputs based on 3-bit select.
13
+
14
+ ## Function
15
+
16
+ DEMUX8(d, s2, s1, s0) -> [y0, y1, ..., y7]
17
+
18
+ yi = d AND (s == i), where s = 4*s2 + 2*s1 + s0
19
+
20
+ ## Architecture
21
+
22
+ Single layer with 8 neurons. Each output yi fires when d=1 AND select matches i.
23
+
24
+ | Output | Weights [d, s2, s1, s0] | Bias |
25
+ |--------|-------------------------|------|
26
+ | y0 | [1, -1, -1, -1] | -1 |
27
+ | y1 | [1, -1, -1, +1] | -2 |
28
+ | y2 | [1, -1, +1, -1] | -2 |
29
+ | y3 | [1, -1, +1, +1] | -3 |
30
+ | y4 | [1, +1, -1, -1] | -2 |
31
+ | y5 | [1, +1, -1, +1] | -3 |
32
+ | y6 | [1, +1, +1, -1] | -3 |
33
+ | y7 | [1, +1, +1, +1] | -4 |
34
+
35
+ ## Parameters
36
+
37
+ | | |
38
+ |---|---|
39
+ | Inputs | 4 (1 data + 3 select) |
40
+ | Outputs | 8 |
41
+ | Neurons | 8 |
42
+ | Layers | 1 |
43
+ | Parameters | 40 |
44
+ | Magnitude | 52 |
45
+
46
+ ## Usage
47
+
48
+ ```python
49
+ from safetensors.torch import load_file
50
+ import torch
51
+
52
+ w = load_file('model.safetensors')
53
+
54
+ def demux8(d, s2, s1, s0):
55
+ inp = torch.tensor([float(d), float(s2), float(s1), float(s0)])
56
+ return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0)
57
+ for i in range(8)]
58
+
59
+ # Route d=1 to output 5 (s=101)
60
+ print(demux8(1, 1, 0, 1)) # [0, 0, 0, 0, 0, 1, 0, 0]
61
+ ```
62
+
63
+ ## License
64
+
65
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-demux8",
3
+ "description": "1:8 demultiplexer as threshold circuit",
4
+ "inputs": 4,
5
+ "outputs": 8,
6
+ "neurons": 8,
7
+ "layers": 1,
8
+ "parameters": 40
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {}
5
+
6
+ # Input order: d, s2, s1, s0 (4 inputs)
7
+ # Output: y0-y7
8
+ # yi = d AND (s == i)
9
+
10
+ for i in range(8):
11
+ w = [1.0] # d weight
12
+ s2_bit = (i >> 2) & 1
13
+ s1_bit = (i >> 1) & 1
14
+ s0_bit = i & 1
15
+ w.append(1.0 if s2_bit else -1.0) # s2
16
+ w.append(1.0 if s1_bit else -1.0) # s1
17
+ w.append(1.0 if s0_bit else -1.0) # s0
18
+ bias = -(1 + bin(i).count('1'))
19
+
20
+ weights[f'y{i}.weight'] = torch.tensor([w], dtype=torch.float32)
21
+ weights[f'y{i}.bias'] = torch.tensor([bias], dtype=torch.float32)
22
+
23
+ save_file(weights, 'model.safetensors')
24
+
25
+ # Verify
26
+ def demux8(d, s2, s1, s0):
27
+ inp = torch.tensor([float(d), float(s2), float(s1), float(s0)])
28
+ outputs = []
29
+ for i in range(8):
30
+ y = int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0)
31
+ outputs.append(y)
32
+ return outputs
33
+
34
+ print("Verifying DEMUX8...")
35
+ errors = 0
36
+ for d in [0, 1]:
37
+ for s in range(8):
38
+ s2, s1, s0 = (s >> 2) & 1, (s >> 1) & 1, s & 1
39
+ result = demux8(d, s2, s1, s0)
40
+ expected = [1 if (d == 1 and i == s) else 0 for i in range(8)]
41
+ if result != expected:
42
+ errors += 1
43
+ print(f"ERROR: d={d}, s={s}, got {result}, expected {expected}")
44
+
45
+ if errors == 0:
46
+ print("All 16 test cases passed!")
47
+ else:
48
+ print(f"FAILED: {errors} errors")
49
+
50
+ mag = sum(t.abs().sum().item() for t in weights.values())
51
+ print(f"Magnitude: {mag:.0f}")
model.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 demux8(d, s2, s1, s0, weights):
8
+ """1:8 Demultiplexer: routes d to output y[s] where s = 4*s2 + 2*s1 + s0"""
9
+ inp = torch.tensor([float(d), float(s2), float(s1), float(s0)])
10
+ outputs = []
11
+ for i in range(8):
12
+ y = int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0)
13
+ outputs.append(y)
14
+ return outputs
15
+
16
+ if __name__ == '__main__':
17
+ w = load_model()
18
+ print('DEMUX8 verification:')
19
+ for s in range(8):
20
+ s2, s1, s0 = (s >> 2) & 1, (s >> 1) & 1, s & 1
21
+ result = demux8(1, s2, s1, s0, w)
22
+ print(f' d=1, s={s} ({s2}{s1}{s0}) -> {"".join(map(str, result))}')
model.safetensors ADDED
Binary file (1.19 kB). View file