tracel-ai/burn · error

Failed to create cache file

Error message

Failed to create cache file

What it means

`download_if_needed` in the afine weights module panics with `File::create(cache_path).expect("Failed to create cache file")` when the cached `afine.pth` cannot be created after a download. The directory exists (it was just ensured by `get_cache_dir`), so failure is typically a permissions issue on the directory or a name collision (e.g. `afine.pth` exists as a directory). It is triggered only when the file is not already cached.

Source

Thrown at crates/burn-train/src/metric/vision/afine/weights.rs:62

/// Cached filename inside `~/.cache/burn-dataset/afine/`.
const CACHE_FILENAME: &str = "afine.pth";

fn get_cache_dir() -> PathBuf {
    let cache_dir = dirs::cache_dir()
        .expect("Could not get cache directory")
        .join("burn-dataset")
        .join("afine");
    if !cache_dir.exists() {
        create_dir_all(&cache_dir).expect("Failed to create cache directory");
    }
    cache_dir
}

fn download_if_needed(url: &str, cache_path: &PathBuf, message: &str) {
    if !cache_path.exists() {
        let bytes = download_file_as_bytes(url, message);
        let mut file = File::create(cache_path).expect("Failed to create cache file");
        file.write_all(&bytes).expect("Failed to write weights");
    }
}

/// Download `afine.pth` (if not cached) and load all six shards into
/// the matching submodules of an `Afine` previously produced by
/// `AfineConfig::init`.
///
/// Errors during loading are logged via `log::warn!` and the function
/// returns the module unconditionally — `allow_partial(true)` plus
/// per-shard regex remapping mean unmapped or unknown checkpoint keys
/// are silently dropped, matching the behaviour of LPIPS, DISTS, and
/// FID's pretrained loaders.
pub(crate) fn load_pretrained_weights(mut afine: Afine) -> Afine {
    let cache_dir = get_cache_dir();
    let cache_path = cache_dir.join(CACHE_FILENAME);
    download_if_needed(AFINE_URL, &cache_path, "Downloading A-FINE weights...");

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Verify the cache directory is writable by the current user: `ls -ld ~/.cache/burn-dataset/afine` and `chown`/`chmod` as needed
  2. Delete a corrupt/stale `afine.pth` entry (`rm -rf ~/.cache/burn-dataset/afine/afine.pth`) so it is recreated
  3. Check free space/quota on the cache filesystem (`df -h ~/.cache`)
  4. Pre-place a valid `afine.pth` in the cache so download is skipped

Example fix

// before (shell)
cargo run -- eval   # panics: Failed to create cache file
// after (shell)
rm -f ~/.cache/burn-dataset/afine/afine.pth
chmod u+w ~/.cache/burn-dataset/afine
cargo run -- eval
Defensive patterns

Strategy: validation

Validate before calling

let cache = dirs::cache_dir().unwrap().join("burn-dataset").join("afine");
let path = cache.join("afine.pth");
// ensure neither a directory nor an unwritable entry blocks creation
if path.is_dir() { eprintln!("{} is a directory; remove it", path.display()); }
assert!(dirs::cache_dir().unwrap().metadata().map(|m| !m.permissions().readonly()).unwrap_or(false));

Type guard

fn cache_file_creatable(dir: &std::path::Path, name: &str) -> bool {
    let p = dir.join(name);
    !p.is_dir() && std::fs::write(dir.join(".probe"), b"").is_ok()
}

Try / catch

let weights = std::panic::catch_unwind(load_pretrained_weights)
    .map_err(|_| anyhow!("failed to create afine.pth cache file; check permissions/quota"))?;

Prevention

When it happens

Trigger: `load_pretrained_weights` (afine) finds no cached `afine.pth`, downloads bytes successfully, but `File::create` fails: read-only or non-writable cache directory, quota exceeded, or `afine.pth` path is an existing directory.

Common situations: Cache directory created by root but process now runs as another user; interrupted prior run left `afine.pth` as a directory or stale artifact; disk quota/full filesystem on the cache volume; read-only bind mount of the cache.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/89738fefe8b12221. Report an issue: GitHub.