| import torch | |
| from safetensors.torch import load_file | |
| def load_model(path='model.safetensors'): | |
| return load_file(path) | |
| def shiftleft4(a3, a2, a1, a0, w): | |
| """Left shift by 1 bit. MSB is lost, 0 shifts in at LSB.""" | |
| inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)]) | |
| return [int((inp @ w[f'y{i}.weight'].T + w[f'y{i}.bias'] >= 0).item()) for i in [3,2,1,0]] | |
| if __name__ == '__main__': | |
| w = load_model() | |
| print('shiftleft4 examples:') | |
| for val in [0b0001, 0b0101, 0b1000, 0b1111]: | |
| a3, a2, a1, a0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1 | |
| result = shiftleft4(a3, a2, a1, a0, w) | |
| print(f' {a3}{a2}{a1}{a0} << 1 = {"".join(map(str, result))}') | |