a
    ¾Àhü  ã                   @   st   d dl mZ ddlmZ er(ddlmZ ddlmZmZm	Z	 ddl
mZ eƒ rVd dlZe	 e¡ZG d	d
„ d
eƒZdS )é    )ÚTYPE_CHECKINGé   )ÚHfQuantizeré   )ÚPreTrainedModel)Úis_auto_round_availableÚis_torch_availableÚlogging)ÚQuantizationConfigMixinNc                       s€   e Zd ZdZdZdgZedœ‡ fdd„Zdd„ Zd	d	d
œdd„Z	ddœdd„Z
ddœdd„Zeedœdd„ƒZddd„Z‡  ZS )ÚAutoRoundQuantizerzW
    Quantizer of the AutoRound method. (https://huggingface.co/papers/2309.05516)
    TZ
auto_round)Úquantization_configc                    s   t ƒ j|fi |¤Ž d S )N)ÚsuperÚ__init__)Úselfr   Úkwargs©Ú	__class__© úh/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/quantizers/quantizer_auto_round.pyr   )   s    zAutoRoundQuantizer.__init__c                 O   s   |  d¡| _tƒ stdƒ‚d S )NÚ
device_mapzbLoading an AutoRound quantized model requires auto-round library (`pip install 'auto-round>=0.5'`))Úgetr   r   ÚImportError)r   Úargsr   r   r   r   Úvalidate_environment,   s
    ÿz'AutoRoundQuantizer.validate_environmentztorch.dtype)ÚdtypeÚreturnc                 C   s   |d u rt j}t d¡ |S )NzMLoading the model in `torch.bfloat16`. To overwrite it, set `dtype` manually.)ÚtorchZbfloat16ÚloggerÚinfo)r   r   r   r   r   Úupdate_dtype3   s    
zAutoRoundQuantizer.update_dtyper   )Úmodelc                 K   sN   |j jdkrt d¡ ddlm}m} | jrJ|| jƒ}|||ƒ\}}|| _	d S )NZ	input_idszRAutoRound offers only limited support for models that are not strictly text-based.r   )Úconvert_hf_modelÚinfer_target_device)
r   Zmain_input_namer   ÚwarningÚ"auto_round.inference.convert_modelr!   r"   Úpre_quantizedr   Úused_backends)r   r    r   r!   r"   Ztarget_devicer&   r   r   r   Ú$_process_model_before_weight_loading9   s    

z7AutoRoundQuantizer._process_model_before_weight_loadingc                 K   s,   | j r ddlm} ||| jƒ ntdƒ‚d S )Nr   )Ú	post_initz+AutoRound only sports pre-quantized models.)r%   r$   r(   r&   Ú
ValueError)r   r    r   r(   r   r   r   Ú#_process_model_after_weight_loadingC   s    z6AutoRoundQuantizer._process_model_after_weight_loading)r   c                 C   s   dS )NFr   )r   r   r   r   Úis_trainableK   s    zAutoRoundQuantizer.is_trainableNc                 C   s   dS )NTr   )r   Zsafe_serializationr   r   r   Úis_serializableO   s    z"AutoRoundQuantizer.is_serializable)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zrequires_calibrationZrequired_packagesr
   r   r   r   r'   r*   ÚpropertyÚboolr+   r,   Ú__classcell__r   r   r   r   r       s   
r   )Útypingr   Úbaser   Zmodeling_utilsr   Úutilsr   r   r	   Zutils.quantization_configr
   r   Z
get_loggerr-   r   r   r   r   r   r   Ú<module>   s   
