class_docstr: RNN Encoder.
docstr: Encode sequence.
  
  :param xs: (bsz x seqlen) LongTensor of input token indices
  
  :returns: encoder outputs, hidden state, attention mask
      encoder outputs are the output state at each step of the encoding.
      the hidden state is the final hidden state of the encoder.
      the attention mask is a mask of which input values are nonzero.
docstr_tok: ["Encode","sequence",".",":","param","xs",":","(","bsz","x","seqlen",")","LongTensor","of","input","token","indices",":","returns",":","encoder","outputs",",","hidden","state",",","attention","mask","encoder","outputs","are","the","output","state","at","each","step","of","the","encoding",".","the","hidden","state","is","the","final","hidden","state","of","the","encoder",".","the","attention","mask","is","a","mask","of","which","input","values","are","nonzero","."]
code: def forward(self, xs):
      """
      Encode sequence.
  
      :param xs: (bsz x seqlen) LongTensor of input token indices
  
      :returns: encoder outputs, hidden state, attention mask
          encoder outputs are the output state at each step of the encoding.
          the hidden state is the final hidden state of the encoder.
          the attention mask is a mask of which input values are nonzero.
      """
      bsz = len(xs)
  
      # embed input tokens
      xs = self.input_dropout(xs)
      xes = self.dropout(self.lt(xs))
      attn_mask = xs.ne(0)
      try:
          x_lens = torch.sum(attn_mask.int(), dim=1).cpu()
          xes = pack_padded_sequence(xes, x_lens, batch_first=True)
          packed = True
      except ValueError:
          # packing failed, don't pack then
          packed = False
  
      encoder_output, hidden = self.rnn(xes)
      if packed:
          # total_length to make sure we give the proper length in the case
          # of multigpu settings.
          # https://pytorch.org/docs/stable/notes/faq.html#pack-rnn-unpack-with-data-parallelism
          encoder_output, _ = pad_packed_sequence(
              encoder_output, batch_first=True, total_length=xs.size(1)
          )
  
      if self.dirs > 1:
          # project to decoder dimension by taking sum of forward and back
          if isinstance(self.rnn, nn.LSTM):
              hidden = (
                  hidden[0].view(-1, self.dirs, bsz, self.hsz).sum(1),
                  hidden[1].view(-1, self.dirs, bsz, self.hsz).sum(1),
              )
          else:
              hidden = hidden.view(-1, self.dirs, bsz, self.hsz).sum(1)
  
      return encoder_output, _transpose_hidden_state(hidden), attn_mask
  
imports: import math
  import torch
  import torch.nn
  from torch.nn.parameter import Parameter
  from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence
  import torch.nn.functional
  from parlai.utils.torch import neginf
  from parlai.core.torch_generator_agent import TorchGeneratorModel
label_desc: Use image node_id 2 for calling the RNNEncoder obj's underlying member method code with example usage: obj.forward(xs) and returns: encoder_output,  _transpose_hidden_state,  attn_mask