Skip to content

Commit f1484b8

Browse files
[Utils] Add deprecate function and move testing_utils under utils (#659)
* [Utils] Add deprecate function * up * up * uP * up * up * up * up * uP * up * fix * up * move to deprecation utils file * fix * fix * fix more
1 parent 1070e1a commit f1484b8

26 files changed

+305
-121
lines changed

examples/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232

3333

3434
def pytest_addoption(parser):
35-
from diffusers.testing_utils import pytest_addoption_shared
35+
from diffusers.utils.testing_utils import pytest_addoption_shared
3636

3737
pytest_addoption_shared(parser)
3838

3939

4040
def pytest_terminal_summary(terminalreporter):
41-
from diffusers.testing_utils import pytest_terminal_summary_main
41+
from diffusers.utils.testing_utils import pytest_terminal_summary_main
4242

4343
make_reports = terminalreporter.config.getoption("--make-reports")
4444
if make_reports:

examples/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from typing import List
2525

2626
from accelerate.utils import write_basic_config
27-
from diffusers.testing_utils import slow
27+
from diffusers.utils import slow
2828

2929

3030
logging.basicConfig(level=logging.DEBUG)

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import inspect
2-
import warnings
32
from typing import Callable, List, Optional, Union
43

54
import torch
@@ -10,7 +9,7 @@
109
from ...models import AutoencoderKL, UNet2DConditionModel
1110
from ...pipeline_utils import DiffusionPipeline
1211
from ...schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
13-
from ...utils import logging
12+
from ...utils import deprecate, logging
1413
from . import StableDiffusionPipelineOutput
1514
from .safety_checker import StableDiffusionSafetyChecker
1615

@@ -59,15 +58,15 @@ def __init__(
5958
super().__init__()
6059

6160
if hasattr(scheduler.config, "steps_offset") and scheduler.config.steps_offset != 1:
62-
warnings.warn(
61+
deprecation_message = (
6362
f"The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`"
6463
f" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure "
6564
"to update the config accordingly as leaving `steps_offset` might led to incorrect results"
6665
" in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,"
6766
" it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`"
68-
" file",
69-
DeprecationWarning,
67+
" file"
7068
)
69+
deprecate("steps_offset!=1", "1.0.0", deprecation_message, standard_warn=False)
7170
new_config = dict(scheduler.config)
7271
new_config["steps_offset"] = 1
7372
scheduler._internal_dict = FrozenDict(new_config)

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import inspect
2-
import warnings
32
from typing import Callable, List, Optional, Union
43

54
import numpy as np
@@ -12,7 +11,7 @@
1211
from ...models import AutoencoderKL, UNet2DConditionModel
1312
from ...pipeline_utils import DiffusionPipeline
1413
from ...schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
15-
from ...utils import logging
14+
from ...utils import deprecate, logging
1615
from . import StableDiffusionPipelineOutput
1716
from .safety_checker import StableDiffusionSafetyChecker
1817

@@ -71,15 +70,15 @@ def __init__(
7170
super().__init__()
7271

7372
if hasattr(scheduler.config, "steps_offset") and scheduler.config.steps_offset != 1:
74-
warnings.warn(
73+
deprecation_message = (
7574
f"The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`"
7675
f" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure "
7776
"to update the config accordingly as leaving `steps_offset` might led to incorrect results"
7877
" in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,"
7978
" it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`"
80-
" file",
81-
DeprecationWarning,
79+
" file"
8280
)
81+
deprecate("steps_offset!=1", "1.0.0", deprecation_message, standard_warn=False)
8382
new_config = dict(scheduler.config)
8483
new_config["steps_offset"] = 1
8584
scheduler._internal_dict = FrozenDict(new_config)

src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import inspect
2-
import warnings
32
from typing import Callable, List, Optional, Union
43

54
import numpy as np
@@ -13,7 +12,7 @@
1312
from ...models import AutoencoderKL, UNet2DConditionModel
1413
from ...pipeline_utils import DiffusionPipeline
1514
from ...schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
16-
from ...utils import logging
15+
from ...utils import deprecate, logging
1716
from . import StableDiffusionPipelineOutput
1817
from .safety_checker import StableDiffusionSafetyChecker
1918

@@ -86,15 +85,15 @@ def __init__(
8685
logger.info("`StableDiffusionInpaintPipeline` is experimental and will very likely change in the future.")
8786

8887
if hasattr(scheduler.config, "steps_offset") and scheduler.config.steps_offset != 1:
89-
warnings.warn(
88+
deprecation_message = (
9089
f"The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`"
9190
f" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure "
9291
"to update the config accordingly as leaving `steps_offset` might led to incorrect results"
9392
" in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,"
9493
" it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`"
95-
" file",
96-
DeprecationWarning,
94+
" file"
9795
)
96+
deprecate("steps_offset!=1", "1.0.0", deprecation_message, standard_warn=False)
9897
new_config = dict(scheduler.config)
9998
new_config["steps_offset"] = 1
10099
scheduler._internal_dict = FrozenDict(new_config)

src/diffusers/schedulers/scheduling_ddim.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
# and https://github.com/hojonathanho/diffusion
1717

1818
import math
19-
import warnings
2019
from dataclasses import dataclass
2120
from typing import Optional, Tuple, Union
2221

2322
import numpy as np
2423
import torch
2524

2625
from ..configuration_utils import ConfigMixin, register_to_config
27-
from ..utils import BaseOutput
26+
from ..utils import BaseOutput, deprecate
2827
from .scheduling_utils import SchedulerMixin
2928

3029

@@ -122,12 +121,12 @@ def __init__(
122121
steps_offset: int = 0,
123122
**kwargs,
124123
):
125-
if "tensor_format" in kwargs:
126-
warnings.warn(
127-
"`tensor_format` is deprecated as an argument and will be removed in version `0.5.0`."
128-
"If you're running your code in PyTorch, you can safely remove this argument.",
129-
DeprecationWarning,
130-
)
124+
deprecate(
125+
"tensor_format",
126+
"0.5.0",
127+
"If you're running your code in PyTorch, you can safely remove this argument.",
128+
take_from=kwargs,
129+
)
131130

132131
if trained_betas is not None:
133132
self.betas = torch.from_numpy(trained_betas)
@@ -175,17 +174,10 @@ def set_timesteps(self, num_inference_steps: int, **kwargs):
175174
num_inference_steps (`int`):
176175
the number of diffusion steps used when generating samples with a pre-trained model.
177176
"""
178-
179-
offset = self.config.steps_offset
180-
181-
if "offset" in kwargs:
182-
warnings.warn(
183-
"`offset` is deprecated as an input argument to `set_timesteps` and will be removed in v0.4.0."
184-
" Please pass `steps_offset` to `__init__` instead.",
185-
DeprecationWarning,
186-
)
187-
188-
offset = kwargs["offset"]
177+
deprecated_offset = deprecate(
178+
"offset", "0.5.0", "Please pass `steps_offset` to `__init__` instead.", take_from=kwargs
179+
)
180+
offset = deprecated_offset or self.config.steps_offset
189181

190182
self.num_inference_steps = num_inference_steps
191183
step_ratio = self.config.num_train_timesteps // self.num_inference_steps

src/diffusers/schedulers/scheduling_ddpm.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
# DISCLAIMER: This file is strongly influenced by https://github.com/ermongroup/ddim
1616

1717
import math
18-
import warnings
1918
from dataclasses import dataclass
2019
from typing import Optional, Tuple, Union
2120

2221
import numpy as np
2322
import torch
2423

2524
from ..configuration_utils import ConfigMixin, register_to_config
26-
from ..utils import BaseOutput
25+
from ..utils import BaseOutput, deprecate
2726
from .scheduling_utils import SchedulerMixin
2827

2928

@@ -115,12 +114,12 @@ def __init__(
115114
clip_sample: bool = True,
116115
**kwargs,
117116
):
118-
if "tensor_format" in kwargs:
119-
warnings.warn(
120-
"`tensor_format` is deprecated as an argument and will be removed in version `0.5.0`."
121-
"If you're running your code in PyTorch, you can safely remove this argument.",
122-
DeprecationWarning,
123-
)
117+
deprecate(
118+
"tensor_format",
119+
"0.5.0",
120+
"If you're running your code in PyTorch, you can safely remove this argument.",
121+
take_from=kwargs,
122+
)
124123

125124
if trained_betas is not None:
126125
self.betas = torch.from_numpy(trained_betas)

src/diffusers/schedulers/scheduling_karras_ve.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
# limitations under the License.
1414

1515

16-
import warnings
1716
from dataclasses import dataclass
1817
from typing import Optional, Tuple, Union
1918

2019
import numpy as np
2120
import torch
2221

2322
from ..configuration_utils import ConfigMixin, register_to_config
24-
from ..utils import BaseOutput
23+
from ..utils import BaseOutput, deprecate
2524
from .scheduling_utils import SchedulerMixin
2625

2726

@@ -89,12 +88,12 @@ def __init__(
8988
s_max: float = 50,
9089
**kwargs,
9190
):
92-
if "tensor_format" in kwargs:
93-
warnings.warn(
94-
"`tensor_format` is deprecated as an argument and will be removed in version `0.5.0`."
95-
"If you're running your code in PyTorch, you can safely remove this argument.",
96-
DeprecationWarning,
97-
)
91+
deprecate(
92+
"tensor_format",
93+
"0.5.0",
94+
"If you're running your code in PyTorch, you can safely remove this argument.",
95+
take_from=kwargs,
96+
)
9897

9998
# setable values
10099
self.num_inference_steps: int = None

src/diffusers/schedulers/scheduling_lms_discrete.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import warnings
1615
from dataclasses import dataclass
1716
from typing import Optional, Tuple, Union
1817

@@ -22,7 +21,7 @@
2221
from scipy import integrate
2322

2423
from ..configuration_utils import ConfigMixin, register_to_config
25-
from ..utils import BaseOutput
24+
from ..utils import BaseOutput, deprecate
2625
from .scheduling_utils import SchedulerMixin
2726

2827

@@ -77,12 +76,12 @@ def __init__(
7776
trained_betas: Optional[np.ndarray] = None,
7877
**kwargs,
7978
):
80-
if "tensor_format" in kwargs:
81-
warnings.warn(
82-
"`tensor_format` is deprecated as an argument and will be removed in version `0.5.0`."
83-
"If you're running your code in PyTorch, you can safely remove this argument.",
84-
DeprecationWarning,
85-
)
79+
deprecate(
80+
"tensor_format",
81+
"0.5.0",
82+
"If you're running your code in PyTorch, you can safely remove this argument.",
83+
take_from=kwargs,
84+
)
8685

8786
if trained_betas is not None:
8887
self.betas = torch.from_numpy(trained_betas)

src/diffusers/schedulers/scheduling_pndm.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
# DISCLAIMER: This file is strongly influenced by https://github.com/ermongroup/ddim
1616

1717
import math
18-
import warnings
1918
from typing import Optional, Tuple, Union
2019

2120
import numpy as np
2221
import torch
2322

2423
from ..configuration_utils import ConfigMixin, register_to_config
24+
from ..utils import deprecate
2525
from .scheduling_utils import SchedulerMixin, SchedulerOutput
2626

2727

@@ -102,12 +102,12 @@ def __init__(
102102
steps_offset: int = 0,
103103
**kwargs,
104104
):
105-
if "tensor_format" in kwargs:
106-
warnings.warn(
107-
"`tensor_format` is deprecated as an argument and will be removed in version `0.5.0`."
108-
"If you're running your code in PyTorch, you can safely remove this argument.",
109-
DeprecationWarning,
110-
)
105+
deprecate(
106+
"tensor_format",
107+
"0.5.0",
108+
"If you're running your code in PyTorch, you can safely remove this argument.",
109+
take_from=kwargs,
110+
)
111111

112112
if trained_betas is not None:
113113
self.betas = torch.from_numpy(trained_betas)
@@ -155,16 +155,10 @@ def set_timesteps(self, num_inference_steps: int, **kwargs) -> torch.FloatTensor
155155
num_inference_steps (`int`):
156156
the number of diffusion steps used when generating samples with a pre-trained model.
157157
"""
158-
159-
offset = self.config.steps_offset
160-
161-
if "offset" in kwargs:
162-
warnings.warn(
163-
"`offset` is deprecated as an input argument to `set_timesteps` and will be removed in v0.4.0."
164-
" Please pass `steps_offset` to `__init__` instead."
165-
)
166-
167-
offset = kwargs["offset"]
158+
deprecated_offset = deprecate(
159+
"offset", "0.5.0", "Please pass `steps_offset` to `__init__` instead.", take_from=kwargs
160+
)
161+
offset = deprecated_offset or self.config.steps_offset
168162

169163
self.num_inference_steps = num_inference_steps
170164
step_ratio = self.config.num_train_timesteps // self.num_inference_steps

src/diffusers/schedulers/scheduling_sde_ve.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch
1616

1717
import math
18-
import warnings
1918
from dataclasses import dataclass
2019
from typing import Optional, Tuple, Union
2120

2221
import torch
2322

2423
from ..configuration_utils import ConfigMixin, register_to_config
25-
from ..utils import BaseOutput
24+
from ..utils import BaseOutput, deprecate
2625
from .scheduling_utils import SchedulerMixin, SchedulerOutput
2726

2827

@@ -78,12 +77,12 @@ def __init__(
7877
correct_steps: int = 1,
7978
**kwargs,
8079
):
81-
if "tensor_format" in kwargs:
82-
warnings.warn(
83-
"`tensor_format` is deprecated as an argument and will be removed in version `0.5.0`."
84-
"If you're running your code in PyTorch, you can safely remove this argument.",
85-
DeprecationWarning,
86-
)
80+
deprecate(
81+
"tensor_format",
82+
"0.5.0",
83+
"If you're running your code in PyTorch, you can safely remove this argument.",
84+
take_from=kwargs,
85+
)
8786

8887
# setable values
8988
self.timesteps = None
@@ -139,11 +138,7 @@ def get_adjacent_sigma(self, timesteps, t):
139138
)
140139

141140
def set_seed(self, seed):
142-
warnings.warn(
143-
"The method `set_seed` is deprecated and will be removed in version `0.4.0`. Please consider passing a"
144-
" generator instead.",
145-
DeprecationWarning,
146-
)
141+
deprecate("set_seed", "0.5.0", "Please consider passing a generator instead.")
147142
torch.manual_seed(seed)
148143

149144
def step_pred(

0 commit comments

Comments
 (0)