Skip to content

Conversation

@vincedovy
Copy link
Contributor

@vincedovy vincedovy commented Jun 22, 2024

Running the diffusion-models-class/unit2/finetune_model.py line 117 (save_pretrained) and 01_finetuning_and_guidance.ipynb crashes trying to convert a WindowsPath to JSON. This worked prior to an conda update (not sure which module update caused the problem.) I have diffusers 0.29.0 installed. Issue was in configuration_utils.py line 590, where it checks whether the value is a PosixPath. In this case, it was a WindowsPath. Fix was to check for WindowsPath, convert to posix, and return the resulting string.

Fixes #8651

Before submitting

Who can review?

@sayakpaul @yiyixuxu @DN6

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

vincedovy and others added 2 commits June 20, 2024 20:00
On Windows, os.path.join returns a WindowsPath. to_json_string does not convert this from a WindowsPath to a string. Added check for WindowsPath to to_json_saveable.
@sayakpaul
Copy link
Member

Thanks for your PR. Is there an easier way to reproduce this issue? That would be very nice.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@vincedovy
Copy link
Contributor Author

Thanks for your PR. Is there an easier way to reproduce this issue? That would be very nice.

The problem with reproducing is that in Linux I cannot instantiate a WindowsPath, getting a NotImplementedError. I could write a test that generates a PureWindowsPath and modify the configuration_utils.py to check for instances of PureWindowsPath rather than WindowsPath. I am looking at tests/others/test_config.py to see if I can shoe horn that in somehow.

@sayakpaul
Copy link
Member

Yeah that should work. Even a minimal reproducer that fails on your Windows environment should be good enough for us.

@vincedovy
Copy link
Contributor Author

Added the unit test to tests/others/test_config.py
The unit test creates two Path objects, calls to_json_string(), parses the result and checks the return values.
I ran the test on both Linux and Windows systems.
On Windows, the Path constructor returns a WindowsPath; on Linux it returns a PosixPath.

sayakpaul and others added 2 commits June 23, 2024 15:33
…o_json_string(). Conditional now tests for Path, and uses Path.as_posix() to convert to string.
Comment on lines 590 to 593
elif isinstance(value, PosixPath):
value = str(value)
elif isinstance(value, WindowsPath):
value = str(value.as_posix())
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the proper fix here would be to check if the value is a Path. Actually, PosixPath and WindowsPath are the 2 only subclasses of Path (see docs) and distinguishing both cases is not needed.

image

So what I would do is:

elif isinstance(value, Path):
    value = value.as_posix()

as_posix is defined for any Path and directly returns a str.

Copy link
Collaborator

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for the changes @vincedovy 🤗

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

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

Nice! Thank you!

@sayakpaul
Copy link
Member

@vincedovy we need to fix the code quality issues.

https://github.com/huggingface/diffusers/actions/runs/9683518936/job/26719146234?pr=8662#step:6:1 provides guidance on how to fix it.

@vincedovy
Copy link
Contributor Author

Done

@sayakpaul sayakpaul merged commit a536e77 into huggingface:main Jun 27, 2024
sayakpaul added a commit that referenced this pull request Dec 23, 2024
* Add check for WindowsPath in to_json_string

On Windows, os.path.join returns a WindowsPath. to_json_string does not convert this from a WindowsPath to a string. Added check for WindowsPath to to_json_saveable.

* Remove extraneous convert to string in test_check_path_types (tests/others/test_config.py)

* Fix style issues in tests/others/test_config.py

* Add unit test to test_config.py to verify that PosixPath and WindowsPath (depending on system) both work when converted to JSON

* Remove distinction between PosixPath and WindowsPath in ConfigMixIn.to_json_string(). Conditional now tests for Path, and uses Path.as_posix() to convert to string.

---------

Co-authored-by: Vincent Dovydaitis <vincedovy@gmail.com>
Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
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.

configuration_utils.to_json_string() fails on WindowsPath

4 participants