code: def sort_batch_by_length(tensor, sequence_lengths):
      """
      Sort a batch first tensor by some specified lengths.
  
      Parameters
      ----------
      tensor : torch.FloatTensor, required.
          A batch first Pytorch tensor.
      sequence_lengths : torch.LongTensor, required.
          A tensor representing the lengths of some dimension of the tensor which
          we want to sort by.
  
      Returns
      -------
      sorted_tensor : torch.FloatTensor
          The original tensor sorted along the batch dimension with respect to sequence_lengths.
      sorted_sequence_lengths : torch.LongTensor
          The original sequence_lengths sorted by decreasing size.
      restoration_indices : torch.LongTensor
          Indices into the sorted_tensor such that
          ``sorted_tensor.index_select(0, restoration_indices) == original_tensor``
      permuation_index : torch.LongTensor
          The indices used to sort the tensor. This is useful if you want to sort many
          tensors using the same ordering.
      """
  
      if not isinstance(tensor, torch.Tensor) or not isinstance(
          sequence_lengths, torch.Tensor
      ):
          raise ConfigurationError(
              "Both the tensor and sequence lengths must be torch.Tensors."
          )
  
      (
          sorted_sequence_lengths,
          permutation_index,
      ) = sequence_lengths.sort(0, descending=True)
      sorted_tensor = tensor.index_select(0, permutation_index)
  
      index_range = sequence_lengths.new_tensor(
          torch.arange(0, len(sequence_lengths))
      )
      # This is the equivalent of zipping with index, sorting by the original
      # sequence lengths and returning the now sorted indices.
      _, reverse_mapping = permutation_index.sort(0, descending=False)
      restoration_indices = index_range.index_select(0, reverse_mapping)
      return (
          sorted_tensor,
          sorted_sequence_lengths,
          restoration_indices,
          permutation_index,
      )
  
imports: from __future__ import division
  from __future__ import absolute_import
  from collections import defaultdict
  import logging
  import math
  import torch
  from allennlp.common.checks import ConfigurationError
label_desc: Use image node_id 4 for calling a global function with example usage: sort_batch_by_length(tensor, sequence_lengths) and returns: sorted_tensor,  sorted_sequence_lengths,  restoration_indices,  permutation_index