Phantom In-Place Operation, Disappears When I Subtract Zero

calebgannon: <p>I keep getting the error <em>one of the variables needed for gradient computation has been modified by an in-place operation</em> 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 = x<em>z (lowercase x and z), but it runs fine with Cq = x</em>y. 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.<br>
Image of code below or attached, I’m not really sure how it works. Also any tips for speed improvement are welcome.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/d/7d60657384cdd7e77dd16931ccce7a645bca6da9.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/7d60657384cdd7e77dd16931ccce7a645bca6da9" title="Capture"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/d/7d60657384cdd7e77dd16931ccce7a645bca6da9_2_690x269.jpeg" alt="Capture" data-base62-sha1="hT8d8ATblbR3fy6lBsF8kgvKVu1" width="690" height="269" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/d/7d60657384cdd7e77dd16931ccce7a645bca6da9_2_690x269.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/7/d/7d60657384cdd7e77dd16931ccce7a645bca6da9_2_1035x403.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/7/d/7d60657384cdd7e77dd16931ccce7a645bca6da9.jpeg 2x" data-dominant-color="2E2E2E"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capture</span><span class="informations">1135×444 38.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

G.M: <ol>
<li>The in-place operation could possibly be on <code>self.points</code> as it was referenced and used then modified . I’m not exactly sure if this is the case.</li>
<li>Subtracting the zero solves the problem by creating a copy of the selected indexes; in this case subtracting the zero would be equivalence to <code>self.points[SOMETHING].clone()</code>
</li>
</ol>

calebgannon: <p>yes I believe it is on self.points, but I don’t understand why. I grab points from one part of the tensors (at slice self.surface which is an integer=0 in this example) modify them with lots of intermediate outputs and then put them in self.points[:, :, :, self.surface+1, :]. I get that I can stop the problem with a clone, but I’m trying to understand why there is a problem in the first place.</p>

G.M: <p>Let’s say the indexes u grabed from <code>self.points</code> is <code>II</code>. First, u did <code>x=self.points[II]</code>, so every operation on x will track a history back to <code>self.points[II]</code>. But in the  end u modified <code>self.points[II]</code>, which is part of the earlier graph. My understandings may not be totally correct,</p>