rten icon indicating copy to clipboard operation
rten copied to clipboard

Document / simplify conversion to/from ndarray arrays (and other libraries)

Open robertknight opened this issue 2 years ago • 1 comments

To make using this library easier in downstream projects, it would help to have more documentation / examples etc. around converting to/from tensors/arrays in other libraries.

In the meantime, here are some useful resources:

  • I put together a quick gist for rten-ndarray conversion
  • There is a simple ImageNet image classification project at https://github.com/robertknight/rten-ndarray-demo/.

Cheatsheet

For the case where the tensor/array is "contiguous" (RTen terminology) or "standard layout" (ndarray terminology):

ndarray -> RTen:

TensorView::from_data(array.shape(), array.as_slice().unwrap());

RTen -> ndarray:

ArrayViewD::from_shape(tensor.shape(), tensor.data().unwrap()).unwrap()

The unwraps above will fail if the layout is not contiguous.

Outputs from RTen model execution are always contiguous / in standard order. If it is not certain that a tensor is in standard order, then Tensor::to_contiguous (RTen) or ArrayView::as_standard_layout can be used to get a contiguous layout tensor, copying only if necessary.

robertknight avatar Jan 07 '24 10:01 robertknight

https://github.com/robertknight/rten/pull/1025 adds a couple of new APIs to simplify input creation and output extraction.

robertknight avatar Nov 06 '25 20:11 robertknight