car-inference 0.56.1

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
//! Where the Hugging Face hub cache is — one answer for every reader and writer.
//!
//! CAR reaches the hub cache three ways: hf-hub (Rust) downloads, readers that
//! look for a ready snapshot, and Python runtimes (mlx-vlm, mflux, ltx-2-mlx,
//! mlx-audio, vllm-mlx) that download on their own. They disagreed. hf-hub's
//! `Api::new()` always wrote `~/.cache/huggingface/hub` while CAR's readers
//! honored `HF_HOME`, so with `HF_HOME` set CAR downloaded to one place and
//! looked in another; the Python side also honors `HF_HUB_CACHE`, which no Rust
//! code read.
//!
//! [`hub_dir`] follows `huggingface_hub/constants.py`: `HF_HUB_CACHE`, then the
//! legacy `HUGGINGFACE_HUB_CACHE`, then `<HF_HOME>/hub`, where `HF_HOME`
//! defaults to `$XDG_CACHE_HOME/huggingface` and then `~/.cache/huggingface`.
//! Each value is expanded as Python's `expandvars(expanduser(…))` does — `~`
//! first, then `$NAME` / `${NAME}` (and `%NAME%` on Windows). Two deliberate
//! differences, both toward safety:
//!
//! - an **empty** variable counts as unset (Python's `os.getenv(name, default)`
//!   would take `""` and produce a relative `hub`);
//! - the result is always **absolute**: a relative value is anchored to the
//!   current directory once, here, and that absolute path is what every hf-hub
//!   client writes and what every Python subprocess is handed as
//!   `HF_HUB_CACHE` ([`apply_to`]). A relative value would otherwise resolve
//!   against each process's own working directory — and a later delete would
//!   act relative to the daemon's.
//!
//! `~user` is not looked up (CAR has no password-database access here); it is
//! kept as written and then anchored like any relative path.

use std::path::{Path, PathBuf};

/// The hub cache directory (`…/hub`), absolute.
pub fn hub_dir() -> PathBuf {
    absolute(resolve(&env, home_dir().as_deref()))
}

/// `HF_HOME`: the directory holding `hub/` (by default), `token` and `xet/`.
pub fn hf_home() -> PathBuf {
    absolute(resolve_home(&env, home_dir().as_deref()))
}

/// The cache directory of one repo, `<hub>/models--<org>--<name>`. For reading;
/// see [`checked_repo_dir_in`] before deleting anything.
pub fn repo_dir(repo_id: &str) -> PathBuf {
    repo_dir_in(&hub_dir(), repo_id)
}

/// [`repo_dir`] under an explicit hub directory.
pub fn repo_dir_in(hub: &Path, repo_id: &str) -> PathBuf {
    hub.join(format!("models--{}", repo_id.replace('/', "--")))
}

/// [`repo_dir_in`] for a repo id that matches the Hub's grammar —
/// `name` or `owner/name`, each `[A-Za-z0-9._-]+` and neither `.` nor `..` —
/// so the result is exactly one `models--…` component under `hub`. `None` for
/// anything else; a path built for deletion must come from here.
pub fn checked_repo_dir_in(hub: &Path, repo_id: &str) -> Option<PathBuf> {
    let part_ok = |p: &str| {
        !p.is_empty()
            && p != "."
            && p != ".."
            && p.chars()
                .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
    };
    let mut parts = repo_id.split('/');
    let ok = match (parts.next(), parts.next(), parts.next()) {
        (Some(name), None, None) => part_ok(name),
        (Some(owner), Some(name), None) => part_ok(owner) && part_ok(name),
        _ => false,
    };
    ok.then(|| repo_dir_in(hub, repo_id))
}

