+
    œ°³j›*  ã                   óÂ   € ^ RI Ht ^RIHtHtHt ^RIHt ^RIH	t	 ]! 4       '       d   ^ RI
t
^RIHt ^RIHtHt ]P                   ! ]4      tR R	 lt ! R
 R]	4      tR# )é    )Údeepcopy)Úis_compressed_tensors_availableÚis_torch_availableÚlogging©ÚCompressedTensorsConfig)ÚHfQuantizerN)ÚWeightConverter)ÚDecompressExpertsÚget_experts_schemec                ó$   € V ^8„  d   QhR\         /# ©é   Úreturn©Úbool)Úformats   "Úu/home/luca/screen-checking/.venv/lib/python3.14/site-packages/transformers/quantizers/quantizer_compressed_tensors.pyÚ__annotate__r       s   € ÷ Uñ Uœdñ Uó    c                ó~   € V P                   pVRJ;'       d'    VP                  R8H  ;'       d    VP                  ^8H  # )zJWhether a compressed-tensors quantization scheme quantizes weights to FP8.NÚfloat)ÚweightsÚtypeÚnum_bits)Úschemer   s   & r   Ú_is_fp8_schemer       s;   € à�n‰n€GØ˜$Ð×TÐT 7§<¡<°7Ñ#:×TÐT¸w×?OÑ?OÐSTÑ?TÐTr   c                   óÆ   a a€ ] tR t^&t oRtRtV3R lV 3R lltR tR tR t	R t
]R	 4       t]V3R
 lR l4       tV3R lR ltV3R lR ltR tR tV3R ltRtVtV ;t# )Ú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.
