class_docstr: RNN encoder.
class_docstr_tok: ["RNN","encoder","."]
code: def forward(self, src_tokens, src_lengths):
      if self.left_pad:
          # convert left-padding to right-padding
          src_tokens = utils.convert_padding_direction(
              src_tokens, self.padding_idx, left_to_right=True
          )
      bsz, seqlen = pytorch_translate_utils.get_source_tokens_tensor(
          src_tokens
      ).size()
  
      # embed tokens
      x = self.embed_tokens(src_tokens)
      if self.encoder_context_embed:
          x = self.embed_tokens_context(x)
      x = F.dropout(x, p=self.dropout_in, training=self.training)
  
      # B x T x C -> T x B x C
      x = x.transpose(0, 1)
      embedded_words = x
  
      # Generate packed seq to deal with varying source seq length
      packed_input, batch_sizes, _, _ = pack_padded_sequence(
          x, src_lengths
      )
      final_hiddens, final_cells = [], []
      next_hiddens = []
      for i, rnn_layer in enumerate(self.layers):
          current_hidden_size = (
              self.hidden_dim // 2
              if rnn_layer.is_bidirectional
              else self.hidden_dim
          )
  
          if self.cell_type in ["lstm", "milstm", "layer_norm_lstm"]:
              prev_hidden = (
                  x.new(bsz, current_hidden_size).zero_(),
                  x.new(bsz, current_hidden_size).zero_(),
              )
          else:
              raise Exception(f"{self.cell_type} not implemented")
  
          hidden, current_output = rnn_layer.forward(
              packed_input, prev_hidden, batch_sizes
          )
          next_hiddens.append(hidden)
          prev_hidden = next_hiddens[-1]
  
          if self.dropout_out != 0:
              current_output = F.dropout(
                  current_output,
                  p=self.dropout_out,
                  training=self.training,
              )
  
          if (
              self.residual_level is not None
              and i >= self.residual_level
          ):
              packed_input = packed_input.clone() + current_output
          else:
              packed_input = current_output
  
      final_hiddens, final_cells = zip(*next_hiddens)
      # Reshape to [num_layer, batch_size, hidden_dim]
      final_hiddens = torch.cat(final_hiddens, dim=0).view(
          self.num_layers, *final_hiddens[0].size()
      )
      final_cells = torch.cat(final_cells, dim=0).view(
          self.num_layers, *final_cells[0].size()
      )
  
      #  [max_seqlen, batch_size, hidden_dim]
      unpacked_output, _ = pad_packed_sequence(
          PackedSequence(packed_input, batch_sizes)
      )
  
      return (
          unpacked_output,
          final_hiddens,
          final_cells,
          src_lengths,
          src_tokens,
          embedded_words,
      )
  
imports: import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  import torch.onnx.operators
  from fairseq import options, utils
  from fairseq.models import FairseqEncoder, FairseqEncoderDecoderModel, register_model, register_model_architecture
  from fairseq.modules import AdaptiveSoftmax
  from pytorch_translate import attention, rnn_cell, utils, vocab_reduction
  from pytorch_translate.common_layers import ContextEmbedding, DecoderWithOutputProjection, Embedding, Linear, RNNLayer, VariableTracker
  from pytorch_translate.data import data
  from pytorch_translate.multi_model import MultiDecoder, MultiEncoder
  from pytorch_translate.multilingual import MultilingualDecoder, MultilingualEncoder
  from pytorch_translate.ngram import NGramDecoder
  from pytorch_translate.semi_supervised import SemiSupervisedModel
  from pytorch_translate.utils import maybe_cat, maybe_cuda, torch_find
  from torch.nn.utils.rnn import pack_padded_sequence, PackedSequence, pad_packed_sequence
label_desc: Use image node_id 2 for calling the RNNEncoder obj's underlying member method code with example usage: obj.forward(src_tokens, src_lengths) and returns: unpacked_output,  final_hiddens,  final_cells,  src_lengths,  src_tokens,  embedded_words