§
    l�’j±+  ã                   ó¾   — d dl mZ ddlmZmZmZ ddlmZ ddlm	Z	  e¦   «         rd dl
Z
ddlmZ dd	lmZmZ  ej        e¦  «        Zd
efd„Z G d„ de	¦  «        ZdS )é    )Údeepcopyé   )Úis_compressed_tensors_availableÚis_torch_availableÚlogging)ÚCompressedTensorsConfigé   )ÚHfQuantizerN)ÚWeightConverter)ÚDecompressExpertsÚget_experts_schemeÚreturnc                 óD   — | j         }|duo|j        dk    o
|j        dk    S )zJWhether a compressed-tensors quantization scheme quantizes weights to FP8.NÚfloaté   )ÚweightsÚtypeÚnum_bits)Úschemer   s     úbC:\Projects\DSCons\.venv\Lib\site-packages\transformers\quantizers\quantizer_compressed_tensors.pyÚ_is_fp8_schemer       s.   € àŒn€GØ˜$ÐÐT 7¤<°7Ò#:ÐT¸wÔ?OÐSTÒ?TÐTó    c                   ó¦   ‡ — e Zd ZU dZdZeed<   defˆ fd„Zd„ Zd„ Z	d„ Z
d„ Zed	„ ¦   «         Zed
efd„¦   «         Zd
efd„Zd
efd„Zd„ Zd„ Zˆ xZS )ÚCompressedTensorsHfQuantizera«  
    Quantizer for the compressed_tensors package. Loads and restores models to
    quantized state with compressed_tensors.

    With `use_optimized_inference=True`, FP8 checkpoints are kept in FP8 and their matmuls run through
    row-wise FP8 kernels (`torch.nn.functional.scaled_mm`) via `CompressedTensorsFP8Linear`, when FP8 matmul
    hardware is available (CUDA SM89+ or XPU). This is opt-in and inference only.

    Otherwise the model goes through the regular compressed-tensors route: `dequantize=True`
    dequantizes the weights at load time, while `dequantize=False` leaves them compressed and lets
    compressed-tensors decompress them on the first forward pass.
    TÚquantization_configc                 ó²   •—  t          ¦   «         j        |fi |¤Ž |                     ¦   «          ddlm} |                     |¦  «        | _        || _        d S )Nr   )ÚModelCompressor)ÚsuperÚ__init__Ú	post_initÚcompressed_tensors.compressorsr   Úfrom_compression_configÚ
compressorr   )Úselfr   Úkwargsr   Ú	__class__s       €r   r   z%CompressedTensorsHfQuantizer.__init__7   sm   ø€ Ø�‰ŒÔÐ,Ð7Ð7°Ð7Ð7Ð7ð 	×%Ò%Ñ'Ô'Ð'ØBÐBÐBÐBÐBÐBà)×AÒAÐBUÑVÔVˆŒØ#6ˆÔ Ð Ð r   c                 óz  — t          ¦   «         st          d¦  «        ‚| j        j        | _        | j        r�t
          j                             ¦   «         set
          j                             ¦   «         r"t
          j         	                    ¦   «         dk    s't                               d¦  «         d| _        d S d S d S d S )NzyUsing `compressed_tensors` quantized models requires compressed-tensors>=0.15.0: `pip install compressed-tensors>=0.15.0`)r   é	   z Ignoring `use_optimized_inference=True`: FP8 matmul kernels need a CUDA GPU with compute capability >= 8.9 (e.g. 4090/H100) or an Intel XPU, and none was found.F)r   ÚImportErrorr   Úuse_optimized_inferenceÚuse_fp8_kernelÚtorchÚxpuÚis_availableÚcudaÚget_device_capabilityÚloggerÚwarning_once)r$   Úargsr%   s      r   Úvalidate_environmentz1CompressedTensorsHfQuantizer.validate_environmentD   sÐ   € Ý.Ñ0Ô0ð 	Ýð;ñô ð ð #Ô6ÔNˆÔØÔð 	(ÝŒI×"Ò"Ñ$Ô$ð	(Ý).¬×)@Ò)@Ñ)BÔ)Bð	(ÝGLÄz×GgÒGgÑGiÔGiÐmsÒGsÐGså×ÒðOñô ð ð #(ˆDÔÐÐð	(ð 	(ð 	(ð 	(ØGsÐGsr   c                 ó<  ‡	— | j         j        }t          |j        ¦  «        }| j        rœddlm} d„ |                     ¦   «         D ¦   «         Š	ˆ	fd„|                     ¦   «         D ¦   «         }|                      |d |j	        ¦  «        | _
        d„ ‰	                     ¦   «         D ¦   «         } ||||j        | j
        ¬¦  «         |sd S ddlm} t          |¦  «        }||_         |||d	¬
