§
    Þ˜ºh=  ã                   ó¢   — d dl mZmZmZ ddlmZ er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dS )
é    )ÚTYPE_CHECKINGÚOptionalÚUnioné   )ÚHfQuantizeré   )ÚPreTrainedModel)Úis_accelerate_availableÚis_torch_availableÚloggingNc                   ó  ‡ — e Zd ZdZdZdZdgZˆ fd„Zd„ Zdd	„Z		 dddde
ee                  fd„Zdeeeeef         f         deeeeef         f         fd„Zdd„Zdd„Zedefd„¦   «         Zedef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
    FTÚ
acceleratec                 óJ   •—  t          ¦   «         j        |fi |¤Ž || _        d S ©N)ÚsuperÚ__init__Úquantization_config)Úselfr   ÚkwargsÚ	__class__s      €úp/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/transformers/quantizers/quantizer_bitnet.pyr   zBitNetHfQuantizer.__init__-   s1   ø€ Ø�‰ŒÔÐ,Ð7Ð7°Ð7Ð7Ð7Ø#6ˆÔ Ð Ð ó    c                 ó<  — t          ¦   «         st          d¦  «        ‚|                     dd¦  «        s|                     dd¦  «        rt          d¦  «        ‚t          j                             ¦   «         st                               d¦  «         d S |                     d¦  «        }|€t                               d¦  «         d S |�Pt          |t          ¦  «        r=d	|                     ¦   «         v sd
|                     ¦   «         v rt          d¦  «        ‚d S d S d S )NzOLoading a BitNet quantized model requires accelerate (`pip install accelerate`)Úfrom_tfFÚ	from_flaxztLoading ternary weights from tf/flax is currently not supported, please make sure the weights are in PyTorch format.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.Ú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ÚgetÚ
ValueErrorÚtorchÚcudaÚis_availableÚloggerÚwarning_onceÚ
isinstanceÚdictÚvalues)r   Úargsr   r   s       r   Úvalidate_environmentz&BitNetHfQuantizer.validate_environment1   sS  € Ý&Ñ(Ô(ð 	qÝÐoÑpÔpÐpà�:Š:�i Ñ'Ô'ð 	¨6¯:ª:°kÀ5Ñ+IÔ+Ið 	Ýð;ñô ð õ
 Œz×&Ò&Ñ(Ô(ð 	Ý×ÒØzñô ð ð ˆFà—Z’Z Ñ-Ô-ˆ
ØÐÝ×ÒðIñô ð ð ð ð Ð#Ý˜*¥dÑ+Ô+ð °¸*×:KÒ:KÑ:MÔ:MÐ1MÐ1MÐQWÐ[e×[lÒ[lÑ[nÔ[nÐQnÐQnÝ ðgñô ð ð $Ð#ðð ÐQnÐQnr   Úmodelr	   c                 ó   — |S r   © )r   r-   r   s      r   Ú#_process_model_after_weight_loadingz5BitNetHfQuantizer._process_model_after_weight_loadingN   s   € Øˆr   NÚkeep_in_fp32_modulesc                 óš   — ddl m} |                      || j        j        |¦  «        | _         ||| j        | j        | j        ¬¦  «        }d S )Nr   )Úreplace_with_bitnet_linear)Úmodules_to_not_convertr   Úpre_quantized)Úintegrationsr3   Úget_modules_to_not_convertr   r4   r5   )r   r-   r1   r   r3   s        r   Ú$_process_model_before_weight_loadingz6BitNetHfQuantizer._process_model_before_weight_loadingQ   sn   € ð 	>Ð=Ð=Ð=Ð=Ð=à&*×&EÒ&EØ�4Ô+ÔBÐDXñ'
ô '
ˆÔ#ð +Ð*ØØ#'Ô#>Ø $Ô 8ØÔ,ð	
ñ 
ô 
ˆˆˆr   Ú
max_memoryÚreturnc                 óB   — d„ |                      ¦   «         D ¦   «         }|S )Nc                 ó    — i | ]\  }}||d z  “ŒS )gÍÌÌÌÌÌì?r/   )Ú.0ÚkeyÚvals      r   ú
<dictcomp>z7BitNetHfQuantizer.adjust_max_memory.<locals>.<dictcomp>e   s"   € ÐIÐIÐI©(¨#¨s�c˜3 ™:ÐIÐIÐIr   )Úitems)r   r9   s     r   Úadjust_max_memoryz#BitNetHfQuantizer.adjust_max_memoryd   s'   € ØIÐI°j×6FÒ6FÑ6HÔ6HÐIÑIÔIˆ
ØÐr   Útarget_dtypeútorch.dtypec                 ó   — t           j        }|S r   )r#   Úint8)r   rC   s     r   Úadjust_target_dtypez%BitNetHfQuantizer.adjust_target_dtypeh   s   € Ý”zˆØÐr   c                 ó   — dS )NTr/   )r   Úsafe_serializations     r   Úis_serializablez!BitNetHfQuantizer.is_serializablel   s   € Øˆtr   c                 óB   — | j         j        dk    o| j         j        dk    S )NÚautobitlinearÚonline©r   Úlinear_classÚquantization_mode©r   s    r   Úis_trainablezBitNetHfQuantizer.is_trainableo   s+   € ð Ô$Ô1°_ÒDð GØÔ(Ô:¸hÒFð	
r   c                 óB   — | j         j        dk    o| j         j        dk    S )zUFlag indicating whether the quantized model can carry out quantization aware trainingrL   rM   rN   rQ   s    r   Úis_qat_trainablez"BitNetHfQuantizer.is_qat_trainablev   s+   € ð Ô$Ô1°_ÒDð GØÔ(Ô:¸hÒFð	
r   )r-   r	   r   )rC   rD   r:   rD   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú requires_parameters_quantizationÚrequires_calibrationÚrequired_packagesr   r,   r0   r   ÚlistÚstrr8   r)   r   ÚintrB   rG   rJ   ÚpropertyÚboolrR   rT   Ú__classcell__)r   s   @r   r   r       sv  ø€ € € € € ðð ð (-Ð$ØÐà%˜Ðð7ð 7ð 7ð 7ð 7ðð ð ð:ð ð ð ð 59ð
ð 
à ð
ð ' t¨C¤yÔ1ð
ð 
ð 
ð 
ð&¨D°°e¸CÀ¸H´oÐ1EÔ,Fð È4ÐPSÐUZÐ[^Ð`cÐ[cÔUdÐPdÔKeð ð ð ð ðð ð ð ðð ð ð ð ð
˜dð 
ð 
ð 
ñ „Xð
ð ð
 $ð 
ð 
ð 
ñ „Xð
ð 
ð 
ð 
ð 
r   r   )Útypingr   r   r   Úbaser   Úmodeling_utilsr	   Úutilsr
   r   r   r#   Ú
get_loggerrU   r&   r   r/   r   r   ú<module>rg      sß   ðð 2Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1à Ð Ð Ð Ð Ð ð ð 1Ø0Ð0Ð0Ð0Ð0Ð0à HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ Hð ÐÑÔð Ø€L€L€Lð 
ˆÔ	˜HÑ	%Ô	%€ð\
ð \
ð \
ð \
ð \
˜ñ \
ô \
ð \
ð \
ð \
r   