it should be thread-safe, and if it’s not, we will fix it.
I’ve tested your example against the master branch of pytorch, and it did not produce segfaults.
I wonder if we fixed the issue you are seeing already.
Can you give the master branch a try to confirm?

  
      
      GitHub
  
  
    

pytorch/pytorch 109

Tensors and Dynamic neural networks in Python with strong GPU acceleration - pytorch/pytorch