Solved by KFrank in post #2 
              
              
                Hi Henry! 

It looks like your issue is due to a troublesome bug in the innards of 
autograd – not specific to torch.where(), but in lower-level infrastructure. 
However, in your use case, you can work around it by clamping the 
denominator of your potential divide-by-zero away from zero.  Here 
is …