If padded is a Variable with padding in it and lengths is a tensor containing the length of each sequence, then this is how to run a (potentially bidirectional) LSTM over the sequences in a way that doesn’t include padding, then pad the result in order to use it in further computations.
import torch.nn.utils.rnn as rnn_utils  
lstm = nn.LSTM(in_size, hidden_size, bidirectional, num_layers)
packed = rnn_utils.pack_padded_sequence(padded, lengths)
packed_out, packed_hidden = lstm(packed)
unpacked, unpacked_len = rnn_utils.pad_packed_sequence(packed_out)