If you have a 3D tensor where the first dimension is the batch dimension the following will work
B=1000 #batch size
A=16    #nrows/ncols
matrices = torch.randn(B,A,A) #batch of matrices
trace = matrices.diagonal(offset=0, dim1=-2, dim2=-1).sum(dim=-1) #grab diagonal along last 2 dims, and sum