Scatter/indexing slows with tensor size?

username: Sam_Lerman
cooked: <pre><code class="lang-auto">for size in [10, 10000, 10000000]:
    src = torch.arange(10)[None, :]
    index = torch.ones_like(src)
    t = time.time()
    torch.zeros(size, 10, dtype=src.dtype).scatter_(0, index, src)
    print('time', time.time() - t)
</code></pre>

username: KFrank
cooked: <p>Hi Sam!</p>
<aside class="quote no-group" data-username="Sam_Lerman" data-post="1" data-topic="143372" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sam_lerman/48/37165_2.png" class="avatar"> Sam_Lerman:</div>
<blockquote>
<pre><code class="lang-auto">    t = time.time()
    torch.zeros(size, 10, dtype=src.dtype).scatter_(0, index, src)
    print('time', time.time() - t)
</code></pre>
</blockquote>
</aside>
<p>The timed section of your code includes the <em>creation</em> of increasingly-large<br>
tensors.</p>
<p>Best.</p>
<p>K. Frank</p>

username: Sam_Lerman
cooked: <p>Oops, maybe this?</p>
<pre><code class="lang-auto">for size in [100, 100000, 100000000]:
    src = torch.arange(100)[None, :]
    index = torch.ones_like(src)
    x = torch.zeros(size, dtype=src.dtype)
    t = time.time()
    x.view(-1, 100).scatter_(1, index, src)
    print('time', time.time() - t)
</code></pre>

username: KFrank
cooked: <p>Hi Sam!</p>
<aside class="quote no-group" data-username="Sam_Lerman" data-post="3" data-topic="143372" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sam_lerman/48/37165_2.png" class="avatar"> Sam_Lerman:</div>
<blockquote>
<p>Oops, maybe this?</p>
<pre><code class="lang-auto">for size in [100, 100000, 100000000]:
    src = torch.arange(100)[None, :]
    index = torch.ones_like(src)
    x = torch.zeros(size, dtype=src.dtype)
    t = time.time()
    x.view(-1, 100).scatter_(1, index, src)
    print('time', time.time() - t)
</code></pre>
</blockquote>
</aside>
<p>I’m not seeing the slow-down that you allude to:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.10.0'
&gt;&gt;&gt; import time
&gt;&gt;&gt; for size in [100, 100000, 100000000]:
...     src = torch.arange(100)[None, :]
...     index = torch.ones_like(src)
...     x = torch.zeros(size, dtype=src.dtype)
...     t = time.time()
...     x.view(-1, 100).scatter_(1, index, src)
...     print('time', time.time() - t)
...
tensor([[ 0, 99,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0]])
time 0.0041332244873046875
tensor([[ 0, 99,  0,  ...,  0,  0,  0],
        [ 0,  0,  0,  ...,  0,  0,  0],
        [ 0,  0,  0,  ...,  0,  0,  0],
        ...,
        [ 0,  0,  0,  ...,  0,  0,  0],
        [ 0,  0,  0,  ...,  0,  0,  0],
        [ 0,  0,  0,  ...,  0,  0,  0]])
time 0.0012705326080322266
tensor([[ 0, 99,  0,  ...,  0,  0,  0],
        [ 0,  0,  0,  ...,  0,  0,  0],
        [ 0,  0,  0,  ...,  0,  0,  0],
        ...,
        [ 0,  0,  0,  ...,  0,  0,  0],
        [ 0,  0,  0,  ...,  0,  0,  0],
        [ 0,  0,  0,  ...,  0,  0,  0]])
time 0.0011951923370361328
</code></pre>
<p>(I attribute the longer time for the first, smaller <code>size</code> to a warm-up effect.)</p>
<p>Best.</p>
<p>K. Frank</p>