Metadata for CUDA tensors - requires sync?

yiftach: <p>Hi,<br>
I know that CUDA calls are non-blocking and run asynchronously, such that if I call some operation on the data then unless I perform some operation that requires a sync (.item(), .to() etc) my code can continue to run other lines of code and not wait for the operation to finish.</p>
<p>I know (or at least I think I know) that the metadata for the tensor is saved on the CPU, so for example a call to .shape does not require a sync.</p>
<p>If that is the case, what happens when the shape of the output of some operation is not yet known? Say:</p>
<pre><code class="lang-auto"># f is something that takes a lot of time
y = f(x)
print(y.shape)
</code></pre>
<p>Or</p>
<pre><code class="lang-auto">y = f(x) 
if y[0] == 0:
    print('a')
else:
    print('b')
</code></pre>
<p>In the second case, does the condition cause a sync?</p>
<p>Testing similar cases myself is something that I usually love to do, but here I’m not sure how to construct a function <code>f(x)</code> that contains a CUDA kernel that takes an arbitrarily long time to run. Bonus points if you can show me a way to do that.</p>
<p>Thanks,<br>
Yiftach</p>

ptrblck: <aside class="quote no-group" data-username="yiftach" data-post="1" data-topic="188462">
<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/yiftach/48/58678_2.png" class="avatar"> yiftach:</div>
<blockquote>
<p>In the second case, does the condition cause a sync?</p>
</blockquote>
</aside>
<p>Yes, since you added a data-dependent control flow and the host needs to wait before deciding which code path to take.</p>
<aside class="quote no-group" data-username="yiftach" data-post="1" data-topic="188462">
<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/yiftach/48/58678_2.png" class="avatar"> yiftach:</div>
<blockquote>
<p>Bonus points if you can show me a way to do that.</p>
</blockquote>
</aside>
<p>Spin a large enough <code>matmul</code> in a loop.<br>
<a href="https://github.com/pytorch/pytorch/issues/109111#issuecomment-1716873219">Here</a> is a recent example I’ve posted in another issue showing how the CPU can run ahead or would be blocked.</p>
<p>EDIT: you can also use the (experimental) <code>torch.cuda.set_sync_debug_mode("warn")</code> mode and set it to <code>warn</code> or <code>error</code> to receive:</p>
<pre data-code-wrap="python"><code class="lang-python"># UserWarning: called a synchronizing CUDA operation (Triggered internally at ../c10/cuda/CUDAFunctions.cpp:147.)
#   if y[0, 0] == 0:
</code></pre>

yiftach: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a>, comprehensive and helpful as usual.<br>
When I try to loop multiple times and then ask for the shape as you suggested, I see that no sync happens. That must mean that the metadata (such as the shape) is saved on the CPU and is available even before the data itself is calculated on the GPU.</p>
<p>I still have questions:</p>
<ol>
<li>So if I understand correctly, when a developer adds a new operation, they must have a portion, separate from the code that runs on the GPU, where they have to specify the expected shape? And I’ve never seen such code because it is part of the C/C++ codebase, abstracted away from the python codebase?</li>
<li>Using your suggested method, I saw that <code>tensor.nonzero()</code>) causes a CUDA sync. Is that a general rule of thumb then, that when the shape cannot be known beforehand, the call must be blocking?</li>
</ol>

ptrblck: <p>Yes, your explanation is valid and usually the shape of the output tensor can be calculated from the input shape as well as the setup of the operation (e.g. padding, stride etc. for a convolution). However, the output shape of <code>nonzero</code> depends on the actual values and is thus triggering a sync.</p>