o
    wiS                     @   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 )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/sommelier/.venv/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.)torch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 )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+   r0   r-   
ValueError)r   r%   r   r0   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   r3   )NTr   )r   safe_serializationr   r   r   is_serializableO   r5   z"AutoRoundQuantizer.is_serializable)r   r   r   r   )r%   r   r   )__name__
__module____qualname____doc__requires_calibrationrequired_packagesr   r   r   r$   r/   r2   propertyr   r4   r7   __classcell__r   r   r   r   r       s    



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