import torch


def cosine_similarity(x1, x2):
  """Calculates cosine similarity of two tensor."""
  dist = torch.sum(torch.multiply(x1, x2), dim=-1)
  dist = dist / (torch.linalg.norm(x1, dim=-1) * torch.linalg.norm(x2, dim=-1))
  return dist