CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
quantizer_quanto.cpython-310.pyc41 linesDownload Raw Back to __pycache__
1o

20�Yi��@s�ddlZddlmZmZmZddlmZddlmZddl	m3Z4er(ddlmZdd	l
mZmZmZmZdd5lmZe�rAddlZe�e�ZGdd�de�ZdS)
�N)�
TYPE_CHECKING�Optional�Union)�version�)�HfQuantizer)�get_module_from_name�)�PreTrainedModel)�is_accelerate_available�is_optimum_quanto_available�is_torch_available�logging)�QuantoConfigcs$eZdZdZddgZdZdZdef�fdd�Zd	d6�Z	dd�Z7d
d�Zd3dd�Zde
edede
efdd�Zdddedefdd�Zdeeeeeffdeeeeefffdd�Zddd d!ded"d#fd$d%�Zd4d'd(�Z	)d5ddd*ee
efd+d,�Zd-d.�Zedefd/d0��Zd5d1d2�Z�ZS)6�QuantoHfQuantizerz*8    Quantizer for the quanto library9    Zquanto�10accelerateTF�quantization_configcs t�j|fi|��|��dS�N)�super�__init__�	post_init)�selfr�kwargs��	__class__���E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\transformers/quantizers/quantizer_quanto.pyr2szQuantoHfQuantizer.__init__cCs"|jjdur
|jstd��dSdS)z 11        Safety checker12        Nz�We don't support quantizing the activations with transformers library.Use quanto library for more complex use cases such as activations quantization, calibration and quantization aware training.)rZactivationsZ
pre_quantized�13ValueError�rrrrr6s14��zQuantoHfQuantizer.post_initcOs t�std��t�std��dS)NzhLoading an optimum-quanto quantized model requires optimum-quanto library (`pip install optimum-quanto`)z`Loading an optimum-quanto quantized model requires accelerate library (`pip install accelerate`))r�ImportErrorr)r�argsrrrr�validate_environment@s���z&QuantoHfQuantizer.validate_environmentcCs|dur
ddi}t�d�|S)N��cpuz�The device_map was not initialized. Setting device_map to {'':'cpu'}. If you want to use the model for inference, please set device_map ='auto')�logger�info)rZ15device_maprrr�update_device_mapJs�z#QuantoHfQuantizer.update_device_map�dtype�torch.dtype�returncCs|durt�d�tj}|S)NzPYou did not specify `dtype` in `from_pretrained`. Setting it to `torch.float32`.)r$r%�torchZfloat32)rr'rrr�update_dtypeTs16zQuantoHfQuantizer.update_dtype�missing_keys�prefixcs�t�r	ddlm}g�|��D]*\}}t||�r9|D]}||vs)||�d|��vr8|�d�s8|�d�s8��|�qq�fdd�|D�S)Nr��QModuleMixin�.z.weightz.biascsg|]}|�vr|�qSrr)�.0�k�Znot_missing_keysrr�17<listcomp>hsz9QuantoHfQuantizer.update_missing_keys.<locals>.<listcomp>)r�optimum.quantor/Z
named_modules�18isinstance�endswith�append)r�modelr,r-r/�name�module�missingrr3r�update_missing_keysZs19��20�z%QuantoHfQuantizer.update_missing_keysr9r2122param_namecKs>t�r	ddlm}t||�\}}t||�rd|vr|jSdS)Nrr.�weightF)rr5r/rr6�frozen)rr9r>rr/r;Ztensor_namerrr�param_needs_quantizationjsz*QuantoHfQuantizer.param_needs_quantization�23max_memorycCsdd�|��D�}|S)NcSsi|]	\}}||d�qS)g�������?r)r1�key�valrrr�24<dictcomp>wsz7QuantoHfQuantizer.adjust_max_memory.<locals>.<dictcomp>)�items)rrBrrr�adjust_max_memoryvsz#QuantoHfQuantizer.adjust_max_memory�param_valueztorch.Tensor�
target_deviceztorch.devicec	Ks@ddlm}||||�|��t||�\}}|��d|j_dS)Nr	)�_load_parameter_into_modelF)�modeling_utilsrJ�tor�freezer?Z
requires_grad)	rr9rHr>rIrrJr;�_rrr�create_quantized_paramzs25z(QuantoHfQuantizer.create_quantized_param�target_dtypecCsVt�tj�d��t�d�kr'ddlm}tj|j|j	|j26d�}||jj}|St
d��)Nrz0.27.0r)�CustomDtype)�int8Zfloat8Zint4Zint2z�You are using `device_map='auto'` on an optimum-quanto quantized model. To automatically compute the appropriate device map, you should upgrade your `accelerate` library,`pip install --upgrade accelerate` or install it from source.)r�parse�	importlib�metadataZaccelerate.utilsrQr*rRZFP8ZINT4ZINT2r�weightsr)rrPrQ�mappingrrr�adjust_target_dtype�s��z%QuantoHfQuantizer.adjust_target_dtypeN�keep_in_fp32_modulescKsDddlm}|�||jj|�|_|||j|jd�\}}|j|j_dS)Nr	)�replace_with_quanto_layers)�modules_to_not_convertr)ZintegrationsrZZget_modules_to_not_convertrr[�config)rr9rYrrZrNrrr�$_process_model_before_weight_loading�s27�2829�z6QuantoHfQuantizer._process_model_before_weight_loadingcKs|Srr)rr9rrrr�#_process_model_after_weight_loading��z5QuantoHfQuantizer._process_model_after_weight_loadingcC�dS)NTrrrrr�is_trainable�szQuantoHfQuantizer.is_trainablecCr`)NFr)rZsafe_serializationrrr�is_serializable�r_z!QuantoHfQuantizer.is_serializable)r'r(r)r()rPr(r)r(r)�__name__�30__module__�__qualname__�__doc__Zrequired_packagesZ requires_parameters_quantizationZrequires_calibrationrrrr!r&r+�list�strr=�boolrA�dictr�intrGrOrXrr]r^�propertyrarb�
__classcell__rrrrr)sB3132 33 342���3536��3738�r)rT�typingrrr�	packagingr�baserZquantizers_utilsrrKr39�utilsrrr
rZutils.quantization_configrr*Z40get_loggerrcr$rrrrr�<module>s
41
Aluode/PerceptionLabPortable · CoolFace