code: def unpack_sequence(
      sequence: Union[torch.Tensor, rnn.PackedSequence]
  ) -> Tuple[torch.Tensor, torch.Tensor]:
      """
      Unpack RNN sequence.
  
      Args:
          sequence (Union[torch.Tensor, rnn.PackedSequence]): RNN packed sequence or tensor of which
              first index are samples and second are timesteps
  
      Returns:
          Tuple[torch.Tensor, torch.Tensor]: tuple of unpacked sequence and length of samples
      """
      if isinstance(sequence, rnn.PackedSequence):
          sequence, lengths = rnn.pad_packed_sequence(
              sequence, batch_first=True
          )
          # batch sizes reside on the CPU by default -> we need to bring them to GPU
          lengths = lengths.to(sequence.device)
      else:
          lengths = torch.ones(
              sequence.size(0), device=sequence.device, dtype=torch.long
          ) * sequence.size(1)
      return sequence, lengths
  
imports: from collections import namedtuple
  from contextlib import redirect_stdout
  import inspect
  import os
  from typing import Any, Callable, Dict, List, Tuple, Union
  import lightning.pytorch
  import torch
  from torch import nn
  from torch.fft import irfft, rfft
  import torch.nn.functional
  from torch.nn.utils import rnn
label_desc: Use image node_id 8 for calling a global function with example usage: unpack_sequence(sequence) and returns: sequence,  lengths