Solved by robert-lieck in post #2 
              
              
                OK, adding create_graph=True to the first backward call fixes the problem. From the docs: 

create_graph (bool, optional) – If True, graph of the derivative will be constructed, allowing to compute higher order derivative products. Defaults to False. 

I kind of expected a warning or error, similar …