oat:

Why I’m unable to get x.grad (dz/dx) in this case?


You are detaching the computation graph by recreating a tensor in:
 y= torch.tensor(x * x)

use y = x*x instead.
PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier