Indexing CUDA tensors in LibTorch inside functions

Inside a function called from main(), I am trying to iterate over the elements of a CUDA LibTorch tensor in CPP.

I fail with both accessor<double, 3>() and packed_accessor<double, 3>(), similarly with packed_accessor64<double, 3>().

The accessor is created fine, but when I try to index any element from the underlying tensor, for example:

auto Psi_spec_abs_acc = Psi_spec_abs.packed_accessor<double, 3>();
std::cout << Psi_spec_abs_acc [0][0][0] << std::endl;

It fails with segfault.

How could I iterate over a CUDA tensor without putting it into the CPU?

Thank you!