I’m not sure how TF broadcasts the matrices internally, but I guess it’s creating a new dim4 for the first tensor?
If that’s the case, the second approach should work:
B, H, W = 2, 5, 5

a = torch.randn(B, 3, H, W)
b = torch.randn(B, 3, 3, H, W)

out1 = torch.matmul(a.unsqueeze(1), b)
out2 = torch.matmul(a.unsqueeze(2), b)

Could you store the results from the TF operation and compare it to the PyTorch result?