Repository: pytorch
Issue: `embedding_bag(..., include_last_offset=True)` should always error if `offsets[-1] != input.size()`
Labels: module: nn,triaged,module: embedding

## 🐛 Bug

As far as I can tell, if `embedding_bag` is given `include_last_offset=True`, then the last element of `offsets` is always supposed to be equal to the size of `input`. My understanding is based on the original issue that proposed adding the `include_last_offset` feature: #29019

However, it is possible to set `offsets[-1] != input.size(0)`. This doesn't seem right to me.

## To Reproduce

```python
>>> embeddings = torch.randn(3, 4)

# Here's a basic valid example with include_last_offset=False
>>> torch.nn.functional.embedding_bag(torch.tensor([0, 0, 1, 2, 0]), embeddings, torch.tensor([0, 3]))
tensor([[ 1.0285,  0.6390, -0.9468,  0.8279],
        [ 0.0011,  0.9743, -0.5688,  0.5476]])

# Here's the correct equivalent call with include_last_offset=True, where offsets[-1] == input.size(0)
>>> torch.nn.functional.embedding_bag(torch.tensor([0, 0, 1, 2, 0]), embeddings, torch.tensor([0, 3, 5]), include_last_offset=True)
tensor([[ 1.0285,  0.6390, -0.9468,  0.8279],
        [ 0.0011,  0.9743, -0.5688,  0.5476]])

# It's possible to set offsets[-1] != input.size(0). offsets[-1]==0 would
# indicate that input.size(0) is 0, which is not true in this case
>>> torch.nn.functional.embedding_bag(torch.tensor([0, 0, 1, 2, 0]), embeddings, torch.tensor([0, 3, 0]), include_last_offset=True)
tensor([[ 1.0285,  0.6390, -0.9468,  0.8279],
        [ 0.0000,  0.0000,  0.0000,  0.0000]])

# Evidently, CUDA treats this logically faulty case differently than CPU
>>> torch.nn.functional.embedding_bag(torch.tensor([0, 0, 1, 2, 0]).cuda(), embeddings.cuda(), torch.tensor([0, 3, 0]).cuda(), include_last_offset=True)
tensor([[ 1.0285,  0.6390, -0.9468,  0.8279],
        [ 0.0011,  0.9743, -0.5688,  0.5476]], device='cuda:0')

# There is at least a check to prevent offsets[-1] > input.size(0), on CPU,
# but the check should be more strict
>>> torch.nn.functional.embedding_bag(torch.tensor([0, 0, 1, 2, 0]), embeddings, torch.tensor([0, 3, 100]), include_last_offset=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/kurtamohler/development_old/pytorch-EmbeddingBag-padding_idx/torch/nn/functional.py", line 2110, in embedding_bag
    ret, _, _, _ = torch.embedding_bag(
RuntimeError: offsets[-1] can not be greater than input's length 5 but got offsets[-1] of 100

# But the offsets[-1] > input.size(0) check does not happen on CUDA
>>> torch.nn.functional.embedding_bag(torch.tensor([0, 0, 1, 2, 0]).cuda(), embeddings.cuda(), torch.tensor([0, 3, 100]).cuda(), include_last_offset=True)
tensor([[ 1.0285,  0.6390, -0.9468,  0.8279],
        [ 0.0011,  0.9743, -0.5688,  0.5476]], device='cuda:0')
```

## Expected behavior

If `include_last_offset=True`, `embedding_bag` should error if `offsets[-1] != input.size()`

## Environment
```
PyTorch version: 1.8.0a0+bc30a6a
Is debug build: False
CUDA used to build PyTorch: 11.0
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (crosstool-NG 1.24.0.133_b0863d8_dirty) 9.3.0
Clang version: Could not collect
CMake version: version 3.19.2

Python version: 3.9 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: 
GPU 0: TITAN RTX
GPU 1: TITAN RTX

Nvidia driver version: 460.27.04
cuDNN version: /usr/local/cuda-10.2.89/targets/x86_64-linux/lib/libcudnn.so.7
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.4
[pip3] torch==1.8.0a0
[conda] magma-cuda110             2.5.2                         1    pytorch
[conda] mkl                       2020.4             h726a3e6_304    conda-forge
[conda] mkl-include               2020.4             h726a3e6_304    conda-forge
[conda] numpy                     1.19.4           py39hdbf815f_2    conda-forge
[conda] torch                     1.8.0a0                   dev_0    <develop>
```

## Additional context

<!-- Add any other context about the problem here. -->


cc @albanD @mruberry @jbschlosser