A = torch.rand(2, 3, 4, 4)
B = torch.rand(2, 4, 4)
C = (A * B.unsqueeze(1)).sum(1, keepdims=True)

A = torch.rand(2, 3, 4, 4)
B = torch.rand(3)
C = (A, B[None, :, None, None]).sum(1, keepdims=True)

A = torch.rand(2, 3, 4, 4)
B = torch.rand(2, 3, 4, 4)
C = (A * B).sum(1, keepdims=True)