From d61be7e9f0b75ed4054e50a49b5affed4cb64fdd Mon Sep 17 00:00:00 2001 From: vincedovy Date: Thu, 20 Jun 2024 20:00:22 -0400 Subject: [PATCH 1/5] 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. --- src/diffusers/configuration_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/diffusers/configuration_utils.py b/src/diffusers/configuration_utils.py index be74ae061909..9a3463ed2cbd 100644 --- a/src/diffusers/configuration_utils.py +++ b/src/diffusers/configuration_utils.py @@ -23,7 +23,7 @@ import os import re from collections import OrderedDict -from pathlib import PosixPath +from pathlib import PosixPath, WindowsPath from typing import Any, Dict, Tuple, Union import numpy as np @@ -589,6 +589,8 @@ def to_json_saveable(value): value = value.tolist() elif isinstance(value, PosixPath): value = str(value) + elif isinstance(value, WindowsPath): + value = str(value.as_posix()) return value config_dict = {k: to_json_saveable(v) for k, v in config_dict.items()} From f701d7cb4fab12742a6bf5b5366575c439aa1f45 Mon Sep 17 00:00:00 2001 From: Vincent Dovydaitis Date: Fri, 21 Jun 2024 21:53:27 -0400 Subject: [PATCH 2/5] Add unit test to test_config.py to verify that PosixPath and WindowsPath (depending on system) both work when converted to JSON --- tests/others/test_config.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/others/test_config.py b/tests/others/test_config.py index eafafe33dfb9..1fe4e3c14844 100644 --- a/tests/others/test_config.py +++ b/tests/others/test_config.py @@ -28,6 +28,8 @@ from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.utils.testing_utils import CaptureLogger +from pathlib import Path +import json class SampleObject(ConfigMixin): config_name = "config.json" @@ -90,6 +92,17 @@ def __init__( ): pass +class SampleObjectPaths(ConfigMixin): + config_name = "config.json" + + @register_to_config + def __init__( + self, + test_file_1 = Path('foo/bar'), + test_file_2 = Path('foo bar\\bar') + ): + pass + class ConfigTester(unittest.TestCase): def test_load_not_from_mixin(self): @@ -286,3 +299,12 @@ def test_use_default_values(self): # Nevertheless "e" should still be correctly loaded to [1, 3] from SampleObject2 instead of defaulting to [1, 5] assert new_config_2.config.e == [1, 3] + + def test_check_path_types(self): + # Verify that we get a string returned from a WindowsPath or PosixPath (depending on system) + config = SampleObjectPaths() + json_string = config.to_json_string() + result = json.loads(json_string) + assert(result['test_file_1'] == str(config.config.test_file_1.as_posix())) + assert(result['test_file_2'] == str(config.config.test_file_2.as_posix())) + \ No newline at end of file From 048a0eba8ef40b46b3d7fb4e0a0313b43bcef4f7 Mon Sep 17 00:00:00 2001 From: Vincent Dovydaitis Date: Sat, 22 Jun 2024 07:35:18 -0400 Subject: [PATCH 3/5] 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. --- src/diffusers/configuration_utils.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/diffusers/configuration_utils.py b/src/diffusers/configuration_utils.py index 9a3463ed2cbd..19f55de463b3 100644 --- a/src/diffusers/configuration_utils.py +++ b/src/diffusers/configuration_utils.py @@ -23,7 +23,7 @@ import os import re from collections import OrderedDict -from pathlib import PosixPath, WindowsPath +from pathlib import Path from typing import Any, Dict, Tuple, Union import numpy as np @@ -587,10 +587,8 @@ def to_json_string(self) -> str: def to_json_saveable(value): if isinstance(value, np.ndarray): value = value.tolist() - elif isinstance(value, PosixPath): - value = str(value) - elif isinstance(value, WindowsPath): - value = str(value.as_posix()) + elif isinstance(value, Path): + value = value.as_posix() return value config_dict = {k: to_json_saveable(v) for k, v in config_dict.items()} From f134215b351c5d6ac781b4c5394f6b4deebe4818 Mon Sep 17 00:00:00 2001 From: Vincent Dovydaitis Date: Sat, 22 Jun 2024 07:38:31 -0400 Subject: [PATCH 4/5] Remove extraneous convert to string in test_check_path_types (tests/others/test_config.py) --- tests/others/test_config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/others/test_config.py b/tests/others/test_config.py index 1fe4e3c14844..6aa8ac4c97ad 100644 --- a/tests/others/test_config.py +++ b/tests/others/test_config.py @@ -305,6 +305,6 @@ def test_check_path_types(self): config = SampleObjectPaths() json_string = config.to_json_string() result = json.loads(json_string) - assert(result['test_file_1'] == str(config.config.test_file_1.as_posix())) - assert(result['test_file_2'] == str(config.config.test_file_2.as_posix())) + assert(result['test_file_1'] == config.config.test_file_1.as_posix()) + assert(result['test_file_2'] == config.config.test_file_2.as_posix()) \ No newline at end of file From c70488143c78a349789df790dc13de7f2261c1f3 Mon Sep 17 00:00:00 2001 From: Vincent Dovydaitis Date: Sat, 22 Jun 2024 08:16:40 -0400 Subject: [PATCH 5/5] Fix style issues in tests/others/test_config.py --- tests/others/test_config.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/others/test_config.py b/tests/others/test_config.py index 6aa8ac4c97ad..664c36ac33d6 100644 --- a/tests/others/test_config.py +++ b/tests/others/test_config.py @@ -13,8 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import tempfile import unittest +from pathlib import Path from diffusers import ( DDIMScheduler, @@ -28,8 +30,6 @@ from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.utils.testing_utils import CaptureLogger -from pathlib import Path -import json class SampleObject(ConfigMixin): config_name = "config.json" @@ -92,15 +92,12 @@ def __init__( ): pass + class SampleObjectPaths(ConfigMixin): config_name = "config.json" @register_to_config - def __init__( - self, - test_file_1 = Path('foo/bar'), - test_file_2 = Path('foo bar\\bar') - ): + def __init__(self, test_file_1=Path("foo/bar"), test_file_2=Path("foo bar\\bar")): pass @@ -305,6 +302,5 @@ def test_check_path_types(self): config = SampleObjectPaths() json_string = config.to_json_string() result = json.loads(json_string) - assert(result['test_file_1'] == config.config.test_file_1.as_posix()) - assert(result['test_file_2'] == config.config.test_file_2.as_posix()) - \ No newline at end of file + assert result["test_file_1"] == config.config.test_file_1.as_posix() + assert result["test_file_2"] == config.config.test_file_2.as_posix()