car-inference 0.14.0

Local model inference for CAR — Candle backend with Qwen3 models
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Local VLM image inference via the `mlx_vlm.generate` Python CLI.
//!
//! Background — issue #115. CAR's two existing local Qwen2.5-VL paths both
//! fail on image inputs today:
//!
//! - **Native MLX (`backend::mlx`)** loads only the `language_model.*`
//!   prefix of a VL checkpoint, so it's a text-only tower. Image content
//!   blocks are rejected up-front with a `UnsupportedMode { mode:
//!   "image-content-block", backend: "native-mlx-text", ... }` error.
//! - **vLLM-MLX HTTP (`backend::remote_backend` via `ModelSource::VllmMlx`)**
//!   forwards an OpenAI-compatible multimodal request, which the upstream
//!   `mlx_vlm.server` honors by erroring with `RuntimeError: There is no
//!   Stream(gpu, 0) in current thread`. Server-side bug, not CAR's.
//!
//! Direct invocation of `mlx_vlm.generate` (the same Python tool) works
//! end-to-end for the same checkpoints. So this module shells out to it
//! the way `external_flux` shells out to `mflux-generate`: subprocess at
//! the feature boundary, env probing, errors that name the install
//! command. Apple Silicon only (mlx-vlm itself is `cfg(target_os =
//! "macos", target_arch = "aarch64")`).

use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;

use base64::Engine as _;

use crate::ContentBlock;
use crate::InferenceError;

const EXECUTABLE_FORMS: &[&str] = &["mlx_vlm.generate", "mlx_vlm"];
const MODULE_FORMS: &[&str] = &["mlx_vlm.generate", "mlx_vlm"];

#[derive(Debug, Clone)]
pub struct CliInvocation {
    program: PathBuf,
    args: Vec<String>,
    label: String,
    python: Option<PathBuf>,
}

#[derive(Debug, Clone)]
pub enum RuntimeStatus {
    Available(CliInvocation),
    MissingCli {
        searched: Vec<PathBuf>,
    },
    MissingDeps {
        invocation: CliInvocation,
        detail: String,
    },
}

impl RuntimeStatus {
    pub fn is_available(&self) -> bool {
        matches!(self, RuntimeStatus::Available(_))
    }

    pub fn user_message(&self) -> String {
        match self {
            RuntimeStatus::Available(invocation) => {
                format!("mlx-vlm CLI available at {}", invocation.label)
            }
            RuntimeStatus::MissingCli { searched } => {
                let searched = searched
                    .iter()
                    .map(|path| path.display().to_string())
                    .collect::<Vec<_>>()
                    .join(", ");
                format!(
                    "mlx-vlm CLI not found. Install with `uv tool install mlx-vlm` \
                     (or `pip install mlx-vlm`). CAR searched PATH plus common \
                     tool locations: {searched}"
                )
            }
            RuntimeStatus::MissingDeps { invocation, detail } => {
                format!(
                    "mlx-vlm CLI found at {}, but its Python environment is missing \
                     runtime dependencies required by Qwen3-VL processors: {detail}. \
                     Install them with `uv pip install --python {} torch torchvision`.",
                    invocation.label,
                    invocation
                        .python
                        .as_ref()
                        .map(|path| path.display().to_string())
                        .unwrap_or_else(|| "<mlx-vlm python>".to_string())
                )
            }
        }
    }
}

impl CliInvocation {
    fn command(&self) -> Command {
        let mut cmd = Command::new(&self.program);
        cmd.args(&self.args);
        cmd
    }
}

pub fn runtime_status() -> RuntimeStatus {
    let mut searched = Vec::new();
    let Some(invocation) = locate_invocation(&mut searched) else {
        return RuntimeStatus::MissingCli { searched };
    };
    if let Some(detail) = missing_processor_deps(&invocation) {
        RuntimeStatus::MissingDeps { invocation, detail }
    } else {
        RuntimeStatus::Available(invocation)
    }
}

/// Probe whether mlx-vlm is reachable. Cheap (`--help` invocations plus a
/// dependency import check). Returns the invocation that worked so callers can
/// reuse it without re-probing.
pub fn locate() -> Option<CliInvocation> {
    match runtime_status() {
        RuntimeStatus::Available(invocation) => Some(invocation),
        _ => None,
    }
}

