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
impl SymTensor
Sourcepub fn unknown(note: &'static str) -> Self
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.
Sourcepub fn from_shape(shape: Vec<SymExpr>) -> Self
pub fn from_shape(shape: Vec<SymExpr>) -> Self
Create a new symbolic tensor with the given shape and unknown values.
Sourcepub fn from_fixed_shape(shape: &[usize]) -> Self
pub fn from_fixed_shape(shape: &[usize]) -> Self
Create a new symbolic tensor with the given shape and unknown values.
Sourcepub fn from_scalar(item: SymExpr) -> Self
pub fn from_scalar(item: SymExpr) -> Self
Create a new symbolic scalar.
Sourcepub fn as_scalar(&self) -> Option<&SymExpr>
pub fn as_scalar(&self) -> Option<&SymExpr>
Return this tensor’s single element, if it is a scalar.
Sourcepub fn as_vector(&self) -> Option<&[SymExpr]>
pub fn as_vector(&self) -> Option<&[SymExpr]>
Return this tensor’s values as a slice, if it is a vector.
Sourcepub fn to_constant(&self) -> Option<Constant>
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.
Sourcepub fn size(&self, index: usize) -> Option<SymExpr>
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.
Sourcepub fn shape(&self) -> Option<impl ExactSizeIterator<Item = SymExpr> + Clone>
pub fn shape(&self) -> Option<impl ExactSizeIterator<Item = SymExpr> + Clone>
Return an iterator over the dimensions or None if unknown.
Sourcepub fn values(&self) -> Option<&[SymExpr]>
pub fn values(&self) -> Option<&[SymExpr]>
Return the symbolic values in this tensor, or None if unknown.
Sourcepub fn simplify(self) -> Self
pub fn simplify(self) -> Self
Simplify symbolic expressions in this tensor.
See SymExpr::simplify.
Trait Implementations§
impl StructuralPartialEq for SymTensor
Auto Trait Implementations§
impl Freeze for SymTensor
impl RefUnwindSafe for SymTensor
impl !Send for SymTensor
impl !Sync for SymTensor
impl Unpin for SymTensor
impl UnwindSafe for SymTensor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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