<h2 dir="auto">Issue description</h2>
<p dir="auto">torch yields a RuntimeError when assigning a vector to an empty set of indices in an array.</p>
<h2 dir="auto">Code example</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch
x = torch.zeros((2, 2))

mask = torch.tensor([True, True])
print(x[mask])
x[mask] = torch.tensor([1., 2.])

mask = torch.tensor([True, False])
print(x[mask])
x[mask] = torch.tensor([1., 2.])

mask = torch.tensor([False, True])
print(x[mask])
x[mask] = torch.tensor([1., 2.])

mask = torch.tensor([False, False])
print(x[mask])
x[mask] = torch.tensor([1., 2.])"><pre class="notranslate"><code class="notranslate">import torch
x = torch.zeros((2, 2))

mask = torch.tensor([True, True])
print(x[mask])
x[mask] = torch.tensor([1., 2.])

mask = torch.tensor([True, False])
print(x[mask])
x[mask] = torch.tensor([1., 2.])

mask = torch.tensor([False, True])
print(x[mask])
x[mask] = torch.tensor([1., 2.])

mask = torch.tensor([False, False])
print(x[mask])
x[mask] = torch.tensor([1., 2.])
</code></pre></div>
<p dir="auto">outputs</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="tensor([[0., 0.],
        [0., 0.]])
tensor([[1., 2.]])
tensor([[1., 2.]])
tensor([])
Traceback (most recent call last):
  File &quot;bug.py&quot;, line 18, in &lt;module&gt;
    x[mask] = torch.tensor([1., 2.])
RuntimeError: The expanded size of the tensor (0) must match the existing size (2) at non-singleton dimension 0"><pre class="notranslate"><code class="notranslate">tensor([[0., 0.],
        [0., 0.]])
tensor([[1., 2.]])
tensor([[1., 2.]])
tensor([])
Traceback (most recent call last):
  File "bug.py", line 18, in &lt;module&gt;
    x[mask] = torch.tensor([1., 2.])
RuntimeError: The expanded size of the tensor (0) must match the existing size (2) at non-singleton dimension 0
</code></pre></div>
<h2 dir="auto">System Info</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python collect_env.py
Collecting environment information...
PyTorch version: 0.4.1
Is debug build: No
CUDA used to build PyTorch: None

OS: Mac OSX 10.13.6
GCC version: Could not collect
CMake version: version 3.11.4

Python version: 3.6
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:
[pip] Could not collect
[conda] torch                     0.4.1                     &lt;pip&gt;
$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ pip --version
pip 18.0 from /anaconda3/lib/python3.6/site-packages/pip (python 3.6)"><pre class="notranslate"><code class="notranslate">$ python collect_env.py
Collecting environment information...
PyTorch version: 0.4.1
Is debug build: No
CUDA used to build PyTorch: None

OS: Mac OSX 10.13.6
GCC version: Could not collect
CMake version: version 3.11.4

Python version: 3.6
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:
[pip] Could not collect
[conda] torch                     0.4.1                     &lt;pip&gt;
$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ pip --version
pip 18.0 from /anaconda3/lib/python3.6/site-packages/pip (python 3.6)
</code></pre></div>
<h2 dir="auto">Current workaround</h2>
<p dir="auto">Current workaround involves using an if-statement conditioned on mask.any(), i.e.:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="if mask.any(): x[mask] = torch.tensor([1., 2.])"><pre class="notranslate"><code class="notranslate">if mask.any(): x[mask] = torch.tensor([1., 2.])
</code></pre></div>
<p dir="auto">In numpy, indexing with a list of booleans is equivalent to indexing with a boolean array, which means it performs masking.</p>
<p dir="auto">In PyTorch, the list of booleans is cast to a long tensor.</p>
<p dir="auto">I believe this discrepancy should be fixed.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="a = torch.rand(3, 3)
print(a)

 0.1041  0.6888  0.7988
 0.9398  0.9151  0.7642
 0.5340  0.4715  0.8128
[torch.FloatTensor of size (3,3)]

print(a[[True, False, False]]

 0.9398  0.9151  0.7642
 0.1041  0.6888  0.7988
 0.1041  0.6888  0.7988
[torch.FloatTensor of size (3,3)]

aa = a.numpy()
print(aa[[True, False, False]])
array([[0.10411686, 0.6887991 , 0.7988465 ]], dtype=float32)"><pre class="notranslate"><span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">rand</span>(<span class="pl-c1">3</span>, <span class="pl-c1">3</span>)
<span class="pl-en">print</span>(<span class="pl-s1">a</span>)

 <span class="pl-c1">0.1041</span>  <span class="pl-c1">0.6888</span>  <span class="pl-c1">0.7988</span>
 <span class="pl-c1">0.9398</span>  <span class="pl-c1">0.9151</span>  <span class="pl-c1">0.7642</span>
 <span class="pl-c1">0.5340</span>  <span class="pl-c1">0.4715</span>  <span class="pl-c1">0.8128</span>
[<span class="pl-s1">torch</span>.<span class="pl-v">FloatTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> (<span class="pl-c1">3</span>,<span class="pl-c1">3</span>)]

<span class="pl-s1">print</span>(<span class="pl-s1">a</span>[[<span class="pl-c1">True</span>, <span class="pl-c1">False</span>, <span class="pl-c1">False</span>]]

 <span class="pl-c1">0.9398</span>  <span class="pl-c1">0.9151</span>  <span class="pl-c1">0.7642</span>
 <span class="pl-c1">0.1041</span>  <span class="pl-c1">0.6888</span>  <span class="pl-c1">0.7988</span>
 <span class="pl-c1">0.1041</span>  <span class="pl-c1">0.6888</span>  <span class="pl-c1">0.7988</span>
[<span class="pl-s1">torch</span>.<span class="pl-v">FloatTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> (<span class="pl-c1">3</span>,<span class="pl-c1">3</span>)]

<span class="pl-s1">aa</span> <span class="pl-c1">=</span> <span class="pl-s1">a</span>.<span class="pl-en">numpy</span>()
<span class="pl-s1">print</span>(<span class="pl-s1">aa</span>[[<span class="pl-c1">True</span>, <span class="pl-c1">False</span>, <span class="pl-c1">False</span>]])
<span class="pl-s1">array</span>([[<span class="pl-c1">0.10411686</span>, <span class="pl-c1">0.6887991</span> , <span class="pl-c1">0.7988465</span> ]], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">float32</span>)</pre></div>