RuntimeError: Expected input_lengths to have value at most 144, but got value 190 (while checking arguments for ctc_loss_cpu)

Hello! Does somebody know what is this error means ‘RuntimeError: Expected input_lengths to have value at most 144, but got value 190 (while checking arguments for ctc_loss_cpu)’? I tried to implement conformer model and 144 - model dimension, but I can’t understand what is 190 number because I didn’t use this number.

torch.Size([32, 598, 80]) - input.shape
torch.Size([32]) - inputs_length.shape
torch.Size([32, 78]) - target.shape
torch.Size([32]) - target_length.shape

I guess the input_length contains the length of 190 and raises the error.
Using the example from the docs reproduces it:

# works

# Target are to be padded
T = 50      # Input sequence length
C = 20      # Number of classes (including blank)
N = 16      # Batch size
S = 30      # Target sequence length of longest target in batch (padding length)
S_min = 10  # Minimum target length, for demonstration purposes
# Initialize random batch of input vectors, for *size = (T,N,C)
input = torch.randn(T, N, C).log_softmax(2).detach().requires_grad_()
# Initialize random batch of targets (0 = blank, 1:C = classes)
target = torch.randint(low=1, high=C, size=(N, S), dtype=torch.long)
input_lengths = torch.full(size=(N,), fill_value=T, dtype=torch.long)
target_lengths = torch.randint(low=S_min, high=S, size=(N,), dtype=torch.long)
ctc_loss = nn.CTCLoss()
loss = ctc_loss(input, target, input_lengths, target_lengths)
loss.backward()

# fails

input_lengths[0] = 190
loss = ctc_loss(input, target, input_lengths, target_lengths)
# RuntimeError: Expected input_lengths to have value at most 50, but got value 190 (while checking arguments for ctc_loss_cpu)
1 Like

@ptrblck Thank you. But how can I fix it? I made collate function to pad sequence but it doesn’t work

The input_length vector should contain valid lengths related to the input tensor.
In my example you can see that T = 50 defines the input sequence length. Thus, the input_lengths tensor should contain values indicating the length in [0, 50]. Setting the input_length to a larger value than the actually sequence dimension in input is invalid and will raise the error so check why your input_length tensor contains these invalid lengths.

1 Like