import torch


def numerically_stable_exp(tensor, dim=-1):
    """ Removes largest value and exponentiates, returning both.

    :param tensor: the input tensor
    :param dim: which is the dim to find max over
    :returns: exp(tensor - max), max
    :rtype: (torch.Tensor, torch.Tensor)

    """
    max_value, _ = torch.max(tensor, dim=dim)
    return torch.exp(tensor - max_value.unsqueeze(dim)), max_value