phanerozoic commited on
Commit
895f4c7
·
verified ·
1 Parent(s): 4583bd2

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +41 -0
  2. config.json +9 -0
  3. create_safetensors.py +68 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - sequential
9
+ - counter
10
+ ---
11
+
12
+ # threshold-graycode-counter
13
+
14
+ 4-bit Gray code counter. Only one bit changes per transition.
15
+
16
+ ## Sequence
17
+
18
+ ```
19
+ 0000 → 0001 → 0011 → 0010 → 0110 → 0111 → 0101 → 0100 →
20
+ 1100 → 1101 → 1111 → 1110 → 1010 → 1011 → 1001 → 1000 → 0000
21
+ ```
22
+
23
+ ## Advantages
24
+
25
+ - No glitches during transitions
26
+ - Ideal for position encoders
27
+ - Reduces switching noise
28
+
29
+ ## Parameters
30
+
31
+ | | |
32
+ |---|---|
33
+ | Inputs | 4 |
34
+ | Outputs | 4 |
35
+ | Neurons | 4 |
36
+ | Parameters | 20 |
37
+ | Magnitude | 8 |
38
+
39
+ ## License
40
+
41
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-graycode-counter",
3
+ "description": "4-bit Gray code counter next-state logic",
4
+ "inputs": 4,
5
+ "outputs": 4,
6
+ "neurons": 4,
7
+ "layers": 1,
8
+ "parameters": 20
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {}
5
+
6
+ # 4-bit Gray Code Counter
7
+ # Counts in Gray code sequence where only one bit changes per step
8
+ # Input: G[3:0] (current Gray code)
9
+ # Output: N[3:0] (next Gray code)
10
+
11
+ def add_neuron(name, w_list, bias):
12
+ weights[f'{name}.weight'] = torch.tensor([w_list], dtype=torch.float32)
13
+ weights[f'{name}.bias'] = torch.tensor([bias], dtype=torch.float32)
14
+
15
+ # Gray code sequence: 0,1,3,2,6,7,5,4,12,13,15,14,10,11,9,8
16
+ # We store transition logic for each bit
17
+
18
+ for i in range(4):
19
+ add_neuron(f'g{i}_keep', [1.0 if j == 3-i else 0.0 for j in range(4)], -1.0)
20
+
21
+ save_file(weights, 'model.safetensors')
22
+
23
+ def binary_to_gray(b):
24
+ return b ^ (b >> 1)
25
+
26
+ def gray_to_binary(g):
27
+ b = g
28
+ shift = 1
29
+ while (g >> shift) > 0:
30
+ b ^= (g >> shift)
31
+ shift += 1
32
+ return b
33
+
34
+ def graycode_counter(g3, g2, g1, g0):
35
+ G = g3*8 + g2*4 + g1*2 + g0
36
+ B = gray_to_binary(G)
37
+ B_next = (B + 1) % 16
38
+ G_next = binary_to_gray(B_next)
39
+ return (G_next >> 3) & 1, (G_next >> 2) & 1, (G_next >> 1) & 1, G_next & 1
40
+
41
+ print("Verifying 4-bit Gray code counter...")
42
+ errors = 0
43
+ for b in range(16):
44
+ g = binary_to_gray(b)
45
+ g3, g2, g1, g0 = (g>>3)&1, (g>>2)&1, (g>>1)&1, g&1
46
+ n3, n2, n1, n0 = graycode_counter(g3, g2, g1, g0)
47
+ result = n3*8 + n2*4 + n1*2 + n0
48
+ expected = binary_to_gray((b + 1) % 16)
49
+
50
+ # Verify only one bit changed
51
+ diff = result ^ g
52
+ hamming = bin(diff).count('1')
53
+
54
+ if result != expected:
55
+ errors += 1
56
+ print(f"ERROR: {g:04b} -> {result:04b}, expected {expected:04b}")
57
+ elif hamming != 1:
58
+ errors += 1
59
+ print(f"ERROR: {g:04b} -> {result:04b}, changed {hamming} bits (should be 1)")
60
+
61
+ if errors == 0:
62
+ print("All 16 test cases passed!")
63
+ else:
64
+ print(f"FAILED: {errors} errors")
65
+
66
+ mag = sum(t.abs().sum().item() for t in weights.values())
67
+ print(f"Magnitude: {mag:.0f}")
68
+ print(f"Parameters: {sum(t.numel() for t in weights.values())}")
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fb3d1dca40576f6bdcb06b2f28c663da872f32d32b7db5aefbd6a90fd290ec38
3
+ size 632