Type Alias ort::Tensor

source ·
pub type Tensor<T> = Value<TensorValueType<T>>;

Aliased Type§

struct Tensor<T> { /* private fields */ }

Implementations§

source§

impl<T: IntoTensorElementType + Debug> Tensor<T>

source

pub fn new(allocator: &Allocator, shape: impl ToDimensions) -> Result<Tensor<T>>

Construct a tensor Value in a given allocator with a given shape and datatype. The data contained in the value will be zero-allocated on the allocation device.

This can be used to create a tensor with data on a certain device. For example, to create a tensor with pinned (CPU) memory for use with CUDA:

let allocator = Allocator::new(
	&session,
	MemoryInfo::new(AllocationDevice::CUDAPinned, 0, AllocatorType::Device, MemoryType::CPUInput)?
)?;

let mut img_input = Tensor::<f32>::new(&allocator, [1, 128, 128, 3])?;
source

pub fn from_array(input: impl IntoValueTensor<Item = T>) -> Result<Tensor<T>>

Construct a tensor Value from an array of data.

Tensor Values can be created from:

  • (with feature ndarray) a shared reference to a ndarray::CowArray (&CowArray<'_, T, D>);
  • (with feature ndarray) a mutable/exclusive reference to an ndarray::ArcArray (&mut ArcArray<T, D>);
  • (with feature ndarray) an owned ndarray::Array;
  • (with feature ndarray) a borrowed view of another array, as an ndarray::ArrayView (ArrayView<'_, T, D>);
  • a tuple of (dimensions, data) where:
    • dimensions is one of Vec<I>, [I] or &[I], where I is i64 or usize;
    • and data is one of Vec<T>, Box<[T]>, Arc<Box<[T]>>, or &[T].
// Create a tensor from a raw data vector
let value = Value::from_array(([1usize, 2, 3], vec![1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0].into_boxed_slice()))?;

// Create a tensor from an `ndarray::Array`
#[cfg(feature = "ndarray")]
let value = Value::from_array(ndarray::Array4::<f32>::zeros((1, 16, 16, 3)))?;

Creating string tensors requires a separate method; see Value::from_string_array.

Note that data provided in an ndarray may be copied in some circumstances:

  • &CowArray<'_, T, D> will always be copied regardless of whether it is uniquely owned or borrowed.
  • &mut ArcArray<T, D> and Array<T, D> will be copied only if the data is not in a contiguous layout (which is the case after most reshape operations)
  • ArrayView<'_, T, D> will always be copied.

Raw data provided as a Arc<Box<[T]>>, Box<[T]>, or Vec<T> will never be copied. Raw data is expected to be in standard, contigous layout.

source§

impl<T: IntoTensorElementType + Debug> Tensor<T>

source

pub fn extract_tensor(&self) -> ArrayViewD<'_, T>

Available on crate feature ndarray only.

Extracts the underlying data into a read-only ndarray::ArrayView.

let array = ndarray::Array4::<f32>::ones((1, 16, 16, 3));
let tensor = Tensor::from_array(array.view())?;

let extracted = tensor.extract_tensor();
assert_eq!(array.into_dyn(), extracted);
source

pub fn extract_tensor_mut(&mut self) -> ArrayViewMutD<'_, T>

Available on crate feature ndarray only.

Extracts the underlying data into a mutable ndarray::ArrayViewMut.

let array = ndarray::Array4::<f32>::ones((1, 16, 16, 3));
let mut tensor = Tensor::from_array(array.view())?;

let mut extracted = tensor.extract_tensor_mut();
extracted[[0, 0, 0, 1]] = 0.0;

let mut array = array.into_dyn();
assert_ne!(array, extracted);
array[[0, 0, 0, 1]] = 0.0;
assert_eq!(array, extracted);
source

pub fn extract_raw_tensor(&self) -> (Vec<i64>, &[T])

Extracts the underlying data into a “raw” view tuple, consisting of the tensor’s dimensions and an immutable view into its data.

let array = vec![1_i64, 2, 3, 4, 5];
let tensor = Tensor::from_array(([array.len()], array.clone().into_boxed_slice()))?;

let (extracted_shape, extracted_data) = tensor.extract_raw_tensor();
assert_eq!(extracted_data, &array);
assert_eq!(extracted_shape, [5]);
source

pub fn extract_raw_tensor_mut(&mut self) -> (Vec<i64>, &mut [T])

Extracts the underlying data into a “raw” view tuple, consisting of the tensor’s dimensions and a mutable view into its data.

let array = vec![1_i64, 2, 3, 4, 5];
let tensor = Tensor::from_array(([array.len()], array.clone().into_boxed_slice()))?;

let (extracted_shape, extracted_data) = tensor.extract_raw_tensor();
assert_eq!(extracted_data, &array);
assert_eq!(extracted_shape, [5]);
source§

impl<T: IntoTensorElementType + Debug> Tensor<T>

source

pub fn upcast(self) -> DynTensor

Converts from a strongly-typed Tensor<T> to a type-erased DynTensor.

source

pub fn upcast_ref(&self) -> DynTensorRef<'_>

Converts from a strongly-typed Tensor<T> to a reference to a type-erased DynTensor.

source

pub fn upcast_mut(&mut self) -> DynTensorRefMut<'_>

Converts from a strongly-typed Tensor<T> to a mutable reference to a type-erased DynTensor.

Trait Implementations§

source§

impl<T: IntoTensorElementType + Clone + Debug, const N: usize> Index<[i64; N]> for Tensor<T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: [i64; N]) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T: IntoTensorElementType + Clone + Debug, const N: usize> IndexMut<[i64; N]> for Tensor<T>

source§

fn index_mut(&mut self, index: [i64; N]) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<'i, 'v, T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<&'i ArrayBase<CowRepr<'v, T>, D>> for Tensor<T>
where 'i: 'v,

Available on crate feature ndarray only.
§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(arr: &'i CowArray<'v, T, D>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<&mut ArrayBase<OwnedArcRepr<T>, D>> for Tensor<T>

Available on crate feature ndarray only.
§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: &mut ArcArray<T, D>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, &[T])> for Tensor<T>

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: (I, &[T])) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, Arc<Box<[T]>>)> for Tensor<T>

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: (I, Arc<Box<[T]>>)) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, Box<[T]>)> for Tensor<T>

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: (I, Box<[T]>)) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<(I, Vec<T>)> for Tensor<T>

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: (I, Vec<T>)) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<ArrayBase<OwnedRepr<T>, D>> for Tensor<T>

Available on crate feature ndarray only.
§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: Array<T, D>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'v, T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<ArrayBase<ViewRepr<&'v T>, D>> for Tensor<T>

Available on crate feature ndarray only.
§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(arr: ArrayView<'v, T, D>) -> Result<Self, Self::Error>

Performs the conversion.