Skip to main content

DimExpr

Enum DimExpr 

Source
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]]), 12);

Variants§

§

Const(usize)

A concrete dimension size.

§

InputDim

Axis size of the op’s input_idx-th input tensor.

Fields

§input_idx: usize
§axis: usize
§

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

Source

pub fn eval(&self, input_shapes: &[&[usize]]) -> usize

Evaluate the expression using actual input tensor shapes.

§Panics

Panics if an InputDim node references an input_idx that is out of bounds for input_shapes, or an axis that is out of bounds for the corresponding shape slice.

§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]]), 7);
Source

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));
Source

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 });
Source

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));
Source

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(&[]), 5);
Source

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(&[]), 5);
Source

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(&[]), 12);
Source

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(&[]), 4);
Source

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(&[]), 3);
Source

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(&[]), 5);
Source

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());
Source

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)]);
Source

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 },
    ]
);
Source

pub fn eval_all(exprs: &[Self], input_shapes: &[&[usize]]) -> Vec<usize>

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]]), vec![3, 4]);
Source

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 }]
);
Source

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§

Source§

impl Clone for DimExpr

Source§

fn clone(&self) -> DimExpr

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 DimExpr

Source§

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

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

impl From<&DimExpr> for DimExpr

Source§

fn from(value: &DimExpr) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for DimExpr

Source§

fn from(v: usize) -> Self

Converts to this type from the input type.
Source§

impl Hash for DimExpr

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for DimExpr

Source§

fn eq(&self, other: &DimExpr) -> 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 Eq for DimExpr

Source§

impl StructuralPartialEq for DimExpr

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
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

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
§

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

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

§

impl<T> MaybeSend for T

§

impl<T> MaybeSendSync for T

§

impl<T> MaybeSync for T