Upload folder using huggingface_hub
Browse files- README.md +36 -0
- config.json +9 -0
- create_safetensors.py +77 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- error-correction
|
| 9 |
+
- bch
|
| 10 |
+
- hamming
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# threshold-bch-encoder
|
| 14 |
+
|
| 15 |
+
BCH(7,4) encoder (Hamming code). Encodes 4 data bits to 7-bit codeword. Can correct 1 error.
|
| 16 |
+
|
| 17 |
+
## Encoding
|
| 18 |
+
|
| 19 |
+
```
|
| 20 |
+
D3 D2 D1 D0 → C6 C5 C4 C3 C2 C1 C0
|
| 21 |
+
─────────── ────────
|
| 22 |
+
data bits parity
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
## Parameters
|
| 26 |
+
|
| 27 |
+
| | |
|
| 28 |
+
|---|---|
|
| 29 |
+
| Data bits | 4 |
|
| 30 |
+
| Codeword | 7 |
|
| 31 |
+
| Correction | 1 bit |
|
| 32 |
+
| Parameters | 65 |
|
| 33 |
+
|
| 34 |
+
## License
|
| 35 |
+
|
| 36 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-bch-encoder",
|
| 3 |
+
"description": "BCH(7,4) / Hamming encoder",
|
| 4 |
+
"inputs": 4,
|
| 5 |
+
"outputs": 7,
|
| 6 |
+
"neurons": 13,
|
| 7 |
+
"layers": 2,
|
| 8 |
+
"parameters": 65
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {}
|
| 5 |
+
|
| 6 |
+
# BCH(7,4) Encoder - encodes 4 data bits to 7-bit codeword
|
| 7 |
+
# Hamming(7,4) equivalent - can correct 1 error
|
| 8 |
+
|
| 9 |
+
def add_neuron(name, w_list, bias):
|
| 10 |
+
weights[f'{name}.weight'] = torch.tensor([w_list], dtype=torch.float32)
|
| 11 |
+
weights[f'{name}.bias'] = torch.tensor([bias], dtype=torch.float32)
|
| 12 |
+
|
| 13 |
+
# Input: D3=0, D2=1, D1=2, D0=3
|
| 14 |
+
# Output: C6-C0 where C6-C3 = D3-D0, C2-C0 = parity
|
| 15 |
+
|
| 16 |
+
# Data bits pass through (threshold >= 1)
|
| 17 |
+
add_neuron('c6', [1.0, 0.0, 0.0, 0.0], -1.0) # D3
|
| 18 |
+
add_neuron('c5', [0.0, 1.0, 0.0, 0.0], -1.0) # D2
|
| 19 |
+
add_neuron('c4', [0.0, 0.0, 1.0, 0.0], -1.0) # D1
|
| 20 |
+
add_neuron('c3', [0.0, 0.0, 0.0, 1.0], -1.0) # D0
|
| 21 |
+
|
| 22 |
+
# Parity bits using odd parity (sum mod 2)
|
| 23 |
+
# For 3-input XOR: output 1 if 1 or 3 inputs are 1
|
| 24 |
+
# C2 = D3 XOR D2 XOR D0: fires on 1 or 3 of {D3,D2,D0}
|
| 25 |
+
add_neuron('c2_atleast1', [1.0, 1.0, 0.0, 1.0], -1.0) # >= 1
|
| 26 |
+
add_neuron('c2_atleast2', [1.0, 1.0, 0.0, 1.0], -2.0) # >= 2
|
| 27 |
+
add_neuron('c2_atleast3', [1.0, 1.0, 0.0, 1.0], -3.0) # >= 3
|
| 28 |
+
|
| 29 |
+
# C1 = D3 XOR D1 XOR D0
|
| 30 |
+
add_neuron('c1_atleast1', [1.0, 0.0, 1.0, 1.0], -1.0)
|
| 31 |
+
add_neuron('c1_atleast2', [1.0, 0.0, 1.0, 1.0], -2.0)
|
| 32 |
+
add_neuron('c1_atleast3', [1.0, 0.0, 1.0, 1.0], -3.0)
|
| 33 |
+
|
| 34 |
+
# C0 = D2 XOR D1 XOR D0
|
| 35 |
+
add_neuron('c0_atleast1', [0.0, 1.0, 1.0, 1.0], -1.0)
|
| 36 |
+
add_neuron('c0_atleast2', [0.0, 1.0, 1.0, 1.0], -2.0)
|
| 37 |
+
add_neuron('c0_atleast3', [0.0, 1.0, 1.0, 1.0], -3.0)
|
| 38 |
+
|
| 39 |
+
save_file(weights, 'model.safetensors')
|
| 40 |
+
|
| 41 |
+
def xor3(a, b, c):
|
| 42 |
+
return a ^ b ^ c
|
| 43 |
+
|
| 44 |
+
def bch_encode(d3, d2, d1, d0):
|
| 45 |
+
c6, c5, c4, c3 = d3, d2, d1, d0
|
| 46 |
+
c2 = xor3(d3, d2, d0)
|
| 47 |
+
c1 = xor3(d3, d1, d0)
|
| 48 |
+
c0 = xor3(d2, d1, d0)
|
| 49 |
+
return c6, c5, c4, c3, c2, c1, c0
|
| 50 |
+
|
| 51 |
+
print("Verifying BCH(7,4) encoder...")
|
| 52 |
+
errors = 0
|
| 53 |
+
for d in range(16):
|
| 54 |
+
d3, d2, d1, d0 = (d>>3)&1, (d>>2)&1, (d>>1)&1, d&1
|
| 55 |
+
c = bch_encode(d3, d2, d1, d0)
|
| 56 |
+
|
| 57 |
+
# Verify data preservation
|
| 58 |
+
if (c[0], c[1], c[2], c[3]) != (d3, d2, d1, d0):
|
| 59 |
+
errors += 1
|
| 60 |
+
print(f"Data error: d={d:04b}")
|
| 61 |
+
|
| 62 |
+
# Verify parity
|
| 63 |
+
if c[4] != xor3(d3, d2, d0):
|
| 64 |
+
errors += 1
|
| 65 |
+
if c[5] != xor3(d3, d1, d0):
|
| 66 |
+
errors += 1
|
| 67 |
+
if c[6] != xor3(d2, d1, d0):
|
| 68 |
+
errors += 1
|
| 69 |
+
|
| 70 |
+
if errors == 0:
|
| 71 |
+
print("All 16 test cases passed!")
|
| 72 |
+
else:
|
| 73 |
+
print(f"FAILED: {errors} errors")
|
| 74 |
+
|
| 75 |
+
mag = sum(t.abs().sum().item() for t in weights.values())
|
| 76 |
+
print(f"Magnitude: {mag:.0f}")
|
| 77 |
+
print(f"Parameters: {sum(t.numel() for t in weights.values())}")
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e2a4ff3dc79dcbd9dc96dc2e635a0bb62d44711392719a9305d0e616d43ff895
|
| 3 |
+
size 2100
|