CharlesCNorton commited on
Commit
0fda8f5
·
0 Parent(s):

4-bit binary to Gray code converter, magnitude 33

Browse files
Files changed (5) hide show
  1. README.md +70 -0
  2. config.json +9 -0
  3. create_safetensors.py +86 -0
  4. model.py +40 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-binary2gray
11
+
12
+ 4-bit binary to Gray code converter.
13
+
14
+ ## Function
15
+
16
+ binary2gray(B3, B2, B1, B0) -> (G3, G2, G1, G0)
17
+
18
+ Conversion formulas:
19
+ - G3 = B3
20
+ - G2 = B3 XOR B2
21
+ - G1 = B2 XOR B1
22
+ - G0 = B1 XOR B0
23
+
24
+ ## Example Conversions
25
+
26
+ | Binary | Gray |
27
+ |--------|------|
28
+ | 0000 (0) | 0000 |
29
+ | 0001 (1) | 0001 |
30
+ | 0010 (2) | 0011 |
31
+ | 0011 (3) | 0010 |
32
+ | 0100 (4) | 0110 |
33
+ | 0101 (5) | 0111 |
34
+ | 0110 (6) | 0101 |
35
+ | 0111 (7) | 0100 |
36
+
37
+ ## Architecture
38
+
39
+ Parallel XOR gates for each output bit:
40
+
41
+ ```
42
+ B3 ─────────────────────────► G3
43
+ B3,B2 ─► [XOR] ─────────────► G2
44
+ B2,B1 ─► [XOR] ─────────────► G1
45
+ B1,B0 ─► [XOR] ─────────────► G0
46
+ ```
47
+
48
+ Each XOR uses 3 neurons (OR, NAND, AND) with mag-7 structure.
49
+
50
+ ## Parameters
51
+
52
+ | | |
53
+ |---|---|
54
+ | Inputs | 4 |
55
+ | Outputs | 4 |
56
+ | Neurons | 10 |
57
+ | Layers | 2 |
58
+ | Parameters | 43 |
59
+ | Magnitude | 33 |
60
+
61
+ ## Usage
62
+
63
+ ```python
64
+ from safetensors.torch import load_file
65
+ # Full implementation in model.py
66
+ ```
67
+
68
+ ## License
69
+
70
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-binary2gray",
3
+ "description": "4-bit binary to Gray code converter",
4
+ "inputs": 4,
5
+ "outputs": 4,
6
+ "neurons": 10,
7
+ "layers": 2,
8
+ "parameters": 43
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ # Binary to Gray: G[i] = B[i] XOR B[i+1] (where B[n]=0)
5
+ # G3 = B3
6
+ # G2 = B3 XOR B2
7
+ # G1 = B2 XOR B1
8
+ # G0 = B1 XOR B0
9
+ #
10
+ # Each XOR uses 3 neurons: OR, NAND, AND
11
+
12
+ weights = {}
13
+
14
+ # Inputs: B3, B2, B1, B0
15
+
16
+ # === G3 = B3 (identity) ===
17
+ weights['g3.weight'] = torch.tensor([[2.0, 0.0, 0.0, 0.0]], dtype=torch.float32)
18
+ weights['g3.bias'] = torch.tensor([-1.0], dtype=torch.float32)
19
+
20
+ # === G2 = XOR(B3, B2) ===
21
+ weights['g2_or.weight'] = torch.tensor([[1.0, 1.0, 0.0, 0.0]], dtype=torch.float32)
22
+ weights['g2_or.bias'] = torch.tensor([-1.0], dtype=torch.float32)
23
+ weights['g2_nand.weight'] = torch.tensor([[-1.0, -1.0, 0.0, 0.0]], dtype=torch.float32)
24
+ weights['g2_nand.bias'] = torch.tensor([1.0], dtype=torch.float32)
25
+ weights['g2.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
26
+ weights['g2.bias'] = torch.tensor([-2.0], dtype=torch.float32)
27
+
28
+ # === G1 = XOR(B2, B1) ===
29
+ weights['g1_or.weight'] = torch.tensor([[0.0, 1.0, 1.0, 0.0]], dtype=torch.float32)
30
+ weights['g1_or.bias'] = torch.tensor([-1.0], dtype=torch.float32)
31
+ weights['g1_nand.weight'] = torch.tensor([[0.0, -1.0, -1.0, 0.0]], dtype=torch.float32)
32
+ weights['g1_nand.bias'] = torch.tensor([1.0], dtype=torch.float32)
33
+ weights['g1.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
34
+ weights['g1.bias'] = torch.tensor([-2.0], dtype=torch.float32)
35
+
36
+ # === G0 = XOR(B1, B0) ===
37
+ weights['g0_or.weight'] = torch.tensor([[0.0, 0.0, 1.0, 1.0]], dtype=torch.float32)
38
+ weights['g0_or.bias'] = torch.tensor([-1.0], dtype=torch.float32)
39
+ weights['g0_nand.weight'] = torch.tensor([[0.0, 0.0, -1.0, -1.0]], dtype=torch.float32)
40
+ weights['g0_nand.bias'] = torch.tensor([1.0], dtype=torch.float32)
41
+ weights['g0.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
42
+ weights['g0.bias'] = torch.tensor([-2.0], dtype=torch.float32)
43
+
44
+ save_file(weights, 'model.safetensors')
45
+
46
+ def binary2gray(b3, b2, b1, b0):
47
+ inp = [b3, b2, b1, b0]
48
+
49
+ # G3 = B3
50
+ g3 = int(2*b3 - 1 >= 0)
51
+
52
+ # G2 = XOR(B3, B2)
53
+ g2_or = int(b3 + b2 - 1 >= 0)
54
+ g2_nand = int(-b3 - b2 + 1 >= 0)
55
+ g2 = int(g2_or + g2_nand - 2 >= 0)
56
+
57
+ # G1 = XOR(B2, B1)
58
+ g1_or = int(b2 + b1 - 1 >= 0)
59
+ g1_nand = int(-b2 - b1 + 1 >= 0)
60
+ g1 = int(g1_or + g1_nand - 2 >= 0)
61
+
62
+ # G0 = XOR(B1, B0)
63
+ g0_or = int(b1 + b0 - 1 >= 0)
64
+ g0_nand = int(-b1 - b0 + 1 >= 0)
65
+ g0 = int(g0_or + g0_nand - 2 >= 0)
66
+
67
+ return g3, g2, g1, g0
68
+
69
+ print("Verifying binary2gray...")
70
+ errors = 0
71
+ for i in range(16):
72
+ b3, b2, b1, b0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
73
+ g3, g2, g1, g0 = binary2gray(b3, b2, b1, b0)
74
+ result = g3 * 8 + g2 * 4 + g1 * 2 + g0
75
+
76
+ expected = i ^ (i >> 1) # Standard gray code formula
77
+
78
+ if result != expected:
79
+ errors += 1
80
+ print(f"ERROR: binary {b3}{b2}{b1}{b0} -> gray {g3}{g2}{g1}{g0} (={result}), expected {expected}")
81
+
82
+ if errors == 0:
83
+ print("All 16 test cases passed!")
84
+
85
+ mag = sum(t.abs().sum().item() for t in weights.values())
86
+ print(f"Magnitude: {mag:.0f}")
model.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 binary2gray(b3, b2, b1, b0, weights):
8
+ """Convert 4-bit binary to Gray code."""
9
+ inp = torch.tensor([float(b3), float(b2), float(b1), float(b0)])
10
+
11
+ # G3 = B3
12
+ g3 = int((inp @ weights['g3.weight'].T + weights['g3.bias'] >= 0).item())
13
+
14
+ # G2 = XOR(B3, B2)
15
+ g2_or = int((inp @ weights['g2_or.weight'].T + weights['g2_or.bias'] >= 0).item())
16
+ g2_nand = int((inp @ weights['g2_nand.weight'].T + weights['g2_nand.bias'] >= 0).item())
17
+ g2_vec = torch.tensor([float(g2_or), float(g2_nand)])
18
+ g2 = int((g2_vec @ weights['g2.weight'].T + weights['g2.bias'] >= 0).item())
19
+
20
+ # G1 = XOR(B2, B1)
21
+ g1_or = int((inp @ weights['g1_or.weight'].T + weights['g1_or.bias'] >= 0).item())
22
+ g1_nand = int((inp @ weights['g1_nand.weight'].T + weights['g1_nand.bias'] >= 0).item())
23
+ g1_vec = torch.tensor([float(g1_or), float(g1_nand)])
24
+ g1 = int((g1_vec @ weights['g1.weight'].T + weights['g1.bias'] >= 0).item())
25
+
26
+ # G0 = XOR(B1, B0)
27
+ g0_or = int((inp @ weights['g0_or.weight'].T + weights['g0_or.bias'] >= 0).item())
28
+ g0_nand = int((inp @ weights['g0_nand.weight'].T + weights['g0_nand.bias'] >= 0).item())
29
+ g0_vec = torch.tensor([float(g0_or), float(g0_nand)])
30
+ g0 = int((g0_vec @ weights['g0.weight'].T + weights['g0.bias'] >= 0).item())
31
+
32
+ return g3, g2, g1, g0
33
+
34
+ if __name__ == '__main__':
35
+ w = load_model()
36
+ print('Binary to Gray conversion:')
37
+ for i in range(16):
38
+ b3, b2, b1, b0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
39
+ g3, g2, g1, g0 = binary2gray(b3, b2, b1, b0, w)
40
+ print(f' {b3}{b2}{b1}{b0} ({i:2d}) -> {g3}{g2}{g1}{g0}')
model.safetensors ADDED
Binary file (1.51 kB). View file