File size: 3,767 Bytes
aeab535 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
---
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
|