Skip to main content

GraphOptimizer

Struct GraphOptimizer 

Source
pub struct GraphOptimizer { /* private fields */ }
Expand description

Graph optimization manager

Implementations§

Source§

impl GraphOptimizer

Source

pub fn new() -> Self

Create a new graph optimizer with default passes

Source

pub fn empty() -> Self

Create an empty optimizer with no passes

Source

pub fn add_default_passes(&mut self)

Add default optimization passes

Source

pub fn for_eager_execution() -> Self

Build an optimizer using only the passes that are safe to run ahead of the eager Session executor.

This deliberately excludes:

  • OperationFusionPass: it rewrites nodes into fused operation names ("Dense", "AddReLU", "ConvBatchNormReLU", …) that the eager executor’s operation dispatcher does not implement. Running it here would silently turn a working graph (e.g. any MatMul immediately followed by Add) into a hard “operation not supported” error at execution time.
  • MemoryOptimizationPass and DevicePlacementOptimizationPass: both only annotate node attributes (lifetime hints, in-place markers, device placement metadata) that the eager executor never reads, so running them would add cost without any effect.

The remaining passes (constant folding, algebraic simplification, common subexpression elimination, strength reduction, scheduling, and dead code elimination) only ever produce operation names the executor already understands, or mutate a node’s inputs/op in place while preserving its id, so they are safe to run unconditionally.

Source

pub fn add_pass(&mut self, pass: Box<dyn OptimizationPass>)

Add an optimization pass

Source

pub fn set_max_iterations(&mut self, max_iterations: usize)

Set maximum number of optimization iterations

Source

pub fn pass_count(&self) -> usize

Get the number of optimization passes

Source

pub fn optimize(&self, graph: &mut Graph) -> Result<OptimizationStats>

Optimize a computation graph

Source

pub fn optimize_with_outputs( &self, graph: &mut Graph, outputs: &HashSet<NodeId>, ) -> Result<OptimizationStats>

Optimize a computation graph while protecting outputs from being eliminated or merged away.

outputs should contain every node id that must keep its identity after optimization — typically the fetch targets a caller (such as the eager Session executor) intends to read back by node id or name. Every pass is informed of this set via OptimizationPass::set_outputs before running; passes that never destroy a node’s identity ignore it, while dead code elimination, common subexpression elimination, and identity-removing algebraic / strength-reduction rewrites treat these ids as protected (skipping the rewrite for that node this iteration rather than applying it).

Trait Implementations§

Source§

impl Default for GraphOptimizer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

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

Source§

fn vzip(self) -> V