<p dir="auto">Sometimes an in-place division where the denominator comes from the same tensor will yield unpredictable results.  For example, if you want an array that counts from 0 to 100 in each of 50 rows, but actually you want to normalize to the range [0..1], it seems natural to write:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch
c = torch.arange(101)[None,:].repeat(50, 1)
c /= c[:,100:]
print(c.max())"><pre class="notranslate"><code class="notranslate">import torch
c = torch.arange(101)[None,:].repeat(50, 1)
c /= c[:,100:]
print(c.max())
</code></pre></div>
<p dir="auto">However, running this on CPU repeatedly will produce an unpredictable max, e.g., 82, 95, 87, etc, rather than the expected 1.0.</p>
<p dir="auto">This operation appears to be reliable on numpy, and for many array shapes it also works fine in pytorch.  But it fails on certain sizes, leading to oddly rare and difficult-to-find bugs.</p>
<p dir="auto">Should <code class="notranslate">c /= c[:,-1:]</code> be reliable?  If not, would it be reasonable to warn on such operations?</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="t = torch.range(1,10)
t[1:] = t[:-1] # wrong result
print(t)
t = torch.range(1,10)
t[1:] = t[:-1].clone() # right result
print(t)
t = torch.range(1, 10).numpy()
t[1:] = t[:-1] # right result
print(t)"><pre class="notranslate"><code class="notranslate">t = torch.range(1,10)
t[1:] = t[:-1] # wrong result
print(t)
t = torch.range(1,10)
t[1:] = t[:-1].clone() # right result
print(t)
t = torch.range(1, 10).numpy()
t[1:] = t[:-1] # right result
print(t)
</code></pre></div>
<p dir="auto">Note that the same (wrong?) behaviour is present in lua torch.</p>