It can be done in the following way:
def argmax_first_and_last(a):
    b = torch.stack([torch.arange(a.shape[1])] * a.shape[0])
    max_values, _ = torch.max(a, dim=1)
    b[a != max_values[:, None]] = a.shape[1]
    first_max, _ = torch.min(b, dim=1)
    b[a != max_values[:, None]] = -1
    last_max, _ = torch.max(b, dim=1)
    return first_max, last_max
    
a = torch.tensor([[ 0, 0, 2, 2, 0, 2, 0, 0],
                  [-1, 0, 0, 1, 0, 0, 1, 0]])

first_max, last_max = argmax_first_and_last(a)
print(first_max) # [2, 3]
print(last_max) # [5, 6]