Compute gradient of determinant function or inverse function

I’m not sure if I misunderstood the post How to calculate the determinant of a variable?

But it seems to me it throw error when the matrix is not positive definite (which is Okey in my case as I requires the metric tensor to be positive definite). However, when I use

a = Variable(torch.Tensor([[2,0],[0,4]]))
torch.potrf(a).diag().prod()

The result is tensor(2.8284)

But I should expect it to be 8 as its determinant is 8.