Skip to main content

Graph

Struct Graph 

Source
pub struct Graph {
    pub nodes: Vec<GraphNode>,
    pub names: BTreeMap<usize, String>,
}
Expand description

The traced computational graph: a list of GraphNodes plus optional node names.

Fields§

§nodes: Vec<GraphNode>

The graph’s nodes, in the order they were recorded.

§names: BTreeMap<usize, String>

Node index → dotted name captured from [crate::name_scope] at recording time.

Implementations§

Source§

impl Graph

Source

pub fn new() -> Self

Creates an empty graph.

Source

pub fn add_node( &mut self, op: Op, inputs: Vec<usize>, dtype: DtypeRepr, shape: Shape, ) -> usize

Appends a new node to the graph, returning its index.

Source

pub fn topological_sort(&self) -> Vec<usize>

Returns node indices in topological order (producers before consumers) using Kahn’s algorithm. Panics if the graph contains a cycle.

Source

pub fn optimise(&self) -> Graph

Rewrite the graph by fusing compatible op sequences into single fused ops.

Currently recognises: Conv2d(no bias) → BatchNorm2d → Silu and replaces the triple with a single Conv2dBnSilu node.

Nodes that are absorbed into a fused node are removed from the output graph; remaining node indices are renumbered contiguously.

This does not include Graph::fuse_elementwise_chains — that pass produces Op::Fused nodes, and folding it in here would silently change what every existing optimise() caller lowers to. Call it separately once the target lowering backend actually knows how to compile Op::Fused.

Source

pub fn fuse_elementwise_chains(&self) -> Graph

Rewrite the graph by fusing adjacent, single-input/single-output elementwise chains (see is_fusable_elementwise) into Op::Fused nodes.

Separate from Graph::optimise on purpose: producing Op::Fused only helps once a lowering backend knows how to compile it (concatenate each member’s kernel source and synthesize an entry point that runs them in sequence — see the Op::Fused doc comment). Call this explicitly once that backend support exists; don’t fold it into optimise(), which every existing caller already depends on producing today’s set of ops.

Iterates Graph::fuse_elementwise_chain_pass to a fixed point: each call only merges one adjacent pair, so a chain longer than two nodes grows by one member per iteration until nothing more merges.

Trait Implementations§

Source§

impl Clone for Graph

Source§

fn clone(&self) -> Graph

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Graph

Source§

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

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

impl Default for Graph

Source§

fn default() -> Graph

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

Auto Trait Implementations§

§

impl Freeze for Graph

§

impl !RefUnwindSafe for Graph

§

impl Send for Graph

§

impl Sync for Graph

§

impl Unpin for Graph

§

impl UnsafeUnpin for Graph

§

impl !UnwindSafe for Graph

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> Same for T

Source§

type Output = T

Should always be Self
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.