Easy, just check out the official docs 17:


retain_graph ( bool  2 , optional ) – If False , the graph used to compute the grad will be freed. Note that in nearly all cases setting this option to True is not needed and often can be worked around in a much more efficient way. Defaults to the value of create_graph .

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

So if you set retain_graph=False this looks promising.