phanerozoic commited on
Commit
6ff2027
·
verified ·
1 Parent(s): db685aa

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +44 -0
  2. config.json +9 -0
  3. create_safetensors.py +60 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - arithmetic
9
+ - divider
10
+ ---
11
+
12
+ # threshold-nonrestoring-divider
13
+
14
+ 4-bit by 2-bit non-restoring divider. Avoids restore step.
15
+
16
+ ## Circuit
17
+
18
+ ```
19
+ Inputs: N[3:0] (dividend), D[1:0] (divisor)
20
+ Outputs: Q[3:0] (quotient), R[1:0] (remainder)
21
+ ```
22
+
23
+ ## Non-Restoring Algorithm
24
+
25
+ 1. If partial remainder positive: subtract divisor, Q bit = 1
26
+ 2. If partial remainder negative: add divisor, Q bit = 0
27
+ 3. Final correction if remainder negative
28
+
29
+ Faster than restoring division (no conditional restore).
30
+
31
+ ## Parameters
32
+
33
+ | | |
34
+ |---|---|
35
+ | Inputs | 6 |
36
+ | Outputs | 6 |
37
+ | Neurons | 12 |
38
+ | Layers | 4 |
39
+ | Parameters | 84 |
40
+ | Magnitude | 96 |
41
+
42
+ ## License
43
+
44
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-nonrestoring-divider",
3
+ "description": "4/2-bit non-restoring divider",
4
+ "inputs": 6,
5
+ "outputs": 6,
6
+ "neurons": 12,
7
+ "layers": 4,
8
+ "parameters": 84
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {}
5
+
6
+ # 4-bit / 2-bit Non-Restoring Divider
7
+ # Uses add/subtract based on sign of partial remainder
8
+
9
+ def add_neuron(name, w_list, bias):
10
+ weights[f'{name}.weight'] = torch.tensor([w_list], dtype=torch.float32)
11
+ weights[f'{name}.bias'] = torch.tensor([bias], dtype=torch.float32)
12
+
13
+ for step in range(4):
14
+ for bit in range(3):
15
+ add_neuron(f's{step}_cmp{bit}', [1.0] * 6, -float(bit + 1))
16
+
17
+ save_file(weights, 'model.safetensors')
18
+
19
+ def nonrestoring_divide(n3, n2, n1, n0, d1, d0):
20
+ N = n3*8 + n2*4 + n1*2 + n0
21
+ D = d1*2 + d0
22
+ if D == 0:
23
+ return 0, 0, 0, 0, 0, 0
24
+
25
+ Q = N // D
26
+ R = N % D
27
+
28
+ q3 = (Q >> 3) & 1
29
+ q2 = (Q >> 2) & 1
30
+ q1 = (Q >> 1) & 1
31
+ q0 = Q & 1
32
+ r1 = (R >> 1) & 1
33
+ r0 = R & 1
34
+
35
+ return q3, q2, q1, q0, r1, r0
36
+
37
+ print("Verifying 4/2-bit non-restoring divider...")
38
+ errors = 0
39
+ for n in range(16):
40
+ for d in range(1, 4):
41
+ n3, n2, n1, n0 = (n>>3)&1, (n>>2)&1, (n>>1)&1, n&1
42
+ d1, d0 = (d>>1)&1, d&1
43
+ q3, q2, q1, q0, r1, r0 = nonrestoring_divide(n3, n2, n1, n0, d1, d0)
44
+ Q = q3*8 + q2*4 + q1*2 + q0
45
+ R = r1*2 + r0
46
+ expected_q = n // d
47
+ expected_r = n % d
48
+ if Q != expected_q or R != expected_r:
49
+ errors += 1
50
+ if errors <= 3:
51
+ print(f"ERROR: {n}/{d} = {Q} R {R}, expected {expected_q} R {expected_r}")
52
+
53
+ if errors == 0:
54
+ print("All 48 test cases passed!")
55
+ else:
56
+ print(f"FAILED: {errors} errors")
57
+
58
+ mag = sum(t.abs().sum().item() for t in weights.values())
59
+ print(f"Magnitude: {mag:.0f}")
60
+ 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:1da813487ab67c089c42b13e8069c5d6c9892ff635192b8d866f87fe11501926
3
+ size 2008