code: def test_non_stateful_states_are_sorted_correctly(self):
      encoder_base = _EncoderBase(stateful=False)
      initial_states = (torch.randn(6, 5, 7), torch.randn(6, 5, 7))
      # Check that we sort the state for non-stateful encoders. To test
      # we'll just use a "pass through" encoder, as we aren't actually testing
      # the functionality of the encoder here anyway.
      (
          _,
          states,
          restoration_indices,
      ) = encoder_base.sort_and_run_forward(
          lambda *x: x, self.tensor, self.mask, initial_states
      )
      # Our input tensor had 2 zero length sequences, so we need
      # to concat a tensor of shape
      # (num_layers * num_directions, batch_size - num_valid, hidden_dim),
      # to the output before unsorting it.
      zeros = torch.zeros([6, 2, 7])
  
      # sort_and_run_forward strips fully-padded instances from the batch;
      # in order to use the restoration_indices we need to add back the two
      #  that got stripped. What we get back should match what we started with.
      for state, original in izip(states, initial_states):
          assert list(state.size()) == [6, 3, 7]
          state_with_zeros = torch.cat([state, zeros], 1)
          unsorted_state = state_with_zeros.index_select(
              1, restoration_indices
          )
          for index in [0, 1, 3]:
              numpy.testing.assert_array_equal(
                  unsorted_state[:, index, :].data.numpy(),
                  original[:, index, :].data.numpy(),
              )
  
file_path: magnitude/pymagnitude/third_party/allennlp/tests/modules/encoder_base_test.py
imports: from __future__ import absolute_import
  import numpy
  import torch
  from torch.nn import LSTM, RNN
  from allennlp.modules.encoder_base import _EncoderBase
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.nn.util import sort_batch_by_length, get_lengths_from_binary_sequence_mask
label_desc: Use image node_id 2 for calling the TestEncoderBase obj's underlying member method code with example usage: obj.test_non_stateful_states_are_sorted_correctly() without return types