Yes, but you have to unsqueeze and swap shape, so the [B1, N] becomes [B1, N, 1] and [B2, N] becomes [1, N, B2]:
t1 = torch.randn((17,1024,1))
t2 = torch.randn((1,1024,512))
s1 = torch.nn.functional.cosine_similarity(t1,t2)
print(s1.shape)

gives
torch.Size([17, 512])