CharlesCNorton commited on
Commit
1bda0cb
·
0 Parent(s):

4-bit left shift, magnitude 7

Browse files
Files changed (5) hide show
  1. README.md +53 -0
  2. config.json +9 -0
  3. create_safetensors.py +50 -0
  4. model.py +18 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-shiftleft4
11
+
12
+ 4-bit logical left shift by 1 bit.
13
+
14
+ ## Function
15
+
16
+ shiftleft4(a3, a2, a1, a0) = [a2, a1, a0, 0]
17
+
18
+ MSB (a3) is discarded, 0 shifts in at LSB.
19
+
20
+ ## Examples
21
+
22
+ | Input | Output |
23
+ |-------|--------|
24
+ | 0001 | 0010 |
25
+ | 0101 | 1010 |
26
+ | 1000 | 0000 |
27
+ | 1111 | 1110 |
28
+
29
+ ## Architecture
30
+
31
+ Single layer with 4 neurons copying bits to shifted positions.
32
+
33
+ | Output | Source | Weights [a3,a2,a1,a0] | Bias |
34
+ |--------|--------|------------------------|------|
35
+ | y3 | a2 | [0, 1, 0, 0] | -1 |
36
+ | y2 | a1 | [0, 0, 1, 0] | -1 |
37
+ | y1 | a0 | [0, 0, 0, 1] | -1 |
38
+ | y0 | 0 | [0, 0, 0, 0] | -1 |
39
+
40
+ ## Parameters
41
+
42
+ | | |
43
+ |---|---|
44
+ | Inputs | 4 |
45
+ | Outputs | 4 |
46
+ | Neurons | 4 |
47
+ | Layers | 1 |
48
+ | Parameters | 8 |
49
+ | Magnitude | 7 |
50
+
51
+ ## License
52
+
53
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-shiftleft4",
3
+ "description": "4-bit left shift by 1",
4
+ "inputs": 4,
5
+ "outputs": 4,
6
+ "neurons": 4,
7
+ "layers": 1,
8
+ "parameters": 8
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {}
5
+
6
+ # Input order: [a3, a2, a1, a0]
7
+ # Left shift: output = [a2, a1, a0, 0]
8
+
9
+ # y3 = a2
10
+ weights['y3.weight'] = torch.tensor([[0.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
11
+ weights['y3.bias'] = torch.tensor([-1.0], dtype=torch.float32)
12
+
13
+ # y2 = a1
14
+ weights['y2.weight'] = torch.tensor([[0.0, 0.0, 1.0, 0.0]], dtype=torch.float32)
15
+ weights['y2.bias'] = torch.tensor([-1.0], dtype=torch.float32)
16
+
17
+ # y1 = a0
18
+ weights['y1.weight'] = torch.tensor([[0.0, 0.0, 0.0, 1.0]], dtype=torch.float32)
19
+ weights['y1.bias'] = torch.tensor([-1.0], dtype=torch.float32)
20
+
21
+ # y0 = 0 (constant)
22
+ weights['y0.weight'] = torch.tensor([[0.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
23
+ weights['y0.bias'] = torch.tensor([-1.0], dtype=torch.float32)
24
+
25
+ save_file(weights, 'model.safetensors')
26
+
27
+ # Verify
28
+ def shiftleft4(a3, a2, a1, a0):
29
+ inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
30
+ y3 = int((inp @ weights['y3.weight'].T + weights['y3.bias'] >= 0).item())
31
+ y2 = int((inp @ weights['y2.weight'].T + weights['y2.bias'] >= 0).item())
32
+ y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
33
+ y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
34
+ return [y3, y2, y1, y0]
35
+
36
+ print("Verifying shiftleft4...")
37
+ errors = 0
38
+ for i in range(16):
39
+ a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
40
+ result = shiftleft4(a3, a2, a1, a0)
41
+ expected = [a2, a1, a0, 0]
42
+ if result != expected:
43
+ errors += 1
44
+ print(f"ERROR: {a3}{a2}{a1}{a0} -> {result}, expected {expected}")
45
+
46
+ if errors == 0:
47
+ print("All 16 test cases passed!")
48
+
49
+ mag = sum(t.abs().sum().item() for t in weights.values())
50
+ print(f"Magnitude: {mag:.0f}")
model.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 shiftleft4(a3, a2, a1, a0, w):
8
+ """Left shift by 1 bit. MSB is lost, 0 shifts in at LSB."""
9
+ inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
10
+ return [int((inp @ w[f'y{i}.weight'].T + w[f'y{i}.bias'] >= 0).item()) for i in [3,2,1,0]]
11
+
12
+ if __name__ == '__main__':
13
+ w = load_model()
14
+ print('shiftleft4 examples:')
15
+ for val in [0b0001, 0b0101, 0b1000, 0b1111]:
16
+ a3, a2, a1, a0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1
17
+ result = shiftleft4(a3, a2, a1, a0, w)
18
+ print(f' {a3}{a2}{a1}{a0} << 1 = {"".join(map(str, result))}')
model.safetensors ADDED
Binary file (592 Bytes). View file