Is torch.matmul with Mx0 and 0xN tensors a zero filled MxN tensor?

Sujoy_Saraswati: <p>Hi,<br>
If there is a torch.matmul of two tensors of shape Mx0 and 0xN, then the resulting tensor (at least on CPU) is an MxN tensor which is zero filled. I see the same behavior on numpy as well. My question - is it necessary for the result tensor to be zero filled or are the values undefined and random? It would be great if there is any document related to this.<br>
Regards,<br>
Sujoy</p>

AlphaBetaGamma96: <p>Hi</p>
<p>It seems that regardless of how the Tensor’s are initialized, it returns a zero-filled Tensor. For example,</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; M=10
&gt;&gt;&gt; N=11
&gt;&gt;&gt; 
&gt;&gt;&gt; mat1 = torch.Tensor(M,0) #empty fill
&gt;&gt;&gt; mat2 = torch.Tensor(0,N)
&gt;&gt;&gt; mat1@mat2
tensor([[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., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
&gt;&gt;&gt; 
&gt;&gt;&gt; mat1 = torch.randn(M,0) #norm. distributed
&gt;&gt;&gt; mat2 = torch.randn(0,N)
&gt;&gt;&gt; mat1@mat2
tensor([[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., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
&gt;&gt;&gt; torch.randn(M,0)
tensor([], size=(10, 0))
</code></pre>
<p>I’m not 100% sure why, it’d be best if you got a dev’s opinion on it, but I’d guess it’s linked to the fact the tensors <code>mat1</code> and <code>mat2</code> are actually initialized as empty lists, like</p>
<pre><code class="lang-auto">&gt;&gt;&gt; mat1 = torch.randn(M,0)
&gt;&gt;&gt; mat1
tensor([], size=(10, 0))
</code></pre>
<p>I’d assume because it’s an empty list, it defaults to 0 when you perform the matmul operation!</p>

Sujoy_Saraswati: <p>Thank you <a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a>, I too see the same behavior but not able to find any document/reference that confirms this to be the expected result.</p>

gphilip: <p>Here is what I suspect happens, though I couldn’t find the code to match it: <code>torch.matmul()</code> starts off by initializing a matrix of the correct output size filled with zeroes. It then iterates over the relevant dimensions of the two input matrices, computes the various dot products, and replaces each zero in the output matrix with the correct dot product. Once this iteration is done, it returns the output matrix which is now the product. This is how we would write simple code for matrix multiplication.</p>
<p>In this case, since there is nothing to iterate over, the zeroes remain as they are and we get a matrix of the correct size filled with zeroes.</p>

tom: <p>If you spell out the formula for matrix multiplication, you see that it has a sum which in your case has no summands. The convention / consistent definition is that empty sums are 0.</p>
<p>Best regards</p>
<p>Thomas</p>

Sujoy_Saraswati: <p>Thanks, so is it correct to conclude that if such an op with matmul (Mx0, 0xN) comes up during the execution of a model, then further ops can expect the semantics that the result to be zero filled MxN?</p>

tom: <p>The result is all zeros and you can rely on that. There will not be any automatic short-circuiting for “known zeros”. I am not sure if you meant the former or the latter.</p>
<p>Best regards</p>
<p>Thomas</p>

Sujoy_Saraswati: <p>Thanks, I had meant the former.</p>