Hi,
Is that expected above that p and q are constants?
As a first point, even if you make this work, your code is going to be very slow as nested python for loops are not efficient. You should try and write your function as operations directly on the full matrices.
In your case here, the .grad attribute won’t be set. As the original tensor was a leaf but after doing inplace ops on it, it is not a leaf anymore. So the gradients will propagate as expected to self.old_f but the .grad attribute of prop_f won’t be set.