Skip to content

Commit 7f0ed08

Browse files
authored
fix: update model express common to use HF_HOME (if specified) (#3638)
1 parent c4abe9b commit 7f0ed08

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

lib/llm/src/hub.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,22 @@ async fn mx_download_direct(model_name: &str, ignore_weights: bool) -> anyhow::R
9090
// TODO: remove in the future. This is a temporary workaround to find common
9191
// cache directory between client and server.
9292
fn get_model_express_cache_dir() -> PathBuf {
93+
// Check HF_HUB_CACHE environment variable
94+
// reference: https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables#hfhubcache
9395
if let Ok(cache_path) = env::var("HF_HUB_CACHE") {
9496
return PathBuf::from(cache_path);
9597
}
9698

99+
// Check HF_HOME environment variable (standard Hugging Face cache directory)
100+
// reference: https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables#hfhome
101+
if let Ok(hf_home) = env::var("HF_HOME") {
102+
return PathBuf::from(hf_home).join("hub");
103+
}
104+
97105
if let Ok(cache_path) = env::var("MODEL_EXPRESS_CACHE_PATH") {
98106
return PathBuf::from(cache_path);
99107
}
108+
100109
let home = env::var("HOME")
101110
.or_else(|_| env::var("USERPROFILE"))
102111
.unwrap_or_else(|_| ".".to_string());
@@ -120,4 +129,21 @@ mod tests {
120129
assert!(!cache_dir.to_string_lossy().is_empty());
121130
assert!(cache_dir.is_absolute() || cache_dir.starts_with("."));
122131
}
132+
133+
#[serial_test::serial]
134+
#[test]
135+
fn test_get_model_express_cache_dir_with_hf_home() {
136+
// Test that HF_HOME is respected when set
137+
unsafe {
138+
// Clear other cache env vars to ensure HF_HOME is tested
139+
env::remove_var("HF_HUB_CACHE");
140+
env::remove_var("MODEL_EXPRESS_CACHE_PATH");
141+
env::set_var("HF_HOME", "/custom/cache/path");
142+
let cache_dir = get_model_express_cache_dir();
143+
assert_eq!(cache_dir, PathBuf::from("/custom/cache/path/hub"));
144+
145+
// Clean up
146+
env::remove_var("HF_HOME");
147+
}
148+
}
123149
}

0 commit comments

Comments
 (0)