pub fn is_available() -> bool {
    runtime_status().is_available()
}

/// Run a single VLM image inference. Writes any base64-embedded images
/// to temp files (mlx-vlm only accepts paths or URLs, not data URIs)
/// and invokes the CLI with the resulting `--image` arguments.
///
/// Returns the model's full text reply on success.
pub fn generate(
    hf_repo: &str,
    prompt: &str,
    images: &[ContentBlock],
    temperature: f64,
    max_tokens: usize,
) -> Result<String, InferenceError> {
    if images.is_empty() {
        return Err(InferenceError::InferenceFailed(
            "mlx_vlm CLI route invoked without any image content blocks; \
             callers must check has_images before dispatching here"
                .into(),
        ));
    }

    let invocation = match runtime_status() {
        RuntimeStatus::Available(invocation) => invocation,
        status => return Err(InferenceError::InferenceFailed(status.user_message())),
    };

    // mlx-vlm wants paths on disk. Write any base64-embedded images to a
    // temp dir; URL-form blocks pass straight through. Lifetime of the
    // temp dir matches this function (RAII drop on return).
    let tmp_dir = tempfile::tempdir().map_err(|e| {
        InferenceError::InferenceFailed(format!(
            "mlx_vlm: failed to create tempdir for image staging: {e}"
        ))
    })?;
    let mut image_args: Vec<String> = Vec::with_capacity(images.len());
    for (idx, block) in images.iter().enumerate() {
        match block {
            ContentBlock::ImageBase64 { data, media_type } => {
                let ext = match media_type.as_str() {
                    "image/png" => "png",
                    "image/jpeg" | "image/jpg" => "jpg",
                    "image/webp" => "webp",
                    "image/gif" => "gif",
                    // Be conservative: unrecognized media type → fall
                    // back to the bytes-as-png path. mlx-vlm sniffs by
                    // content, not extension, so this is harmless.
                    _ => "png",
                };
                let bytes = base64::engine::general_purpose::STANDARD
                    .decode(data)
                    .map_err(|e| {
                        InferenceError::InferenceFailed(format!(
                            "mlx_vlm: image #{idx} base64 decode failed: {e}"
                        ))
                    })?;
                let path = tmp_dir.path().join(format!("img_{idx}.{ext}"));
                let mut f = std::fs::File::create(&path).map_err(|e| {
                    InferenceError::InferenceFailed(format!(
                        "mlx_vlm: write staged image to {}: {e}",
                        path.display()
                    ))
                })?;
                f.write_all(&bytes).map_err(|e| {
                    InferenceError::InferenceFailed(format!(
                        "mlx_vlm: write staged image to {}: {e}",
                        path.display()
                    ))
                })?;
                image_args.push(path.to_string_lossy().into_owned());
            }
            ContentBlock::ImageUrl { url, .. } => {
                image_args.push(url.clone());
            }
            // Text blocks belong in the prompt, not in --image. Skip
            // silently — caller is responsible for prompt assembly.
            // Video/audio blocks are not understood by mlx_vlm.generate;
            // the dispatch in lib.rs already rejects video+audio against
            // this backend before we get here, so warn-and-skip is safe
            // and prevents a partial/confusing CLI invocation if
            // dispatch ever drifts.
            other => {
                tracing::warn!(
                    block = ?other,
                    "mlx_vlm CLI: ignoring non-image content block; only Text + Image* are accepted by mlx_vlm.generate"
                );
            }
        }
    }

    let mut cmd = invocation.command();
    cmd.arg("--model").arg(hf_repo);
    for path in &image_args {
        cmd.arg("--image").arg(path);
    }
    cmd.arg("--prompt").arg(prompt);
    cmd.arg("--max-tokens").arg(max_tokens.to_string());
    if temperature.is_finite() && temperature >= 0.0 {
        cmd.arg("--temperature").arg(format!("{temperature}"));
    }

    tracing::info!(
        repo = hf_repo,
        images = image_args.len(),
        max_tokens,
        "mlx_vlm CLI: invoking"
    );

    let output = cmd.output().map_err(|e| {
        InferenceError::InferenceFailed(format!(
            "mlx_vlm CLI failed to spawn ({}): {e}. \
             Reinstall with `uv tool install mlx-vlm`.",
            invocation.label
        ))
    })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        if let Some(detail) = classify_missing_deps(&stderr) {
            return Err(InferenceError::InferenceFailed(format!(
                "mlx_vlm found at {} but missing runtime dependencies: {detail}",
                invocation.label
            )));
        }
        return Err(InferenceError::InferenceFailed(format!(
            "mlx_vlm exited with status {}: {}",
            output.status,
            stderr.trim()
        )));
    }

    let text = parse_output(&String::from_utf8_lossy(&output.stdout));
    if text.trim().is_empty() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(InferenceError::InferenceFailed(format!(
            "mlx_vlm produced empty output. stderr: {}",
            stderr.trim()
        )));
    }
    Ok(text)
}

