MCPcopy Index your code
hub / github.com/huggingface/diffusers / RMSNorm

Class RMSNorm

src/diffusers/models/normalization.py:510–567  ·  view source on GitHub ↗

r""" RMS Norm as introduced in https://huggingface.co/papers/1910.07467 by Zhang et al. Args: dim (`int`): Number of dimensions to use for `weights`. Only effective when `elementwise_affine` is True. eps (`float`): Small value to use when calculating the reciprocal of the sq

Source from the content-addressed store, hash-verified

508
509
510class RMSNorm(nn.Module):
511 r"""
512 RMS Norm as introduced in https://huggingface.co/papers/1910.07467 by Zhang et al.
513
514 Args:
515 dim (`int`): Number of dimensions to use for `weights`. Only effective when `elementwise_affine` is True.
516 eps (`float`): Small value to use when calculating the reciprocal of the square-root.
517 elementwise_affine (`bool`, defaults to `True`):
518 Boolean flag to denote if affine transformation should be applied.
519 bias (`bool`, defaults to False): If also training the `bias` param.
520 """
521
522 def __init__(self, dim, eps: float, elementwise_affine: bool = True, bias: bool = False):
523 super().__init__()
524
525 self.eps = eps
526 self.elementwise_affine = elementwise_affine
527
528 if isinstance(dim, numbers.Integral):
529 dim = (dim,)
530
531 self.dim = torch.Size(dim)
532
533 self.weight = None
534 self.bias = None
535
536 if elementwise_affine:
537 self.weight = nn.Parameter(torch.ones(dim))
538 if bias:
539 self.bias = nn.Parameter(torch.zeros(dim))
540
541 def forward(self, hidden_states):
542 if is_torch_npu_available():
543 import torch_npu
544
545 if self.weight is not None:
546 # convert into half-precision if necessary
547 if self.weight.dtype in [torch.float16, torch.bfloat16]:
548 hidden_states = hidden_states.to(self.weight.dtype)
549 hidden_states = torch_npu.npu_rms_norm(hidden_states, self.weight, epsilon=self.eps)[0]
550 if self.bias is not None:
551 hidden_states = hidden_states + self.bias
552 else:
553 input_dtype = hidden_states.dtype
554 variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True)
555 hidden_states = hidden_states * torch.rsqrt(variance + self.eps)
556
557 if self.weight is not None:
558 # convert into half-precision if necessary
559 if self.weight.dtype in [torch.float16, torch.bfloat16]:
560 hidden_states = hidden_states.to(self.weight.dtype)
561 hidden_states = hidden_states * self.weight
562 if self.bias is not None:
563 hidden_states = hidden_states + self.bias
564 else:
565 hidden_states = hidden_states.to(input_dtype)
566
567 return hidden_states

Callers 15

__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
get_normalizationFunction · 0.70
__init__Method · 0.50
__init__Method · 0.50
__init__Method · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…