import torch


def contains_nan(x, max_value=float('inf')):
    """Check whether a tensor contains a NaN or an infinity value.

    Args:
        x (torch.Tensor or list): The input.
        max_value (float): The highest acceptable value.

    Returns:
        (bool): Whether the tensor contains a NaN or infinity.
    """
    nb_nans = 0
    nb_infs = 0
    if isinstance(x, list):
        for el in x:
            nb_nans += torch.isnan(el).sum()
            nb_infs += (torch.abs(el) > float('inf')).sum()
    else:
        nb_nans = torch.isnan(x).sum()
        nb_infs = (torch.abs(x) > float('inf')).sum()
    
    return nb_nans > 0 or nb_infs > 0