MCPcopy Create free account
hub / github.com/huggingface/diffusers / AdaLayerNormZero

Class AdaLayerNormZero

src/diffusers/models/normalization.py:130–170  ·  view source on GitHub ↗

r""" Norm layer adaptive layer norm zero (adaLN-Zero). Parameters: embedding_dim (`int`): The size of each embedding vector. num_embeddings (`int`): The size of the embeddings dictionary.

Source from the content-addressed store, hash-verified

128
129
130class AdaLayerNormZero(nn.Module):
131 r"""
132 Norm layer adaptive layer norm zero (adaLN-Zero).
133
134 Parameters:
135 embedding_dim (`int`): The size of each embedding vector.
136 num_embeddings (`int`): The size of the embeddings dictionary.
137 """
138
139 def __init__(self, embedding_dim: int, num_embeddings: int | None = None, norm_type="layer_norm", bias=True):
140 super().__init__()
141 if num_embeddings is not None:
142 self.emb = CombinedTimestepLabelEmbeddings(num_embeddings, embedding_dim)
143 else:
144 self.emb = None
145
146 self.silu = nn.SiLU()
147 self.linear = nn.Linear(embedding_dim, 6 * embedding_dim, bias=bias)
148 if norm_type == "layer_norm":
149 self.norm = nn.LayerNorm(embedding_dim, elementwise_affine=False, eps=1e-6)
150 elif norm_type == "fp32_layer_norm":
151 self.norm = FP32LayerNorm(embedding_dim, elementwise_affine=False, bias=False)
152 else:
153 raise ValueError(
154 f"Unsupported `norm_type` ({norm_type}) provided. Supported ones are: 'layer_norm', 'fp32_layer_norm'."
155 )
156
157 def forward(
158 self,
159 x: torch.Tensor,
160 timestep: torch.Tensor | None = None,
161 class_labels: torch.LongTensor | None = None,
162 hidden_dtype: torch.dtype | None = None,
163 emb: torch.Tensor | None = None,
164 ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
165 if self.emb is not None:
166 emb = self.emb(timestep, class_labels, hidden_dtype=hidden_dtype)
167 emb = self.linear(self.silu(emb))
168 shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = emb.chunk(6, dim=1)
169 x = self.norm(x) * (1 + scale_msa[:, None]) + shift_msa[:, None]
170 return x, gate_msa, shift_mlp, scale_mlp, gate_mlp
171
172
173class AdaLayerNormZeroSingle(nn.Module):

Callers 14

__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…