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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
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
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 |
|
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
259 260 261 262 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 294 |
|
mindone.diffusers.models.normalization.AdaLayerNormContinuous
¶
Bases: Cell
Source code in mindone/diffusers/models/normalization.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
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
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 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 |
|
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
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 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 |
|
mindone.diffusers.models.normalization.RMSNorm
¶
Bases: Cell
Source code in mindone/diffusers/models/normalization.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
|