class_docstr: A stacked, bidirectional LSTM which uses
  :class:`~allennlp.modules.lstm_cell_with_projection.LstmCellWithProjection`'s
  with highway layers between the inputs to layers.
  The inputs to the forward and backward directions are independent - forward and backward
  states are not concatenated between layers.
  Additionally, this LSTM maintains its `own` state, which is updated every time
  ``forward`` is called. It is dynamically resized for different batch sizes and is
  designed for use with non-continuous inputs (i.e inputs which aren't formatted as a stream,
  such as text used for a language modelling task, which is how stateful RNNs are typically used).
  This is non-standard, but can be thought of as having an "end of sentence" state, which is
  carried across different sentences.
  Parameters
  ----------
  input_size : ``int``, required
      The dimension of the inputs to the LSTM.
  hidden_size : ``int``, required
      The dimension of the outputs of the LSTM.
  cell_size : ``int``, required.
      The dimension of the memory cell of the
      :class:`~allennlp.modules.lstm_cell_with_projection.LstmCellWithProjection`.
  num_layers : ``int``, required
      The number of bidirectional LSTMs to use.
  requires_grad: ``bool``, optional
      If True, compute gradient of ELMo parameters for fine tuning.
  recurrent_dropout_probability: ``float``, optional (default = 0.0)
      The dropout probability to be used in a dropout scheme as stated in
      `A Theoretically Grounded Application of Dropout in Recurrent Neural Networks
      <https://arxiv.org/abs/1512.05287>`_ .
  state_projection_clip_value: ``float``, optional, (default = None)
      The magnitude with which to clip the hidden_state after projecting it.
  memory_cell_clip_value: ``float``, optional, (default = None)
      The magnitude with which to clip the memory cell.
docstr: Parameters
  ----------
  inputs : ``PackedSequence``, required.
      A batch first ``PackedSequence`` to run the stacked LSTM over.
  initial_state : ``Tuple[torch.Tensor, torch.Tensor]``, optional, (default = None)
      A tuple (state, memory) representing the initial hidden state and memory
      of the LSTM, with shape (num_layers, batch_size, 2 * hidden_size) and
      (num_layers, batch_size, 2 * cell_size) respectively.
  Returns
  -------
  output_sequence : ``torch.FloatTensor``
      The encoded sequence of shape (num_layers, batch_size, sequence_length, hidden_size)
  final_states: ``Tuple[torch.FloatTensor, torch.FloatTensor]``
      The per-layer final (state, memory) states of the LSTM, with shape
      (num_layers, batch_size, 2 * hidden_size) and  (num_layers, batch_size, 2 * cell_size)
      respectively. The last dimension is duplicated because it contains the state/memory
      for both the forward and backward layers.
