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