YEs, you can by the way below.
Assume the shape of x1 and x2 is  [B1, N] and  [B2, N], then cosine similarity is computed below.
x1_norm = F.normalize(x1, p=2, dim=1)
x2_norm = F.normalize(x2, p=2, dim=1)
cosine similarity = x1_norm * x2_norm.t()