phanerozoic's picture
Upload folder using huggingface_hub
96dfc17 verified
raw
history blame contribute delete
802 Bytes
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def encode4to2(i3, i2, i1, i0, weights):
"""Priority encoder: returns binary index of highest-set input."""
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())
return y1, y0
if __name__ == '__main__':
w = load_model()
print('4-to-2 Priority Encoder:')
for val in range(16):
i3, i2, i1, i0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1
y1, y0 = encode4to2(i3, i2, i1, i0, w)
print(f' {i3}{i2}{i1}{i0} -> {y1}{y0} (={2*y1+y0})')