File size: 769 Bytes
ccee2d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import torch
from safetensors.torch import load_file

def load_model(path='model.safetensors'):
    return load_file(path)

def signbit4(a3, a2, a1, a0, weights):
    """Returns sign bit (MSB) of 4-bit number. 1 = negative in 2's complement."""
    inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])
    return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())

if __name__ == '__main__':
    w = load_model()
    print('signbit4 truth table:')
    for i in range(16):
        a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
        result = signbit4(a3, a2, a1, a0, w)
        signed_val = i if i < 8 else i - 16
        print(f'  {a3}{a2}{a1}{a0} (unsigned={i:2d}, signed={signed_val:+3d}) -> sign={result}')