Ë
    ÜÓ^jØ  ã                   óÀ   — d dl mZmZ ddlmZ ddlmZ erddlmZ ddl	m
Z
 ddlmZmZmZmZmZ dd	l	mZ  e«       rd d
lZ ej&                  e«      Z G d„ de«      Zy
)é    )ÚTYPE_CHECKINGÚOptionalé   )ÚHfQuantizer)Úget_module_from_nameé   )ÚPreTrainedModel)ÚFPQuantConfig)Úis_fp_quant_availableÚis_qutlass_availableÚis_torch_availableÚis_torch_xpu_availableÚlogging)ÚQuantizationConfigMixinNc                   óš   ‡ — e Zd ZU dZdZdZded<   defˆ fd„Zd„ Z	dd	„Z
d
ddedefd„Z	 	 dd„Zedd
ed   fd„«       Zd„ Zd„ Zd„ Zˆ xZS )ÚFPQuantHfQuantizerzŒ
    Quantizer for the FP-Quant method. Enables the loading of prequantized models and in-flight quantization of full-precision models.
    FTr
   Úquantization_configc                 ó&   •— t        ‰| �  |fi |¤Ž y ©N)ÚsuperÚ__init__)Úselfr   ÚkwargsÚ	__class__s      €úu/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/quantizers/quantizer_fp_quant.pyr   zFPQuantHfQuantizer.__init__+   s   ø€ Ü‰ÑÐ,Ñ7°Ó7ó    c                 óF  — t         j                  j                  «       st        «       st	        d«      ‚t        «       s!| j                  j                  st        d«      ‚| j                  j                  rf| j                  j                  dk(  rMt         j                  j                  «       r/t         j                  j                  «       d   dk  rt        d«      ‚| j                  j                  rt        j                  d«       t        «       st        d«      ‚|€!| j                  j                  st        d	«      ‚t        |t         «      rT| j                  j                  s t#        |«      d
kD  rd|j%                  «       v sd|j%                  «       v rt        d«      ‚y y )Nz]FPQuant quantization is only supported on GPU or Intel XPU. Please use a different quantizer.a€  Using `fp_quant` with real quantization requires a **Blackwell GPU** and qutlass: `git clone https://github.com/IST-DASLab/qutlass.git && cd qutlass && pip install --no-build-isolation .`. You can use `FPQuantConfig(pseudoquantization=True, ...)` to use Triton-based pseudo-quantization. It doesn't provide any speedups but emulates the quantization behavior of the real quantization.Únvfp4r   é	   zäNVFP4 pseudoquantization requires a GPU with compute capability >= 9.0 (Hopper or newer) because the Triton kernel uses the `fp8e4nv` type. Please use `forward_dtype='mxfp4'` instead, or use a GPU with compute capability >= 9.0.zŠUsing pseudo-quantization for FP-Quant. This doesn't provide any speedups but emulates the quantization behavior of the real quantization.zGUsing `fp_quant` quantization requires fp_quant: `pip install fp_quant`zyYou are attempting to load a FPQuant model without setting device_map. Please set device_map comprised of 'cuda' devices.r   ÚcpuÚdiskz±You are attempting to load a FPQuant model with a device_map that contains a CPU or disk device. This is not supported. Please remove the CPU or disk device from the device_map.)ÚtorchÚcudaÚis_availabler   ÚNotImplementedErrorr   r   ÚpseudoquantizationÚImportErrorÚforward_dtypeÚget_device_capabilityÚ
ValueErrorÚloggerÚwarningr   Ú
isinstanceÚdictÚlenÚvalues)r   Ú
device_mapr   s      r   Úvalidate_environmentz'FPQuantHfQuantizer.validate_environment.   sv  € Ü�z‰z×&Ñ&Ô(Ô1GÔ1IÜ%Øoóð ô $Ô%¨d×.FÑ.F×.YÒ.YÜð Sóð ð
 ×$Ñ$×7Ò7Ø×(Ñ(×6Ñ6¸'ÒAÜ—
‘
×'Ñ'Ô)Ü—
‘
×0Ñ0Ó2°1Ñ5¸Ò9äð?óð ð ×#Ñ#×6Ò6Ü�N‰Nð ]ôô %Ô&ÜÐgÓhÐhàÐ d×&>Ñ&>×&QÒ&QÜðFóð ô ˜
¤DÔ)à×,Ñ,×?Ò?Ü˜
“O aÒ'Ø˜Z×.Ñ.Ó0Ñ0Ø˜Z×.Ñ.Ó0Ñ0ä ðhóð ð 1ð *r   Úreturnc                 ó~   — |t         j                  k7  r)t        j                  d|› d�«       t         j                  }|S )NzSetting dtype to zP, but only bfloat16 is supported right now. Overwriting torch_dtype to bfloat16.)r"   Úbfloat16r+   Úwarning_once)r   Údtypes     r   Úupdate_dtypezFPQuantHfQuantizer.update_dtype^   s9   € Ø”E—N‘NÒ"Ü×ÑØ# E 7Ð*zÐ{ôô —N‘NˆEØˆr   Úmodelr	   Ú
param_namec                 óP   — ddl m} t        ||«      \  }}t        ||«      r|dv ryy)Nr   )ÚFPQuantLinear)ÚweightÚqweightÚdqweightTF)Úfp_quantr<   r   r-   )r   r9   r:   r   r<   ÚmoduleÚtensor_names          r   Úparam_needs_quantizationz+FPQuantHfQuantizer.param_needs_quantizationf   s.   € Ý*ä2°5¸*ÓEÑˆ�Ü�f˜mÔ,°Ð@aÑ1aààr   c                 óP   — ddl m} ddlm}  || || j                  «      ¬«       y )Nr   )Úreplace_with_fp_quant_linearr   )Úadapt_fp_quant_config)Úfp_quant_linear_config)r@   rE   Úintegrations.fp_quantrF   r   )r   r9   r   rE   rF   s        r   Ú$_process_model_before_weight_loadingz7FPQuantHfQuantizer._process_model_before_weight_loadingp   s#   € õ
 	:åAá$ØÙ#8¸×9QÑ9QÓ#Rö	
