def test_combined_loader_modes_for_namedtuple():
    """Test `CombinedLoaderIterator` given a namedtuple of iterables."""

    class IterablesNamedTuple(NamedTuple):
        a: Any
        b: Any

    iterables = IterablesNamedTuple(
        a=torch.utils.data.DataLoader(range(10), batch_size=4), b=torch.utils.data.DataLoader(range(20), batch_size=5)
    )
    lengths = [len(v) for v in iterables]

    # min_size with namedtuple
    min_len = min(lengths)
    combined_loader = CombinedLoader(iterables, "min_size")
    iter(combined_loader)
    assert len(combined_loader) == min_len
    for item, idx, _ in combined_loader:
        assert isinstance(combined_loader._iterator, _MinSize)
        assert isinstance(item, IterablesNamedTuple)
    assert idx == min_len - 1
    assert idx == len(combined_loader) - 1

    # max_size_cycle with namedtuple
    max_len = max(lengths)
    combined_loader = CombinedLoader(iterables, "max_size_cycle")
    iter(combined_loader)
    assert len(combined_loader) == max_len
    for item, idx, _ in combined_loader:
        assert isinstance(combined_loader._iterator, _MaxSizeCycle)
        assert isinstance(item, IterablesNamedTuple)
    assert idx == max_len - 1
    assert idx == len(combined_loader) - 1

    # max_size with namedtuple
    combined_loader = CombinedLoader(iterables, "max_size")
    iter(combined_loader)
    assert len(combined_loader) == max_len
    for item, idx, _ in combined_loader:
        assert isinstance(combined_loader._iterator, _MaxSize)
        assert isinstance(item, IterablesNamedTuple)
        are_nones = [x is None for x in item]
        should_be_nones = [idx >= length for length in lengths]
        assert are_nones == should_be_nones
    assert idx == max_len - 1
    assert idx == len(combined_loader) - 1

    # sequential with namedtuple
    sum_len = sum(lengths)
    combined_loader = CombinedLoader(iterables, "sequential")
    iter(combined_loader)
    assert combined_loader._iterator is not None
    assert len(combined_loader) == sum_len
    for total_idx, (item, batch_idx, dataloader_idx) in enumerate(combined_loader):
        assert isinstance(combined_loader._iterator, _Sequential)
        assert isinstance(batch_idx, int)
        assert isinstance(item, Tensor)
    assert idx == lengths[-1] - 1
    assert total_idx == sum_len - 1
    assert total_idx == len(combined_loader) - 1
    assert dataloader_idx == len(iterables) - 1
Test `CombinedLoaderIterator` given a namedtuple of iterables.
tests/tests_pytorch/utilities/test_combined_loader.py