8-bit count trailing zeros (CTZ). Returns the number of trailing zero bits before the first set bit. Returns 8 if input is zero.
x7 x6 x5 x4 x3 x2 x1 x0
│ │ │ │ │ │ │ │
└───┴───┴───┴───┴───┴───┴───┘
│
▼
┌─────────────────────┐
│ Priority Detect │ Layer 1-3
│ (find first 1) │
└─────────────────────┘
│
▼
┌─────────────────────┐
│ Position Encoder │ Layer 4-5
│ (binary encoding) │
└─────────────────────┘
│
▼
[c2, c1, c0]
(count 0-7)
Plus: all_zero detector for count=8
ctz8(x7, x6, x5, x4, x3, x2, x1, x0) -> (c3, c2, c1, c0)
where c = 4*c3 + 2*c2 + c1 + c0 = number of trailing zeros
If x0 is set, count = 0. If only x7 is set, count = 7. If no bits set, count = 8.
1from safetensors.torch import load_file
2import torch
3
4w = load_file('model.safetensors')
5
6def ctz8(bits):
7 # bits = [x0, x1, x2, x3, x4, x5, x6, x7] (LSB first)
8 inp = torch.tensor([float(b) for b in bits])
9
10 c0 = int((inp @ w['c0.weight'].T + w['c0.bias'] >= 0).item())
11 c1 = int((inp @ w['c1.weight'].T + w['c1.bias'] >= 0).item())
12 c2 = int((inp @ w['c2.weight'].T + w['c2.bias'] >= 0).item())
13 c3 = int((inp @ w['c3.weight'].T + w['c3.bias'] >= 0).item())
14
15 return c3, c2, c1, c0
16
17# Examples
18print(ctz8([1,0,0,0,0,0,0,0])) # (0,0,0,0) = 0 trailing zeros
19print(ctz8([0,0,0,0,1,0,0,0])) # (0,1,0,0) = 4 trailing zeros
20print(ctz8([0,0,0,0,0,0,0,0])) # (1,0,0,0) = 8 trailing zeros (all zero)
threshold-ctz8/
├── model.safetensors
├── model.py
├── create_safetensors.py
├── config.json
└── README.md