fn locate_invocation(searched: &mut Vec<PathBuf>) -> Option<CliInvocation> {
    for exe in EXECUTABLE_FORMS {
        if let Some(path) = find_executable(exe, searched) {
            let mut invocation = CliInvocation {
                program: path.clone(),
                args: Vec::new(),
                label: path.display().to_string(),
                python: python_from_shebang(&path),
            };
            if help_succeeds(&invocation) {
                return Some(invocation);
            }
            invocation.args.clear();
        }
    }

    for python in python_candidates(searched) {
        for module in MODULE_FORMS {
            let invocation = CliInvocation {
                program: python.clone(),
                args: vec!["-m".to_string(), (*module).to_string()],
                label: format!("{} -m {module}", python.display()),
                python: Some(python.clone()),
            };
            if help_succeeds(&invocation) {
                return Some(invocation);
            }
        }
    }
    None
}

fn help_succeeds(invocation: &CliInvocation) -> bool {
    let mut cmd = invocation.command();
    cmd.arg("--help")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

fn find_executable(name: &str, searched: &mut Vec<PathBuf>) -> Option<PathBuf> {
    for dir in executable_search_dirs() {
        let path = dir.join(name);
        searched.push(path.clone());
        if path.exists() && path.is_file() {
            return Some(path);
        }
    }
    None
}

fn executable_search_dirs() -> Vec<PathBuf> {
    let mut dirs: Vec<PathBuf> = std::env::var_os("PATH")
        .map(|paths| std::env::split_paths(&paths).collect())
        .unwrap_or_default();
    if let Some(home) = std::env::var_os("HOME").map(PathBuf::from) {
        dirs.push(home.join(".local").join("bin"));
        dirs.push(
            home.join(".local")
                .join("share")
                .join("uv")
                .join("tools")
                .join("mlx-vlm")
                .join("bin"),
        );
        dirs.push(home.join(".car").join("visual-runtime").join("bin"));
    }
    dedupe_paths(dirs)
}

fn python_candidates(searched: &mut Vec<PathBuf>) -> Vec<PathBuf> {
    let mut candidates = Vec::new();
    if let Ok(path) = std::env::var("CAR_MLX_VLM_PYTHON") {
        candidates.push(PathBuf::from(path));
    }
    if let Some(home) = std::env::var_os("HOME").map(PathBuf::from) {
        candidates.push(
            home.join(".local")
                .join("share")
                .join("uv")
                .join("tools")
                .join("mlx-vlm")
                .join("bin")
                .join("python"),
        );
        candidates.push(
            home.join(".car")
                .join("visual-runtime")
                .join("bin")
                .join("python"),
        );
    }
    candidates.extend(
        ["python3", "python"]
            .iter()
            .filter_map(|name| find_executable(name, searched)),
    );
    dedupe_paths(candidates)
        .into_iter()
        .filter(|path| {
            searched.push(path.clone());
            path.exists() || path.components().count() == 1
        })
        .collect()
}

fn dedupe_paths(paths: Vec<PathBuf>) -> Vec<PathBuf> {
    let mut out = Vec::new();
    for path in paths {
        if !out.contains(&path) {
            out.push(path);
        }
    }
    out
}

fn python_from_shebang(path: &Path) -> Option<PathBuf> {
    let bytes = std::fs::read(path).ok()?;
    let first_line = bytes.split(|byte| *byte == b'\n').next()?;
    let line = std::str::from_utf8(first_line).ok()?.trim();
    let shebang = line.strip_prefix("#!")?.trim();
    if shebang.contains("python") {
        Some(PathBuf::from(shebang.split_whitespace().next()?))
    } else {
        None
    }
}

fn missing_processor_deps(invocation: &CliInvocation) -> Option<String> {
    let python = invocation.python.as_ref()?;
    let output = Command::new(python)
        .args(["-c", "import torch, torchvision"])
        .output()
        .ok()?;
    if output.status.success() {
        return None;
    }
    classify_missing_deps(&String::from_utf8_lossy(&output.stderr))
        .or_else(|| Some(String::from_utf8_lossy(&output.stderr).trim().to_string()))
}

fn classify_missing_deps(stderr: &str) -> Option<String> {
    let lower = stderr.to_ascii_lowercase();
    let mut missing = Vec::new();
    if lower.contains("no module named 'torch'")
        || lower.contains("no module named torch")
        || lower.contains("pytorch library but it was not found")
    {
        missing.push("torch");
    }
    if lower.contains("no module named 'torchvision'")
        || lower.contains("no module named torchvision")
        || lower.contains("torchvision library but it was not found")
    {
        missing.push("torchvision");
    }
    if missing.is_empty() {
        None
    } else {
        Some(missing.join(", "))
    }
}

/// Strip mlx-vlm's framing. Every successful run prints a banner like
/// `==========`, then the generated text, then performance lines such
/// as `Prompt: N tokens`, `Peak memory: X.YYY GB`, etc. Keep only the
/// content between banners.
fn parse_output(stdout: &str) -> String {
    let mut in_body = false;
    let mut body: Vec<&str> = Vec::new();
    for line in stdout.lines() {
        let trimmed = line.trim_end();
        // Treat a line of '=' (with optional whitespace) as a banner.
        if !trimmed.is_empty() && trimmed.chars().all(|c| c == '=' || c.is_whitespace()) {
            if !in_body {
                in_body = true;
            } else {
                // Closing banner: stop accumulating; everything after is
                // mlx-vlm's perf summary.
                break;
            }
            continue;
        }
        if in_body {
            body.push(line);
        }
    }
    if body.is_empty() {
        // Older mlx-vlm versions don't print banners — return raw
        // stdout, trimmed.
        return stdout.trim().to_string();
    }
    body.join("\n").trim_end().to_string()
}

/// Where on disk to expect a previously-pulled MLX VL repo. Used by the
/// availability probe so `car models list --capability vision` can
/// distinguish "registered" from "weights actually present".
pub fn cached_repo_path(hf_repo: &str) -> PathBuf {
    // mlx-vlm uses huggingface_hub's standard cache layout. We don't
    // need to honor HF_HUB_CACHE here — `car models pull` will write
    // into the same cache via `huggingface_hub.snapshot_download`.
    let home = std::env::var_os("HOME")
        .map(PathBuf::from)
        .unwrap_or_default();
    home.join(".cache")
        .join("huggingface")
        .join("hub")
        .join(format!("models--{}", hf_repo.replace('/', "--")))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_output_strips_banners_and_perf_lines() {
        let raw = "Loading model...\n\
                   ==========\n\
                   The image is a blank canvas with a grid pattern.\n\
                   ==========\n\
                   Prompt: 234 tokens\n\
                   Generation: 12 tokens, 80.123 tokens/s\n\
                   Peak memory: 2.345 GB";
        assert_eq!(
            parse_output(raw),
            "The image is a blank canvas with a grid pattern."
        );
    }

    #[test]
    fn parse_output_handles_missing_banners() {
        let raw = "  Some single-line response.  \n";
        assert_eq!(parse_output(raw), "Some single-line response.");
    }

    #[test]
    fn parse_output_handles_multiline_body() {
        let raw = "==========\nLine one.\nLine two.\n==========\nPrompt: 1 tokens";
        assert_eq!(parse_output(raw), "Line one.\nLine two.");
    }

    #[test]
    fn classify_missing_deps_distinguishes_processor_imports() {
        let stderr = "ImportError: Qwen3VLVideoProcessor requires the Torchvision library but it was not found in your environment.\n\
                      ModuleNotFoundError: No module named 'torch'";
        assert_eq!(
            classify_missing_deps(stderr),
            Some("torch, torchvision".to_string())
        );
    }
}