1use crate::Precision;
23use rlx_ir::OpKind;
24use rlx_ir::logical_kernel::{KernelDispatchConfig, KernelDispatchPolicy};
25use rlx_opt::rlx_compile::scaled_quant_insert::ScaledQuantConfig;
26use rlx_opt::{FusionOptions, FusionTarget, PrecisionPolicy};
27use std::collections::HashMap;
28
29#[derive(Debug, Clone)]
32pub struct CompileOptions {
33 pub precision: Precision,
35 pub policy: Option<PrecisionPolicy>,
37 pub scaled_quant: Option<ScaledQuantConfig>,
42 pub rng: rlx_ir::RngOptions,
44 pub dce: bool,
46 pub constant_folding: bool,
48 pub verbose: bool,
51 pub fusion_target: Option<FusionTarget>,
53 pub fusion_opts: FusionOptions,
55 pub arena_alignment: usize,
57 pub assert_fusion_clean: bool,
59 pub supported_ops: Option<&'static [OpKind]>,
62 pub dim_binding: Option<rlx_ir::DimBinding>,
64 pub param_bindings: Option<HashMap<String, Vec<f32>>>,
66 pub quant_param_bindings: Option<HashMap<String, Vec<u8>>>,
68 pub kernel_dispatch: KernelDispatchConfig,
70 pub disable_mpsgraph: bool,
75 pub scan_unroll_max_length: u32,
81 pub cache_param_invariant: bool,
88 pub lint_numerics: bool,
90 pub fusion_report: bool,
92 pub disable_conv_bias_act_fusion: bool,
94 pub no_io_peaks_output: bool,
96}
97
98impl Default for CompileOptions {
99 fn default() -> Self {
100 Self::from_env()
101 }
102}
103
104impl CompileOptions {
105 pub fn baseline() -> Self {
107 Self {
108 precision: Precision::F32,
109 policy: None,
110 scaled_quant: None,
111 rng: rlx_ir::RngOptions::default(),
112 dce: true,
113 constant_folding: true,
114 verbose: false,
115 fusion_target: None,
116 fusion_opts: FusionOptions::default(),
117 arena_alignment: 64,
118 assert_fusion_clean: false,
119 supported_ops: None,
120 dim_binding: None,
121 param_bindings: None,
122 quant_param_bindings: None,
123 kernel_dispatch: KernelDispatchConfig::default(),
124 disable_mpsgraph: false,
125 scan_unroll_max_length: 64,
126 cache_param_invariant: false,
127 lint_numerics: false,
128 fusion_report: false,
129 disable_conv_bias_act_fusion: false,
130 no_io_peaks_output: false,
131 }
132 }
133
134 pub fn from_env() -> Self {
136 let mut opts = Self::baseline();
137 opts.apply_env();
138 opts
139 }
140
141 pub fn apply_env(&mut self) {
144 use rlx_ir::env_registry;
145 if env_registry::flag("RLX_VERBOSE") {
146 self.verbose = true;
147 }
148 if env_registry::flag("RLX_CACHE_PARAM_INVARIANT") {
149 self.cache_param_invariant = true;
150 }
151 if env_registry::flag("RLX_DISABLE_MPSGRAPH") {
152 self.disable_mpsgraph = true;
153 }
154 self.kernel_dispatch = KernelDispatchConfig::from_env();
155 if env_registry::flag("RLX_LINT_NUMERICS") {
156 self.lint_numerics = true;
157 }
158 if env_registry::flag("RLX_FUSION_REPORT") {
159 self.fusion_report = true;
160 }
161 if env_registry::flag("RLX_DISABLE_CONV_BIAS_ACT_FUSION") {
162 self.disable_conv_bias_act_fusion = true;
163 }
164 if env_registry::flag("RLX_NO_IO_PEAKS_OUTPUT") {
165 self.no_io_peaks_output = true;
166 }
167 }
168
169 pub fn new() -> Self {
170 Self::from_env()
171 }
172
173 pub fn precision(mut self, p: Precision) -> Self {
174 self.precision = p;
175 self
176 }
177 pub fn policy(mut self, p: PrecisionPolicy) -> Self {
178 self.policy = Some(p);
179 self
180 }
181 pub fn scaled_quant(mut self, cfg: ScaledQuantConfig) -> Self {
186 self.scaled_quant = Some(cfg);
187 self
188 }
189 pub fn rng(mut self, rng: rlx_ir::RngOptions) -> Self {
190 self.rng = rng;
191 self
192 }
193 pub fn rng_backend(mut self, backend: rlx_ir::RngBackend) -> Self {
194 self.rng.backend = backend;
195 self
196 }
197 pub fn rng_seed(mut self, seed: u64) -> Self {
198 self.rng.seed = seed;
199 self
200 }
201 pub fn no_policy(mut self) -> Self {
202 self.policy = None;
203 self
204 }
205 pub fn with_dce(mut self, on: bool) -> Self {
206 self.dce = on;
207 self
208 }
209 pub fn with_constant_folding(mut self, on: bool) -> Self {
210 self.constant_folding = on;
211 self
212 }
213 pub fn with_verbose(mut self, on: bool) -> Self {
214 self.verbose = on;
215 self
216 }
217 pub fn fusion_target(mut self, target: FusionTarget) -> Self {
218 self.fusion_target = Some(target);
219 self
220 }
221 pub fn fusion_opts(mut self, opts: FusionOptions) -> Self {
222 self.fusion_opts = opts;
223 self
224 }
225 pub fn arena_alignment(mut self, bytes: usize) -> Self {
226 self.arena_alignment = bytes;
227 self
228 }
229 pub fn supported_ops(mut self, ops: &'static [OpKind]) -> Self {
230 self.supported_ops = Some(ops);
231 self
232 }
233 pub fn assert_fusion_clean(mut self, on: bool) -> Self {
234 self.assert_fusion_clean = on;
235 self
236 }
237 pub fn dim_binding(mut self, binding: rlx_ir::DimBinding) -> Self {
238 self.dim_binding = Some(binding);
239 self
240 }
241 pub fn param_bindings(mut self, bindings: HashMap<String, Vec<f32>>) -> Self {
242 self.param_bindings = Some(bindings);
243 self
244 }
245 pub fn quant_param_bindings(mut self, bindings: HashMap<String, Vec<u8>>) -> Self {
247 self.quant_param_bindings = Some(bindings);
248 self
249 }
250 pub fn kernel_dispatch(mut self, policy: KernelDispatchPolicy) -> Self {
251 self.kernel_dispatch.policy = policy;
252 self
253 }
254
255 pub fn kernel_dispatch_config(mut self, config: KernelDispatchConfig) -> Self {
256 self.kernel_dispatch = config;
257 self
258 }
259
260 pub fn scan_unroll_max_length(mut self, max: u32) -> Self {
262 self.scan_unroll_max_length = max;
263 self
264 }
265
266 pub fn force_common_kinds(mut self, kinds: &'static [OpKind]) -> Self {
268 self.kernel_dispatch.force_common_kinds = kinds;
269 self
270 }
271
272 pub fn force_native_kinds(mut self, kinds: &'static [OpKind]) -> Self {
274 self.kernel_dispatch.force_native_kinds = kinds;
275 self
276 }
277
278 pub fn with_lint_numerics(mut self, on: bool) -> Self {
279 self.lint_numerics = on;
280 self
281 }
282
283 pub fn with_fusion_report(mut self, on: bool) -> Self {
284 self.fusion_report = on;
285 self
286 }
287
288 pub fn with_disable_mpsgraph(mut self, on: bool) -> Self {
289 self.disable_mpsgraph = on;
290 self
291 }
292
293 pub fn with_cache_param_invariant(mut self, on: bool) -> Self {
294 self.cache_param_invariant = on;
295 self
296 }
297
298 pub fn with_no_io_peaks_output(mut self, on: bool) -> Self {
299 self.no_io_peaks_output = on;
300 self
301 }
302
303 pub fn with_disable_conv_bias_act_fusion(mut self, on: bool) -> Self {
304 self.disable_conv_bias_act_fusion = on;
305 self
306 }
307}