Repository: pytorch
Issue: Add Validity Checks for Parameter Combinations and Input Shapes in torch.nn.MaxPool2d to Prevent Unexpected Outputs
Labels: module: nn,triaged

### 🐛 Describe the bug

When using `torch.nn.MaxPool2d`, specific parameter combinations (such as `kernel_size=2`, `stride=7`, `padding=1`, `dilation=(2, 8)`, `ceil_mode=True`) can result in an output of `-inf`. The following code example reproduces this issue:

```python
import torch
import torch.nn as nn

pool = nn.MaxPool2d(kernel_size=2, stride=7, padding=1, dilation=(2, 8), ceil_mode=True)
x = torch.tensor([[[[0.1772, 0.1772, 0.1772],
                    [0.1772, 0.1772, 0.1772],
                    [0.1772, 0.1772, 0.1772]],

                   [[0.1830, 0.1830, 0.1830],
                    [0.1830, 0.1830, 0.1830],
                    [0.1830, 0.1830, 0.1830]],

                   [[0.1841, 0.1841, 0.1841],
                    [0.1841, 0.1841, 0.1841],
                    [0.1841, 0.1841, 0.1841]]]])

output = pool(x)
print(output)  # Output: tensor([[[[-inf]], [[-inf]], [[-inf]]]])
```
To enhance user experience and model robustness, it is recommended to add validity checks in `torch.nn.MaxPool2d` and other related layers. Specific suggestions include:

1. **Check output dimensions**: Before performing pooling operations, calculate the expected output dimensions and check if they are positive. If the output dimensions are zero or negative, immediately raise an error to prompt the user to adjust the parameters.
   
2. **Validate parameter combinations**: For example, check if the combination of `dilation` and `padding` could cause the pooling window to exceed the input boundaries. If so, suggest the user reset these parameters. ceil_mode is set to true and you should alert if there are abnormal values in the result.

3. **Provide user-friendly error messages**: When detecting unreasonable parameter settings, provide detailed error messages and suggestions to help users understand and resolve the issue.

```python
def max_pool2d_check(input_shape, kernel_size, stride, padding, dilation):
    # Ensure all parameters are tuples for consistent processing
    if isinstance(kernel_size, int):
        kernel_size = (kernel_size, kernel_size)
    if isinstance(stride, int):
        stride = (stride, stride)
    if isinstance(padding, int):
        padding = (padding, padding)
    if isinstance(dilation, int):
        dilation = (dilation, dilation)
    
    # Calculate output dimensions
    output_height = ((input_shape[-2] + 2 * padding[0] - dilation[0] * (kernel_size[0] - 1) - 1) // stride[0] + 1)
    output_width = ((input_shape[-1] + 2 * padding[1] - dilation[1] * (kernel_size[1] - 1) - 1) // stride[1] + 1)
    
    # Check if output dimensions are valid
    if output_height <= 0 or output_width <= 0:
        raise ValueError(f"Invalid pooling parameters with input shape {input_shape}: resulting output size ({output_height}, {output_width}) is invalid.")



pool = nn.MaxPool2d(kernel_size=2, stride=7, padding=1, dilation=(2, 8), ceil_mode=True)

# 测试数据
x = torch.tensor([[[[0.1772, 0.1772, 0.1772],
                    [0.1772, 0.1772, 0.1772],
                    [0.1772, 0.1772, 0.1772]],

                   [[0.1830, 0.1830, 0.1830],
                    [0.1830, 0.1830, 0.1830],
                    [0.1830, 0.1830, 0.1830]],

                   [[0.1841, 0.1841, 0.1841],
                    [0.1841, 0.1841, 0.1841],
                    [0.1841, 0.1841, 0.1841]]]])

input_shape = (1, 3, 3, 3)  # 输入张量形状
kernel_size = 2
stride = 7
padding = 1
dilation = (2, 8)
max_pool2d_check(input_shape, kernel_size, stride, padding, dilation)

output = pool(x)
print(output)

```
For example, adding the above checking code would prevent -inf from appearing in the output.

### Versions

pytorch 2.0.0
python 3.9
cuda 11.7

cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki