ó
    qyüi=  ã                   óÜ   • S SK Jr  SSKJr  SSKJr  SSKJr  \(       a  SSKJ	r	  SSK
Jr  SS	KJrJrJrJrJr  SS
K
Jr  \" 5       (       a  S SKr\R(                  " \5      r " S S\5      rg)é    )ÚTYPE_CHECKINGé   )Útqdmé   )ÚHfQuantizer)Úget_module_from_name)ÚPreTrainedModel)ÚHiggsConfig)Úis_accelerate_availableÚis_flute_availableÚis_hadamard_availableÚis_torch_availableÚlogging)ÚQuantizationConfigMixinNc                   ó¦   ^ • \ rS rSr% SrSrS\S'   S\4U 4S jjrS r	SS	 jr
  SS jrSS jr\S\4S j5       rS rS
SS\S\4S jrS rSrU =r$ )ÚHiggsHfQuantizeré#   z€
Quantizer of the HIGGS method. Enables the loading of prequantized models and in-flight quantization of full-precision models.
Fr
   Úquantization_configc                 ó(   >• [         TU ]  " U40 UD6  g )N)ÚsuperÚ__init__)Úselfr   ÚkwargsÚ	__class__s      €Úd/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/quantizers/quantizer_higgs.pyr   ÚHiggsHfQuantizer.__init__+   s   ø€ Ü‰ÒÐ,Ñ7°Ó7ó    c                 óª  • [         R                  R                  5       (       d  [        S5      e[	        5       (       d  [        S5      e[        5       (       d  [        S5      e[        5       (       d  [        S5      eUc  [        S5      e[        U[        5      (       a4  SUR                  5       ;   d  SUR                  5       ;   a  [        S5      eg g )	NzNHIGGS quantization is only supported on GPU. Please use a different quantizer.zHUsing `higgs` quantization requires Accelerate: `pip install accelerate`zLUsing `higgs` quantization requires FLUTE: `pip install flute-kernel>=0.3.0`zbUsing `higgs` quantization requires fast_hadamard_transform: `pip install fast_hadamard_transform`zwYou are attempting to load a HIGGS model without setting device_map. Please set device_map comprised of 'cuda' devices.ÚcpuÚdiskz¯You are attempting to load a HIGGS 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_availableÚNotImplementedErrorr   ÚImportErrorr   r   Ú
ValueErrorÚ
isinstanceÚdictÚvalues)r   Ú
device_mapr   s      r   Úvalidate_environmentÚ%HiggsHfQuantizer.validate_environment.   sÍ   € Ü�z‰z×&Ñ&×(Ñ(Ü%Ð&vÓwÐwä&×(Ñ(ÜÐhÓiÐiä!×#Ñ#ÜÐlÓmÐmä$×&Ñ&ÜØtóð ð ÑÜðFóð ô ˜
¤D×)Ñ)Ø˜
×)Ñ)Ó+Ó+¨v¸×9JÑ9JÓ9LÓ/LÜ ðhóð ð 0Mð *r   Úreturnc                 ót   • U[         R                  :w  a#  U[         R                  :w  a  [        SU S35      eU$ )NzInvalid `dtype` zS. HIGGS quantization only supports `dtype=torch.float16` or `dtype=torch.bfloat16`.)r!   Úfloat16Úbfloat16r&   )r   Údtypes     r   Úupdate_dtypeÚHiggsHfQuantizer.update_dtypeI   s:   € Ø”E—M‘MÓ! e¬u¯~©~Ó&=ÜØ" 5 'Ð)|Ð}óð ð ˆr   Úmodelr	   c                 ó²   • SSK Jn  U R                  XR                  R                  UR
                  5      U l        U" UU R                  U R                  S9  g )Nr   )Úreplace_with_higgs_linear)r   Úmodules_to_not_convert)Úintegrationsr6   Úget_modules_to_not_convertr   r7   Ú_keep_in_fp32_modules)r   r4   r   r6   s       r   Ú$_process_model_before_weight_loadingÚ5HiggsHfQuantizer._process_model_before_weight_loadings   sP   € õ
 	=à&*×&EÑ&EØ×+Ñ+×BÑBÀE×D_ÑD_ó'
ˆÔ#ñ 	"ØØ $× 8Ñ 8Ø#'×#>Ñ#>ó	
r   c                 ó  • SSK JnJn  SSKJn  SSKJn  0 nUR                  5        VV	s0 s H  u  p‰[        X–5      (       d  M  X‰_M     n
nn	[        U
R                  5       SSS9 GH  u  p‰U	R                  R                  U;  a0  U" U	R                  R                  S	9XyR                  R                  '   XyR                  R                     U	l        UR                  U R                  R                   U   5      U	l        U" U	R                  R"                  U	R$                  R"                  U	R                   S
9u  U	R                  l        U	l        U	R                   R'                  5       U R                  R                   U'   GM     g s  sn	nf )Nr   )ÚTuneMetaDataÚmaybe_tune_and_repack)Úmake_workspace_streamkr   ©ÚHiggsLinearzRepacking HIGGS modulesF)ÚdescÚleave)Údevice)ÚweightÚscalesÚmetadata)Ú
flute.tuner>   r?   Úflute.utilsr@   r8   rB   Únamed_modulesr'   r   ÚitemsrF   rE   Ú	workspaceÚ	from_dictr   Útune_metadataÚdatarG   Úto_dict)r   r4   r   r>   r?   r@   rB   Úflute_workspacesÚnameÚmoduleÚflute_moduless              r   Ú#_process_model_after_weight_loadingÚ4HiggsHfQuantizer._process_model_after_weight_loading„   s@  € ßBÝ6å.àÐØ:?×:MÑ:MÔ:OÔsÒ:O©,¨$ÔS]Ð^d×Sr›˜šÑ:OˆÑsÜ  ×!4Ñ!4Ó!6Ð=VÐ^cÕd‰LˆDð �}‰}×#Ñ#Ð+;Ó;Ù9OÐW]×WdÑWd×WkÑWkÑ9lÐ §¡×!5Ñ!5Ñ6Ø/·±×0DÑ0DÑEˆFÔð $0×#9Ñ#9¸$×:RÑ:R×:`Ñ:`ÐaeÑ:fÓ#gˆFÔ Ù7LØ—}‘}×)Ñ)Ø—}‘}×)Ñ)Ø×-Ñ-ñ8Ñ4ˆF�M‰MÔ Ô 4ð
 <B×;OÑ;O×;WÑ;WÓ;YˆD×$Ñ$×2Ñ2°4Ô8ò eùó ts   ªFÁFc                 ó   • g)NF© ©r   s    r   Úis_trainableÚHiggsHfQuantizer.is_trainable�   s   € àr   c                 ó   • g)NTrY   rZ   s    r   Úis_serializableÚ HiggsHfQuantizer.is_serializable¡   s   € Ør   Ú
