o
    i                     @   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 r+d 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                       sr   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efddZdddZ  ZS )AutoRoundQuantizerzW
    Quantizer of the AutoRound method. (https://huggingface.co/papers/2309.05516)
    T
auto_roundquantization_configc                    s   t  j|fi | d S N)super__init__)selfr   kwargs	__class__ i/home/ubuntu/veenaModal/venv/lib/python3.10/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_environmentdtypetorch.dtypereturnc                 C   s   |d u rt j}td |S )NzMLoading the model in `torch.bfloat16`. To overwrite it, set `dtype` manually.)torchbfloat16loggerinfo)r   r   r   r   r   update_dtype3   s   
zAutoRoundQuantizer.update_dtypemodelr   c                 K   sR   |j jdkrtd ddlm}m} | jr'|| j}|||\}}|| _	d S d S )N	input_idszRAutoRound offers only limited support for models that are not strictly text-based.r   )convert_hf_modelinfer_target_device)
r   main_input_namer!   warning"auto_round.inference.convert_modelr&   r'   pre_quantizedr   used_backends)r   r$   r   r&   r'   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_loadingc                 C      dS )NFr   )r   r   r   r   is_trainableK      zAutoRoundQuantizer.is_trainableNc                 C   r2   )NTr   )r   safe_serializationr   r   r   is_serializableO   r4   z"AutoRoundQuantizer.is_serializable)r   r   r   r   )r$   r   r   )__name__
__module____qualname____doc__requires_calibrationrequired_packagesr
   r   r   r#   r.   r1   propertyboolr3   r6   __classcell__r   r   r   r   r       s    



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