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 38. vmap essentially “adds a batch dimension to your code”:
B, N = 2, 3
x = torch.randn(B, N, N)
batch_trace = torch.vmap(torch.trace)(x)