Skip to content

fix: file exists on consecutive metric evaluations#699

Merged
kfachikov merged 1 commit into
mainfrom
fix/file-exists-error-during-metric-evaluation
Jun 23, 2026
Merged

fix: file exists on consecutive metric evaluations#699
kfachikov merged 1 commit into
mainfrom
fix/file-exists-error-during-metric-evaluation

Conversation

@kfachikov

Copy link
Copy Markdown
Contributor

Description

This change resolves a FileExistsError caused by shutil.copytree when the destination directory already exists.

TL;DR The Cause

Setting

When the model under evaluation is a complex one (i.e., have submodules) such as Qwen-Image-2512, the metrics for each submodule are saved in a nested directory. When the evaluation comprise multiple GPUMemoryStats metrics, the save_pretrained method is invoked for each metric. When the model is smashed, the particular save function is SAVE_FUNCTIONS.save_before_apply.

Problem

The save_before_apply function distinguishes between files and directories and invokes shutil.copy and shutil.copytree correspondingly. However, they work differently with their default parameters. While copy always writes the file unconditionally (i.e., if there is already one with such a name, it is replaced), copytree fails when the directory exists.

copy documentation; relevant

Copies the file src to the file or directory dst. src and dst should be path-like objects or strings. If dst specifies a directory, the file will be copied into dst using the base filename from src. If dst specifies a file that already exists, it will be replaced. Returns the path to the newly created file.

copytree documentation; relevant

If dirs_exist_ok is false (the default) and dst already exists, a FileExistsError is raised. If dirs_exist_ok is true, the copying operation will continue if it encounters existing directories, and files within the dst tree will be overwritten by corresponding files from the src tree.

Related Issue

Fixes #(issue number)

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Refactor (no functional change)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Testing

  • I added or updated tests covering my changes
    • I did empirical tests, successfully running an experiment with a smashed model with DiskMemoryMetric and InferenceMemoryMetric in the same evaluation agent.
  • Existing tests pass locally (uv run pytest -m "cpu and not slow")

For full setup and testing instructions, see the Contributing Guide.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code, especially for agent-assisted changes
  • I updated the documentation where necessary

Thanks for contributing to Pruna! We're excited to review your work.

New to contributing? Check out our Contributing Guide for everything you need to get started.

Note:

  • Draft PRs or PRs without a clear and detailed overview may be delayed.
  • Please mark your PR as Ready for Review and ensure the sections above are filled out.
  • Contributions that are entirely AI-generated without meaningful human review are discouraged.

This change resolves a FileExistsError caused by shutil.copytree when
the destination directory already exists.
@begumcig begumcig self-requested a review June 22, 2026 16:11

@begumcig begumcig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You dropped this 👑👑👑👑👑

@kfachikov kfachikov merged commit a935ffd into main Jun 23, 2026
10 checks passed
@kfachikov kfachikov deleted the fix/file-exists-error-during-metric-evaluation branch June 23, 2026 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants