CoolFace
Apppublic

Aluode/PerceptionLabPortable

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

20�Yi=�@spddlmZmZmZddlmZerddlmZddlm	Z	m3Z4mZe5�r)ddlZe�
e�ZGdd	�d	e�ZdS)6�)�
TYPE_CHECKING�Optional�Union�)�HfQuantizer�)�PreTrainedModel)�is_accelerate_available�is_torch_available�loggingNcs�eZdZdZdZdZdgZ�fdd�Zdd�Zddd�Z		
d d	d7de8eefdd�Z
deeeeeffdeeeeefffdd�Zd!dd�Zd dd�Zedefdd��Zedefdd��Z�ZS)"�BitNetHfQuantizerz�9    1.58-bit quantization from BitNet quantization method:10    Before loading: it converts the linear layers into BitLinear layers during loading.11 12    Check out the paper introducing this method: https://huggingface.co/papers/2402.1776413    FTZ14acceleratecst�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_bitnet.pyr-s15zBitNetHfQuantizer.__init__cOs�t�std��|�dd�s|�dd�rtd��tj��s#t�d�dS|�d�}|dur3t�d�dS|durLt	|t16�rNd	|��vsHd17|��vrPtd��dSdSdS)NzOLoading a BitNet quantized model requires accelerate (`pip install accelerate`)Zfrom_tfFZ	from_flaxztLoading ternary weights from tf/flax is currently not supported, please make sure the weights are in PyTorch format.zhYou don't have a GPU available to load the model, the inference will be slow because of weight unpacking�18device_mapz�You have loaded a BitNet 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 a BitNet 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	�ImportError�get�19ValueError�torch�cudaZis_available�loggerZwarning_once�20isinstance�dict�values)r�argsrrrrr�validate_environment1s.�21�22�"��z&BitNetHfQuantizer.validate_environment�modelrcKs|Sr
r)rr$rrrr�#_process_model_after_weight_loadingN�z5BitNetHfQuantizer._process_model_after_weight_loadingN�keep_in_fp32_modulescKs:ddlm}|�||jj|�|_|||j|j|jd�}dS)Nr)�replace_with_bitnet_linear)�modules_to_not_convertr�
pre_quantized)Zintegrationsr(Zget_modules_to_not_convertrr)r*)rr$r'rr(rrr�$_process_model_before_weight_loadingQs23�24�z6BitNetHfQuantizer._process_model_before_weight_loading�25max_memory�returncCsdd�|��D�}|S)NcSsi|]	\}}||d�qS)g�������?r)�.0�key�valrrr�26<dictcomp>esz7BitNetHfQuantizer.adjust_max_memory.<locals>.<dictcomp>)�items)rr,rrr�adjust_max_memorydsz#BitNetHfQuantizer.adjust_max_memory�target_dtype�torch.dtypecCs27tj}|Sr
)rZint8)rr4rrr�adjust_target_dtypehsz%BitNetHfQuantizer.adjust_target_dtypecCsdS)NTr)rZsafe_serializationrrr�is_serializablelr&z!BitNetHfQuantizer.is_serializablecC�|jjdko|jjdkS)N�
autobitlinear�online�rZlinear_classZquantization_mode�rrrr�is_trainableos28�zBitNetHfQuantizer.is_trainablecCr8)zUFlag indicating whether the quantized model can carry out quantization aware trainingr9r:r;r<rrr�is_qat_trainablevs29�z"BitNetHfQuantizer.is_qat_trainable)r$rr
)r4r5r-r5)�__name__�30__module__�__qualname__�__doc__Z requires_parameters_quantizationZrequires_calibrationZrequired_packagesrr#r%r�list�strr+r r�intr3r6r7�property�boolr=r>�
__classcell__rrrrr s*31��3233�23435r)�typingrrr�baserZmodeling_utilsr�utilsr	r36rrZ37get_loggerr?rrrrrr�<module>s
38
Aluode/PerceptionLabPortable · CoolFace