r""" A custom RMS normalization layer. Args: dim (int): The number of dimensions to normalize over. channel_first (bool, optional): Whether the input tensor has channels as the first dimension. Default is True. images (bool, optional): Whether the input r
| 177 | |
| 178 | |
| 179 | class WanRMS_norm(nn.Module): |
| 180 | r""" |
| 181 | A custom RMS normalization layer. |
| 182 | |
| 183 | Args: |
| 184 | dim (int): The number of dimensions to normalize over. |
| 185 | channel_first (bool, optional): Whether the input tensor has channels as the first dimension. |
| 186 | Default is True. |
| 187 | images (bool, optional): Whether the input represents image data. Default is True. |
| 188 | bias (bool, optional): Whether to include a learnable bias term. Default is False. |
| 189 | """ |
| 190 | |
| 191 | def __init__(self, dim: int, channel_first: bool = True, images: bool = True, bias: bool = False) -> None: |
| 192 | super().__init__() |
| 193 | broadcastable_dims = (1, 1, 1) if not images else (1, 1) |
| 194 | shape = (dim, *broadcastable_dims) if channel_first else (dim,) |
| 195 | |
| 196 | self.channel_first = channel_first |
| 197 | self.scale = dim**0.5 |
| 198 | self.gamma = nn.Parameter(torch.ones(shape)) |
| 199 | self.bias = nn.Parameter(torch.zeros(shape)) if bias else 0.0 |
| 200 | |
| 201 | def forward(self, x): |
| 202 | return F.normalize(x, dim=(1 if self.channel_first else -1)) * self.scale * self.gamma + self.bias |
| 203 | |
| 204 | |
| 205 | class WanUpsample(nn.Upsample): |