import torch from safetensors.torch import load_file def load_model(path='model.safetensors'): return load_file(path) def reverse4(a3, a2, a1, a0, w): """Reverse bit order of 4-bit input.""" inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)]) 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] if __name__ == '__main__': w = load_model() print('reverse4 examples:') for val in [0b0001, 0b1000, 0b0110, 0b1010, 0b1111]: a3, a2, a1, a0 = (val >> 3) & 1, (val >> 2) & 1, (val >> 1) & 1, val & 1 result = reverse4(a3, a2, a1, a0, w) print(f' {a3}{a2}{a1}{a0} -> {"".join(map(str, result))}')