akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
Question about torch.max

chichi: <p>I have a question for the following code:</p>
<p>tensor_1 = torch.randint(0, 10, [5, 2], dtype=torch.float32)<br>
tensor_2 = torch.randint(0, 10, [4, 2], dtype=torch.float32)<br>
max_1 = torch.max(tensor_1[:, None, :1], tensor_2[:, :1])</p>
<p>print(max_1.size())</p>
<p>The output is: torch.Size([5, 4, 1])</p>
<p>How this is computed?</p>

KFrank: <p>Hi Chichi!</p>
<p>The short answer is pytorch’s <a href="https://pytorch.org/docs/1.7.0/notes/broadcasting.html" rel="noopener nofollow ugc">broadcasting</a>.</p>
<aside class="quote no-group quote-modified" data-username="chichi" data-post="1" data-topic="110990" 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/chichi/48/34291_2.png" class="avatar"> chichi:</div>
<blockquote>
<p>max_1 = torch.max(tensor_1[:, None, :1], tensor_2[:, :1])</p>
<p>The output is: torch.Size([5, 4, 1])</p>
</blockquote>
</aside>
<p>The arguments to <code>max()</code> have the following shapes:</p>
<pre data-code-wrap="python"><code class="lang-python">tensor_1[:, None, :1].shape = torch.Size([5, 1, 1])
tensor_2[:, :1].shape =  torch.Size([4, 1])
</code></pre>
<p>When broadcast, the two trailing <code>1</code>s line up, the middle <code>1</code> of the first<br>
argument gets broadcast to match the <code>4</code> of the second argument, and<br>
the “missing” <code>5</code> in the second argument gets broadcast to match the<br>
leading <code>5</code> of the first argument.  Thus your final shape is <code>[5, 4, 1]</code>.</p>
<p>Best.</p>
<p>K. Frank</p>