
    bi3+                         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  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)PreTrainedModelN)	HQQLinearc                 D    t          j        d| j        | j                  S )Nr   )dtypedevice)torchemptycompute_dtyper   selfs    `/root/projects/butler/venv/lib/python3.11/site-packages/transformers/quantizers/quantizer_hqq.pyweightr   $   s    {1D$6t{KKKK    c                   |     e Zd ZdZdZ 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().
    Fc                     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   quantization_configkwargs	__class__s      r   r   zHqqHfQuantizer.__init__5   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_environment@   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   r6   r7   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:    )rF   rH   	hqq_layers    r   <lambda>z:HqqHfQuantizer._patch_layer_for_multigpu.<locals>.<lambda>   s    &9&9)Q&G&G r   )forward)r   rK   rH   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   r6   r$   s      r   $_process_model_before_weight_loadingz3HqqHfQuantizer._process_model_before_weight_loading   s     'u$BZ[[[r   c                 F    d|_         |                                 |_        |S NT)is_hqq_quantizedis_serializableis_hqq_serializablerP   s      r   #_process_model_after_weight_loadingz2HqqHfQuantizer._process_model_after_weight_loading   s#    !%$($8$8$:$:!r   c                     dS rS   rJ   r   s    r   rU   zHqqHfQuantizer.is_serializable  s    tr   c                     dS rS   rJ   r   s    r   is_trainablezHqqHfQuantizer.is_trainable  s    tr   )r6   r   )__name__
__module____qualname____doc__requires_calibrationr   r5   strboolr?   rN   rQ   rW   rU   propertyrZ   __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   r   hqq.core.quantizer   rb   r   
get_loggerr[   r+   r   rJ   r   r   <module>rl      s@   !           1 1 1 1 1 1 A A A A A A A A A A       2 2 2 2 2 2  1000000  LLL 
++++++
 L L XL I		H	%	%Y Y Y Y Y[ Y Y Y Y Yr   