CoolFace
Apppublic

Aluode/PerceptionLabPortable

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

20�Yiz�@s|ddlmZmZddlmZerddlmZddlmZm	Z	m3Z4mZddlm
Z
e5�r/ddlZe�e�ZGd	d6�d7e�ZdS)�)�
TYPE_CHECKING�Optional�)�HfQuantizer�)�PreTrainedModel)�is_accelerate_available�is_eetq_available�is_torch_available�logging)�get_module_from_nameNcs�eZdZdZdZdZddgZ�fdd�Zdd	�Zd$d
d�Z	ddde8defdd�Zddddde9ddfdd�Z
d%dd�Z	d&dddeee10fdd�Zd&d d!�Zedefd"d#��Z�ZS)'�EetqHfQuantizera11    8-bit quantization from EETQ quantization method:12        before loading: converts transformer layers into W8A16Linear during loading: load 16bit weight and pass to the13        layer object after: quantizes individual weights in Linear8bitLt into 8bit at first .cuda() call14    TF�eetqZ15acceleratecst�j|fi|��||_dS�N)�super�__init__�quantization_config)�selfr�kwargs��	__class__���E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\transformers/quantizers/quantizer_eetq.pyr-s16zEetqHfQuantizer.__init__c17Os�t�std��zddl}Wnty%}zdt|�vr td�|��d}~wwt�s-td��|�dd�s9|�dd�r=td	��tj�	�sFt18d19��|�d�}|durVt�d�dS|durot
|t�rqd
|��vskd|��vrstd��dSdSdS)Nz�Using `eetq` 8-bit quantization requires eetq.Please install the latest version of eetq from : https://github.com/NetEase-FuXi/EETQrZshard_checkpointz�You are using a version of EETQ that is incompatible with the current transformers version. Either downgrade transformers to <= v4.46.3 or, if available, upgrade EETQ to > v1.0.0.zNLoading an EETQ quantized model requires accelerate (`pip install accelerate`)Zfrom_tfFZ	from_flaxz�Converting into 8-bit weights from tf/flax weights is currently not supported, please make sure the weights are in PyTorch format.z/No GPU found. A GPU is needed for quantization.�20device_mapz�You have loaded an EETQ model on CPU and have a CUDA device available, make sure to set your model on a GPU device in order to run your model.�cpuZdiskz�You are attempting to load an EETQ model with a device_map that contains a CPU or disk device. This is not supported. Please remove the CPU or disk device from the device_map.)r	�ImportErrorr�strr�get�21ValueError�torch�cudaZis_available�RuntimeError�loggerZwarning_once�22isinstance�dict�values)r�argsrr�excrrrr�validate_environment1sH������2324�"��z$EetqHfQuantizer.validate_environment�dtype�torch.dtype�returncCs6|durtj}t�d|�|S|tjkrt�d�|S)Nz�Overriding dtype=%s with `dtype=torch.float16` due to requirements of `eetq` to enable model loading in 8-bit. Pass your own dtype to specify the dtype of the remaining non-linear layers or pass dtype=torch.float16 to remove this warning.zLWe suggest you to set `dtype=torch.float16` for better efficiency with EETQ.)rZfloat16r"�info)rr)rrr�update_dtype_s�	2526zEetqHfQuantizer.update_dtype�modelr�27param_namecKs>ddlm}t||�\}}t||�r|js|dkrdSdSdS)Nr)�28EetqLinear�biasFT)rr0rr#�
pre_quantized)rr.r/rr0�module�tensor_namerrr�param_needs_quantizationms29z(EetqHfQuantizer.param_needs_quantization�param_valueztorch.Tensor�
target_deviceztorch.devicecKs�ddlm}m}t||�\}}	||�\}30}t||�r8|js!|	dkr0|	dkr/|jtjkr/t	d��n|	dkr8t	d��|3132|�|j|	<|�d|�33|��dS)	Nr)r0�quantize_and_preprocess_weightsr1�weightz6Expect quantized weights but got an unquantized weight�weight_scalez;Expect unquantized weights but got a quantized weight_scaleZ
weight_scales)
rr0r8rr#r2r)rZint8r�to�_buffers�register)rr.r6r/r7rr0r8r3r4�	new_valuer:rrr�create_quantized_paramys34�z&EetqHfQuantizer.create_quantized_paramcKs|Srr)rr.rrrr�#_process_model_after_weight_loading��z3EetqHfQuantizer._process_model_after_weight_loadingN�keep_in_fp32_modulescKsDddlm}|�||jj|�|_|||j|j|jd�}|j|j_dS)Nr)�replace_with_eetq_linear)�modules_to_not_convertrr2)ZintegrationsrCZget_modules_to_not_convertrrDr2�config)rr.rBrrCrrr�$_process_model_before_weight_loading�s35��z4EetqHfQuantizer._process_model_before_weight_loadingcC�dS�NTr)rZsafe_serializationrrr�is_serializable�rAzEetqHfQuantizer.is_serializablecCrGrHr)rrrr�is_trainable�szEetqHfQuantizer.is_trainable)r)r*r+r*)r.rr)�__name__�36__module__�__qualname__�__doc__Z requires_parameters_quantizationZrequires_calibrationZrequired_packagesrr(r-r�boolr5r?r@r�listrFrI�propertyrJ�
__classcell__rrrrr
!s837.���3839��404142r
)�typingrr�baserZmodeling_utilsr�utilsrr	r43rZquantizers_utilsrrZ44get_loggerrKr"r
rrrr�<module>s
45
Aluode/PerceptionLabPortable · CoolFace