import torch


def format_hparam_dict_for_tb(d, super_key=''):
    """
    tensorboard hparam logger needs a dict with all values as ints, floats, or strings.
    This function flattens a dict into that format.
    """
    hparam_dict = {}
    for k, v in d.items():
        k = super_key + '_' + k
        if isinstance(v, dict):
            hparam_dict.update(format_hparam_dict_for_tb(v, super_key=k))
        elif not isinstance(v, (int, float, str, bool, torch.Tensor)):
            hparam_dict[k] = str(v)
        else:
            hparam_dict[k] = v
    return hparam_dict