Repository: pytorch/pytorch

Issue:
RuntimeError: dimension specified as 0 but tensor has no dimensions
My code run well under torch 0.2, however, the following error occurs when I use torch 0.3.1, why?

Traceback (most recent call last):
  File "../train.py", line 149, in <module>
    loss.backward()
  File "/home/jxliu/.local/lib/python3.6/site-packages/torch/autograd/variable.py", line 167, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, retain_variables)
  File "/home/jxliu/.local/lib/python3.6/site-packages/torch/autograd/__init__.py", line 99, in backward
    variables, grad_variables, retain_graph)
RuntimeError: dimension specified as 0 but tensor has no dimensions


Discussion:
Is this easily reproducible?  It's likely because we changed the shape of reduction operations (and indexing a 1-dimensional tensor) to be 0-dimensional (with a value) instead of 1-dimensional.
I met this problem when using `torch.cat` on a list of 0-dim Tensor. It had worked on previous versions, what should I do to fix this?
@zou3519 you looked at this right?
I looked at it but haven't thought about it much yet. There's code right now on master that prevents a 0-dim tensor from going into `torch.cat` (did we have 0-dim tensors in pytorch 0.3?), but it can still take empty tensors (`tensor.dim() == 1 and tensor.numel() == 0`) that have been causing issues: #5332 #5739
@paduvi are you on master?  If you could post a reproduction that would be helpful.
@gchanan yes, I'm current on master.

This is minimum code:

```
x = [Variable(torch.rand(1), requires_grad=True)] * 10  # input
y = [Variable(torch.rand(1), requires_grad=True)] * 10  # input

z = map(lambda a, b: torch.matmul(a, b), x, y)

# on master it print ()
# at previous version it print (1,)
print(z[0].size())  

torch.cat(z, dim=0)
```
@paduvi what map are you using? This is what I get when I run your code
```

In [2]: import torch

In [3]: from torch.autograd import Variable

In [4]: x = [Variable(torch.rand(1), requires_grad=True)] * 10  # input
   ...: y = [Variable(torch.rand(1), requires_grad=True)] * 10  # input
   ...:
   ...: z = map(lambda a, b: torch.matmul(a, b), x, y)
   ...:
   ...: # on master it print ()
   ...: # at previous version it print (1,)
   ...: print(z[0].size())
   ...:
   ...: torch.cat(z, dim=0)
   ...:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-86f566c9a3d7> in <module>()
      6 # on master it print ()
      7 # at previous version it print (1,)
----> 8 print(z[0].size())
      9
     10 torch.cat(z, dim=0)

TypeError: 'map' object is not subscriptable
```
@zou3519 `map` returns a list in Python 2, so you probably need to change it to `list(map(...))`

Resolving patch:
diff --git a/tools/autograd/load_derivatives.py b/tools/autograd/load_derivatives.py
--- a/tools/autograd/load_derivatives.py
+++ b/tools/autograd/load_derivatives.py
@@ -294,10 +294,10 @@ def saved_variables(formula, args):
             'suffix': '_numel',
             'type': 'int64_t',
         }),
-        # replace to_arg_sizes(self, 2) with self_argsizes_2
-        (r'to_arg_sizes\({}, (\w+)\)', {
-            'suffix': lambda m: '_sizes_{}'.format(*m.groups()),
-            'type': 'IntList',
+        # replace to_args_sizes(self) with self_args_sizes
+        (r'to_args_sizes\({}\)', {
+            'suffix': '_args_sizes',
+            'type': 'std::vector<std::vector<int64_t>>',
         }),
         # replace TensorGeometry(self) with self_geometry
         (r'TensorGeometry\({}\)', {


Test patch:
diff --git a/test/test_torch.py b/test/test_torch.py
--- a/test/test_torch.py
+++ b/test/test_torch.py
@@ -2305,7 +2305,9 @@ def test_cat_bad_input_sizes(self):
     def test_cat_scalars(self):
         x = torch.tensor(0)
         y = torch.tensor(1)
-        self.assertRaises(RuntimeError, lambda: torch.cat([x, y]))
+        with self.assertRaisesRegexp(RuntimeError,
+                                     'zero-dimensional.*cannot be concatenated'):
+            torch.cat([x, y])
 
     def test_stack(self):
         x = torch.rand(2, 3, 4)