param_namec                 óX   • SSK Jn  [        X5      u  pV[        XT5      (       a  US:X  a  gg)Nr   rA   rF   TF)r8   rB   r   r'   )r   r4   r`   r   rB   rT   Útensor_names          r   Úparam_needs_quantizationÚ)HiggsHfQuantizer.param_needs_quantization¤   s*   € Ý.ä2°5ÓEÑˆÜ�f×*Ñ*¨{¸hÓ/Fààr   c                 ó"   • SSK Jn  U" U5      nU$ )Nr   )Údequantize_higgs)r8   rf   )r   r4   rf   s      r   Ú_dequantizeÚHiggsHfQuantizer._dequantize®   s   € Ý3á  Ó'ˆØˆr   )r7   )r1   útorch.dtyper-   ri   )r4   r	   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úrequires_calibrationÚ__annotations__r   r   r+   r2   r;   rV   ÚpropertyÚboolr[   r^   Ústrrc   rg   Ú__static_attributes__Ú__classcell__)r   s   @r   r   r   #   s‡   ø‡ ñð !ÐØ&Ó&ð8Ð,C÷ 8òô6ðT
à ô
ô"Zð2 ð˜dó ó ðòðÐ.?ð ÈSð Ð_cô ÷ð r   r   )Útypingr   Úutils.loggingr   Úbaser   Úquantizers_utilsr   Úmodeling_utilsr	   Úutils.quantization_configr
   Úutilsr   r   r   r   r   r   r!   Ú
get_loggerrj   Úloggerr   rY   r   r   Ú<module>r      sS   ðõ !å  Ý Ý 2ö Ý0Ý7ç sÕ sÝ ?ñ ×ÑÛà	×	Ò	˜HÓ	%€ôO�{õ Or   