Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/controlnet/train_controlnet_flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ def load_model_hook(models, input_dir):
if args.enable_npu_flash_attention:
if is_torch_npu_available():
logger.info("npu flash attention enabled.")
flux_transformer.enable_npu_flash_attention()
flux_transformer.set_attention_backend("_native_npu")
else:
raise ValueError("npu flash attention requires torch_npu extensions and is supported only on npu devices.")

Expand Down
3 changes: 2 additions & 1 deletion examples/controlnet/train_controlnet_sdxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
UNet2DConditionModel,
UniPCMultistepScheduler,
)
from diffusers.models.attention_processor import AttnProcessorNPU
from diffusers.optimization import get_scheduler
from diffusers.utils import check_min_version, is_wandb_available, make_image_grid
from diffusers.utils.hub_utils import load_or_create_model_card, populate_model_card
Expand Down Expand Up @@ -979,7 +980,7 @@ def load_model_hook(models, input_dir):
if args.enable_npu_flash_attention:
if is_torch_npu_available():
logger.info("npu flash attention enabled.")
unet.enable_npu_flash_attention()
unet.set_attn_processor(AttnProcessorNPU())
else:
raise ValueError("npu flash attention requires torch_npu extensions and is supported only on npu devices.")

Expand Down
3 changes: 2 additions & 1 deletion examples/dreambooth/train_dreambooth_lora_sana.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
SanaPipeline,
SanaTransformer2DModel,
)
from diffusers.models.attention_processor import AttnProcessorNPU
from diffusers.optimization import get_scheduler
from diffusers.training_utils import (
_collate_lora_metadata,
Expand Down Expand Up @@ -1023,7 +1024,7 @@ def main(args):
if is_torch_npu_available():
logger.info("npu flash attention enabled.")
for block in transformer.transformer_blocks:
block.attn2.set_use_npu_flash_attention(True)
block.attn2.set_processor(AttnProcessorNPU())
else:
raise ValueError("npu flash attention requires torch_npu extensions and is supported only on npu device ")

Expand Down
3 changes: 2 additions & 1 deletion examples/research_projects/lpl/train_sdxl_lpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

import diffusers
from diffusers import AutoencoderKL, DDPMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel
from diffusers.models.attention_processor import AttnProcessorNPU
from diffusers.optimization import get_scheduler
from diffusers.training_utils import EMAModel, compute_snr
from diffusers.utils import check_min_version, is_wandb_available
Expand Down Expand Up @@ -852,7 +853,7 @@ def main(args):
if args.enable_npu_flash_attention:
if is_torch_npu_available():
logger.info("npu flash attention enabled.")
unet.enable_npu_flash_attention()
unet.set_attn_processor(AttnProcessorNPU())
else:
raise ValueError("npu flash attention requires torch_npu extensions and is supported only on npu devices.")
if args.enable_xformers_memory_efficient_attention:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _main(index, args, text_pipe, ckpt_id):
flux_pipe = FluxPipeline.from_pretrained(
ckpt_id, text_encoder=None, tokenizer=None, text_encoder_2=None, tokenizer_2=None, torch_dtype=torch.bfloat16
).to(device0)
flux_pipe.transformer.enable_xla_flash_attention(partition_spec=("data", None, None, None), is_flux=True)
flux_pipe.transformer.set_attention_backend("_native_xla")
FlashAttention.DEFAULT_BLOCK_SIZES = {
"block_q": 1536,
"block_k_major": 1536,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def main(args):
spec[largest_dim] = "model"
xs.mark_sharding(param, mesh, tuple(spec))

flux_pipe.transformer.enable_xla_flash_attention(partition_spec=("data", None, None, None), is_flux=True)
flux_pipe.transformer.set_attention_backend("_native_xla")
FlashAttention.DEFAULT_BLOCK_SIZES = {
"block_q": 1536,
"block_k_major": 1536,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
StableDiffusionPipeline,
UNet2DConditionModel,
)
from diffusers.models.attention_processor import XLAFlashAttnProcessor2_0
from diffusers.training_utils import compute_snr
from diffusers.utils import is_wandb_available
from diffusers.utils.hub_utils import load_or_create_model_card, populate_model_card
Expand Down Expand Up @@ -520,7 +521,7 @@ def main(args):
from torch_xla.distributed.fsdp.utils import apply_xla_patch_to_nn_linear

unet = apply_xla_patch_to_nn_linear(unet, xs.xla_patched_nn_linear_forward)
unet.enable_xla_flash_attention(partition_spec=("data", None, None, None))
unet.set_attn_processor(XLAFlashAttnProcessor2_0(partition_spec=("data", None, None, None)))

vae.requires_grad_(False)
text_encoder.requires_grad_(False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
SanaSprintPipeline,
SanaTransformer2DModel,
)
from diffusers.models.attention_processor import Attention
from diffusers.models.attention_processor import Attention, AttnProcessorNPU
from diffusers.optimization import get_scheduler
from diffusers.training_utils import (
free_memory,
Expand Down Expand Up @@ -1106,9 +1106,9 @@ def main(args):
if is_torch_npu_available():
logger.info("npu flash attention enabled.")
for block in transformer.transformer_blocks:
block.attn2.set_use_npu_flash_attention(True)
block.attn2.set_processor(AttnProcessorNPU())
for block in pretrained_model.transformer_blocks:
block.attn2.set_use_npu_flash_attention(True)
block.attn2.set_processor(AttnProcessorNPU())
else:
raise ValueError("npu flash attention requires torch_npu extensions and is supported only on npu device ")

Expand Down
3 changes: 2 additions & 1 deletion examples/text_to_image/train_text_to_image_lora_sdxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
UNet2DConditionModel,
)
from diffusers.loaders import StableDiffusionLoraLoaderMixin
from diffusers.models.attention_processor import AttnProcessorNPU
from diffusers.optimization import get_scheduler
from diffusers.training_utils import _set_state_dict_into_text_encoder, cast_training_params, compute_snr
from diffusers.utils import (
Expand Down Expand Up @@ -679,7 +680,7 @@ def main(args):
if args.enable_npu_flash_attention:
if is_torch_npu_available():
logger.info("npu flash attention enabled.")
unet.enable_npu_flash_attention()
unet.set_attn_processor(AttnProcessorNPU())
else:
raise ValueError("npu flash attention requires torch_npu extensions and is supported only on npu devices.")

Expand Down
3 changes: 2 additions & 1 deletion examples/text_to_image/train_text_to_image_sdxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import diffusers
from diffusers import AutoencoderKL, DDPMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel
from diffusers.models.attention_processor import AttnProcessorNPU
from diffusers.optimization import get_scheduler
from diffusers.training_utils import EMAModel, compute_snr
from diffusers.utils import check_min_version, is_wandb_available
Expand Down Expand Up @@ -736,7 +737,7 @@ def main(args):
if args.enable_npu_flash_attention:
if is_torch_npu_available():
logger.info("npu flash attention enabled.")
unet.enable_npu_flash_attention()
unet.set_attn_processor(AttnProcessorNPU())
else:
raise ValueError("npu flash attention requires torch_npu extensions and is supported only on npu devices.")
if args.enable_xformers_memory_efficient_attention:
Expand Down
37 changes: 21 additions & 16 deletions src/diffusers/models/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,15 @@ def set_attention_backend(self, backend: str):
self.processor._attention_backend = backend

def set_use_npu_flash_attention(self, use_npu_flash_attention: bool) -> None:
"""
Set whether to use NPU flash attention from `torch_npu` or not.

Args:
use_npu_flash_attention (`bool`): Whether to use NPU flash attention or not.
"""
# Deprecated and scheduled for removal in version 0.46.0.
deprecation_message = (
"`set_use_npu_flash_attention` is deprecated and will be removed in version 0.46.0. Use"
' `set_attention_backend("_native_npu")` instead.'
)
deprecate("set_use_npu_flash_attention", "0.46.0", deprecation_message, standard_warn=False)
self._set_use_npu_flash_attention(use_npu_flash_attention)

def _set_use_npu_flash_attention(self, use_npu_flash_attention: bool) -> None:
if use_npu_flash_attention:
if not is_torch_npu_available():
raise ImportError("torch_npu is not available")
Expand All @@ -194,17 +196,20 @@ def set_use_xla_flash_attention(
partition_spec: tuple[str | None, ...] | None = None,
is_flux=False,
) -> None:
"""
Set whether to use XLA flash attention from `torch_xla` or not.
# Deprecated and scheduled for removal in version 0.46.0.
deprecation_message = (
"`set_use_xla_flash_attention` is deprecated and will be removed in version 0.46.0. Use"
' `set_attention_backend("_native_xla")` instead.'
)
deprecate("set_use_xla_flash_attention", "0.46.0", deprecation_message, standard_warn=False)
self._set_use_xla_flash_attention(use_xla_flash_attention, partition_spec, is_flux)

Args:
use_xla_flash_attention (`bool`):
Whether to use pallas flash attention kernel from `torch_xla` or not.
partition_spec (`tuple[]`, *optional*):
Specify the partition specification if using SPMD. Otherwise None.
is_flux (`bool`, *optional*, defaults to `False`):
Whether the model is a Flux model.
"""
def _set_use_xla_flash_attention(
self,
use_xla_flash_attention: bool,
partition_spec: tuple[str | None, ...] | None = None,
is_flux=False,
) -> None:
if use_xla_flash_attention:
if not is_torch_xla_available():
raise ImportError("torch_xla is not available")
Expand Down
32 changes: 21 additions & 11 deletions src/diffusers/models/attention_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,20 @@ def set_use_xla_flash_attention(
partition_spec: tuple[str | None, ...] | None = None,
is_flux=False,
) -> None:
r"""
Set whether to use xla flash attention from `torch_xla` or not.
# Deprecated and scheduled for removal in version 0.46.0.
deprecation_message = (
"`set_use_xla_flash_attention` is deprecated and will be removed in version 0.46.0. Use"
" `set_processor(XLAFlashAttnProcessor2_0(partition_spec))` instead."
)
deprecate("set_use_xla_flash_attention", "0.46.0", deprecation_message, standard_warn=False)
self._set_use_xla_flash_attention(use_xla_flash_attention, partition_spec, is_flux)

Args:
use_xla_flash_attention (`bool`):
Whether to use pallas flash attention kernel from `torch_xla` or not.
partition_spec (`tuple[]`, *optional*):
Specify the partition specification if using SPMD. Otherwise None.
"""
def _set_use_xla_flash_attention(
self,
use_xla_flash_attention: bool,
partition_spec: tuple[str | None, ...] | None = None,
is_flux=False,
) -> None:
if use_xla_flash_attention:
if not is_torch_xla_available:
raise "torch_xla is not available"
Expand All @@ -342,10 +347,15 @@ def set_use_xla_flash_attention(
self.set_processor(processor)

def set_use_npu_flash_attention(self, use_npu_flash_attention: bool) -> None:
r"""
Set whether to use npu flash attention from `torch_npu` or not.
# Deprecated and scheduled for removal in version 0.46.0.
deprecation_message = (
"`set_use_npu_flash_attention` is deprecated and will be removed in version 0.46.0. Use"
" `set_processor(AttnProcessorNPU())` instead."
)
deprecate("set_use_npu_flash_attention", "0.46.0", deprecation_message, standard_warn=False)
self._set_use_npu_flash_attention(use_npu_flash_attention)

"""
def _set_use_npu_flash_attention(self, use_npu_flash_attention: bool) -> None:
if use_npu_flash_attention:
processor = AttnProcessorNPU()
else:
Expand Down
71 changes: 48 additions & 23 deletions src/diffusers/models/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ def __exit__(self, *args, **kwargs):

_LOW_CPU_MEM_USAGE_DEFAULT = True

NPU_FLASH_ATTENTION_DEPRECATION_MESSAGE = (
"`enable_npu_flash_attention`, `disable_npu_flash_attention` and `set_use_npu_flash_attention` are deprecated and"
' will be removed in version 0.46.0. Use `set_attention_backend("_native_npu")` instead. For models that do not'
" support attention backends, such as UNets, use `set_attn_processor(AttnProcessorNPU())`."
)
XLA_FLASH_ATTENTION_DEPRECATION_MESSAGE = (
"`enable_xla_flash_attention`, `disable_xla_flash_attention` and `set_use_xla_flash_attention` are deprecated and"
' will be removed in version 0.46.0. Use `set_attention_backend("_native_xla")` instead. For models that do not'
" support attention backends, such as UNets, use `set_attn_processor(XLAFlashAttnProcessor2_0(partition_spec))`."
)


if is_accelerate_available():
import accelerate
Expand Down Expand Up @@ -329,12 +340,17 @@ def disable_gradient_checkpointing(self) -> None:
self._set_gradient_checkpointing(enable=False)

def set_use_npu_flash_attention(self, valid: bool) -> None:
r"""
Set the switch for the npu flash attention.
"""
# Deprecated and scheduled for removal in version 0.46.0.
deprecate(
"set_use_npu_flash_attention", "0.46.0", NPU_FLASH_ATTENTION_DEPRECATION_MESSAGE, standard_warn=False
)
self._set_use_npu_flash_attention(valid)

def _set_use_npu_flash_attention(self, valid: bool) -> None:
def fn_recursive_set_npu_flash_attention(module: torch.nn.Module):
if hasattr(module, "set_use_npu_flash_attention"):
if hasattr(module, "_set_use_npu_flash_attention"):
module._set_use_npu_flash_attention(valid)
elif hasattr(module, "set_use_npu_flash_attention"):
module.set_use_npu_flash_attention(valid)

for child in module.children():
Expand All @@ -345,27 +361,36 @@ def fn_recursive_set_npu_flash_attention(module: torch.nn.Module):
fn_recursive_set_npu_flash_attention(module)

def enable_npu_flash_attention(self) -> None:
r"""
Enable npu flash attention from torch_npu

"""
self.set_use_npu_flash_attention(True)
# Deprecated and scheduled for removal in version 0.46.0.
deprecate("enable_npu_flash_attention", "0.46.0", NPU_FLASH_ATTENTION_DEPRECATION_MESSAGE, standard_warn=False)
self._set_use_npu_flash_attention(True)

def disable_npu_flash_attention(self) -> None:
r"""
disable npu flash attention from torch_npu

"""
self.set_use_npu_flash_attention(False)
# Deprecated and scheduled for removal in version 0.46.0.
deprecate(
"disable_npu_flash_attention", "0.46.0", NPU_FLASH_ATTENTION_DEPRECATION_MESSAGE, standard_warn=False
)
self._set_use_npu_flash_attention(False)

def set_use_xla_flash_attention(
self, use_xla_flash_attention: bool, partition_spec: Callable | None = None, **kwargs
) -> None:
# Deprecated and scheduled for removal in version 0.46.0.
deprecate(
"set_use_xla_flash_attention", "0.46.0", XLA_FLASH_ATTENTION_DEPRECATION_MESSAGE, standard_warn=False
)
self._set_use_xla_flash_attention(use_xla_flash_attention, partition_spec, **kwargs)

def _set_use_xla_flash_attention(
self, use_xla_flash_attention: bool, partition_spec: Callable | None = None, **kwargs
) -> None:
# Recursively walk through all the children.
# Any children which exposes the set_use_xla_flash_attention method
# gets the message
def fn_recursive_set_flash_attention(module: torch.nn.Module):
if hasattr(module, "set_use_xla_flash_attention"):
if hasattr(module, "_set_use_xla_flash_attention"):
module._set_use_xla_flash_attention(use_xla_flash_attention, partition_spec, **kwargs)
elif hasattr(module, "set_use_xla_flash_attention"):
module.set_use_xla_flash_attention(use_xla_flash_attention, partition_spec, **kwargs)

for child in module.children():
Expand All @@ -376,16 +401,16 @@ def fn_recursive_set_flash_attention(module: torch.nn.Module):
fn_recursive_set_flash_attention(module)

def enable_xla_flash_attention(self, partition_spec: Callable | None = None, **kwargs):
r"""
Enable the flash attention pallals kernel for torch_xla.
"""
self.set_use_xla_flash_attention(True, partition_spec, **kwargs)
# Deprecated and scheduled for removal in version 0.46.0.
deprecate("enable_xla_flash_attention", "0.46.0", XLA_FLASH_ATTENTION_DEPRECATION_MESSAGE, standard_warn=False)
self._set_use_xla_flash_attention(True, partition_spec, **kwargs)

def disable_xla_flash_attention(self):
r"""
Disable the flash attention pallals kernel for torch_xla.
"""
self.set_use_xla_flash_attention(False)
# Deprecated and scheduled for removal in version 0.46.0.
deprecate(
"disable_xla_flash_attention", "0.46.0", XLA_FLASH_ATTENTION_DEPRECATION_MESSAGE, standard_warn=False
)
self._set_use_xla_flash_attention(False)

def set_use_memory_efficient_attention_xformers(self, valid: bool, attention_op: Callable | None = None) -> None:
# Recursively walk through all the children.
Expand Down
Loading
Loading