def leaf_datasets(dataset: TraverseT, include_leaf_transforms: bool = True):
    """
    Obtains the leaf datasets of a Dataset.

    This is a low level utility. For most use cases, it is better to use
    :func:`single_flat_dataset` or :func:`flat_datasets_from_benchmark`.

    :param dataset: The dataset to traverse.
    :param include_leaf_transforms: If True, include the transformations
        found in the leaf dataset in the transforms list. Defaults to True.
    :return: A dictionary mapping each leaf dataset to a list of tuples.
        Each tuple contains two elements: the index and the transformation
        applied to that exemplar.
    """

    def leaf_selector(subset, indices, transforms):
        if isinstance(subset, (AvalancheDataset, FlatData, Subset, ConcatDataset)):
            # Returning None => continue traversing
            return None

        if indices is None:
            indices = range(len(subset))

        if include_leaf_transforms:
            leaf_transforms = _extract_transforms_from_standard_dataset(subset)

            if leaf_transforms is not None:
                transforms = list(transforms) + [leaf_transforms]

        return [(subset, idx, transforms) for idx in indices]

    def transform_selector(subset, transforms):
        if isinstance(subset, _FlatDataWithTransform):
            if subset._frozen_transform_groups is not None and not isinstance(
                subset._frozen_transform_groups, EmptyTransformGroups
            ):
                transforms = list(transforms) + [
                    subset._frozen_transform_groups[
                        subset._frozen_transform_groups.current_group
                    ]
                ]
            if subset._transform_groups is not None and not isinstance(
                subset._transform_groups, EmptyTransformGroups
            ):
                transforms = list(transforms) + [
                    subset._transform_groups[subset._transform_groups.current_group]
                ]

        return transforms

    leaves = _traverse_supported_dataset_with_intermediate(
        dataset,
        leaf_selector,
        intermediate_selector=transform_selector,
        intermediate=[],
    )

    leaves_dict: Dict[Any, List[Tuple[int, Any]]] = defaultdict(list)
    for leaf_dataset, idx, transform in leaves:
        transform_reversed = list(reversed(transform))
        leaves_dict[leaf_dataset].append((idx, transform_reversed))

    return leaves_dict

    Obtains the leaf datasets of a Dataset.

    This is a low level utility. For most use cases, it is better to use
    :func:`single_flat_dataset` or :func:`flat_datasets_from_benchmark`.

    :param dataset: The dataset to traverse.
    :param include_leaf_transforms: If True, include the transformations
        found in the leaf dataset in the transforms list. Defaults to True.
    :return: A dictionary mapping each leaf dataset to a list of tuples.
        Each tuple contains two elements: the index and the transformation
        applied to that exemplar.
    
avalanche/benchmarks/utils/dataset_traversal_utils.py