phanerozoic commited on
Commit
9c4af73
Β·
verified Β·
1 Parent(s): cb82bf6

Rename from tiny-2OutOf8-verified

Browse files
Files changed (4) hide show
  1. README.md +81 -0
  2. config.json +22 -0
  3. model.py +51 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-2outof8
11
+
12
+ At-least-two detector for 8 inputs. Fires when 2 or more bits are set.
13
+
14
+ ## Circuit
15
+
16
+ ```
17
+ xβ‚€ x₁ xβ‚‚ x₃ xβ‚„ xβ‚… x₆ x₇
18
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
19
+ β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”Όβ”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜
20
+ β–Ό
21
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
22
+ β”‚ w: all 1β”‚
23
+ β”‚ b: -2 β”‚
24
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
25
+ β”‚
26
+ β–Ό
27
+ HW β‰₯ 2
28
+ ```
29
+
30
+ ## Mechanism
31
+
32
+ - Sum = (number of 1s) - 2
33
+ - Fires when Hamming weight β‰₯ 2
34
+
35
+ A single active input isn't enough. Needs corroboration from at least one other.
36
+
37
+ ## k-out-of-8 Family
38
+
39
+ | Circuit | Bias | Fires when |
40
+ |---------|------|------------|
41
+ | 1-out-of-8 | -1 | HW β‰₯ 1 |
42
+ | **2-out-of-8** | **-2** | **HW β‰₯ 2 (this)** |
43
+ | 3-out-of-8 | -3 | HW β‰₯ 3 |
44
+ | ... | ... | ... |
45
+
46
+ All share weights [1,1,1,1,1,1,1,1]. Only the bias differs.
47
+
48
+ ## Parameters
49
+
50
+ | | |
51
+ |---|---|
52
+ | Weights | [1, 1, 1, 1, 1, 1, 1, 1] |
53
+ | Bias | -2 |
54
+ | Total | 9 parameters |
55
+
56
+ ## Usage
57
+
58
+ ```python
59
+ from safetensors.torch import load_file
60
+ import torch
61
+
62
+ w = load_file('model.safetensors')
63
+
64
+ def at_least_2(bits):
65
+ inputs = torch.tensor([float(b) for b in bits])
66
+ return int((inputs * w['weight']).sum() + w['bias'] >= 0)
67
+ ```
68
+
69
+ ## Files
70
+
71
+ ```
72
+ threshold-2outof8/
73
+ β”œβ”€β”€ model.safetensors
74
+ β”œβ”€β”€ model.py
75
+ β”œβ”€β”€ config.json
76
+ └── README.md
77
+ ```
78
+
79
+ ## License
80
+
81
+ MIT
config.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "2_out_of_8_threshold",
4
+ "architecture": "8 -> 1",
5
+ "input_size": 8,
6
+ "output_size": 1,
7
+ "num_neurons": 1,
8
+ "num_parameters": 9,
9
+ "threshold": 2,
10
+ "activation": "heaviside",
11
+ "weight_constraints": "integer",
12
+ "verification": {
13
+ "method": "coq_proof",
14
+ "exhaustive": true,
15
+ "inputs_tested": 256
16
+ },
17
+ "accuracy": {
18
+ "all_inputs": "256/256",
19
+ "percentage": 100.0
20
+ },
21
+ "github": "https://github.com/CharlesCNorton/coq-circuits"
22
+ }
model.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Threshold Network for 2-out-of-8 Gate
3
+
4
+ A formally verified single-neuron threshold network.
5
+ Outputs 1 when at least 2 of the 8 inputs are true.
6
+ """
7
+
8
+ import torch
9
+ from safetensors.torch import load_file
10
+
11
+
12
+ class Threshold2OutOf8:
13
+ """
14
+ 2-out-of-8 threshold gate.
15
+
16
+ Circuit: output = (sum of inputs - 2 >= 0)
17
+ Fires when hamming weight >= 2.
18
+ """
19
+
20
+ def __init__(self, weights_dict):
21
+ self.weight = weights_dict['weight']
22
+ self.bias = weights_dict['bias']
23
+
24
+ def __call__(self, bits):
25
+ inputs = torch.tensor([float(b) for b in bits])
26
+ weighted_sum = (inputs * self.weight).sum() + self.bias
27
+ return (weighted_sum >= 0).float()
28
+
29
+ @classmethod
30
+ def from_safetensors(cls, path="model.safetensors"):
31
+ return cls(load_file(path))
32
+
33
+
34
+ def forward(x, weights):
35
+ x = torch.as_tensor(x, dtype=torch.float32)
36
+ weighted_sum = (x * weights['weight']).sum(dim=-1) + weights['bias']
37
+ return (weighted_sum >= 0).float()
38
+
39
+
40
+ if __name__ == "__main__":
41
+ weights = load_file("model.safetensors")
42
+ model = Threshold2OutOf8(weights)
43
+
44
+ print("2-out-of-8 Gate Tests:")
45
+ print("-" * 35)
46
+ for hw in range(9):
47
+ bits = [1]*hw + [0]*(8-hw)
48
+ out = int(model(bits).item())
49
+ expected = 1 if hw >= 2 else 0
50
+ status = "OK" if out == expected else "FAIL"
51
+ print(f"HW={hw}: {out} [{status}]")
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cdcb4d3d18f2cceb7d6883eaf5167593396b226264cc357b772ab22db6c434ea
3
+ size 164