def test_packing_with_dataloader(
    packing_ratio: Any,
    tiny_gpt2_tokenizer: PreTrainedTokenizerBase,
):
    """Tests that packing works with a dataloader."""
    reproducibility.seed_all(17)
    tokenizer = tiny_gpt2_tokenizer
    cfg = {
        'dataset': {
            'hf_name': 'tatsu-lab/alpaca',
            'split': 'train',
            'max_seq_len': 2048,
            'decoder_only_format': True,
            'allow_pad_trimming': False,
            'packing_ratio': packing_ratio,
            'shuffle': False,
        },
        'drop_last': False,
        # Need to test with 0 num_workers because the packing collator object
        # Gets copied per worker and we cannot check the waste for child processes.
        'num_workers': 0,
        'pin_memory': False,
        'prefetch_factor': None,
        'persistent_workers': False,
        'timeout': 0,
    }

    loader = build_finetuning_dataloader(
        **cfg,
        tokenizer=tokenizer,
        device_batch_size=6,
    ).dataloader

    assert isinstance(loader, DataLoader)
    assert isinstance(loader.collate_fn, LossGeneratingTokensCollatorWrapper)
    pack_collator = loader.collate_fn.base_collator
    assert isinstance(pack_collator, BinPackCollator)

    batch_ix = 0
    for _ in loader:
        batch_ix += 1
        if batch_ix >= 3:
            break

    padding = (1 - pack_collator.efficiency)
    if packing_ratio == 'auto':
        assert pack_collator.waste == approx(0)
        assert padding == approx(0.292019, rel=.01)
    else:
        assert pack_collator.waste == approx(0)
        assert padding == approx(0.873720, rel=.01)
Tests that packing works with a dataloader.
tests/data/test_packing.py