Skip to content

Commit c228331

Browse files
patil-surajpcuenca
andauthored
[examples] add check_min_version (huggingface#1550)
* add check_min_version for examples * move __version__ to the top * Apply suggestions from code review Co-authored-by: Pedro Cuenca <[email protected]> * fix comment * fix error_message * adapt the install message Co-authored-by: Pedro Cuenca <[email protected]>
1 parent ae4112d commit c228331

21 files changed

+110
-20
lines changed

examples/dreambooth/README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ The `train_dreambooth.py` script shows how to implement the training procedure a
99

1010
Before running the scripts, make sure to install the library's training dependencies:
1111

12+
**Important**
13+
14+
To make sure you can successfully run the latest versions of the example scripts, we highly recommend **installing from source** and keeping the install up to date as we update the example scripts frequently and install some example-specific requirements. To do this, execute the following steps in a new virtual environment:
15+
```bash
16+
git clone https://github.com/huggingface/diffusers
17+
cd diffusers
18+
pip install -e .
19+
```
20+
21+
Then cd in the example folder and run
1222
```bash
13-
pip install -U -r requirements.txt
23+
pip install -r requirements.txt
1424
```
1525

1626
And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with:

examples/dreambooth/requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
diffusers>==0.5.0
21
accelerate
32
torchvision
43
transformers>=4.21.0

examples/dreambooth/requirements_flax.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
diffusers>==0.5.1
21
transformers>=4.21.0
32
flax
43
optax

examples/dreambooth/train_dreambooth.py

+4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
from accelerate.utils import set_seed
1717
from diffusers import AutoencoderKL, DDPMScheduler, DiffusionPipeline, UNet2DConditionModel
1818
from diffusers.optimization import get_scheduler
19+
from diffusers.utils import check_min_version
1920
from huggingface_hub import HfFolder, Repository, whoami
2021
from PIL import Image
2122
from torchvision import transforms
2223
from tqdm.auto import tqdm
2324
from transformers import AutoTokenizer, PretrainedConfig
2425

2526

27+
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
28+
check_min_version("0.10.0.dev0")
29+
2630
logger = get_logger(__name__)
2731

2832

examples/dreambooth/train_dreambooth_flax.py

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
FlaxUNet2DConditionModel,
2424
)
2525
from diffusers.pipelines.stable_diffusion import FlaxStableDiffusionSafetyChecker
26+
from diffusers.utils import check_min_version
2627
from flax import jax_utils
2728
from flax.training import train_state
2829
from flax.training.common_utils import shard
@@ -33,6 +34,9 @@
3334
from transformers import CLIPFeatureExtractor, CLIPTokenizer, FlaxCLIPTextModel, set_seed
3435

3536

37+
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
38+
check_min_version("0.10.0.dev0")
39+
3640
logger = logging.getLogger(__name__)
3741

3842

examples/research_projects/dreambooth_inpaint/train_dreambooth_inpaint.py

+4
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
UNet2DConditionModel,
2525
)
2626
from diffusers.optimization import get_scheduler
27+
from diffusers.utils import check_min_version
2728
from huggingface_hub import HfFolder, Repository, whoami
2829
from PIL import Image, ImageDraw
2930
from torchvision import transforms
3031
from tqdm.auto import tqdm
3132
from transformers import CLIPTextModel, CLIPTokenizer
3233

3334

35+
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
36+
check_min_version("0.10.0.dev0")
37+
3438
logger = get_logger(__name__)
3539

3640

examples/text_to_image/README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@ ___This script is experimental. The script fine-tunes the whole model and often
1212

1313
Before running the scripts, make sure to install the library's training dependencies:
1414

15+
**Important**
16+
17+
To make sure you can successfully run the latest versions of the example scripts, we highly recommend **installing from source** and keeping the install up to date as we update the example scripts frequently and install some example-specific requirements. To do this, execute the following steps in a new virtual environment:
18+
```bash
19+
git clone https://github.com/huggingface/diffusers
20+
cd diffusers
21+
pip install .
22+
```
23+
24+
Then cd in the example folder and run
1525
```bash
16-
pip install "git+https://github.com/huggingface/diffusers.git#egg=diffusers[training]"
17-
pip install -U -r requirements.txt
26+
pip install -r requirements.txt
1827
```
1928

