`torch.nn.CTCLoss` Trigger heap-buffer-overflow under AddressSanitizer
Labels: module: nn, module: loss, triaged, actionable, module: sanitizers, module: edge cases

### 🐛 Describe the bug

A test case for `torch.nn.CTCLoss` aborted because of heap-buffer-overflow error when it's run under the address sanitizer.
Reproduction:
```python
import torch

def test():
    ctc_loss = torch.nn.CTCLoss()
    arg_1_0 = torch.rand([50, 16, 20], dtype=torch.float32).clone()
    arg_1_1 = torch.randint(-512,4,[16, 30], dtype=torch.int64).clone()
    arg_1_2 = torch.randint(-4,0,[16], dtype=torch.int64).clone()
    arg_1_3 = torch.randint(-512,0,[16], dtype=torch.int64).clone()
    arg_1 = [arg_1_0,arg_1_1,arg_1_2,arg_1_3,]
    res = ctc_loss(*arg_1)

test()
```
Error log:
```bash
=================================================================
==202==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x609000000284 at pc 0x7fecebd6b267 bp 0x7ffe509837b0 sp 0x7ffe509837a8
WRITE of size 4 at 0x609000000284 thread T0
    #0 0x7fecebd6b266  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xdbbd266)
    #1 0x7fecebd5fac4  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xdbb1ac4)
    #2 0x7fecebd747ad  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xdbc67ad)
    #3 0x7fecee341ece  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0x10193ece)
    #4 0x7fecece4375a  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xec9575a)
    #5 0x7fececb6bbc6  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xe9bdbc6)
    #6 0x7fecf2acf943  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0x14921943)
    #7 0x7fecf2aced44  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0x14920d44)
    #8 0x7fececb6b53f  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xe9bd53f)
    #9 0x7fed129edfed  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_python.so+0x25d8fed)
    #10 0x4d23df  (/usr/bin/python3.7+0x4d23df)
    #11 0x51088a  (/usr/bin/python3.7+0x51088a)
    #12 0x58fd36  (/usr/bin/python3.7+0x58fd36)
    #13 0x50c4fb  (/usr/bin/python3.7+0x50c4fb)
    #14 0x5b4ee5  (/usr/bin/python3.7+0x5b4ee5)
    #15 0x6005a2  (/usr/bin/python3.7+0x6005a2)
    #16 0x607795  (/usr/bin/python3.7+0x607795)
    #17 0x60785b  (/usr/bin/python3.7+0x60785b)
    #18 0x60a435  (/usr/bin/python3.7+0x60a435)
    #19 0x64db81  (/usr/bin/python3.7+0x64db81)
    #20 0x64dd2d  (/usr/bin/python3.7+0x64dd2d)
    #21 0x7fed1d089c86  (/lib/x86_64-linux-gnu/libc.so.6+0x21c86)
    #22 0x5b6369  (/usr/bin/python3.7+0x5b6369)

0x609000000284 is located 0 bytes to the right of 4-byte region [0x609000000280,0x609000000284)
allocated by thread T0 here:
    #0 0x7fed1d55f308  (/usr/lib/llvm-6.0/lib/clang/6.0.0/lib/linux/libclang_rt.asan-x86_64.so+0x106308)
    #1 0x7fecdddc5101  (/usr/local/lib/python3.7/dist-packages/torch/lib/libc10.so+0x16a101)
    #2 0x7fecddd4f9a1  (/usr/local/lib/python3.7/dist-packages/torch/lib/libc10.so+0xf49a1)
    #3 0x7fecebbf3ada  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xda45ada)
    #4 0x7fecebbf481e  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xda4681e)
    #5 0x7fecebbf6240  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xda48240)
    #6 0x7fecebbf320d  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xda4520d)
    #7 0x7fecebbf2ffb  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xda44ffb)
    #8 0x7fecebd5f70c  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xdbb170c)
    #9 0x7fecebd747ad  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xdbc67ad)
    #10 0x7fecee341ece  (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0x10193ece)

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/local/lib/python3.7/dist-packages/torch/lib/libtorch_cpu.so+0xdbbd266) 
Shadow bytes around the buggy address:
  0x0c127fff8000: fa fa fa fa fa fa fa fa fd fa fa fa fa fa fa fa
  0x0c127fff8010: fa fa fa fa fa fa fa fa 04 fa fa fa fa fa fa fa
  0x0c127fff8020: fa fa fa fa fa fa fa fa fd fd fa fa fa fa fa fa
  0x0c127fff8030: fa fa fa fa fa fa fa fa 00 00 fa fa fa fa fa fa
  0x0c127fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x0c127fff8050:[04]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff8090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff80a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==202==ABORTING
```

### Versions

```
PyTorch version: 1.14.0a0+gita86278b
Is debug build: False
CUDA used to build PyTorch: Could not collect
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.1 LTS (x86_64)
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Clang version: 11.1.0-6
CMake version: version 3.24.1
Libc version: glibc-2.35

Python version: 3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.15.0-50-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: 11.6.124
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: 
GPU 0: NVIDIA GeForce RTX 3090
GPU 1: NVIDIA GeForce RTX 3090
GPU 2: NVIDIA GeForce RTX 3090

Nvidia driver version: 515.65.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.4.1
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.21.5
[pip3] torch==1.14.0a0+gita86278b
[pip3] torchvision==0.13.1
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               11.3.1               h2bc3f7f_2  
[conda] mkl                       2021.4.0           h06a4308_640  
[conda] mkl-service               2.4.0            py39h7f8727e_0  
[conda] mkl_fft                   1.3.1            py39hd3c417c_0  
[conda] mkl_random                1.2.2            py39h51133e4_0  
[conda] numpy                     1.21.5           py39he7a7128_1  
[conda] numpy-base                1.21.5           py39hf524024_1  
[conda] numpydoc                  1.2                pyhd3eb1b0_0
```

cc @ezyang @gchanan @zou3519 @albanD @mruberry @jbschlosser @walterddr @kshitij12345 @saketh-are