phanerozoic commited on
Commit
5937f23
·
verified ·
1 Parent(s): b0d3ebe

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +80 -80
  2. config.json +9 -9
  3. create_safetensors.py +63 -63
  4. model.py +23 -23
README.md CHANGED
@@ -1,80 +1,80 @@
1
- ---
2
- license: mit
3
- tags:
4
- - pytorch
5
- - safetensors
6
- - threshold-logic
7
- - neuromorphic
8
- ---
9
-
10
- # threshold-8to3encoder
11
-
12
- 8-to-3 priority encoder. Outputs binary index of highest-priority set input.
13
-
14
- ## Function
15
-
16
- encode(I7..I0) -> (Y2, Y1, Y0)
17
-
18
- Priority: I7 > I6 > I5 > I4 > I3 > I2 > I1 > I0
19
-
20
- ## Example Encodings
21
-
22
- | Input | Highest | Output |
23
- |-------|---------|--------|
24
- | 10000000 | I7 | 111 (7) |
25
- | 01000000 | I6 | 110 (6) |
26
- | 00100000 | I5 | 101 (5) |
27
- | 00010000 | I4 | 100 (4) |
28
- | 00001000 | I3 | 011 (3) |
29
- | 00000100 | I2 | 010 (2) |
30
- | 00000010 | I1 | 001 (1) |
31
- | 00000001 | I0 | 000 (0) |
32
- | 11111111 | I7 | 111 (7) |
33
-
34
- ## Architecture
35
-
36
- Single layer with 3 neurons using weighted priority:
37
-
38
- | Output | Function | Weights [I7..I0] | Bias |
39
- |--------|----------|------------------|------|
40
- | Y2 | I7∨I6∨I5∨I4 | [1,1,1,1,0,0,0,0] | -1 |
41
- | Y1 | Priority bit 1 | [16,16,-4,-4,1,1,0,0] | -1 |
42
- | Y0 | Priority bit 0 | [128,-64,32,-16,8,-4,2,0] | -1 |
43
-
44
- Y1 and Y0 use weighted dominance: higher-priority inputs have larger weights
45
- that override lower-priority inputs through the threshold mechanism.
46
-
47
- ## Parameters
48
-
49
- | | |
50
- |---|---|
51
- | Inputs | 8 |
52
- | Outputs | 3 |
53
- | Neurons | 3 |
54
- | Layers | 1 |
55
- | Parameters | 27 |
56
- | Magnitude | 303 |
57
-
58
- ## Usage
59
-
60
- ```python
61
- from safetensors.torch import load_file
62
- import torch
63
-
64
- w = load_file('model.safetensors')
65
-
66
- def encode8to3(i7, i6, i5, i4, i3, i2, i1, i0):
67
- inp = torch.tensor([float(i7), float(i6), float(i5), float(i4),
68
- float(i3), float(i2), float(i1), float(i0)])
69
- y2 = int((inp @ w['y2.weight'].T + w['y2.bias'] >= 0).item())
70
- y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
71
- y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
72
- return y2, y1, y0
73
-
74
- # I5 is highest set bit
75
- print(encode8to3(0, 0, 1, 0, 1, 0, 0, 0)) # (1, 0, 1) = 5
76
- ```
77
-
78
- ## License
79
-
80
- MIT
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-8to3encoder
11
+
12
+ 8-to-3 priority encoder. Outputs binary index of highest-priority set input.
13
+
14
+ ## Function
15
+
16
+ encode(I7..I0) -> (Y2, Y1, Y0)
17
+
18
+ Priority: I7 > I6 > I5 > I4 > I3 > I2 > I1 > I0
19
+
20
+ ## Example Encodings
21
+
22
+ | Input | Highest | Output |
23
+ |-------|---------|--------|
24
+ | 10000000 | I7 | 111 (7) |
25
+ | 01000000 | I6 | 110 (6) |
26
+ | 00100000 | I5 | 101 (5) |
27
+ | 00010000 | I4 | 100 (4) |
28
+ | 00001000 | I3 | 011 (3) |
29
+ | 00000100 | I2 | 010 (2) |
30
+ | 00000010 | I1 | 001 (1) |
31
+ | 00000001 | I0 | 000 (0) |
32
+ | 11111111 | I7 | 111 (7) |
33
+
34
+ ## Architecture
35
+
36
+ Single layer with 3 neurons using weighted priority:
37
+
38
+ | Output | Function | Weights [I7..I0] | Bias |
39
+ |--------|----------|------------------|------|
40
+ | Y2 | I7∨I6∨I5∨I4 | [1,1,1,1,0,0,0,0] | -1 |
41
+ | Y1 | Priority bit 1 | [16,16,-4,-4,1,1,0,0] | -1 |
42
+ | Y0 | Priority bit 0 | [128,-64,32,-16,8,-4,2,0] | -1 |
43
+
44
+ Y1 and Y0 use weighted dominance: higher-priority inputs have larger weights
45
+ that override lower-priority inputs through the threshold mechanism.
46
+
47
+ ## Parameters
48
+
49
+ | | |
50
+ |---|---|
51
+ | Inputs | 8 |
52
+ | Outputs | 3 |
53
+ | Neurons | 3 |
54
+ | Layers | 1 |
55
+ | Parameters | 27 |
56
+ | Magnitude | 303 |
57
+
58
+ ## Usage
59
+
60
+ ```python
61
+ from safetensors.torch import load_file
62
+ import torch
63
+
64
+ w = load_file('model.safetensors')
65
+
66
+ def encode8to3(i7, i6, i5, i4, i3, i2, i1, i0):
67
+ inp = torch.tensor([float(i7), float(i6), float(i5), float(i4),
68
+ float(i3), float(i2), float(i1), float(i0)])
69
+ y2 = int((inp @ w['y2.weight'].T + w['y2.bias'] >= 0).item())
70
+ y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
71
+ y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
72
+ return y2, y1, y0
73
+
74
+ # I5 is highest set bit
75
+ print(encode8to3(0, 0, 1, 0, 1, 0, 0, 0)) # (1, 0, 1) = 5
76
+ ```
77
+
78
+ ## License
79
+
80
+ MIT
config.json CHANGED
@@ -1,9 +1,9 @@
1
- {
2
- "name": "threshold-8to3encoder",
3
- "description": "8-to-3 priority encoder",
4
- "inputs": 8,
5
- "outputs": 3,
6
- "neurons": 3,
7
- "layers": 1,
8
- "parameters": 27
9
- }
 
