Solved by apsvieira in post #5 
              
              
                Sorry for the half answer. That seems to be very memory-inefficient. Trying to run the same code on cpu gave me a “Tried to allocate 16GB” error message. 
I believe the same output can be obtained with masked_fill. Tried it here and I believe it to be correct. 
Generating A_array with torch.rand res…