pub struct Graph<T: DType> { /* private fields */ }
Implementations§
Source§impl<T: DType> Graph<T>
impl<T: DType> Graph<T>
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Create an empty Graph
Examples found in repository?
examples/hello_world/main.rs (line 4)
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
examples/matmul/main.rs (line 13)
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}
Sourcepub fn get_ops(&self) -> RwLockReadGuard<'_, Vec<GraphNode<T>>>
pub fn get_ops(&self) -> RwLockReadGuard<'_, Vec<GraphNode<T>>>
Read-only access to the list of operations
pub fn to_petgraph(&self) -> PetGraph<String, String>
Sourcepub fn visualize<P: AsRef<Path>>(&self, filename: P) -> Result<()>
pub fn visualize<P: AsRef<Path>>(&self, filename: P) -> Result<()>
Visualize the graph by saving it to this file.
Install graphvis:
- brew install graphviz
- apt install graphviz
Examples found in repository?
examples/hello_world/main.rs (line 15)
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}
Sourcepub fn optimize(&mut self)
pub fn optimize(&mut self)
Optimize this graph.
Apply the following optimizations:
- Constant folding of elementwise fills
- Fuse mul-add into FMA
- Inplace binary operations when safe
- Inplace fused multiply-add when safe
- Inplace matrix-multiplication when safe
- Dead code removal
Examples found in repository?
examples/hello_world/main.rs (line 13)
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
examples/matmul/main.rs (line 21)
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}
Sourcepub fn compile<S: Shape, D: Dev>(self) -> Result<CompiledGraph<S, T, D>>
pub fn compile<S: Shape, D: Dev>(self) -> Result<CompiledGraph<S, T, D>>
Compile this graph and insert device-specific optimizations such as CUDA streams.
Examples found in repository?
examples/hello_world/main.rs (line 17)
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
examples/matmul/main.rs (line 22)
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§
Auto Trait Implementations§
impl<T> Freeze for Graph<T>
impl<T> RefUnwindSafe for Graph<T>
impl<T> !Send for Graph<T>
impl<T> !Sync for Graph<T>
impl<T> Unpin for Graph<T>
impl<T> UnwindSafe for Graph<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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