import torch


def renormalize(log_p, alpha):
    """
    Take the model-computed log probabilities over candidates (log_p), apply
    a smoothing parameter (alpha), and renormalize to a q distribution such
    that the probabilities of candidates sum to 1. Return the log of q.
    """
    alpha_log_p = alpha * log_p
    log_q = alpha_log_p - torch.logsumexp(alpha_log_p, dim=1).unsqueeze(1)
    return log_q