Upload extensions_built_in/diffusion_models/omnigen2/src/models/transformers/block_lumina2.py with huggingface_hub
Browse files
extensions_built_in/diffusion_models/omnigen2/src/models/transformers/block_lumina2.py
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Copyright 2024 Alpha-VLLM Authors and The HuggingFace Team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
import warnings
|
| 17 |
+
from typing import Optional, Tuple
|
| 18 |
+
|
| 19 |
+
import torch
|
| 20 |
+
import torch.nn as nn
|
| 21 |
+
|
| 22 |
+
from diffusers.models.embeddings import Timesteps
|
| 23 |
+
from ..embeddings import TimestepEmbedding
|
| 24 |
+
|
| 25 |
+
from ...utils.import_utils import is_flash_attn_available, is_triton_available
|
| 26 |
+
|
| 27 |
+
if is_triton_available():
|
| 28 |
+
from ...ops.triton.layer_norm import RMSNorm
|
| 29 |
+
else:
|
| 30 |
+
from torch.nn import RMSNorm
|
| 31 |
+
warnings.warn("Cannot import triton, install triton to use fused RMSNorm for better performance")
|
| 32 |
+
|
| 33 |
+
if is_flash_attn_available():
|
| 34 |
+
from flash_attn.ops.activations import swiglu
|
| 35 |
+
else:
|
| 36 |
+
from .components import swiglu
|
| 37 |
+
warnings.warn("Cannot import flash_attn, install flash_attn to use fused SwiGLU for better performance")
|
| 38 |
+
|
| 39 |
+
# try:
|
| 40 |
+
# from flash_attn.ops.activations import swiglu as fused_swiglu
|
| 41 |
+
# FUSEDSWIGLU_AVALIBLE = True
|
| 42 |
+
# except ImportError:
|
| 43 |
+
|
| 44 |
+
# FUSEDSWIGLU_AVALIBLE = False
|
| 45 |
+
# warnings.warn("Cannot import apex RMSNorm, switch to vanilla implementation")
|
| 46 |
+
|
| 47 |
+
class LuminaRMSNormZero(nn.Module):
|
| 48 |
+
"""
|
| 49 |
+
Norm layer adaptive RMS normalization zero.
|
| 50 |
+
|
| 51 |
+
Parameters:
|
| 52 |
+
embedding_dim (`int`): The size of each embedding vector.
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
def __init__(
|
| 56 |
+
self,
|
| 57 |
+
embedding_dim: int,
|
| 58 |
+
norm_eps: float,
|
| 59 |
+
norm_elementwise_affine: bool,
|
| 60 |
+
):
|
| 61 |
+
super().__init__()
|
| 62 |
+
self.silu = nn.SiLU()
|
| 63 |
+
self.linear = nn.Linear(
|
| 64 |
+
min(embedding_dim, 1024),
|
| 65 |
+
4 * embedding_dim,
|
| 66 |
+
bias=True,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
self.norm = RMSNorm(embedding_dim, eps=norm_eps)
|
| 70 |
+
|
| 71 |
+
def forward(
|
| 72 |
+
self,
|
| 73 |
+
x: torch.Tensor,
|
| 74 |
+
emb: Optional[torch.Tensor] = None,
|
| 75 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
| 76 |
+
emb = self.linear(self.silu(emb))
|
| 77 |
+
scale_msa, gate_msa, scale_mlp, gate_mlp = emb.chunk(4, dim=1)
|
| 78 |
+
x = self.norm(x) * (1 + scale_msa[:, None])
|
| 79 |
+
return x, gate_msa, scale_mlp, gate_mlp
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class LuminaLayerNormContinuous(nn.Module):
|
| 83 |
+
def __init__(
|
| 84 |
+
self,
|
| 85 |
+
embedding_dim: int,
|
| 86 |
+
conditioning_embedding_dim: int,
|
| 87 |
+
# NOTE: It is a bit weird that the norm layer can be configured to have scale and shift parameters
|
| 88 |
+
# because the output is immediately scaled and shifted by the projected conditioning embeddings.
|
| 89 |
+
# Note that AdaLayerNorm does not let the norm layer have scale and shift parameters.
|
| 90 |
+
# However, this is how it was implemented in the original code, and it's rather likely you should
|
| 91 |
+
# set `elementwise_affine` to False.
|
| 92 |
+
elementwise_affine=True,
|
| 93 |
+
eps=1e-5,
|
| 94 |
+
bias=True,
|
| 95 |
+
norm_type="layer_norm",
|
| 96 |
+
out_dim: Optional[int] = None,
|
| 97 |
+
):
|
| 98 |
+
super().__init__()
|
| 99 |
+
|
| 100 |
+
# AdaLN
|
| 101 |
+
self.silu = nn.SiLU()
|
| 102 |
+
self.linear_1 = nn.Linear(conditioning_embedding_dim, embedding_dim, bias=bias)
|
| 103 |
+
|
| 104 |
+
if norm_type == "layer_norm":
|
| 105 |
+
self.norm = nn.LayerNorm(embedding_dim, eps, elementwise_affine, bias)
|
| 106 |
+
elif norm_type == "rms_norm":
|
| 107 |
+
self.norm = RMSNorm(embedding_dim, eps=eps, elementwise_affine=elementwise_affine)
|
| 108 |
+
else:
|
| 109 |
+
raise ValueError(f"unknown norm_type {norm_type}")
|
| 110 |
+
|
| 111 |
+
self.linear_2 = None
|
| 112 |
+
if out_dim is not None:
|
| 113 |
+
self.linear_2 = nn.Linear(embedding_dim, out_dim, bias=bias)
|
| 114 |
+
|
| 115 |
+
def forward(
|
| 116 |
+
self,
|
| 117 |
+
x: torch.Tensor,
|
| 118 |
+
conditioning_embedding: torch.Tensor,
|
| 119 |
+
) -> torch.Tensor:
|
| 120 |
+
# convert back to the original dtype in case `conditioning_embedding`` is upcasted to float32 (needed for hunyuanDiT)
|
| 121 |
+
emb = self.linear_1(self.silu(conditioning_embedding).to(x.dtype))
|
| 122 |
+
scale = emb
|
| 123 |
+
x = self.norm(x) * (1 + scale)[:, None, :]
|
| 124 |
+
|
| 125 |
+
if self.linear_2 is not None:
|
| 126 |
+
x = self.linear_2(x)
|
| 127 |
+
|
| 128 |
+
return x
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
class LuminaFeedForward(nn.Module):
|
| 132 |
+
r"""
|
| 133 |
+
A feed-forward layer.
|
| 134 |
+
|
| 135 |
+
Parameters:
|
| 136 |
+
hidden_size (`int`):
|
| 137 |
+
The dimensionality of the hidden layers in the model. This parameter determines the width of the model's
|
| 138 |
+
hidden representations.
|
| 139 |
+
intermediate_size (`int`): The intermediate dimension of the feedforward layer.
|
| 140 |
+
multiple_of (`int`, *optional*): Value to ensure hidden dimension is a multiple
|
| 141 |
+
of this value.
|
| 142 |
+
ffn_dim_multiplier (float, *optional*): Custom multiplier for hidden
|
| 143 |
+
dimension. Defaults to None.
|
| 144 |
+
"""
|
| 145 |
+
|
| 146 |
+
def __init__(
|
| 147 |
+
self,
|
| 148 |
+
dim: int,
|
| 149 |
+
inner_dim: int,
|
| 150 |
+
multiple_of: Optional[int] = 256,
|
| 151 |
+
ffn_dim_multiplier: Optional[float] = None,
|
| 152 |
+
):
|
| 153 |
+
super().__init__()
|
| 154 |
+
self.swiglu = swiglu
|
| 155 |
+
|
| 156 |
+
# custom hidden_size factor multiplier
|
| 157 |
+
if ffn_dim_multiplier is not None:
|
| 158 |
+
inner_dim = int(ffn_dim_multiplier * inner_dim)
|
| 159 |
+
inner_dim = multiple_of * ((inner_dim + multiple_of - 1) // multiple_of)
|
| 160 |
+
|
| 161 |
+
self.linear_1 = nn.Linear(
|
| 162 |
+
dim,
|
| 163 |
+
inner_dim,
|
| 164 |
+
bias=False,
|
| 165 |
+
)
|
| 166 |
+
self.linear_2 = nn.Linear(
|
| 167 |
+
inner_dim,
|
| 168 |
+
dim,
|
| 169 |
+
bias=False,
|
| 170 |
+
)
|
| 171 |
+
self.linear_3 = nn.Linear(
|
| 172 |
+
dim,
|
| 173 |
+
inner_dim,
|
| 174 |
+
bias=False,
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
def forward(self, x):
|
| 178 |
+
h1, h2 = self.linear_1(x), self.linear_3(x)
|
| 179 |
+
return self.linear_2(self.swiglu(h1, h2))
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
class Lumina2CombinedTimestepCaptionEmbedding(nn.Module):
|
| 183 |
+
def __init__(
|
| 184 |
+
self,
|
| 185 |
+
hidden_size: int = 4096,
|
| 186 |
+
text_feat_dim: int = 2048,
|
| 187 |
+
frequency_embedding_size: int = 256,
|
| 188 |
+
norm_eps: float = 1e-5,
|
| 189 |
+
timestep_scale: float = 1.0,
|
| 190 |
+
) -> None:
|
| 191 |
+
super().__init__()
|
| 192 |
+
|
| 193 |
+
self.time_proj = Timesteps(
|
| 194 |
+
num_channels=frequency_embedding_size, flip_sin_to_cos=True, downscale_freq_shift=0.0, scale=timestep_scale
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
self.timestep_embedder = TimestepEmbedding(
|
| 198 |
+
in_channels=frequency_embedding_size, time_embed_dim=min(hidden_size, 1024)
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
self.caption_embedder = nn.Sequential(
|
| 202 |
+
RMSNorm(text_feat_dim, eps=norm_eps),
|
| 203 |
+
nn.Linear(text_feat_dim, hidden_size, bias=True),
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
self._initialize_weights()
|
| 207 |
+
|
| 208 |
+
def _initialize_weights(self):
|
| 209 |
+
nn.init.trunc_normal_(self.caption_embedder[1].weight, std=0.02)
|
| 210 |
+
nn.init.zeros_(self.caption_embedder[1].bias)
|
| 211 |
+
|
| 212 |
+
def forward(
|
| 213 |
+
self, timestep: torch.Tensor, text_hidden_states: torch.Tensor, dtype: torch.dtype
|
| 214 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 215 |
+
timestep_proj = self.time_proj(timestep).to(dtype=dtype)
|
| 216 |
+
time_embed = self.timestep_embedder(timestep_proj)
|
| 217 |
+
caption_embed = self.caption_embedder(text_hidden_states)
|
| 218 |
+
return time_embed, caption_embed
|