In this case, your (1,1,512,1) shaped Tensor will copy itself to match the target dimension is (3,1,512,1), a technique known as Broadcasting.
You just need to write the code for Eucledian distance, Pytorch will perform Broadcasting inherently.
Here’s the code
A= torch.randn(1,1,512,1).reshape(-1,512)
B = torch.randn(3,1,512,1).reshape(-1,512)

Distance = ((A-B)**2).sum(axis=0)