def flat_datasets_from_benchmark(
    benchmark: CLScenario, include_leaf_transforms: bool = True
):
    """
    Obtain a list of flattened datasets from a benchmark.

    In practice, this function will traverse all the
    datasets in the benchmark to find the leaf datasets.
    A dataset can be traversed and flattened to (one or more) leaf
    dataset(s) if all subset and dataset concatenations point to a
    single leaf dataset and if transformations are the same across
    all paths.

    Traversing the dataset means traversing :class:`AvalancheDataset`
    as well as PyTorch :class:`Subset` and :class:`ConcatDataset` to
    obtain the leaf datasets, the indices, and the transformations chain.

    Note: this means that datasets will be plain PyTorch datasets,
    not :class:`AvalancheDataset` (Avalanche datasets are traversed).

    In common benchmarks, this returns one dataset for the train
    and one dataset for test.

    :param benchmark: The benchmark to traverse.
    :param include_leaf_transforms: If True, include the transformations
        found in the leaf dataset in the transforms list. Defaults to True.
    :return: The list of leaf datasets. Each element in the list is
        a tuple `(dataset, indices, transforms)`.
    """
    single_datasets = dataset_list_from_benchmark(benchmark)
    leaves = leaf_datasets(
        AvalancheDataset(single_datasets),
        include_leaf_transforms=include_leaf_transforms,
    )

    result = []
    for dataset, indices_and_transforms in leaves.items():
        # Check that all transforms are the same
        first_transform = indices_and_transforms[0][1]
        same_transforms = all([first_transform == t for _, t in indices_and_transforms])

        if not same_transforms:
            for indices, transforms in indices_and_transforms:
                result.append((dataset, indices, transforms))
            continue

        flat_indices = [i for i, _ in indices_and_transforms]

        result.append((dataset, flat_indices, first_transform))
    return result

    Obtain a list of flattened datasets from a benchmark.

    In practice, this function will traverse all the
    datasets in the benchmark to find the leaf datasets.
    A dataset can be traversed and flattened to (one or more) leaf
    dataset(s) if all subset and dataset concatenations point to a
    single leaf dataset and if transformations are the same across
    all paths.

    Traversing the dataset means traversing :class:`AvalancheDataset`
    as well as PyTorch :class:`Subset` and :class:`ConcatDataset` to
    obtain the leaf datasets, the indices, and the transformations chain.

    Note: this means that datasets will be plain PyTorch datasets,
    not :class:`AvalancheDataset` (Avalanche datasets are traversed).

    In common benchmarks, this returns one dataset for the train
    and one dataset for test.

    :param benchmark: The benchmark to traverse.
    :param include_leaf_transforms: If True, include the transformations
        found in the leaf dataset in the transforms list. Defaults to True.
    :return: The list of leaf datasets. Each element in the list is
        a tuple `(dataset, indices, transforms)`.
    
avalanche/benchmarks/utils/dataset_traversal_utils.py