|
| 1 | +use std::fs; |
| 2 | + |
| 3 | +use luminal::{ |
| 4 | + self, |
| 5 | + graph::{hlir_to_egglog, Graph, Runtime}, |
| 6 | + prelude::*, |
| 7 | + serialized_egraph::SerializedEGraph, |
| 8 | + visualization::{ToDot, ToHtml}, |
| 9 | +}; |
| 10 | +use luminal_cuda::runtime::{CudaRuntime, CustomState}; |
| 11 | + |
| 12 | +use egglog::{prelude::RustSpan, var, EGraph}; |
| 13 | +use egglog_ast::span::Span; |
| 14 | +use rustc_hash::FxHashMap; |
| 15 | + |
| 16 | +fn main() { |
| 17 | + // Create a new graph |
| 18 | + let mut cx = Graph::new(); |
| 19 | + |
| 20 | + // Create input tensor using constant values |
| 21 | + |
| 22 | + let (m, n, k) = (4096, 14336, 9192); |
| 23 | + |
| 24 | + let a = cx.tensor((m, k)); |
| 25 | + let b = cx.tensor((k, n)); |
| 26 | + |
| 27 | + let _c = a.matmul(b); |
| 28 | + |
| 29 | + let ctx = luminal_cuda::cudarc::driver::CudaContext::new(0).unwrap(); |
| 30 | + ctx.bind_to_thread().unwrap(); |
| 31 | + let _stream = ctx.default_stream(); |
| 32 | + let _custom_state: FxHashMap<String, CustomState> = FxHashMap::default(); |
| 33 | + |
| 34 | + println!("Visualizing HLIR"); |
| 35 | + fs::write("HLIR.dot", cx.graph.to_dot().unwrap()).unwrap(); |
| 36 | + |
| 37 | + println!("Building and Saturating EGraph"); |
| 38 | + cx.build_search_space::<CudaRuntime>(); |
| 39 | + |
| 40 | + let (program, root) = hlir_to_egglog(&cx); |
| 41 | + |
| 42 | + let mut ops = <CudaRuntime as Runtime>::Ops::into_vec(); |
| 43 | + ops.extend(<luminal::op::Ops as IntoEgglogOp>::into_vec()); |
| 44 | + |
| 45 | + let mut egglog_obj: EGraph = egglog::EGraph::default(); |
| 46 | + |
| 47 | + // setup the rules and datatypes |
| 48 | + egglog_obj |
| 49 | + .parse_and_run_program(None, luminal::egglog_utils::BASE) |
| 50 | + .unwrap(); |
| 51 | + egglog_obj |
| 52 | + .parse_and_run_program(None, &luminal::egglog_utils::op_defs_string(&ops)) |
| 53 | + .unwrap(); |
| 54 | + egglog_obj |
| 55 | + .parse_and_run_program(None, &luminal::egglog_utils::op_rewrites_string(&ops)) |
| 56 | + .unwrap(); |
| 57 | + egglog_obj |
| 58 | + .parse_and_run_program(None, luminal::egglog_utils::BASE_CLEANUP) |
| 59 | + .unwrap(); |
| 60 | + egglog_obj |
| 61 | + .parse_and_run_program(None, &luminal::egglog_utils::op_cleanups_string(&ops)) |
| 62 | + .unwrap(); |
| 63 | + |
| 64 | + // load the program |
| 65 | + egglog_obj.parse_and_run_program(None, &program).unwrap(); |
| 66 | + |
| 67 | + // run the graph |
| 68 | + egglog_obj |
| 69 | + .parse_and_run_program(None, luminal::egglog_utils::RUN_SCHEDULE) |
| 70 | + .unwrap(); |
| 71 | + |
| 72 | + // EGraph Optimization Complete |
| 73 | + println!("Visualizing EGraph"); |
| 74 | + // save the egraph visualizations |
| 75 | + fs::write("egraph.html", egglog_obj.to_html().unwrap()).unwrap(); |
| 76 | + fs::write("egraph.dot", egglog_obj.to_dot().unwrap()).unwrap(); |
| 77 | + |
| 78 | + let (sort, value) = egglog_obj.eval_expr(&var!(root)).unwrap(); |
| 79 | + let s_egraph = SerializedEGraph::new(&egglog_obj, vec![(sort, value)]); |
| 80 | + let llir_graphs = egglog_to_llir(&s_egraph, &ops, 100); |
| 81 | + |
| 82 | + let example_llir_graph = llir_graphs.last().unwrap(); |
| 83 | + |
| 84 | + println!("Visualizing LLIR Graph"); |
| 85 | + fs::write("LLIR.dot", example_llir_graph.to_dot().unwrap()).unwrap(); |
| 86 | +} |
0 commit comments