+
    NV-jÙ9  ã                   ó®   € ^ RI t ^ RIHtHtHt ^ RIHt ^ RIH	t	 ^ RI
Ht R tRRRRR	R
R/R R lllt ! R R]	4      t ! R R]	4      t ! R R]	4      tR# )é    N)ÚCallableÚOptionalÚUnion)ÚModule)Útree_map_with_pathc                 óZ   € R RRRRRRR/pW0,          w  rET;'       g    TT;'       g    T3# )ÚaffineÚmxfp4Únvfp4Úmxfp8)é@   é   )é    r   )é   r   )r   é   © )ÚmodeÚ
group_sizeÚbitsÚmode_defaultsÚdefault_group_sizeÚdefault_bitss   &&&   Úh/Volumes/fast/ai/experiments/ui-tars-smoke/.venv/lib/python3.14/site-packages/mlx/nn/layers/quantized.pyÚ_defaults_for_moder      sG   € à�'Ø�Ø�Ø�ð	€Mð (5Õ':Ñ$ÐØ×+Ð+Ð+¨T×-AÐ-A°\ÐAÐAó    r   r	   Úquantize_inputFÚclass_predicatec                óÆ   € V ^8„  d   QhR\         R\        R\        R\        R\        R\        \
        \        \         .\        \        \        3,          3,          ,          /# )é   Úmodelr   r   r   r   r   )r   ÚintÚstrÚboolr   r   r   Údict)Úformats   "r   Ú__annotate__r&      sh   € ÷ I!ñ I!ÜðI!äðI!ô ðI!ô
 ðI!ô ðI!ô œh¬¬V }´e¼DÄ$¸JÕ6GÐ'GÕHÕIñI!r   c               ó¶   aaaaa€ S;'       g    R oVVVVV3R lpV P                  4       p\        Wg\        P                  R7      pV P	                  V4       R# )a[  Quantize the sub-modules of a module according to a predicate.

By default all layers that define a ``to_quantized()`` method will be
quantized. Both :obj:`Linear` and :obj:`Embedding` layers will be
quantized. The module is updated in-place.

Note:
    ``quantize_input=True`` is only supported for ``"nvfp4"`` and ``"mxfp8"``
    modes and :obj:`Linear` layers.

Args:
    model (mlx.nn.Module): The model whose leaf modules may be quantized.
    group_size (Optional[int]): The quantization group size (see
       :func:`mlx.core.quantize`). Default: ``None``.
    bits (Optional[int]): The number of bits per parameter (see
       :func:`mlx.core.quantize`). Default: ``None``.
    mode (str): The quantization method to use (see
       :func:`mlx.core.quantize`). Default: ``"affine"``.
    quantize_input (bool): Whether to quantize activations. Default: ``False``.
    class_predicate (Optional[Callable]): A callable which receives the
       :obj:`Module` path and :obj:`Module` itself and returns ``True`` or a
       dict of params for ``to_quantized`` if it should be quantized and
       ``False`` otherwise. If ``None``, then all layers that define a
       ``to_quantized()`` method are quantized. Default: ``None``.

Example:
    Weight only quantization for all layers that define a ``to_quantized()`` method:

    >>> import mlx.nn as nn
    >>> nn.quantize(model, group_size=64, bits=4, mode="affine")

    Weight and input quantization for all linear layers:

    >>> predicate = lambda p, m: isinstance(m, nn.Linear)
    >>> nn.quantize(model, mode="nvfp4", quantize_input=True, class_predicate=predicate)
