threshold-prefix-or

4-bit parallel prefix OR operation. Computes running OR from MSB to each position. Used in leading-one detection and any-ones-above detection.

Circuit

    x3      x2      x1      x0
     β”‚       β”‚       β”‚       β”‚
     β–Ό       β–Ό       β–Ό       β–Ό
   β”Œβ”€β”€β”€β”   β”Œβ”€β”€β”€β”   β”Œβ”€β”€β”€β”   β”Œβ”€β”€β”€β”
   β”‚y3 β”‚   β”‚y2 β”‚   β”‚y1 β”‚   β”‚y0 β”‚
   β”‚>=1β”‚   β”‚>=1β”‚   β”‚>=1β”‚   β”‚>=1β”‚
   β””β”€β”€β”€β”˜   β””β”€β”€β”€β”˜   β””β”€β”€β”€β”˜   β””β”€β”€β”€β”˜
     β”‚       β”‚       β”‚       β”‚
     β–Ό       β–Ό       β–Ό       β–Ό
   (x3)  (x3|x2) (x3|x2|x1) (all)

Function

prefix_or(x3, x2, x1, x0) -> (y3, y2, y1, y0)

y3 = x3
y2 = x3 OR x2
y1 = x3 OR x2 OR x1
y0 = x3 OR x2 OR x1 OR x0

Each output yi is the OR of all inputs from x3 down to xi.

Truth Table (selected)

x3 x2 x1 x0 y3 y2 y1 y0 Meaning
0 0 0 0 0 0 0 0 All zeros
0 0 0 1 0 0 0 1 Only LSB set
0 0 1 0 0 0 1 1 First one at pos 1
0 1 0 0 0 1 1 1 First one at pos 2
1 0 0 0 1 1 1 1 First one at MSB
1 1 1 1 1 1 1 1 All ones
1 0 1 0 1 1 1 1 Mixed pattern

Mechanism

Single-layer parallel implementation:

Output Condition Weights Bias
y3 x3 >= 1 [1,0,0,0] -1
y2 x3 + x2 >= 1 [1,1,0,0] -1
y1 x3 + x2 + x1 >= 1 [1,1,1,0] -1
y0 x3 + x2 + x1 + x0 >= 1 [1,1,1,1] -1

All use bias -1 (fires when at least one relevant input is 1).

Parameters

Inputs 4
Outputs 4
Neurons 4
Layers 1
Parameters 20
Magnitude 14

Applications

  • Leading-one detection: Transition from 0β†’1 in output marks MSB position
  • Any-bit-set prefix: y_i = 1 means some bit from MSB to position i is set
  • Carry kill detection: In adders, prefix-OR of "kill" signals

Usage

from safetensors.torch import load_file
import torch

w = load_file('model.safetensors')

def prefix_or(x3, x2, x1, x0):
    inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
    y3 = int((inp @ w['y3.weight'].T + w['y3.bias'] >= 0).item())
    y2 = int((inp @ w['y2.weight'].T + w['y2.bias'] >= 0).item())
    y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
    y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
    return y3, y2, y1, y0

print(prefix_or(0, 0, 1, 0))  # (0, 0, 1, 1)
print(prefix_or(1, 0, 0, 0))  # (1, 1, 1, 1)

License

MIT

Downloads last month
5
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Collection including phanerozoic/threshold-prefix-or