Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPU Adaption for Sanna #10409

Merged
merged 31 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
626e339
NPU Adaption for Sanna
Dec 30, 2024
1a72a00
NPU Adaption for Sanna
leisuzz Dec 30, 2024
4d67c54
Merge branch 'main' into main
sayakpaul Jan 3, 2025
a1965dd
NPU Adaption for Sanna
Jan 7, 2025
2c3b117
Merge https://github.com/leisuzz/diffusers
Jan 7, 2025
326b98d
NPU Adaption for Sanna
Jan 7, 2025
715822f
Merge branch 'main' into main
leisuzz Jan 7, 2025
963e290
NPU Adaption for Sanna
Jan 7, 2025
510e1d6
Merge https://github.com/leisuzz/diffusers
Jan 7, 2025
3d3aae3
NPU Adaption for Sanna
Jan 8, 2025
4cea819
NPU Adaption for Sanna
Jan 8, 2025
0d9e1b3
NPU Adaption for Sanna
Jan 8, 2025
2052049
NPU Adaption for Sanna
Jan 8, 2025
487dd1a
Merge branch 'main' into main
leisuzz Jan 13, 2025
cfbbb8f
NPU Adaption for Sanna
Jan 14, 2025
7b8ad74
Merge branch 'main' of https://github.com/leisuzz/diffusers
Jan 14, 2025
d7d54d8
Merge branch 'main' into main
leisuzz Jan 16, 2025
ad4beaa
Merge branch 'main' into main
leisuzz Jan 17, 2025
52d8c71
Merge branch 'main' into main
leisuzz Jan 22, 2025
4c1d56d
NPU Adaption for Sanna
Jan 23, 2025
63e3459
Merge https://github.com/leisuzz/diffusers
Jan 23, 2025
d61d570
NPU Adaption for Sanna
Jan 23, 2025
ab2d71b
NPU Adaption for Sanna
Jan 23, 2025
a323229
Merge branch 'main' into main
leisuzz Jan 23, 2025
a456fb1
NPU Adaption for Sanna
Jan 23, 2025
fedfdd4
NPU Adaption for Sanna
Jan 24, 2025
7364276
Merge branch 'main' of https://github.com/leisuzz/diffusers
Jan 24, 2025
3add6de
NPU Adaption for Sanna
Jan 24, 2025
70cf529
NPU Adaption for Sanna
Jan 24, 2025
8f18aae
NPU Adaption for Sanna
Jan 24, 2025
feb8064
Merge branch 'main' into main
leisuzz Jan 24, 2025
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
15 changes: 13 additions & 2 deletions examples/dreambooth/train_dreambooth_lora_sana.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
is_wandb_available,
)
from diffusers.utils.hub_utils import load_or_create_model_card, populate_model_card
from diffusers.utils.import_utils import is_torch_npu_available
from diffusers.utils.torch_utils import is_compiled_module


Expand All @@ -74,6 +75,9 @@

logger = get_logger(__name__)

if is_torch_npu_available():
torch.npu.config.allow_internal_format = False


def save_model_card(
repo_id: str,
Expand Down Expand Up @@ -601,6 +605,7 @@ def parse_args(input_args=None):
)
parser.add_argument("--local_rank", type=int, default=-1, help="For distributed training: local_rank")
parser.add_argument("--enable_vae_tiling", action="store_true", help="Enabla vae tiling in log validation")
parser.add_argument("--enable_npu_flash_attention", action="store_true", help="Enabla Flash Attention for NPU")

if input_args is not None:
args = parser.parse_args(input_args)
Expand Down Expand Up @@ -924,8 +929,7 @@ def main(args):
image.save(image_filename)

del pipeline
if torch.cuda.is_available():
torch.cuda.empty_cache()
free_memory()

# Handle the repository creation
if accelerator.is_main_process:
Expand Down Expand Up @@ -988,6 +992,13 @@ def main(args):
# because Gemma2 is particularly suited for bfloat16.
text_encoder.to(dtype=torch.bfloat16)

if args.enable_npu_flash_attention:
if is_torch_npu_available():
logger.info("npu flash attention enabled.")
transformer.enable_npu_flash_attention()
else:
raise ValueError("npu flash attention requires torch_npu extensions and is supported only on npu device ")

# Initialize a text encoding pipeline and keep it to CPU for now.
text_encoding_pipeline = SanaPipeline.from_pretrained(
args.pretrained_model_name_or_path,
Expand Down
5 changes: 5 additions & 0 deletions src/diffusers/models/attention_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3154,6 +3154,11 @@ def __call__(
# scaled_dot_product_attention expects attention_mask shape to be
# (batch, heads, source_length, target_length)
attention_mask = attention_mask.view(batch_size, attn.heads, -1, attention_mask.shape[-1])
attention_mask = attention_mask.repeat(1, 1, hidden_states.shape[1], 1)
if attention_mask.dtype == torch.bool:
attention_mask = torch.logical_not(attention_mask.bool())
else:
attention_mask = attention_mask.bool()

if attn.group_norm is not None:
hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)
Expand Down