Rename from tiny-mod7-verified
Browse files- README.md +94 -0
- config.json +31 -0
- model.py +68 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- modular-arithmetic
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# threshold-mod7
|
| 12 |
+
|
| 13 |
+
Computes Hamming weight mod 7 for 8-bit inputs. Multi-layer network with thermometer encoding.
|
| 14 |
+
|
| 15 |
+
## Circuit
|
| 16 |
+
|
| 17 |
+
```
|
| 18 |
+
x₀ x₁ x₂ x₃ x₄ x₅ x₆ x₇
|
| 19 |
+
│ │ │ │ │ │ │ │
|
| 20 |
+
└──┴──┴──┴──┼──┴──┴──┴──┘
|
| 21 |
+
▼
|
| 22 |
+
┌─────────────┐
|
| 23 |
+
│ Thermometer │ Layer 1: 9 neurons
|
| 24 |
+
└─────────────┘
|
| 25 |
+
│
|
| 26 |
+
▼
|
| 27 |
+
┌─────────────┐
|
| 28 |
+
│ MOD-7 │ Layer 2: 6 neurons
|
| 29 |
+
│ Detection │ Pattern (1,1,1,1,1,1,-6)
|
| 30 |
+
└─────────────┘
|
| 31 |
+
│
|
| 32 |
+
▼
|
| 33 |
+
┌─────────────┐
|
| 34 |
+
│ Classify │ Output: 7 classes
|
| 35 |
+
└─────────────┘
|
| 36 |
+
│
|
| 37 |
+
▼
|
| 38 |
+
{0, 1, 2, 3, 4, 5, 6}
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
## Algebraic Insight
|
| 42 |
+
|
| 43 |
+
Pattern `(1, 1, 1, 1, 1, 1, -6)` cycles mod 7:
|
| 44 |
+
|
| 45 |
+
```
|
| 46 |
+
HW=0: sum=0 → 0 mod 7
|
| 47 |
+
...
|
| 48 |
+
HW=6: sum=6 → 6 mod 7
|
| 49 |
+
HW=7: sum=0 → 0 mod 7 (reset: 1+1+1+1+1+1-6=0)
|
| 50 |
+
HW=8: sum=1 → 1 mod 7
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
For 8-bit inputs, only one reset occurs (at HW=7).
|
| 54 |
+
|
| 55 |
+
## Architecture
|
| 56 |
+
|
| 57 |
+
| Layer | Neurons | Function |
|
| 58 |
+
|-------|---------|----------|
|
| 59 |
+
| Input | 8 | Binary bits |
|
| 60 |
+
| Hidden 1 | 9 | Thermometer encoding |
|
| 61 |
+
| Hidden 2 | 6 | MOD-7 detection |
|
| 62 |
+
| Output | 7 | One-hot classification |
|
| 63 |
+
|
| 64 |
+
**Total: 22 neurons, 190 parameters**
|
| 65 |
+
|
| 66 |
+
## Usage
|
| 67 |
+
|
| 68 |
+
```python
|
| 69 |
+
from safetensors.torch import load_file
|
| 70 |
+
import torch
|
| 71 |
+
|
| 72 |
+
w = load_file('model.safetensors')
|
| 73 |
+
|
| 74 |
+
def forward(x):
|
| 75 |
+
x = x.float()
|
| 76 |
+
x = (x @ w['layer1.weight'].T + w['layer1.bias'] >= 0).float()
|
| 77 |
+
x = (x @ w['layer2.weight'].T + w['layer2.bias'] >= 0).float()
|
| 78 |
+
out = x @ w['output.weight'].T + w['output.bias']
|
| 79 |
+
return out.argmax(dim=-1)
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
## Files
|
| 83 |
+
|
| 84 |
+
```
|
| 85 |
+
threshold-mod7/
|
| 86 |
+
├── model.safetensors
|
| 87 |
+
├── model.py
|
| 88 |
+
├── config.json
|
| 89 |
+
└── README.md
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
## License
|
| 93 |
+
|
| 94 |
+
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
|