You are confusing the shapes with the values in a tensor.
The first example won’t work, as the shapes are invalid for a matrix multipliciation:
A = torch.randn([32, 32, 5, 15])
B = torch.randn([32, 32, 5, 15])

C = torch.matmul(A, B)
> RuntimeError: Expected batch2_sizes[0] == bs && batch2_sizes[1] == contraction_size to be true, but got false.

So you would have to permute one of the tensors depending on the expected output shape:
C = torch.matmul(A.permute(0, 1, 3, 2), B)
print(C.shape)
> torch.Size([32, 32, 15, 15])

# or
C = torch.matmul(A, B.permute(0, 1, 3, 2))
print(C.shape)
> torch.Size([32, 32, 5, 5])

Your second example was using just 4 values and is thus returning a scalar.