Note that this only works for contiguous tensors, so you want to either TORCH_CHECK is_contiguous or call auto tc = t.contiguous(), but in the latter case you cannot reliably modify t anymore.
As a convenient (but not terribly efficient for CPU because you likely won’t be using SIMD with it) method to index an arbitrarily strided tensor, you can use the array-like interface of t.accessor<...>(), but that requires you to know the number of dimensions.
As an alternative, you can do the index calcs yourself using stride and storage_offset.
Best regards
Thomas