I will mention this as probably an obvious answer. If you are importing torch already, you can just make your own copy of this function using the original function as a template and calling torch.topk and torch.cumsum the way the original function did.