Hi, thank you for a great code snippet.
I found an error case where your code returns a Runtime Error.
When there is only one element in valid_idx and that element is at the 0th position in a it returns the following error:
RuntimeError: invalid argument 2: invalid multinomial distribution (sum of probabilities <= 0) at /Users/distiller/project/conda/conda-bld/pytorch_1556653464916/work/aten/src/TH/generic/THTensorRandom.cpp:343

My reproducible code is as follows:
a  = torch.Tensor([-1,4,6,3])
indices = (a == -1).nonzero().view(-1)
choice = torch.multinomial(indices.float(), 1)

Your second suggestion works fine with this case.