2029
And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with:
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
diffusers==0.4.1
21
accelerate
32
torchvision
43
transformers>=4.21.0
4+
datasets
55
ftfy
66
tensorboard
77
modelcards

examples/text_to_image/requirements_flax.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
diffusers>==0.5.1
21
transformers>=4.21.0
2+
datasets
33
flax
44
optax
55
torch

examples/text_to_image/train_text_to_image.py

+4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
from datasets import load_dataset
1818
from diffusers import AutoencoderKL, DDPMScheduler, StableDiffusionPipeline, UNet2DConditionModel
1919
from diffusers.optimization import get_scheduler
20+
from diffusers.utils import check_min_version
2021
from huggingface_hub import HfFolder, Repository, whoami
2122
from torchvision import transforms
2223
from tqdm.auto import tqdm
2324
from transformers import CLIPTextModel, CLIPTokenizer
2425

2526

27+
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
28+
check_min_version("0.10.0.dev0")
29+
2630
logger = get_logger(__name__)
2731

2832

examples/text_to_image/train_text_to_image_flax.py

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
FlaxUNet2DConditionModel,
2424
)
2525
from diffusers.pipelines.stable_diffusion import FlaxStableDiffusionSafetyChecker
26+
from diffusers.utils import check_min_version
2627
from flax import jax_utils
2728
from flax.training import train_state
2829
from flax.training.common_utils import shard
@@ -32,6 +33,9 @@
3233
from transformers import CLIPFeatureExtractor, CLIPTokenizer, FlaxCLIPTextModel, set_seed
3334

3435

36+
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
37+
check_min_version("0.10.0.dev0")
38+
3539
logger = logging.getLogger(__name__)
3640

3741

examples/textual_inversion/README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,18 @@ Colab for inference
1616

1717
Before running the scripts, make sure to install the library's training dependencies:
1818

19+
**Important**
20+
21+
To make sure you can successfully run the latest versions of the example scripts, we highly recommend **installing from source** and keeping the install up to date as we update the example scripts frequently and install some example-specific requirements. To do this, execute the following steps in a new virtual environment:
22+
```bash
23+
git clone https://github.com/huggingface/diffusers
24+
cd diffusers
25+
pip install .
26+
```
27+
28+
Then cd in the example folder and run
1929
```bash
20-
pip install diffusers"[training]" accelerate "transformers>=4.21.0"
30+
pip install -r requirements.txt
2131
```
2232

2333
And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with:
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
accelerate
22
torchvision
33
transformers>=4.21.0
4+
ftfy
5+
tensorboard
6+
modelcards

examples/textual_inversion/requirements_flax.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
diffusers>==0.5.1
21
transformers>=4.21.0
32
flax
43
optax

examples/textual_inversion/textual_inversion.py

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from diffusers import AutoencoderKL, DDPMScheduler, PNDMScheduler, StableDiffusionPipeline, UNet2DConditionModel
2020
from diffusers.optimization import get_scheduler
2121
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
22+
from diffusers.utils import check_min_version
2223
from huggingface_hub import HfFolder, Repository, whoami
2324

2425
# TODO: remove and import from diffusers.utils when the new version of diffusers is released
@@ -48,6 +49,10 @@
4849
# ------------------------------------------------------------------------------
4950

5051

52+
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
53+
check_min_version("0.10.0.dev0")
54+
55+
5156
logger = get_logger(__name__)
5257

5358

examples/textual_inversion/textual_inversion_flax.py

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
FlaxUNet2DConditionModel,
2525
)
2626
from diffusers.pipelines.stable_diffusion import FlaxStableDiffusionSafetyChecker
27+
from diffusers.utils import check_min_version
2728
from flax import jax_utils
2829
from flax.training import train_state
2930
from flax.training.common_utils import shard
@@ -55,6 +56,9 @@
5556
}
5657
# ------------------------------------------------------------------------------
5758

59+
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
60+
check_min_version("0.10.0.dev0")
61+
5862
logger = logging.getLogger(__name__)
5963

6064

