Thank you for the help! I guess I don’t need retain_graph to be true as well?
The function seems to work now!
def fwd_gradients(ys, xs, d_xs):
    v = torch.zeros_like(ys)
    v.requires_grad = True
    g = torch.autograd.grad(
            outputs=[ys],
            inputs=xs,
            grad_outputs=[v],
            create_graph=True,        
        )[0]

    out = torch.autograd.grad(
            outputs=[g],
            inputs=v,
            grad_outputs=d_xs,
        )[0]

Also, as PyTorch doesn’t use placeholders d_xs can probably be changed so that it’s just a ones tensor with the correct size.