import torch


def logsumexpvec(x):
    """The same as "logsumexp" but calculates result for each row separately."""

    mx = torch.max(x, 1).values
    lset = torch.logsumexp(torch.t(x) - mx, 0)
    return torch.t(lset + mx)