+
    QV-jµ  ã                   ó¶   € ^ RI Ht ^RIHt ]'       d   ^RIHt ^RIHt ^RIH	t	H
t
Ht ]
! 4       '       d   ^ RIt]P                  ! ]4      t ! R R]4      tR# )	é    )ÚTYPE_CHECKING)ÚHfQuantizer)ÚPreTrainedModel)ÚBitNetQuantConfig)Úis_accelerate_availableÚis_torch_availableÚloggingNc                   ó´   a a€ ] tR t^!t oRtRtV 3R ltR tV3R lR ltV3R lR lt	R	 t
]V3R
 lR l4       t]V3R lR l4       tR tV3R ltRtVtV ;t# )ÚBitNetHfQuantizerzã
1.58-bit quantization from BitNet quantization method:
Before loading: it converts the linear layers into BitLinear layers during loading.

Check out the paper introducing this method: https://huggingface.co/papers/2402.17764
Tc                ó*   <€ \         SV `  ! V3/ VB  R # )N)ÚsuperÚ__init__)ÚselfÚquantization_configÚkwargsÚ	__class__s   &&,€Úy/Volumes/fast/ai/experiments/ui-tars-smoke/.venv/lib/python3.14/site-packages/transformers/quantizers/quantizer_bitnet.pyr   ÚBitNetHfQuantizer.__init__,   s   ø€ Ü‰ÒÐ,Ñ7°Ô7ó    c                óÄ  € \        4       '       g   \        R 4      h\        P                  P	                  4       '       g   \
        P                  R4       R# VP                  R4      pVf   \
        P                  R4       R# \        V\        4      '       dH   \        V4      ^8”  d   RVP                  4       9   g   RVP                  4       9   d   \        R4      hR# R# )zOLoading a BitNet quantized model requires accelerate (`pip install accelerate`)zhYou don't have a GPU available to load the model, the inference will be slow because of weight unpackingNÚ
device_mapz�You have loaded a BitNet model on CPU and have a CUDA device available, make sure to set your model on a GPU device in order to run your model.ÚcpuÚdiskz¯You are attempting to load a BitNet 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.)r   ÚImportErrorÚtorchÚcudaÚis_availableÚloggerÚwarning_onceÚgetÚ
isinstanceÚdictÚlenÚvaluesÚ
ValueError)r   Úargsr   r   s   &*, r   Úvalidate_environmentÚ&BitNetHfQuantizer.validate_environment/   sÃ   € Ü&×(Ò(ÜÐoÓpÐpä�z‰z×&Ñ&×(Ò(Ü×ÑØzôñ à—Z‘Z Ó-ˆ
ØÒÜ×ÑðIöô ˜
¤D×)Ò)Ü�:‹ Ô" u°
×0AÑ0AÓ0CÔ'CÀvÐQ[×QbÑQbÓQdÔGdÜ ðgóð ñ Heñ *r   c                ó   <€ V ^8„  d   QhRR/# )é   Úmodelr   © )ÚformatÚ__classdict__s   "€r   Ú__annotate__ÚBitNetHfQuantizer.__annotate__F   s   ø€ ÷ 
ñ 
à ñ
r   c                óº   € ^RI Hp V P                  WP                  P                  VP
                  4      V n        V! VV P                  V P                  R7      pR# )r*   )Úreplace_with_bitnet_linear)Úmodules_to_not_convertr   N)Úintegrationsr2   Úget_modules_to_not_convertr   r3   Ú_keep_in_fp32_modules)r   r+   r   r2   s   &&, r   Ú$_process_model_before_weight_loadingÚ6BitNetHfQuantizer._process_model_before_weight_loadingF   sR   € õ
 	>à&*×&EÑ&EØ×+Ñ+×BÑBÀE×D_ÑD_ó'
ˆÔ#ñ +ØØ#'×#>Ñ#>Ø $× 8Ñ 8ô
Šr   c                ór   <€ V ^8„  d   QhRS[ S[S[S[,          3,          RS[ S[S[S[,          3,          /# )r*   Ú
max_memoryÚreturn)r"   ÚstrÚint)r-   r.   s   "€r   r/   r0   W   s6   ø€ ÷ ñ ©D±±c¹Cµi°Õ,@ð ÁTÉ#ÉsÑUXÍyÈ.ÕEYñ r   c                óf   € VP                  4        UUu/ uF  w  r#W#R ,          bK  	  pppV# u uppi )gÍÌÌÌÌÌì?)Úitems)r   r:   ÚkeyÚvals   &&  r   Úadjust_max_memoryÚ#BitNetHfQuantizer.adjust_max_memoryW   s5   € Ø6@×6FÑ6FÔ6HÔIÑ6H©(¨#�c �:’oÑ6Hˆ
ÑIØÐùó Js   ”-c                ó   € R # )Tr,   ©r   s   &r   Úis_serializableÚ!BitNetHfQuantizer.is_serializable[   s   € Ùr   c                ó    <€ V ^8„  d   QhRS[ /# ©r*   r;   ©Úbool)r-   r.   s   "€r   r/   r0   _   s   ø€ ÷ 
ñ 
™dñ 
r   c                óv   € V P                   P                  R 8H  ;'       d    V P                   P                  R8H  # )ÚautobitlinearÚonline©r   Úlinear_classÚquantization_moderE   s   &r   Úis_trainableÚBitNetHfQuantizer.is_trainable^   s=   € ð ×$Ñ$×1Ñ1°_ÑD÷ Gð GØ×(Ñ(×:Ñ:¸hÑFð	
r   c                ó    <€ V ^8„  d   QhRS[ /# rI   rJ   )r-   r.   s   "€r   r/   r0   f   s   ø€ ÷ 
ñ 
¡$ñ 
r   c                óv   € V P                   P                  R8H  ;'       d    V P                   P                  R8H  # )zUFlag indicating whether the quantized model can carry out quantization aware trainingrM   rN   rO   rE   s   &r   Úis_qat_trainableÚ"BitNetHfQuantizer.is_qat_trainablee   s=   € ð ×$Ñ$×1Ñ1°_ÑD÷ Gð GØ×(Ñ(×:Ñ:¸hÑFð	
r   c                ó´   € ^RI Hp ^RIHp V P                  P
                  R8X  d1   V P                  P                  R8X  d   V! R.R.V! V 4      .R7      .# . # )r*   )ÚWeightConverter)ÚBitNetDeserializerM   ÚofflineÚweight)Úsource_patternsÚtarget_patternsÚ
operations)Úcore_model_loadingrY   Úintegrations.bitnetrZ   r   rP   rQ   )r   rY   rZ   s   &  r   Úget_weight_conversionsÚ(BitNetHfQuantizer.get_weight_conversionsm   sb   € Ý8Ý;ð ×$Ñ$×1Ñ1°_ÔDØ×(Ñ(×:Ñ:¸iÔGñ  Ø%- JØ%- JÙ 1°$Ó 7Ð8ôðð ð ˆ	r   c                ó$   <€ V ^8„  d   Qh/ R;R&   # )r*   r   r   r,   )r-   r.   s   "€r   r/   r0   !   s   ø‡ ‚ ð -Ñ,ò r   )r3   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úrequires_calibrationr   r'   r7   rB   rF   ÚpropertyrR   rV   rb   Ú__annotate_func__Ú__static_attributes__Ú__classdictcell__Ú__classcell__)r   r.   s   @@r   r   r   !   sj   ù‡ € ñð  Ðõ8ò÷.
ð 
÷"ð òð ÷
ó ð
ð ÷
ó ð
ò÷Y … r   r   )Útypingr   Úbaser   Úmodeling_utilsr   Úutils.quantization_configr   Úutilsr   r   r	   r   Ú
get_loggerre   r   r   r,   r   r   Ú<module>rv      sL   ðõ !å ÷ Ý0Ý=ç HÑ Hñ ×ÒÛð 
×	Ò	˜HÓ	%€ô[˜ö [r   