Solved by tom in post #3 
              
              
                The obvious thing would be to use torch.multinomial(torch.ones(n, d), 4), which would take out ~1/3 of the time for me but is somewhat slow. 
I can half the time (relative to your code, on my machine, on CPU etc.) by using rand + topk. 
    sample = torch.rand(n, d).topk(4, dim=1).indices
    mask =…