class_docstr: RNN Encoder.
  
  Modified to encode history vector in context lstm.
class_docstr_tok: ["RNN","Encoder",".","Modified","to","encode","history","vector","in","context","lstm","."]
code: def forward(self, xs, context_vec, hist_lens):
      # encode current utterrance
      (
          enc_state,
          (hidden_state, cell_state),
          attn_mask,
      ) = super().forward(xs)
      # if all utterances in context vec length 1, unsqueeze to prevent loss of dimensionality
      if len(context_vec.shape) < 2:
          context_vec = context_vec.unsqueeze(1)
      # get utt lengths of each utt in context vector
      utt_lens = torch.sum(context_vec.ne(0).int(), dim=1)
      # sort by lengths descending for utterance encoder
      sorted_lens, sorted_idx = utt_lens.sort(descending=True)
      sorted_context_vec = context_vec[sorted_idx]
      (_, (sorted_hidden_state, _), _) = super().forward(
          sorted_context_vec
      )
      sorted_final_hidden_states = sorted_hidden_state[:, -1, :]
  
      ### reshape and pad hidden states to bsz x max_hist_len x hidden_size using hist_lens
      original_order_final_hidden = torch.zeros_like(
          sorted_final_hidden_states
      ).scatter_(
          0,
          sorted_idx.unsqueeze(1).expand(
              -1, sorted_final_hidden_states.shape[1]
          ),
          sorted_final_hidden_states,
      )
      # pad to max hist_len
      original_size_final_hidden = self.sequence_to_padding(
          original_order_final_hidden, hist_lens
      )
      # pack padded sequence so that we ignore padding
      original_size_final_hidden_packed = (
          nn.utils.rnn.pack_padded_sequence(
              original_size_final_hidden,
              hist_lens.cpu(),
              batch_first=True,
              enforce_sorted=False,
          )
      )
      # pass through context lstm
      _, (context_h_n, _) = self.context_lstm(
          original_size_final_hidden_packed
      )
      return (
          enc_state,
          (hidden_state, cell_state),
          attn_mask,
          _transpose_hidden_state(context_h_n),
      )
  
file_path: ParlAI/parlai/agents/hred/modules.py
imports: import torch
  import torch.nn
  from parlai.agents.seq2seq.modules import OutputLayer, RNNEncoder, _transpose_hidden_state
  from parlai.core.torch_generator_agent import TorchGeneratorModel
label_desc: Use image node_id 2 for calling the HredEncoder obj's underlying member method code with example usage: obj.forward(xs, context_vec, hist_lens) and returns: enc_state,  hidden_state,  cell_state,  attn_mask,  _transpose_hidden_state