Sign extend a 4-bit signed integer to 8 bits.
Single layer with 8 neurons. Each neuron passes through one input bit:
1from safetensors.torch import load_file
2import torch
3
4w = load_file('model.safetensors')
5
6def signextend4to8(x3, x2, x1, x0):
7 inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
8 return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0)
9 for i in range(8)]
10
11# Sign extend -3 (1101 in 4-bit) to 8-bit
12result = signextend4to8(1, 1, 0, 1)
13print(result) # [1, 0, 1, 1, 1, 1, 1, 1] = 11111101 = -3