import torch


def _cosine_dist(x, y, norm=True):
    """
    Compute cosine distance betweeen features in x and y
    :param x: torch.FloatTensor shape: [M, feat_size]  query here
    :param y: torch.FloatTensor shape: [N, feat_size]  gallery here
    :return: distmat, shape: [M, N]
    """
    if norm:
        x = torch.nn.functional.normalize(x, dim=1, p=2)
        y = torch.nn.functional.normalize(y, dim=1, p=2)
    return 1. - torch.mm(x, y.t()).cpu().numpy()