| ---
|
| 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
|
|
|