Skip to main content

rlx_ir/
lib.rs

1// RLX — versatile ML compiler + runtime.
2// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, version 3.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16//! RLX Tensor IR — the intermediate representation for the RLX ML compiler.
17//!
18//! This IR is:
19//! - **Standalone**: no runtime, no backend, no framework coupling
20//! - **Serializable**: graphs can be saved/loaded for AOT compilation
21//! - **Optimizable**: designed for pattern-matching fusion and buffer planning
22//!
23//! The compiler pipeline has three named levels:
24//!
25//! - **HIR** ([`hir`]) — block-oriented IR for model builders (`Linear`,
26//!   `SwiGLU`, `ResidualRmsNorm`, …).
27//! - **MIR** ([`mir`]) — fused tensor DAG; input to [`rlx_opt`].
28//! - **LIR** ([`lir`]) — optimized MIR + arena buffer plan for backends.
29//!
30//! [`Graph`] is the primary DX surface. Use [`Graph::define`] for
31//! fusion-first HIR builders, or [`Graph::new`] / [`GraphModule::mir`]
32//! for primitive MIR. [`GraphModule`] tracks pipeline stage (HIR/MIR/LIR).
33//!
34//! - [`Graph`]: a DAG of tensor operations (like XLA's HloModule)
35//! - [`Node`]: a single operation with typed inputs/outputs
36//! - [`Op`]: the operation kind with parameters
37
38pub mod ad;
39pub mod async_copy;
40pub mod const_check;
41pub mod dtype;
42pub mod dynamic;
43pub mod env;
44pub mod graph;
45pub mod hir;
46pub mod infer;
47pub mod infer_shape;
48pub mod inspect;
49pub mod layout;
50pub mod lir;
51pub mod logical_kernel;
52pub mod measure;
53pub mod mir;
54pub mod module;
55pub mod nvfp4;
56pub mod op;
57pub mod op_registry;
58pub mod ops;
59pub mod perfetto;
60pub mod phase;
61pub mod pretty;
62pub mod provenance;
63pub mod quant;
64pub mod rng;
65pub use nvfp4::{FP4_E2M1_LUT, NVFP4_GROUP_SIZE, fp4_e2m1_to_f32, fp8_e4m3_scale_to_f32};
66pub mod binding_manifest;
67pub mod component;
68pub mod hir_extension;
69pub mod reflect;
70#[cfg(feature = "serialize")]
71pub mod serialize;
72pub mod shape;
73pub mod target;
74pub mod variant;
75pub mod verify;
76
77pub use ad::AdPipelineStage;
78pub use async_copy::{AsyncCopy, BarrierToken, DoubleBuffer, SyncCopy};
79pub use dtype::{DType, Element, ElementSubtype};
80pub use dynamic::sym;
81pub use dynamic::{
82    DimEnv, bind_graph, collect_dynamic_symbols, has_dynamic_dims, infer_bindings_from_f32_inputs,
83    infer_bindings_from_inputs, same_binding, sync_concat_shapes, sync_graph_shapes,
84    sync_narrow_ops, sync_reshape_ops,
85};
86pub use env::{RlxEnv, RuntimeOverrides, flag, is_unset, parse_or, set, unset, var, var_os};
87pub use graph::{Graph, Node, NodeId};
88pub use hir::{FusionPolicy, HirGraphExt, HirModule, HirMut, HirNode, HirNodeId, HirOp};
89pub use infer::GraphExt;
90pub use inspect::{
91    inspect_buffer_plan, inspect_graph, inspect_graph_diff, inspect_hir, inspect_hir_stats,
92    inspect_lir, inspect_mir, inspect_mir_diff, inspect_mir_stats,
93};
94pub use layout::{Coord2, Ragged, ShapeTuple, Strides2, Strides3, Tile2, Tile3};
95pub use lir::{
96    LirBufferPlan, LirBufferSlot, LirFingerprint, LirIoManifest, LirModule, LirViewAlias,
97};
98pub use logical_kernel::{
99    KernelDispatchConfig, KernelDispatchPolicy, LogicalKernelEntry, logical_kinds_in_graph,
100    registered_logical_kernels, should_lower_to_common,
101};
102pub use measure::{CacheBuster, Tick, time_ns};
103pub use mir::{MirModule, MirNode, MirNodeId, MirOp};
104pub use module::{GraphModule, GraphStage};
105pub use op::{Op, OpKind};
106pub use op_registry::{
107    JvpContext, OpExtension, OpRegistry, VjpContext, VmapContext, global_registry, lookup_op,
108    register_op,
109};
110pub use ops::attention::attention_kind_op;
111pub use phase::{Phase, PhaseSchedule, derive_phases};
112pub use provenance::{NodeOrigin, node_label, stamp_pass_origins};
113pub use quant::{QuantMap, QuantScheme};
114pub use rng::Philox4x32;
115#[cfg(feature = "serialize")]
116pub use serialize::{hir_from_json, hir_to_json, lir_from_json, lir_to_json};
117pub use verify::{VerifyError, verify, verify_all, verify_shapes};
118
119/// Lower a HIR module to MIR, then extract the legacy [`Graph`] API surface.
120pub fn hir_to_graph(hir: HirModule) -> Result<Graph, hir::LowerError> {
121    Ok(hir.lower_to_mir()?.into_graph())
122}
123pub use binding_manifest::{BindingManifest, IoBindingEntry, WeightBlock};
124pub use component::{CompilationMode, ModelComponent};
125pub use hir_extension::{
126    HirExtensionFn, apply_hir_extensions, apply_hir_extensions_named, register_hir_extension,
127    registered_hir_extensions,
128};
129pub use reflect::{
130    BlockSpecialization, HirReflection, ManifestDiff, MirReflection, SpecializeBlockRecord,
131    layout_for_binding, layout_from_lir, probe_block_specialization, symbolic_layout_hint,
132};
133pub use shape::{Dim, DimBinding, Shape};
134pub use variant::{ModelPhase, ModelVariant};