It does not seem that it is giving the correct result.
> C = torch.zeros(batch_size, N,M,P)
> for i in range(batch_size):
>     for j in range(N):
>         C[i,j] = torch.mm(A[i,j],B[i].transpose(0,1))
> is_same = C==torch.einsum('bnmd,bpd -> bnmp', [A, B])

is_same is not all 1’s.