+
    &j .                         ^ RI Ht ^ RIHt ^ RIHt  ! R R]4      t ! R R]4      t ! R R	]4      t	 ! R
 R]4      t
 ! R R]4      tR# )    )TupleN)Modulec                   ^   a a ] tR t^	t oRtRV3R lV 3R llltR tV3R lR ltRtVt	V ;t
# )	InstanceNormav  Applies instance normalization [1] on the inputs.

Computes

.. math::

    y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta,

where :math:`\gamma` and :math:`\beta` are learned per feature dimension
parameters initialized at 1 and 0 respectively. Both are of size :attr:`dims`,
if :attr:`affine` is ``True``.

Args:
    dims (int): The number of features of the input.
    eps (float): A value added to the denominator for numerical stability. Default: ``1e-5``.
    affine (bool): Default: ``False``.

Shape:
  - Input: :math:`(..., C)` where :math:`C` is equal to :attr:`dims`.
  - Output: Same shape as the input.

Examples:
    >>> import mlx.core as mx
    >>> import mlx.nn as nn
    >>> x = mx.random.normal((8, 4, 4, 16))
    >>> inorm = nn.InstanceNorm(dims=16)
    >>> output = inorm(x)

References:
    [1]: https://arxiv.org/abs/1607.08022
c                ,   < V ^8  d   QhRS[ RS[RS[/# )   dimsepsaffineintfloatbool)format__classdict__s   "s/Users/jameslopez/projects/CWCArchive/cwc-podcast/.venv/lib/python3.14/site-packages/mlx/nn/layers/normalization.py__annotate__InstanceNorm.__annotate__*   s)        	    c                   < \         SV `  4        V'       d9   \        P                  ! V34      V n        \        P
                  ! V34      V n        Wn        W n        R # N)	super__init__mxonesweightzerosbiasr	   r
   )selfr	   r
   r   	__class__s   &&&&r   r   InstanceNorm.__init__*   sA     	''4'*DK$)DI	r   c                D    V P                    R V P                   RRV 9    2# , eps=	, affine=r   r	   r
   r   s   &r   _extra_reprInstanceNorm._extra_repr7   '    ))F488*Ih$6F5GHHr   c                N   < V ^8  d   QhRS[ P                  RS[ P                  /# r   xreturnr   array)r   r   s   "r   r   r   :   s'     H H"(( Hrxx Hr   c                n   \        \        ^VP                  ^,
          4      4      p\        P                  ! WRR7      p\        P
                  ! WRR7      pW,
          \        P                  ! W@P                  ,           4      ,          pRV 9   d%   V P                  V,          V P                  ,           # T# )   Taxiskeepdimsr   )
tuplerangendimr   meanvarrsqrtr
   r   r   r   r-   reduction_axesr9   r:   s   &&   r   __call__InstanceNorm.__call__:   sy    uQ
34wwq=ffQd;X#.1108D0@a$))+GaGr   r   r	   r
   r   )h㈵>F__name__
__module____qualname____firstlineno____doc__r   r(   r>   __static_attributes____classdictcell____classcell__r    r   s   @@r   r   r   	   s-     @ IH H Hr   r   c                   R   a a ] tR t^Et oRtRV3R lV 3R llltR tR tRtVt	V ;t
# )	LayerNorma  Applies layer normalization [1] on the inputs.

Computes

.. math::

    y = \frac{x - E[x]}{\sqrt{Var[x]} + \epsilon} \gamma + \beta,

where :math:`\gamma` and :math:`\beta` are learned per feature dimension
parameters initialized at 1 and 0 respectively.

[1]: https://arxiv.org/abs/1607.06450

Args:
    dims (int): The feature dimension of the input to normalize over
    eps (float): A small additive constant for numerical stability
    affine (bool): If True learn an affine transform to apply after the
        normalization
    bias (bool): If True include a translation to the affine
        transformation. If set to False the transformation is not really affine
        just scaling.
c                2   < V ^8  d   QhRS[ RS[RS[RS[/# )r   r	   r
   r   r   r   )r   r   s   "r   r   LayerNorm.__annotate__]   s-     	 		#	48	GK	r   c                   < \         SV `  4        V'       dA   \        P                  ! V34      V n        V'       d   \        P
                  ! V34      V n        W n        Wn        R # r   )	r   r   r   r   r   r   r   r
   r	   )r   r	   r
   r   r   r    s   &&&&&r   r   LayerNorm.__init__]   sD     	''4'*DKHHdW-		r   c                D    V P                    R V P                   RRV 9    2# r#   r&   r'   s   &r   r(   LayerNorm._extra_reprh   r*   r   c                    R V 9   d   V P                   MRpRV 9   d   V P                  MRp\        P                  P	                  WW0P
                  4      # )r   Nr   )r   r   r   fast
layer_normr
   )r   r-   r   r   s   &&  r   r>   LayerNorm.__call__k   s@     (D 0d"dNtyyww!!!T88<<r   r@   )rA   TTrB   rK   s   @@r   rM   rM   E   s$     .	 	I= =r   rM   c                   R   a a ] tR t^qt oRtRV3R lV 3R llltR tR tRtVt	V ;t
# )RMSNorma  Applies Root Mean Square normalization [1] to the inputs.

Computes

..  math::

    y = \frac{x}{\sqrt{E[x^2] + \epsilon}} \gamma

where :math:`\gamma` is a learned per feature dimension parameter initialized at
1.

Note the accumulation for the mean is done in 32-bit precision.

[1]: https://arxiv.org/abs/1910.07467

Args:
    dims (int): The feature dimension of the input to normalize over
    eps (float): A small additive constant for numerical stability
c                &   < V ^8  d   QhRS[ RS[/# )r   r	   r
   )r   r   )r   r   s   "r   r   RMSNorm.__annotate__   s      S u r   c                h   < \         SV `  4        \        P                  ! V34      V n        W n        R # r   )r   r   r   r   r   r
   )r   r	   r
   r    s   &&&r   r   RMSNorm.__init__   s%    ggtg&r   c                Z    V P                   P                  ^ ,           RV P                   2# )r   r$   )r   shaper
   r'   s   &r   r(   RMSNorm._extra_repr   s'    ++##A&'vdhhZ88r   c                d    \         P                  P                  WR ,          V P                  4      # r   )r   rU   rms_normr
   )r   r-   s   &&r   r>   RMSNorm.__call__   s!    ww>488<<r   )r
   r   )rA   rB   rK   s   @@r   rY   rY   q   s#     ( 
