In python, when creating a tensor from a numpy array pytorch makes no copy.
At the moment we are making 2 copies when casting from R to a tensor.
The first one (inside R) to convert from the col-major representation to a row-major (used by libtorch).
The other when after creating the tensor inside cpp code, because R doesn't know that torch points to the data address when the R object is removed.
I think it should be possible to make at most one copy, but maybe with ALTREP we could make the casting zero copy.
In python, when creating a tensor from a numpy array pytorch makes no copy. At the moment we are making 2 copies when casting from R to a tensor.
The first one (inside R) to convert from the col-major representation to a row-major (used by libtorch). The other when after creating the tensor inside cpp code, because R doesn't know that torch points to the data address when the R object is removed.
I think it should be possible to make at most one copy, but maybe with ALTREP we could make the casting zero copy.
I would love some help on it!