prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN 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.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `torch/legacy/nn/Padding.py`
  Content:
  ```
  1 import torch
  2 from .Module import Module
  3 
  4 
  5 class Padding(Module):
  6     # pad puts in [pad] amount of [value] over dimension [dim], starting at
  7     # index [index] in that dimension. If pad<0, index counts from the left.
  8     # If pad>0 index counts from the right index = 1 pads before index 1.
  9     # index = 2 pads starting before index 2 and after index 1 in dimension [dim]
  10 
  11     def __init__(self, dim, pad, value=0, index=0):
  12         self.value = value
  13         self.index = index
  14         self.dim = dim
  15         self.pad = pad
  16         self.outputSize = torch.Size()
  17         super(Padding, self).__init__()
  18 
  19     def updateOutput(self, input):
  20         outputSize = list(input.size())
  21         outputSize[self.dim] += abs(self.pad)
  22         self.outputSize = torch.Size(outputSize)
  23         dim = self.dim
  24 
  25         self.output.resize_(self.outputSize)
  26         self.output.fill_(self.value)
  27         index = self.index
  28         pad = self.pad
  29         if pad > 0:
  30             index = input.size(dim) - index
  31         else:
  32             pad = -pad
  33 
  34         if index == 0:
  35             self.output.narrow(dim, pad, input.size(dim)).copy_(input)
  36         elif index == input.size(dim):
  37             self.output.narrow(dim, 0, input.size(dim)).copy_(input)
  38         else:
  39             self.output.narrow(dim, 0, index).copy_(input.narrow(dim, 0, index))
  40             self.output.narrow(dim, index + pad, input.size(dim) -
  41                                index).copy_(input.narrow(dim, index, input.size(dim) - index))
  42 
  43         return self.output
  44 
  45     def updateGradInput(self, input, gradOutput):
  46         self.gradInput.resize_as_(input)
  47         dim = self.dim
  48 
  49         index = self.index
  50         pad = self.pad
  51         if pad > 0:
  52             index = input.size(dim) - index
  53         else:
  54             pad = -pad
  55 
  56         if index == 0:
  57             self.gradInput.copy_(gradOutput.narrow(dim, pad, input.size(dim)))
  58         elif index == input.size(dim):
  59             self.gradInput.copy_(gradOutput.narrow(dim, 0, input.size(dim)))
  60         else:
  61             self.gradInput.narrow(dim, 0, index).copy_(gradOutput.narrow(dim, 0, index))
  62             self.gradInput.narrow(dim, index, input.size(
  63                 dim) - index).copy_(gradOutput.narrow(dim, index + pad, input.size(dim) - index))
  64 
  65         return self.gradInput
  66
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: 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 @@
       # 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 @@
           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 @@
           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:
  
verification_info: {"golden_diff": "diff --git a/torch/legacy/nn/Padding.py b/torch/legacy/nn/Padding.py\n--- a/torch/legacy/nn/Padding.py\n+++ b/torch/legacy/nn/Padding.py\n@@ -7,12 +7,15 @@\n     # index [index] in that dimension. If pad<0, index counts from the left.\n     # If pad>0 index counts from the right index = 1 pads before index 1.\n     # index = 2 pads starting before index 2 and after index 1 in dimension [dim]\n+    # When nInputDim is provided, inputs larger than that value will be considered batches\n+    # where the actual dim to be padded will be dimension dim + 1.\n \n-    def __init__(self, dim, pad, value=0, index=0):\n+    def __init__(self, dim, pad, value=0, index=0, nInputDim=0):\n         self.value = value\n         self.index = index\n         self.dim = dim\n         self.pad = pad\n+        self.nInputDim = nInputDim\n         self.outputSize = torch.Size()\n         super(Padding, self).__init__()\n \n@@ -22,6 +25,9 @@\n         self.outputSize = torch.Size(outputSize)\n         dim = self.dim\n \n+        if hasattr(self, \"nInputDim\") and self.nInputDim > 0 and input.dim() != self.nInputDim:\n+            dim = dim + 1\n+\n         self.output.resize_(self.outputSize)\n         self.output.fill_(self.value)\n         index = self.index\n@@ -46,6 +52,9 @@\n         self.gradInput.resize_as_(input)\n         dim = self.dim\n \n+        if hasattr(self, \"nInputDim\") and self.nInputDim > 0 and input.dim() != self.nInputDim:\n+            dim = dim + 1\n+\n         index = self.index\n         pad = self.pad\n         if pad > 0:\n", "issue": "Bug in legacy padding layer\nI've found a bug in [Padding.py](https://github.com/pytorch/pytorch/blob/master/torch/legacy/nn/Padding.py).\r\nIt 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.\r\nTo 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:\r\n`import numpy as np`\r\n`import torch`\r\n`from torch.utils.serialization import load_lua`\r\n`image = np.ones((1,3,1024,2048))`\r\n`tensor = torch.FloatTensor(image)`\r\n`net_torch = load_lua(torch_model)`\r\n`out_torch = net_torch.forward(tensor)`\r\n\r\nNow I've got en exception: \r\nTraceback (most recent call last):\r\n```\r\n  File \"/hdd/PycharmProjects/untitled/test.py\", line 110, in <module>\r\n    out_torch = net_torch.forward(tensor).numpy()\r\n  File \"/usr/local/lib/python2.7/dist-packages/torch/legacy/nn/Module.py\", line 33, in forward\r\n    return self.updateOutput(input)\r\n  File \"/usr/local/lib/python2.7/dist-packages/torch/legacy/nn/Sequential.py\", line 36, in updateOutput\r\n    currentOutput = module.updateOutput(currentOutput)\r\n  File \"/usr/local/lib/python2.7/dist-packages/torch/legacy/nn/Sequential.py\", line 36, in updateOutput\r\n    currentOutput = module.updateOutput(currentOutput)\r\n  File \"/usr/local/lib/python2.7/dist-packages/torch/legacy/nn/CAddTable.py\", line 20, in updateOutput\r\n    self.output.add_(input[i])\r\nRuntimeError: inconsistent tensor size at /b/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:827\r\n```\r\n\r\nPadding is added to wrong axis, what's why CAddTable can't sum blobs with different shapes and throws exception.\r\nIf I edit the code of Padding.py by adding one to self.dim variables (lines 21 and 23 in Padding.py), all works correctly.\n", "before_files": [{"content": "import torch\nfrom .Module import Module\n\n\nclass Padding(Module):\n    # pad puts in [pad] amount of [value] over dimension [dim], starting at\n    # index [index] in that dimension. If pad<0, index counts from the left.\n    # If pad>0 index counts from the right index = 1 pads before index 1.\n    # index = 2 pads starting before index 2 and after index 1 in dimension [dim]\n\n    def __init__(self, dim, pad, value=0, index=0):\n        self.value = value\n        self.index = index\n        self.dim = dim\n        self.pad = pad\n        self.outputSize = torch.Size()\n        super(Padding, self).__init__()\n\n    def updateOutput(self, input):\n        outputSize = list(input.size())\n        outputSize[self.dim] += abs(self.pad)\n        self.outputSize = torch.Size(outputSize)\n        dim = self.dim\n\n        self.output.resize_(self.outputSize)\n        self.output.fill_(self.value)\n        index = self.index\n        pad = self.pad\n        if pad > 0:\n            index = input.size(dim) - index\n        else:\n            pad = -pad\n\n        if index == 0:\n            self.output.narrow(dim, pad, input.size(dim)).copy_(input)\n        elif index == input.size(dim):\n            self.output.narrow(dim, 0, input.size(dim)).copy_(input)\n        else:\n            self.output.narrow(dim, 0, index).copy_(input.narrow(dim, 0, index))\n            self.output.narrow(dim, index + pad, input.size(dim) -\n                               index).copy_(input.narrow(dim, index, input.size(dim) - index))\n\n        return self.output\n\n    def updateGradInput(self, input, gradOutput):\n        self.gradInput.resize_as_(input)\n        dim = self.dim\n\n        index = self.index\n        pad = self.pad\n        if pad > 0:\n            index = input.size(dim) - index\n        else:\n            pad = -pad\n\n        if index == 0:\n            self.gradInput.copy_(gradOutput.narrow(dim, pad, input.size(dim)))\n        elif index == input.size(dim):\n            self.gradInput.copy_(gradOutput.narrow(dim, 0, input.size(dim)))\n        else:\n            self.gradInput.narrow(dim, 0, index).copy_(gradOutput.narrow(dim, 0, index))\n            self.gradInput.narrow(dim, index, input.size(\n                dim) - index).copy_(gradOutput.narrow(dim, index + pad, input.size(dim) - index))\n\n        return self.gradInput\n", "path": "torch/legacy/nn/Padding.py"}], "after_files": [{"content": "import torch\nfrom .Module import Module\n\n\nclass Padding(Module):\n    # pad puts in [pad] amount of [value] over dimension [dim], starting at\n    # index [index] in that dimension. If pad<0, index counts from the left.\n    # If pad>0 index counts from the right index = 1 pads before index 1.\n    # index = 2 pads starting before index 2 and after index 1 in dimension [dim]\n    # When nInputDim is provided, inputs larger than that value will be considered batches\n    # where the actual dim to be padded will be dimension dim + 1.\n\n    def __init__(self, dim, pad, value=0, index=0, nInputDim=0):\n        self.value = value\n        self.index = index\n        self.dim = dim\n        self.pad = pad\n        self.nInputDim = nInputDim\n        self.outputSize = torch.Size()\n        super(Padding, self).__init__()\n\n    def updateOutput(self, input):\n        outputSize = list(input.size())\n        outputSize[self.dim] += abs(self.pad)\n        self.outputSize = torch.Size(outputSize)\n        dim = self.dim\n\n        if hasattr(self, \"nInputDim\") and self.nInputDim > 0 and input.dim() != self.nInputDim:\n            dim = dim + 1\n\n        self.output.resize_(self.outputSize)\n        self.output.fill_(self.value)\n        index = self.index\n        pad = self.pad\n        if pad > 0:\n            index = input.size(dim) - index\n        else:\n            pad = -pad\n\n        if index == 0:\n            self.output.narrow(dim, pad, input.size(dim)).copy_(input)\n        elif index == input.size(dim):\n            self.output.narrow(dim, 0, input.size(dim)).copy_(input)\n        else:\n            self.output.narrow(dim, 0, index).copy_(input.narrow(dim, 0, index))\n            self.output.narrow(dim, index + pad, input.size(dim) -\n                               index).copy_(input.narrow(dim, index, input.size(dim) - index))\n\n        return self.output\n\n    def updateGradInput(self, input, gradOutput):\n        self.gradInput.resize_as_(input)\n        dim = self.dim\n\n        if hasattr(self, \"nInputDim\") and self.nInputDim > 0 and input.dim() != self.nInputDim:\n            dim = dim + 1\n\n        index = self.index\n        pad = self.pad\n        if pad > 0:\n            index = input.size(dim) - index\n        else:\n            pad = -pad\n\n        if index == 0:\n            self.gradInput.copy_(gradOutput.narrow(dim, pad, input.size(dim)))\n        elif index == input.size(dim):\n            self.gradInput.copy_(gradOutput.narrow(dim, 0, input.size(dim)))\n        else:\n            self.gradInput.narrow(dim, 0, index).copy_(gradOutput.narrow(dim, 0, index))\n            self.gradInput.narrow(dim, index, input.size(\n                dim) - index).copy_(gradOutput.narrow(dim, index + pad, input.size(dim) - index))\n\n        return self.gradInput\n", "path": "torch/legacy/nn/Padding.py"}]}