+
    NV-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   "€Úl/Volumes/fast/ai/experiments/ui-tars-smoke/.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 'Ó*ˆDŒKÜŸš $ Ó)ˆDŒIØŒ	ØŽ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   ó'   € Ø—)‘)�˜F 4§8¡8 *¨I°hÀ$Ñ6FÐ5GÐ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¡r§x¡xñ 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   € Üœu Q¨¯©°­
Ó3Ó4ˆä�wŠw�q¸Ô=ˆÜ�fŠf�Q°dÔ;ˆà�XœŸš #¯©¥.Ó1Õ1ˆà08¸DÔ0@�—‘˜a• $§)¡)Õ+ÐGÀaÐ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 'Ó*ˆDŒKßÜŸHšH d WÓ-�”	ØŒØŽ	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ˆØ" dœNˆt�yŠy°ˆÜ�w‰w×!Ñ! !¨T·8±8Ó<Ð<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%   ø€ Ü‰ÑÔÜ—g’g˜t˜gÓ&ˆŒØŽr   c                óZ   € V P                   P                  ^ ,           RV P                   2# )r   r$   )r   Úshaper
   r'   s   &r   r(   ÚRMSNorm._extra_repr‹   s'   € Ø—+‘+×#Ñ# AÕ&Ð' v¨d¯h©h¨ZÐ8Ð8r   c                ód   € \         P                  P                  WR ,          V P                  4      # ©r   )r   rU   Úrms_normr
   )r   r-   s   &&r   r>   ÚRMSNorm.__call__Ž   s!   € Ü�w‰w×Ñ ¨¥>°4·8±8Ó<Ð<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   ø€ ô 	‰ÑÔßÜŸš $ Ó)ˆDŒIÜŸ'š' 4 'Ó*ˆDŒKØ$ŒØŒ	ØŒØ"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 $§)¡) ¨F°4·8±8°*ð =Ø $Ñ&Ð'Ð'<¸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·   € Ø—_‘_ˆ
ØŸW™WÑˆ�dØÕ'ˆ
ð �I‰I�e˜R Ó8ˆØ�K‰K˜˜1˜a Ó#×+Ñ+¨E¸rÓBˆô �G‰G×Ñ˜q§h¡h°tÀ$ÐÓGˆà�I‰I�e¨¨ZÓ8ˆØ�K‰K˜˜1˜a Ó#×+Ò+¨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‹   € Ø—_‘_ˆ
ØŸW™WÑˆ�dð �I‰I�e˜R Ó,ˆô —’˜ ¨DÔ1ˆÜ�fŠf�Q˜Q¨Ô.ˆØ�Yœ"Ÿ(š( 3¯©¥>Ó2Õ2ˆØ�IŠI�eÐ)˜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   € ð ×&×&Ð&ð ×/Ò/à×!Ñ!ð 	ñ
 �q‹MˆØ08¸DÔ0@�—‘˜a• $§)¡)Õ+ÐGÀaÐ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Ô çÜŸ'š' < /Ó2ˆDŒKÜŸš , Ó1ˆDŒIà×#×#Ð#Ü "§¢¨,¨Ó 9ˆDÔÜ!Ÿwšw¨ Ó7ˆDÔØ�K‰K˜n¨mÐ<ÀeˆKÖ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°¸DÑ0@Ð/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   ø€ ÷ ñ ™RŸX™Xð ©%±·±¹"¿(¹(Ð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   € ô œu Q¨¯©°­
Ó3Ó4ˆä�wŠw�qÔ.ˆÜ�fŠf�QÔ,ˆàˆ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¡r§x¡xñ 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  € ð �6‰6�AŒ:˜Ÿ™ !œÜØNÈqÏvÉvÈhÐWóð ð ×$Ñ$ QÓ'‰	ˆØ�=�=ˆ=˜T×5×5Ð5Ø—‘ˆBØ!" R¥¨4×+<Ñ+<Õ <¸r½yÕ HˆDÔØ ! B¥¨$×*:Ñ*:Õ:¸R½XÕEˆDÕØ×%×%Ð%Ø×$Ñ$ˆDØ×"Ñ"ˆCà�XœŸš #¯©¥.Ó1Õ1ˆØ08¸DÔ0@�—‘˜a• $§)¡)Õ+ÐGÀaÐ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   