Hi,
Keep in mind that detach() or detach_() won’t modify an existing graph. It will stop tracking operations for the current Tensor for every new operations you’re going to perform on them.
I am not sure to understand 100% your code but it looks like you’re trying to modify an existing graph no?