I’m not completely sure, how all tensors are defined in your code, but I think you could use torch.unique to create a list with all masks and loop once over these masks:
masks = [u==idx for u in idx.unique()]

Would that work for you?