Repository: pytorch/pytorch

Issue:
Bug in legacy padding layer
I've found a bug in [Padding.py](https://github.com/pytorch/pytorch/blob/master/torch/legacy/nn/Padding.py).
It doesn't handle nInputDim original Torch Padding layer's parameter . As far as I've unterstood, you've rewritten lua code from [here](https://github.com/torch/nn/blob/master/Padding.lua), but not fully. 19th line in this file contains code for dealing with nInputDim parameter.
To reproduce issue, you need to download ENet model from [here](https://www.dropbox.com/sh/dywzk3gyb12hpe5/AAD5YkUa8XgMpHs2gCRgmCVCa)(original model from ENet authors) and make inference:
`import numpy as np`
`import torch`
`from torch.utils.serialization import load_lua`
`image = np.ones((1,3,1024,2048))`
`tensor = torch.FloatTensor(image)`
`net_torch = load_lua(torch_model)`
`out_torch = net_torch.forward(tensor)`

Now I've got en exception: 
Traceback (most recent call last):
```
  File "/hdd/PycharmProjects/untitled/test.py", line 110, in <module>
    out_torch = net_torch.forward(tensor).numpy()
  File "/usr/local/lib/python2.7/dist-packages/torch/legacy/nn/Module.py", line 33, in forward
    return self.updateOutput(input)
  File "/usr/local/lib/python2.7/dist-packages/torch/legacy/nn/Sequential.py", line 36, in updateOutput
    currentOutput = module.updateOutput(currentOutput)
  File "/usr/local/lib/python2.7/dist-packages/torch/legacy/nn/Sequential.py", line 36, in updateOutput
    currentOutput = module.updateOutput(currentOutput)
  File "/usr/local/lib/python2.7/dist-packages/torch/legacy/nn/CAddTable.py", line 20, in updateOutput
    self.output.add_(input[i])
RuntimeError: inconsistent tensor size at /b/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:827
```

Padding is added to wrong axis, what's why CAddTable can't sum blobs with different shapes and throws exception.
If I edit the code of Padding.py by adding one to self.dim variables (lines 21 and 23 in Padding.py), all works correctly.


Discussion:
are you a single example (3D) or a batch (4D) as an input?
I've used 4D tensor

Resolving patch:
diff --git a/torch/legacy/nn/Padding.py b/torch/legacy/nn/Padding.py
--- a/torch/legacy/nn/Padding.py
+++ b/torch/legacy/nn/Padding.py
@@ -7,12 +7,15 @@ class Padding(Module):
     # index [index] in that dimension. If pad<0, index counts from the left.
     # If pad>0 index counts from the right index = 1 pads before index 1.
     # index = 2 pads starting before index 2 and after index 1 in dimension [dim]
+    # When nInputDim is provided, inputs larger than that value will be considered batches
+    # where the actual dim to be padded will be dimension dim + 1.
 
-    def __init__(self, dim, pad, value=0, index=0):
+    def __init__(self, dim, pad, value=0, index=0, nInputDim=0):
         self.value = value
         self.index = index
         self.dim = dim
         self.pad = pad
+        self.nInputDim = nInputDim
         self.outputSize = torch.Size()
         super(Padding, self).__init__()
 
@@ -22,6 +25,9 @@ def updateOutput(self, input):
         self.outputSize = torch.Size(outputSize)
         dim = self.dim
 
+        if hasattr(self, "nInputDim") and self.nInputDim > 0 and input.dim() != self.nInputDim:
+            dim = dim + 1
+
         self.output.resize_(self.outputSize)
         self.output.fill_(self.value)
         index = self.index
@@ -46,6 +52,9 @@ def updateGradInput(self, input, gradOutput):
         self.gradInput.resize_as_(input)
         dim = self.dim
 
+        if hasattr(self, "nInputDim") and self.nInputDim > 0 and input.dim() != self.nInputDim:
+            dim = dim + 1
+
         index = self.index
         pad = self.pad
         if pad > 0:


Test patch: