import copy
import logging


def transfer_verification(model_state_dict, partial_state_dict, modules):
    """Verify tuples (key, shape) for input model modules match specified modules.

    Args:
        model_state_dict (OrderedDict): the initial model state_dict
        partial_state_dict (OrderedDict): the trained model state_dict
        modules (list): specified module list for transfer

    Return:
        (boolean): allow transfer

    """
    partial_modules = []
    partial_state_dict_org = copy.deepcopy(partial_state_dict)
    for key_p, value_p in partial_state_dict_org.items():
        if any(key_p.startswith(m) for m in modules):
            if key_p in list(model_state_dict.keys()):
                if value_p.shape == model_state_dict[key_p].shape:
                    partial_modules += [(key_p, value_p.shape)]
            else:
                logging.info(f'Removing {key_p}...')
                del partial_state_dict[key_p]
    return len(partial_modules) > 0, partial_state_dict