
    }-j+                        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d
S )    )TYPE_CHECKING   )prepare_for_hqq_linear)is_hqq_availableis_torch_availablelogging   )HfQuantizer)get_module_from_name)PreTrainedModel)	HqqConfigN)	HQQLinearc                 D    t          j        d| j        | j                  S )Nr   )dtypedevice)torchemptycompute_dtyper   selfs    e/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/transformers/quantizers/quantizer_hqq.pyweightr   %   s    {1D$6t{KK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                      j        |fi | d | _        d| _        t          d d                                           dhz
  | _        d S )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777
$!$--==??6(Jr   c                    | j         =d|v r|d         | _         n+t          j        | _         t                              d           |                    d          }t          |t                    rtd|                                v sd|                                v rt          d          t          t          |                                                    dk    | _        d S d S )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nnZZ--
j$'' 	I
))++++v9J9J9L9L/L/L h  
 (+3z/@/@/B/B+C+C'D'Dq'H$$$	I 	Ir   modelr   
param_namereturnc                 f    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22r   c                 &    d fd_         S )Nc                     t          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   sP    ,qttDK00$//2C2C2E2E2G2GHHCy$ty Jr   c                      |           S r;    )rG   rI   	hqq_layers    r   <lambda>z:HqqHfQuantizer._patch_layer_for_multigpu.<locals>.<lambda>   s    &9&9)Q&G&G r   )forward)r   rL   rI   s    `@r   _patch_layer_for_multigpuz(HqqHfQuantizer._patch_layer_for_multigpu   s0    	 	 	 HGGGG	r   c                 2    t          || j                  }d S )N)r   )r   r   r   r7   r%   s      r   $_process_model_before_weight_loadingz3HqqHfQuantizer._process_model_before_weight_loading   s     'u$BZ[[[r   c                 n    t          |dd           t          |d|                                            |S )Nis_hqq_quantizedTis_hqq_serializable)setattris_serializablerQ   s      r   #_process_model_after_weight_loadingz2HqqHfQuantizer._process_model_after_weight_loading   s9    )4000,d.B.B.D.DEEEr   c                     dS NTrK   r   s    r   rW   zHqqHfQuantizer.is_serializable  s    tr   c                     dS rZ   rK   r   s    r   is_trainablezHqqHfQuantizer.is_trainable  s    tr   )r7   r   )__name__
__module____qualname____doc__requires_calibration__annotations__r!   r6   strboolr@   rO   rR   rX   rW   propertyr\   __classcell__)r&   s   @r   r   r   .   s         
 !$$$$	K 	K 	K 	K 	KI I I`3.? 3S 3_c 3 3 3 3r  \ \ \ \ \   
   d    X    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      sR   !           1 1 1 1 1 1 A A A A A A A A A A       2 2 2 2 2 2  6000000555555  LLL 
++++++
 L L XL I		H	%	%Z Z Z Z Z[ Z Z Z Z Zr   