Hi,
When you use .detach(), you break the graph and so gradients cannot be computed anymore.
But the root issue is that for the autograd to work, the autograd engine needs to be able to know how to compute the gradient for each operation that is done.
Unfortunately, if you don’t use pytorch ops, it cannot know how to compute the gradients and so you won’t be able to get gradients.
You will either have to re-implement it using pytorch operations.
Or you will have to write a custom function where you tell the autograd how to compute the backward for that part where it does not know how to do it. See doc here 59 on how to do this.