import torch


def get_multimodal_similarity(attention, n, eps=1e-08):
    """
    Returns
        1
        - * sum max A_{j,z}
        N    j   z

    Note: attention tensor should be already masked

    :param attention: A [*, d2, d1] tensor
    :param n: A [*] tensor
    :param eps: A float value
    :return: A [*] tensor
    """
    attention, _ = torch.max(attention, dim=-1)  # [*, d2]
    attention = torch.sum(attention, dim=-1)     # [*]

    return attention / (n + eps)