Have you tried those 2 ways:

using index API, https://pytorch.org/cppdocs/notes/tensor_indexing.html 11


and

https://pytorch.org/cppdocs/notes/tensor_basics.html 9

CUDA accessors
global void packed_accessor_kernel(
PackedTensorAccessor64<float, 2> foo,
float* trace) {
int i=threadIdx.x
gpuAtomicAdd(trace, foo[i][i])
}
torch::Tensor foo = torch::rand({12, 12});
// assert foo is 2-dimensional and holds floats.
auto foo_a = foo.packed_accessor64<float,2>();
float trace = 0;
packed_accessor_kernel<<<1, 12>>>(foo_a, &trace);