1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! External Flux image backend — shells out to `mflux-generate`.
//!
//! **Not a temporary bridge any more.** This module was written as one, while
//! the native Rust port collapsed every prompt to near-white output; that is
//! fixed and `mlx_flux.rs` now reaches prompt-faithful parity, so the old
//! header describing this as a stopgap was stale and actively misleading — it
//! reads as "the native path is broken" long after it stopped being true.
//!
//! What this module is now is the **architecture escape hatch**, the image
//! counterpart of `vllm_pool` for text. `mlx_flux.rs` implements exactly one
//! architecture (Flux.1-lite-8B). mflux implements many — FLUX.2, Krea 2,
//! Qwen-Image, Z-Image, Fibo — and gains more without CAR writing any Rust.
//! [`native_backend_serves`] decides between them on what the requested model
//! actually needs.
//!
//! Dispatch is by capability, not by an env toggle. `CAR_IMAGE_BACKEND` used to
//! choose the backend and was removed: it existed to A/B the Rust port during
//! the parity migration, outlived that migration, and defaulted to `native`
//! unconditionally — so asking for a FLUX.2 checkpoint silently routed to a
//! backend that cannot load it.
//!
//! The subprocess call stays at the feature boundary (command → PNG path on
//! disk), never an internal runtime dependency.
use Command;
use crate;
use crateInferenceError;
const CLI_BINARY: &str = "mflux-generate";
/// Default Flux checkpoint to pass via `--model`. Same quantized weights
/// the native `mlx_flux` backend loads — so no additional download cost
/// when swapping backends.
const DEFAULT_MODEL: &str = "mlx-community/Flux-1.lite-8B-MLX-Q4";
/// mflux needs a `--base-model` hint for custom `--model` paths; the
/// Flux.1-lite checkpoint is a distilled variant of Flux.1-dev, so `dev`
/// is the right architecture family.
const DEFAULT_BASE_MODEL: &str = "dev";
/// Checkpoints the native Rust `mlx_flux` backend implements.
///
/// `mlx_flux.rs` is a hand-written implementation of the **Flux.1-lite-8B**
/// architecture against these exact quantized weights — it is not a general
/// Flux loader. Anything else (FLUX.2, Krea 2, Qwen-Image, Z-Image, Fibo) has
/// no Rust implementation and must go through mflux.
const NATIVE_SERVED_MODELS: & = &;
/// Whether the native Rust backend can serve `model` (`None` = CAR's default).
///
/// This replaces the old `CAR_IMAGE_BACKEND` env toggle. The toggle existed to
/// A/B the Rust port against mflux while the port was reaching parity; parity
/// landed, and the toggle outlived it. Worse, it defaulted to `native`
/// unconditionally, so asking for a FLUX.2 checkpoint silently routed to a
/// backend that cannot load that architecture. Letting the model decide is the
/// same rule the text path uses in `backend::local::has_native_backend`.
/// mflux's own built-in model identifiers, from `mflux-generate --base-model`.
///
/// When the caller names one of these it *is* the base model: mflux resolves
/// the checkpoint itself and no `--model` path is needed. Pairing one with the
/// hardcoded `dev` hint describes a FLUX.2 or Krea checkpoint as Flux.1-dev,
/// which is why every architecture past Flux.1 was unreachable through this
/// bridge even though the installed mflux already supported them.
const BUILTIN_BASE_MODELS: & = &;
/// Whether `model` is one of mflux's built-in identifiers rather than a
/// HuggingFace repo path.
/// Absolute path to `mflux-generate`, preferring CAR's managed runtime — but
/// only a copy that actually runs.
///
/// Two failure modes, and fixing one naively causes the other:
///
/// - `Command::new("mflux-generate")` searches PATH only, so on a machine where
/// CAR provisioned the venv but the user never ran `uv tool install mflux`,
/// availability probed false and image generation fell back to the native
/// backend — which this bridge exists precisely because it is broken.
/// - Preferring the managed copy *unconditionally* is equally wrong: the
/// sibling `ltx-2-mlx` in this very runtime raises `ModuleNotFoundError: No
/// module named 'ltx_core_mlx'` while the PATH install works fine. A
/// dependency-broken venv entry point exists on disk and fails on import.
///
/// So preference is by *working*, not by existing: probe the managed copy and
/// fall back to PATH when it cannot run. Same best-available rule as the
/// vllm-mlx binary, keyed on functioning rather than version.
/// The managed runtime's copy of the CLI, if the file is present.
/// Whether `bin` executes successfully. A venv entry point whose imports are
/// broken exits non-zero here despite existing on disk.
/// The `mflux-generate` command for one image, with the hub cache and token.