I had the same issue, and on closer look I realised that length was actually longer than the actual length of series.
I used a clamp to prevent that error.
 torch.nn.utils.rnn.pack_padded_sequence(x, length.clamp(max=max_expected_len), enforce_sorted=False, batch_first=True)