Ho good catch !
Another quirk of the checkpoint module…
So, here indeed, it might be simpler to remove this special code and detach() outside of the checkpoint.
Note that in this code, the detach() is not needed as the no_grad() block already prevent any graph from being created.