|
|
---
|
|
|
license: mit
|
|
|
tags:
|
|
|
- pytorch
|
|
|
- safetensors
|
|
|
- threshold-logic
|
|
|
- neuromorphic
|
|
|
- modular-arithmetic
|
|
|
---
|
|
|
|
|
|
# threshold-mod4
|
|
|
|
|
|
Computes Hamming weight mod 4 directly on inputs. Single-layer circuit using repeated weight pattern.
|
|
|
|
|
|
## Circuit
|
|
|
|
|
|
```
|
|
|
xβ xβ xβ xβ xβ xβ
xβ xβ
|
|
|
β β β β β β β β
|
|
|
β β β β β β β β
|
|
|
w: 1 1 1 -3 1 1 1 -3
|
|
|
ββββ΄βββ΄βββ΄βββΌβββ΄βββ΄βββ΄βββ
|
|
|
βΌ
|
|
|
βββββββββββ
|
|
|
β b: 0 β
|
|
|
βββββββββββ
|
|
|
β
|
|
|
βΌ
|
|
|
HW mod 4
|
|
|
```
|
|
|
|
|
|
## Algebraic Insight
|
|
|
|
|
|
The pattern `(1, 1, 1, -3)` repeats twice across 8 inputs:
|
|
|
|
|
|
- Positions 1-3: weight +1 each
|
|
|
- Position 4: weight -3 (reset: 1+1+1-3 = 0)
|
|
|
- Positions 5-7: weight +1 each
|
|
|
- Position 8: weight -3 (reset again)
|
|
|
|
|
|
Every 4 bits, the sum resets. For 8 bits, two complete cycles.
|
|
|
|
|
|
```
|
|
|
HW=0: sum=0 β 0 mod 4
|
|
|
HW=1: sum=1 β 1 mod 4
|
|
|
HW=2: sum=2 β 2 mod 4
|
|
|
HW=3: sum=3 β 3 mod 4
|
|
|
HW=4: sum=0 β 0 mod 4 (reset)
|
|
|
...
|
|
|
```
|
|
|
|
|
|
## Parameters
|
|
|
|
|
|
| | |
|
|
|
|---|---|
|
|
|
| Weights | [1, 1, 1, -3, 1, 1, 1, -3] |
|
|
|
| Bias | 0 |
|
|
|
| Total | 9 parameters |
|
|
|
|
|
|
## MOD-m Family
|
|
|
|
|
|
| m | Weight pattern |
|
|
|
|---|----------------|
|
|
|
| 3 | (1, 1, -2) |
|
|
|
| **4** | **(1, 1, 1, -3)** |
|
|
|
| 5 | (1, 1, 1, 1, -4) |
|
|
|
| m | (1, ..., 1, 1-m) with m-1 ones |
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
```python
|
|
|
from safetensors.torch import load_file
|
|
|
import torch
|
|
|
|
|
|
w = load_file('model.safetensors')
|
|
|
|
|
|
def mod4(bits):
|
|
|
inputs = torch.tensor([float(b) for b in bits])
|
|
|
return int((inputs * w['weight']).sum() + w['bias'])
|
|
|
```
|
|
|
|
|
|
## Files
|
|
|
|
|
|
```
|
|
|
threshold-mod4/
|
|
|
βββ model.safetensors
|
|
|
βββ model.py
|
|
|
βββ config.json
|
|
|
βββ README.md
|
|
|
```
|
|
|
|
|
|
## License
|
|
|
|
|
|
MIT
|
|
|
|