Download model.py from phanerozoic/threshold-ffs4: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/phanerozoic/threshold-ffs4/resolve/main/model.py
- Command line
-
hf download hf://phanerozoic/threshold-ffs4/model.py
-
curl -L -o model.py https://huggingface.co/phanerozoic/threshold-ffs4/resolve/main/model.py
1.44 kB
| 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]}') | |