Upload extensions_built_in/diffusion_models/hidream/src/models/attention.py with huggingface_hub
Browse files
extensions_built_in/diffusion_models/hidream/src/models/attention.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from diffusers.models.attention_processor import Attention
|
| 5 |
+
from diffusers.utils.torch_utils import maybe_allow_in_graph
|
| 6 |
+
|
| 7 |
+
@maybe_allow_in_graph
|
| 8 |
+
class HiDreamAttention(Attention):
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
query_dim: int,
|
| 12 |
+
heads: int = 8,
|
| 13 |
+
dim_head: int = 64,
|
| 14 |
+
upcast_attention: bool = False,
|
| 15 |
+
upcast_softmax: bool = False,
|
| 16 |
+
scale_qk: bool = True,
|
| 17 |
+
eps: float = 1e-5,
|
| 18 |
+
processor = None,
|
| 19 |
+
out_dim: int = None,
|
| 20 |
+
single: bool = False
|
| 21 |
+
):
|
| 22 |
+
super(Attention, self).__init__()
|
| 23 |
+
self.inner_dim = out_dim if out_dim is not None else dim_head * heads
|
| 24 |
+
self.query_dim = query_dim
|
| 25 |
+
self.upcast_attention = upcast_attention
|
| 26 |
+
self.upcast_softmax = upcast_softmax
|
| 27 |
+
self.out_dim = out_dim if out_dim is not None else query_dim
|
| 28 |
+
|
| 29 |
+
self.scale_qk = scale_qk
|
| 30 |
+
self.scale = dim_head**-0.5 if self.scale_qk else 1.0
|
| 31 |
+
|
| 32 |
+
self.heads = out_dim // dim_head if out_dim is not None else heads
|
| 33 |
+
self.sliceable_head_dim = heads
|
| 34 |
+
self.single = single
|
| 35 |
+
|
| 36 |
+
linear_cls = nn.Linear
|
| 37 |
+
self.linear_cls = linear_cls
|
| 38 |
+
self.to_q = linear_cls(query_dim, self.inner_dim)
|
| 39 |
+
self.to_k = linear_cls(self.inner_dim, self.inner_dim)
|
| 40 |
+
self.to_v = linear_cls(self.inner_dim, self.inner_dim)
|
| 41 |
+
self.to_out = linear_cls(self.inner_dim, self.out_dim)
|
| 42 |
+
self.q_rms_norm = nn.RMSNorm(self.inner_dim, eps)
|
| 43 |
+
self.k_rms_norm = nn.RMSNorm(self.inner_dim, eps)
|
| 44 |
+
|
| 45 |
+
if not single:
|
| 46 |
+
self.to_q_t = linear_cls(query_dim, self.inner_dim)
|
| 47 |
+
self.to_k_t = linear_cls(self.inner_dim, self.inner_dim)
|
| 48 |
+
self.to_v_t = linear_cls(self.inner_dim, self.inner_dim)
|
| 49 |
+
self.to_out_t = linear_cls(self.inner_dim, self.out_dim)
|
| 50 |
+
self.q_rms_norm_t = nn.RMSNorm(self.inner_dim, eps)
|
| 51 |
+
self.k_rms_norm_t = nn.RMSNorm(self.inner_dim, eps)
|
| 52 |
+
|
| 53 |
+
self.set_processor(processor)
|
| 54 |
+
self.apply(self._init_weights)
|
| 55 |
+
|
| 56 |
+
def _init_weights(self, m):
|
| 57 |
+
if isinstance(m, nn.Linear):
|
| 58 |
+
nn.init.xavier_uniform_(m.weight)
|
| 59 |
+
if m.bias is not None:
|
| 60 |
+
nn.init.constant_(m.bias, 0)
|
| 61 |
+
|
| 62 |
+
def forward(
|
| 63 |
+
self,
|
| 64 |
+
norm_image_tokens: torch.FloatTensor,
|
| 65 |
+
image_tokens_masks: torch.FloatTensor = None,
|
| 66 |
+
norm_text_tokens: torch.FloatTensor = None,
|
| 67 |
+
rope: torch.FloatTensor = None,
|
| 68 |
+
) -> torch.Tensor:
|
| 69 |
+
return self.processor(
|
| 70 |
+
self,
|
| 71 |
+
image_tokens = norm_image_tokens,
|
| 72 |
+
image_tokens_masks = image_tokens_masks,
|
| 73 |
+
text_tokens = norm_text_tokens,
|
| 74 |
+
rope = rope,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
class FeedForwardSwiGLU(nn.Module):
|
| 78 |
+
def __init__(
|
| 79 |
+
self,
|
| 80 |
+
dim: int,
|
| 81 |
+
hidden_dim: int,
|
| 82 |
+
multiple_of: int = 256,
|
| 83 |
+
ffn_dim_multiplier: Optional[float] = None,
|
| 84 |
+
):
|
| 85 |
+
super().__init__()
|
| 86 |
+
hidden_dim = int(2 * hidden_dim / 3)
|
| 87 |
+
# custom dim factor multiplier
|
| 88 |
+
if ffn_dim_multiplier is not None:
|
| 89 |
+
hidden_dim = int(ffn_dim_multiplier * hidden_dim)
|
| 90 |
+
hidden_dim = multiple_of * (
|
| 91 |
+
(hidden_dim + multiple_of - 1) // multiple_of
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
self.w1 = nn.Linear(dim, hidden_dim, bias=False)
|
| 95 |
+
self.w2 = nn.Linear(hidden_dim, dim, bias=False)
|
| 96 |
+
self.w3 = nn.Linear(dim, hidden_dim, bias=False)
|
| 97 |
+
self.apply(self._init_weights)
|
| 98 |
+
|
| 99 |
+
def _init_weights(self, m):
|
| 100 |
+
if isinstance(m, nn.Linear):
|
| 101 |
+
nn.init.xavier_uniform_(m.weight)
|
| 102 |
+
if m.bias is not None:
|
| 103 |
+
nn.init.constant_(m.bias, 0)
|
| 104 |
+
|
| 105 |
+
def forward(self, x):
|
| 106 |
+
return self.w2(torch.nn.functional.silu(self.w1(x)) * self.w3(x))
|