r   c                 ó`   — | j                   j                  }|st        j                  d«       |S )Nz²You are attempting to train a model with FPQuant quantization. This is only supported when `store_master_weights=True`. Please set `store_master_weights=True` to train the model.)r   Ústore_master_weightsr+   r,   )r   r9   Ú	trainables      r   Úis_trainablezFPQuantHfQuantizer.is_trainable~   s0   € à×,Ñ,×AÑAˆ	ÙÜ�N‰Nð Eôð Ðr   c                  ó   — y)NT© )r   s    r   Úis_serializablez"FPQuantHfQuantizer.is_serializable‡   s   € Ør   c                 ó   — ddl m}  || «      S )Nr   )ÚFpQuantQuantize)rH   rR   )r   rR   s     r   Úget_quantize_opsz#FPQuantHfQuantizer.get_quantize_opsŠ   s   € Ý;á˜tÓ$Ð$r   c                 ó²   — ddl m} ddlm} | j                  r>| j
                  j                  r |dgd || «      g¬«      gS  |dgd || «      g¬«      gS g S )Nr   )ÚWeightConverter)ÚFpQuantDeserializez	.dqweight)Úsource_patternsÚtarget_patternsÚ
operationsz.qweight)Úcore_model_loadingrU   rH   rV   Úpre_quantizedr   r&   )r   rU   rV   s      r   Úget_weight_conversionsz)FPQuantHfQuantizer.get_weight_conversions�   ss   € Ý8Ý>à×ÒØ×'Ñ'×:Ò:á#Ø)4¨Ø(3Ù$6°tÓ$<Ð#=ôðð ñ $Ø)3¨Ø(2Ù$6°tÓ$<Ð#=ôðð ð ˆ	r   )r7   útorch.dtyper3   r]   )r9   r	   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrequires_calibrationÚis_qat_trainableÚ__annotations__r   r   r2   r8   ÚstrÚboolrC   rI   Úpropertyr   rM   rP   rS   r\   Ú__classcell__)r   s   @r   r   r   "   s�   ø… ñð !ÐØÐØ(Ó(ð8Ð,Cõ 8ò.ó`ðÐ.?ð ÈSð Ð_có ð
à ó
ð ñ (Ð+<Ñ"=ò ó ðòò%ö
r   r   )Útypingr   r   Úbaser   Úquantizers_utilsr   Úmodeling_utilsr	   Úutils.quantization_configr
   Úutilsr   r   r   r   r   r   r"   Ú
get_loggerr^   r+   r   rO   r   r   ú<module>rp      sO   ð÷ +å Ý 2ñ Ý0Ý9ç tÕ tÝ ?ñ ÔÛà	ˆ×	Ñ	˜HÓ	%€ôB˜õ Br   