import torch


def relaxed_allclose(x, y, atol=0.01, pct_wrong_allowable=0.1):
    """Comparing two Torch tensors using `allclose` repeatedly fails due to
    numerical issues. This test relaxes the constraint that every single element
    in the two tensors are similar. Instead, a percentage of elements must be
    similar.
    """
    res     = torch.isclose(x, y, atol=atol)
    n_wrong = res.numel() - res.sum()
    n_wrong_allowable = pct_wrong_allowable * res.numel()
    return n_wrong <= n_wrong_allowable