¦  «         | j        j        r| j                              |¬¦  «         d S d S )Nr   )Ú*replace_with_compressed_tensors_fp8_linearc                 ó8   — i | ]\  }}t          |¦  «        ¯||“ŒS © )r   )Ú.0ÚnameÚgroups      r   ú
<dictcomp>zUCompressedTensorsHfQuantizer._process_model_before_weight_loading.<locals>.<dictcomp>a   s-   € ÐkÐkÐk©+¨$°ÕUcÐdiÑUjÔUjÐk˜$ ÐkÐkÐkr   c                 ó$   •— i | ]\  }}|‰v¯	||“ŒS r8   r8   )r9   r:   r;   Ú
fp8_groupss      €r   r<   zUCompressedTensorsHfQuantizer._process_model_before_weight_loading.<locals>.<dictcomp>b   s+   ø€ ÐrÐrÐr±°°eÐ[_ÐgqÐ[qÐ[q  eÐ[qÐ[qÐ[qr   c                 ó&   — g | ]}|j         D ]}|‘ŒŒS r8   )Útargets)r9   r;   Útargets      r   ú
<listcomp>zUCompressedTensorsHfQuantizer._process_model_before_weight_loading.<locals>.<listcomp>e   s'   € Ð[Ð[Ð[ %ÈUÌ]Ð[Ð[À6�vÐ[Ð[Ð[Ð[r   )r@   ÚignoreÚmodules_to_not_convertr   )Úapply_quantization_configF)Úrun_compressed©Úmodel)r#   r   ÚdictÚconfig_groupsr+   Úintegrations.compressed_tensorsr6   ÚitemsÚget_modules_to_not_convertÚ_keep_in_fp32_modulesrD   ÚvaluesrC   Úcompressed_tensors.quantizationrE   r   Úis_quantization_compressedÚcompress_model)
r$   rH   r%   Ú	ct_configÚremaining_groupsr6   r@   rE   Úremaining_configr>   s
            @r   Ú$_process_model_before_weight_loadingzACompressedTensorsHfQuantizer._process_model_before_weight_loadingX   sy  ø€ Ø”OÔ7ˆ	Ý 	Ô 7Ñ8Ô8ÐàÔð 	ØdÐdÐdÐdÐdÐdð lÐkÐ9I×9OÒ9OÑ9QÔ9QÐkÑkÔkˆJØrÐrÐrÐrÐ?O×?UÒ?UÑ?WÔ?WÐrÑrÔrÐà*.×*IÒ*IÈ%ÐQUÐW\ÔWrÑ*sÔ*sˆDÔ'Ø[Ð[¨:×+<Ò+<Ñ+>Ô+>Ð[Ñ[Ô[ˆGØ6Ð6ØØØ Ô'Ø'+Ô'Bð	ñ ô ð ð  ð 	ØˆFàMÐMÐMÐMÐMÐMå# IÑ.Ô.ÐØ)9ÐÔ&à!Ð! %Ð)9È%ÐPÑPÔPÐPð Ô#Ô>ð 	8ØŒO×*Ò*°Ð*Ñ7Ô7Ð7Ð7Ð7ð	8ð 	8r   c                 ót   — | j         j        r)| j         j        r| j                             |¬¦  «         dS dS dS )aÀ  Dequantize the layers loaded through the compressed-tensors wrappers, but only when asked
        for: with `dequantize=False` the weights are left compressed, and the hook `compress_model`
        registered decompresses them on the first forward pass instead.

        FP8-kernel modules were never wrapped (their weights loaded directly in FP8) and hold no
        compressed-tensors scheme, so neither the call below nor that hook touches them.rG   N)r   Ú
