| | import torch |
| | import torch.nn as nn |
| | import torch.nn.functional as F |
| | import math |
| | from diffusers.models.attention_processor import Attention |
| | from typing import Optional |
| | from diffusers.models.embeddings import apply_rotary_emb |
| |
|
| |
|
| | class FluxAttnProcessor2_0: |
| | """Attention processor used typically in processing the SD3-like self-attention projections.""" |
| |
|
| | def __init__(self, train_seq_len=512 + 64 * 64): |
| | if not hasattr(F, "scaled_dot_product_attention"): |
| | raise ImportError( |
| | "FluxAttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0." |
| | ) |
| | self.train_seq_len = train_seq_len |
| |
|
| | def __call__( |
| | self, |
| | attn: Attention, |
| | hidden_states: torch.FloatTensor, |
| | encoder_hidden_states: torch.FloatTensor = None, |
| | attention_mask: Optional[torch.FloatTensor] = None, |
| | image_rotary_emb: Optional[torch.Tensor] = None, |
| | proportional_attention=False, |
| | ) -> torch.FloatTensor: |
| | batch_size, _, _ = ( |
| | hidden_states.shape |
| | if encoder_hidden_states is None |
| | else encoder_hidden_states.shape |
| | ) |
| |
|
| | |
| | query = attn.to_q(hidden_states) |
| | key = attn.to_k(hidden_states) |
| | value = attn.to_v(hidden_states) |
| |
|
| | inner_dim = key.shape[-1] |
| | head_dim = inner_dim // attn.heads |
| |
|
| | query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) |
| | key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) |
| | value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) |
| |
|
| | if attn.norm_q is not None: |
| | query = attn.norm_q(query) |
| | if attn.norm_k is not None: |
| | key = attn.norm_k(key) |
| |
|
| | |
| | if encoder_hidden_states is not None: |
| | |
| | encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states) |
| | encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states) |
| | encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states) |
| |
|
| | encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.view( |
| | batch_size, -1, attn.heads, head_dim |
| | ).transpose(1, 2) |
| | encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.view( |
| | batch_size, -1, attn.heads, head_dim |
| | ).transpose(1, 2) |
| | encoder_hidden_states_value_proj = encoder_hidden_states_value_proj.view( |
| | batch_size, -1, attn.heads, head_dim |
| | ).transpose(1, 2) |
| |
|
| | if attn.norm_added_q is not None: |
| | encoder_hidden_states_query_proj = attn.norm_added_q( |
| | encoder_hidden_states_query_proj |
| | ) |
| | if attn.norm_added_k is not None: |
| | encoder_hidden_states_key_proj = attn.norm_added_k( |
| | encoder_hidden_states_key_proj |
| | ) |
| |
|
| | |
| | query = torch.cat([encoder_hidden_states_query_proj, query], dim=2) |
| | key = torch.cat([encoder_hidden_states_key_proj, key], dim=2) |
| | value = torch.cat([encoder_hidden_states_value_proj, value], dim=2) |
| |
|
| | if image_rotary_emb is not None: |
| | query = apply_rotary_emb(query, image_rotary_emb) |
| | key = apply_rotary_emb(key, image_rotary_emb) |
| |
|
| | if proportional_attention: |
| | attention_scale = math.sqrt( |
| | math.log(key.size(2), self.train_seq_len) / head_dim |
| | ) |
| | else: |
| | attention_scale = math.sqrt(1 / head_dim) |
| |
|
| | hidden_states = F.scaled_dot_product_attention( |
| | query, key, value, dropout_p=0.0, is_causal=False, scale=attention_scale |
| | ) |
| | hidden_states = hidden_states.transpose(1, 2).reshape( |
| | batch_size, -1, attn.heads * head_dim |
| | ) |
| | hidden_states = hidden_states.to(query.dtype) |
| |
|
| | if encoder_hidden_states is not None: |
| | encoder_hidden_states, hidden_states = ( |
| | hidden_states[:, : encoder_hidden_states.shape[1]], |
| | hidden_states[:, encoder_hidden_states.shape[1] :], |
| | ) |
| |
|
| | |
| | hidden_states = attn.to_out[0](hidden_states) |
| | |
| | hidden_states = attn.to_out[1](hidden_states) |
| | encoder_hidden_states = attn.to_add_out(encoder_hidden_states) |
| |
|
| | return hidden_states, encoder_hidden_states |
| | else: |
| | return hidden_states |
| |
|
| |
|
| | class FluxAttnAdaptationProcessor2_0(nn.Module): |
| | """Attention processor used typically in processing the SD3-like self-attention projections.""" |
| |
|
| | def __init__(self, rank=16, dim=3072, to_out=False, train_seq_len=512 + 64 * 64): |
| | super().__init__() |
| | if not hasattr(F, "scaled_dot_product_attention"): |
| | raise ImportError( |
| | "FluxAttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0." |
| | ) |
| | self.to_q_a = nn.Linear(dim, rank, bias=False) |
| | self.to_q_b = nn.Linear(rank, dim, bias=False) |
| | self.to_q_b.weight.data = torch.zeros_like(self.to_q_b.weight.data) |
| | self.to_k_a = nn.Linear(dim, rank, bias=False) |
| | self.to_k_b = nn.Linear(rank, dim, bias=False) |
| | self.to_k_b.weight.data = torch.zeros_like(self.to_k_b.weight.data) |
| | self.to_v_a = nn.Linear(dim, rank, bias=False) |
| | self.to_v_b = nn.Linear(rank, dim, bias=False) |
| | self.to_v_b.weight.data = torch.zeros_like(self.to_v_b.weight.data) |
| | if to_out: |
| | self.to_out_a = nn.Linear(dim, rank, bias=False) |
| | self.to_out_b = nn.Linear(rank, dim, bias=False) |
| | self.to_out_b.weight.data = torch.zeros_like(self.to_out_b.weight.data) |
| | self.train_seq_len = train_seq_len |
| |
|
| | def __call__( |
| | self, |
| | attn: Attention, |
| | hidden_states: torch.FloatTensor, |
| | encoder_hidden_states: torch.FloatTensor = None, |
| | attention_mask: Optional[torch.FloatTensor] = None, |
| | image_rotary_emb: Optional[torch.Tensor] = None, |
| | proportional_attention=False, |
| | ) -> torch.FloatTensor: |
| | batch_size, _, _ = ( |
| | hidden_states.shape |
| | if encoder_hidden_states is None |
| | else encoder_hidden_states.shape |
| | ) |
| |
|
| | use_adaptation = True |
| |
|
| | |
| | query = attn.to_q(hidden_states) |
| | key = attn.to_k(hidden_states) |
| | value = attn.to_v(hidden_states) |
| |
|
| | if use_adaptation: |
| | query += self.to_q_b(self.to_q_a(hidden_states)) |
| | key += self.to_k_b(self.to_k_a(hidden_states)) |
| | value += self.to_v_b(self.to_v_a(hidden_states)) |
| |
|
| | inner_dim = key.shape[-1] |
| | head_dim = inner_dim // attn.heads |
| |
|
| | query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) |
| | key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) |
| | value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2) |
| |
|
| | if attn.norm_q is not None: |
| | query = attn.norm_q(query) |
| | if attn.norm_k is not None: |
| | key = attn.norm_k(key) |
| |
|
| | |
| | if encoder_hidden_states is not None: |
| | |
| | encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states) |
| | encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states) |
| | encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states) |
| |
|
| | encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.view( |
| | batch_size, -1, attn.heads, head_dim |
| | ).transpose(1, 2) |
| | encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.view( |
| | batch_size, -1, attn.heads, head_dim |
| | ).transpose(1, 2) |
| | encoder_hidden_states_value_proj = encoder_hidden_states_value_proj.view( |
| | batch_size, -1, attn.heads, head_dim |
| | ).transpose(1, 2) |
| |
|
| | if attn.norm_added_q is not None: |
| | encoder_hidden_states_query_proj = attn.norm_added_q( |
| | encoder_hidden_states_query_proj |
| | ) |
| | if attn.norm_added_k is not None: |
| | encoder_hidden_states_key_proj = attn.norm_added_k( |
| | encoder_hidden_states_key_proj |
| | ) |
| |
|
| | |
| | query = torch.cat([encoder_hidden_states_query_proj, query], dim=2) |
| | key = torch.cat([encoder_hidden_states_key_proj, key], dim=2) |
| | value = torch.cat([encoder_hidden_states_value_proj, value], dim=2) |
| |
|
| | if image_rotary_emb is not None: |
| | query = apply_rotary_emb(query, image_rotary_emb) |
| | key = apply_rotary_emb(key, image_rotary_emb) |
| |
|
| | if proportional_attention: |
| | attention_scale = math.sqrt( |
| | math.log(key.size(2), self.train_seq_len) / head_dim |
| | ) |
| | else: |
| | attention_scale = math.sqrt(1 / head_dim) |
| |
|
| | hidden_states = F.scaled_dot_product_attention( |
| | query, key, value, dropout_p=0.0, is_causal=False, scale=attention_scale |
| | ) |
| | hidden_states = hidden_states.transpose(1, 2).reshape( |
| | batch_size, -1, attn.heads * head_dim |
| | ) |
| | hidden_states = hidden_states.to(query.dtype) |
| |
|
| | if encoder_hidden_states is not None: |
| | encoder_hidden_states, hidden_states = ( |
| | hidden_states[:, : encoder_hidden_states.shape[1]], |
| | hidden_states[:, encoder_hidden_states.shape[1] :], |
| | ) |
| |
|
| | |
| | hidden_states = ( |
| | ( |
| | attn.to_out[0](hidden_states) |
| | + self.to_out_b(self.to_out_a(hidden_states)) |
| | ) |
| | if use_adaptation |
| | else attn.to_out[0](hidden_states) |
| | ) |
| | |
| | hidden_states = attn.to_out[1](hidden_states) |
| | encoder_hidden_states = attn.to_add_out(encoder_hidden_states) |
| |
|
| | return hidden_states, encoder_hidden_states |
| | else: |
| | return hidden_states |
| |
|