Tc                ó    <€ V ^8„  d   QhRS[ /# ©r   Úquantization_configr   )r   Ú__classdict__s   "€r   r   Ú)CompressedTensorsHfQuantizer.__annotate__7   s   ø€ ÷ 7ñ 7Ñ,Cñ 7r   c                óŽ   <€ \         SV `  ! V3/ VB  VP                  4        ^ RIHp VP                  V4      V n        Wn        R# )r   )ÚModelCompressorN)ÚsuperÚ__init__Ú	post_initÚcompressed_tensors.compressorsr&   Úfrom_compression_configÚ
compressorr"   )Úselfr"   Úkwargsr&   Ú	__class__s   &&, €r   r(   Ú%CompressedTensorsHfQuantizer.__init__7   s@   ø€ Ü‰ÒÐ,Ñ7°Ò7ð 	×%Ñ%Ô'ÝBà)×AÑAÐBUÓVˆŒØ#6Ö r   c                ó°  € \        4       '       g   \        R 4      hV P                  P                  V n        V P                  '       dŽ   \
        P                  P                  4       '       gh   \
        P                  P                  4       '       d$   \
        P                  P                  4       R8¼  g   \        P                  R4       RV n        R# R# R# R# )zyUsing `compressed_tensors` quantized models requires compressed-tensors>=0.15.0: `pip install compressed-tensors>=0.15.0`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.FN)é   é	   )r   ÚImportErrorr"   Úuse_optimized_inferenceÚuse_fp8_kernelÚtorchÚxpuÚis_availableÚcudaÚget_device_capabilityÚloggerÚwarning_once)r-   Úargsr.   s   &*,r   Úvalidate_environmentÚ1CompressedTensorsHfQuantizer.validate_environmentD   s¦   € Ü.×0Ò0Üð;óð ð #×6Ñ6×NÑNˆÔØ××ÐÜ�I‰I×"Ñ"×$Ò$¬¯©×)@Ñ)@×)BÒ)BÄuÇzÁz×GgÑGgÓGiÐmsÔGsä×ÑðOôð #(ˆDÖñ HtÑ$ñ r   c                ó  € V P                   P                  p\        VP                  4      pV P                  '       dÜ   ^RIHp VP                  4        UUu/ uF  w  rg\        V4      '       g   K  WgbK  	  pppVP                  4        UUu/ uF  w  rgWh9  g   K  WgbK  	  pppV P                  VRVP                  4      V n        VP                  4        UU	u. uF  qwP                   F  q™NK  	  K  	  p
pp	V! VV
VP                  V P                  R7       ^ RIHp \#        V4      pWLn        V! WRR7       V P                  P$                  '       d   V P                   P'                  VR7       R# R# u uppi u uppi u up	pi )r   )Ú*replace_with_compressed_tensors_fp8_linearN)ÚtargetsÚignoreÚmodules_to_not_convert)Úapply_quantization_configF)Úrun_compressed©Úmodel)r,   r"   ÚdictÚconfig_groupsr6   Úintegrations.compressed_tensorsrB   Úitemsr   Úget_modules_to_not_convertÚ_keep_in_fp32_modulesrE   ÚvaluesrC   rD   Úcompressed_tensors.quantizationrF   r   Úis_quantization_compressedÚcompress_model)r-   rI   r.   Ú	ct_configÚremaining_groupsrB   ÚnameÚgroupÚ
fp8_groupsÚtargetrC   rF   Úremaining_configs   &&,          r   Ú$_process_model_before_weight_loadingÚACompressedTensorsHfQuantizer._process_model_before_weight_loadingX   sT  € Ø—O‘O×7Ñ7ˆ	Ü 	× 7Ñ 7Ó8Ðà××ÐÝdð :J×9OÑ9OÔ9QÔkÑ9Q©+¨$ÔUcÐdi×Ujœ+˜$š+Ñ9QˆJÑkØ?O×?UÑ?UÔ?WÔrÑ?W±°Ð[_Ñ[q¤ ¢Ñ?WÐÑrà*.×*IÑ*IÈ%ÐQUÐW\×WrÑWrÓ*sˆDÔ'Ø+5×+<Ñ+<Ô+>Ô[Ñ+> %Ï]Ì]À6’vÉ]‘vÑ+>ˆGÑ[Ù6ØØØ ×'Ñ'Ø'+×'BÑ'Bõ	õ 	Nä# IÓ.ÐØ)9Ô&á! %È%ÕPð ×#Ñ#×>×>Ð>Ø�O‰O×*Ñ*°Ð*Ö7ñ ?ùó) lùÛrùó \s   ÁE6Á1E6Â
E<ÂE<ÃFc                ó¶   € V P                   P                  '       d=   V P                   P                  '       d   V P                  P	                  VR7       R# R# R# )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.rH   N)r"   Ú
dequantizerR   r,   Údecompress_model)r-   rI   r.   s   &&,r   Ú#_process_model_after_weight_loadingÚ@CompressedTensorsHfQuantizer._process_model_after_weight_loadingx   sE   € ð ×#Ñ#×.×.Ð.°4×3KÑ3K×3f×3fÐ3fØ�O‰O×,Ñ,°5Ð,Ö9ñ 4gÑ.r   c           
     óÌ   € R RRRRRRRRR/pVP                  4       eF   VP                  4       P                  e*   VP                  4       P                  P                  V4       V# )z0layers.*.feed_forward.experts.*.gate_proj.weightÚcolwisez6layers.*.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Úrowwise)Úget_text_configÚbase_model_tp_planÚupdate)r-   ÚconfigÚadditional_plans   && r   Úupdate_tp_planÚ+CompressedTensorsHfQuantizer.update_tp_plan„   si   € à>À	ØDÀiØ<¸iØBÀIØ>À	ð
ˆð ×!Ñ!Ó#Ò/°F×4JÑ4JÓ4L×4_Ñ4_Ò4kØ×"Ñ"Ó$×7Ñ7×>Ñ>¸ÔOàˆr   c                ó$   € V P                   '       * # )N)r6   ©r-   s   &r   Úis_trainableÚ)CompressedTensorsHfQuantizer.is_trainable‘   s   € ð ×&Ñ&Ô&Ð&r   c                ó    <€ V ^8„  d   QhRS[ /# r   r   )r   r#   s   "€r   r   r$   —   s   ø€ ÷ ñ ¡ñ r   c                ó   € R # )T© rm   s   &r   Úis_compileableÚ+CompressedTensorsHfQuantizer.is_compileable–   ó   € ár   c                ó    <€ V ^8„  d   QhRS[ /# r   r   )r   r#   s   "€r   r   r$   š   s   ø€ ÷ nñ n¡$ñ nr   c                óœ   € V P                   '       d   R# V P                  P                  ;'       g    V P                  P                  '       * # )z7Loaded Models can carry out quantization aware trainingF)r6   r"   r^   rR   rm   s   &r   Úis_qat_trainableÚ-CompressedTensorsHfQuantizer.is_qat_trainableš   s:   € à××ÐÙà×'Ñ'×2Ñ2×mÐm¸$×:RÑ:R×:mÑ:mÔ6mÐmr   c                ó    <€ V ^8„  d   QhRS[ /# r   r   )r   r#   s   "€r   r   r$   ¡   s   ø€ ÷ ñ ¡ñ r   c                ó   € R# )z>Models quantized using compressed tensors can be saved to diskTrr   rm   s   &r   Úis_serializableÚ,CompressedTensorsHfQuantizer.is_serializable¡   ru   r   c                ód   € V P                   '       g   . # ^RIHp \        R.R.V! 4       .R7      .# )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`.)ÚConvertFP8LinearScaleÚweight_scale©Úsource_patternsÚtarget_patternsÚ
operations)r6   rL   r   r
   )r-   r   s   & r   Úget_weight_conversionsÚ3CompressedTensorsHfQuantizer.get_weight_conversions¥   sA   € ð ×"×"Ð"ØˆIåKô Ø!/Ð 0Ø!/Ð 0Ù1Ó3Ð4ôð
ð 	
r   c                ój  € . pV EFì  p\        V\        4      '       dO   \        ;QJ d&    R VP                   4       F  '       g   K   RM	  RM! R VP                   4       4      '       d   VP	                  V4       K{  VP                   Uu. uF  qDP                  R4      '       g   K  VNK  	  ppV'       Ed%   \        V P                  P                  4      pV Uu. uF  qDR,           NK  	  ppVP                   Uu. uF  qDP                  R4      '       d   K  VNK  	  pp\        V4      '       d(   V Uu. uF  qDR,           NK  	  upV,           V,           p	MDV Uu. uF  qDR,           NK  	  p
pV Uu. uF  qDR,           NK  	  ppW§,           V,           V,           p	\        WR	7      .\        VP                  4      ,           p\        V	VP                  VR
7      pVP	                  V4       EKï  	  VP                  V P                  4       4       V# u upi u upi u upi u upi u upi u upi )a  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   ó,   "  € T F
  pR V9  x € K  	  R# 5i)ÚexpertsNrr   )Ú.0Úps   & r   Ú	<genexpr>ÚICompressedTensorsHfQuantizer.update_weight_conversions.<locals>.<genexpr>Ã   s   é € Ð;mÑXlÐST¸IÈQÖ<NÓXlùs   ‚TFz.weightz_scale$Ú$z_packed$z_shape$)r   r�   )Ú
isinstancer
   Úanyr‚   ÚappendÚendswithr   r"   r   r   Úlistr„   Ú_original_target_patternsÚextendr…   )r-   Úweight_conversionsÚupdatedÚconvr‹   Úweight_sourcesr   Úscale_sourcesÚotherÚnew_sourcesÚpacked_weightÚshape_sourcesÚnew_opss   &&           r   Úupdate_weight_conversionsÚ6CompressedTensorsHfQuantizer.update_weight_conversions¶   sË  € ð ˆÜ&ˆDä˜d¤O×4Ò4¿»Ñ;mÐX\×XlÒXlÓ;m¿¿ºÑ;mÐX\×XlÒXlÓ;m×8mÒ8mØ—‘˜tÔ$ÙØ)-×)=Ò)=ÓWÑ)= AÇÁÈI×AVŸa˜aÑ)=ˆNÐWßˆ~Ü+¨D×,DÑ,D×,XÑ,XÓY�Ù8FÓ G¹°1 Y§ ¹�Ð GØ$(×$8Ò$8ÓVÑ$8˜qÇ
Á
È9×@UŸ˜Ñ$8�ÐVÜ! &×)Ò)ñ
 5CÓ"C±N¨q s§7 7±NÑ"CÀmÕ"SÐV[Õ"[‘Ká=KÓ$L¹^¸¨§^ ^¹^�MÐ$LÙ<JÓ$K¹N°q¨§] ]¹N�MÐ$KØ"/Õ"?À-Õ"OÐRWÕ"W�KÜ,¨TÔAÐBÄTÈ$Ï/É/ÓEZÕZ�Ü&Ø$/Ø$(×$BÑ$BØ&ô�ð
 �N‰N˜4× ñ5 'ð8 	�‰�t×2Ñ2Ó4Ô5Øˆùò1 Xùò !HùÚVùò #Dùâ$LùÚ$Ks0   ÂHÂ+HÃHÄ H!ÄH!Ä:H&Å!H+Å8H0c                ó&   <€ V ^8„  d   Qh/ S[ ;R&   # r!   r   )r   r#   s   "€r   r   r$   &   s   ø‡ ‚ ñ 1Ñ0ò r   )r,   rE   r"   r6   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úrequires_calibrationr(   r?   r[   r`   rj   Úpropertyrn   rs   rx   r|   r…   r    Ú__annotate_func__Ú__static_attributes__Ú__classdictcell__Ú__classcell__)r/   r#   s   @@r   r   r   &   s�   ù‡ € ñð  Ð÷7ó 7ò(ò(8ò@:òð ñ'ó ð'ð ÷ó ð÷nð n÷ð ò
ò"(÷a … r   r   )Úcopyr   Úutilsr   r   r   Úutils.quantization_configr   Úbaser	   r7   Úcore_model_loadingr
   rL   r   r   Ú
get_loggerr£   r<   r   r   rr   r   r   Ú<module>r´      sP   ðõ ç PÑ PÝ ?Ý ñ ×ÒÛå4ßWð 
×	Ò	˜HÓ	%€õUôx ;ö xr   