CharlesCNorton
4-to-2 priority encoder, magnitude 13
81fa364
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def priority_encode(i3, i2, i1, i0, weights):
"""4-to-2 priority encoder. Returns (y1, y0, valid)."""
inp = torch.tensor([float(i3), float(i2), float(i1), float(i0)])
y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
v = int((inp @ weights['v.weight'].T + weights['v.bias'] >= 0).item())
return y1, y0, v
if __name__ == '__main__':
w = load_model()
print('Priority Encoder 4')
print('i3 i2 i1 i0 | y1 y0 v | highest')
print('-' * 35)
for val in range(16):
i3, i2, i1, i0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1
y1, y0, v = priority_encode(i3, i2, i1, i0, w)
highest = 'none' if v == 0 else f'i{2*y1 + y0}'
print(f' {i3} {i2} {i1} {i0} | {y1} {y0} {v} | {highest}')