tracel-ai/burn · error
Failed to rename temporary file to the actual VGG19 cache fi
Error message
Failed to rename temporary file to the actual VGG19 cache file name for Gram Matrix Loss
What it means
This panic occurs in `download_weights_if_not_saved` when the atomic rename of the freshly downloaded VGG19 weights (written to `vgg19.pth.tmp`) to its final cache path `~/.cache/burn-pretrained-models/loss/vgg19/vgg19.pth` fails. The library writes to a temp file first so that a partial download never masquerades as a complete cache; the rename is the commit step. `std::fs::rename` fails on OS-level issues such as permission problems, the temp file being removed, or the source and destination not being writable.
Source
Thrown at crates/burn-vision/src/loss/gram_matrix/weights.rs:52
/// use the locally cached `.pth` file.
fn download_weights_if_not_saved(cache_path: &PathBuf) {
if !cache_path.exists() {
let bytes = download_file_as_bytes(
VGG19_URL,
"Downloading VGG19 ImageNet weights for Gram Matrix Loss...",
);
// Write to a temporary file. If writing gets completed, then rename to the actual/correct name.
// If writing is not completed, the file with the correct name (i.e. `cache_path`) will not exist
// so this code block can run again which is the desired behavior.
let temp_path = cache_path.with_extension("pth.tmp");
let mut file = File::create(&temp_path)
.expect("Failed to create VGG19 cache file for Gram Matrix Loss");
file.write_all(&bytes)
.expect("Failed to write VGG19 weights to the cache file for Gram Matrix Loss");
rename(temp_path, cache_path)
.expect("Failed to rename temporary file to the actual VGG19 cache file name for Gram Matrix Loss");
}
}
/// Loads ImageNet pretrained weights into the provided VGG19 feature extractor.
///
/// This function downloads the official PyTorch VGG19 weights, remaps the keys
/// from PyTorch's `features.X` format to Burn's `convX_Y` format, and loads
/// them into the module.
///
/// # Arguments
///
/// - `vgg19` - An initialized VGG19 module with random weights.
///
/// # Returns
///
/// The VGG19 module with pretrained ImageNet weights loaded.
pub fn load_vgg19_weights(mut vgg19: Vgg19) -> Vgg19 {
let cache_dir = get_cache_dir();View on GitHub (pinned to d16f7ba2ed)
Solutions
- Check that the cache directory `~/.cache/burn-pretrained-models/loss/vgg19/` is writable by the current user and fix permissions (chmod/chown) or run with appropriate privileges.
- Delete any leftover `vgg19.pth.tmp` and any partially written `vgg19.pth` in the cache dir, then retry `load_vgg19_weights`.
- Verify free disk space / quota; free space if low and retry.
- Avoid running multiple processes that trigger the first-time download simultaneously, or pre-seed the cache by copying a valid vgg19.pth into the cache path so the download path is skipped.
- As a workaround, download https://download.pytorch.org/models/vgg19-dcbb9e9d.pth manually, place it at the cache path, and call the API again.
Example fix
// before (panics on rename failure)
let cache_dir = dirs::cache_dir().expect("...").join("burn-pretrained-models/loss/vgg19");
load_vgg19_weights(vgg19); // may panic: Failed to rename temporary file...
// after (pre-seed or prepare the cache dir beforehand)
let cache_dir = dirs::cache_dir().unwrap().join("burn-pretrained-models/loss/vgg19");
std::fs::create_dir_all(&cache_dir).unwrap();
let cache_path = cache_dir.join("vgg19.pth");
if !cache_path.exists() {
// ensure writable and no stale temp file blocks the rename
let _ = std::fs::remove_file(cache_path.with_extension("pth.tmp"));
assert!(is_dir_writable(&cache_dir), "cache dir not writable");
}
let vgg19 = load_vgg19_weights(vgg19); Defensive patterns
Strategy: validation
Validate before calling
// Run before calling load_vgg19_weights on first use
use std::path::Path;
fn is_dir_writable(dir: &Path) -> bool {
let probe = dir.join(".write_probe");
match std::fs::File::create(&probe) {
Ok(_) => { let _ = std::fs::remove_file(&probe); true }
Err(_) => false,
}
}
let cache_dir = dirs::cache_dir().unwrap().join("burn-pretrained-models/loss/vgg19");
assert!(is_dir_writable(&cache_dir), "VGG19 cache dir not writable: {:?}", cache_dir);
// also clear any stale temp file that could break the rename
let _ = std::fs::remove_file(cache_dir.join("vgg19.pth.tmp")); Try / catch
// load_vgg19_weights panics via expect; isolate it on a thread if you must recover
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| load_vgg19_weights(vgg19)));
match result {
Ok(vgg) => vgg,
Err(_) => { /* fall back to random weights or repair the cache dir and retry */ }
} Prevention
- Pre-provision the cache by placing a valid vgg19.pth at ~/.cache/burn-pretrained-models/loss/vgg19/vgg19.pth before first run (skips download+rename entirely).
- In CI/containers, mount the cache dir as writable or set it via a writable HOME/XDG_CACHE_HOME.
- Check free disk space before first-run downloads of large weight files.
- Avoid concurrent first-time downloads from multiple processes; use a lock or pre-seeding.
When it happens
Trigger: Calling `load_vgg19_weights(vgg19)` (GramMatrixLoss initialization) on first run when `vgg19.pth` is not yet cached, the ~500MB download succeeds and is written to `vgg19.pth.tmp`, but `rename(temp_path, cache_path)` returns an Err (e.g. destination directory permissions changed mid-flight, temp file deleted by a concurrent cleaner, or the path is on a filesystem that disallows the operation).
Common situations: Running under a user whose cache dir (`~/.cache` or %LOCALAPPDATA%) is read-only; disk-full or quota situations where cleanup tools delete large .tmp files; running multiple processes concurrently that both download and one deletes the other's temp file; containers with read-only cache volumes mounted after File::create but before rename; antivirus quarantining the .tmp file.
Related errors
- Can delete model checkpoint.
- capture tensor operations must run inside CaptureDevice::cap
- Capture tensors do not support autodiff
- Autodiff should not wrap an autodiff tensor.
- Requires autodiff tensor.
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/1970f62762eca1ac.
Report an issue: GitHub.