1
+ {
2
+ "name": "threshold-8to3encoder",
3
+ "description": "8-to-3 priority encoder",
4
+ "inputs": 8,
5
+ "outputs": 3,
6
+ "neurons": 3,
7
+ "layers": 1,
8
+ "parameters": 27
9
+ }
create_safetensors.py CHANGED
@@ -1,63 +1,63 @@
1
- import torch
2
- from safetensors.torch import save_file
3
-
4
- # Priority encoder: outputs binary index of highest-set input
5
- # Inputs: I7, I6, I5, I4, I3, I2, I1, I0 (I7 is highest priority)
6
- # Outputs: Y2, Y1, Y0 (binary encoding)
7
-
8
- weights = {}
9
-
10
- # Y2: fires when position >= 4 (I7 OR I6 OR I5 OR I4)
11
- weights['y2.weight'] = torch.tensor([[1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
12
- weights['y2.bias'] = torch.tensor([-1.0], dtype=torch.float32)
13
-
14
- # Y1: fires when position has bit 1 set (positions 2,3,6,7)
15
- # I7, I6 dominate; I5, I4 suppress; I3, I2 activate when higher bits absent
16
- weights['y1.weight'] = torch.tensor([[16.0, 16.0, -4.0, -4.0, 1.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
17
- weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32)
18
-
19
- # Y0: fires when position has bit 0 set (positions 1,3,5,7)
20
- # Geometric weights with alternating signs for priority cascade
21
- weights['y0.weight'] = torch.tensor([[128.0, -64.0, 32.0, -16.0, 8.0, -4.0, 2.0, 0.0]], dtype=torch.float32)
22
- weights['y0.bias'] = torch.tensor([-1.0], dtype=torch.float32)
23
-
24
- save_file(weights, 'model.safetensors')
25
-
26
- def encode8to3(i7, i6, i5, i4, i3, i2, i1, i0):
27
- inp = torch.tensor([float(i7), float(i6), float(i5), float(i4),
28
- float(i3), float(i2), float(i1), float(i0)])
29
- y2 = int((inp @ weights['y2.weight'].T + weights['y2.bias'] >= 0).item())
30
- y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
31
- y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
32
- return y2, y1, y0
33
-
34
- print("Verifying 8to3encoder...")
35
- errors = 0
36
- for val in range(256):
37
- bits = [(val >> (7-i)) & 1 for i in range(8)]
38
- y2, y1, y0 = encode8to3(*bits)
39
-
40
- # Expected: binary of highest set bit position
41
- highest = -1
42
- for i in range(8):
43
- if bits[i]:
44
- highest = 7 - i
45
- break
46
-
47
- if highest < 0:
48
- expected = (0, 0, 0)
49
- else:
50
- expected = ((highest >> 2) & 1, (highest >> 1) & 1, highest & 1)
51
-
52
- if (y2, y1, y0) != expected:
53
- errors += 1
54
- if errors <= 10:
55
- print(f"ERROR: I={''.join(map(str,bits))} -> ({y2},{y1},{y0}), expected {expected} (pos {highest})")
56
-
57
- if errors == 0:
58
- print("All 256 test cases passed!")
59
- else:
60
- print(f"FAILED: {errors} errors")
61
-
62
- mag = sum(t.abs().sum().item() for t in weights.values())
63
- print(f"Magnitude: {mag:.0f}")
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ # Priority encoder: outputs binary index of highest-set input
5
+ # Inputs: I7, I6, I5, I4, I3, I2, I1, I0 (I7 is highest priority)
6
+ # Outputs: Y2, Y1, Y0 (binary encoding)
7
+
8
+ weights = {}
9
+
10
+ # Y2: fires when position >= 4 (I7 OR I6 OR I5 OR I4)
11
+ weights['y2.weight'] = torch.tensor([[1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
12
+ weights['y2.bias'] = torch.tensor([-1.0], dtype=torch.float32)
13
+
14
+ # Y1: fires when position has bit 1 set (positions 2,3,6,7)
15
+ # I7, I6 dominate; I5, I4 suppress; I3, I2 activate when higher bits absent
16
+ weights['y1.weight'] = torch.tensor([[16.0, 16.0, -4.0, -4.0, 1.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
17
+ weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32)
18
+
19
+ # Y0: fires when position has bit 0 set (positions 1,3,5,7)
20
+ # Geometric weights with alternating signs for priority cascade
21
+ weights['y0.weight'] = torch.tensor([[128.0, -64.0, 32.0, -16.0, 8.0, -4.0, 2.0, 0.0]], dtype=torch.float32)
22
+ weights['y0.bias'] = torch.tensor([-1.0], dtype=torch.float32)
23
+
24
+ save_file(weights, 'model.safetensors')
25
+
26
+ def encode8to3(i7, i6, i5, i4, i3, i2, i1, i0):
27
+ inp = torch.tensor([float(i7), float(i6), float(i5), float(i4),
28
+ float(i3), float(i2), float(i1), float(i0)])
29
+ y2 = int((inp @ weights['y2.weight'].T + weights['y2.bias'] >= 0).item())
30
+ y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
31
+ y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
32
+ return y2, y1, y0
33
+
34
+ print("Verifying 8to3encoder...")
35
+ errors = 0
36
+ for val in range(256):
37
+ bits = [(val >> (7-i)) & 1 for i in range(8)]
38
+ y2, y1, y0 = encode8to3(*bits)
39
+
40
+ # Expected: binary of highest set bit position
41
+ highest = -1
42
+ for i in range(8):
43
+ if bits[i]:
44
+ highest = 7 - i
45
+ break
46
+
47
+ if highest < 0:
48
+ expected = (0, 0, 0)
49
+ else:
50
+ expected = ((highest >> 2) & 1, (highest >> 1) & 1, highest & 1)
51
+
52
+ if (y2, y1, y0) != expected:
53
+ errors += 1
54
+ if errors <= 10:
55
+ print(f"ERROR: I={''.join(map(str,bits))} -> ({y2},{y1},{y0}), expected {expected} (pos {highest})")
56
+
57
+ if errors == 0:
58
+ print("All 256 test cases passed!")
59
+ else:
60
+ print(f"FAILED: {errors} errors")
61
+
62
+ mag = sum(t.abs().sum().item() for t in weights.values())
63
+ print(f"Magnitude: {mag:.0f}")
model.py CHANGED
@@ -1,23 +1,23 @@
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 encode8to3(i7, i6, i5, i4, i3, i2, i1, i0, weights):
8
- """Priority encoder: returns binary index of highest-set input."""
9
- inp = torch.tensor([float(i7), float(i6), float(i5), float(i4),
10
- float(i3), float(i2), float(i1), float(i0)])
11
- y2 = int((inp @ weights['y2.weight'].T + weights['y2.bias'] >= 0).item())
12
- y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
13
- y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
14
- return y2, y1, y0
15
-
16
- if __name__ == '__main__':
17
- w = load_model()
18
- print('8-to-3 Priority Encoder examples:')
19
- for val in [0b10000000, 0b01000000, 0b00100000, 0b00010000,
20
- 0b00001000, 0b00000100, 0b00000010, 0b00000001, 0b11111111]:
21
- bits = [(val >> (7-i)) & 1 for i in range(8)]
22
- y2, y1, y0 = encode8to3(*bits, w)
23
- print(f' I={"".join(map(str,bits))} -> {y2}{y1}{y0} (={4*y2+2*y1+y0})')
 
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 encode8to3(i7, i6, i5, i4, i3, i2, i1, i0, weights):
8
+ """Priority encoder: returns binary index of highest-set input."""
9
+ inp = torch.tensor([float(i7), float(i6), float(i5), float(i4),
10
+ float(i3), float(i2), float(i1), float(i0)])
11
+ y2 = int((inp @ weights['y2.weight'].T + weights['y2.bias'] >= 0).item())
12
+ y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
13
+ y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
14
+ return y2, y1, y0
15
+
16
+ if __name__ == '__main__':
17
+ w = load_model()
18
+ print('8-to-3 Priority Encoder examples:')
19
+ for val in [0b10000000, 0b01000000, 0b00100000, 0b00010000,
20
+ 0b00001000, 0b00000100, 0b00000010, 0b00000001, 0b11111111]:
21
+ bits = [(val >> (7-i)) & 1 for i in range(8)]
22
+ y2, y1, y0 = encode8to3(*bits, w)
23
+ print(f' I={"".join(map(str,bits))} -> {y2}{y1}{y0} (={4*y2+2*y1+y0})')