code: def test_pack_padded_sequence(self):
      def generate_test_case(sorted_lengths, should_shuffle):
          def pad(tensor, length):
              return torch.cat(
                  [
                      tensor,
                      tensor.new(
                          length - tensor.size(0), *tensor.size()[1:]
                      ).zero_(),
                  ]
              )
  
          max_length = sorted_lengths[0]
          batch_sizes = [
              sum(map(bool, filter(lambda x: x >= i, sorted_lengths)))
              for i in range(1, max_length + 1)
          ]
          offset = 0
          padded = torch.cat(
              [
                  pad(
                      i * 100
                      + torch.arange(1.0, 5 * l + 1).view(l, 1, 5),
                      max_length,
                  )
                  for i, l in enumerate(sorted_lengths, 1)
              ],
              1,
          )
          expected_data = [
              [
                  torch.arange(1.0, 6) + (i + 1) * 100 + 5 * n
                  for i in range(batch_size)
              ]
              for n, batch_size in enumerate(batch_sizes)
          ]
          expected_data = list(
              itertools.chain.from_iterable(expected_data)
          )
          expected_data = torch.stack(expected_data, dim=0)
  
          if should_shuffle:
              # Shuffle the padded sequence to create an unsorted sequence
              permutation = list(range(len(sorted_lengths)))
              random.shuffle(permutation)
  
              unsorted_indices = torch.tensor(permutation)
              padded = padded.index_select(1, unsorted_indices)
              lengths = torch.tensor(sorted_lengths).index_select(
                  0, unsorted_indices
              )
          else:
              unsorted_indices = None
              lengths = sorted_lengths
  
          return (
              padded.requires_grad_(),
              lengths,
              expected_data,
              batch_sizes,
              unsorted_indices,
          )
  
      test_cases = [
          # sorted_lengths, should_shuffle
          [[10, 8, 4, 2, 2, 2, 1], False],
          [[11, 10, 8, 6, 4, 3, 1], False],
          [[11, 10, 8, 6, 4, 3, 1], True],
      ]
  
      for test_case, batch_first in itertools.product(
          test_cases, (True, False)
      ):
          sorted_lengths, should_shuffle = test_case
          (
              padded,
              lengths,
              expected_data,
              batch_sizes,
              unsorted_indices,
          ) = generate_test_case(sorted_lengths, should_shuffle)
  
          src = padded
          if batch_first:
              src = src.transpose(0, 1)
  
          # check output
          packed = rnn_utils.pack_padded_sequence(
              src,
              lengths,
              batch_first=batch_first,
              enforce_sorted=not should_shuffle,
          )
          self.assertEqual(packed.data.data, expected_data)
          self.assertEqual(packed.batch_sizes, batch_sizes)
          self.assertEqual(packed.unsorted_indices, unsorted_indices)
  
          # test inverse
          unpacked, unpacked_len = rnn_utils.pad_packed_sequence(
              packed, batch_first=batch_first
          )
          self.assertEqual(unpacked, src)
          self.assertEqual(unpacked_len, lengths)
  
          # check grad
          if padded.grad is not None:
              padded.grad.data.zero_()
          grad_output = unpacked.data.clone().normal_()
          unpacked.backward(grad_output)
          if batch_first:
              grad_output.transpose_(0, 1)
          for i, l in enumerate(lengths):
              self.assertEqual(
                  padded.grad.data[:l, i], grad_output[:l, i]
              )
              if l < 10:
                  self.assertEqual(
                      padded.grad.data[l:, i].abs().sum(), 0
                  )
  
      # test error messages
      with self.assertRaisesRegex(
          RuntimeError, "You can pass `enforce_sorted=False`"
      ):
          packed = rnn_utils.pack_padded_sequence(
              torch.randn(3, 3), [1, 3, 2]
          )
      with self.assertRaisesRegex(RuntimeError, "empty tensor"):
          packed = rnn_utils.pack_padded_sequence(torch.randn(0, 0), [])
      with self.assertRaisesRegex(RuntimeError, "empty tensor"):
          packed = rnn_utils.pack_padded_sequence(
              torch.randn([0, 1, 10]),
              torch.randn([11, 14, 14, 2]),
              True,
          )
  
imports: import itertools
  import random
  import torch
  from torch.testing._internal.common_utils import TestCase, run_tests
  import torch.nn.utils.rnn
label_desc: Use image node_id 15 for calling the PackedSequenceTest obj's underlying member method code with example usage: obj.test_pack_padded_sequence() and returns: padded,  lengths,  expected_data,  batch_sizes,  unsorted_indices, torch