phanerozoic's picture
Upload folder using huggingface_hub
8b64896 verified
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- error-correction
- hamming-code
---
# threshold-hamming74decoder
Hamming(7,4) decoder with single-error correction. Takes a 7-bit codeword (possibly corrupted) and outputs the corrected 4 data bits.
## Circuit Overview
```
c1 c2 c3 c4 c5 c6 c7
β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
└──┴──┴──┴──┴──┴──┴─────────────────────┐
β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
β–Ό β–Ό β–Ό β–Ό β–Ό β–Ό β–Ό β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ Syndrome Computer β”‚ β”‚
β”‚ s1 = c1βŠ•c3βŠ•c5βŠ•c7 β”‚ β”‚
β”‚ s2 = c2βŠ•c3βŠ•c6βŠ•c7 β”‚ β”‚
β”‚ s3 = c4βŠ•c5βŠ•c6βŠ•c7 β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ s1,s2,s3 β”‚
β–Ό β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ Error Locator β”‚ β”‚
β”‚ flip3 = s1∧s2∧¬s3 β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ flip5 = s1∧¬s2∧s3 β”‚ β”‚ c3,c5,c6,c7
β”‚ flip6 = Β¬s1∧s2∧s3 β”‚ β”‚
β”‚ flip7 = s1∧s2∧s3 β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚
β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Corrector β”‚
β”‚ d1 = c3 βŠ• flip3 β”‚
β”‚ d2 = c5 βŠ• flip5 β”‚
β”‚ d3 = c6 βŠ• flip6 β”‚
β”‚ d4 = c7 βŠ• flip7 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
d1 d2 d3 d4
```
## Decoding Algorithm
**Step 1: Compute Syndrome**
The syndrome is a 3-bit value that indicates the error position:
| s3 | s2 | s1 | Decimal | Meaning |
|----|----|----|---------|---------|
| 0 | 0 | 0 | 0 | No error |
| 0 | 0 | 1 | 1 | Error in c1 (parity) |
| 0 | 1 | 0 | 2 | Error in c2 (parity) |
| 0 | 1 | 1 | 3 | Error in c3 (d1) |
| 1 | 0 | 0 | 4 | Error in c4 (parity) |
| 1 | 0 | 1 | 5 | Error in c5 (d2) |
| 1 | 1 | 0 | 6 | Error in c6 (d3) |
| 1 | 1 | 1 | 7 | Error in c7 (d4) |
**Step 2: Locate and Correct**
Only data positions (3, 5, 6, 7) need correction in the output. Parity bit errors (positions 1, 2, 4) don't affect data extraction.
**Step 3: Extract Data**
Data bits are at positions 3, 5, 6, 7 of the codeword, XORed with their flip signals.
## 4-Way XOR Implementation
Each syndrome bit requires a 4-input XOR:
```
XOR(a,b,c,d) = XOR(XOR(a,b), XOR(c,d))
a b c d
β”‚ β”‚ β”‚ β”‚
β””β”€β”¬β”€β”˜ β””β”€β”¬β”€β”˜
β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”
β”‚ XOR β”‚ β”‚ XOR β”‚ Layer 1-2
β””β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
β–Ό
β”Œβ”€β”€β”€β”€β”€β”
β”‚ XOR β”‚ Layer 3-4
β””β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
XOR(a,b,c,d)
```
## Architecture
| Stage | Component | Neurons | Layers |
|-------|-----------|---------|--------|
| Syndrome | 3 Γ— 4-way XOR | 18 | 4 |
| Error Locator | 4 detectors | 4 | 1 |
| Corrector | 4 Γ— 2-way XOR | 12 | 2 |
| **Total** | | **34** | **6** |
Note: Syndrome computation and final XOR stages run in parallel where possible.
## Error Correction Examples
```
Original: 1011 β†’ encode β†’ 0110011
Corrupted: 0110011 β†’ flip bit 5 β†’ 0110111
Syndrome: s1=1, s2=0, s3=1 β†’ position 5
Corrected: d1=1, d2=0, d3=1, d4=1 βœ“
Original: 0000 β†’ encode β†’ 0000000
Corrupted: 0000000 β†’ flip bit 7 β†’ 0000001
Syndrome: s1=1, s2=1, s3=1 β†’ position 7
Corrected: d1=0, d2=0, d3=0, d4=0 βœ“
```
## Limitations
- Corrects **single-bit** errors only
- **Detects** double-bit errors (non-zero syndrome, wrong correction)
- Cannot distinguish 2-bit errors from 1-bit errors
For stronger protection, use Hamming(7,4) + overall parity (SECDED).
## Usage
```python
from safetensors.torch import load_file
w = load_file('model.safetensors')
def hamming74_decode(codeword):
"""Decode 7-bit Hamming codeword to 4 data bits with error correction"""
# See model.py for full implementation
pass
# Received corrupted codeword (error at position 3)
received = [0, 1, 0, 0, 0, 1, 1] # Should be [0,1,1,0,0,1,1]
data = hamming74_decode(received)
# Returns [1, 0, 1, 1] (corrected)
```
## Files
```
threshold-hamming74decoder/
β”œβ”€β”€ model.safetensors
β”œβ”€β”€ model.py
β”œβ”€β”€ config.json
└── README.md
```
## License
MIT