fix calculation of number of elements to not overflow (#46997)
Summary:
Possibly fixes https://github.com/pytorch/pytorch/issues/46764.
Computing number of tensor elements in many cases is written as
```
int64_t numel = std::accumulate(oldshape.begin(), oldshape.end(), 1,
std::multiplies<int64_t>());
```
This computes the product with the type of `1` literal, which is `int`. When there's more than INT_MAX elements, result overflows. In https://github.com/pytorch/pytorch/issues/46746, the tensor that was sent to reshape had 256^4 elements, and that was computed as `0`, so reshape was not done correctly.
I've audited usages of std::accumulate and changed them to use int64_t as `init` type.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46997
Reviewed By: albanD
Differential Revision: D24624654
Pulled By: ngimel
fbshipit-source-id: 3d9c5e6355531a9df6b10500eec140e020aac77e
Author
Natalia Gimelshein