phanerozoic commited on
Commit
25d9c47
·
verified ·
1 Parent(s): 3c882fe

Initial commit: verified MOD-7 threshold circuit

Browse files
Files changed (4) hide show
  1. README.md +80 -0
  2. config.json +31 -0
  3. model.py +68 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - formal-verification
7
+ - coq
8
+ - mod7
9
+ - modular-arithmetic
10
+ - threshold-network
11
+ - neuromorphic
12
+ ---
13
+
14
+ # tiny-mod7-prover
15
+
16
+ Formally verified neural network that computes the MOD-7 function (Hamming weight mod 7) on 8-bit inputs. For Coq source code, see [mod7-verified](https://github.com/CharlesCNorton/mod7-verified).
17
+
18
+ ## Overview
19
+
20
+ Threshold network computing `mod7(x) = HW(x) mod 7` for 8-bit binary inputs. Outputs 0-6 corresponding to the seven residue classes.
21
+
22
+ **Key properties:**
23
+ - 100% accuracy on all 256 possible inputs
24
+ - Correctness proven in Coq (axiom-free)
25
+ - Integer weights, Heaviside activation
26
+ - Part of the verified MOD-m family
27
+
28
+ ## Architecture
29
+
30
+ | Layer | Neurons | Function |
31
+ |-------|---------|----------|
32
+ | Input | 8 | Binary input bits |
33
+ | Hidden 1 | 9 | Thermometer encoding |
34
+ | Hidden 2 | 6 | MOD-7 detection |
35
+ | Output | 7 | Classification |
36
+
37
+ **Total: 22 neurons, 190 parameters**
38
+
39
+ ## Algebraic Insight
40
+
41
+ For MOD-m, use weights `(1, 1, ..., 1, 1-m)` with `m-1` ones before the reset.
42
+
43
+ MOD-7 uses `(1, 1, 1, 1, 1, 1, -6)`:
44
+ ```
45
+ HW=0: cumsum=0, HW=1: cumsum=1, ..., HW=6: cumsum=6
46
+ HW=7: cumsum=0 (reset: 1+1+1+1+1+1-6=0)
47
+ HW=8: cumsum=1
48
+ ```
49
+
50
+ ## Formal Verification
51
+
52
+ ```coq
53
+ Theorem network_correct_exhaustive : verify_all = true.
54
+ Theorem network_correct_constructive : forall x0 x1 x2 x3 x4 x5 x6 x7,
55
+ predict [x0; x1; x2; x3; x4; x5; x6; x7] = mod7 [x0; x1; x2; x3; x4; x5; x6; x7].
56
+ Theorem cumsum_eq_mod7 : forall k,
57
+ (k <= 8)%nat -> cumsum k = Z.of_nat (Nat.modulo k 7).
58
+ ```
59
+
60
+ All proofs axiom-free.
61
+
62
+ ## The MOD-m Family
63
+
64
+ | Model | Function | Neurons | Params | Weight Pattern |
65
+ |-------|----------|---------|--------|----------------|
66
+ | tiny-parity-prover | MOD-2 | 14 | 139 | (1, -1) |
67
+ | tiny-mod3-prover | MOD-3 | 14 | 110 | (1, 1, -2) |
68
+ | tiny-mod5-prover | MOD-5 | 18 | 146 | (1, 1, 1, 1, -4) |
69
+ | **tiny-mod7-prover** | MOD-7 | 22 | 190 | (1, 1, 1, 1, 1, 1, -6) |
70
+
71
+ ## Related
72
+
73
+ - [mod7-verified](https://github.com/CharlesCNorton/mod7-verified) — Coq proofs
74
+ - [tiny-mod5-prover](https://huggingface.co/phanerozoic/tiny-mod5-prover)
75
+ - [tiny-mod3-prover](https://huggingface.co/phanerozoic/tiny-mod3-prover)
76
+ - [tiny-parity-prover](https://huggingface.co/phanerozoic/tiny-parity-prover)
77
+
78
+ ## License
79
+
80
+ MIT
config.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "mod7_classification",
4
+ "architecture": "8 -> 9 -> 6 -> 7",
5
+ "input_size": 8,
6
+ "hidden1_size": 9,
7
+ "hidden2_size": 6,
8
+ "output_size": 7,
9
+ "num_parameters": 190,
10
+ "num_neurons": 22,
11
+ "activation": "heaviside",
12
+ "weight_constraints": "integer",
13
+ "verification": {
14
+ "method": "coq_proof",
15
+ "exhaustive": true,
16
+ "constructive": true,
17
+ "algebraic": true,
18
+ "axiom_free": true
19
+ },
20
+ "accuracy": {
21
+ "all_inputs": "256/256",
22
+ "percentage": 100.0
23
+ },
24
+ "algebraic_insight": "Weights (1,1,1,1,1,1,-6) on thermometer encoding produce cumsum = HW mod 7",
25
+ "github": "https://github.com/CharlesCNorton/mod7-verified",
26
+ "related": {
27
+ "mod5_model": "https://huggingface.co/phanerozoic/tiny-mod5-prover",
28
+ "mod3_model": "https://huggingface.co/phanerozoic/tiny-mod3-prover",
29
+ "parity_model": "https://huggingface.co/phanerozoic/tiny-parity-prover"
30
+ }
31
+ }
model.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Inference code for mod7-verified threshold network.
3
+
4
+ This network computes MOD-7 (Hamming weight mod 7) on 8-bit binary inputs.
5
+ """
6
+
7
+ import torch
8
+ import torch.nn as nn
9
+ from safetensors.torch import load_file
10
+
11
+
12
+ def heaviside(x):
13
+ return (x >= 0).float()
14
+
15
+
16
+ class Mod7Network(nn.Module):
17
+ """
18
+ Verified threshold network for MOD-7 computation.
19
+ Architecture: 8 -> 9 -> 6 -> 7
20
+ """
21
+
22
+ def __init__(self):
23
+ super().__init__()
24
+ self.layer1 = nn.Linear(8, 9)
25
+ self.layer2 = nn.Linear(9, 6)
26
+ self.output = nn.Linear(6, 7)
27
+
28
+ def forward(self, x):
29
+ x = x.float()
30
+ x = heaviside(self.layer1(x))
31
+ x = heaviside(self.layer2(x))
32
+ return self.output(x)
33
+
34
+ def predict(self, x):
35
+ return self.forward(x).argmax(dim=-1)
36
+
37
+ @classmethod
38
+ def from_safetensors(cls, path):
39
+ model = cls()
40
+ weights = load_file(path)
41
+ model.layer1.weight.data = weights['layer1.weight']
42
+ model.layer1.bias.data = weights['layer1.bias']
43
+ model.layer2.weight.data = weights['layer2.weight']
44
+ model.layer2.bias.data = weights['layer2.bias']
45
+ model.output.weight.data = weights['output.weight']
46
+ model.output.bias.data = weights['output.bias']
47
+ return model
48
+
49
+
50
+ def mod7_reference(x):
51
+ return (x.sum(dim=-1) % 7).long()
52
+
53
+
54
+ def verify(model):
55
+ inputs = torch.zeros(256, 8)
56
+ for i in range(256):
57
+ for j in range(8):
58
+ inputs[i, j] = (i >> j) & 1
59
+ targets = mod7_reference(inputs)
60
+ predictions = model.predict(inputs)
61
+ correct = (predictions == targets).sum().item()
62
+ print(f"Verification: {correct}/256 ({100*correct/256:.1f}%)")
63
+ return correct == 256
64
+
65
+
66
+ if __name__ == '__main__':
67
+ model = Mod7Network.from_safetensors('model.safetensors')
68
+ verify(model)
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:18f19f668565235b040177e936da5a4cdf4062357e965c46115a13d9965ad621
3
+ size 1184