c                 ó   € \        VR 4      # )Úto_quantized)Úhasattr)Ú_Úms   &&r   Ú<lambda>Úquantize.<locals>.<lambda>C   s   € ´w¸qÀ.Ô7Qr   c                 ó¨  <€ S! W4      ;p'       dÁ   \        VR 4      '       d˜   \        V\        4      '       d(   RSRSRS/pS'       d   SVR&   VP                  ! R/ VB # \        V\        4      '       d:   RV9   d!   VR,          '       g   VP                  R4       VP                  ! R/ VB # \        R4      h\        R\        V4       24      hV# )r)   r   r   r   r   zZ``class_predicate`` must return a bool or a dict of parameters to pass to ``to_quantized``z!Unable to quantize model of type r   )r*   Ú
isinstancer#   r)   r$   ÚpopÚ
ValueErrorÚtype)	Úpathr,   Úbool_or_paramsÚkwargsr   r   r   r   r   s	   &&  €€€€€r   Ú_maybe_quantizeÚ!quantize.<locals>._maybe_quantizeE   sÙ   ø€ Ù,¨TÓ5Ð5ˆ>Ö5Ü�q˜.×)Ò)Ü˜n¬d×3Ò3Ø*¨J¸ÀÀfÈdÐS�Fß%Ø3A˜Ð/Ñ0ØŸ>š>Ñ3¨FÑ3Ð3Ü ´×5Ò5Ø(¨NÔ:ÀNØ(÷Eô Eð '×*Ñ*Ð+;Ô<ØŸ>š>Ñ;¨NÑ;Ð;ä$ðOóð ô
 !Ð#DÄTÈ!ÃWÀIÐ!NÓOÐOàˆHr   )Úis_leafN)Úleaf_modulesr   r   Ú	is_moduleÚupdate_modules)r    r   r   r   r   r   r7   Úleavess   &ffddd  r   Úquantizer>      sN   ü€ ðZ &×RÐRÑ*Q€O÷ñ ð0 ×ÑÓ!€FÜ Ä×AQÑAQÔR€FØ	×Ñ˜Ö r   c                   óx   a a€ ] tR t^bt oRtR
V3R lV 3R llltR tR tR t]	R
V3R lR ll4       t
R	tVtV ;t# )ÚQuantizedEmbeddinga   The same as :obj:`Embedding` but with a  quantized weight matrix.

:obj:`QuantizedEmbedding` also provides a :meth:`from_embedding`
classmethod to convert embedding layers to :obj:`QuantizedEmbedding`
layers.

Args:
    num_embeddings (int): How many possible discrete tokens can we embed.
       Usually called the vocabulary size.
    dims (int): The dimensionality of the embeddings.
    group_size (Optional[int]): The group size to use for the quantized
        weight. See :func:`~mlx.core.quantize`. Default: ``None``.
    bits (Optional[int]): The bit width to use for the quantized weight.
        See :func:`~mlx.core.quantize`. Default: ``None``.
    mode (str): The quantization method to use (see
       :func:`mlx.core.quantize`). Default: ``"affine"``.
c          
      ó8   <€ V ^8„  d   QhRS[ RS[ RS[ RS[ RS[/# )r   Únum_embeddingsÚdimsr   r   r   ©r!   r"   )r%   Ú__classdict__s   "€r   r&   ÚQuantizedEmbedding.__annotate__u   s=   ø€ ÷ ñ áðñ ðñ ð	ñ
 ðñ ñr   c                ó’  <€ \         S	V `  4        \        WSV4      w  V n        V n        WPn        \        P                  ! ^V,          4      p\        P                  P                  W3VR7      p\        P                  ! WsWER7      vV n        V n        pV'       d
   V^ ,          MRV n        Wn        W n        V P#                  4        R# )é   )ÚshapeÚscale©r   N)ÚsuperÚ__init__r   r   r   r   ÚmathÚsqrtÚmxÚrandomÚnormalr>   ÚweightÚscalesÚbiasesrB   rC   Úfreeze)
ÚselfrB   rC   r   r   r   rJ   rS   rU   Ú	__class__s
   &&&&&&   €r   rM   ÚQuantizedEmbedding.__init__u   s¡   ø€ ô 	‰ÑÔô &8¸È$Ó%OÑ"ˆŒ˜œØŒ	ô —	’	˜!˜d�(Ó#ˆÜ—‘×!Ñ!¨Ð(>ÀeÐ!ÓLˆÜ,.¯KªKØ ô-
Ð)ˆŒ�T”[ 6÷ $*�f˜Q–i¨tˆŒØ,ÔØŒ	ð 	�‰Žr   c           	     óè   € V P                  R 4      p\        P                  ! V R,          V,          V R,          V,          Ve	   W!,          MRV P                  V P                  V P
                  R7      # )rU   rS   rT   N)rT   rU   r   r   r   )ÚgetrP   Ú
dequantizer   r   r   )rW   ÚxrU   s   && r   Ú__call__ÚQuantizedEmbedding.__call__�   sZ   € Ø—‘˜(Ó#ˆÜ�}Š}Ø��N˜1ÕØ˜•> !Õ$Ø &Ò 2�6–9¸Ø—‘Ø—‘Ø—‘ô
ð 	
r   c                ó´   € \         P                  ! VV R,          V R,          V P                  R4      RV P                  V P                  V P
                  R7      # )z”
Call the quantized embedding layer as a quantized linear layer.

Use this for example when input embedding and output projection
weights are tied.
rS   rT   rU   T©rT   rU   Ú	transposer   r   r   ©rP   Úquantized_matmulr[   r   r   r   ©rW   r]   s   &&r   Ú	as_linearÚQuantizedEmbedding.as_linear›   sL   € ô ×"Ò"ØØ��NØ˜•>Ø—8‘8˜HÓ%ØØ—‘Ø—‘Ø—‘ô	
ð 		
r   c           	     ó†   € V P                    R V P                   RV P                   RV P                   RV P                   2	# )z, ú, group_size=ú, bits=ú, mode=)rB   rC   r   r   r   ©rW   s   &r   Ú_extra_reprÚQuantizedEmbedding._extra_repr­   sF   € à×"Ñ"Ð# 2 d§i¡i [ð 1ØŸ/™/Ð*¨'°$·)±)°¸GÀDÇIÁIÀ;ðPð	
r   c                ó2   <€ V ^8„  d   QhRS[ RS[RS[RS[/# )r   Úembedding_layerr   r   r   ©r   r!   r"   )r%   rE   s   "€r   r&   rF   ´   s3   ø€ ÷ ñ áðñ ðñ ð	ñ
 ñr   c                óì   € VP                   P                  w  rVV ! WVW#VR7      p\        P                  ! VP                   VVVR7      vVn         Vn        pV'       d   V^ ,          Vn        V# RVn        V# )zHCreate a :obj:`QuantizedEmbedding` layer from an :obj:`Embedding` layer.rK   N)rS   rI   rP   r>   rT   rU   )	Úclsrp   r   r   r   Úembedding_dimsrC   ÚqlrU   s	   &&&&&    r   Úfrom_embeddingÚ!QuantizedEmbedding.from_embedding³   ss   € ð  /×5Ñ5×;Ñ;ÑˆÙ� z¸dÔCˆÜ(*¯ªØ×"Ñ"ØØØô	)
Ð%ˆŒ	�2”9˜v÷ "(�F˜1•IˆŒ	Øˆ	ð .2ˆŒ	Øˆ	r   )rU   r   rC   r   r   rB   rT   rS   ©NNr	   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rM   r^   rf   rm   Úclassmethodrv   Ú__static_attributes__Ú__classdictcell__Ú__classcell__©rX   rE   s   @@r   r@   r@   b   sA   ù‡ € ñ÷$õ ò6	
ò
ò$
ð ÷ñ ó ÷ð r   r@   c                   ór   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
V3R lR ll4       t	Rt
VtV ;t# )ÚQuantizedLineara  Applies an affine transformation to the input using a quantized weight matrix.

It is the quantized equivalent of :class:`mlx.nn.Linear`. For now its
parameters are frozen and will not be included in any gradient computation
but this will probably change in the future.

:obj:`QuantizedLinear` also provides a classmethod :meth:`from_linear` to
convert linear layers to :obj:`QuantizedLinear` layers.

Args:
    input_dims (int): The dimensionality of the input features.
    output_dims (int): The dimensionality of the output features.
    bias (bool, optional): If set to ``False`` then the layer will not use
        a bias. Default: ``True``.
    group_size (Optional[int]): The group size to use for the quantized
        weight. See :func:`~mlx.core.quantize`. Default: ``None``.
    bits (Optional[int]): The bit width to use for the quantized weight.
        See :func:`~mlx.core.quantize`. Default: ``None``.
    mode (str): The quantization method to use (see
       :func:`mlx.core.quantize`). Default: ``"affine"``.
c                ó>   <€ V ^8„  d   QhRS[ RS[ RS[RS[ RS[ RS[/# )r   Ú
input_dimsÚoutput_dimsÚbiasr   r   r   )r!   r#   r"   )r%   rE   s   "€r   r&   ÚQuantizedLinear.__annotate__ß   sG   ø€ ÷  ñ  áð ñ ð ñ ð	 ñ
 ð ñ ð ñ ñ r   c                óÆ  <€ \         S
V `  4        \        WdV4      w  V n        V n        W`n        \        P                  ! ^V,          4      p\        P                  P                  V) VW!3R7      p\        P                  ! W„WVR7      vV n        V n        p	V	'       d
   V	^ ,          MRV n        V'       d   \        P                  ! V34      V n        V P#                  4        R# )rH   ©ÚlowÚhighrI   rK   N)rL   rM   r   r   r   r   rN   rO   rP   rQ   Úuniformr>   rS   rT   rU   Úzerosrˆ   rV   )rW   r†   r‡   rˆ   r   r   r   rJ   rS   rU   rX   s   &&&&&&&   €r   rM   ÚQuantizedLinear.__init__ß   sº   ø€ ô 	‰ÑÔô &8¸È$Ó%OÑ"ˆŒ˜œØŒ	ô —	’	˜!˜j�.Ó)ˆÜ—‘×"Ñ"Ø�ØØÐ+ð #ó 
ˆô
 -/¯KªKØ ô-
Ð)ˆŒ�T”[ 6÷ $*�f˜Q–i¨tˆŒ÷ ÜŸš + Ó0ˆDŒIð 	�‰Žr   c                óÐ   € V P                   P                  w  rV^ ,          V P                  ,          pRV RV RRV 9    RV P                   RV P                   RV P                   2# )r   úinput_dims=ú, output_dims=z, bias=rˆ   ri   rj   rk   )rS   rI   r   r   r   ©rW   Úout_dimsÚin_dimss   &  r   rm   ÚQuantizedLinear._extra_repr  sm   € Ø ŸK™K×-Ñ-ÑˆØ˜R•< D§I¡IÕ-ˆà˜'˜ .°°
¸'À&ÈDÁ.ÐAQð RØŸ/™/Ð*¨'°$·)±)°¸GÀDÇIÁIÀ;ðPð	
r   c                óä   € \         P                  ! VV R ,          V R,          V P                  R4      RV P                  V P                  V P
                  R7      pRV 9   d   WR,          ,           pV# )rS   rT   rU   Tra   rˆ   rc   re   s   &&r   r^   ÚQuantizedLinear.__call__	  sa   € Ü×ÒØØ��NØ˜•>Ø—8‘8˜HÓ%ØØ—‘Ø—‘Ø—‘ô	
ˆð �TŒ>Ø˜•LÕ ˆAØˆr   c                ó2   <€ V ^8„  d   QhRS[ RS[RS[RS[/# ©r   Úlinear_layerr   r   r   rq   )r%   rE   s   "€r   r&   r‰     s3   ø€ ÷ ñ áðñ ðñ ð	ñ
 ñr   c           	     ó  € VP                   P                  w  rVV ! WeRW#VR7      p\        P                  ! VP                   VVVR7      vVn         Vn        pV'       d
   V^ ,          MRVn        RV9   d   VP                  Vn        V# )zACreate a :obj:`QuantizedLinear` layer from a :obj:`Linear` layer.FrK   Nrˆ   )rS   rI   rP   r>   rT   rU   rˆ   )	rs   rœ   r   r   r   r‡   r†   ru   rU   s	   &&&&&    r   Úfrom_linearÚQuantizedLinear.from_linear  s~   € ð #/×"5Ñ"5×";Ñ";ÑˆÙ�¨%°ÈÔMˆÜ(*¯ªØ×ÑØØØô	)
Ð%ˆŒ	�2”9˜v÷ "(�F˜1–I¨TˆŒ	à�\Ô!Ø"×'Ñ'ˆBŒGàˆ	r   )rˆ   rU   r   r   r   rT   rS   )TNNr	   rx   )ry   rz   r{   r|   r}   rM   rm   r^   r~   rž   r   r€   r�   r‚   s   @@r   r„   r„   È   s=   ù‡ € ñ÷, õ  òD
òð ÷ñ ó ÷ð r   r„   c                   ó–   a a€ ] tR tRt oRtRV3R lV 3R llltR tR tR tV3R lV 3R	 llt	R
 t
]RV3R lR ll4       tRtVtV ;t# )ÚQQLineari1  a»  Quantizes the input and applies an affine transformation using quantized weights.

Two use cases are supported:

1) **Eval**:  The weights are frozen and stored in quantized form together with
   their scales (``self.weight`` is quantized and ``self.scales`` is provided).
2) **Train**: The weights are stored in higher precision and are quantized on
     the fly during computation so that gradients with respect to the weights
     can be computed.

To switch between the two cases, use ``layer.eval()`` and ``layer.train()`` respectively.

Compared to the :class:`mlx.nn.QuantizedLinear` layer, this layer
quantizes the input as well and includes weights in gradient computations.

:obj:`QQLinear` also provides the class method :meth:`from_linear` to
convert :class:`mlx.nn.Linear` layers to :obj:`QQLinear` layers.

Note: This layer does not support a bias term yet.

Args:
    input_dims (int): The dimensionality of the input features.
    output_dims (int): The dimensionality of the output features.
    group_size (Optional[int]): The group size to use for the quantized weight.
        See :func:`~mlx.core.quantize`. Default: ``None``.
    bits (Optional[int]): The bit width to use for the quantized weight.
        See :func:`~mlx.core.quantize`. Default: ``None``.
    mode (Optional[str]): The quantization method to use (see
        :func:`mlx.core.quantize`). Currently, only ``"nvfp4"`` and ``"mxfp8"``
        are supported. Default: ``"nvfp4"``.
c          
      ó8   <€ V ^8„  d   QhRS[ RS[ RS[ RS[ RS[/# )r   r†   r‡   r   r   r   rD   )r%   rE   s   "€r   r&   ÚQQLinear.__annotate__R  s=   ø€ ÷  ñ  áð ñ ð ñ ð	 ñ
 ð ñ ñ r   c                óü   <€ \         SV `  4        \        WSV4      w  V n        V n        WPn        \        P                  ! ^V,          4      p\        P                  P                  V) VW!3R7      V n        RV n        R# )rH   r‹   FN)rL   rM   r   r   r   r   rN   rO   rP   rQ   rŽ   rS   Ú
_quantized)rW   r†   r‡   r   r   r   rJ   rX   s   &&&&&& €r   rM   ÚQQLinear.__init__R  sp   ø€ ô 	‰ÑÔô &8¸È$Ó%OÑ"ˆŒ˜œØŒ	ä—	’	˜!˜j�.Ó)ˆÜ—i‘i×'Ñ'Ø�ØØÐ+ð (ó 
ˆŒð
  ˆŽr   c           
     ó  € V P                   P                  w  rV P                   P                  \        P                  8X  d   V^ ,          V P
                  ,          pRV RV RV P                   RV P
                   RV P                   2
# )r   r’   r“   ri   rj   rk   )rS   rI   ÚdtyperP   Úuint32r   r   r   r”   s   &  r   rm   ÚQQLinear._extra_reprh  sw   € Ø ŸK™K×-Ñ-ÑˆØ�;‰;×Ñ¤§	¡	Ô)Ø •|¨¯	©	Õ1ˆGà˜'˜ .°°
ð ;ØŸ/™/Ð*¨'°$·)±)°¸GÀDÇIÁIÀ;ðPð	
r   c                óÚ   € V P                   '       gY   \        P                  ! V P                  V P                  V P
                  V P                  R 7      w  V n        V n        RV n         R# R# )rK   TN)r¥   rP   r>   rS   r   r   r   rT   rl   s   &r   r>   ÚQQLinear.quantizeq  sL   € Ø��ˆÜ')§{¢{Ø—‘Ø—‘Ø—	‘	Ø—Y‘Yô	(Ñ$ˆDŒK˜œð #ˆDŽOñ r   c                ó  € V P                   '       dm   \        P                  ! V P                  V P                  V P
                  V P                  V P                  R 7      V n        V P                  R4       RV n         R# R# )©rT   r   r   r   rT   FN)	r¥   rP   r\   rS   rT   r   r   r   Ú__delattr__rl   s   &r   r\   ÚQQLinear.dequantize{  sZ   € Ø�?�?ˆ?ÜŸ-š-Ø—‘Ø—{‘{ØŸ?™?Ø—Y‘YØ—Y‘YôˆDŒKð ×Ñ˜XÔ&Ø#ˆDŽOñ r   c                ó    <€ V ^8„  d   QhRS[ /# )r   r   )r#   )r%   rE   s   "€r   r&   r£   ‡  s   ø€ ÷ ñ ¡tñ r   c                óŽ   <€ \         SV `  V4       V P                  '       d   V P                  4        R # V P	                  4        R # )N)rL   Ú_set_training_modeÚ	_trainingr\   r>   )rW   r   rX   s   &&€r   r³   ÚQQLinear._set_training_mode‡  s,   ø€ Ü‰Ñ" 4Ô(à�>�>ˆ>Ø�O‰OÖà�M‰MŽOr   c           	     ó¦   € \         P                  ! VV R ,          V P                  R4      V P                  V P                  V P
                  R7      pV# )rS   rT   r®   )rP   Úqqmmr[   r   r   r   re   s   &&r   r^   ÚQQLinear.__call__�  sB   € Ü�GŠGØØ��NØ—8‘8˜HÓ%Ø—‘Ø—‘Ø—‘ô
ˆð ˆr   c                ó2   <€ V ^8„  d   QhRS[ RS[RS[RS[/# r›   rq   )r%   rE   s   "€r   r&   r£   ›  s3   ø€ ÷ ñ áðñ ðñ ð	ñ
 ñr   c                óà   € VP                   P                  w  rVVP                  R4      e   \        R4      hV ! WeW#VR7      pVP                   Vn         VP	                  VP
                  4       V# )z:Create a :obj:`QQLinear` layer from a :obj:`Linear` layer.rˆ   z#QQLinear does not support bias yet.rK   )rS   rI   r[   ÚNotImplementedErrorÚtrainÚtraining)rs   rœ   r   r   r   r‡   r†   ru   s   &&&&&   r   rž   ÚQQLinear.from_linearš  sg   € ð #/×"5Ñ"5×";Ñ";ÑˆØ×Ñ˜FÓ#Ò/Ü%Ð&KÓLÐLÙ�¨*ÀÔFˆØ ×'Ñ'ˆŒ	Ø
�‰�×&Ñ&Ô'àˆ	r   )r¥   r   r   r   rT   rS   )NNr   )ry   rz   r{   r|   r}   rM   rm   r>   r\   r³   r^   r~   rž   r   r€   r�   r‚   s   @@r   r¡   r¡   1  sQ   ù‡ € ñ÷@ õ  ò,
ò#ò
$÷ó ò	ð ÷ñ ó ÷ð r   r¡   )NN)rN   Útypingr   r   r   Úmlx.coreÚcorerP   Úmlx.nn.layers.baser   Ú	mlx.utilsr   r   r>   r@   r„   r¡   r   r   r   Ú<module>rÄ      s{   ðó ß ,Ñ ,å Ý %Ý (òBñI!ð
 ðI!ð !ðI!ð MQ÷I!ð I!ôXc˜ô côLf�fô fôRyˆvö yr   