docstr_tok: ["Parameters","--","--","--","--","--","inputs",":","``","PackedSequence","``",",","required",".","A","batch","first","``","PackedSequence","``","to","run","the","stacked","LSTM","over",".","initial_state",":","``","Tuple","[","torch.Tensor",",","torch.Tensor","]","``",",","optional",",","(","default","=","None",")","A","tuple","(","state",",","memory",")","representing","the","initial","hidden","state","and","memory","of","the","LSTM",",","with","shape","(","num_layers",",","batch_size",",","2","*","hidden_size",")","and","(","num_layers",",","batch_size",",","2","*","cell_size",")","respectively",".","Returns","--","--","--","-","output_sequence",":","``","torch.FloatTensor","``","The","encoded","sequence","of","shape","(","num_layers",",","batch_size",",","sequence_length",",","hidden_size",")","final_states",":","``","Tuple","[","torch.FloatTensor",",","torch.FloatTensor","]","``","The","per-layer","final","(","state",",","memory",")","states","of","the","LSTM",",","with","shape","(","num_layers",",","batch_size",",","2","*","hidden_size",")","and","(","num_layers",",","batch_size",",","2","*","cell_size",")","respectively",".","The","last","dimension","is","duplicated","because","it","contains","the","state\/memory","for","both","the","forward","and","backward","layers","."]
code: def _lstm_forward(self, inputs, initial_state=None):
      """
      Parameters
      ----------
      inputs : ``PackedSequence``, required.
          A batch first ``PackedSequence`` to run the stacked LSTM over.
      initial_state : ``Tuple[torch.Tensor, torch.Tensor]``, optional, (default = None)
          A tuple (state, memory) representing the initial hidden state and memory
          of the LSTM, with shape (num_layers, batch_size, 2 * hidden_size) and
          (num_layers, batch_size, 2 * cell_size) respectively.
      Returns
      -------
      output_sequence : ``torch.FloatTensor``
          The encoded sequence of shape (num_layers, batch_size, sequence_length, hidden_size)
      final_states: ``Tuple[torch.FloatTensor, torch.FloatTensor]``
          The per-layer final (state, memory) states of the LSTM, with shape
          (num_layers, batch_size, 2 * hidden_size) and  (num_layers, batch_size, 2 * cell_size)
          respectively. The last dimension is duplicated because it contains the state/memory
          for both the forward and backward layers.
      """
      if initial_state is None:
          hidden_states = [None] * len(self.forward_layers)
      elif initial_state[0].size()[0] != len(self.forward_layers):
          raise ConfigurationError(
              "Initial states were passed to forward() but the number of "
              "initial states does not match the number of layers."
          )
      else:
          hidden_states = list(
              zip(
                  initial_state[0].split(1, 0),
                  initial_state[1].split(1, 0),
              )
          )
  
      inputs, batch_lengths = pad_packed_sequence(
          inputs, batch_first=True
      )
      forward_output_sequence = inputs
      backward_output_sequence = inputs
  
      final_states = []
      sequence_outputs = []
      for layer_index, state in enumerate(hidden_states):
          forward_layer = getattr(
              self, "forward_layer_{}".format(layer_index)
          )
          backward_layer = getattr(
              self, "backward_layer_{}".format(layer_index)
          )
  
          forward_cache = forward_output_sequence
          backward_cache = backward_output_sequence
  
          if state is not None:
              forward_hidden_state, backward_hidden_state = state[
                  0
              ].split(self.hidden_size, 2)
              forward_memory_state, backward_memory_state = state[
                  1
              ].split(self.cell_size, 2)
              forward_state = (
                  forward_hidden_state,
                  forward_memory_state,
              )
              backward_state = (
                  backward_hidden_state,
                  backward_memory_state,
              )
          else:
              forward_state = None
              backward_state = None
  
          forward_output_sequence, forward_state = forward_layer(
              forward_output_sequence, batch_lengths, forward_state
          )
          backward_output_sequence, backward_state = backward_layer(
              backward_output_sequence, batch_lengths, backward_state
          )
          # Skip connections, just adding the input to the output.
          if layer_index != 0:
              forward_output_sequence += forward_cache
              backward_output_sequence += backward_cache
  
          sequence_outputs.append(
              torch.cat(
                  [forward_output_sequence, backward_output_sequence],
                  -1,
              )
          )
          # Append the state tuples in a list, so that we can return
          # the final states for all the layers.
          final_states.append(
              (
                  torch.cat([forward_state[0], backward_state[0]], -1),
                  torch.cat([forward_state[1], backward_state[1]], -1),
              )
          )
  
      stacked_sequence_outputs = torch.stack(sequence_outputs)
      # Stack the hidden state and memory for each layer into 2 tensors of shape
      # (num_layers, batch_size, hidden_size) and (num_layers, batch_size, cell_size)
      # respectively.
      final_hidden_states, final_memory_states = zip(*final_states)
      final_state_tuple = (
          torch.cat(final_hidden_states, 0),
          torch.cat(final_memory_states, 0),
      )
      return stacked_sequence_outputs, final_state_tuple
  
file_path: magnitude/pymagnitude/third_party/allennlp/modules/elmo_lstm.py
imports: import warnings
  import torch
  from torch.nn.utils.rnn import PackedSequence, pad_packed_sequence
  import numpy
  from allennlp.modules.lstm_cell_with_projection import LstmCellWithProjection
  from allennlp.common.checks import ConfigurationError
  from allennlp.modules.encoder_base import _EncoderBase
  from allennlp.common.file_utils import cached_path
label_desc: Use image node_id 3 for calling the ElmoLstm obj's underlying member method code with example usage: obj._lstm_forward(inputs, initial_state) and returns: stacked_sequence_outputs,  final_state_tuple