maybe i have the same problem,i try two ways,
First:
when i spcify the function code ‘pack_padded_sequence’:
def pack_padded_sequence(input, lengths, batch_first=False, enforce_sorted=True):
    if torch._C._get_tracing_state() and not isinstance(lengths, torch.Tensor):
        warnings.warn('pack_padded_sequence has been called with a Python list of '
                      'sequence lengths. The tracer cannot track the data flow of Python '
                      'values, and it will treat them as constants, likely rendering '
                      'the trace incorrect for any other combination of lengths.',
                      stacklevel=2)
    lengths = torch.as_tensor(lengths, dtype=torch.int64)
    lengths = lengths.cpu() # 此处给pytorch提的issue解决的
    if enforce_sorted:
        sorted_indices = None
    else:
        lengths, sorted_indices = torch.sort(lengths, descending=True)
        sorted_indices = sorted_indices.to(input.device)
        batch_dim = 0 if batch_first else 1
        input = input.index_select(batch_dim, sorted_indices)

    data, batch_sizes = \
        _VF._pack_padded_sequence(input, lengths, batch_first)
    return PackedSequence(data, batch_sizes, sorted_indices, None)

when i debug my code, the type of input and lengths is all ‘‘torch.LongTensor’’  and ‘‘torch.FloatTensor’’,
企业微信截图_159038988921711662×1360 150 KB
but it raise the error:

File “C:\Anaconda3\lib\site-packages\torch\nn\utils\rnn.py”, line 277, in pack_padded_sequence
_VF._pack_padded_sequence(input, lengths, batch_first)
RuntimeError: ‘lengths’ argument should be a 1D CPU int64 tensor

Second  i try def the function ‘hotfix_pack_padded_sequence’:
def hotfix_pack_padded_sequence(input, lengths, batch_first=False, enforce_sorted=True):
    lengths = torch.as_tensor(lengths, dtype=torch.int64)
    lengths = lengths.cpu()
    if enforce_sorted:
        sorted_indices = None
    else:
        lengths, sorted_indices = torch.sort(lengths, descending=True)
        sorted_indices = sorted_indices.to(input.device)
        batch_dim = 0 if batch_first else 1
        input = input.index_select(batch_dim, sorted_indices)

    data, batch_sizes = \
        torch._C._VariableFunctions._pack_padded_sequence(input, lengths, batch_first)
    return PackedSequence(data, batch_sizes, sorted_indices)

when i debug my code, the type of input and lengths is all ‘‘torch.LongTensor’’  and ‘‘torch.FloatTensor’’,
but it raise the same error:

“D:\soft\NLP_Frame\algorithm\modules\encoder\lstm_encoder.py”, line 55, in hotfix_pack_padded_sequence
torch._C._VariableFunctions._pack_padded_sequence(input, lengths, batch_first)
RuntimeError: ‘lengths’ argument should be a 1D CPU int64 tensor

I’m going crazy，