That sounds good.
Could you try to use torchviz 2 to print the graph and see where it overlaps (give it as input the loss from the first and second iteration and see where the two graphs start to overlap)? As it is a bit hard to say from just looking at the code indeed.