This is correct. The x.grad tensor is the dz/dx term. By setting the create_graph parameter to True, the graph of the derivatives is created and a backward function is attached to the x.grad tensor. Hence, deriving gradient of x.grad w.r.t y is the correct way to calculate the value of d(dz/dx)/dy