Solved by albanD in post #2 
              
              
                Hi, 
You can check if torch.is_grad_enabled() in the backward and if gd.requires_grad. 
That will tell if you something wants the gradients to be computed for your function. Namely if grad mode is enabled and the input requires_grad then you should create the graph. Otherwise, it is not needed.