def test_batch_sampler_shuffle_setting():
    """Test whether the `shuffle` state is correctly set in the `BatchSampler`."""

    random_sampler = RandomSampler(range(10))
    seq_sampler = SequentialSampler(range(10))
    shuffled_dataloader = DataLoader(
        range(10), batch_sampler=BatchSampler(random_sampler, batch_size=2, drop_last=False)
    )
    sequential_dataloader = DataLoader(
        range(10), batch_sampler=BatchSampler(seq_sampler, batch_size=2, drop_last=False)
    )

    # if batch_size is 1, the pytorch init a default SequentialSampler and set BatchSampler to None
    single_dataloader = DataLoader(range(10), batch_sampler=BatchSampler(seq_sampler, batch_size=1, drop_last=False))
    assert _is_dataloader_shuffled(shuffled_dataloader)
    assert not _is_dataloader_shuffled(sequential_dataloader)
    assert not _is_dataloader_shuffled(single_dataloader)

    # if batch_size is 1, and no batch_sampler is set, the pytorch will set BatchSampler to None
    single_dataloader = DataLoader(range(10), batch_size=1)
    shuffled_single_dataloader = DataLoader(range(10), batch_size=1, shuffle=True)
    assert not _is_dataloader_shuffled(single_dataloader)
    assert _is_dataloader_shuffled(shuffled_single_dataloader)
Test whether the `shuffle` state is correctly set in the `BatchSampler`.
tests/tests_pytorch/utilities/test_data.py