SymTensor

Struct SymTensor 

Source
pub struct SymTensor(/* private fields */);
Expand description

Tensor with symbolic shape and elements.

This is a tensor where the elements and dimension sizes can be either concrete values or symbolic expressions. This type is used during shape inference to represent the shapes of operator inputs and outputs, as well as the values of operations which manipulate shapes.

The symbolic expressions can be integers, symbols with names and assumptions about their values or composite expressions (addition, multiplication etc.)

use rten_shape_inference::{SymTensor, SymExpr};

// Create a matrix with `nr` rows, `nc` columns and unknown values.
let nr = SymExpr::from("nr");
let nc = SymExpr::from("nc");
let matrix = SymTensor::from_shape(vec![nr.clone(), nc.clone()]);
assert_eq!(matrix.ndim(), Some(2));
assert_eq!(matrix.size(0), Some(nr.clone()));
assert_eq!(matrix.size(1), Some(nc.clone()));

// Turn the matrix's shape into a vector with values `["nr", "nc"]`.
let shape = SymTensor::from_vec(matrix.shape().unwrap().collect());
assert_eq!(shape.ndim(), Some(1));
assert_eq!(shape.values(), Some([nr.clone(), nc.clone()].as_slice()));

// Get the number of elements in the matrix as an expression `Some(nr * nc)`.
let len = shape.values().map(|v| v.iter().fold(
    SymExpr::Value(1),
    |prod, dim| prod * dim.clone()
).simplify());
assert_eq!(len, Some(SymExpr::Mul((nr.into(), nc.into()))));

Implementations§

Source§

impl SymTensor

Source

pub fn unknown(note: &'static str) -> Self

Create a new symbolic tensor with unknown shape and values.

note is a short string indicating the reason why the tensor shape and values are unknown. This is used for debugging purposes.

Source

pub fn from_shape(shape: Vec<SymExpr>) -> Self

Create a new symbolic tensor with the given shape and unknown values.

Source

pub fn from_fixed_shape(shape: &[usize]) -> Self

Create a new symbolic tensor with the given shape and unknown values.

Source

pub fn from_vec(vec: Vec<SymExpr>) -> Self

Create a new symbolic vector.

Source

pub fn from_scalar(item: SymExpr) -> Self

Create a new symbolic scalar.

Source

pub fn as_scalar(&self) -> Option<&SymExpr>

Return this tensor’s single element, if it is a scalar.

Source

pub fn as_vector(&self) -> Option<&[SymExpr]>

Return this tensor’s values as a slice, if it is a vector.

Source

pub fn to_constant(&self) -> Option<Constant>

Return this tensor’s fixed values, if it is a scalar or a vector and all values are fixed.

Source

pub fn ndim(&self) -> Option<usize>

Return the number of dimensions, if known.

Source

pub fn size(&self, index: usize) -> Option<SymExpr>

Return the size of the index’th dimension.

Returns None if the index is out of bounds or the tensor’s shape is unknown.

Source

pub fn shape(&self) -> Option<impl ExactSizeIterator<Item = SymExpr> + Clone>

Return an iterator over the dimensions or None if unknown.

Source

pub fn values(&self) -> Option<&[SymExpr]>

Return the symbolic values in this tensor, or None if unknown.

Source

pub fn simplify(self) -> Self

Simplify symbolic expressions in this tensor.

See SymExpr::simplify.

Trait Implementations§

Source§

impl Clone for SymTensor

Source§

fn clone(&self) -> SymTensor

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SymTensor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for SymTensor

Source§

fn eq(&self, other: &SymTensor) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for SymTensor

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.