Upload extensions_built_in/diffusion_models/hidream/src/models/attention_processor.py with huggingface_hub
Browse files
extensions_built_in/diffusion_models/hidream/src/models/attention_processor.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
import torch
|
| 3 |
+
from .attention import HiDreamAttention
|
| 4 |
+
|
| 5 |
+
# Try to import Flash Attention first
|
| 6 |
+
flash_attn_available = False
|
| 7 |
+
try:
|
| 8 |
+
from flash_attn_interface import flash_attn_func
|
| 9 |
+
USE_FLASH_ATTN3 = True
|
| 10 |
+
flash_attn_available = True
|
| 11 |
+
except ImportError:
|
| 12 |
+
try:
|
| 13 |
+
from flash_attn import flash_attn_func
|
| 14 |
+
USE_FLASH_ATTN3 = False
|
| 15 |
+
flash_attn_available = True
|
| 16 |
+
except ImportError:
|
| 17 |
+
USE_FLASH_ATTN3 = False
|
| 18 |
+
flash_attn_available = False
|
| 19 |
+
|
| 20 |
+
# Copied from https://github.com/black-forest-labs/flux/blob/main/src/flux/math.py
|
| 21 |
+
def apply_rope(xq: torch.Tensor, xk: torch.Tensor, freqs_cis: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
| 22 |
+
xq_ = xq.float().reshape(*xq.shape[:-1], -1, 1, 2)
|
| 23 |
+
xk_ = xk.float().reshape(*xk.shape[:-1], -1, 1, 2)
|
| 24 |
+
xq_out = freqs_cis[..., 0] * xq_[..., 0] + freqs_cis[..., 1] * xq_[..., 1]
|
| 25 |
+
xk_out = freqs_cis[..., 0] * xk_[..., 0] + freqs_cis[..., 1] * xk_[..., 1]
|
| 26 |
+
return xq_out.reshape(*xq.shape).type_as(xq), xk_out.reshape(*xk.shape).type_as(xk)
|
| 27 |
+
|
| 28 |
+
def attention(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor):
|
| 29 |
+
if flash_attn_available:
|
| 30 |
+
if USE_FLASH_ATTN3:
|
| 31 |
+
hidden_states = flash_attn_func(query, key, value, causal=False, deterministic=False)[0]
|
| 32 |
+
else:
|
| 33 |
+
hidden_states = flash_attn_func(query, key, value, dropout_p=0., causal=False)
|
| 34 |
+
else:
|
| 35 |
+
# Use torch's scaled dot-product attention as fallback
|
| 36 |
+
# Reshape for torch.nn.functional.scaled_dot_product_attention which expects [batch, heads, seq_len, head_dim]
|
| 37 |
+
query = query.transpose(1, 2) # [batch, heads, seq_len, head_dim]
|
| 38 |
+
key = key.transpose(1, 2)
|
| 39 |
+
value = value.transpose(1, 2)
|
| 40 |
+
|
| 41 |
+
hidden_states = torch.nn.functional.scaled_dot_product_attention(
|
| 42 |
+
query, key, value,
|
| 43 |
+
attn_mask=None,
|
| 44 |
+
dropout_p=0.0,
|
| 45 |
+
is_causal=False
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Restore original shape
|
| 49 |
+
hidden_states = hidden_states.transpose(1, 2) # [batch, seq_len, heads, head_dim]
|
| 50 |
+
|
| 51 |
+
hidden_states = hidden_states.flatten(-2)
|
| 52 |
+
hidden_states = hidden_states.to(query.dtype)
|
| 53 |
+
return hidden_states
|
| 54 |
+
|
| 55 |
+
class HiDreamAttnProcessor_flashattn:
|
| 56 |
+
"""Attention processor used typically in processing the SD3-like self-attention projections."""
|
| 57 |
+
|
| 58 |
+
def __call__(
|
| 59 |
+
self,
|
| 60 |
+
attn: HiDreamAttention,
|
| 61 |
+
image_tokens: torch.FloatTensor,
|
| 62 |
+
image_tokens_masks: Optional[torch.FloatTensor] = None,
|
| 63 |
+
text_tokens: Optional[torch.FloatTensor] = None,
|
| 64 |
+
rope: torch.FloatTensor = None,
|
| 65 |
+
*args,
|
| 66 |
+
**kwargs,
|
| 67 |
+
) -> torch.FloatTensor:
|
| 68 |
+
dtype = image_tokens.dtype
|
| 69 |
+
batch_size = image_tokens.shape[0]
|
| 70 |
+
|
| 71 |
+
query_i = attn.q_rms_norm(attn.to_q(image_tokens)).to(dtype=dtype)
|
| 72 |
+
key_i = attn.k_rms_norm(attn.to_k(image_tokens)).to(dtype=dtype)
|
| 73 |
+
value_i = attn.to_v(image_tokens)
|
| 74 |
+
|
| 75 |
+
inner_dim = key_i.shape[-1]
|
| 76 |
+
head_dim = inner_dim // attn.heads
|
| 77 |
+
|
| 78 |
+
query_i = query_i.view(batch_size, -1, attn.heads, head_dim)
|
| 79 |
+
key_i = key_i.view(batch_size, -1, attn.heads, head_dim)
|
| 80 |
+
value_i = value_i.view(batch_size, -1, attn.heads, head_dim)
|
| 81 |
+
if image_tokens_masks is not None:
|
| 82 |
+
key_i = key_i * image_tokens_masks.view(batch_size, -1, 1, 1)
|
| 83 |
+
|
| 84 |
+
if not attn.single:
|
| 85 |
+
query_t = attn.q_rms_norm_t(attn.to_q_t(text_tokens)).to(dtype=dtype)
|
| 86 |
+
key_t = attn.k_rms_norm_t(attn.to_k_t(text_tokens)).to(dtype=dtype)
|
| 87 |
+
value_t = attn.to_v_t(text_tokens)
|
| 88 |
+
|
| 89 |
+
query_t = query_t.view(batch_size, -1, attn.heads, head_dim)
|
| 90 |
+
key_t = key_t.view(batch_size, -1, attn.heads, head_dim)
|
| 91 |
+
value_t = value_t.view(batch_size, -1, attn.heads, head_dim)
|
| 92 |
+
|
| 93 |
+
num_image_tokens = query_i.shape[1]
|
| 94 |
+
num_text_tokens = query_t.shape[1]
|
| 95 |
+
query = torch.cat([query_i, query_t], dim=1)
|
| 96 |
+
key = torch.cat([key_i, key_t], dim=1)
|
| 97 |
+
value = torch.cat([value_i, value_t], dim=1)
|
| 98 |
+
else:
|
| 99 |
+
query = query_i
|
| 100 |
+
key = key_i
|
| 101 |
+
value = value_i
|
| 102 |
+
|
| 103 |
+
if query.shape[-1] == rope.shape[-3] * 2:
|
| 104 |
+
query, key = apply_rope(query, key, rope)
|
| 105 |
+
else:
|
| 106 |
+
query_1, query_2 = query.chunk(2, dim=-1)
|
| 107 |
+
key_1, key_2 = key.chunk(2, dim=-1)
|
| 108 |
+
query_1, key_1 = apply_rope(query_1, key_1, rope)
|
| 109 |
+
query = torch.cat([query_1, query_2], dim=-1)
|
| 110 |
+
key = torch.cat([key_1, key_2], dim=-1)
|
| 111 |
+
|
| 112 |
+
hidden_states = attention(query, key, value)
|
| 113 |
+
|
| 114 |
+
if not attn.single:
|
| 115 |
+
hidden_states_i, hidden_states_t = torch.split(hidden_states, [num_image_tokens, num_text_tokens], dim=1)
|
| 116 |
+
hidden_states_i = attn.to_out(hidden_states_i)
|
| 117 |
+
hidden_states_t = attn.to_out_t(hidden_states_t)
|
| 118 |
+
return hidden_states_i, hidden_states_t
|
| 119 |
+
else:
|
| 120 |
+
hidden_states = attn.to_out(hidden_states)
|
| 121 |
+
return hidden_states
|