r""" Adaptive normalization layer with a norm layer (layer_norm or rms_norm). Args: embedding_dim (`int`): Embedding dimension to use during projection. conditioning_embedding_dim (`int`): Dimension of the input condition. elementwise_affine (`bool`, defaults to `Tru
| 305 | |
| 306 | |
| 307 | class AdaLayerNormContinuous(nn.Module): |
| 308 | r""" |
| 309 | Adaptive normalization layer with a norm layer (layer_norm or rms_norm). |
| 310 | |
| 311 | Args: |
| 312 | embedding_dim (`int`): Embedding dimension to use during projection. |
| 313 | conditioning_embedding_dim (`int`): Dimension of the input condition. |
| 314 | elementwise_affine (`bool`, defaults to `True`): |
| 315 | Boolean flag to denote if affine transformation should be applied. |
| 316 | eps (`float`, defaults to 1e-5): Epsilon factor. |
| 317 | bias (`bias`, defaults to `True`): Boolean flag to denote if bias should be use. |
| 318 | norm_type (`str`, defaults to `"layer_norm"`): |
| 319 | Normalization layer to use. Values supported: "layer_norm", "rms_norm". |
| 320 | """ |
| 321 | |
| 322 | def __init__( |
| 323 | self, |
| 324 | embedding_dim: int, |
| 325 | conditioning_embedding_dim: int, |
| 326 | # NOTE: It is a bit weird that the norm layer can be configured to have scale and shift parameters |
| 327 | # because the output is immediately scaled and shifted by the projected conditioning embeddings. |
| 328 | # Note that AdaLayerNorm does not let the norm layer have scale and shift parameters. |
| 329 | # However, this is how it was implemented in the original code, and it's rather likely you should |
| 330 | # set `elementwise_affine` to False. |
| 331 | elementwise_affine=True, |
| 332 | eps=1e-5, |
| 333 | bias=True, |
| 334 | norm_type="layer_norm", |
| 335 | ): |
| 336 | super().__init__() |
| 337 | self.silu = nn.SiLU() |
| 338 | self.linear = nn.Linear(conditioning_embedding_dim, embedding_dim * 2, bias=bias) |
| 339 | if norm_type == "layer_norm": |
| 340 | self.norm = LayerNorm(embedding_dim, eps, elementwise_affine, bias) |
| 341 | elif norm_type == "rms_norm": |
| 342 | self.norm = RMSNorm(embedding_dim, eps, elementwise_affine) |
| 343 | else: |
| 344 | raise ValueError(f"unknown norm_type {norm_type}") |
| 345 | |
| 346 | def forward(self, x: torch.Tensor, conditioning_embedding: torch.Tensor) -> torch.Tensor: |
| 347 | # convert back to the original dtype in case `conditioning_embedding`` is upcasted to float32 (needed for hunyuanDiT) |
| 348 | emb = self.linear(self.silu(conditioning_embedding).to(x.dtype)) |
| 349 | scale, shift = torch.chunk(emb, 2, dim=1) |
| 350 | x = self.norm(x) * (1 + scale)[:, None, :] + shift[:, None, :] |
| 351 | return x |
| 352 | |
| 353 | |
| 354 | class LuminaLayerNormContinuous(nn.Module): |
no outgoing calls
no test coverage detected
searching dependent graphs…