do not automatically enable xformers (#1640) · zmic/diffusers@6b68afd · GitHub
Skip to content

Commit 6b68afd

Browse files
do not automatically enable xformers (huggingface#1640)
* do not automatically enable xformers * uP
1 parent 63c4944 commit 6b68afd

4 files changed

Lines changed: 30 additions & 11 deletions

File tree

‎examples/dreambooth/train_dreambooth.py‎

Lines changed: 10 additions & 0 deletions

‎examples/text_to_image/train_text_to_image.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from diffusers import AutoencoderKL, DDPMScheduler, StableDiffusionPipeline, UNet2DConditionModel
1919
from diffusers.optimization import get_scheduler
2020
from diffusers.utils import check_min_version
21+
from diffusers.utils.import_utils import is_xformers_available
2122
from huggingface_hub import HfFolder, Repository, whoami
2223
from torchvision import transforms
2324
from tqdm.auto import tqdm
@@ -364,6 +365,15 @@ def main():
364365
revision=args.revision,
365366
)
366367

368+
if is_xformers_available():
369+
try:
370+
unet.enable_xformers_memory_efficient_attention(True)
371+
except Exception as e:
372+
logger.warning(
373+
"Could not enable memory efficient attention. Make sure xformers is installed"
374+
f" correctly and a GPU is available: {e}"
375+
)
376+
367377
# Freeze vae and text_encoder
368378
vae.requires_grad_(False)
369379
text_encoder.requires_grad_(False)

‎examples/textual_inversion/textual_inversion.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from diffusers.optimization import get_scheduler
2121
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
2222
from diffusers.utils import check_min_version
23+
from diffusers.utils.import_utils import is_xformers_available
2324
from huggingface_hub import HfFolder, Repository, whoami
2425

2526
# TODO: remove and import from diffusers.utils when the new version of diffusers is released
@@ -439,6 +440,15 @@ def main():
439440
revision=args.revision,
440441
)
441442

443+
if is_xformers_available():
444+
try:
445+
unet.enable_xformers_memory_efficient_attention(True)
446+
except Exception as e:
447+
logger.warning(
448+
"Could not enable memory efficient attention. Make sure xformers is installed"
449+
f" correctly and a GPU is available: {e}"
450+
)
451+
442452
# Resize the token embeddings as we are adding new special tokens to the tokenizer
443453
text_encoder.resize_token_embeddings(len(tokenizer))
444454

‎src/diffusers/models/attention.py‎

Lines changed: 0 additions & 11 deletions

0 commit comments

Comments
 (0)