U
    Ôd¨i1  ã                   @   s”   d dl Z d dlmZmZ 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mZ dd	lmZmZ eƒ rvd dlZe e¡ZG d
d„ deƒZdS )é    N)ÚTYPE_CHECKINGÚOptional)Úversioné   )ÚHfQuantizeré   )ÚPreTrainedModel)Úis_auto_gptq_availableÚis_optimum_availableÚis_torch_availableÚlogging)Ú
GPTQConfigÚQuantizationConfigMixinc                       sŒ   e Zd ZdZdZddgZd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ded dœdd„ƒZddd„Z‡  ZS )ÚGptqHfQuantizerzÖ
    Quantizer of the GPTQ method - for GPTQ the quantizer support calibration of the model through
    `auto_gptq` package. Quantization is done under the hood for users if they load a non-prequantized model.
    FZoptimumÚ	auto_gptqN)Úquantization_configc                    s2   t ƒ j|f|Ž ddlm} | | j ¡ ¡| _d S )Nr   )ÚGPTQQuantizer)ÚsuperÚ__init__Zoptimum.gptqr   Ú	from_dictr   Zto_dict_optimumÚoptimum_quantizer)Úselfr   Úkwargsr   ©Ú	__class__© úJ/tmp/pip-unpacked-wheel-bm_b0l5e/transformers/quantizers/quantizer_gptq.pyr   -   s    zGptqHfQuantizer.__init__c                 O   sr   t  tj  d¡¡t  d¡k}|s4tj ¡ s4tdƒ‚n:tƒ r@t	ƒ sJt
dƒ‚n$t  tj  d¡¡t  d¡k rnt
dƒ‚d S )Nz	auto-gptqz0.4.2z2GPU is required to quantize or run quantize model.zwLoading a GPTQ quantized model requires optimum (`pip install optimum`) and auto-gptq library (`pip install auto-gptq`)r   zWYou need a version of auto_gptq >= 0.4.2 to use GPTQ: `pip install --upgrade auto-gptq`)r   ÚparseÚ	importlibÚmetadataÚtorchZcudaZis_availableÚRuntimeErrorr
   r	   ÚImportError)r   Úargsr   Zgptq_supports_cpur   r   r   Úvalidate_environment3   s    
ÿÿz$GptqHfQuantizer.validate_environmentztorch.dtype)Útorch_dtypeÚreturnc                 C   s(   |d krt j}n|t jkr$t d¡ |S )NzRWe suggest you to set `torch_dtype=torch.float16` for better efficiency with GPTQ.)r    Zfloat16ÚloggerÚinfo)r   r%   r   r   r   Úupdate_torch_dtype@   s
    

z"GptqHfQuantizer.update_torch_dtyper   )Úmodelc                 K   s*   |j jdkrtdƒ‚| jr&| j |¡}d S )NZ	input_idsz%We can only quantize pure text model.)r   Zmain_input_namer!   Úpre_quantizedr   Zconvert_model©r   r*   r   r   r   r   Ú$_process_model_before_weight_loadingG   s    z4GptqHfQuantizer._process_model_before_weight_loadingc                 K   sT   | j r| j |¡}n<| jjd kr*|j| j_| j || jj¡ t | j 	¡ ¡|j
_d S )N)r+   r   Zpost_init_modelr   Ú	tokenizerZname_or_pathZquantize_modelr   r   Zto_dictÚconfigr,   r   r   r   Ú#_process_model_after_weight_loadingN   s    
z3GptqHfQuantizer._process_model_after_weight_loadingc                 C   s   dS ©NTr   )r   r*   r   r   r   Úis_trainableX   s    zGptqHfQuantizer.is_trainablec                 C   s   dS r1   r   )r   Zsafe_serializationr   r   r   Úis_serializable\   s    zGptqHfQuantizer.is_serializable)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zrequires_calibrationZrequired_packagesr   r   r   r$   r)   r-   r0   Úpropertyr   r2   r3   Ú__classcell__r   r   r   r   r   #   s   
r   )r   Útypingr   r   Ú	packagingr   Úbaser   Zmodeling_utilsr   Úutilsr	   r
   r   r   Zutils.quantization_configr   r   r    Z
get_loggerr4   r'   r   r   r   r   r   Ú<module>   s   
