Skip to content

Commit

Permalink
[examples] add check_min_version (huggingface#1550)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
patil-suraj and pcuenca authored Dec 6, 2022
1 parent ae4112d commit c228331
Show file tree
Hide file tree
Showing 21 changed files with 110 additions and 20 deletions.
12 changes: 11 additions & 1 deletion examples/dreambooth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ The `train_dreambooth.py` script shows how to implement the training procedure a

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

**Important**

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:
```bash
git clone https://github.com/huggingface/diffusers
cd diffusers
pip install -e .
```

Then cd in the example folder and run
```bash
pip install -U -r requirements.txt
pip install -r requirements.txt
```

And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with:
Expand Down
1 change: 0 additions & 1 deletion examples/dreambooth/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
diffusers>==0.5.0
accelerate
torchvision
transformers>=4.21.0
Expand Down
1 change: 0 additions & 1 deletion examples/dreambooth/requirements_flax.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
diffusers>==0.5.1
transformers>=4.21.0
flax
optax
Expand Down
4 changes: 4 additions & 0 deletions examples/dreambooth/train_dreambooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
from accelerate.utils import set_seed
from diffusers import AutoencoderKL, DDPMScheduler, DiffusionPipeline, UNet2DConditionModel
from diffusers.optimization import get_scheduler
from diffusers.utils import check_min_version
from huggingface_hub import HfFolder, Repository, whoami
from PIL import Image
from torchvision import transforms
from tqdm.auto import tqdm
from transformers import AutoTokenizer, PretrainedConfig


# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.10.0.dev0")

logger = get_logger(__name__)


Expand Down
4 changes: 4 additions & 0 deletions examples/dreambooth/train_dreambooth_flax.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
FlaxUNet2DConditionModel,
)
from diffusers.pipelines.stable_diffusion import FlaxStableDiffusionSafetyChecker
from diffusers.utils import check_min_version
from flax import jax_utils
from flax.training import train_state
from flax.training.common_utils import shard
Expand All @@ -33,6 +34,9 @@
from transformers import CLIPFeatureExtractor, CLIPTokenizer, FlaxCLIPTextModel, set_seed


# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.10.0.dev0")

logger = logging.getLogger(__name__)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
UNet2DConditionModel,
)
from diffusers.optimization import get_scheduler
from diffusers.utils import check_min_version
from huggingface_hub import HfFolder, Repository, whoami
from PIL import Image, ImageDraw
from torchvision import transforms
from tqdm.auto import tqdm
from transformers import CLIPTextModel, CLIPTokenizer


# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.10.0.dev0")

logger = get_logger(__name__)


Expand Down
13 changes: 11 additions & 2 deletions examples/text_to_image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ ___This script is experimental. The script fine-tunes the whole model and often

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

**Important**

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:
```bash
git clone https://github.com/huggingface/diffusers
cd diffusers
pip install .
```

Then cd in the example folder and run
```bash
pip install "git+https://github.com/huggingface/diffusers.git#egg=diffusers[training]"
pip install -U -r requirements.txt
pip install -r requirements.txt
```

And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with:
Expand Down
2 changes: 1 addition & 1 deletion examples/text_to_image/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
diffusers==0.4.1
accelerate
torchvision
transformers>=4.21.0
datasets
ftfy
tensorboard
modelcards
2 changes: 1 addition & 1 deletion examples/text_to_image/requirements_flax.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diffusers>==0.5.1
transformers>=4.21.0
datasets
flax
optax
torch
Expand Down
4 changes: 4 additions & 0 deletions examples/text_to_image/train_text_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
from datasets import load_dataset
from diffusers import AutoencoderKL, DDPMScheduler, StableDiffusionPipeline, UNet2DConditionModel
from diffusers.optimization import get_scheduler
from diffusers.utils import check_min_version
from huggingface_hub import HfFolder, Repository, whoami
from torchvision import transforms
from tqdm.auto import tqdm
from transformers import CLIPTextModel, CLIPTokenizer


# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.10.0.dev0")

logger = get_logger(__name__)


Expand Down
4 changes: 4 additions & 0 deletions examples/text_to_image/train_text_to_image_flax.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
FlaxUNet2DConditionModel,
)
from diffusers.pipelines.stable_diffusion import FlaxStableDiffusionSafetyChecker
from diffusers.utils import check_min_version
from flax import jax_utils
from flax.training import train_state
from flax.training.common_utils import shard
Expand All @@ -32,6 +33,9 @@
from transformers import CLIPFeatureExtractor, CLIPTokenizer, FlaxCLIPTextModel, set_seed


# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.10.0.dev0")

logger = logging.getLogger(__name__)


Expand Down
12 changes: 11 additions & 1 deletion examples/textual_inversion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ Colab for inference

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

**Important**

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:
```bash
git clone https://github.com/huggingface/diffusers
cd diffusers
pip install .
```