/// An hf-hub client that writes into [`hub_dir`], with the endpoint and token
/// the Python library would use: `HF_ENDPOINT`, and the token from `HF_TOKEN`,
/// then the file at `HF_TOKEN_PATH` (default `<HF_HOME>/token`). Built from an
/// explicit cache rather than `ApiBuilder::from_env`, whose token comes from
/// the unexpanded `HF_HOME` and whose default cache panics without a home.
pub fn api() -> hf_hub::api::tokio::ApiBuilder {
    let mut builder = hf_hub::api::tokio::ApiBuilder::from_cache(hf_hub::Cache::new(hub_dir()))
        .with_token(token());
    if let Some(endpoint) = env("HF_ENDPOINT") {
        builder = builder.with_endpoint(endpoint);
    }
    builder
}

/// Point a Python subprocess at the same hub cache CAR reads and writes.
pub fn apply_to(cmd: &mut tokio::process::Command) {
    cmd.env("HF_HUB_CACHE", hub_dir());
}

/// [`apply_to`] for a synchronous command.
pub fn apply_to_std(cmd: &mut std::process::Command) {
    cmd.env("HF_HUB_CACHE", hub_dir());
}

/// A non-empty environment variable.
fn env(name: &str) -> Option<String> {
    std::env::var(name).ok().filter(|v| !v.is_empty())
}

fn token() -> Option<String> {
    if let Some(token) = env("HF_TOKEN") {
        return Some(token);
    }
    let path = match env("HF_TOKEN_PATH") {
        Some(path) => absolute(expand(&path, &env, home_dir().as_deref())),
        None => hf_home().join("token"),
    };
    std::fs::read_to_string(path)
        .ok()
        .map(|t| t.trim().to_string())
        .filter(|t| !t.is_empty())
}

/// The user's home, as Python's `expanduser` finds it: `HOME` on unix (when
/// set and non-empty), `USERPROFILE` on Windows (Python ignores `HOME` there
/// since 3.8), else the OS account database.
fn home_dir() -> Option<PathBuf> {
    let var = if cfg!(windows) { "USERPROFILE" } else { "HOME" };
    env(var)
        .map(PathBuf::from)
        .or_else(std::env::home_dir)
        .filter(|home| home.is_absolute())
}

fn absolute(path: PathBuf) -> PathBuf {
    if path.is_absolute() {
        return path;
    }
    std::path::absolute(&path).unwrap_or(path)
}

fn resolve(env: &dyn Fn(&str) -> Option<String>, home: Option<&Path>) -> PathBuf {
    for name in ["HF_HUB_CACHE", "HUGGINGFACE_HUB_CACHE"] {
        if let Some(value) = env(name) {
            return expand(&value, env, home);
        }
    }
    // Python expands the defaulted hub path again on its way through
    // `HUGGINGFACE_HUB_CACHE`; an already-expanded path is unchanged by that.
    resolve_home(env, home).join("hub")
}

fn resolve_home(env: &dyn Fn(&str) -> Option<String>, home: Option<&Path>) -> PathBuf {
    if let Some(value) = env("HF_HOME") {
        return expand(&value, env, home);
    }
    let cache = match env("XDG_CACHE_HOME") {
        Some(xdg) => PathBuf::from(xdg),
        None => home.unwrap_or(Path::new(".")).join(".cache"),
    };
    expand(&cache.join("huggingface").to_string_lossy(), env, home)
}

/// Python's `expandvars(expanduser(value))`: a leading `~` becomes home first,
/// then `$NAME` and `${NAME}` (and `%NAME%` on Windows) are replaced when set
/// and left as written when not.
fn expand(value: &str, env: &dyn Fn(&str) -> Option<String>, home: Option<&Path>) -> PathBuf {
    let tilde = match (value.strip_prefix('~'), home) {
        // `\\` separates only on Windows, as in `ntpath.expanduser`.
        (Some(tail), Some(home))
            if tail.is_empty()
                || tail.starts_with('/')
                || (cfg!(windows) && tail.starts_with('\\')) =>
        {
            home.join(tail.trim_start_matches(['/', '\\']))
                .to_string_lossy()
                .into_owned()
        }
        _ => value.to_string(),
    };
    let dollar = expand_dollar(&tilde, env);
    PathBuf::from(if cfg!(windows) {
        expand_percent(&dollar, env)
    } else {
        dollar
    })
}

