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

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

def signbit8(a7, a6, a5, a4, a3, a2, a1, a0, weights):
    """Returns sign bit (MSB) of 8-bit number. 1 = negative in 2's complement."""
    inp = torch.tensor([float(a7), float(a6), float(a5), float(a4),
                        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('signbit8 selected tests:')
    for i in [0, 1, 127, 128, 255]:
        bits = [(i >> (7-j)) & 1 for j in range(8)]
        result = signbit8(*bits, w)
        signed_val = i if i < 128 else i - 256
        print(f'  {i:3d} ({i:08b}) signed={signed_val:+4d} -> sign={result}')