o
    i                     @   s   d dl 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 e r3d d	lZeeZG d
d deZd	S )    )TYPE_CHECKING   )HfQuantizer)get_module_from_name   )PreTrainedModel)is_accelerate_availableis_optimum_quanto_availableis_torch_availablelogging)QuantoConfigNc                       s   e Zd ZdZdZdef fddZdd Zdd	d
ede	fddZ
deeeeB f deeeeB f fddZdd	d
edddef fddZdddZede	fddZdd Zdd Z  ZS )QuantoHfQuantizerz*
    Quantizer for the quanto library
    Fquantization_configc                    s8   t  j|fi | ddddd}|| jjd | _d S )Nr   g      ?g      ?)int8float8int4int2)super__init__getr   weightsquantized_param_size)selfr   kwargsmap_to_param_size	__class__ j/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/transformers/quantizers/quantizer_quanto.pyr   -   s   zQuantoHfQuantizer.__init__c                 O   st   t  stdt std|d}t|tr.t|dkr$d| v s*d| v r.td| j	j
d ur8tdd S )	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`)
device_mapr   cpudiskzYou are attempting to load an model with a device_map that contains a CPU or disk device.This is not supported with quanto when the model is quantized on the fly. Please remove the CPU or disk device from the device_map.zWe 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.)r	   ImportErrorr   r   
isinstancedictlenvalues
ValueErrorr   activations)r   argsr   r   r   r   r   validate_environment7   s&   

$z&QuantoHfQuantizer.validate_environmentmodelr   
param_namereturnc                 K   s8   ddl m} t||\}}t||rd|v r|j S dS )Nr   )QModuleMixinweightF)optimum.quantor.   r   r#   frozen)r   r+   r,   r   r.   moduletensor_namer   r   r   param_needs_quantizationN   s
   z*QuantoHfQuantizer.param_needs_quantization
max_memoryc                 C   s   dd |  D }|S )Nc                 S   s   i | ]	\}}||d  qS )g?r   ).0keyvalr   r   r   
<dictcomp>Z   s    z7QuantoHfQuantizer.adjust_max_memory.<locals>.<dictcomp>)items)r   r5   r   r   r   adjust_max_memoryY   s   z#QuantoHfQuantizer.adjust_max_memoryparamztorch.Tensorc                    s,   |  ||r| jdur| jS t |||S )z4Return the element size (in bytes) for `param_name`.N)r4   r   r   param_element_size)r   r+   r,   r<   r   r   r   r=   ]   s   z$QuantoHfQuantizer.param_element_sizec                 K   s8   ddl m} | || jj|j| _||| j| jd}d S )Nr   )replace_with_quanto_layers)modules_to_not_convertr   )integrationsr>   get_modules_to_not_convertr   r?   _keep_in_fp32_modules)r   r+   r   r>   r   r   r   $_process_model_before_weight_loadingd   s   

z6QuantoHfQuantizer._process_model_before_weight_loadingc                 C      dS )NTr   r   r   r   r   is_trainableo   s   zQuantoHfQuantizer.is_trainablec                 C   rD   )NFr   rE   r   r   r   is_serializables   s   z!QuantoHfQuantizer.is_serializablec                 C   s   ddl m} || S )Nr   )QuantoQuantize)integrations.quantorH   )r   rH   r   r   r   get_quantize_opsv   s   z"QuantoHfQuantizer.get_quantize_ops)r+   r   )__name__
__module____qualname____doc__requires_calibrationr   r   r*   strboolr4   r$   intr;   floatr=   rC   propertyrF   rG   rJ   __classcell__r   r   r   r   r   &   s    
*
r   )typingr   baser   quantizers_utilsr   modeling_utilsr   utilsr   r	   r
   r   utils.quantization_configr   torch
get_loggerrK   loggerr   r   r   r   r   <module>   s   
