code: def test_sort_tensor_by_length(self):
      tensor = torch.rand([5, 7, 9])
      tensor[0, 3:, :] = 0
      tensor[1, 4:, :] = 0
      tensor[2, 1:, :] = 0
      tensor[3, 5:, :] = 0
  
      sequence_lengths = torch.LongTensor([3, 4, 1, 5, 7])
      (
          sorted_tensor,
          sorted_lengths,
          reverse_indices,
          _,
      ) = util.sort_batch_by_length(tensor, sequence_lengths)
  
      # Test sorted indices are padded correctly.
      numpy.testing.assert_array_equal(
          sorted_tensor[1, 5:, :].data.numpy(), 0.0
      )
      numpy.testing.assert_array_equal(
          sorted_tensor[2, 4:, :].data.numpy(), 0.0
      )
      numpy.testing.assert_array_equal(
          sorted_tensor[3, 3:, :].data.numpy(), 0.0
      )
      numpy.testing.assert_array_equal(
          sorted_tensor[4, 1:, :].data.numpy(), 0.0
      )
  
      assert sorted_lengths.data.equal(
          torch.LongTensor([7, 5, 4, 3, 1])
      )
  
      # Test restoration indices correctly recover the original tensor.
      assert sorted_tensor.index_select(0, reverse_indices).data.equal(
          tensor.data
      )
  
imports: from __future__ import with_statement
  from __future__ import division
  from __future__ import absolute_import
  import numpy
  from numpy.testing import assert_array_almost_equal, assert_almost_equal
  import torch
  import pytest
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.nn import util
label_desc: Use image node_id 4 for calling the TestNnUtil obj's underlying member method code with example usage: obj.test_sort_tensor_by_length() without return types