
    i+                         d dl mZ ddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ erddlmZ dd	lmZ  e       rd d
lZ e       rd dlmZ ed        Zee_         ej*                  e      Z G d de	      Zy
)    )TYPE_CHECKING   )prepare_for_hqq_linear)is_hqq_availableis_torch_availablelogging   )HfQuantizer)get_module_from_name)PreTrainedModel)	HqqConfigN)	HQQLinearc                 Z    t        j                  d| j                  | j                        S )Nr   )dtypedevice)torchemptycompute_dtyper   selfs    v/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/quantizers/quantizer_hqq.pyweightr   %   s    {{1D$6$6t{{KK    c                        e Zd ZU dZdZded<    fdZd Zddd	ed
e	fdZ
d Z	 	 ddZddZd Zed
e	fd       Z xZS )HqqHfQuantizerz
    HQQ quantizer base HF class.
    nn.Linear modules are first tagged with quant_config in _process_model_before_weight_loading().
    Fr   quantization_configc                     t               st        d      t        |   |fi | d | _        d| _        t        d d       j                         dhz
  | _        y )NzA valid HQQ version (>=0.2.1) is not available. Please follow the instructions to install it: `https://github.com/mobiusml/hqq/`.Fbias)	r   ImportErrorsuper__init__r   using_multi_gpur   state_dict_keyshqq_keys)r   r   kwargs	__class__s      r   r!   zHqqHfQuantizer.__init__7   s^    ! T  	,77
$!$-==?6(Jr   c                    | j                   9d|v r|d   | _         n*t        j                  | _         t        j	                  d       |j                  d      }t        |t              rZd|j                         v sd|j                         v rt        d      t        t        |j                                     dkD  | _        y y )Nr   zOSetting dtype to torch.float32 as the default value since it was not specified.
device_mapcpudiskzYou are attempting to use an HQQ 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	   )r   r   float32loggerinfoget
isinstancedictvalues
ValueErrorlensetr"   )r   argsr%   r(   s       r   validate_environmentz#HqqHfQuantizer.validate_environmentB   s    ::& #G_
"]]
mnZZ-
j$'
))++v9J9J9L/L h 
 (+3z/@/@/B+C'Dq'H$ (r   modelr   
param_namereturnc                 h    t        ||      \  }}t        |t        j                  j                        S N)r   r/   r   nnLinear)r   r7   r8   r%   module_s         r   param_needs_quantizationz'HqqHfQuantizer.param_needs_quantization   s)    (
;	 &%((//22r   c                 &    d fd_         S )Nc                     t        j                  |j                  | j                        | j	                         j                               }| j                  || j                  z  }|S r;   )r   matmultor   
dequantizetr   )r   xouts      r   forward_with_devicezEHqqHfQuantizer._patch_layer_for_multigpu.<locals>.forward_with_device   sL    ,,qttDKK0$//2C2E2E2GHCyy$tyy Jr   c                      |       S r;    )rG   rI   	hqq_layers    r   <lambda>z:HqqHfQuantizer._patch_layer_for_multigpu.<locals>.<lambda>   s    &9)Q&G r   )forward)r   rL   rI   s    `@r   _patch_layer_for_multigpuz(HqqHfQuantizer._patch_layer_for_multigpu   s    	 H	r   c                 2    t        || j                        }y )N)r   )r   r   r   r7   r%   s      r   $_process_model_before_weight_loadingz3HqqHfQuantizer._process_model_before_weight_loading   s     'u$BZBZ[r   c                 V    t        |dd       t        |d| j                                |S )Nis_hqq_quantizedTis_hqq_serializable)setattris_serializablerQ   s      r   #_process_model_after_weight_loadingz2HqqHfQuantizer._process_model_after_weight_loading   s*    )40,d.B.B.DEr   c                      yNTrK   r   s    r   rW   zHqqHfQuantizer.is_serializable  s    r   c                      yrZ   rK   r   s    r   is_trainablezHqqHfQuantizer.is_trainable  s    r   )r7   r   )__name__
__module____qualname____doc__requires_calibration__annotations__r!   r6   strboolr@   rO   rR   rX   rW   propertyr\   __classcell__)r&   s   @r   r   r   .   sy    
 !$$	KI`3.? 3S 3_c 3r\ \
 d  r   r   )typingr   integrationsr   utilsr   r   r   baser
   quantizers_utilsr   modeling_utilsr   utils.quantization_configr   r   hqq.core.quantizer   re   r   
get_loggerr]   r,   r   rK   r   r   <module>rp      sx    ! 1 A A  2 05 +
 L L I			H	%Z[ Zr   