class MyModule(nn.Module):
       # ... init, other methods, etc.
   # padded_input is of shape [B x T x *] (batch_first mode) and contains
   # the sequences sorted by lengths
   #   B is the batch size
   #   T is max sequence length
   def forward(self, padded_input, input_lengths):
       total_length = padded_input.size(1)  # get the max sequence length
       packed_input = pack_padded_sequence(padded_input, input_lengths,
                                           batch_first=True)
       packed_output, _ = self.my_lstm(packed_input)
       output, _ = pad_packed_sequence(packed_output, batch_first=True,
                                       total_length=total_length)
       return output

m = MyModule().cuda()
   dp_m = nn.DataParallel(m)
Additionally, extra care needs to be taken when batch dimension is dim
"1" (i.e., "batch_first=False") with data parallelism. In this case,