Enum CompiledGraph

Source
pub enum CompiledGraph<S: Shape, T: DType, D: Dev> {
    Cpu {
        order: Vec<usize>,
        graph: Vec<GraphNode<T>>,
        ghost: PhantomData<(S, T, D)>,
    },
}
Expand description

A representation of the compiled graph. The shape is the output shape.

Variants§

§

Cpu

Fields

§order: Vec<usize>
§graph: Vec<GraphNode<T>>

Implementations§

Source§

impl<S: Shape, T: DType, D: Dev> CompiledGraph<S, T, D>

Source

pub fn run(&self) -> Result<Tensor<S, T, D>>

Run the precompiled graph. This executes all nodes on the specified backend device and returns a concrete tensor.

Examples found in repository?
examples/hello_world/main.rs (line 18)
3fn main() {
4    let mut graph: Graph<f32> = Graph::empty();
5    let _arange = GraphTensor::<R1<10>, f32, Cpu>::arange(&mut graph, 0., 1.);
6    let a = GraphTensor::<R2<3, 4>, f32, Cpu>::fill(&mut graph, 1.0);
7    let b = GraphTensor::<R2<3, 4>, f32, Cpu>::fill(&mut graph, 2.0);
8    let c = GraphTensor::<R2<3, 4>, f32, Cpu>::fill(&mut graph, 3.0);
9    let d = GraphTensor::<R2<3, 4>, f32, Cpu>::fill(&mut graph, 4.0);
10    let res = a * b + c;
11    let _out = res + d;
12
13    graph.optimize();
14
15    graph.visualize("graph.png").unwrap();
16
17    let compiled: constensor_core::CompiledGraph<R2<3, 4>, f32, Cpu> = graph.compile().unwrap();
18    let res = compiled.run().unwrap();
19
20    let tensor: Tensor<R2<3, 4>, f32, Cpu> = res;
21
22    assert_eq!(tensor.data().unwrap().to_vec(), vec![vec![9.0; 4]; 3],);
23}
More examples
Hide additional examples
examples/matmul/main.rs (line 27)
4fn bench<T: DType, const B: usize, const M: usize, const K: usize, const N: usize>(
5    type_name: &str,
6    alpha: T,
7    beta: T,
8) {
9    // Number of times to run the matmul for averaging
10    let iterations = 1;
11    let mut total = std::time::Duration::new(0, 0);
12
13    let mut graph = Graph::empty();
14    let a = GraphTensor::<R3<B, M, K>, T, BestDevice<0>>::fill(&mut graph, T::from_f64(1.));
15    // Strided matmuls works on all devices.
16    let b = GraphTensor::<R3<B, N, K>, T, BestDevice<0>>::fill(&mut graph, T::from_f64(2.)).t();
17    // let b = GraphTensor::<R3<B, K, N>, T, BestDevice<0>>::fill(&mut graph, T::from_f64(2.));
18    let o = GraphTensor::<R3<B, M, N>, T, BestDevice<0>>::fill(&mut graph, T::from_f64(3.));
19    let _c = a.matmul_axpby(b, o, alpha, beta);
20
21    graph.optimize();
22    let compiled: CompiledGraph<R3<B, M, N>, T, BestDevice<0>> = graph.compile().unwrap();
23
24    for _ in 0..iterations {
25        let start = Instant::now();
26
27        let tensor = std::hint::black_box(compiled.run().unwrap());
28        dbg!(tensor.data().unwrap());
29
30        total += start.elapsed();
31    }
32
33    let avg = total / (iterations as u32);
34    println!("Average execution time for {type_name} over {iterations} iterations: {avg:?}");
35}

Auto Trait Implementations§

§

impl<S, T, D> Freeze for CompiledGraph<S, T, D>

§

impl<S, T, D> !RefUnwindSafe for CompiledGraph<S, T, D>

§

impl<S, T, D> !Send for CompiledGraph<S, T, D>

§

impl<S, T, D> !Sync for CompiledGraph<S, T, D>

§

impl<S, T, D> Unpin for CompiledGraph<S, T, D>
where S: Unpin, T: Unpin, D: Unpin,

§

impl<S, T, D> !UnwindSafe for CompiledGraph<S, T, D>

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

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

Source§

fn vzip(self) -> V