1use crate::backend::Backend;
19use crate::compiled::CompiledGraph;
20use crate::precision::Precision;
21use rlx_driver::Device;
22use rlx_ir::Graph;
23use rlx_ir::GraphModule;
24use rlx_ir::hir::HirModule;
25use rlx_opt::PrecisionPolicy;
26
27pub struct Session {
29 device: Device,
30 precision: Precision,
31 policy: Option<PrecisionPolicy>,
35}
36
37impl Session {
38 pub fn new(device: Device) -> Self {
43 Self::new_with_precision(device, Precision::F32)
44 }
45
46 pub fn new_with_precision(device: Device, precision: Precision) -> Self {
49 assert!(
50 crate::device_ext::is_available(device),
51 "device {} is not available — enable the `{}` Cargo feature",
52 device,
53 feature_name(device)
54 );
55 Self {
56 device,
57 precision,
58 policy: None,
59 }
60 }
61
62 pub fn with_policy(mut self, policy: PrecisionPolicy) -> Self {
66 self.policy = Some(policy);
67 self
68 }
69
70 pub fn device(&self) -> Device {
71 self.device
72 }
73 pub fn precision(&self) -> Precision {
74 self.precision
75 }
76 pub fn policy(&self) -> Option<&PrecisionPolicy> {
77 self.policy.as_ref()
78 }
79
80 pub fn compile(&self, graph: Graph) -> CompiledGraph {
92 if rlx_ir::dynamic::has_dynamic_dims(&graph) && !self.coreml_native_flex() {
93 return self.compile_deferred(graph, self.default_options());
94 }
95 self.compile_module(GraphModule::from_graph(graph))
96 .expect("compile MIR graph through fusion pipeline")
97 }
98
99 fn compile_deferred(&self, graph: Graph, options: crate::CompileOptions) -> CompiledGraph {
101 let backend = self.create_backend();
102 let inner = Box::new(crate::deferred::DeferredExecutable::new(
103 graph,
104 backend,
105 options,
106 self.device,
107 ));
108 CompiledGraph::new(inner, self.device)
109 }
110
111 pub fn compile_graph(&self, graph: Graph) -> CompiledGraph {
113 self.compile(graph)
114 }
115
116 pub fn compile_with(&self, graph: Graph, options: &crate::CompileOptions) -> CompiledGraph {
121 if rlx_ir::dynamic::has_dynamic_dims(&graph) && !self.coreml_native_flex() {
122 return self.compile_deferred(graph, options.clone());
123 }
124 self.compile_module_with(GraphModule::from_graph(graph), options)
125 .expect("compile MIR graph through fusion pipeline")
126 }
127
128 pub fn compile_hir(&self, hir: HirModule) -> Result<CompiledGraph, rlx_ir::hir::LowerError> {
130 self.compile_hir_with(hir, &self.default_options())
131 }
132
133 pub fn compile_hir_with(
135 &self,
136 hir: HirModule,
137 options: &crate::CompileOptions,
138 ) -> Result<CompiledGraph, rlx_ir::hir::LowerError> {
139 let backend = self.create_backend();
140 let executable = backend.compile_hir(hir, self.device, options)?;
141 Ok(CompiledGraph::new(executable, self.device))
142 }
143
144 pub fn compile_module(
146 &self,
147 module: GraphModule,
148 ) -> Result<CompiledGraph, rlx_ir::hir::LowerError> {
149 self.compile_module_with(module, &self.default_options())
150 }
151
152 pub fn compile_module_with(
154 &self,
155 module: GraphModule,
156 options: &crate::CompileOptions,
157 ) -> Result<CompiledGraph, rlx_ir::hir::LowerError> {
158 let backend = self.create_backend();
159 let executable = backend.compile_module(module, self.device, options)?;
160 Ok(CompiledGraph::new(executable, self.device))
161 }
162
163 fn default_options(&self) -> crate::CompileOptions {
164 let opts = crate::CompileOptions::new().precision(self.precision);
165 match &self.policy {
166 Some(p) => opts.policy(p.clone()),
167 None => opts,
168 }
169 }
170
171 fn coreml_native_flex(&self) -> bool {
173 #[cfg(all(
174 feature = "coreml",
175 target_vendor = "apple",
176 not(target_os = "watchos")
177 ))]
178 {
179 self.device == Device::Ane
180 && std::env::var("RLX_COREML_NATIVE_FLEX").ok().as_deref() == Some("1")
181 }
182 #[cfg(not(all(
183 feature = "coreml",
184 target_vendor = "apple",
185 not(target_os = "watchos")
186 )))]
187 {
188 false
189 }
190 }
191
192 fn create_backend(&self) -> Box<dyn Backend> {
193 crate::registry::backend_for(self.device).unwrap_or_else(|| {
197 panic!(
198 "no backend registered for device {} — enable feature `{}` \
199 (or call `rlx_runtime::register_backend` for an external backend)",
200 self.device,
201 feature_name(self.device)
202 )
203 })
204 }
205}
206
207fn feature_name(device: Device) -> &'static str {
208 match device {
209 Device::Cpu => "cpu",
210 Device::Metal => "metal",
211 Device::Mlx => "mlx",
212 Device::Ane => "ane",
213 Device::Cuda => "cuda",
214 Device::Rocm => "rocm",
215 Device::OneApi => "oneapi",
216 Device::Tpu => "tpu",
217 Device::Hexagon => "qnn",
218 Device::Gpu => "gpu",
219 Device::Vulkan => "vulkan",
220 Device::OpenGl => "opengl",
221 Device::DirectX => "directx",
222 Device::WebGpu => "webgpu",
223 }
224}