phanerozoic's picture
Upload folder using huggingface_hub
aeab535 verified
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- encoder
---
# threshold-priorityencoder
8-to-3 priority encoder. Outputs the binary index of the highest-priority active input.
## Circuit
```
xβ‚€ x₁ xβ‚‚ x₃ xβ‚„ xβ‚… x₆ x₇
β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”Όβ”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚ β”‚
β–Ό β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”
β”‚winβ‚€ β”‚...β”‚win₃ β”‚...β”‚win₇ β”‚ Layer 1: Winner detectors
β”‚+1,0..β”‚ β”‚inhibitβ”‚ β”‚just β”‚ (8 neurons)
β”‚b=-1 β”‚ β”‚higher β”‚ β”‚x₇ β”‚
β””β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚ β”‚
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
β”‚ yβ‚€ OR β”‚β”‚ yβ‚‚ OR β”‚ Layer 2: Output encoding
β”‚ 1,3,5,7 β”‚β”‚ 4,5,6,7 β”‚ (4 neurons)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β–Ό β–Ό
yβ‚€ y₁ yβ‚‚ valid
```
## Mechanism
**Winner Detection**: Each position has a neuron that fires only when:
1. That input is active (weight +1)
2. No higher-priority input is active (weight -1 on each higher input)
```
winnerβ‚…: fires when xβ‚…=1 AND x₆=0 AND x₇=0
weights: [0, 0, 0, 0, 0, +1, -1, -1]
bias: -1
```
**Output Encoding**: The 3-bit output is assembled by OR-ing the appropriate winners:
- yβ‚€ = OR(win₁, win₃, winβ‚…, win₇) β€” odd indices
- y₁ = OR(winβ‚‚, win₃, win₆, win₇) β€” indices with bit 1 set
- yβ‚‚ = OR(winβ‚„, winβ‚…, win₆, win₇) β€” indices with bit 2 set
## Truth Table (samples)
| Active inputs | Winner | Output (yβ‚‚y₁yβ‚€) | Index |
|---------------|--------|-----------------|-------|
| xβ‚€ only | winβ‚€ | 000 | 0 |
| x₃ only | win₃ | 011 | 3 |
| x₇ only | win₇ | 111 | 7 |
| xβ‚€, x₃ | win₃ | 011 | 3 |
| xβ‚‚, xβ‚…, x₆ | win₆ | 110 | 6 |
| all | win₇ | 111 | 7 |
| none | none | 000 | 0* |
*valid=0 when no inputs active
## Priority Convention
Highest index wins. x₇ has absolute priority over all others.
| Input | Priority |
|-------|----------|
| x₇ | Highest |
| x₆ | ... |
| ... | ... |
| xβ‚€ | Lowest |
## Architecture
| Layer | Neurons | Function |
|-------|---------|----------|
| 1 | 8 | Winner detectors |
| 2 | 4 | Output OR gates (yβ‚€, y₁, yβ‚‚, valid) |
**Total: 12 neurons, 96 parameters, 2 layers**
## The Inhibition Principle
The key insight: each winner neuron is *inhibited* by all higher-priority inputs.
```
winner₃ weights: [0, 0, 0, +1, -1, -1, -1, -1]
x₃ xβ‚„ xβ‚… x₆ x₇
```
If any of xβ‚„-x₇ is active, the negative weight cancels x₃'s contribution.
## Usage
```python
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def priority_encode(bits):
"""Returns (y2, y1, y0, valid)"""
# See model.py for full implementation
pass
# Multiple active: highest wins
bits = [1, 0, 1, 0, 0, 1, 0, 0] # x0, x2, x5 active
y2, y1, y0, valid = priority_encode(bits)
# Result: (1, 0, 1, 1) = index 5
```
## Files
```
threshold-priorityencoder/
β”œβ”€β”€ model.safetensors
β”œβ”€β”€ model.py
β”œβ”€β”€ config.json
└── README.md
```
## License
MIT