Repository: pytorch
Issue: An exported onnx model can't reduce on dim with value of 0 if 'keepdims' is false
Labels: module: onnx,triaged

## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->
An exported onnx model can't reduce on dim with value of 0 if 'keepdims' is false however there are no exceptions on the pytorch side.

## To Reproduce

Steps to reproduce the behavior:
```
import numpy as np
import onnxruntime as ort
import onnx
import torch
import torch.nn as nn


class Module(nn.Module):
    def forward(self, x):
        out = x.sum(dim=0)
        return out


@torch.no_grad()
def eval_x(x):
    model = Module()
    model.eval()
    out = model(x)
    out = out.numpy()
    onnx_path = 'model.onnx'
    torch.onnx.export(model, x, onnx_path)
    ort_session = ort.InferenceSession(onnx_path)
    input_names = [inp.name for inp in ort_session.get_inputs()]
    np_input_sample = (x.numpy().astype(np.float32),)
    onnx_out = ort_session.run(None, dict(zip(input_names, np_input_sample)))[0]
    assert np.allclose(out.flatten(), onnx_out.flatten(), rtol=1e-5)


def test_onnx_export():
    for x in (torch.randn(1, 10), torch.randn(0, 10)):
        print(f'Processing {x}')
        eval_x(x)


def main():
    test_onnx_export()

if __name__ == "__main__":
    main()
```


Traceback:
```
Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/discort/tests/test_export.py", line 153, in <module>
    main()
  File "/home/discort/tests/test_export.py", line 149, in main
    test_onnx_export()
  File "/home/discort/.env/lib/python3.7/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/home/discort/tests/test_export.py", line 60, in test_onnx_export
    onnx_out = ort_session.run(None, dict(zip(input_names, np_input_sample)))[0]
  File "/home/discort/.env/lib/python3.7/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 188, in run
    return self._sess.run(output_names, input_feed, run_options)
onnxruntime.capi.onnxruntime_pybind11_state.RuntimeException: [ONNXRuntimeError] : 6 : RUNTIME_EXCEPTION : Non-zero status code returned while running ReduceSum node. Name:'ReduceSum_0' Status Message: /onnxruntime_src/onnxruntime/core/providers/cpu/reduction/reduction_ops.cc:754 void onnxruntime::ValidateKeepDims(const onnxruntime::TensorShape&, int64_t) keepdims was false. Can't reduce on dim with value of 0 if 'keepdims' is false. Invalid output shape would be produced. input_shape:{0,10}
```

## Expected behavior

The output from onnx must me the same as for torch

## Environment

```
Collecting environment information...
PyTorch version: 1.8.1+cu102
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.18.4
Libc version: glibc-2.26

Python version: 3.7.5 (default, Feb 23 2021, 13:22:40)  [GCC 8.4.0] (64-bit runtime)
Python platform: Linux-5.4.0-1055-aws-x86_64-with-Ubuntu-18.04-bionic
Is CUDA available: True
CUDA runtime version: Could not collect
...
Nvidia driver version: 450.80.02
Versions of relevant libraries:
[pip3] numpy==1.19.5
[pip3] pytorch-lightning==1.2.8
[pip3] pytorch-ranger==0.1.1
[pip3] torch==1.8.1
[pip3] torch-optimizer==0.1.0
[pip3] torchmetrics==0.4.1
[pip3] torchvision==0.9.1
```

## Additional context

<!-- Add any other context about the problem here. -->


cc @BowenBao @neginraoof