fn expand_dollar(value: &str, env: &dyn Fn(&str) -> Option<String>) -> String {
    let mut out = String::with_capacity(value.len());
    let mut rest = value;
    while let Some(i) = rest.find('$') {
        out.push_str(&rest[..i]);
        let after = &rest[i + 1..];
        let (name, consumed) = match after.strip_prefix('{') {
            Some(braced) => match braced.find('}') {
                Some(end) => (&braced[..end], end + 2),
                None => ("", 0),
            },
            None => {
                let end = after
                    .find(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
                    .unwrap_or(after.len());
                (&after[..end], end)
            }
        };
        match env(name).filter(|_| !name.is_empty()) {
            Some(v) => out.push_str(&v),
            None => out.push_str(&rest[i..i + 1 + consumed]),
        }
        // A bare `$` or an unterminated `${` consumed nothing and was kept.
        rest = &rest[i + 1 + consumed..];
    }
    out.push_str(rest);
    out
}

/// Windows `%NAME%`, as `ntpath.expandvars` reads it.
fn expand_percent(value: &str, env: &dyn Fn(&str) -> Option<String>) -> String {
    let mut out = String::with_capacity(value.len());
    let mut rest = value;
    while let Some(i) = rest.find('%') {
        out.push_str(&rest[..i]);
        let after = &rest[i + 1..];
        match after.find('%') {
            Some(end) if end > 0 => {
                let name = &after[..end];
                match env(name) {
                    Some(v) => out.push_str(&v),
                    None => out.push_str(&rest[i..i + end + 2]),
                }
                rest = &after[end + 1..];
            }
            _ => {
                out.push('%');
                rest = after;
            }
        }
    }
    out.push_str(rest);
    out
}

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

    fn with(vars: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> {
        let map: HashMap<String, String> = vars
            .iter()
            .filter(|(_, v)| !v.is_empty())
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect();
        move |name| map.get(name).cloned()
    }

    const HOME: &str = "/Users/someone";

    fn hub(vars: &[(&str, &str)]) -> PathBuf {
        resolve(&with(vars), Some(Path::new(HOME)))
    }

    fn home_of(vars: &[(&str, &str)]) -> PathBuf {
        resolve_home(&with(vars), Some(Path::new(HOME)))
    }

    /// The order `huggingface_hub/constants.py` uses, case by case.
    #[test]
    fn resolves_the_hub_the_way_the_python_library_does() {
        assert_eq!(
            hub(&[]),
            PathBuf::from("/Users/someone/.cache/huggingface/hub")
        );
        assert_eq!(
            hub(&[("XDG_CACHE_HOME", "/xdg")]),
            PathBuf::from("/xdg/huggingface/hub")
        );
        assert_eq!(
            hub(&[("HF_HOME", "/hf"), ("XDG_CACHE_HOME", "/xdg")]),
            PathBuf::from("/hf/hub")
        );
        assert_eq!(
            hub(&[("HUGGINGFACE_HUB_CACHE", "/legacy"), ("HF_HOME", "/hf")]),
            PathBuf::from("/legacy")
        );
        assert_eq!(
            hub(&[
                ("HF_HUB_CACHE", "/hub"),
                ("HUGGINGFACE_HUB_CACHE", "/legacy"),
                ("HF_HOME", "/hf"),
            ]),
            PathBuf::from("/hub")
        );
        assert_eq!(
            home_of(&[("HF_HOME", "~/hf")]),
            PathBuf::from("/Users/someone/hf")
        );
        assert_eq!(
            home_of(&[("XDG_CACHE_HOME", "~/xdg")]),
            PathBuf::from("/Users/someone/xdg/huggingface")
        );
    }

    /// An empty variable is unset — deliberately, where Python would take `""`
    /// and produce a relative `hub`.
    #[test]
    fn empty_variables_count_as_unset() {
        assert_eq!(
            hub(&[("HF_HUB_CACHE", ""), ("HF_HOME", "/hf")]),
            PathBuf::from("/hf/hub")
        );
        assert_eq!(
            hub(&[("HF_HOME", "")]),
            PathBuf::from("/Users/someone/.cache/huggingface/hub")
        );
    }

    #[test]
    fn expands_home_then_variables_as_python_does() {
        assert_eq!(
            hub(&[("HF_HOME", "~/hf")]),
            PathBuf::from("/Users/someone/hf/hub")
        );
        assert_eq!(
            hub(&[("HF_HUB_CACHE", "~")]),
            PathBuf::from("/Users/someone")
        );
        assert_eq!(
            hub(&[("HF_HUB_CACHE", "$ROOT/cache"), ("ROOT", "/data")]),
            PathBuf::from("/data/cache")
        );
        assert_eq!(
            hub(&[("HF_HUB_CACHE", "${ROOT}/cache"), ("ROOT", "/data")]),
            PathBuf::from("/data/cache")
        );
        // `~` is expanded before variables, so a variable holding `~` stays.
        assert_eq!(
            hub(&[("HF_HUB_CACHE", "$T/x"), ("T", "~")]),
            PathBuf::from("~/x")
        );
        // Unset, empty-named, bare and unterminated forms are kept as written.
        assert_eq!(
            hub(&[("HF_HUB_CACHE", "$NOPE/c")]),
            PathBuf::from("$NOPE/c")
        );
        assert_eq!(
            hub(&[("HF_HUB_CACHE", "/a/${}/b")]),
            PathBuf::from("/a/${}/b")
        );
        assert_eq!(hub(&[("HF_HUB_CACHE", "/a$")]), PathBuf::from("/a$"));
        assert_eq!(
            hub(&[("HF_HUB_CACHE", "/a/${OPEN")]),
            PathBuf::from("/a/${OPEN")
        );
        // `~user` is not looked up.
        assert_eq!(
            hub(&[("HF_HUB_CACHE", "~root/x")]),
            PathBuf::from("~root/x")
        );
    }

    #[test]
    fn percent_variables_expand_as_windows_does() {
        let env = with(&[("LOCALAPPDATA", "C:\\Users\\me\\AppData\\Local")]);
        assert_eq!(
            expand_percent("%LOCALAPPDATA%\\hf", &env),
            "C:\\Users\\me\\AppData\\Local\\hf"
        );
        assert_eq!(expand_percent("%NOPE%\\hf", &env), "%NOPE%\\hf");
        assert_eq!(expand_percent("50%", &env), "50%");
    }

    /// With no home at all, the default is anchored rather than left relative,
    /// so no process resolves it against its own working directory.
    #[test]
    fn no_home_still_yields_an_absolute_hub() {
        let relative = resolve(&with(&[]), None);
        assert!(!relative.is_absolute(), "{relative:?}");
        assert!(relative.ends_with(".cache/huggingface/hub"));
        assert!(absolute(relative).is_absolute());
        assert!(absolute(PathBuf::from("rel/hub")).is_absolute());
    }

    #[test]
    fn a_repo_dir_is_the_hub_layout() {
        assert_eq!(
            repo_dir_in(Path::new("/h"), "mlx-community/Qwen3-4B-4bit"),
            PathBuf::from("/h/models--mlx-community--Qwen3-4B-4bit")
        );
    }

    /// A path built for deletion is exactly one `models--…` component.
    #[test]
    fn checked_repo_dirs_accept_only_hub_repo_ids() {
        let hub = Path::new("/h");
        assert_eq!(
            checked_repo_dir_in(hub, "mlx-community/Qwen3-4B-4bit"),
            Some(PathBuf::from("/h/models--mlx-community--Qwen3-4B-4bit"))
        );
        assert_eq!(
            checked_repo_dir_in(hub, "gpt2"),
            Some(PathBuf::from("/h/models--gpt2"))
        );
        for bad in [
            "", "/", "a/", "/b", "a/b/c", "..", "a/..", ".", "a\\..\\x", "a b/c", "a/b:c",
        ] {
            assert_eq!(checked_repo_dir_in(hub, bad), None, "{bad:?}");
        }
    }
}