r""" Norm layer adaptive layer norm single (adaLN-single). As proposed in PixArt-Alpha (see: https://arxiv.org/abs/2310.00426; Section 2.3). Parameters: embedding_dim (`int`): The size of each embedding vector. use_additional_conditions (`bool`): To
| 262 | |
| 263 | |
| 264 | class AdaLayerNormSingle(nn.Module): |
| 265 | r""" |
| 266 | Norm layer adaptive layer norm single (adaLN-single). |
| 267 | |
| 268 | As proposed in PixArt-Alpha (see: https://arxiv.org/abs/2310.00426; Section 2.3). |
| 269 | |
| 270 | Parameters: |
| 271 | embedding_dim (`int`): The size of each embedding vector. |
| 272 | use_additional_conditions (`bool`): To use additional conditions for normalization or not. |
| 273 | """ |
| 274 | def __init__(self, embedding_dim: int, use_additional_conditions: bool = False, time_step_rescale=1000): |
| 275 | super().__init__() |
| 276 | |
| 277 | self.emb = PixArtAlphaCombinedTimestepSizeEmbeddings( |
| 278 | embedding_dim, size_emb_dim=embedding_dim // 2, use_additional_conditions=use_additional_conditions |
| 279 | ) |
| 280 | |
| 281 | self.silu = nn.SiLU() |
| 282 | self.linear = nn.Linear(embedding_dim, 6 * embedding_dim, bias=True) |
| 283 | |
| 284 | self.time_step_rescale = time_step_rescale ## timestep usually in [0, 1], we rescale it to [0,1000] for stability |
| 285 | |
| 286 | def forward( |
| 287 | self, |
| 288 | timestep: torch.Tensor, |
| 289 | added_cond_kwargs: Dict[str, torch.Tensor] = None, |
| 290 | ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: |
| 291 | embedded_timestep = self.emb(timestep*self.time_step_rescale, **added_cond_kwargs) |
| 292 | |
| 293 | out = self.linear(self.silu(embedded_timestep)) |
| 294 | |
| 295 | return out, embedded_timestep |
| 296 | |
| 297 | |
| 298 | class PixArtAlphaTextProjection(nn.Module): |