phanerozoic commited on
Commit
3c9a514
Β·
verified Β·
1 Parent(s): 670b70f

Rename from tiny-3OutOf8-verified

Browse files
Files changed (4) hide show
  1. README.md +80 -0
  2. config.json +22 -0
  3. model.py +51 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-3outof8
11
+
12
+ At-least-three detector. Fires when 3 or more of 8 inputs 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: -3 β”‚
24
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
25
+ β”‚
26
+ β–Ό
27
+ HW β‰₯ 3
28
+ ```
29
+
30
+ ## Mechanism
31
+
32
+ - Sum = (number of 1s) - 3
33
+ - Fires when Hamming weight β‰₯ 3
34
+
35
+ Two active inputs aren't enough. Needs a third to cross threshold.
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 |
43
+ | **3-out-of-8** | **-3** | **HW β‰₯ 3 (this)** |
44
+ | 4-out-of-8 | -4 | HW β‰₯ 4 |
45
+ | ... | ... | ... |
46
+
47
+ ## Parameters
48
+
49
+ | | |
50
+ |---|---|
51
+ | Weights | [1, 1, 1, 1, 1, 1, 1, 1] |
52
+ | Bias | -3 |
53
+ | Total | 9 parameters |
54
+
55
+ ## Usage
56
+
57
+ ```python
58
+ from safetensors.torch import load_file
59
+ import torch
60
+
61
+ w = load_file('model.safetensors')
62
+
63
+ def at_least_3(bits):
64
+ inputs = torch.tensor([float(b) for b in bits])
65
+ return int((inputs * w['weight']).sum() + w['bias'] >= 0)
66
+ ```
67
+
68
+ ## Files
69
+
70
+ ```
71
+ threshold-3outof8/
72
+ β”œβ”€β”€ model.safetensors
73
+ β”œβ”€β”€ model.py
74
+ β”œβ”€β”€ config.json
75
+ └── README.md
76
+ ```
77
+
78
+ ## License
79
+
80
+ MIT
config.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "3_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": 3,
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 3-out-of-8 Gate
3
+
4
+ A formally verified single-neuron threshold network.
5
+ Outputs 1 when at least 3 of the 8 inputs are true.
6
+ """
7
+
8
+ import torch
9
+ from safetensors.torch import load_file
10
+
11
+
12
+ class Threshold3OutOf8:
13
+ """
14
+ 3-out-of-8 threshold gate.
15
+
16
+ Circuit: output = (sum of inputs - 3 >= 0)
17
+ Fires when hamming weight >= 3.
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 = Threshold3OutOf8(weights)
43
+
44
+ print("3-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 >= 3 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:d7c400c212c6559f53a41c37603644629a101482bad4b96f2b11c979f6cb8254
3
+ size 164