pub enum DimExpr {
Const(usize),
InputDim {
input_idx: usize,
axis: usize,
},
Add(Box<DimExpr>, Box<DimExpr>),
Sub(Box<DimExpr>, Box<DimExpr>),
Mul(Box<DimExpr>, Box<DimExpr>),
FloorDiv(Box<DimExpr>, Box<DimExpr>),
Min(Box<DimExpr>, Box<DimExpr>),
Max(Box<DimExpr>, Box<DimExpr>),
}Expand description
Arithmetic expression over tensor dimension sizes.
Evaluated at execution time from actual input tensor shapes.
InputDim { input_idx, axis } references the axis size of
the op’s input_idx-th input tensor.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let expr = DimExpr::mul(
DimExpr::InputDim {
input_idx: 0,
axis: 0,
},
DimExpr::InputDim {
input_idx: 0,
axis: 1,
},
);
assert_eq!(expr.eval(&[&[3, 4]]).unwrap(), 12);Variants§
Const(usize)
A concrete dimension size.
InputDim
Axis size of the op’s input_idx-th input tensor.
Add(Box<DimExpr>, Box<DimExpr>)
Sum of two dimension expressions.
Sub(Box<DimExpr>, Box<DimExpr>)
Difference of two dimension expressions.
Mul(Box<DimExpr>, Box<DimExpr>)
Product of two dimension expressions.
FloorDiv(Box<DimExpr>, Box<DimExpr>)
Floor division of two dimension expressions.
Min(Box<DimExpr>, Box<DimExpr>)
Minimum of two dimension expressions.
Max(Box<DimExpr>, Box<DimExpr>)
Maximum of two dimension expressions.
Implementations§
Source§impl DimExpr
impl DimExpr
Sourcepub fn eval(&self, input_shapes: &[&[usize]]) -> Result<usize, DimExprEvalError>
pub fn eval(&self, input_shapes: &[&[usize]]) -> Result<usize, DimExprEvalError>
Evaluate the expression using actual input tensor shapes.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let expr = DimExpr::add(
DimExpr::InputDim {
input_idx: 0,
axis: 0,
},
DimExpr::Const(2),
);
assert_eq!(expr.eval(&[&[5, 7]]).unwrap(), 7);§Errors
Returns DimExprEvalError when an input or axis is unavailable, an
arithmetic operation overflows, subtraction underflows, or division
would use zero as its divisor.
Sourcepub fn max_input_idx(&self) -> Option<usize>
pub fn max_input_idx(&self) -> Option<usize>
Return the maximum referenced input_idx, or None if the expression
contains only constants.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let expr = DimExpr::add(
DimExpr::InputDim {
input_idx: 0,
axis: 0,
},
DimExpr::InputDim {
input_idx: 2,
axis: 1,
},
);
assert_eq!(expr.max_input_idx(), Some(2));Sourcepub fn remap(&self, from: usize, to: usize) -> Self
pub fn remap(&self, from: usize, to: usize) -> Self
Remap InputDim { input_idx: from, .. } to InputDim { input_idx: to, .. }.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let expr = DimExpr::InputDim {
input_idx: 0,
axis: 1,
};
assert_eq!(expr.remap(0, 2), DimExpr::InputDim { input_idx: 2, axis: 1 });Sourcepub fn constant(v: usize) -> Self
pub fn constant(v: usize) -> Self
Construct a constant dimension expression.
§Examples
use tenferro_ops::dim_expr::DimExpr;
assert_eq!(DimExpr::constant(4), DimExpr::Const(4));Sourcepub fn add(a: Self, b: Self) -> Self
pub fn add(a: Self, b: Self) -> Self
Construct an addition node.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let expr = DimExpr::add(DimExpr::Const(2), DimExpr::Const(3));
assert_eq!(expr.eval(&[]).unwrap(), 5);Sourcepub fn sub(a: Self, b: Self) -> Self
pub fn sub(a: Self, b: Self) -> Self
Construct a subtraction node.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let expr = DimExpr::sub(DimExpr::Const(7), DimExpr::Const(2));
assert_eq!(expr.eval(&[]).unwrap(), 5);Sourcepub fn mul(a: Self, b: Self) -> Self
pub fn mul(a: Self, b: Self) -> Self
Construct a multiplication node.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let expr = DimExpr::mul(DimExpr::Const(3), DimExpr::Const(4));
assert_eq!(expr.eval(&[]).unwrap(), 12);Sourcepub fn floor_div(a: Self, b: Self) -> Self
pub fn floor_div(a: Self, b: Self) -> Self
Construct a floor-division node.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let expr = DimExpr::floor_div(DimExpr::Const(9), DimExpr::Const(2));
assert_eq!(expr.eval(&[]).unwrap(), 4);Sourcepub fn min(a: Self, b: Self) -> Self
pub fn min(a: Self, b: Self) -> Self
Construct a minimum node.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let expr = DimExpr::min(DimExpr::Const(3), DimExpr::Const(5));
assert_eq!(expr.eval(&[]).unwrap(), 3);Sourcepub fn max(a: Self, b: Self) -> Self
pub fn max(a: Self, b: Self) -> Self
Construct a maximum node.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let expr = DimExpr::max(DimExpr::Const(3), DimExpr::Const(5));
assert_eq!(expr.eval(&[]).unwrap(), 5);Sourcepub fn is_const(&self) -> bool
pub fn is_const(&self) -> bool
Return true when this expression is a constant.
§Examples
use tenferro_ops::dim_expr::DimExpr;
assert!(DimExpr::Const(3).is_const());Sourcepub fn from_concrete(shape: &[usize]) -> Vec<Self>
pub fn from_concrete(shape: &[usize]) -> Vec<Self>
Convert a concrete shape to constant expressions.
§Examples
use tenferro_ops::dim_expr::DimExpr;
assert_eq!(DimExpr::from_concrete(&[2, 3]), vec![DimExpr::Const(2), DimExpr::Const(3)]);Sourcepub fn input_shape(input_idx: usize, rank: usize) -> Vec<Self>
pub fn input_shape(input_idx: usize, rank: usize) -> Vec<Self>
Build [InputDim(input_idx, 0), ..., InputDim(input_idx, rank - 1)].
§Examples
use tenferro_ops::dim_expr::DimExpr;
let shape = DimExpr::input_shape(1, 2);
assert_eq!(
shape,
vec![
DimExpr::InputDim { input_idx: 1, axis: 0 },
DimExpr::InputDim { input_idx: 1, axis: 1 },
]
);Sourcepub fn eval_all(
exprs: &[Self],
input_shapes: &[&[usize]],
) -> Result<Vec<usize>, DimExprEvalError>
pub fn eval_all( exprs: &[Self], input_shapes: &[&[usize]], ) -> Result<Vec<usize>, DimExprEvalError>
Evaluate a slice of expressions against actual input shapes.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let exprs = vec![
DimExpr::InputDim { input_idx: 0, axis: 0 },
DimExpr::Const(4),
];
assert_eq!(DimExpr::eval_all(&exprs, &[&[3, 5]]).unwrap(), vec![3, 4]);§Errors
Returns the first DimExprEvalError encountered while evaluating an
expression.
Sourcepub fn remap_all(exprs: &[Self], from: usize, to: usize) -> Vec<Self>
pub fn remap_all(exprs: &[Self], from: usize, to: usize) -> Vec<Self>
Remap all InputDim references in a slice of expressions.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let exprs = vec![DimExpr::InputDim { input_idx: 0, axis: 0 }];
assert_eq!(
DimExpr::remap_all(&exprs, 0, 1),
vec![DimExpr::InputDim { input_idx: 1, axis: 0 }]
);Sourcepub fn max_input_idx_all(exprs: &[Self]) -> Option<usize>
pub fn max_input_idx_all(exprs: &[Self]) -> Option<usize>
Compute the maximum referenced input_idx across a slice.
§Examples
use tenferro_ops::dim_expr::DimExpr;
let exprs = vec![
DimExpr::InputDim { input_idx: 0, axis: 0 },
DimExpr::InputDim { input_idx: 2, axis: 1 },
];
assert_eq!(DimExpr::max_input_idx_all(&exprs), Some(2));Trait Implementations§
impl Eq for DimExpr
impl StructuralPartialEq for DimExpr
Auto Trait Implementations§
impl Freeze for DimExpr
impl RefUnwindSafe for DimExpr
impl Send for DimExpr
impl Sync for DimExpr
impl Unpin for DimExpr
impl UnsafeUnpin for DimExpr
impl UnwindSafe for DimExpr
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