Skip to main content

rlx_runtime/
session.rs

1// RLX — versatile ML compiler + runtime.
2// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5//! Session — the main entry point for compiling and executing graphs.
6
7use crate::backend::Backend;
8use crate::compiled::CompiledGraph;
9use crate::precision::Precision;
10use rlx_driver::Device;
11use rlx_ir::Graph;
12use rlx_ir::GraphModule;
13use rlx_ir::hir::HirModule;
14use rlx_opt::PrecisionPolicy;
15
16/// A session manages graph compilation and execution on a device.
17pub struct Session {
18    device: Device,
19    precision: Precision,
20    /// Optional per-op precision policy. If set, runs AutoMixedPrecision
21    /// rewrite before backend compile. Works identically across all modes
22    /// (AOT compile, trace/JIT, proc-macro AOT) — it's just a graph pass.
23    policy: Option<PrecisionPolicy>,
24}
25
26impl Session {
27    /// Create a session for the given device at default (F32) precision.
28    ///
29    /// # Panics
30    /// Panics if the device is not available (missing feature flag).
31    pub fn new(device: Device) -> Self {
32        Self::new_with_precision(device, Precision::F32)
33    }
34
35    /// Create a session targeting a specific numeric precision.
36    /// Backends fall back to F32 if the requested precision isn't supported.
37    pub fn new_with_precision(device: Device, precision: Precision) -> Self {
38        // Availability has two halves — feature compiled in, and device present.
39        // Name the one that is actually missing: "enable the `gpu` feature" is
40        // wrong (and misleading) on a headless host where it already is enabled.
41        assert!(
42            crate::device_ext::is_available(device),
43            "device {} is not available — {}",
44            device,
45            if crate::device_ext::feature_compiled(device) {
46                format!(
47                    "the `{}` feature is enabled but no such device/driver was found on this host",
48                    feature_name(device)
49                )
50            } else {
51                format!("enable the `{}` Cargo feature", feature_name(device))
52            }
53        );
54        Self {
55            device,
56            precision,
57            policy: None,
58        }
59    }
60
61    /// Builder: set a per-op precision policy. Applied as a graph rewrite
62    /// before backend compile. Same mechanism works for AOT compile, JIT
63    /// tracing, and proc-macro AOT — it's a graph pass, not a runtime mode.
64    pub fn with_policy(mut self, policy: PrecisionPolicy) -> Self {
65        self.policy = Some(policy);
66        self
67    }
68
69    pub fn device(&self) -> Device {
70        self.device
71    }
72    pub fn precision(&self) -> Precision {
73        self.precision
74    }
75    pub fn policy(&self) -> Option<&PrecisionPolicy> {
76        self.policy.as_ref()
77    }
78
79    /// Compile a MIR graph through the fusion-first pipeline (`GraphModule` → LIR).
80    ///
81    /// Prefer [`Self::compile_hir`] or [`Self::compile_module`] for new code.
82    /// This entry wraps the graph as a MIR-stage [`GraphModule`].
83    ///
84    /// If the graph carries `Dim::Dynamic` dims it is compiled lazily: the first
85    /// `run` infers the concrete shape and specializes, so the same graph runs at
86    /// multiple input shapes on every backend. See [`crate::deferred`].
87    ///
88    /// On ANE with `RLX_COREML_NATIVE_FLEX=1`, dynamic graphs compile once with
89    /// CoreML `ShapeRange` and infer shapes at predict time instead.
90    pub fn compile(&self, graph: Graph) -> CompiledGraph {
91        if rlx_ir::dynamic::has_dynamic_dims(&graph) && !self.coreml_native_flex() {
92            return self.compile_deferred(graph, self.default_options());
93        }
94        let opts = self.default_options();
95        if opts.cache_param_invariant
96            && let Some(staged) = self.try_hoist_param_invariant(&graph, &opts)
97        {
98            return staged;
99        }
100        self.compile_module(GraphModule::from_graph(graph))
101            .expect("compile MIR graph through fusion pipeline")
102    }
103
104    /// If `graph` has a hoistable param-invariant closure, compile it as a
105    /// separate run-once `prepare` graph and attach it to the compiled main
106    /// graph. Returns `None` when there is nothing to hoist. Sub-compiles clear
107    /// the flag so this does not recurse.
108    fn try_hoist_param_invariant(
109        &self,
110        graph: &Graph,
111        options: &crate::CompileOptions,
112    ) -> Option<CompiledGraph> {
113        let split = rlx_compile::split_param_invariant(graph)?;
114        let mut sub = options.clone();
115        sub.cache_param_invariant = false;
116        let prepare = self.compile_with(split.prepare, &sub);
117        let main = self.compile_with(split.main, &sub);
118        Some(main.with_staging(
119            prepare,
120            split.boundary,
121            split.prepare_params,
122            split.main_params,
123        ))
124    }
125
126    /// Wrap a dynamic-shape graph so it specializes + recompiles per input shape.
127    fn compile_deferred(&self, graph: Graph, options: crate::CompileOptions) -> CompiledGraph {
128        let backend = self.create_backend();
129        let inner = Box::new(crate::deferred::DeferredExecutable::new(
130            graph,
131            backend,
132            options,
133            self.device,
134        ));
135        CompiledGraph::new(inner, self.device)
136    }
137
138    /// Explicit legacy alias — same as [`Self::compile`].
139    pub fn compile_graph(&self, graph: Graph) -> CompiledGraph {
140        self.compile(graph)
141    }
142
143    /// Compile with explicit options (full control over the pipeline).
144    /// Most callers use `compile()` and configure the session via
145    /// `new_with_precision` / `with_policy`. This escape hatch is for
146    /// callers that need finer control (e.g., disable DCE for debugging).
147    pub fn compile_with(&self, graph: Graph, options: &crate::CompileOptions) -> CompiledGraph {
148        // Opt-in native low-precision GEMM: rewrite every 2-D matmul into a
149        // dynamically-quantized `ScaledMatMul` in the requested format before
150        // the rest of the pipeline (must precede constant folding). Requires
151        // static matmul shapes.
152        let graph = match options.scaled_quant {
153            Some(cfg) => {
154                rlx_opt::rlx_compile::scaled_quant_insert::insert_scaled_matmul(graph, cfg)
155            }
156            None => graph,
157        };
158        if rlx_ir::dynamic::has_dynamic_dims(&graph) && !self.coreml_native_flex() {
159            return self.compile_deferred(graph, options.clone());
160        }
161        if options.cache_param_invariant
162            && let Some(staged) = self.try_hoist_param_invariant(&graph, options)
163        {
164            return staged;
165        }
166        self.compile_module_with(GraphModule::from_graph(graph), options)
167            .expect("compile MIR graph through fusion pipeline")
168    }
169
170    /// Compile a fusion-first HIR module through HIR → MIR → LIR.
171    pub fn compile_hir(&self, hir: HirModule) -> Result<CompiledGraph, rlx_ir::hir::LowerError> {
172        self.compile_hir_with(hir, &self.default_options())
173    }
174
175    /// Compile HIR with explicit compile options.
176    pub fn compile_hir_with(
177        &self,
178        hir: HirModule,
179        options: &crate::CompileOptions,
180    ) -> Result<CompiledGraph, rlx_ir::hir::LowerError> {
181        let backend = self.create_backend();
182        let executable = backend.compile_hir(hir, self.device, options)?;
183        Ok(CompiledGraph::new(executable, self.device))
184    }
185
186    /// Compile a [`GraphModule`] (HIR/MIR/LIR stage) through the pipeline.
187    pub fn compile_module(
188        &self,
189        module: GraphModule,
190    ) -> Result<CompiledGraph, rlx_ir::hir::LowerError> {
191        self.compile_module_with(module, &self.default_options())
192    }
193
194    /// Compile a [`GraphModule`] with explicit compile options.
195    pub fn compile_module_with(
196        &self,
197        module: GraphModule,
198        options: &crate::CompileOptions,
199    ) -> Result<CompiledGraph, rlx_ir::hir::LowerError> {
200        let backend = self.create_backend();
201        let executable = backend.compile_module(module, self.device, options)?;
202        Ok(CompiledGraph::new(executable, self.device))
203    }
204
205    fn default_options(&self) -> crate::CompileOptions {
206        let opts = crate::CompileOptions::new().precision(self.precision);
207        match &self.policy {
208            Some(p) => opts.policy(p.clone()),
209            None => opts,
210        }
211    }
212
213    /// ANE + `RLX_COREML_NATIVE_FLEX=1`: one CoreML model with `ShapeRange`.
214    fn coreml_native_flex(&self) -> bool {
215        #[cfg(all(
216            feature = "coreml",
217            target_vendor = "apple",
218            not(target_os = "watchos")
219        ))]
220        {
221            self.device == Device::Ane
222                && std::env::var("RLX_COREML_NATIVE_FLEX").ok().as_deref() == Some("1")
223        }
224        #[cfg(not(all(
225            feature = "coreml",
226            target_vendor = "apple",
227            not(target_os = "watchos")
228        )))]
229        {
230            false
231        }
232    }
233
234    fn create_backend(&self) -> Box<dyn Backend> {
235        // Single dispatch point: consult the registry. Backends register
236        // themselves (builtins via cfg-gated `register_builtin`; external
237        // crates via `register_backend`). No hardcoded match here.
238        crate::registry::backend_for(self.device).unwrap_or_else(|| {
239            panic!(
240                "no backend registered for device {} — enable feature `{}` \
241                 (or call `rlx_runtime::register_backend` for an external backend)",
242                self.device,
243                feature_name(self.device)
244            )
245        })
246    }
247}
248
249fn feature_name(device: Device) -> &'static str {
250    match device {
251        Device::Cpu => "cpu",
252        Device::Metal => "metal",
253        Device::Mlx => "mlx",
254        Device::Ane => "ane",
255        Device::Cuda => "cuda",
256        Device::Rocm => "rocm",
257        Device::Xdna => "xdna",
258        Device::OneApi => "oneapi",
259        Device::Tpu => "tpu",
260        Device::Hexagon => "qnn",
261        Device::Gpu => "gpu",
262        Device::Vulkan => "vulkan",
263        Device::OpenGl => "opengl",
264        Device::DirectX => "directx",
265        Device::WebGpu => "webgpu",
266    }
267}