Here is another try, especially if you want to only use torch APIs.
import torch

def f(x):
    # non zero values mask
    non_zero_mask = x != 0

    # operations on the mask to find first nonzero values in the rows
    mask_max_values, mask_max_indices = torch.max(non_zero_mask, dim=1)

    # if the max-mask is zero, there is no nonzero value in the row
    mask_max_indices[mask_max_values == 0] = -1
    return mask_max_indices

x = torch.randn(4, 5)
x[x<0] = 0
x, sort_indices = x.sort(dim=1)
print('x', x)
first_nonzero = f(x)

print(first_nonzero)