I keep getting the error one of the variables needed for gradient computation has been modified by an in-place operation but I don’t think I have made any in place operations. Below is the code that is causing the error, it specifically points to the line with Cq, and I can reproduce the error by just setting Cq = xz (lowercase x and z), but it runs fine with Cq = xy. Points and rays are tensors with xyz values as the second dimension which have been initialized with zeros. ABCD etc…have been initialized externally and don’t use the points or rays for computation, so shouldn’t cause any issues. When I subtract zero in the location shown in the picture, the problem seems to go away but I am having similar problems in other places in the code and want to know what is actually going on.
Image of code below or attached, I’m not really sure how it works. Also any tips for speed improvement are welcome.

Capture1135×444 38.2 KB