o
    ZhK                     @   sx   d dl 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
 ddlmZ e	 r-d dlZe
eZG d	d
 d
eZdS )    )TYPE_CHECKINGOptional   )HfQuantizer   )PreTrainedModel)is_auto_round_availableis_torch_availablelogging)QuantizationConfigMixinNc                       sx   e Zd ZdZdZdgZdef fddZdd ZdddZ	dddZ
dddZedded fddZdddZ  ZS )AutoRoundQuantizerzO
    Quantizer of the AutoRound method. (https://arxiv.org/pdf/2309.05516)
    TZ
auto_roundquantization_configc                    s   t  j|fi | d S N)super__init__)selfr   kwargs	__class__ [/var/www/auris/lib/python3.10/site-packages/transformers/quantizers/quantizer_auto_round.pyr   )   s   zAutoRoundQuantizer.__init__c                 O   s    | d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_environmenttorch_dtypetorch.dtypereturnc                 C   s   |d u rt j}td |S )NzSLoading the model in `torch.bfloat16`. To overwrite it, set `torch_dtype` manually.)torchZbfloat16loggerinfo)r   r   r   r   r   update_torch_dtype3   s   
z%AutoRoundQuantizer.update_torch_dtypemodelr   c                 K   sR   |j jdkrtd ddlm}m} | jr'|| j}|||\}}|| _	d S 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 d S td)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_loadingNc                 C      dS )NFr   )r   r#   r   r   r   is_trainableK      zAutoRoundQuantizer.is_trainablec                 C   r.   )NTr   )r   Zsafe_serializationr   r   r   is_serializableO   r0   z"AutoRoundQuantizer.is_serializable)r   r   r   r   )r#   r   r   )__name__
__module____qualname____doc__Zrequires_calibrationZrequired_packagesr   r   r   r"   r*   r-   propertyr   r/   r1   __classcell__r   r   r   r   r       s    



r   )typingr   r   baser   Zmodeling_utilsr   utilsr   r	   r
   Zutils.quantization_configr   r   Z
get_loggerr2   r    r   r   r   r   r   <module>   s   
