class_docstr: Speech enhancement or separation Frontend model
docstr: Sort the input list of tensors by the specified permutation.
  
  Args:
      nn_output: List[torch.Tensor(Batch, ...)], len(nn_output) == num_spk
      perm: (Batch, num_spk) or List[torch.Tensor(num_spk)]
  Returns:
      nn_output_new: List[torch.Tensor(Batch, ...)]
docstr_tok: ["Sort","the","input","list","of","tensors","by","the","specified","permutation",".","Args",":","nn_output",":","List","[","torch.Tensor","(","Batch",",","...",")","]",",","len","(","nn_output",")","==","num_spk","perm",":","(","Batch",",","num_spk",")","or","List","[","torch.Tensor","(","num_spk",")","]","Returns",":","nn_output_new",":","List","[","torch.Tensor","(","Batch",",","...",")","]"]
code: def sort_by_perm(nn_output, perm):
      """Sort the input list of tensors by the specified permutation.
  
      Args:
          nn_output: List[torch.Tensor(Batch, ...)], len(nn_output) == num_spk
          perm: (Batch, num_spk) or List[torch.Tensor(num_spk)]
      Returns:
          nn_output_new: List[torch.Tensor(Batch, ...)]
      """
      if len(nn_output) == 1:
          return nn_output
      # (Batch, num_spk, ...)
      nn_output = torch.stack(nn_output, dim=1)
      if not isinstance(perm, torch.Tensor):
          # perm is a list or tuple
          perm = torch.stack(perm, dim=0)
      assert nn_output.size(1) == perm.size(1), (
          nn_output.shape,
          perm.shape,
      )
      diff_dim = nn_output.dim() - perm.dim()
      if diff_dim > 0:
          perm = perm.view(
              *perm.shape, *[1 for _ in range(diff_dim)]
          ).expand_as(nn_output)
      return torch.gather(nn_output, 1, perm).unbind(dim=1)
  
imports: import contextlib
  from typing import Dict, List, Optional, OrderedDict, Tuple
  import numpy
  import torch
  from packaging.version import parse
  from typeguard import check_argument_types
  from espnet2.diar.layers.abs_mask import AbsMask
  from espnet2.enh.decoder.abs_decoder import AbsDecoder
  from espnet2.enh.encoder.abs_encoder import AbsEncoder
  from espnet2.enh.loss.criterions.tf_domain import FrequencyDomainLoss
  from espnet2.enh.loss.criterions.time_domain import TimeDomainLoss
  from espnet2.enh.loss.wrappers.abs_wrapper import AbsLossWrapper
  from espnet2.enh.separator.abs_separator import AbsSeparator
  from espnet2.enh.separator.dan_separator import DANSeparator
  from espnet2.enh.separator.uses_separator import USESSeparator
  from espnet2.torch_utils.device_funcs import force_gatherable
  from espnet2.train.abs_espnet_model import AbsESPnetModel
label_desc: Use image node_id 9 for calling the ESPnetEnhancementModel obj's underlying member method code with example usage: obj.sort_by_perm(nn_output, perm) and returns: torch, nn_output