CharlesCNorton commited on
Commit
81fa364
·
0 Parent(s):

4-to-2 priority encoder, magnitude 13

Browse files
Files changed (5) hide show
  1. README.md +73 -0
  2. config.json +9 -0
  3. create_safetensors.py +57 -0
  4. model.py +24 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - encoder
9
+ ---
10
+
11
+ # threshold-priorityencoder4
12
+
13
+ 4-to-2 priority encoder. Outputs binary encoding of highest-priority active input.
14
+
15
+ ## Function
16
+
17
+ priority_encode(i3, i2, i1, i0) -> (y1, y0, valid)
18
+
19
+ - i3 = highest priority, i0 = lowest priority
20
+ - y1,y0 = 2-bit binary encoding of highest active input
21
+ - valid = 1 if any input is active
22
+
23
+ ## Truth Table (selected)
24
+
25
+ | i3 | i2 | i1 | i0 | y1 | y0 | v | highest |
26
+ |----|----|----|----|----|----|----|---------|
27
+ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | none |
28
+ | 0 | 0 | 0 | 1 | 0 | 0 | 1 | i0 |
29
+ | 0 | 0 | 1 | X | 0 | 1 | 1 | i1 |
30
+ | 0 | 1 | X | X | 1 | 0 | 1 | i2 |
31
+ | 1 | X | X | X | 1 | 1 | 1 | i3 |
32
+
33
+ ## Architecture
34
+
35
+ Single layer with 3 neurons:
36
+
37
+ - y1 = i3 OR i2: weights [1,1,0,0], bias -1
38
+ - y0 = i3 OR (i1 AND NOT i2): weights [2,-1,1,0], bias -1
39
+ - v = i3 OR i2 OR i1 OR i0: weights [1,1,1,1], bias -1
40
+
41
+ ## Parameters
42
+
43
+ | | |
44
+ |---|---|
45
+ | Inputs | 4 |
46
+ | Outputs | 3 |
47
+ | Neurons | 3 |
48
+ | Layers | 1 |
49
+ | Parameters | 15 |
50
+ | Magnitude | 13 |
51
+
52
+ ## Usage
53
+
54
+ ```python
55
+ from safetensors.torch import load_file
56
+ import torch
57
+
58
+ w = load_file('model.safetensors')
59
+
60
+ def priority_encode(i3, i2, i1, i0):
61
+ inp = torch.tensor([float(i3), float(i2), float(i1), float(i0)])
62
+ y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
63
+ y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
64
+ v = int((inp @ w['v.weight'].T + w['v.bias'] >= 0).item())
65
+ return y1, y0, v
66
+
67
+ print(priority_encode(0, 1, 1, 0)) # (1, 0, 1) -> i2 is highest
68
+ print(priority_encode(1, 1, 1, 1)) # (1, 1, 1) -> i3 is highest
69
+ ```
70
+
71
+ ## License
72
+
73
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-priorityencoder4",
3
+ "description": "4-to-2 priority encoder as threshold circuit",
4
+ "inputs": 4,
5
+ "outputs": 3,
6
+ "neurons": 3,
7
+ "layers": 1,
8
+ "parameters": 15
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ # Input order: i3, i2, i1, i0 (i3 = highest priority)
5
+ # Outputs: y1, y0 (binary encoding), v (valid)
6
+
7
+ weights = {
8
+ # y1 = i3 OR i2
9
+ 'y1.weight': torch.tensor([[1.0, 1.0, 0.0, 0.0]], dtype=torch.float32),
10
+ 'y1.bias': torch.tensor([-1.0], dtype=torch.float32),
11
+ # y0 = i3 OR (NOT i2 AND i1)
12
+ 'y0.weight': torch.tensor([[2.0, -1.0, 1.0, 0.0]], dtype=torch.float32),
13
+ 'y0.bias': torch.tensor([-1.0], dtype=torch.float32),
14
+ # v = i3 OR i2 OR i1 OR i0
15
+ 'v.weight': torch.tensor([[1.0, 1.0, 1.0, 1.0]], dtype=torch.float32),
16
+ 'v.bias': torch.tensor([-1.0], dtype=torch.float32),
17
+ }
18
+ save_file(weights, 'model.safetensors')
19
+
20
+ def priority_encode(i3, i2, i1, i0):
21
+ inp = torch.tensor([float(i3), float(i2), float(i1), float(i0)])
22
+ y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
23
+ y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
24
+ v = int((inp @ weights['v.weight'].T + weights['v.bias'] >= 0).item())
25
+ return y1, y0, v
26
+
27
+ print("Verifying priorityencoder4...")
28
+ errors = 0
29
+ for val in range(16):
30
+ i3, i2, i1, i0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1
31
+ y1, y0, v = priority_encode(i3, i2, i1, i0)
32
+
33
+ # Determine expected output
34
+ if i3:
35
+ exp_idx, exp_v = 3, 1
36
+ elif i2:
37
+ exp_idx, exp_v = 2, 1
38
+ elif i1:
39
+ exp_idx, exp_v = 1, 1
40
+ elif i0:
41
+ exp_idx, exp_v = 0, 1
42
+ else:
43
+ exp_idx, exp_v = 0, 0
44
+
45
+ exp_y1, exp_y0 = (exp_idx >> 1) & 1, exp_idx & 1
46
+ if exp_v == 0:
47
+ exp_y1, exp_y0 = 0, 0 # don't care, but we output 0
48
+
49
+ if v != exp_v or (v == 1 and (y1 != exp_y1 or y0 != exp_y0)):
50
+ errors += 1
51
+ print(f"ERROR: {i3}{i2}{i1}{i0} -> y1={y1},y0={y0},v={v}, expected {exp_y1},{exp_y0},{exp_v}")
52
+
53
+ if errors == 0:
54
+ print("All 16 test cases passed!")
55
+
56
+ mag = sum(t.abs().sum().item() for t in weights.values())
57
+ print(f"Magnitude: {mag:.0f}")
model.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 priority_encode(i3, i2, i1, i0, weights):
8
+ """4-to-2 priority encoder. Returns (y1, y0, valid)."""
9
+ inp = torch.tensor([float(i3), float(i2), float(i1), float(i0)])
10
+ y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
11
+ y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
12
+ v = int((inp @ weights['v.weight'].T + weights['v.bias'] >= 0).item())
13
+ return y1, y0, v
14
+
15
+ if __name__ == '__main__':
16
+ w = load_model()
17
+ print('Priority Encoder 4')
18
+ print('i3 i2 i1 i0 | y1 y0 v | highest')
19
+ print('-' * 35)
20
+ for val in range(16):
21
+ i3, i2, i1, i0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1
22
+ y1, y0, v = priority_encode(i3, i2, i1, i0, w)
23
+ highest = 'none' if v == 0 else f'i{2*y1 + y0}'
24
+ print(f' {i3} {i2} {i1} {i0} | {y1} {y0} {v} | {highest}')
model.safetensors ADDED
Binary file (444 Bytes). View file