| import torch | |
| from safetensors.torch import load_file | |
| def load_model(path='model.safetensors'): | |
| return load_file(path) | |
| def decode_3to8(a2, a1, a0, weights): | |
| """3-to-8 decoder: converts 3-bit binary to one-hot 8-bit output. | |
| Returns list [y0, y1, ..., y7] where yi=1 iff input = i. | |
| """ | |
| inp = torch.tensor([float(a2), float(a1), float(a0)]) | |
| outputs = [] | |
| for i in range(8): | |
| y = int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0) | |
| outputs.append(y) | |
| return outputs | |
| if __name__ == '__main__': | |
| w = load_model() | |
| print('3-to-8 Decoder') | |
| print('Input -> One-hot output') | |
| for val in range(8): | |
| a2, a1, a0 = (val >> 2) & 1, (val >> 1) & 1, val & 1 | |
| outputs = decode_3to8(a2, a1, a0, w) | |
| print(f" {val} ({a2},{a1},{a0}) -> {''.join(map(str, outputs))}") | |