Repository: pytorch
Issue: `nn.functional.max_unpool(2|3)d`: failing shape check for correct inputs (with `dilation > 1`) with specified `output_size`
Labels: module: nn,triaged,module: pooling

## 🐛 Bug

When `max_unpool` receives an input produced with `max_pool` with some values of `dilation > 1`, an error is raised when `output_size` is set to the size of the input from `max_pool`.

## To Reproduce

Below is an example for 2d case, but it could be extended to 3d with the same outcome.

```python                                                                                                                                                                                                              
In [1]: import torch                                                                                     
                                                                                                         
In [2]: import torch.nn.functional as F                                                                  
                                                                                                         
In [3]: x = torch.rand(2, 2, 3, 6)                                                                                                                                                                                 
                                                                                                                                                                                                                   
In [4]: pool, idx = F.max_pool2d(x, kernel_size=3, dilation=(1, 2), return_indices=True)                 
/home/nik/git/Quansight/pytorch/torch/nn/functional.py:735: UserWarning: Note that order of the arguments: ceil_mode and return_indices will changeto match the args list in nn.MaxPool2d in a future release.
  warnings.warn("Note that order of the arguments: ceil_mode and return_indices will change"                                                                                                                       
                                                                                                         
In [5]: unpool = F.max_unpool2d(pool, idx, kernel_size=3, output_size=x.size())            
---------------------------------------------------------------------------                              
ValueError                                Traceback (most recent call last)                              
<ipython-input-5-84fa7bf27ae8> in <module>                                                                                                                                                                         
----> 1 unpool = F.max_unpool2d(pool, idx, kernel_size=3, output_size=x.size())
                                                                                                         
~/git/Quansight/pytorch/torch/nn/functional.py in max_unpool2d(input, indices, kernel_size, stride, padding, output_size)
    990         _stride = kernel_size                                                                    
    991     padding = _pair(padding)                
--> 992     output_size = _unpool_output_size(input, kernel_size, _stride, padding, output_size)                                                                                                                   
    993     return torch._C._nn.max_unpool2d(input, indices, output_size)                                                                                                                                          
    994                                                                                                  
                                                                                                                                                                                                                   
~/git/Quansight/pytorch/torch/nn/functional.py in _unpool_output_size(input, kernel_size, stride, padding, output_size)
    916             max_size = default_size[d] + stride[d]                                               
    917             if not (min_size < output_size[d] < max_size):                                                                                                                                                 
--> 918                 raise ValueError(                                                                
    919                     'invalid output_size "{}" (dim {} must be between {} and {})'.format(        
    920                         output_size, d, min_size, max_size
                                                                                                         
ValueError: invalid output_size "torch.Size([3, 6])" (dim 1 must be between 0 and 6)

```

## Additional context

It is true that `max_unpool` operation does not need the `dilation` parameter as indices produced by the pool operations are sufficient.
However, `dilation` affects the output shape, and the shape check for unpool does not take it into consideration at all. This could be seen from the code of this shape check below:
https://github.com/pytorch/pytorch/blob/33e9a0b5f6674bd429bda689534e1c987b38cf6e/torch/nn/functional.py#L895-L925
Given the `kernel_size`, `stride` and `padding`, it is possible, however, to deduce the value for `dilation` (when output shape is provided) which could be then incorporated into the shape check.


cc @albanD @mruberry @jbschlosser @walterddr