Loss between unordered matrices

What is a good loss function between a pair of two matrices that row i in the target matrix does not necessarily correspond to row i in the trained matrix? More specifically, I’m looking to minimize the sum of all errors between target and trained tensors, where row i in the target corresponds to row j in the trained tensor where the sum of all “distances” between (i,j) is minimal, and if j is already matched with i, then it cannot match another k in the target matrix as well (“without replacement”).

Is there a built-in function for this in torch?
I’ve implemented maximum mean discrepancy (MMD) distance metric, but it’s not giving me satisfactory results.