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

Class SD35AdaLayerNormZeroX

src/diffusers/models/normalization.py:96–127  ·  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

94
95
96class SD35AdaLayerNormZeroX(nn.Module):
97 r"""
98 Norm layer adaptive layer norm zero (AdaLN-Zero).
99
100 Parameters:
101 embedding_dim (`int`): The size of each embedding vector.
102 num_embeddings (`int`): The size of the embeddings dictionary.
103 """
104
105 def __init__(self, embedding_dim: int, norm_type: str = "layer_norm", bias: bool = True) -> None:
106 super().__init__()
107
108 self.silu = nn.SiLU()
109 self.linear = nn.Linear(embedding_dim, 9 * embedding_dim, bias=bias)
110 if norm_type == "layer_norm":
111 self.norm = nn.LayerNorm(embedding_dim, elementwise_affine=False, eps=1e-6)
112 else:
113 raise ValueError(f"Unsupported `norm_type` ({norm_type}) provided. Supported ones are: 'layer_norm'.")
114
115 def forward(
116 self,
117 hidden_states: torch.Tensor,
118 emb: torch.Tensor | None = None,
119 ) -> tuple[torch.Tensor, ...]:
120 emb = self.linear(self.silu(emb))
121 shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp, shift_msa2, scale_msa2, gate_msa2 = emb.chunk(
122 9, dim=1
123 )
124 norm_hidden_states = self.norm(hidden_states)
125 hidden_states = norm_hidden_states * (1 + scale_msa[:, None]) + shift_msa[:, None]
126 norm_hidden_states2 = norm_hidden_states * (1 + scale_msa2[:, None]) + shift_msa2[:, None]
127 return hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp, norm_hidden_states2, gate_msa2
128
129
130class AdaLayerNormZero(nn.Module):

Callers 1

__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…