Normalization layers¶
Customized normalization layers for supporting various models in 🤗 Diffusers.
mindone.diffusers.models.normalization.AdaLayerNorm
¶
Bases: Cell
Norm layer modified to incorporate timestep embeddings.
PARAMETER | DESCRIPTION |
---|---|
embedding_dim |
The size of each embedding vector.
TYPE:
|
num_embeddings |
The size of the embeddings dictionary.
TYPE:
|
output_dim |
TYPE:
|
norm_elementwise_affine |
TYPE:
|
norm_eps |
TYPE:
|
chunk_dim |
TYPE:
|
Source code in mindone/diffusers/models/normalization.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
mindone.diffusers.models.normalization.AdaLayerNormZero
¶
Bases: Cell
Norm layer adaptive layer norm zero (adaLN-Zero).
PARAMETER | DESCRIPTION |
---|---|
embedding_dim |
The size of each embedding vector.
TYPE:
|
num_embeddings |
The size of the embeddings dictionary.
TYPE:
|
Source code in mindone/diffusers/models/normalization.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
mindone.diffusers.models.normalization.AdaLayerNormSingle
¶
Bases: Cell
Norm layer adaptive layer norm single (adaLN-single).
As proposed in PixArt-Alpha (see: https://arxiv.org/abs/2310.00426; Section 2.3).
PARAMETER | DESCRIPTION |
---|---|
embedding_dim |
The size of each embedding vector.
TYPE:
|
use_additional_conditions |
To use additional conditions for normalization or not.
TYPE:
|
Source code in mindone/diffusers/models/normalization.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
mindone.diffusers.models.normalization.AdaGroupNorm
¶
Bases: Cell
GroupNorm layer modified to incorporate timestep embeddings.
PARAMETER | DESCRIPTION |
---|---|
embedding_dim |
The size of each embedding vector.
TYPE:
|
num_embeddings |
The size of the embeddings dictionary.
TYPE:
|
num_groups |
The number of groups to separate the channels into.
TYPE:
|
act_fn |
The activation function to use.
TYPE:
|
eps |
The epsilon value to use for numerical stability.
TYPE:
|
Source code in mindone/diffusers/models/normalization.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
mindone.diffusers.models.normalization.AdaLayerNormContinuous
¶
Bases: Cell
Source code in mindone/diffusers/models/normalization.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
|
mindone.diffusers.models.normalization.LayerNorm
¶
Bases: Cell
Applies Layer Normalization over a mini-batch of inputs.
This layer implements the operation as described in
the paper Layer Normalization <https://arxiv.org/abs/1607.06450>
__
.. math:: y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta
The mean and standard-deviation are calculated over the last D
dimensions, where D
is the dimension of :attr:normalized_shape
. For example, if :attr:normalized_shape
is (3, 5)
(a 2-dimensional shape), the mean and standard-deviation are computed over
the last 2 dimensions of the input (i.e. input.mean((-2, -1))
).
:math:\gamma
and :math:\beta
are learnable affine transform parameters of
:attr:normalized_shape
if :attr:elementwise_affine
is True
.
The standard-deviation is calculated via the biased estimator, equivalent to
ops.var(input, unbiased=False)
.
.. note::
Unlike Batch Normalization and Instance Normalization, which applies
scalar scale and bias for each entire channel/plane with the
:attr:affine
option, Layer Normalization applies per-element scale and
bias with :attr:elementwise_affine
.
This layer uses statistics computed from input data in both training and evaluation modes.
PARAMETER | DESCRIPTION |
---|---|
normalized_shape |
input shape from an expected input of size .. math:: [* \times \text{normalized_shape}[0] \times \text{normalized_shape}[1] \times \ldots \times \text{normalized_shape}[-1]] If a single integer is used, it is treated as a singleton list, and this module will normalize over the last dimension which is expected to be of that specific size.
TYPE:
|
eps |
a value added to the denominator for numerical stability. Default: 1e-5
DEFAULT:
|
elementwise_affine |
a boolean value that when set to
TYPE:
|
ATTRIBUTE | DESCRIPTION |
---|---|
weight |
the learnable weights of the module of shape
:math:
|
bias |
the learnable bias of the module of shape
:math:
|
Shape
- Input: :math:
(N, *)
- Output: :math:
(N, *)
(same shape as input)
>>> # NLP Example
>>> batch, sentence_length, embedding_dim = 20, 5, 10
>>> embedding = ops.randn(batch, sentence_length, embedding_dim)
>>> layer_norm = LayerNorm(embedding_dim)
>>> # Activate module
>>> layer_norm(embedding)
>>>
>>> # Image Example
>>> N, C, H, W = 20, 5, 10, 10
>>> input = ops.randn(N, C, H, W)
>>> # Normalize over the last three dimensions (i.e. the channel and spatial dimensions)
>>> # as shown in the image below
>>> layer_norm = LayerNorm([C, H, W])
>>> output = layer_norm(input)
Source code in mindone/diffusers/models/normalization.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
|
mindone.diffusers.models.normalization.GroupNorm
¶
Bases: Cell
Applies Group Normalization over a mini-batch of inputs.
This layer implements the operation as described in
the paper Group Normalization <https://arxiv.org/abs/1803.08494>
__
.. math:: y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta
The input channels are separated into :attr:num_groups
groups, each containing
num_channels / num_groups
channels. :attr:num_channels
must be divisible by
:attr:num_groups
. The mean and standard-deviation are calculated
separately over the each group. :math:\gamma
and :math:\beta
are learnable
per-channel affine transform parameter vectors of size :attr:num_channels
if
:attr:affine
is True
.
This layer uses statistics computed from input data in both training and evaluation modes.
PARAMETER | DESCRIPTION |
---|---|
num_groups |
number of groups to separate the channels into
TYPE:
|
num_channels |
number of channels expected in input
TYPE:
|
eps |
a value added to the denominator for numerical stability. Default: 1e-5
TYPE:
|
affine |
a boolean value that when set to
TYPE:
|
Shape
- Input: :math:
(N, C, *)
where :math:C=\text{num\_channels}
- Output: :math:
(N, C, *)
(same shape as input)
>>> input = ops.randn(20, 6, 10, 10)
>>> # Separate 6 channels into 3 groups
>>> m = GroupNorm(3, 6)
>>> # Separate 6 channels into 6 groups (equivalent with InstanceNorm)
>>> m = GroupNorm(6, 6)
>>> # Put all 6 channels into a single group (equivalent with LayerNorm)
>>> m = GroupNorm(1, 6)
>>> # Activating the module
>>> output = m(input)
Source code in mindone/diffusers/models/normalization.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
|
mindone.diffusers.models.normalization.RMSNorm
¶
Bases: Cell
Source code in mindone/diffusers/models/normalization.py
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
|