phanerozoic commited on
Commit
914cb8c
·
verified ·
1 Parent(s): d578387

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +36 -0
  2. config.json +9 -0
  3. create_safetensors.py +55 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - arithmetic
9
+ ---
10
+
11
+ # threshold-sqrt
12
+
13
+ 4-bit integer square root. Computes floor(√X).
14
+
15
+ ## Truth Table
16
+
17
+ | X | √X |
18
+ |---|---|
19
+ | 0 | 0 |
20
+ | 1-3 | 1 |
21
+ | 4-8 | 2 |
22
+ | 9-15 | 3 |
23
+
24
+ ## Parameters
25
+
26
+ | | |
27
+ |---|---|
28
+ | Inputs | 4 |
29
+ | Outputs | 2 |
30
+ | Neurons | 3 |
31
+ | Parameters | 15 |
32
+ | Magnitude | 48 |
33
+
34
+ ## License
35
+
36
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-sqrt",
3
+ "description": "4-bit integer square root",
4
+ "inputs": 4,
5
+ "outputs": 2,
6
+ "neurons": 3,
7
+ "layers": 1,
8
+ "parameters": 15
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {}
5
+
6
+ # 4-bit Integer Square Root
7
+ # Input: X[3:0] (0-15)
8
+ # Output: Y[1:0] (floor(sqrt(X)))
9
+ # 0-0:0, 1-3:1, 4-8:2, 9-15:3
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
+ # sqrt(X) >= 1 when X >= 1
16
+ add_neuron('ge1', [1.0, 1.0, 1.0, 1.0], -1.0) # X >= 1
17
+
18
+ # sqrt(X) >= 2 when X >= 4
19
+ add_neuron('ge4', [8.0, 4.0, 2.0, 1.0], -4.0) # X >= 4
20
+
21
+ # sqrt(X) >= 3 when X >= 9
22
+ add_neuron('ge9', [8.0, 4.0, 2.0, 1.0], -9.0) # X >= 9
23
+
24
+ save_file(weights, 'model.safetensors')
25
+
26
+ def isqrt(x3, x2, x1, x0):
27
+ X = x3*8 + x2*4 + x1*2 + x0
28
+ if X == 0:
29
+ return 0, 0
30
+ elif X < 4:
31
+ return 0, 1
32
+ elif X < 9:
33
+ return 1, 0
34
+ else:
35
+ return 1, 1
36
+
37
+ print("Verifying 4-bit integer square root...")
38
+ errors = 0
39
+ for x in range(16):
40
+ x3, x2, x1, x0 = (x>>3)&1, (x>>2)&1, (x>>1)&1, x&1
41
+ y1, y0 = isqrt(x3, x2, x1, x0)
42
+ result = y1*2 + y0
43
+ expected = int(x ** 0.5)
44
+ if result != expected:
45
+ errors += 1
46
+ print(f"ERROR: sqrt({x}) = {result}, expected {expected}")
47
+
48
+ if errors == 0:
49
+ print("All 16 test cases passed!")
50
+ else:
51
+ print(f"FAILED: {errors} errors")
52
+
53
+ mag = sum(t.abs().sum().item() for t in weights.values())
54
+ print(f"Magnitude: {mag:.0f}")
55
+ 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:6c9ff11a0cc6f3fcb8e01a89a0079f6c26c4228821eb7629b200af6597bbeff6
3
+ size 452