Open
Description
🐛 Bug
Hi,
While working on some CTC extensions, I noticed that torch's CTCLoss was computing incorrect gradient. At least when using CPU (I have not tested on GPU yet). I observed this problem on both pytorch 1.3 and pytorch 1.7.1 , using MacOS/anaconda distribution.
To Reproduce
Torch's own gradcheck tool detect this problem. Here is a minimal example of gradient computation going wrong (for the record, it is not a problem of precision with gradcheck. The computed gradient is really wrong.
import torch
import torch.nn as nn
# For this minimal example, let us use a mninbatch of size 1, 3 time steps and a label vocabulary size of 2
V = 2
T = 3
# Generate some random input parameters for CTC
params_ctc = nn.functional.log_softmax(torch.randn(T, 1, V+1))
params_ctc = params_ctc.double() # convert to double so that gradcheck is more accurate
params_ctc.requires_grad = True
torch_ctc = nn.CTCLoss()
labels = torch.tensor([[1]] )
labels_size = torch.tensor([1])
T_lengths = torch.tensor([T])
torch.autograd.gradcheck(torch_ctc, [params_ctc,
labels ,T_lengths, labels_size])
Expected behavior
The gradcheck call above should pass without error.
Environment
- PyTorch Version (e.g., 1.0): 1.7.1
- OS (e.g., Linux): MacOS
- How you installed PyTorch (
conda
,pip
, source): conda - Build command you used (if compiling from source):
- Python version: 3.8
- CUDA/cuDNN version: N/A (CPU)
- GPU models and configuration: N/A
- Any other relevant information:
cc @ezyang @albanD @zou3519 @gqchen @pearu @nikitaved @soulitzer @brianjo @mruberry @jlin27