Hi @albanD, thanks a lot! So yes I was forgetting a big detail, since my function depends simultaneously on x[col] and x[row] the first derivative at any x[i] will contain values canceling out for the neighboring pairs (i,j) and only values for (j,k) not containing (i), so the second derivative will always be zero…
Since what I want to obtain is a function that considers the x[i] coordinate as the local variable the simplest solution I found was to copy x into a detached variable:
Simpler 1D test with 3 nodes, 2 edges:
x = torch.tensor( [[0.0],[1.0],[2.0]], requires_grad=True)
y = x.clone().detach()

edges = torch.tensor([[0, 1], [1, 2]])

dist_sq = torch.sum( (y[col] - x[row]).pow(2) , dim = 1)
weight = dist_sq.pow(-1)

# Compute first gradient 
ones = torch.ones_like(weight)
qx = torch.autograd.grad(weight, x, grad_outputs=ones, create_graph=True, retain_graph=True)[0]
print(qx)

# Compute second gradient 
ones = torch.ones_like(qx)
qxx = torch.autograd.grad(qx, x, grad_outputs=ones, create_graph=True, retain_graph=True)[0]
print(qxx)

Now I manage to obtain my second derivative!
Thanks again!
P.S.: I still get though the UsreWarning (in debug mode)
“UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won’t be populated during autograd.backward(). If you indeed want the gradient for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor …”
It seems to be associated to the creation of the dist_sq tensor. I tried doing dist_sq.retain_grad() but same thing … Since I manage to compute the gradients I’m not sure if this could cause an error or miscalculation during back propagation for the training !?