If you have access to a GPU may be you can use https://pytorch.org/docs/stable/generated/torch.nn.CosineSimilarity.html