Then cd in the example folder and run
```bash
pip install diffusers"[training]" accelerate "transformers>=4.21.0"
pip install -r requirements.txt
```

And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with:
Expand Down
3 changes: 3 additions & 0 deletions examples/textual_inversion/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
accelerate
torchvision
transformers>=4.21.0
ftfy
tensorboard
modelcards
1 change: 0 additions & 1 deletion examples/textual_inversion/requirements_flax.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
diffusers>==0.5.1
transformers>=4.21.0
flax
optax
Expand Down
5 changes: 5 additions & 0 deletions examples/textual_inversion/textual_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from diffusers import AutoencoderKL, DDPMScheduler, PNDMScheduler, StableDiffusionPipeline, UNet2DConditionModel
from diffusers.optimization import get_scheduler
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
from diffusers.utils import check_min_version
from huggingface_hub import HfFolder, Repository, whoami

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


# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.10.0.dev0")


logger = get_logger(__name__)


Expand Down
4 changes: 4 additions & 0 deletions examples/textual_inversion/textual_inversion_flax.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
FlaxUNet2DConditionModel,
)
from diffusers.pipelines.stable_diffusion import FlaxStableDiffusionSafetyChecker
from diffusers.utils import check_min_version
from flax import jax_utils
from flax.training import train_state
from flax.training.common_utils import shard
Expand Down Expand Up @@ -55,6 +56,9 @@
}
# ------------------------------------------------------------------------------

# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.10.0.dev0")

logger = logging.getLogger(__name__)


Expand Down
13 changes: 12 additions & 1 deletion examples/unconditional_image_generation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ Creating a training image set is [described in a different document](https://hug

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

**Important**

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:
```bash
pip install diffusers[training] accelerate datasets tensorboard
git clone https://github.com/huggingface/diffusers
cd diffusers
pip install .
```

Then cd in the example folder and run
```bash
pip install -r requirements.txt
```


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

```bash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from accelerate import Accelerator
from accelerate.logging import get_logger
from datasets import load_dataset
from diffusers import DDPMPipeline, DDPMScheduler, UNet2DModel, __version__
from diffusers import DDPMPipeline, DDPMScheduler, UNet2DModel
from diffusers.optimization import get_scheduler
from diffusers.training_utils import EMAModel
from diffusers.utils import check_min_version
from huggingface_hub import HfFolder, Repository, whoami
from packaging import version
from torchvision.transforms import (
CenterCrop,
Compose,
Expand All @@ -28,8 +28,11 @@
from tqdm.auto import tqdm


# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.10.0.dev0")


logger = get_logger(__name__)
diffusers_version = version.parse(version.parse(__version__).base_version)


def _extract_into_tensor(arr, timesteps, broadcast_shape):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from diffusers import DDPMPipeline, DDPMScheduler, UNet2DModel
from diffusers.optimization import get_scheduler
from diffusers.training_utils import EMAModel
from diffusers.utils import check_min_version
from huggingface_hub import HfFolder, Repository, whoami
from onnxruntime.training.ortmodule import ORTModule
from torchvision.transforms import (
Expand All @@ -27,6 +28,9 @@
from tqdm.auto import tqdm


# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.10.0.dev0")

logger = get_logger(__name__)


Expand Down
12 changes: 5 additions & 7 deletions src/diffusers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
__version__ = "0.10.0.dev0"

from .configuration_utils import ConfigMixin
from .onnx_utils import OnnxRuntimeModel
from .utils import (
is_flax_available,
is_inflect_available,
Expand All @@ -6,16 +10,10 @@
is_torch_available,
is_transformers_available,
is_unidecode_available,
logging,
)


__version__ = "0.10.0.dev0"

from .configuration_utils import ConfigMixin
from .onnx_utils import OnnxRuntimeModel
from .utils import logging


if is_torch_available():
from .modeling_utils import ModelMixin
from .models import AutoencoderKL, Transformer2DModel, UNet1DModel, UNet2DConditionModel, UNet2DModel, VQModel
Expand Down
16 changes: 16 additions & 0 deletions src/diffusers/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

import os

from packaging import version

from .. import __version__
from .deprecation_utils import deprecate
from .import_utils import (
ENV_VARS_TRUE_AND_AUTO_VALUES,
Expand Down Expand Up @@ -88,3 +91,16 @@
"EulerAncestralDiscreteScheduler",
"DPMSolverMultistepScheduler",
]


def check_min_version(min_version):
if version.parse(__version__) < version.parse(min_version):
if "dev" in min_version:
error_message = (
"This example requires a source install from HuggingFace diffusers (see "
"`https://huggingface.co/docs/diffusers/installation#install-from-source`),"
)
else:
error_message = f"This example requires a minimum version of {min_version},"
error_message += f" but the version found is {__version__}.\n"
raise ImportError(error_message)

0 comments on commit c228331

Please sign in to comment.