File size: 1,440 Bytes
c0340f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import torch
from safetensors.torch import load_file

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

def ffs4(a3, a2, a1, a0, w):
    """Find first set bit. Returns 3-bit binary position (1-indexed), 0 if no bits set."""
    inp = torch.tensor([float(a3), float(a2), float(a1), float(a0)])

    # Layer 1
    has0 = int((inp @ w['has0.weight'].T + w['has0.bias'] >= 0).item())
    has1_first = int((inp @ w['has1_first.weight'].T + w['has1_first.bias'] >= 0).item())
    has2_first = int((inp @ w['has2_first.weight'].T + w['has2_first.bias'] >= 0).item())
    has3_first = int((inp @ w['has3_first.weight'].T + w['has3_first.bias'] >= 0).item())

    # Layer 2
    l1 = torch.tensor([float(has0), float(has1_first), float(has2_first), float(has3_first)])
    y0 = int((l1 @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
    y1 = int((l1 @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
    y2 = int((l1 @ w['y2.weight'].T + w['y2.bias'] >= 0).item())

    return [y2, y1, y0]

if __name__ == '__main__':
    w = load_model()
    print('ffs4 truth table:')
    print('input | ffs | y2 y1 y0')
    print('------+-----+---------')
    for i in range(16):
        a3, a2, a1, a0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
        result = ffs4(a3, a2, a1, a0, w)
        ffs_val = result[0] * 4 + result[1] * 2 + result[2]
        print(f'{a3}{a2}{a1}{a0}  |  {ffs_val}  |  {result[0]}  {result[1]}  {result[2]}')