
    i0                         d dl mZ 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 dd	lmZ  e       rd d
lZ G d de      Zy
)    )TYPE_CHECKING   )is_fouroversix_available   )HfQuantizer)get_module_from_name)PreTrainedModel)FourOverSixConfig)is_torch_availableNc                        e Zd ZU dZdZded<    fdZd Zddd	ed
dde	f fdZ
ddd	edefdZ	 	 ddZddZd Zedefd       Zd Zd Z xZS )FourOverSixHfQuantizerz,
    FP4 quantization with fouroversix.
    Fr
   quantization_configc                 &    t        |   |fi | y N)super__init__)selfr   kwargs	__class__s      ~/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/quantizers/quantizer_fouroversix.pyr   zFourOverSixHfQuantizer.__init__   s    ,77    c                 .    t               st        d      y )NzXUsing `fouroversix` requires fouroversix: `pip install fouroversix --no-build-isolation`)r   ImportError)r   argsr   s      r   validate_environmentz+FourOverSixHfQuantizer.validate_environment    s    ')j  *r   modelr	   
param_nameparamztorch.Tensorreturnc                     ddl m} t        ||      \  }}|j                  t	        |            r|j                  |      S t        |   |||      S Nr   )QuantizedModule)fouroversixr"   r   is_quantized_module_typetypeget_element_sizer   param_element_size)r   r   r   r   r"   moduletensor_namer   s          r   r'   z)FourOverSixHfQuantizer.param_element_size&   sP     	025*E33DLA**;77w)%UCCr   c                     ddl m} t        ||      \  }}|j                  t	        |            xr ||j
                  v S r!   )r#   r"   r   r$   r%   parameters_to_quantize)r   r   r   r   r"   r(   r)   s          r   param_needs_quantizationz/FourOverSixHfQuantizer.param_needs_quantization5   s>     	025*E77VEv+Y_YvYvJvvr   c                 :   ddl m}m} ddlm}  || || j
                               | j                  rh| j
                  j                  sQ|j                         D ]=  \  }}|j                  t        |            s!|j                  D ]  }	t        ||	        ? y y y )Nr   )r"   quantize_modelr   )adapt_fouroversix_config)r#   r"   r.   integrations.fouroversixr/   r   pre_quantizedkeep_master_weightsnamed_modulesr$   r%   r+   delattr)
r   r   
device_mapr   r"   r.   r/   _r(   parameter_names
             r   $_process_model_before_weight_loadingz;FourOverSixHfQuantizer._process_model_before_weight_loadingA   s     	@G$T%=%=>	
 d&>&>&R&R"002 8	6";;DLI*0*G*G 8788 'Sr   c                     |S r    )r   r   r   s      r   #_process_model_after_weight_loadingz:FourOverSixHfQuantizer._process_model_after_weight_loadingX   s    r   c                      y)NTr:   r   s    r   is_serializablez&FourOverSixHfQuantizer.is_serializable[   s    r   c                 .    | j                   j                  S r   )r   r2   r=   s    r   is_trainablez#FourOverSixHfQuantizer.is_trainable^   s    '';;;r   c                     ddl m}  ||       S )Nr   )FourOverSixQuantize)r0   rB   )r   rB   s     r   get_quantize_opsz'FourOverSixHfQuantizer.get_quantize_opsb   s    B"4((r   c                     ddl m} t        | j                  d      r)| j                  j                  }|j                  |      }|S g S )a  
        Return weight conversions for loading pre-quantized checkpoints of
        other pre-quantized models (not fouroversix models). After first use,
        the pre_quantized_model_config_type attribute is set to None to ensure
        subsequent calls (e.g., during save_pretrained) return an empty list
        since, by then, the model will be saved with our framework's format
        so weight conversions are no longer needed.
        r   )WeightConversionspre_quantized_model_config_type)r#   rE   hasattrr   rF   get_weight_conversions)r   rE   model_config_typeweight_conversionss       r   rH   z-FourOverSixHfQuantizer.get_weight_conversionsg   sM     	2 4++-NO $ 8 8 X X!2!I!I!" &%	r   )r   r	   )__name__
__module____qualname____doc__requires_calibration__annotations__r   r   strfloatr'   boolr,   r8   r;   r>   propertyr@   rC   rH   __classcell__)r   s   @r   r   r      s     !,,8D D D 	D
 
D
w 
w 
w
 

w8 8. <d < <)
r   r   )typingr   utils.import_utilsr   baser   quantizers_utilsr   modeling_utilsr	   utils.quantization_configr
   utilsr   torchr   r:   r   r   <module>r^      s:      9  2 0=
 f[ fr   