CharlesCNorton commited on
Commit ·
26fe3f1
1
Parent(s): 0e59122
8-bit parity tree
Browse files- README.md +96 -0
- config.json +9 -0
- create_safetensors.py +92 -0
- model.safetensors +0 -0
README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- parity
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# threshold-parity-tree
|
| 12 |
+
|
| 13 |
+
8-bit parity computation using balanced XOR tree structure for minimum depth.
|
| 14 |
+
|
| 15 |
+
## Circuit
|
| 16 |
+
|
| 17 |
+
```
|
| 18 |
+
x7 ─┬─[XOR]─┐
|
| 19 |
+
x6 ─┘ ├─[XOR]─┐
|
| 20 |
+
x5 ─┬─[XOR]─┘ │
|
| 21 |
+
x4 ─┘ ├─[XOR]──► parity
|
| 22 |
+
x3 ─┬─[XOR]─┐ │
|
| 23 |
+
x2 ─┘ ├─[XOR]─┘
|
| 24 |
+
x1 ─┬─[XOR]─┘
|
| 25 |
+
x0 ─┘
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
## Output
|
| 29 |
+
|
| 30 |
+
- **parity = 0**: Even number of 1s in input
|
| 31 |
+
- **parity = 1**: Odd number of 1s in input
|
| 32 |
+
|
| 33 |
+
## Tree vs Linear
|
| 34 |
+
|
| 35 |
+
```
|
| 36 |
+
Linear chain (depth 7):
|
| 37 |
+
x0─[XOR]─[XOR]─[XOR]─[XOR]─[XOR]─[XOR]─[XOR]─► parity
|
| 38 |
+
x1 x2 x3 x4 x5 x6 x7
|
| 39 |
+
|
| 40 |
+
Tree structure (depth 3):
|
| 41 |
+
Layer 1: 4 parallel XORs
|
| 42 |
+
Layer 2: 2 parallel XORs
|
| 43 |
+
Layer 3: 1 final XOR
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
## Architecture
|
| 47 |
+
|
| 48 |
+
| Layer | XOR Gates | Function |
|
| 49 |
+
|-------|-----------|----------|
|
| 50 |
+
| 1 | 4 | Pairs: (0,1), (2,3), (4,5), (6,7) |
|
| 51 |
+
| 2 | 2 | Pairs: (01,23), (45,67) |
|
| 52 |
+
| 3 | 1 | Final: (0123,4567) |
|
| 53 |
+
|
| 54 |
+
**Total: 21 neurons (7 XORs × 3), 111 parameters, 3 layers**
|
| 55 |
+
|
| 56 |
+
## Scalability
|
| 57 |
+
|
| 58 |
+
For n-bit parity:
|
| 59 |
+
- Tree depth: log₂(n)
|
| 60 |
+
- XOR gates: n - 1
|
| 61 |
+
- Linear depth: n - 1
|
| 62 |
+
|
| 63 |
+
| Bits | Tree Depth | Linear Depth |
|
| 64 |
+
|------|------------|--------------|
|
| 65 |
+
| 8 | 3 | 7 |
|
| 66 |
+
| 16 | 4 | 15 |
|
| 67 |
+
| 32 | 5 | 31 |
|
| 68 |
+
| 64 | 6 | 63 |
|
| 69 |
+
|
| 70 |
+
## Usage
|
| 71 |
+
|
| 72 |
+
```python
|
| 73 |
+
from safetensors.torch import load_file
|
| 74 |
+
|
| 75 |
+
w = load_file('model.safetensors')
|
| 76 |
+
|
| 77 |
+
# Examples:
|
| 78 |
+
# 0x00 (00000000) → parity=0
|
| 79 |
+
# 0x01 (00000001) → parity=1
|
| 80 |
+
# 0x03 (00000011) → parity=0
|
| 81 |
+
# 0xFF (11111111) → parity=0
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
## Files
|
| 85 |
+
|
| 86 |
+
```
|
| 87 |
+
threshold-parity-tree/
|
| 88 |
+
├── model.safetensors
|
| 89 |
+
├── create_safetensors.py
|
| 90 |
+
├── config.json
|
| 91 |
+
└── README.md
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
## License
|
| 95 |
+
|
| 96 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-parity-tree",
|
| 3 |
+
"description": "8-bit parity using balanced XOR tree",
|
| 4 |
+
"inputs": 8,
|
| 5 |
+
"outputs": 1,
|
| 6 |
+
"neurons": 21,
|
| 7 |
+
"layers": 3,
|
| 8 |
+
"parameters": 111
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {}
|
| 5 |
+
|
| 6 |
+
# 8-bit Parity Tree
|
| 7 |
+
# Inputs: x7,x6,x5,x4,x3,x2,x1,x0 (8 inputs)
|
| 8 |
+
# Output: parity (1 output)
|
| 9 |
+
#
|
| 10 |
+
# Tree structure (balanced, depth 3):
|
| 11 |
+
# Layer 1: XOR pairs (4 XORs)
|
| 12 |
+
# p01 = x0 XOR x1
|
| 13 |
+
# p23 = x2 XOR x3
|
| 14 |
+
# p45 = x4 XOR x5
|
| 15 |
+
# p67 = x6 XOR x7
|
| 16 |
+
# Layer 2: XOR pairs (2 XORs)
|
| 17 |
+
# p0123 = p01 XOR p23
|
| 18 |
+
# p4567 = p45 XOR p67
|
| 19 |
+
# Layer 3: Final XOR
|
| 20 |
+
# parity = p0123 XOR p4567
|
| 21 |
+
|
| 22 |
+
def add_xor_from_inputs(name, a_idx, b_idx, total_inputs):
|
| 23 |
+
w_or = [0.0] * total_inputs
|
| 24 |
+
w_or[a_idx] = 1.0
|
| 25 |
+
w_or[b_idx] = 1.0
|
| 26 |
+
weights[f'{name}.or.weight'] = torch.tensor([w_or], dtype=torch.float32)
|
| 27 |
+
weights[f'{name}.or.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 28 |
+
w_nand = [0.0] * total_inputs
|
| 29 |
+
w_nand[a_idx] = -1.0
|
| 30 |
+
w_nand[b_idx] = -1.0
|
| 31 |
+
weights[f'{name}.nand.weight'] = torch.tensor([w_nand], dtype=torch.float32)
|
| 32 |
+
weights[f'{name}.nand.bias'] = torch.tensor([1.0], dtype=torch.float32)
|
| 33 |
+
weights[f'{name}.and.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
|
| 34 |
+
weights[f'{name}.and.bias'] = torch.tensor([-2.0], dtype=torch.float32)
|
| 35 |
+
|
| 36 |
+
def add_xor(name):
|
| 37 |
+
weights[f'{name}.or.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
|
| 38 |
+
weights[f'{name}.or.bias'] = torch.tensor([-1.0], dtype=torch.float32)
|
| 39 |
+
weights[f'{name}.nand.weight'] = torch.tensor([[-1.0, -1.0]], dtype=torch.float32)
|
| 40 |
+
weights[f'{name}.nand.bias'] = torch.tensor([1.0], dtype=torch.float32)
|
| 41 |
+
weights[f'{name}.and.weight'] = torch.tensor([[1.0, 1.0]], dtype=torch.float32)
|
| 42 |
+
weights[f'{name}.and.bias'] = torch.tensor([-2.0], dtype=torch.float32)
|
| 43 |
+
|
| 44 |
+
# Layer 1: XOR pairs from inputs
|
| 45 |
+
add_xor_from_inputs('xor01', 0, 1, 8)
|
| 46 |
+
add_xor_from_inputs('xor23', 2, 3, 8)
|
| 47 |
+
add_xor_from_inputs('xor45', 4, 5, 8)
|
| 48 |
+
add_xor_from_inputs('xor67', 6, 7, 8)
|
| 49 |
+
|
| 50 |
+
# Layer 2: XOR pairs
|
| 51 |
+
add_xor('xor0123')
|
| 52 |
+
add_xor('xor4567')
|
| 53 |
+
|
| 54 |
+
# Layer 3: Final XOR
|
| 55 |
+
add_xor('parity')
|
| 56 |
+
|
| 57 |
+
save_file(weights, 'model.safetensors')
|
| 58 |
+
|
| 59 |
+
def eval_xor(a, b):
|
| 60 |
+
return int((a or b) and not (a and b))
|
| 61 |
+
|
| 62 |
+
def parity_tree(x):
|
| 63 |
+
bits = [(x >> i) & 1 for i in range(8)]
|
| 64 |
+
p01 = eval_xor(bits[0], bits[1])
|
| 65 |
+
p23 = eval_xor(bits[2], bits[3])
|
| 66 |
+
p45 = eval_xor(bits[4], bits[5])
|
| 67 |
+
p67 = eval_xor(bits[6], bits[7])
|
| 68 |
+
p0123 = eval_xor(p01, p23)
|
| 69 |
+
p4567 = eval_xor(p45, p67)
|
| 70 |
+
return eval_xor(p0123, p4567)
|
| 71 |
+
|
| 72 |
+
print("Verifying 8-bit Parity Tree...")
|
| 73 |
+
errors = 0
|
| 74 |
+
for x in range(256):
|
| 75 |
+
result = parity_tree(x)
|
| 76 |
+
expected = bin(x).count('1') % 2
|
| 77 |
+
if result != expected:
|
| 78 |
+
errors += 1
|
| 79 |
+
if errors <= 5:
|
| 80 |
+
print(f"ERROR: x={x:08b} ({x}), got {result}, expected {expected}")
|
| 81 |
+
|
| 82 |
+
if errors == 0:
|
| 83 |
+
print("All 256 test cases passed!")
|
| 84 |
+
else:
|
| 85 |
+
print(f"FAILED: {errors} errors")
|
| 86 |
+
|
| 87 |
+
mag = sum(t.abs().sum().item() for t in weights.values())
|
| 88 |
+
print(f"Magnitude: {mag:.0f}")
|
| 89 |
+
print(f"Parameters: {sum(t.numel() for t in weights.values())}")
|
| 90 |
+
|
| 91 |
+
print("\nTree depth: 3 (vs 7 for linear chain)")
|
| 92 |
+
print("Total XOR gates: 7")
|
model.safetensors
ADDED
|
Binary file (3.48 kB). View file
|
|
|