You need custom operators for that.
A hint of how they work can be had from the tests:

  
      
      GitHub
  
  
    

pytorch/pytorch 12

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


  
  
    
    
  
  


Best regards
Thomas