You stopped gradient at b, which should have back proped to a. If you don’t use detach, it works
a = torch.tensor([3.], requires_grad=True)
b = a.view_as(a).requires_grad_()
S = lambda x,y : x*y
f = S(a,b) # = exp(-a*b)
g = torch.autograd.grad( 2*f, [b], create_graph=True)[0]
h = torch.autograd.grad(   g, [a], create_graph=True)[0]