Struct GraphTensor

Source
pub struct GraphTensor<S: Shape, T: DType, D: Dev> { /* private fields */ }
Expand description

A tensor representing an intermediary result of a graph. Performing operations on this tensor will not cause any computations.

Implementations§

Source§

impl<const B: usize, const M: usize, const K: usize, T: DType, D: Dev> GraphTensor<R3<B, M, K>, T, D>

Source

pub fn matmul<const N: usize>( self, rhs: GraphTensor<R3<B, K, N>, T, D>, ) -> GraphTensor<R3<B, M, N>, T, D>

Source

pub fn matmul_axpby<const N: usize>( self, rhs: GraphTensor<R3<B, K, N>, T, D>, out: GraphTensor<R3<B, M, N>, T, D>, alpha: T, beta: T, ) -> GraphTensor<R3<B, M, N>, T, D>

out = out * alpha + beta * lhs * rhs

Examples found in repository?
examples/matmul/main.rs (line 19)
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}
Source§

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

Source

pub fn fill(graph: &mut Graph<T>, v: T) -> Self

Create a tensor filled with some value.

Examples found in repository?
examples/hello_world/main.rs (line 6)
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 14)
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}
Source

pub fn zeros(graph: &mut Graph<T>) -> Self

Create a tensor filled with zeros.

Source

pub fn ones(graph: &mut Graph<T>) -> Self

Create a tensor filled with ones.

Source

pub fn sqrt(self) -> GraphTensor<S, T, D>

Elementwise unary square root.

Source

pub fn rand(graph: &mut Graph<T>) -> Self

Create a tensor filled with uniform random values in [0,1).

Source

pub fn randn(graph: &mut Graph<T>, mean: T, std: T) -> Self

Create a tensor filled with normally distributed random values (mean, std).

Source§

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

Source

pub fn graph(&self) -> RwLockReadGuard<'_, Graph<T>>

Retrieve the graph for this GraphTensor.

Source

pub fn id(&self) -> GraphTensorId

Get the graph tensor ID.

Source§

impl<const A: usize, T: DType, D: Dev> GraphTensor<R1<A>, T, D>

Source

pub fn arange(graph: &mut Graph<T>, start: T, stop: T) -> Self

A GraphTensor representing a vector ranging from start to stop with step computed using A.

Examples found in repository?
examples/hello_world/main.rs (line 5)
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}
Source§

impl<T: DType, const A: usize, const B: usize, D: Dev> GraphTensor<R2<A, B>, T, D>

Source

pub fn t(&self) -> GraphTensor<R2<B, A>, T, D>

Return a view of this matrix with dimensions transposed (A x B -> B x A).

Source§

impl<T: DType, const A: usize, const B: usize, const C: usize, D: Dev> GraphTensor<R3<A, B, C>, T, D>

Source

pub fn t(&self) -> GraphTensor<R3<A, C, B>, T, D>

Return a view of this tensor with last two reversed axes (A x B x C -> A x C x B).

Examples found in repository?
examples/matmul/main.rs (line 16)
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}

Trait Implementations§

Source§

impl<S: Shape, T: DType, D: Dev> Add for GraphTensor<S, T, D>

Source§

fn add(self, rhs: Self) -> Self::Output

Add an elementwise operation to the graph.

Source§

type Output = GraphTensor<S, T, D>

The resulting type after applying the + operator.
Source§

impl<S: Clone + Shape, T: Clone + DType, D: Clone + Dev> Clone for GraphTensor<S, T, D>

Source§

fn clone(&self) -> GraphTensor<S, T, D>

Returns a copy 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<S: Shape, T: DType, D: Dev> Div for GraphTensor<S, T, D>

Source§

fn div(self, rhs: Self) -> Self::Output

Add an elementwise operation to the graph.

Source§

type Output = GraphTensor<S, T, D>

The resulting type after applying the / operator.
Source§

impl<S: Shape, T: DType, D: Dev> Mul for GraphTensor<S, T, D>

Source§

fn mul(self, rhs: Self) -> Self::Output

Add an elementwise operation to the graph.

Source§

type Output = GraphTensor<S, T, D>

The resulting type after applying the * operator.
Source§

impl<S: Shape, T: DType + Neg<Output = T>, D: Dev> Neg for GraphTensor<S, T, D>

Source§

fn neg(self) -> Self::Output

Add an elementwise addition operation to the graph.

Source§

type Output = GraphTensor<S, T, D>

The resulting type after applying the - operator.
Source§

impl<S: Shape, T: DType, D: Dev> Sub for GraphTensor<S, T, D>

Source§

fn sub(self, rhs: Self) -> Self::Output

Add an elementwise operation to the graph.

Source§

type Output = GraphTensor<S, T, D>

The resulting type after applying the - operator.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<S, T, D> !UnwindSafe for GraphTensor<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> 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> 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> 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.
Source§

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

Source§

fn vzip(self) -> V