<h2 dir="auto">🐛 Bug</h2>
<p dir="auto">I am observing confusing (and probably errornous behavior) when using *= tensor operator with expand function. I don't think this should be an expected behavior, but if it is, there should at least be a user warning. Below is a minimally reproducible example:</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">code input:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import torch
a = torch.tensor([1,2,3]).view(1,3)
b = torch.tensor([4,5]).view(2,1)
print(a)
print(b)
print(a*b) # directly multiplying will trigger the correct broadcast"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">tensor</span>([<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>]).<span class="pl-en">view</span>(<span class="pl-c1">1</span>,<span class="pl-c1">3</span>)
<span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">tensor</span>([<span class="pl-c1">4</span>,<span class="pl-c1">5</span>]).<span class="pl-en">view</span>(<span class="pl-c1">2</span>,<span class="pl-c1">1</span>)
<span class="pl-en">print</span>(<span class="pl-s1">a</span>)
<span class="pl-en">print</span>(<span class="pl-s1">b</span>)
<span class="pl-en">print</span>(<span class="pl-s1">a</span><span class="pl-c1">*</span><span class="pl-s1">b</span>) <span class="pl-c"># directly multiplying will trigger the correct broadcast</span></pre></div>
<p dir="auto">output:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="tensor([[1, 2, 3]])
tensor([[4],
        [5]])
tensor([[ 4,  8, 12],
        [ 5, 10, 15]])"><pre class="notranslate">tensor([[1, 2, 3]])
tensor([[4],
        [5]])
tensor([[ 4,  8, 12],
        [ 5, 10, 15]])</pre></div>
<p dir="auto">code input:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="print(a.expand(2,3)*b.expand(2,3)) # explicitly using expand to broadcast followed by * is also fine"><pre class="notranslate"><span class="pl-en">print</span>(<span class="pl-s1">a</span>.<span class="pl-en">expand</span>(<span class="pl-c1">2</span>,<span class="pl-c1">3</span>)<span class="pl-c1">*</span><span class="pl-s1">b</span>.<span class="pl-en">expand</span>(<span class="pl-c1">2</span>,<span class="pl-c1">3</span>)) <span class="pl-c"># explicitly using expand to broadcast followed by * is also fine</span></pre></div>
<p dir="auto">output:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="tensor([[ 4,  8, 12],
        [ 5, 10, 15]])"><pre class="notranslate">tensor([[ 4,  8, 12],
        [ 5, 10, 15]])</pre></div>
<p dir="auto">code input:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="a *= b # trying to broadcast with *= doesn't work"><pre class="notranslate"><span class="pl-s1">a</span> <span class="pl-c1">*=</span> <span class="pl-s1">b</span> <span class="pl-c"># trying to broadcast with *= doesn't work</span></pre></div>
<p dir="auto">output:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
RuntimeError: output with shape [1, 3] doesn't match the broadcast shape [2, 3]"><pre class="notranslate">Traceback (most recent call last):
  File <span class="pl-s"><span class="pl-pds">"</span>&lt;stdin&gt;<span class="pl-pds">"</span></span>, line 1, <span class="pl-k">in</span> <span class="pl-k">&lt;</span>module<span class="pl-k">&gt;</span>
RuntimeError: output with shape [1, 3] doesn<span class="pl-s"><span class="pl-pds">'</span>t match the broadcast shape [2, 3]</span></pre></div>
<p dir="auto">code input:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="a = a.expand(2,3) # explicitly broadcasting with expand
b = b.expand(2,3) # explicitly broadcasting with expand
a *= b # this produces weird values that I don't understand
print(a)"><pre class="notranslate"><span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">a</span>.<span class="pl-en">expand</span>(<span class="pl-c1">2</span>,<span class="pl-c1">3</span>) <span class="pl-c"># explicitly broadcasting with expand</span>
<span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">b</span>.<span class="pl-en">expand</span>(<span class="pl-c1">2</span>,<span class="pl-c1">3</span>) <span class="pl-c"># explicitly broadcasting with expand</span>
<span class="pl-s1">a</span> <span class="pl-c1">*=</span> <span class="pl-s1">b</span> <span class="pl-c"># this produces weird values that I don't understand</span>
<span class="pl-en">print</span>(<span class="pl-s1">a</span>)</pre></div>
<p dir="auto">output:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="tensor([[20, 40, 60],
        [20, 40, 60]])"><pre class="notranslate">tensor([[20, 40, 60],
        [20, 40, 60]])</pre></div>
<h2 dir="auto">Environment</h2>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Collecting environment information...
PyTorch version: 1.0.0
Is debug build: No
CUDA used to build PyTorch: None

OS: Mac OSX 10.14.2
GCC version: Could not collect
CMake version: version 3.10.1

Python version: 3.5
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip3] numpy==1.11.3
[pip3] torch==0.4.0
[pip3] torchvision==0.2.1
[conda] blas                      1.0                         mkl
[conda] mkl                       2019.0                      118
[conda] mkl-service               1.1.2                    py35_3
[conda] mkl_fft                   1.0.6            py35hb8a8100_0
[conda] mkl_random                1.0.1            py35h5d10147_1
[conda] pytorch                   1.0.0                   py3.5_1    pytorch
[conda] torchvision               0.2.1                      py_2    pytorch"><pre class="notranslate">Collecting environment information...
PyTorch version: 1.0.0
Is debug build: No
CUDA used to build PyTorch: None

OS: Mac OSX 10.14.2
GCC version: Could not collect
CMake version: version 3.10.1

Python version: 3.5
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip3] numpy==1.11.3
[pip3] torch==0.4.0
[pip3] torchvision==0.2.1
[conda] blas                      1.0                         mkl
[conda] mkl                       2019.0                      118
[conda] mkl-service               1.1.2                    py35_3
[conda] mkl_fft                   1.0.6            py35hb8a8100_0
[conda] mkl_random                1.0.1            py35h5d10147_1
[conda] pytorch                   1.0.0                   py3.5_1    pytorch
[conda] torchvision               0.2.1                      py_2    pytorch</pre></div>
<p dir="auto">I don't know if this is a bug (and it might have been reported already but I didn't find anything).<br>
But it could at least be added to the documentation for the <code class="notranslate">expand</code> operation on a tensor:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import torch
T = torch.Tensor([2]).expand(1,3)
print(T.mul_(2))  # expected a Tensor with [4 4 4] here, got [16, 16, 16]"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-v">T</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>([<span class="pl-c1">2</span>]).<span class="pl-en">expand</span>(<span class="pl-c1">1</span>,<span class="pl-c1">3</span>)
<span class="pl-en">print</span>(<span class="pl-v">T</span>.<span class="pl-en">mul_</span>(<span class="pl-c1">2</span>))  <span class="pl-c"># expected a Tensor with [4 4 4] here, got [16, 16, 16]</span></pre></div>
<p dir="auto">Even if you are aware that expand doesn't copy data, I believe most will find this result quite counter-intuitive. Of course, this can be fixed atm either by replacing <code class="notranslate">expand</code> by <code class="notranslate">repeat</code> or <code class="notranslate">mul_</code> by <code class="notranslate">mul</code>, but that requires more memory. It would be awesome if the arithmetic ops like <code class="notranslate">mul</code> had a special case when the tensor's <code class="notranslate">stride</code> is 0 in some dimension</p>
<p dir="auto">Edit: My torch version string is <code class="notranslate">0.1.10_2</code></p>