Solved by isalirezag in post #2 
              
              
                The answer is something like this: 
A_1 = torch.unsqueeze(A,1)
A_2 = torch.unsqueeze(A,2)
D = torch.max(A_1-A_2,dim = 2)[0]