dequantizerQ   r#   Údecompress_model)r$   rH   r%   s      r   Ú#_process_model_after_weight_loadingz@CompressedTensorsHfQuantizer._process_model_after_weight_loading{   sR   € ð Ô#Ô.ð 	:°4Ô3KÔ3fð 	:ØŒO×,Ò,°5Ð,Ñ9Ô9Ð9Ð9Ð9ð	:ð 	:ð 	:ð 	:r   c                 óÈ   — ddddddœ}|                      ¦   «         �E|                      ¦   «         j        �,|                      ¦   «         j                             |¦  «         |S )NÚcolwiseÚrowwise)z0layers.*.feed_forward.experts.*.gate_proj.weightz6layers.*.feed_forward.experts.*.gate_proj.weight_scalez.layers.*.feed_forward.experts.*.up_proj.weightz4layers.*.feed_forward.experts.*.up_proj.weight_scalez0layers.*.feed_forward.experts.*.down_proj.weight)Úget_text_configÚbase_model_tp_planÚupdate)r$   ÚconfigÚadditional_plans      r   Úupdate_tp_planz+CompressedTensorsHfQuantizer.update_tp_plan‡   sm   € à@IØFOØ>GØDMØ@Ið
ð 
ˆð ×!Ò!Ñ#Ô#Ð/°F×4JÒ4JÑ4LÔ4LÔ4_Ð4kØ×"Ò"Ñ$Ô$Ô7×>Ò>¸ÑOÔOÐOàˆr   c                 ó   — | j          S )N)r+   ©r$   s    r   Úis_trainablez)CompressedTensorsHfQuantizer.is_trainable”   s   € ð Ô&Ð&Ð&r   r   c                 ó   — dS )NTr8   re   s    r   Úis_compileablez+CompressedTensorsHfQuantizer.is_compileable™   ó   € àˆtr   c                 óF   — | j         rdS | j        j        p| j        j         S )z7Loaded Models can carry out quantization aware trainingF)r+   r   rX   rQ   re   s    r   Úis_qat_trainablez-CompressedTensorsHfQuantizer.is_qat_trainable�   s,   € àÔð 	Ø�5àÔ'Ô2Ðm¸$Ô:RÔ:mÐ6mÐmr   c                 ó   — dS )z>Models quantized using compressed tensors can be saved to diskTr8   re   s    r   Úis_serializablez,CompressedTensorsHfQuantizer.is_serializable¤   ri   r   c                 ó\   — | j         sg S ddlm} t          dgdg |¦   «         g¬¦  «        gS )uÆ   On the FP8 kernel path, a generic converter reshapes the checkpoint ``weight_scale``
        tensors into the row-wise kernel layout once at load time â€” see
        :class:`ConvertFP8LinearScale`.r   )ÚConvertFP8LinearScaleÚweight_scale©Úsource_patternsÚtarget_patternsÚ
operations)r+   rK   ro   r   )r$   ro   s     r   Úget_weight_conversionsz3CompressedTensorsHfQuantizer.get_weight_conversions¨   s_   € ð Ô"ð 	ØˆIàKÐKÐKÐKÐKÐKõ Ø!/Ð 0Ø!/Ð 0Ø1Ð1Ñ3Ô3Ð4ðñ ô ð
ð 	
r   c                 óº  — g }|D �]-}t          |t          ¦  «        rt          d„ |j        D ¦   «         ¦  «        r|                     |¦  «         ŒLd„ |j        D ¦   «         }|r¹t          | j        j        ¦  «        }d„ |D ¦   «         }d„ |j        D ¦   «         }t          |¦  «        rd„ |D ¦   «         |z   |z   }n#d„ |D ¦   «         }	d„ |D ¦   «         }
|	|z   |
z   |z   }t          | |¬¦  «        gt          |j
        ¦  «        z   }t          ||j        |¬	¦  «        }|                     |¦  «         �Œ/|                     |                      ¦   «         ¦  «         |S )
aQ  Attach the quantization sources (scales, packed weights) of MoE expert converters
        to their bucket and prepend a :class:`DecompressExperts` op, so the per-expert
        (weight, scale) pairs are dequantized *before* the merge / concat ops collapse the
        per-expert structure. FP8 checkpoints keep the plain ``weight`` name; packed formats
        use ``weight_packed`` / ``weight_shape``.

        The generic converters from :meth:`get_weight_conversions` are appended at the end:
        converters are matched in order, so the expert converters keep their scale keys.
        c              3   ó   K  — | ]}d |vV — Œ	dS )ÚexpertsNr8   ©r9   Úps     r   ú	<genexpr>zICompressedTensorsHfQuantizer.update_weight_conversions.<locals>.<genexpr>Æ   s(   è è € Ð;mÐ;mÐST¸IÈQÐ<NÐ;mÐ;mÐ;mÐ;mÐ;mÐ;mr   c                 ó<   — g | ]}|                      d ¦  «        ¯|‘ŒS ©z.weight©Úendswithry   s     r   rB   zJCompressedTensorsHfQuantizer.update_weight_conversions.<locals>.<listcomp>É   s)   € ÐWÐWÐW AÀÇÂÈIÑAVÔAVÐW˜aÐWÐWÐWr   c                 ó   — g | ]}|d z   ‘ŒS )z_scale$r8   ry   s     r   rB   zJCompressedTensorsHfQuantizer.update_weight_conversions.<locals>.<listcomp>Ì   s   € Ð GÐ GÐ G°1  Y¡Ð GÐ GÐ Gr   c                 ó<   — g | ]}|                      d ¦  «        °|‘ŒS r}   r~   ry   s     r   rB   zJCompressedTensorsHfQuantizer.update_weight_conversions.<locals>.<listcomp>Í   s)   € ÐVÐVÐV˜qÀÇ