9= =r   rY   c                   ^   a a ] tR t^t oRtR	V3R lV 3R llltR tR tR tR t	Rt
VtV ;t# )
	GroupNormaK  Applies Group Normalization [1] to the inputs.

Computes the same normalization as layer norm, namely

.. math::

    y = \frac{x - E[x]}{\sqrt{Var[x]} + \epsilon} \gamma + \beta,

where :math:`\gamma` and :math:`\beta` are learned per feature dimension
parameters initialized at 1 and 0 respectively. However, the mean and
variance are computed over the spatial dimensions and each group of
features. In particular, the input is split into num_groups across the
feature dimension.

The feature dimension is assumed to be the last dimension and the dimensions
that precede it (except the first) are considered the spatial dimensions.

[1]: https://arxiv.org/abs/1803.08494

Args:
    num_groups (int): Number of groups to separate the features into
    dims (int): The feature dimensions of the input to normalize over
    eps (float): A small additive constant for numerical stability
    affine (bool): If True learn an affine transform to apply after the
        normalization.
    pytorch_compatible (bool): If True perform the group normalization in
        the same order/grouping as PyTorch.
c          
      8   < V ^8  d   QhRS[ RS[ RS[RS[RS[/# )r   
num_groupsr	   r
   r   pytorch_compatibler   )r   r   s   "r   r   GroupNorm.__annotate__   s=     5 55 5 	5
 5 !5r   c                   < \         SV `  4        V'       d9   \        P                  ! V34      V n        \        P
                  ! V34      V n        Wn        W n        W0n	        WPn
        R # r   )r   r   r   r   r   r   r   rh   r	   r
   ri   )r   rh   r	   r
   r   ri   r    s   &&&&&&r   r   GroupNorm.__init__   sL     	$)DI''4'*DK$	"4r   c           	     x    V P                    R V P                   RV P                   RRV 9    RV P                   2	# )z, r$   r%   r   z, pytorch_compatible=)rh   r	   r
   ri   r'   s   &r   r(   GroupNorm._extra_repr   sI    r$))F488* =$&''<T=T=T<UW	
r   c                   V P                   pVP                  Evr4pWR,          pVP                  VRW&4      pVP                  ^ ^^^4      P                  W2R4      p\        P
                  P                  WP                  RRR7      pVP                  W2RV4      pVP                  ^ ^^^4      P                  ! V.VOVN5!  pV# )r2   N)r
   r   r   )rh   r_   reshape	transposer   rU   rV   r
   )r   r-   rh   batchrestr	   
group_sizes   &&     r   _pytorch_compatible_group_norm(GroupNorm._pytorch_compatible_group_norm   s    __
WWd'
 IIeR8KK1a#++ErB GGqhht$GIIeZ8KK1a#++E?D?$?r   c                X   V P                   pVP                  Evr4pVP                  VRV4      p\        P                  ! V^RR7      p\        P
                  ! V^RR7      pW,
          \        P                  ! WpP                  ,           4      ,          pVP                  ! V.VOVN5!  pV# )r2   Tr3   rp   )rh   r_   rq   r   r9   r:   r;   r
   )r   r-   rh   rs   rt   r	   meansr:   s   &&      r   _group_normGroupNorm._group_norm   s    __
WWd IIeR, D1ffQQ.Y"((3>22IIe)d)D)r   c                    V P                   '       d   V P                  MV P                  pV! V4      pR V 9   d%   V P                  V,          V P                  ,           # T# rb   )ri   rv   rz   r   r   )r   r-   
group_norms   && r   r>   GroupNorm.__call__   sW     &&& //!! 	
 qM08D0@a$))+GaGr   )r   r	   r
   rh   ri   r   )rA   TF)rC   rD   rE   rF   rG   r   r(   rv   rz   r>   rH   rI   rJ   rK   s   @@r   rf   rf      s/     :5 5"
 H Hr   rf   c                   |   a a ] tR t^t oRtRV3R lV 3R llltV 3R ltR tV3R lR ltV3R lR	 lt	R
t
VtV ;t# )	BatchNorma  Applies Batch Normalization over a 2D or 3D input.

Computes

.. math::

    y = \frac{x - E[x]}{\sqrt{Var[x]} + \epsilon} \gamma + \beta,

where :math:`\gamma` and :math:`\beta` are learned per feature dimension
parameters initialized at 1 and 0 respectively.

The input shape is specified as ``NC`` or ``NLC``, where ``N`` is the
batch, ``C`` is the number of features or channels, and ``L`` is the
sequence length. The output has the same shape as the input. For
four-dimensional arrays, the shape is ``NHWC``, where ``H`` and ``W`` are
the height and width respectively.

For more information on Batch Normalization, see the original paper `Batch
Normalization: Accelerating Deep Network Training by Reducing Internal
Covariate Shift <https://arxiv.org/abs/1502.03167>`_.

Args:
    num_features (int): The feature dimension to normalize over.
    eps (float, optional): A small additive constant for numerical
        stability. Default: ``1e-5``.
    momentum (float, optional): The momentum for updating the running
        mean and variance. Default: ``0.1``.
    affine (bool, optional): If ``True``, apply a learned affine
        transformation after the normalization. Default: ``True``.
    track_running_stats (bool, optional): If ``True``, track the
        running mean and variance. Default: ``True``.

Examples:
    >>> import mlx.core as mx
    >>> import mlx.nn as nn
    >>> x = mx.random.normal((5, 4))
    >>> bn = nn.BatchNorm(num_features=4, affine=True)
    >>> output = bn(x)
c          
      8   < V ^8  d   QhRS[ RS[RS[RS[RS[/# )r   num_featuresr
   momentumr   track_running_statsr   )r   r   s   "r   r   BatchNorm.__annotate__  sD     M MM M 	M
 M "Mr   c                  < \         SV `  4        Wn        W n        W0n        WPn        V'       d9   \        P                  ! V34      V n        \        P                  ! V34      V n
        V P
                  '       dP   \        P                  ! V34      V n        \        P                  ! V34      V n        V P                  R R.RR7       R# R# )running_meanrunning_varFkeysrecurseN)r   r   r   r
   r   r   r   r   r   r   r   r   r   freeze)r   r   r
   r   r   r   r    s   &&&&&&r   r   BatchNorm.__init__  s     	( #6 ''</2DK,1DI### ", 9D!ww7DKKnm<eKL $r   c                R   < \         SV `  ! V/ VB  V P                  RR.RR7       R# )zRWrap unfreeze to make sure that running_mean and var are always
frozen parameters.r   r   Fr   N)r   unfreezer   )r   argskwargsr    s   &*,r   r   BatchNorm.unfreeze1  s,     	$)&).-8%Hr   c           	     x    V P                    R V P                   RV P                   RRV 9    RV P                   2	# )r$   z, momentum=r%   r   z, track_running_stats=)r   r
   r   r   r'   s   &r   r(   BatchNorm._extra_repr7  sN      !z 2iD0@/A B##'#;#;"<>	
r   c                x   < V ^8  d   QhRS[ P                  RS[S[ P                  S[ P                  3,          /# r,   )r   r0   r   )r   r   s   "r   r   r   >  s1      RXX %"((0B*C r   c                    \        \        ^ VP                  ^,
          4      4      p\        P                  ! WR7      p\        P
                  ! WR7      pW43# )z
Calculate the mean and variance of the input tensor across the batch
and spatial dimensions.

Args:
    x (array): Input tensor.

Returns:
    tuple: Tuple containing mean and variance.
)r4   )r6   r7   r8   r   r9   r:   r<   s   &&   r   _calc_statsBatchNorm._calc_stats>  sA     uQ
34wwq.ffQ,yr   c                N   < V ^8  d   QhRS[ P                  RS[ P                  /# r,   r/   )r   r   s   "r   r   r   P  s'     H H"(( Hrxx Hr   c                   VP                   ^8  g   VP                   ^8  d   \        RVP                    24      hV P                  V4      w  r#V P                  '       dx   V P                  '       df   V P
                  p^V,
          V P                  ,          WB,          ,           V n        ^V,
          V P                  ,          WC,          ,           V n        M*V P                  '       d   V P                  pV P                  pW,
          \        P                  ! W0P                  ,           4      ,          pRV 9   d%   V P                  V,          V P                  ,           # T# )zo
Forward pass of BatchNorm.

Args:
    x (array): Input tensor.

Returns:
    array: Normalized output tensor.
z<Expected input tensor to have 2, 3 or 4 dimensions, but got r   )r8   
ValueErrorr   trainingr   r   r   r   r   r;   r
   r   r   )r   r-   r9   r:   mus   &&   r   r>   BatchNorm.__call__P  s    66A:!NqvvhW  $$Q'	===T555B!"R4+<+< <ry HD !B$*:*::RXED%%%$$D""CX#.1108D0@a$))+GaGr   )r   r
   r   r   r   r   r   r   )rA   g?TT)rC   rD   rE   rF   rG   r   r   r(   r   r>   rH   rI   rJ   rK   s   @@r   r   r      s>     &PM M0I
 $H H Hr   r   )typingr   mlx.corecorer   mlx.nn.layers.baser   r   rM   rY   rf   r    r   r   <module>r      sZ      %9H6 9Hx)= )=X=f =B[H [H|{H {Hr   