Ë
    Hêñiµ  ã                   óœ   — 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  e
«       rd dlZ ej                  e«      Z G d	„ d
e«      Zy)é    )ÚTYPE_CHECKINGé   )ÚHfQuantizeré   )ÚPreTrainedModel)ÚBitNetQuantConfig)Úis_accelerate_availableÚis_torch_availableÚloggingNc                   ó¬   ‡ — e Zd ZU dZdZded<   ˆ fd„Zd„ Z	 	 dd„Zde	e
ee
z  f   d	e	e
ee
z  f   fd
„Zd„ Zed	efd„«       Zed	efd„«       Zd„ Zˆ xZS )Ú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
    Tr   Úquantization_configc                 ó&   •— t        ‰| �  |fi |¤Ž y )N)ÚsuperÚ__init__)Úselfr   ÚkwargsÚ	__class__s      €új/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/transformers/quantizers/quantizer_bitnet.pyr   zBitNetHfQuantizer.__init__,   s   ø€ Ü‰ÑÐ,Ñ7°Ó7ó    c                 ó„  — t        «       st        d«      ‚t        j                  j	                  «       st
        j                  d«       y |j                  d«      }|€t
        j                  d«       y t        |t        «      r>t        |«      dkD  rd|j                  «       v sd|j                  «       v rt        d«      ‚y y )	NzOLoading 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 unpackingÚ
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.r   Ú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_environmentz&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                 óº   — ddl m} | j                  || j                  j                  |j
                  «      | _         ||| j                  | j                  ¬«      }y )Nr   )Úreplace_with_bitnet_linear)Úmodules_to_not_convertr   )Úintegrationsr*   Úget_modules_to_not_convertr   r+   Ú_keep_in_fp32_modules)r   Úmodelr   r*   s       r   Ú$_process_model_before_weight_loadingz6BitNetHfQuantizer._process_model_before_weight_loadingF   sT   € õ
 	>à&*×&EÑ&EØ�4×+Ñ+×BÑBÀE×D_ÑD_ó'
ˆÔ#ñ +ØØ#'×#>Ñ#>Ø $× 8Ñ 8ô
‰r   Ú
max_memoryÚreturnc                 ó^   — |j                  «       D ��ci c]  \  }}||dz  “Œ }}}|S c c}}w )NgÍÌÌÌÌÌì?)Úitems)r   r1   ÚkeyÚvals       r   Úadjust_max_memoryz#BitNetHfQuantizer.adjust_max_memoryW   s6   € Ø6@×6FÑ6FÓ6H×I©(¨#¨s�c˜3 ™:‘oÐIˆ
ÑIØÐùó Js   ”)c                  ó   — y)NT© ©r   s    r   Úis_serializablez!BitNetHfQuantizer.is_serializable[   s   € Ør   c                 ój   — | j                   j                  dk(  xr | j                   j                  dk(  S )NÚautobitlinearÚonline©r   Úlinear_classÚquantization_moder:   s    r   Úis_trainablezBitNetHfQuantizer.is_trainable^   s7   € ð ×$Ñ$×1Ñ1°_ÑDò GØ×(Ñ(×:Ñ:¸hÑFð	
r   c                 ój   — | j                   j                  dk(  xr | j                   j                  dk(  S )zUFlag indicating whether the quantized model can carry out quantization aware trainingr=   r>   r?   r:   s    r   Úis_qat_trainablez"BitNetHfQuantizer.is_qat_trainablee   s7   € ð ×$Ñ$×1Ñ1°_ÑDò GØ×(Ñ(×:Ñ:¸hÑFð	
r   c                 ó¬   — ddl m} ddlm} | j                  j
                  dk(  r.| j                  j                  dk(  r |dgdg || «      g¬«      gS g S )Nr   )ÚWeightConverter)ÚBitNetDeserializer=   ÚofflineÚweight)Úsource_patternsÚtarget_patternsÚ
operations)Úcore_model_loadingrF   Úintegrations.bitnetrG   r   r@   rA   )r   rF   rG   s      r   Úget_weight_conversionsz(BitNetHfQuantizer.get_weight_conversionsm   sb   € Ý8Ý;ð ×$Ñ$×1Ñ1°_ÒDØ×(Ñ(×:Ñ:¸iÒGñ  Ø%- JØ%- JÙ 1°$Ó 7Ð8ôðð ð ˆ	r   )r/   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrequires_calibrationÚ__annotations__r   r(   r0   r#   ÚstrÚintr7   r;   ÚpropertyÚboolrB   rD   rO   Ú__classcell__)r   s   @r   r   r   !   s›   ø… ñð  ÐØ,Ó,ô8òð.
à ó
ð"¨D°°c¸C±i°Ñ,@ð ÀTÈ#ÈsÐUXÉyÈ.ÑEYó òð ð
˜dò 
ó ð
ð ð
 $ò 
ó ð
ör   r   )Útypingr   Úbaser   Úmodeling_utilsr   Úutils.quantization_configr   Úutilsr	   r
   r   r   Ú
get_loggerrP   r   r   r9   r   r   ú<module>ra      sK   ðõ !å ñ Ý0Ý=ç HÑ Hñ ÔÛð 
ˆ×	Ñ	˜HÓ	%€ô[˜õ [r   