import collections


def collect_hierarchical_module_name(base_name, model, module_to_name=None):
    """
    Create a meaningful name of the module based on the module hierarchy

    Args:
        base_name: the base name
        model: the model
        module_to_name: where to store the module to name conversion

    Returns:
        a dictionary with mapping nn.Module to string
    """
    if module_to_name is None:
        module_to_name = collections.OrderedDict()

    module_to_name[model] = base_name
    for child_id, child in enumerate(model.children()):
        child_name = base_name + '/' + type(child).__name__ + f'_{child_id}'
        collect_hierarchical_module_name(child_name, child, module_to_name=module_to_name)

    return module_to_name