code: def test_dynamic_batch_size(self):
      if not FAIRSEQ_AVAILABLE:
          return
      ds, max_tokens, tokenizer = self._get_dataset(max_len=64)
      required_batch_size_multiple = 64
      batch_sampler = ds.make_dynamic_sampler(
          max_tokens,
          required_batch_size_multiple=required_batch_size_multiple,
      )
      batch_sizes = [len(x) for x in batch_sampler]
      assert (
          len(set(batch_sizes)) > 1
      )  # it's not dynamic batch size if every batch is the same length
      assert sum(batch_sizes) == len(ds)  # no dropped or added examples
      data_loader = DataLoader(
          ds,
          batch_sampler=batch_sampler,
          collate_fn=ds.collate_fn,
          num_workers=2,
      )
      failures = []
      num_src_per_batch = []
      for batch in data_loader:
          src_shape = batch["input_ids"].shape
          bs = src_shape[0]
          assert (
              bs % required_batch_size_multiple == 0
              or bs < required_batch_size_multiple
          )
          num_src_tokens = np.product(batch["input_ids"].shape)
          num_src_per_batch.append(num_src_tokens)
          if num_src_tokens > (max_tokens * 1.1):
              failures.append(num_src_tokens)
      assert num_src_per_batch[0] == max(num_src_per_batch)
      if failures:
          raise AssertionError(
              f"too many tokens in {len(failures)} batches"
          )
  
imports: import os
  from pathlib import Path
  import numpy
  import pytest
  from pack_dataset import pack_data_dir
  from parameterized import parameterized
  from save_len_file import save_len_file
  from torch.utils.data import DataLoader
  from transformers import AutoTokenizer
  from transformers.models.mbart.modeling_mbart import shift_tokens_right
  from transformers.testing_utils import TestCasePlus, slow
  from utils import FAIRSEQ_AVAILABLE, DistributedSortishSampler, LegacySeq2SeqDataset, Seq2SeqDataset
label_desc: Use image node_id 4 for calling the TestAll obj's underlying member method code with example usage: obj.test_dynamic_batch_size() without return types