Â
È9Ñ@UÔ@UÐV˜ÐVÐVÐVr   c                 ó   — g | ]}|d z   ‘ŒS )ú$r8   ry   s     r   rB   zJCompressedTensorsHfQuantizer.update_weight_conversions.<locals>.<listcomp>Ó   s   € Ð"CÐ"CÐ"C¨q 1 s¡7Ð"CÐ"CÐ"Cr   c                 ó   — g | ]}|d z   ‘ŒS )z_packed$r8   ry   s     r   rB   zJCompressedTensorsHfQuantizer.update_weight_conversions.<locals>.<listcomp>Õ   s   € Ð$LÐ$LÐ$L¸ Q¨¡^Ð$LÐ$LÐ$Lr   c                 ó   — g | ]}|d z   ‘ŒS )z_shape$r8   ry   s     r   rB   zJCompressedTensorsHfQuantizer.update_weight_conversions.<locals>.<listcomp>Ö   s   € Ð$KÐ$KÐ$K°q Q¨¡]Ð$KÐ$KÐ$Kr   )r   rq   )Ú
isinstancer   Úanyrr   Úappendr   r   r   r   Úlistrt   Ú_original_target_patternsÚextendru   )r$   Úweight_conversionsÚupdatedÚconvÚweight_sourcesr   Úscale_sourcesÚotherÚnew_sourcesÚpacked_weightÚshape_sourcesÚnew_opss               r   Úupdate_weight_conversionsz6CompressedTensorsHfQuantizer.update_weight_conversions¹   s¨  € ð ˆØ&ð 	!ñ 	!ˆDå˜d¥OÑ4Ô4ð ½Ð;mÐ;mÐX\ÔXlÐ;mÑ;mÔ;mÑ8mÔ8mð Ø—’˜tÑ$Ô$Ð$ØØWÐW¨Ô)=ÐWÑWÔWˆNØð Ý+¨DÔ,DÔ,XÑYÔY�Ø GÐ G¸Ð GÑ GÔ G�ØVÐV DÔ$8ÐVÑVÔV�Ý! &Ñ)Ô)ð 	Xð
 #DÐ"C°NÐ"CÑ"CÔ"CÀmÑ"SÐV[Ñ"[�K�Kà$LÐ$L¸^Ð$LÑ$LÔ$L�MØ$KÐ$K¸NÐ$KÑ$KÔ$K�MØ"/°-Ñ"?À-Ñ"OÐRWÑ"W�KÝ,¨T¸&ÐAÑAÔAÐBÅTÈ$Ì/ÑEZÔEZÑZ�Ý&Ø$/Ø$(Ô$BØ&ðñ ô �ð
 �NŠN˜4Ñ Ô Ð Ñ à�Š�t×2Ò2Ñ4Ô4Ñ5Ô5Ð5Øˆr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrequires_calibrationr   Ú__annotations__r   r4   rV   rZ   rc   Úpropertyrf   Úboolrh   rk   rm   ru   r–   Ú__classcell__)r&   s   @r   r   r   &   s=  ø€ € € € € € ðð ð  ÐØ0Ð0Ð0Ñ0ð7Ð,Cð 7ð 7ð 7ð 7ð 7ð 7ð(ð (ð (ð(!8ð !8ð !8ðF:ð :ð :ðð ð ð ð'ð 'ñ „Xð'ð ð ð ð ð ñ „Xððn $ð nð nð nð nð ð ð ð ð ð
ð 
ð 
ð"(ð (ð (ð (ð (ð (ð (r   r   )Úcopyr   Úutilsr   r   r   Úutils.quantization_configr   Úbaser
   r,   Úcore_model_loadingr   rK   r   r   Ú
get_loggerr—   r1   rž   r   r   r8   r   r   ú<module>r¦      s  ðð Ð Ð Ð Ð Ð à PÐ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PØ ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø Ð Ð Ð Ð Ð ð ÐÑÔð XØ€L€L€Là4Ð4Ð4Ð4Ð4Ð4ØWÐWÐWÐWÐWÐWÐWÐWð 
ˆÔ	˜HÑ	%Ô	%€ðU˜dð Uð Uð Uð Uð{ð {ð {ð {ð { ;ñ {ô {ð {ð {ð {r   