examples/unconditional_image_generation/README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ Creating a training image set is [described in a different document](https://hug
66

77
Before running the scripts, make sure to install the library's training dependencies:
88

9+
**Important**
10+
11+
To make sure you can successfully run the latest versions of the example scripts, we highly recommend **installing from source** and keeping the install up to date as we update the example scripts frequently and install some example-specific requirements. To do this, execute the following steps in a new virtual environment:
912
```bash
10-
pip install diffusers[training] accelerate datasets tensorboard
13+
git clone https://github.com/huggingface/diffusers
14+
cd diffusers
15+
pip install .
1116
```
1217

18+
Then cd in the example folder and run
19+
```bash
20+
pip install -r requirements.txt
21+
```
22+
23+
1324
And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with:
1425

1526
```bash

examples/unconditional_image_generation/train_unconditional.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
from accelerate import Accelerator
1212
from accelerate.logging import get_logger
1313
from datasets import load_dataset
14-
from diffusers import DDPMPipeline, DDPMScheduler, UNet2DModel, __version__
14+
from diffusers import DDPMPipeline, DDPMScheduler, UNet2DModel
1515
from diffusers.optimization import get_scheduler
1616
from diffusers.training_utils import EMAModel
17+
from diffusers.utils import check_min_version
1718
from huggingface_hub import HfFolder, Repository, whoami
18-
from packaging import version
1919
from torchvision.transforms import (
2020
CenterCrop,
2121
Compose,
@@ -28,8 +28,11 @@
2828
from tqdm.auto import tqdm
2929

3030

31+
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
32+
check_min_version("0.10.0.dev0")
33+
34+
3135
logger = get_logger(__name__)
32-
diffusers_version = version.parse(version.parse(__version__).base_version)
3336

3437

3538
def _extract_into_tensor(arr, timesteps, broadcast_shape):

examples/unconditional_image_generation/train_unconditional_ort.py

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from diffusers import DDPMPipeline, DDPMScheduler, UNet2DModel
1414
from diffusers.optimization import get_scheduler
1515
from diffusers.training_utils import EMAModel
16+
from diffusers.utils import check_min_version
1617
from huggingface_hub import HfFolder, Repository, whoami
1718
from onnxruntime.training.ortmodule import ORTModule
1819
from torchvision.transforms import (
@@ -27,6 +28,9 @@
2728
from tqdm.auto import tqdm
2829

2930

31+
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
32+
check_min_version("0.10.0.dev0")
33+
3034
logger = get_logger(__name__)
3135

3236

src/diffusers/__init__.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
__version__ = "0.10.0.dev0"
2+
3+
from .configuration_utils import ConfigMixin
4+
from .onnx_utils import OnnxRuntimeModel
15
from .utils import (
26
is_flax_available,
37
is_inflect_available,
@@ -6,16 +10,10 @@
610
is_torch_available,
711
is_transformers_available,
812
is_unidecode_available,
13+
logging,
914
)
1015

1116

12-
__version__ = "0.10.0.dev0"
13-
14-
from .configuration_utils import ConfigMixin
15-
from .onnx_utils import OnnxRuntimeModel
16-
from .utils import logging
17-
18-
1917
if is_torch_available():
2018
from .modeling_utils import ModelMixin
2119
from .models import AutoencoderKL, Transformer2DModel, UNet1DModel, UNet2DConditionModel, UNet2DModel, VQModel

src/diffusers/utils/__init__.py

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
import os
1717

18+
from packaging import version
19+
20+
from .. import __version__
1821
from .deprecation_utils import deprecate
1922
from .import_utils import (
2023
ENV_VARS_TRUE_AND_AUTO_VALUES,
@@ -88,3 +91,16 @@
8891
"EulerAncestralDiscreteScheduler",
8992
"DPMSolverMultistepScheduler",
9093
]
94+
95+
96+
def check_min_version(min_version):
97+
if version.parse(__version__) < version.parse(min_version):
98+
if "dev" in min_version:
99+
error_message = (
100+
"This example requires a source install from HuggingFace diffusers (see "
101+
"`https://huggingface.co/docs/diffusers/installation#install-from-source`),"
102+
)
103+
else:
104+
error_message = f"This example requires a minimum version of {min_version},"
105+
error_message += f" but the version found is {__version__}.\n"
106+
raise ImportError(error_message)

0 commit comments

Comments
 (0)