Solved by richard in post #2 
              
              
                You can do this via a combination of taking a (batch) diagonal and then summing each diagonal. 
So: 
B, N = 2, 3
x = torch.randn(B, N, N)
x.diagonal(offset=0, dim1=-1, dim2=-2).sum(-1)

If you’re on a nightly build of PyTorch, this can be accomplished in one shot via torch.vmap. vmap essentially “ad…